home *** CD-ROM | disk | FTP | other *** search
/ Netrunner 2004 October / NETRUNNER0410.ISO / regular / ActivePerl-5.8.4.810-MSWin32-x86.msi / _333d9799ab9e5055732a7e82dff1a662 < prev    next >
Text File  |  2004-06-01  |  5KB  |  166 lines

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