← Index
NYTProf Performance Profile   « line view »
For /usr/local/libexec/sympa/task_manager-debug.pl
  Run on Tue Jun 1 22:32:51 2021
Reported on Tue Jun 1 22:35:10 2021

Filename/usr/local/lib/perl5/site_perl/CGI/Util.pm
StatementsExecuted 0 statements in 0s
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
0000s0sCGI::Util::::BEGIN@2CGI::Util::BEGIN@2
0000s0sCGI::Util::::BEGIN@4CGI::Util::BEGIN@4
0000s0sCGI::Util::::_rearrange_paramsCGI::Util::_rearrange_params
0000s0sCGI::Util::::ascii2ebcdicCGI::Util::ascii2ebcdic
0000s0sCGI::Util::::ebcdic2asciiCGI::Util::ebcdic2ascii
0000s0sCGI::Util::::escapeCGI::Util::escape
0000s0sCGI::Util::::expire_calcCGI::Util::expire_calc
0000s0sCGI::Util::::expiresCGI::Util::expires
0000s0sCGI::Util::::make_attributesCGI::Util::make_attributes
0000s0sCGI::Util::::rearrangeCGI::Util::rearrange
0000s0sCGI::Util::::rearrange_headerCGI::Util::rearrange_header
0000s0sCGI::Util::::simple_escapeCGI::Util::simple_escape
0000s0sCGI::Util::::unescapeCGI::Util::unescape
0000s0sCGI::Util::::utf8_chrCGI::Util::utf8_chr
Call graph for these subroutines as a Graphviz dot language file.
Line State
ments
Time
on line
Calls Time
in subs
Code
1package CGI::Util;
2use parent 'Exporter';
3require 5.008001;
4use strict;
5our @EXPORT_OK = qw(rearrange rearrange_header make_attributes unescape escape
6 expires ebcdic2ascii ascii2ebcdic);
7
8our $VERSION = '4.52';
9
10our $_EBCDIC = "\t" ne "\011";
11
12my $appease_cpants_kwalitee = q/
13use strict;
14use warnings;
15#/;
16
17# (ord('^') == 95) for codepage 1047 as on os390, vmesa
18our @A2E = (
19 0, 1, 2, 3, 55, 45, 46, 47, 22, 5, 21, 11, 12, 13, 14, 15,
20 16, 17, 18, 19, 60, 61, 50, 38, 24, 25, 63, 39, 28, 29, 30, 31,
21 64, 90,127,123, 91,108, 80,125, 77, 93, 92, 78,107, 96, 75, 97,
22 240,241,242,243,244,245,246,247,248,249,122, 94, 76,126,110,111,
23 124,193,194,195,196,197,198,199,200,201,209,210,211,212,213,214,
24 215,216,217,226,227,228,229,230,231,232,233,173,224,189, 95,109,
25 121,129,130,131,132,133,134,135,136,137,145,146,147,148,149,150,
26 151,152,153,162,163,164,165,166,167,168,169,192, 79,208,161, 7,
27 32, 33, 34, 35, 36, 37, 6, 23, 40, 41, 42, 43, 44, 9, 10, 27,
28 48, 49, 26, 51, 52, 53, 54, 8, 56, 57, 58, 59, 4, 20, 62,255,
29 65,170, 74,177,159,178,106,181,187,180,154,138,176,202,175,188,
30 144,143,234,250,190,160,182,179,157,218,155,139,183,184,185,171,
31 100,101, 98,102, 99,103,158,104,116,113,114,115,120,117,118,119,
32 172,105,237,238,235,239,236,191,128,253,254,251,252,186,174, 89,
33 68, 69, 66, 70, 67, 71,156, 72, 84, 81, 82, 83, 88, 85, 86, 87,
34 140, 73,205,206,203,207,204,225,112,221,222,219,220,141,142,223
35 );
36our @E2A = (
37 0, 1, 2, 3,156, 9,134,127,151,141,142, 11, 12, 13, 14, 15,
38 16, 17, 18, 19,157, 10, 8,135, 24, 25,146,143, 28, 29, 30, 31,
39 128,129,130,131,132,133, 23, 27,136,137,138,139,140, 5, 6, 7,
40 144,145, 22,147,148,149,150, 4,152,153,154,155, 20, 21,158, 26,
41 32,160,226,228,224,225,227,229,231,241,162, 46, 60, 40, 43,124,
42 38,233,234,235,232,237,238,239,236,223, 33, 36, 42, 41, 59, 94,
43 45, 47,194,196,192,193,195,197,199,209,166, 44, 37, 95, 62, 63,
44 248,201,202,203,200,205,206,207,204, 96, 58, 35, 64, 39, 61, 34,
45 216, 97, 98, 99,100,101,102,103,104,105,171,187,240,253,254,177,
46 176,106,107,108,109,110,111,112,113,114,170,186,230,184,198,164,
47 181,126,115,116,117,118,119,120,121,122,161,191,208, 91,222,174,
48 172,163,165,183,169,167,182,188,189,190,221,168,175, 93,180,215,
49 123, 65, 66, 67, 68, 69, 70, 71, 72, 73,173,244,246,242,243,245,
50 125, 74, 75, 76, 77, 78, 79, 80, 81, 82,185,251,252,249,250,255,
51 92,247, 83, 84, 85, 86, 87, 88, 89, 90,178,212,214,210,211,213,
52 48, 49, 50, 51, 52, 53, 54, 55, 56, 57,179,219,220,217,218,159
53 );
54
55if ($_EBCDIC && ord('^') == 106) { # as in the BS2000 posix-bc coded character set
56 $A2E[91] = 187; $A2E[92] = 188; $A2E[94] = 106; $A2E[96] = 74;
57 $A2E[123] = 251; $A2E[125] = 253; $A2E[126] = 255; $A2E[159] = 95;
58 $A2E[162] = 176; $A2E[166] = 208; $A2E[168] = 121; $A2E[172] = 186;
59 $A2E[175] = 161; $A2E[217] = 224; $A2E[219] = 221; $A2E[221] = 173;
60 $A2E[249] = 192;
61
62 $E2A[74] = 96; $E2A[95] = 159; $E2A[106] = 94; $E2A[121] = 168;
63 $E2A[161] = 175; $E2A[173] = 221; $E2A[176] = 162; $E2A[186] = 172;
64 $E2A[187] = 91; $E2A[188] = 92; $E2A[192] = 249; $E2A[208] = 166;
65 $E2A[221] = 219; $E2A[224] = 217; $E2A[251] = 123; $E2A[253] = 125;
66 $E2A[255] = 126;
67 }
68elsif ($_EBCDIC && ord('^') == 176) { # as in codepage 037 on os400
69 $A2E[10] = 37; $A2E[91] = 186; $A2E[93] = 187; $A2E[94] = 176;
70 $A2E[133] = 21; $A2E[168] = 189; $A2E[172] = 95; $A2E[221] = 173;
71
72 $E2A[21] = 133; $E2A[37] = 10; $E2A[95] = 172; $E2A[173] = 221;
73 $E2A[176] = 94; $E2A[186] = 91; $E2A[187] = 93; $E2A[189] = 168;
74}
75
76# Smart rearrangement of parameters to allow named parameter
77# calling. We do the rearrangement if:
78# the first parameter begins with a -
79
80sub rearrange {
81 my ($order,@param) = @_;
82 my ($result, $leftover) = _rearrange_params( $order, @param );
83 push @$result, make_attributes( $leftover, defined $CGI::Q ? $CGI::Q->{escape} : 1 )
84 if keys %$leftover;
85 @$result;
86}
87
88sub rearrange_header {
89 my ($order,@param) = @_;
90
91 my ($result,$leftover) = _rearrange_params( $order, @param );
92 push @$result, make_attributes( $leftover, 0, 1 ) if keys %$leftover;
93
94 @$result;
95}
96
97sub _rearrange_params {
98 my($order,@param) = @_;
99 return [] unless @param;
100
101 if (ref($param[0]) eq 'HASH') {
102 @param = %{$param[0]};
103 } else {
104 return \@param
105 unless (defined($param[0]) && substr($param[0],0,1) eq '-');
106 }
107
108 # map parameters into positional indices
109 my ($i,%pos);
110 $i = 0;
111 foreach (@$order) {
112 foreach (ref($_) eq 'ARRAY' ? @$_ : $_) { $pos{lc($_)} = $i; }
113 $i++;
114 }
115
116 my %params_as_hash = ( @param );
117
118 my (@result,%leftover);
119 $#result = $#$order; # preextend
120
121 foreach my $k (
122 # sort keys alphabetically but favour certain keys before others
123 # specifically for the case where there could be several options
124 # for a param key, but one should be preferred (see GH #155)
125 sort {
126 if ( $a =~ /content/i ) { return 1 }
127 elsif ( $b =~ /content/i ) { return -1 }
128 else { $a cmp $b }
129 }
130 keys( %params_as_hash )
131 ) {
132 my $key = lc($k);
133 $key =~ s/^\-//;
134 if (exists $pos{$key}) {
135 $result[$pos{$key}] = $params_as_hash{$k};
136 } else {
137 $leftover{$key} = $params_as_hash{$k};
138 }
139 }
140
141 return \@result, \%leftover;
142}
143
144sub make_attributes {
145 my $attr = shift;
146 return () unless $attr && ref($attr) && ref($attr) eq 'HASH';
147 my $escape = shift || 0;
148 my $do_not_quote = shift;
149
150 my $quote = $do_not_quote ? '' : '"';
151
152 my @attr_keys= sort keys %$attr;
153 my(@att);
154 foreach (@attr_keys) {
155 my($key) = $_;
156 $key=~s/^\-//; # get rid of initial - if present
157
158 # old way: breaks EBCDIC!
159 # $key=~tr/A-Z_/a-z-/; # parameters are lower case, use dashes
160
161 ($key="\L$key") =~ tr/_/-/; # parameters are lower case, use dashes
162
163 my $value = $escape ? simple_escape($attr->{$_}) : $attr->{$_};
164 push(@att,defined($attr->{$_}) ? qq/$key=$quote$value$quote/ : qq/$key/);
165 }
166 return sort @att;
167}
168
169sub simple_escape {
170 return unless defined(my $toencode = shift);
171 $toencode =~ s{&}{&}gso;
172 $toencode =~ s{<}{&lt;}gso;
173 $toencode =~ s{>}{&gt;}gso;
174 $toencode =~ s{\"}{&quot;}gso;
175# Doesn't work. Can't work. forget it.
176# $toencode =~ s{\x8b}{&#139;}gso;
177# $toencode =~ s{\x9b}{&#155;}gso;
178 $toencode;
179}
180
181sub utf8_chr {
182 my $c = shift(@_);
183 my $u = chr($c);
184 utf8::encode($u); # drop utf8 flag
185 return $u;
186}
187
188# unescape URL-encoded data
189sub unescape {
190 shift() if @_ > 0 and (ref($_[0]) || (defined $_[1] && $_[0] eq $CGI::DefaultClass));
191 my $todecode = shift;
192 return undef unless defined($todecode);
193 $todecode =~ tr/+/ /; # pluses become spaces
194 if ($_EBCDIC) {
195 $todecode =~ s/%([0-9a-fA-F]{2})/chr $A2E[hex($1)]/ge;
196 } else {
197 # handle surrogate pairs first -- dankogai. Ref: http://unicode.org/faq/utf_bom.html#utf16-2
198 $todecode =~ s{
199 utf8_chr(
200 0x10000
201 + (hex($1) - 0xD800) * 0x400
202 + (hex($2) - 0xDC00)
203 )
204 }gex;
205
- -
208 $todecode =~ s/%(?:([0-9a-fA-F]{2})|u([0-9a-fA-F]{4}))/
209 defined($1)? chr hex($1) : utf8_chr(hex($2))/ge;
210 }
211 return $todecode;
212}
213
214# URL-encode data
215#
216# We cannot use the %u escapes, they were rejected by W3C, so the official
217# way is %XX-escaped utf-8 encoding.
218# Naturally, Unicode strings have to be converted to their utf-8 byte
219# representation.
220# Byte strings were traditionally used directly as a sequence of octets.
221# This worked if they actually represented binary data (i.e. in CGI::Compress).
222# This also worked if these byte strings were actually utf-8 encoded; e.g.,
223# when the source file used utf-8 without the appropriate "use utf8;".
224# This fails if the byte string is actually a Latin 1 encoded string, but it
225# was always so and cannot be fixed without breaking the binary data case.
226# -- Stepan Kasal <skasal@redhat.com>
227#
228
229sub escape {
230 # If we being called in an OO-context, discard the first argument.
231 shift() if @_ > 1 and ( ref($_[0]) || (defined $_[1] && $_[0] eq $CGI::DefaultClass));
232 my $toencode = shift;
233 return undef unless defined($toencode);
234 utf8::encode($toencode) if utf8::is_utf8($toencode);
235 if ($_EBCDIC) {
236 $toencode=~s/([^a-zA-Z0-9_.~-])/uc sprintf("%%%02x",$E2A[ord($1)])/eg;
237 } else {
238 $toencode=~s/([^a-zA-Z0-9_.~-])/uc sprintf("%%%02x",ord($1))/eg;
239 }
240 return $toencode;
241}
242
243# This internal routine creates date strings suitable for use in
244# cookies and HTTP headers. (They differ, unfortunately.)
245# Thanks to Mark Fisher for this.
246sub expires {
247 my($time,$format) = @_;
248 $format ||= 'http';
249
250 my(@MON)=qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/;
251 my(@WDAY) = qw/Sun Mon Tue Wed Thu Fri Sat/;
252
253 # pass through preformatted dates for the sake of expire_calc()
254 $time = expire_calc($time);
255 return $time unless $time =~ /^\d+$/;
256
257 # make HTTP/cookie date string from GMT'ed time
258 # (cookies use '-' as date separator, HTTP uses ' ')
259 my($sc) = ' ';
260 $sc = '-' if $format eq "cookie";
261 my($sec,$min,$hour,$mday,$mon,$year,$wday) = gmtime($time);
262 $year += 1900;
263 return sprintf("%s, %02d$sc%s$sc%04d %02d:%02d:%02d GMT",
264 $WDAY[$wday],$mday,$MON[$mon],$year,$hour,$min,$sec);
265}
266
267# This internal routine creates an expires time exactly some number of
268# hours from the current time. It incorporates modifications from
269# Mark Fisher.
270sub expire_calc {
271 my($time) = @_;
272 my(%mult) = ('s'=>1,
273 'm'=>60,
274 'h'=>60*60,
275 'd'=>60*60*24,
276 'M'=>60*60*24*30,
277 'y'=>60*60*24*365);
278 # format for time can be in any of the forms...
279 # "now" -- expire immediately
280 # "+180s" -- in 180 seconds
281 # "+2m" -- in 2 minutes
282 # "+12h" -- in 12 hours
283 # "+1d" -- in 1 day
284 # "+3M" -- in 3 months
285 # "+2y" -- in 2 years
286 # "-3m" -- 3 minutes ago(!)
287 # If you don't supply one of these forms, we assume you are
288 # specifying the date yourself
289 my($offset);
290 if (!$time || (lc($time) eq 'now')) {
291 $offset = 0;
292 } elsif ($time=~/^\d+/) {
293 return $time;
294 } elsif ($time=~/^([+-]?(?:\d+|\d*\.\d*))([smhdMy])/) {
295 $offset = ($mult{$2} || 1)*$1;
296 } else {
297 return $time;
298 }
299 my $cur_time = time;
300 return ($cur_time+$offset);
301}
302
303sub ebcdic2ascii {
304 my $data = shift;
305 $data =~ s/(.)/chr $E2A[ord($1)]/ge;
306 $data;
307}
308
309sub ascii2ebcdic {
310 my $data = shift;
311 $data =~ s/(.)/chr $A2E[ord($1)]/ge;
312 $data;
313}
314
3151;
316
317__END__