Filename | /usr/share/perl5/vendor_perl/CGI/Util.pm |
Statements | Executed 2859 statements in 7.41ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
218 | 4 | 2 | 1.88ms | 3.08ms | rearrange | CGI::Util::
1 | 1 | 1 | 1.52ms | 1.93ms | BEGIN@10 | CGI::Util::
1 | 1 | 1 | 1.38ms | 1.57ms | BEGIN@2 | CGI::Util::
218 | 1 | 1 | 1.20ms | 1.20ms | _rearrange_params | CGI::Util::
43 | 3 | 3 | 920µs | 1.08ms | make_attributes | CGI::Util::
45 | 1 | 1 | 164µs | 164µs | simple_escape | CGI::Util::
4 | 2 | 1 | 14µs | 14µs | unescape | CGI::Util::
1 | 1 | 1 | 13µs | 32µs | BEGIN@4 | CGI::Util::
0 | 0 | 0 | 0s | 0s | ascii2ebcdic | CGI::Util::
0 | 0 | 0 | 0s | 0s | ebcdic2ascii | CGI::Util::
0 | 0 | 0 | 0s | 0s | escape | CGI::Util::
0 | 0 | 0 | 0s | 0s | expire_calc | CGI::Util::
0 | 0 | 0 | 0s | 0s | expires | CGI::Util::
0 | 0 | 0 | 0s | 0s | rearrange_header | CGI::Util::
0 | 0 | 0 | 0s | 0s | utf8_chr | CGI::Util::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | package CGI::Util; | ||||
2 | 2 | 170µs | 2 | 1.69ms | # spent 1.57ms (1.38+194µs) within CGI::Util::BEGIN@2 which was called:
# once (1.38ms+194µs) by CGI::BEGIN@28 at line 2 # spent 1.57ms making 1 call to CGI::Util::BEGIN@2
# spent 112µs making 1 call to base::import |
3 | 1 | 16µs | require 5.008001; | ||
4 | 2 | 93µs | 2 | 52µs | # spent 32µs (13+20) within CGI::Util::BEGIN@4 which was called:
# once (13µs+20µs) by CGI::BEGIN@28 at line 4 # spent 32µs making 1 call to CGI::Util::BEGIN@4
# spent 20µs making 1 call to strict::import |
5 | 1 | 3µs | our @EXPORT_OK = qw(rearrange rearrange_header make_attributes unescape escape | ||
6 | expires ebcdic2ascii ascii2ebcdic); | ||||
7 | |||||
8 | 1 | 400ns | our $VERSION = '3.62'; | ||
9 | |||||
10 | 2 | 2.70ms | 2 | 1.99ms | # spent 1.93ms (1.52+409µs) within CGI::Util::BEGIN@10 which was called:
# once (1.52ms+409µs) by CGI::BEGIN@28 at line 10 # spent 1.93ms making 1 call to CGI::Util::BEGIN@10
# spent 56µs making 1 call to constant::import |
11 | |||||
12 | # This option is not documented and may change or go away. | ||||
13 | # The HTML spec does not require attributes to be sorted, | ||||
14 | # but it's useful for testing to get a predictable order back. | ||||
15 | 1 | 100ns | our $SORT_ATTRIBUTES; | ||
16 | |||||
17 | # (ord('^') == 95) for codepage 1047 as on os390, vmesa | ||||
18 | 1 | 20µs | our @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 | ); | ||||
36 | 1 | 21µs | our @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 | |||||
55 | if (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 | } | ||||
68 | elsif (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 | |||||
80 | # spent 3.08ms (1.88+1.20) within CGI::Util::rearrange which was called 218 times, avg 14µs/call:
# 207 times (1.81ms+1.13ms) by Foswiki::Request::param at line 410 of /var/www/foswiki11/lib/Foswiki/Request.pm, avg 14µs/call
# 6 times (34µs+21µs) by Foswiki::Request::header at line 549 of /var/www/foswiki11/lib/Foswiki/Request.pm, avg 9µs/call
# 3 times (23µs+38µs) by Foswiki::Request::url at line 241 of /var/www/foswiki11/lib/Foswiki/Request.pm, avg 20µs/call
# 2 times (11µs+7µs) by CGI::delete at line 6 of (eval 46)[CGI.pm:884], avg 9µs/call | ||||
81 | 218 | 204µs | my ($order,@param) = @_; | ||
82 | 218 | 487µs | 218 | 1.20ms | my ($result, $leftover) = _rearrange_params( $order, @param ); # spent 1.20ms making 218 calls to CGI::Util::_rearrange_params, avg 5µs/call |
83 | 218 | 220µs | push @$result, make_attributes( $leftover, defined $CGI::Q ? $CGI::Q->{escape} : 1 ) | ||
84 | if keys %$leftover; | ||||
85 | 218 | 809µs | @$result; | ||
86 | } | ||||
87 | |||||
88 | sub 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 | |||||
97 | # spent 1.20ms within CGI::Util::_rearrange_params which was called 218 times, avg 5µs/call:
# 218 times (1.20ms+0s) by CGI::Util::rearrange at line 82, avg 5µs/call | ||||
98 | 218 | 175µs | my($order,@param) = @_; | ||
99 | 218 | 111µs | return [] unless @param; | ||
100 | |||||
101 | 209 | 139µs | if (ref($param[0]) eq 'HASH') { | ||
102 | @param = %{$param[0]}; | ||||
103 | } else { | ||||
104 | return \@param | ||||
105 | 209 | 972µs | unless (defined($param[0]) && substr($param[0],0,1) eq '-'); | ||
106 | } | ||||
107 | |||||
108 | # map parameters into positional indices | ||||
109 | 3 | 800ns | my ($i,%pos); | ||
110 | 3 | 800ns | $i = 0; | ||
111 | 3 | 3µs | foreach (@$order) { | ||
112 | 24 | 22µs | foreach (ref($_) eq 'ARRAY' ? @$_ : $_) { $pos{lc($_)} = $i; } | ||
113 | 10 | 3µs | $i++; | ||
114 | } | ||||
115 | |||||
116 | 3 | 2µs | my (@result,%leftover); | ||
117 | 3 | 7µs | $#result = $#$order; # preextend | ||
118 | 3 | 2µs | while (@param) { | ||
119 | 6 | 4µs | my $key = lc(shift(@param)); | ||
120 | 6 | 8µs | $key =~ s/^\-//; | ||
121 | 6 | 10µs | if (exists $pos{$key}) { | ||
122 | $result[$pos{$key}] = shift(@param); | ||||
123 | } else { | ||||
124 | $leftover{$key} = shift(@param); | ||||
125 | } | ||||
126 | } | ||||
127 | |||||
128 | 3 | 14µs | return \@result, \%leftover; | ||
129 | } | ||||
130 | |||||
131 | # spent 1.08ms (920µs+164µs) within CGI::Util::make_attributes which was called 43 times, avg 25µs/call:
# 24 times (497µs+62µs) by CGI::a at line 6 of (eval 204)[CGI.pm:884], avg 23µs/call
# 16 times (316µs+66µs) by CGI::span at line 6 of (eval 245)[CGI.pm:884], avg 24µs/call
# 3 times (107µs+36µs) by CGI::img at line 6 of (eval 244)[CGI.pm:884], avg 48µs/call | ||||
132 | 43 | 17µs | my $attr = shift; | ||
133 | 43 | 42µs | return () unless $attr && ref($attr) && ref($attr) eq 'HASH'; | ||
134 | 43 | 20µs | my $escape = shift || 0; | ||
135 | 43 | 11µs | my $do_not_quote = shift; | ||
136 | |||||
137 | 43 | 22µs | my $quote = $do_not_quote ? '' : '"'; | ||
138 | |||||
139 | 43 | 103µs | my @attr_keys= keys %$attr; | ||
140 | 43 | 12µs | if ($SORT_ATTRIBUTES) { | ||
141 | @attr_keys= sort @attr_keys; | ||||
142 | } | ||||
143 | 43 | 10µs | my(@att); | ||
144 | 43 | 54µs | foreach (@attr_keys) { | ||
145 | 63 | 31µs | my($key) = $_; | ||
146 | 63 | 58µs | $key=~s/^\-//; # get rid of initial - if present | ||
147 | |||||
148 | # old way: breaks EBCDIC! | ||||
149 | # $key=~tr/A-Z_/a-z-/; # parameters are lower case, use dashes | ||||
150 | |||||
151 | 63 | 79µs | ($key="\L$key") =~ tr/_/-/; # parameters are lower case, use dashes | ||
152 | |||||
153 | 63 | 115µs | 45 | 164µs | my $value = $escape ? simple_escape($attr->{$_}) : $attr->{$_}; # spent 164µs making 45 calls to CGI::Util::simple_escape, avg 4µs/call |
154 | 63 | 155µs | push(@att,defined($attr->{$_}) ? qq/$key=$quote$value$quote/ : qq/$key/); | ||
155 | } | ||||
156 | 43 | 170µs | return @att; | ||
157 | } | ||||
158 | |||||
159 | # spent 164µs within CGI::Util::simple_escape which was called 45 times, avg 4µs/call:
# 45 times (164µs+0s) by CGI::Util::make_attributes at line 153, avg 4µs/call | ||||
160 | 45 | 25µs | return unless defined(my $toencode = shift); | ||
161 | 45 | 24µs | $toencode =~ s{&}{&}gso; | ||
162 | 45 | 12µs | $toencode =~ s{<}{<}gso; | ||
163 | 45 | 14µs | $toencode =~ s{>}{>}gso; | ||
164 | 45 | 13µs | $toencode =~ s{\"}{"}gso; | ||
165 | # Doesn't work. Can't work. forget it. | ||||
166 | # $toencode =~ s{\x8b}{‹}gso; | ||||
167 | # $toencode =~ s{\x9b}{›}gso; | ||||
168 | 45 | 146µs | $toencode; | ||
169 | } | ||||
170 | |||||
171 | sub utf8_chr { | ||||
172 | my $c = shift(@_); | ||||
173 | my $u = chr($c); | ||||
174 | utf8::encode($u); # drop utf8 flag | ||||
175 | return $u; | ||||
176 | } | ||||
177 | |||||
178 | # unescape URL-encoded data | ||||
179 | # spent 14µs within CGI::Util::unescape which was called 4 times, avg 4µs/call:
# 2 times (9µs+0s) by CGI::parse_params at line 788 of CGI.pm, avg 5µs/call
# 2 times (5µs+0s) by CGI::parse_params at line 789 of CGI.pm, avg 3µs/call | ||||
180 | 4 | 2µs | shift() if @_ > 0 and (ref($_[0]) || (defined $_[1] && $_[0] eq $CGI::DefaultClass)); | ||
181 | 4 | 2µs | my $todecode = shift; | ||
182 | 4 | 500ns | return undef unless defined($todecode); | ||
183 | 4 | 2µs | $todecode =~ tr/+/ /; # pluses become spaces | ||
184 | 4 | 1µs | if (EBCDIC) { | ||
185 | $todecode =~ s/%([0-9a-fA-F]{2})/chr $A2E[hex($1)]/ge; | ||||
186 | } else { | ||||
187 | # handle surrogate pairs first -- dankogai. Ref: http://unicode.org/faq/utf_bom.html#utf16-2 | ||||
188 | 4 | 1µs | $todecode =~ s{ | ||
189 | %u([Dd][89a-bA-B][0-9a-fA-F]{2}) # hi | ||||
190 | %u([Dd][c-fC-F][0-9a-fA-F]{2}) # lo | ||||
191 | }{ | ||||
192 | utf8_chr( | ||||
193 | 0x10000 | ||||
194 | + (hex($1) - 0xD800) * 0x400 | ||||
195 | + (hex($2) - 0xDC00) | ||||
196 | ) | ||||
197 | }gex; | ||||
198 | 4 | 1µs | $todecode =~ s/%(?:([0-9a-fA-F]{2})|u([0-9a-fA-F]{4}))/ | ||
199 | defined($1)? chr hex($1) : utf8_chr(hex($2))/ge; | ||||
200 | } | ||||
201 | 4 | 10µs | return $todecode; | ||
202 | } | ||||
203 | |||||
204 | # URL-encode data | ||||
205 | # | ||||
206 | # We cannot use the %u escapes, they were rejected by W3C, so the official | ||||
207 | # way is %XX-escaped utf-8 encoding. | ||||
208 | # Naturally, Unicode strings have to be converted to their utf-8 byte | ||||
209 | # representation. | ||||
210 | # Byte strings were traditionally used directly as a sequence of octets. | ||||
211 | # This worked if they actually represented binary data (i.e. in CGI::Compress). | ||||
212 | # This also worked if these byte strings were actually utf-8 encoded; e.g., | ||||
213 | # when the source file used utf-8 without the apropriate "use utf8;". | ||||
214 | # This fails if the byte string is actually a Latin 1 encoded string, but it | ||||
215 | # was always so and cannot be fixed without breaking the binary data case. | ||||
216 | # -- Stepan Kasal <skasal@redhat.com> | ||||
217 | # | ||||
218 | |||||
219 | sub escape { | ||||
220 | # If we being called in an OO-context, discard the first argument. | ||||
221 | shift() if @_ > 1 and ( ref($_[0]) || (defined $_[1] && $_[0] eq $CGI::DefaultClass)); | ||||
222 | my $toencode = shift; | ||||
223 | return undef unless defined($toencode); | ||||
224 | utf8::encode($toencode) if utf8::is_utf8($toencode); | ||||
225 | if (EBCDIC) { | ||||
226 | $toencode=~s/([^a-zA-Z0-9_.~-])/uc sprintf("%%%02x",$E2A[ord($1)])/eg; | ||||
227 | } else { | ||||
228 | $toencode=~s/([^a-zA-Z0-9_.~-])/uc sprintf("%%%02x",ord($1))/eg; | ||||
229 | } | ||||
230 | return $toencode; | ||||
231 | } | ||||
232 | |||||
233 | # This internal routine creates date strings suitable for use in | ||||
234 | # cookies and HTTP headers. (They differ, unfortunately.) | ||||
235 | # Thanks to Mark Fisher for this. | ||||
236 | sub expires { | ||||
237 | my($time,$format) = @_; | ||||
238 | $format ||= 'http'; | ||||
239 | |||||
240 | my(@MON)=qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/; | ||||
241 | my(@WDAY) = qw/Sun Mon Tue Wed Thu Fri Sat/; | ||||
242 | |||||
243 | # pass through preformatted dates for the sake of expire_calc() | ||||
244 | $time = expire_calc($time); | ||||
245 | return $time unless $time =~ /^\d+$/; | ||||
246 | |||||
247 | # make HTTP/cookie date string from GMT'ed time | ||||
248 | # (cookies use '-' as date separator, HTTP uses ' ') | ||||
249 | my($sc) = ' '; | ||||
250 | $sc = '-' if $format eq "cookie"; | ||||
251 | my($sec,$min,$hour,$mday,$mon,$year,$wday) = gmtime($time); | ||||
252 | $year += 1900; | ||||
253 | return sprintf("%s, %02d$sc%s$sc%04d %02d:%02d:%02d GMT", | ||||
254 | $WDAY[$wday],$mday,$MON[$mon],$year,$hour,$min,$sec); | ||||
255 | } | ||||
256 | |||||
257 | # This internal routine creates an expires time exactly some number of | ||||
258 | # hours from the current time. It incorporates modifications from | ||||
259 | # Mark Fisher. | ||||
260 | sub expire_calc { | ||||
261 | my($time) = @_; | ||||
262 | my(%mult) = ('s'=>1, | ||||
263 | 'm'=>60, | ||||
264 | 'h'=>60*60, | ||||
265 | 'd'=>60*60*24, | ||||
266 | 'M'=>60*60*24*30, | ||||
267 | 'y'=>60*60*24*365); | ||||
268 | # format for time can be in any of the forms... | ||||
269 | # "now" -- expire immediately | ||||
270 | # "+180s" -- in 180 seconds | ||||
271 | # "+2m" -- in 2 minutes | ||||
272 | # "+12h" -- in 12 hours | ||||
273 | # "+1d" -- in 1 day | ||||
274 | # "+3M" -- in 3 months | ||||
275 | # "+2y" -- in 2 years | ||||
276 | # "-3m" -- 3 minutes ago(!) | ||||
277 | # If you don't supply one of these forms, we assume you are | ||||
278 | # specifying the date yourself | ||||
279 | my($offset); | ||||
280 | if (!$time || (lc($time) eq 'now')) { | ||||
281 | $offset = 0; | ||||
282 | } elsif ($time=~/^\d+/) { | ||||
283 | return $time; | ||||
284 | } elsif ($time=~/^([+-]?(?:\d+|\d*\.\d*))([smhdMy])/) { | ||||
285 | $offset = ($mult{$2} || 1)*$1; | ||||
286 | } else { | ||||
287 | return $time; | ||||
288 | } | ||||
289 | my $cur_time = time; | ||||
290 | return ($cur_time+$offset); | ||||
291 | } | ||||
292 | |||||
293 | sub ebcdic2ascii { | ||||
294 | my $data = shift; | ||||
295 | $data =~ s/(.)/chr $E2A[ord($1)]/ge; | ||||
296 | $data; | ||||
297 | } | ||||
298 | |||||
299 | sub ascii2ebcdic { | ||||
300 | my $data = shift; | ||||
301 | $data =~ s/(.)/chr $A2E[ord($1)]/ge; | ||||
302 | $data; | ||||
303 | } | ||||
304 | |||||
305 | 1 | 45µs | 1; | ||
306 | |||||
307 | __END__ |