Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 lines
4.9 KiB

  1. #
  2. # $Id: Escape.pm,v 3.19 2001/08/24 17:25:43 gisle Exp $
  3. #
  4. package URI::Escape;
  5. use strict;
  6. =head1 NAME
  7. URI::Escape - Escape and unescape unsafe characters
  8. =head1 SYNOPSIS
  9. use URI::Escape;
  10. $safe = uri_escape("10% is enough\n");
  11. $verysafe = uri_escape("foo", "\0-\377");
  12. $str = uri_unescape($safe);
  13. =head1 DESCRIPTION
  14. This module provides functions to escape and unescape URI strings as
  15. defined by RFC 2396 (and updated by RFC 2732).
  16. URIs consist of a restricted set of characters,
  17. denoted as C<uric> in RFC 2396. The restricted set of characters
  18. consists of digits, letters, and a few graphic symbols chosen from
  19. those common to most of the character encodings and input facilities
  20. available to Internet users:
  21. "A" .. "Z", "a" .. "z", "0" .. "9",
  22. ";", "/", "?", ":", "@", "&", "=", "+", "$", ",", "[", "]", # reserved
  23. "-", "_", ".", "!", "~", "*", "'", "(", ")"
  24. In addition any byte (octet) can be represented in a URI by an escape
  25. sequence; a triplet consisting of the character "%" followed by two
  26. hexadecimal digits. Bytes can also be represented directly by a
  27. character using the US-ASCII character for that octet (iff the
  28. character is part of C<uric>).
  29. Some of the C<uric> characters are I<reserved> for use as delimiters
  30. or as part of certain URI components. These must be escaped if they are
  31. to be treated as ordinary data. Read RFC 2396 for further details.
  32. The functions provided (and exported by default) from this module are:
  33. =over 4
  34. =item uri_escape($string, [$unsafe])
  35. This function replaces all unsafe characters in the $string with their
  36. escape sequences and returns the result.
  37. The uri_escape() function takes an optional second argument that
  38. overrides the set of characters that are to be escaped. The set is
  39. specified as a string that can be used in a regular expression
  40. character class (between [ ]). E.g.:
  41. "\x00-\x1f\x7f-\xff" # all control and hi-bit characters
  42. "a-z" # all lower case characters
  43. "^A-Za-z" # everything not a letter
  44. The default set of characters to be escaped is all those which are
  45. I<not> part of the C<uric> character class shown above as well as the
  46. reserved characters. I.e. the default is:
  47. "^A-Za-z0-9\-_.!~*'()"
  48. =item uri_unescape($string,...)
  49. Returns a string with all %XX sequences replaced with the actual byte
  50. (octet).
  51. This does the same as:
  52. $string =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
  53. but does not modify the string in-place as this RE would. Using the
  54. uri_unescape() function instead of the RE might make the code look
  55. cleaner and is a few characters less to type.
  56. In a simple benchmark test I made I got something like 40% slowdown by
  57. calling the function (instead of the inline RE above) if a few chars
  58. where unescaped and something like 700% slowdown if none where. If
  59. you are going to unescape a lot of times it might be a good idea to
  60. inline the RE.
  61. If the uri_unescape() function is passed multiple strings, then each
  62. one is unescaped returned.
  63. =back
  64. The module can also export the C<%escapes> hash which contains the
  65. mapping from all 256 bytes to the corresponding escape code. Lookup
  66. in this hash is faster than evaluating C<sprintf("%%%02X", ord($byte))>
  67. each time.
  68. =head1 SEE ALSO
  69. L<URI>
  70. =head1 COPYRIGHT
  71. Copyright 1995-2001 Gisle Aas.
  72. This program is free software; you can redistribute it and/or modify
  73. it under the same terms as Perl itself.
  74. =cut
  75. use vars qw(@ISA @EXPORT @EXPORT_OK $VERSION);
  76. use vars qw(%escapes);
  77. require Exporter;
  78. @ISA = qw(Exporter);
  79. @EXPORT = qw(uri_escape uri_unescape);
  80. @EXPORT_OK = qw(%escapes);
  81. $VERSION = sprintf("%d.%02d", q$Revision: 3.19 $ =~ /(\d+)\.(\d+)/);
  82. use Carp ();
  83. # Build a char->hex map
  84. for (0..255) {
  85. $escapes{chr($_)} = sprintf("%%%02X", $_);
  86. }
  87. my %subst; # compiled patternes
  88. sub uri_escape
  89. {
  90. my($text, $patn) = @_;
  91. return undef unless defined $text;
  92. if (defined $patn){
  93. unless (exists $subst{$patn}) {
  94. # Because we can't compile the regex we fake it with a cached sub
  95. (my $tmp = $patn) =~ s,/,\\/,g;
  96. $subst{$patn} =
  97. eval "sub {\$_[0] =~ s/([$tmp])/\$escapes{\$1}/g; }";
  98. Carp::croak("uri_escape: $@") if $@;
  99. }
  100. &{$subst{$patn}}($text);
  101. } else {
  102. # Default unsafe characters. RFC 2732 ^(uric - reserved)
  103. $text =~ s/([^A-Za-z0-9\-_.!~*'()])/$escapes{$1}/g;
  104. }
  105. $text;
  106. }
  107. sub uri_unescape
  108. {
  109. # Note from RFC1630: "Sequences which start with a percent sign
  110. # but are not followed by two hexadecimal characters are reserved
  111. # for future extension"
  112. my $str = shift;
  113. if (@_ && wantarray) {
  114. # not executed for the common case of a single argument
  115. my @str = ($str, @_); # need to copy
  116. foreach (@str) {
  117. s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg
  118. }
  119. return @str;
  120. }
  121. $str =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
  122. $str;
  123. }
  124. 1;