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.

258 lines
8.7 KiB

  1. package CGI::Util;
  2. use strict;
  3. use vars '$VERSION','@EXPORT_OK','@ISA','$EBCDIC','@A2E','@E2A';
  4. require Exporter;
  5. @ISA = qw(Exporter);
  6. @EXPORT_OK = qw(rearrange make_attributes unescape escape expires);
  7. $VERSION = '1.1';
  8. $EBCDIC = "\t" ne "\011";
  9. if ($EBCDIC) {
  10. # (ord('^') == 95) for codepage 1047 as on os390, vmesa
  11. @A2E = (
  12. 0, 1, 2, 3, 55, 45, 46, 47, 22, 5, 21, 11, 12, 13, 14, 15,
  13. 16, 17, 18, 19, 60, 61, 50, 38, 24, 25, 63, 39, 28, 29, 30, 31,
  14. 64, 90,127,123, 91,108, 80,125, 77, 93, 92, 78,107, 96, 75, 97,
  15. 240,241,242,243,244,245,246,247,248,249,122, 94, 76,126,110,111,
  16. 124,193,194,195,196,197,198,199,200,201,209,210,211,212,213,214,
  17. 215,216,217,226,227,228,229,230,231,232,233,173,224,189, 95,109,
  18. 121,129,130,131,132,133,134,135,136,137,145,146,147,148,149,150,
  19. 151,152,153,162,163,164,165,166,167,168,169,192, 79,208,161, 7,
  20. 32, 33, 34, 35, 36, 37, 6, 23, 40, 41, 42, 43, 44, 9, 10, 27,
  21. 48, 49, 26, 51, 52, 53, 54, 8, 56, 57, 58, 59, 4, 20, 62,255,
  22. 65,170, 74,177,159,178,106,181,187,180,154,138,176,202,175,188,
  23. 144,143,234,250,190,160,182,179,157,218,155,139,183,184,185,171,
  24. 100,101, 98,102, 99,103,158,104,116,113,114,115,120,117,118,119,
  25. 172,105,237,238,235,239,236,191,128,253,254,251,252,186,174, 89,
  26. 68, 69, 66, 70, 67, 71,156, 72, 84, 81, 82, 83, 88, 85, 86, 87,
  27. 140, 73,205,206,203,207,204,225,112,221,222,219,220,141,142,223
  28. );
  29. @E2A = (
  30. 0, 1, 2, 3,156, 9,134,127,151,141,142, 11, 12, 13, 14, 15,
  31. 16, 17, 18, 19,157, 10, 8,135, 24, 25,146,143, 28, 29, 30, 31,
  32. 128,129,130,131,132,133, 23, 27,136,137,138,139,140, 5, 6, 7,
  33. 144,145, 22,147,148,149,150, 4,152,153,154,155, 20, 21,158, 26,
  34. 32,160,226,228,224,225,227,229,231,241,162, 46, 60, 40, 43,124,
  35. 38,233,234,235,232,237,238,239,236,223, 33, 36, 42, 41, 59, 94,
  36. 45, 47,194,196,192,193,195,197,199,209,166, 44, 37, 95, 62, 63,
  37. 248,201,202,203,200,205,206,207,204, 96, 58, 35, 64, 39, 61, 34,
  38. 216, 97, 98, 99,100,101,102,103,104,105,171,187,240,253,254,177,
  39. 176,106,107,108,109,110,111,112,113,114,170,186,230,184,198,164,
  40. 181,126,115,116,117,118,119,120,121,122,161,191,208, 91,222,174,
  41. 172,163,165,183,169,167,182,188,189,190,221,168,175, 93,180,215,
  42. 123, 65, 66, 67, 68, 69, 70, 71, 72, 73,173,244,246,242,243,245,
  43. 125, 74, 75, 76, 77, 78, 79, 80, 81, 82,185,251,252,249,250,255,
  44. 92,247, 83, 84, 85, 86, 87, 88, 89, 90,178,212,214,210,211,213,
  45. 48, 49, 50, 51, 52, 53, 54, 55, 56, 57,179,219,220,217,218,159
  46. );
  47. if (ord('^') == 106) { # as in the BS2000 posix-bc coded character set
  48. $A2E[91] = 187; $A2E[92] = 188; $A2E[94] = 106; $A2E[96] = 74;
  49. $A2E[123] = 251; $A2E[125] = 253; $A2E[126] = 255; $A2E[159] = 95;
  50. $A2E[162] = 176; $A2E[166] = 208; $A2E[168] = 121; $A2E[172] = 186;
  51. $A2E[175] = 161; $A2E[217] = 224; $A2E[219] = 221; $A2E[221] = 173;
  52. $A2E[249] = 192;
  53. $E2A[74] = 96; $E2A[95] = 159; $E2A[106] = 94; $E2A[121] = 168;
  54. $E2A[161] = 175; $E2A[173] = 221; $E2A[176] = 162; $E2A[186] = 172;
  55. $E2A[187] = 91; $E2A[188] = 92; $E2A[192] = 249; $E2A[208] = 166;
  56. $E2A[221] = 219; $E2A[224] = 217; $E2A[251] = 123; $E2A[253] = 125;
  57. $E2A[255] = 126;
  58. }
  59. elsif (ord('^') == 176) { # as in codepage 037 on os400
  60. $A2E[10] = 37; $A2E[91] = 186; $A2E[93] = 187; $A2E[94] = 176;
  61. $A2E[133] = 21; $A2E[168] = 189; $A2E[172] = 95; $A2E[221] = 173;
  62. $E2A[21] = 133; $E2A[37] = 10; $E2A[95] = 172; $E2A[173] = 221;
  63. $E2A[176] = 94; $E2A[186] = 91; $E2A[187] = 93; $E2A[189] = 168;
  64. }
  65. }
  66. # Smart rearrangement of parameters to allow named parameter
  67. # calling. We do the rearangement if:
  68. # the first parameter begins with a -
  69. sub rearrange {
  70. my($order,@param) = @_;
  71. return () unless @param;
  72. if (ref($param[0]) eq 'HASH') {
  73. @param = %{$param[0]};
  74. } else {
  75. return @param
  76. unless (defined($param[0]) && substr($param[0],0,1) eq '-');
  77. }
  78. # map parameters into positional indices
  79. my ($i,%pos);
  80. $i = 0;
  81. foreach (@$order) {
  82. foreach (ref($_) eq 'ARRAY' ? @$_ : $_) { $pos{lc($_)} = $i; }
  83. $i++;
  84. }
  85. my (@result,%leftover);
  86. $#result = $#$order; # preextend
  87. while (@param) {
  88. my $key = lc(shift(@param));
  89. $key =~ s/^\-//;
  90. if (exists $pos{$key}) {
  91. $result[$pos{$key}] = shift(@param);
  92. } else {
  93. $leftover{$key} = shift(@param);
  94. }
  95. }
  96. push (@result,make_attributes(\%leftover,1)) if %leftover;
  97. @result;
  98. }
  99. sub make_attributes {
  100. my $attr = shift;
  101. return () unless $attr && ref($attr) && ref($attr) eq 'HASH';
  102. my $escape = shift || 0;
  103. my(@att);
  104. foreach (keys %{$attr}) {
  105. my($key) = $_;
  106. $key=~s/^\-//; # get rid of initial - if present
  107. $key=~tr/A-Z_/a-z-/; # parameters are lower case, use dashes
  108. my $value = $escape ? simple_escape($attr->{$_}) : $attr->{$_};
  109. push(@att,defined($attr->{$_}) ? qq/$key="$value"/ : qq/$key/);
  110. }
  111. return @att;
  112. }
  113. sub simple_escape {
  114. return unless defined(my $toencode = shift);
  115. $toencode =~ s{&}{&}gso;
  116. $toencode =~ s{<}{&lt;}gso;
  117. $toencode =~ s{>}{&gt;}gso;
  118. $toencode =~ s{\"}{&quot;}gso;
  119. # Doesn't work. Can't work. forget it.
  120. # $toencode =~ s{\x8b}{&#139;}gso;
  121. # $toencode =~ s{\x9b}{&#155;}gso;
  122. $toencode;
  123. }
  124. # unescape URL-encoded data
  125. sub unescape {
  126. shift() if ref($_[0]) || (defined $_[1] && $_[0] eq $CGI::DefaultClass);
  127. my $todecode = shift;
  128. return undef unless defined($todecode);
  129. $todecode =~ tr/+/ /; # pluses become spaces
  130. $EBCDIC = "\t" ne "\011";
  131. if ($EBCDIC) {
  132. $todecode =~ s/%([0-9a-fA-F]{2})/chr $A2E[hex($1)]/ge;
  133. } else {
  134. $todecode =~ s/%([0-9a-fA-F]{2})/chr hex($1)/ge;
  135. }
  136. return $todecode;
  137. }
  138. # URL-encode data
  139. sub escape {
  140. shift() if ref($_[0]) || (defined $_[1] && $_[0] eq $CGI::DefaultClass);
  141. my $toencode = shift;
  142. return undef unless defined($toencode);
  143. if ($EBCDIC) {
  144. $toencode=~s/([^a-zA-Z0-9_.-])/uc sprintf("%%%02x",$E2A[ord($1)])/eg;
  145. } else {
  146. $toencode=~s/([^a-zA-Z0-9_.-])/uc sprintf("%%%02x",ord($1))/eg;
  147. }
  148. return $toencode;
  149. }
  150. # This internal routine creates date strings suitable for use in
  151. # cookies and HTTP headers. (They differ, unfortunately.)
  152. # Thanks to Mark Fisher for this.
  153. sub expires {
  154. my($time,$format) = @_;
  155. $format ||= 'http';
  156. my(@MON)=qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/;
  157. my(@WDAY) = qw/Sun Mon Tue Wed Thu Fri Sat/;
  158. # pass through preformatted dates for the sake of expire_calc()
  159. $time = expire_calc($time);
  160. return $time unless $time =~ /^\d+$/;
  161. # make HTTP/cookie date string from GMT'ed time
  162. # (cookies use '-' as date separator, HTTP uses ' ')
  163. my($sc) = ' ';
  164. $sc = '-' if $format eq "cookie";
  165. my($sec,$min,$hour,$mday,$mon,$year,$wday) = gmtime($time);
  166. $year += 1900;
  167. return sprintf("%s, %02d$sc%s$sc%04d %02d:%02d:%02d GMT",
  168. $WDAY[$wday],$mday,$MON[$mon],$year,$hour,$min,$sec);
  169. }
  170. # This internal routine creates an expires time exactly some number of
  171. # hours from the current time. It incorporates modifications from
  172. # Mark Fisher.
  173. sub expire_calc {
  174. my($time) = @_;
  175. my(%mult) = ('s'=>1,
  176. 'm'=>60,
  177. 'h'=>60*60,
  178. 'd'=>60*60*24,
  179. 'M'=>60*60*24*30,
  180. 'y'=>60*60*24*365);
  181. # format for time can be in any of the forms...
  182. # "now" -- expire immediately
  183. # "+180s" -- in 180 seconds
  184. # "+2m" -- in 2 minutes
  185. # "+12h" -- in 12 hours
  186. # "+1d" -- in 1 day
  187. # "+3M" -- in 3 months
  188. # "+2y" -- in 2 years
  189. # "-3m" -- 3 minutes ago(!)
  190. # If you don't supply one of these forms, we assume you are
  191. # specifying the date yourself
  192. my($offset);
  193. if (!$time || (lc($time) eq 'now')) {
  194. $offset = 0;
  195. } elsif ($time=~/^\d+/) {
  196. return $time;
  197. } elsif ($time=~/^([+-]?(?:\d+|\d*\.\d*))([mhdMy]?)/) {
  198. $offset = ($mult{$2} || 1)*$1;
  199. } else {
  200. return $time;
  201. }
  202. return (time+$offset);
  203. }
  204. 1;
  205. __END__
  206. =head1 NAME
  207. CGI::Util - Internal utilities used by CGI module
  208. =head1 SYNOPSIS
  209. none
  210. =head1 DESCRIPTION
  211. no public subroutines
  212. =head1 AUTHOR INFORMATION
  213. Copyright 1995-1998, Lincoln D. Stein. All rights reserved.
  214. This library is free software; you can redistribute it and/or modify
  215. it under the same terms as Perl itself.
  216. Address bug reports and comments to: lstein@cshl.org. When sending
  217. bug reports, please provide the version of CGI.pm, the version of
  218. Perl, the name and version of your Web server, and the name and
  219. version of the operating system you are using. If the problem is even
  220. remotely browser dependent, please provide information about the
  221. affected browers as well.
  222. =head1 SEE ALSO
  223. L<CGI>
  224. =cut