home *** CD-ROM | disk | FTP | other *** search
/ CLIX - Fazer Clix Custa Nix / CLIX-CD.cdr / mac / lib / URI / Escape.pm next >
Text File  |  1997-11-18  |  2KB  |  92 lines

  1. #
  2. # $Id: Escape.pm,v 1.1 1997/11/18 00:33:39 neeri Exp $
  3. #
  4.  
  5. package URI::Escape;
  6.  
  7. =head1 NAME
  8.  
  9. uri_escape - Escape unsafe characters
  10.  
  11. uri_unescape - Unescape escaped characters
  12.  
  13. =head1 SYNOPSIS
  14.  
  15.  use URI::Escape;
  16.  $safe = uri_escape("10% is enough\n");
  17.  $verysafe = uri_escape("foo", "\0-\377");
  18.  $str  = uri_unescape($safe);
  19.  
  20. =head1 DESCRIPTION
  21.  
  22. This module provide functions to escape and unescape URI strings.
  23. Some characters are regarded as "unsafe" and must be escaped in
  24. accordance with RFC 1738.  Escaped characters are represented by a
  25. triplet consisting of the character "%" followed by two hexadecimal
  26. digits.
  27.  
  28. The uri_escape() function takes an optional second argument that
  29. overrides the set of characters that are to be escaped.  The set is
  30. specified as a string that can be used in a regular expression
  31. character class (between [ ]).  E.g.:
  32.  
  33.   \x00-\x1f\x7f-\xff          # all control and hi-bit characters
  34.   a-z                         # all lower case characters
  35.   ^A-Za-z                     # everything not a letter
  36.  
  37. The default set of characters to be escaped is:
  38.  
  39.   \x00-\x20"#%;<>?{}|\\\\^~`\[\]\x7F-\xFF
  40.  
  41. The module can also export the %escapes hash which contains the
  42. mapping from all characters to the corresponding escape code.
  43.  
  44. =head1 SEE ALSO
  45.  
  46. L<URI::URL>
  47.  
  48. =cut
  49.  
  50. require Exporter;
  51. @ISA = qw(Exporter);
  52. @EXPORT = qw(uri_escape uri_unescape);
  53. @EXPORT_OK = qw(%escapes);
  54.  
  55. use Carp ();
  56.  
  57. # Build a char->hex map
  58. for (0..255) {
  59.     $escapes{chr($_)} = sprintf("%%%02X", $_);
  60. }
  61.  
  62. sub uri_escape
  63. {
  64.     my($text, $patn) = @_;
  65.     return undef unless defined $text;
  66.     if (defined $patn){
  67.     unless (exists  $subst{$patn}) {
  68.         # Because we can't compile regex we fake it with a cached sub
  69.         $subst{$patn} =
  70.           eval "sub {\$_[0] =~ s/([$patn])/\$escapes{\$1}/g; }";
  71.         Carp::croak("uri_escape: $@") if $@;
  72.     }
  73.     &{$subst{$patn}}($text);
  74.     } else {
  75.     # Default unsafe characters. (RFC1738 section 2.2)
  76.     $text =~ s/([\x00-\x20"#%;<>?{}|\\\\^~`\[\]\x7F-\xFF])/$escapes{$1}/g; #"
  77.     }
  78.     $text;
  79. }
  80.  
  81. sub uri_unescape
  82. {
  83.     # Note from RFC1630:  "Sequences which start with a percent sign
  84.     # but are not followed by two hexadecimal characters are reserved
  85.     # for future extension"
  86.     my @copy = @_;
  87.     for (@copy) { s/%([\dA-Fa-f]{2})/chr(hex($1))/eg; }
  88.     wantarray ? @copy : $copy[0];
  89. }
  90.  
  91. 1;
  92.