Utils.pm

filter: Statementstmt 24.6% 203 / 824 Branchbran 10.1% 37 / 364 Conditioncond 12.2% 16 / 131 MC/DCmcdc 3.7% 3 / 80 Subroutinesub 32.5% 28 / 86 Podpod 90.4% 38 / 42 totaltotal 21.2% 325 / 1527 CC 245 SCAR 48.9
CC
245
Cov
19%
getHeader
812.0
_modPerlParse
272.0
_parse_sub_params
240.0
CRAP 133.1
↑ Summary Utils/UploadFile.pm »
1 # -*-perl-*-
2 # Creation date: 2003-08-13 20:23:50
3 # Authors: Don
4 # Change log:
5 # $Id: Utils.pm,v 1.73 2008/11/13 03:56:46 don Exp $
6  
7 # Copyright (c) 2003-2008 Don Owens
8  
9 # All rights reserved. This program is free software; you can
10 # redistribute it and/or modify it under the same terms as Perl
11 # itself.
12  
13 =pod
14  
15 =head1 NAME
16  
17 CGI::Utils - Utilities for retrieving information through the
18 Common Gateway Interface
19  
20 =head1 SYNOPSIS
21  
22 use CGI::Utils;
23 my $utils = CGI::Utils->new;
24  
25 my $fields = $utils->vars; # or $utils->Vars
26 my $field1 = $$fields{field1};
27  
28 or
29  
30 my $field1 = $utils->param('field1');
31  
32  
33 # File uploads
34 my $file_handle = $utils->param('file0'); # or $$fields{file0};
35 my $file_name = "$file_handle";
36  
37 =head1 DESCRIPTION
38  
39 This module can be used almost as a drop-in replacement for
40 CGI.pm for those of you who do not use the HTML generating
41 features of CGI.pm
42  
43 This module provides an object-oriented interface for retrieving
44 information provided by the Common Gateway Interface, as well as
45 url-encoding and decoding values, and parsing CGI
46 parameters. For example, CGI has a utility for escaping HTML,
47 but no public interface for url-encoding a value or for taking a
48 hash of values and returning a url-encoded query string suitable
49 for passing to a CGI script. This module does that, as well as
50 provide methods for creating a self-referencing url, converting
51 relative urls to absolute, adding CGI parameters to the end of a
52 url, etc. Please see the METHODS section below for more
53 detailed descriptions of functionality provided by this module.
54  
55 File uploads via the multipart/form-data encoding are supported.
56 The parameter for the field name corresponding to the file is a
57 file handle that, when evaluated in string context, returns the
58 name of the file uploaded. To get the contents of the file,
59 just read from the file handle.
60  
61 mod_perl is supported if a value for apache_request is passed to
62 new(), or if the apache request object is available via
63 Apache->request, or if running under HTML::Mason. See the
64 documentation for the new() method for details.
65  
66 If not running in a mod_perl or CGI environment, @ARGV will be
67 searched for key/value pairs in the format
68  
69 key1=val1 key2=val2
70  
71 If all command-line arguments are in this format, the key/value
72 pairs will be available as if they were passed via a CGI or
73 mod_perl interface.
74  
75 =head1 METHODS
76  
77 =cut
78  
79 # TODO
80 # modify CGI::Utils::UploadFile to use hidden attributes instead of making up class names
81 # cache values like parsed cookies
82 # NPH stuff for getHeader()
83  
84 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
85  
86 { package CGI::Utils;
87  
88 6 ▶ use vars qw($VERSION @ISA @EXPORT_OK @EXPORT %EXPORT_TAGS $AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
89  
90 6 ▶ use CGI::Utils::UploadFile;
Subroutine BEGIN: called CC=1 SCAR=0.0
91
92 6 ▶ BEGIN {
Subroutine BEGIN: called CC=3 SCAR=13.3
93 6 $VERSION = '0.12'; # update below in POD as well
94  
95 6 local($SIG{__DIE__});
96 6 ▶ if (defined($ENV{MOD_PERL}) and $ENV{MOD_PERL} ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ENV{"MOD_PERL"} and $ENV{"MOD_PERL"} ne "") 0 6 6
Condition33%1/3
defined $ENV{"MOD_PERL"} and $ENV{"MOD_PERL"} ne ""
!ll&&!rl&&r
600
MC/DC0%0/2
defined $ENV{"MOD_PERL"} and $ENV{"MOD_PERL"} ne ""
defined $ENV{"MOD_PERL"} $ENV{"MOD_PERL"} ne ""
Truth table33%1/3
A and B
A= defined $ENV{"MOD_PERL"}B= $ENV{"MOD_PERL"} ne ""
ABresult
0X0
100
111
97 0 eval q{
98 use mod_perl;
99 $CGI::Utils::MP2 = $mod_perl::VERSION >= 1.99;
100 if (defined($CGI::Utils::MP2)) {
101 if ($CGI::Utils::MP2) {
102 require Apache2::Const;
103 require Apache2::RequestUtil;
104 }
105 else {
106 require Apache::Constants;
107 }
108 $CGI::Utils::Loaded_Apache_Constants = 1;
109 }
110 };
111 }
112 }
113  
114 6 ▶ use constant MP2 => $CGI::Utils::MP2;
Subroutine BEGIN: called CC=1 SCAR=0.0
115
116 6 require Exporter;
117 6 @ISA = 'Exporter';
118 6 @EXPORT = ();
119 6 @EXPORT_OK = qw(urlEncode urlDecode urlEncodeVars urlDecodeVars getSelfRefHostUrl
120 getSelfRefUrl getSelfRefUrlWithQuery getSelfRefUrlDir addParamsToUrl
121 getParsedCookies escapeHtml escapeHtmlFormValue convertRelativeUrlWithParams
122 convertRelativeUrlWithArgs getSelfRefUri);
123 6 $EXPORT_TAGS{all_utils} = [ qw(urlEncode urlDecode urlEncodeVars urlDecodeVars
124 getSelfRefHostUrl
125 getSelfRefUrl getSelfRefUrlWithQuery getSelfRefUrlDir
126 addParamsToUrl getParsedCookies escapeHtml escapeHtmlFormValue
127 convertRelativeUrlWithParams convertRelativeUrlWithArgs
128 getSelfRefUri)
129 ];
130  
131 =pod
132  
133 =head2 new(\%params)
134  
135 Returns a new CGI::Utils object. Parameters are optional.
136 CGI::Utils supports mod_perl if the Apache request object is
137 passed as $params{apache_request}, or if it is available via
138 Apache->request (or Apache2::RequestUtil->request), or if running
139 under HTML::Mason.
140  
141 You may also pass max_post_size in %params.
142  
143 =cut
144 3 ▶ sub new {
Subroutine new: called CC=3 SCAR=11.8
Pod documented
145 3 my ($proto, $args) = @_;
146 3 ▶ $args = {} unless ref($args) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args eq "HASH" 3 0 3
147 my $self = { _params => {}, _param_order => [], _upload_info => {},
148 _max_post_size => $$args{max_post_size},
149 _apache_request => $$args{apache_request},
150 _mason => $$args{mason},
151 3 };
152 3 ▶ bless $self, ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
030
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
153 3 return $self;
154 }
155  
156 # added for v0.07
157 0 ▶ sub _getApacheRequest {
Subroutine _getApacheRequest: not called CC=7 SCAR=40.3
158 0 my ($self) = @_;
159 0 my $r;
160 0 ▶ $r = $self->{_apache_request} if ref($self);
Branch: 1 branch
BranchTrueFalseTotal
if ref $self 0 0 0
161 0 ▶ return $r if $r;
Branch: 1 branch
BranchTrueFalseTotal
if $r 0 0 0
162  
163 0 ▶ if ($ENV{MOD_PERL}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"MOD_PERL"}) 0 0 0
164 0 ▶ if ($self->_getMasonObject) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_getMasonObject) { } 0 0 0
elsif (defined $mod_perl::VERSION) { } 0 0 0
165 # we're running under mason
166 0 return $self->_getApacheRequestFromMason;
167 } elsif (defined($mod_perl::VERSION)) {
168 0 if (MP2) {
169 $r = Apache2::RequestUtil->request;
170 }
171 else {
172 0 $r = Apache->request;
173 }
174 0 ▶ return $r if $r;
Branch: 1 branch
BranchTrueFalseTotal
if $r 0 0 0
175 }
176 }
177  
178 0 return;
179 }
180  
181 0 ▶ sub _getModPerlVersion {
Subroutine _getModPerlVersion: not called CC=3 SCAR=24.8
182 0 ▶ if (defined($mod_perl::VERSION)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mod_perl::VERSION) { } 0 0 0
183 0 ▶ if ($mod_perl::VERSION >= 1.99) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mod_perl::VERSION >= 1.99) { } 0 0 0
184 0 return 2;
185 } else {
186 0 return 1;
187 }
188 } else {
189 0 return undef;
190 }
191 }
192  
193 41 ▶ sub _isModPerl {
Subroutine _isModPerl: called CC=3 SCAR=14.2
194 41 ▶ if ($ENV{MOD_PERL} and defined $mod_perl::VERSION) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"MOD_PERL"} and defined $mod_perl::VERSION) 0 41 41
Condition33%1/3
$ENV{"MOD_PERL"} and defined $mod_perl::VERSION
!ll&&!rl&&r
4100
MC/DC0%0/2
$ENV{"MOD_PERL"} and defined $mod_perl::VERSION
$ENV{"MOD_PERL"} defined $mod_perl::VERSION
Truth table33%1/3
A and B
A= $ENV{"MOD_PERL"}B= defined $mod_perl::VERSION
ABresult
0X0
100
111
195 0 return 1;
196 }
197 41 return undef;
198 }
199
200 # added for v0.07
201 0 ▶ sub _getMasonObject {
Subroutine _getMasonObject: not called CC=2 SCAR=17.9
202 0 my $self = shift;
203 0 ▶ if (defined ${'HTML::Mason::Commands::m'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined ${"HTML::Mason::Commands::m";}) 0 0 0
204 0 return $HTML::Mason::Commands::m; #; fix parsing bug in cperl
205 }
206 0 return undef;
207 }
208  
209 # added for v0.07
210 0 ▶ sub _getMasonArgs {
Subroutine _getMasonArgs: not called CC=2 SCAR=17.9
211 0 my $self = shift;
212 0 my $m = $self->_getMasonObject;
213 0 ▶ if ($m) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m) 0 0 0
214 0 return $m->request_args;
215 }
216 0 return undef;
217 }
218  
219 # added for v0.07
220 0 ▶ sub _getApacheRequestFromMason {
Subroutine _getApacheRequestFromMason: not called CC=2 SCAR=17.9
221 0 my ($self) = @_;
222 0 ▶ if (defined ${'HTML::Mason::Commands::r'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined ${"HTML::Mason::Commands::r";}) 0 0 0
223 0 return $HTML::Mason::Commands::r; #; fix parsing bug in cperl
224 }
225 0 return undef;
226 }
227
228 # added for v0.07
229 39 ▶ sub _isCgi {
Subroutine _isCgi: called CC=2 SCAR=8.1
230 39 ▶ if ($ENV{GATEWAY_INTERFACE}
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"GATEWAY_INTERFACE"}) 39 0 39
231 # and $ENV{GATEWAY_INTERFACE} !~ /perl/i # don't count cgi env vars under mod_perl
232 ) {
233 39 return 1;
234 }
235 0 return undef;
236 }
237  
238 # added for v0.07
239 30 ▶ sub _fromCgiOrModPerl {
Subroutine _fromCgiOrModPerl: called CC=4 SCAR=19.7
240 30 my ($self, $apache_request_method, $cgi_env_var) = @_;
241 30 ▶ if ($self->_isModPerl) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_isModPerl) { } 0 30 30
elsif ($self->_isCgi) { } 30 0 30
242 0 my $r = $self->_getApacheRequest;
243 0 ▶ return $r->$apache_request_method() if $r;
Branch: 1 branch
BranchTrueFalseTotal
if $r 0 0 0
244 } elsif ($self->_isCgi) {
245 30 return $ENV{$cgi_env_var};
246 }
247 0 return undef;
248 }
249  
250 # added for v0.07
251 0 ▶ sub _fromCgiOrModPerlConnection {
Subroutine _fromCgiOrModPerlConnection: not called CC=4 SCAR=30.0
252 0 my ($self, $apache_connection_method, $cgi_env_var) = @_;
253 0 ▶ if ($self->_isModPerl) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_isModPerl) { } 0 0 0
elsif ($self->_isCgi) { } 0 0 0
254 0 my $r = $self->_getApacheRequest;
255 0 ▶ if ($r) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r) 0 0 0
256 0 my $c = $r->connection;
257 0 return $c->$apache_connection_method();
258 }
259 } elsif ($self->_isCgi) {
260 0 return $ENV{$cgi_env_var};
261 }
262 0 return undef;
263 }
264  
265 # added for v0.07
266 0 ▶ sub _getHttpHeader {
Subroutine _getHttpHeader: not called CC=4 SCAR=30.0
267 0 my $self = shift;
268 0 my $header = shift;
269 0 ▶ if ($self->_isModPerl) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_isModPerl) { } 0 0 0
elsif ($self->_isCgi) { } 0 0 0
270 0 my $r = $self->_getApacheRequest;
271 0 ▶ if ($r) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r) 0 0 0
272 0 return $r->headers_in()->{$header};
273 }
274 } elsif ($self->_isCgi) {
275 0 $header =~ s/-/_/g;
276 0 return $ENV{'HTTP_' . uc($header)};
277 }
278 0 return undef;
279 }
280  
281 =pod
282  
283 =head2 urlEncode($str)
284  
285 Returns the fully URL-encoded version of the given string. It
286 does not convert space characters to '+' characters.
287  
288 Aliases: url_encode()
289  
290 =cut
291 6 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.0
292 6 ▶ if ($] >= 5.006) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] >= 5.006) { } 6 0 6
293 6 ▶ eval q{
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine urlEncode: called CC=1 SCAR=0.0
Pod documented
294 sub urlEncode {
295 my ($self, $str) = @_;
296
297 use bytes;
298 $str =~ s{([^A-Za-z0-9_])}{sprintf("%%%02x", ord($1))}eg;
299 return $str;
300 }
301 *url_encode = \&urlEncode;
302 };
303 } else {
304 0 eval q{
305 sub urlEncode {
306 my ($self, $str) = @_;
307  
308 $str =~ s{([^A-Za-z0-9_])}{sprintf("%%%02x", ord($1))}eg;
309 return $str;
310 }
311 *url_encode = \&urlEncode;
312 };
313 }
314 }
315  
316 =pod
317  
318 =head2 urlUnicodeEncode($str)
319  
320 Returns the fully URL-encoded version of the given string as
321 unicode characters. It does not convert space characters to '+'
322 characters.
323  
324 Aliases: url_unicode_encode()
325  
326 =cut
327 0 ▶ sub urlUnicodeEncode {
Subroutine urlUnicodeEncode: not called CC=1 SCAR=6.9
Pod documented
328 0 my ($self, $str) = @_;
329 0 $str =~ s{([^A-Za-z0-9_])}{sprintf("%%u%04x", ord($1))}eg;
330 0 return $str;
331 }
332 6 *url_unicode_encode = \&urlUnicodeEncode;
333  
334 =pod
335  
336 =head2 urlDecode($url_encoded_str)
337  
338 Returns the decoded version of the given URL-encoded string.
339  
340 Aliases: url_decode()
341  
342 =cut
343 23 ▶ sub urlDecode {
Subroutine urlDecode: called CC=1 SCAR=0.0
Pod documented
344 23 my ($self, $str) = @_;
345 23 $str =~ tr/+/ /;
346 23 $str =~ s|%([A-Fa-f0-9]{2})|chr(hex($1))|eg;
347 23 return $str;
348 }
349 6 *url_decode = \&urlDecode;
350  
351 =pod
352  
353 =head2 urlUnicodeDecode($url_encoded_str)
354  
355 Returns the decoded version of the given URL-encoded string,
356 with unicode support.
357  
358 Aliases: url_unicode_decode()
359  
360 =cut
361 0 ▶ sub urlUnicodeDecode {
Subroutine urlUnicodeDecode: not called CC=1 SCAR=6.9
Pod documented
362 0 my ($self, $str) = @_;
363 0 $str =~ tr/+/ /;
364 0 $str =~ s|%([A-Fa-f0-9]{2})|chr(hex($1))|eg;
365 0 $str =~ s|%u([A-Fa-f0-9]{2,4})|chr(hex($1))|eg;
366 0 return $str;
367 }
368 6 *url_unicode_decode = \&urlUnicodeDecode;
369  
370 =pod
371  
372 =head2 urlEncodeVars($var_hash, $sep)
373  
374 Takes a hash of name/value pairs and returns a fully URL-encoded
375 query string suitable for passing in a URL. By default, uses
376 the newer separator, a semicolon, as recommended by the W3C. If
377 you pass in a second argument, it is used as the separator
378 between key/value pairs.
379  
380 Aliases: url_encode_vars()
381  
382 =cut
383 11 ▶ sub urlEncodeVars {
Subroutine urlEncodeVars: called CC=5 SCAR=16.1
Pod documented
384 11 my ($self, $var_hash, $sep) = @_;
385 11 ▶ $sep = ';' unless defined $sep;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sep 1 10 11
386 11 my @pairs;
387 11 foreach my $key (sort keys %$var_hash) {
388 21 my $val = $$var_hash{$key};
389 21 my $ref = ref($val);
390 21 ▶ if ($ref eq 'ARRAY' or $ref =~ /=ARRAY/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq 'ARRAY' or $ref =~ /=ARRAY/) { } 1 20 21
Condition66%2/3
$ref eq "ARRAY" or $ref =~ /=ARRAY/
l!l&&r!l&&!r
1020
MC/DC50%1/2
$ref eq "ARRAY" or $ref =~ /=ARRAY/
$ref eq "ARRAY" $ref =~ /=ARRAY/
Truth table66%2/3
A or B
A= $ref eq "ARRAY"B= $ref =~ /=ARRAY/
ABresult
1X1
011
000
391 1 push @pairs, map { $self->urlEncode($key) . "=" . $self->urlEncode($_) } @$val;
392 } else {
393 20 push @pairs, $self->urlEncode($key) . "=" . $self->urlEncode($val);
394 }
395 }
396  
397 11 return join($sep, @pairs);
398 }
399 6 *url_encode_vars = \&urlEncodeVars;
400  
401 =pod
402  
403 =head2 urlDecodeVars($query_string)
404  
405 Takes a URL-encoded query string, decodes it, and returns a
406 reference to a hash of name/value pairs. For multivalued
407 fields, the value is an array of values. If called in array
408 context, it returns a reference to a hash of name/value pairs,
409 and a reference to an array of field names in the order they
410 appear in the query string.
411  
412 Aliases: url_decode_vars()
413  
414 =cut
415 2 ▶ sub urlDecodeVars {
Subroutine urlDecodeVars: called CC=5 SCAR=16.2
Pod documented
416 2 my ($self, $query) = @_;
417 2 my $var_hash = {};
418 2 my @pairs = split /[;&]/, $query;
419 2 my $var_order = [];
420
421 2 foreach my $pair (@pairs) {
422 8 my ($name, $value) = map { $self->urlDecode($_) } split /=/, $pair, 2;
423 8 ▶ if (exists($$var_hash{$name})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $var_hash->{$name}) { } 2 6 8
424 2 my $this_val = $$var_hash{$name};
425 2 ▶ if (ref($this_val) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $this_val eq 'ARRAY') { } 0 2 2
426 0 push @$this_val, $value;
427 } else {
428 2 $$var_hash{$name} = [ $this_val, $value ];
429 }
430 } else {
431 6 $$var_hash{$name} = $value;
432 6 push @$var_order, $name;
433 }
434 }
435
436 2 ▶ return wantarray ? ($var_hash, $var_order) : $var_hash;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 2 2
437 }
438 6 *url_decode_vars = \&urlDecodeVars;
439  
440 =pod
441  
442 =head2 escapeHtml($text)
443  
444 Escapes the given text so that it is not interpreted as HTML. &,
445 <, >, and " characters are escaped.
446  
447 Aliases: escape_html()
448  
449 =cut
450 # added for v0.05
451 0 ▶ sub escapeHtml {
Subroutine escapeHtml: not called CC=2 SCAR=17.9
Pod documented
452 0 my ($self, $text) = @_;
453 0 ▶ return undef unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 0 0
454
455 0 $text =~ s/\&/\&amp;/g;
456 0 $text =~ s/</\&lt;/g;
457 0 $text =~ s/>/\&gt;/g;
458 0 $text =~ s/\"/\&quot;/g;
459 0 $text =~ s/\'/\&#39;/g;
460  
461 0 return $text;
462 }
463 6 *escape_html = \&escapeHtml;
464  
465 =pod
466  
467 =head2 escapeHtmlFormValue($text)
468  
469 Escapes the given text so that it is valid to put in a form
470 field.
471  
472 Aliases: escape_html_form_value()
473  
474 =cut
475 # added for v0.05
476 0 ▶ sub escapeHtmlFormValue {
Subroutine escapeHtmlFormValue: not called CC=1 SCAR=6.9
Pod documented
477 0 my ($self, $str) = @_;
478 0 $str =~ s/\"/&quot;/g;
479 0 $str =~ s/>/&gt;/g;
480 0 $str =~ s/</&lt;/g;
481
482 0 return $str;
483 }
484 6 *escape_html_form_value = \&escapeHtmlFormValue;
485  
486  
487 =pod
488  
489 =head2 getSelfRefHostUrl()
490  
491 Returns a url referencing top level directory in the current
492 domain, e.g., http://mydomain.com
493  
494 Aliases: get_self_ref_host_url()
495  
496 =cut
497 10 ▶ sub getSelfRefHostUrl {
Subroutine getSelfRefHostUrl: called CC=4 SCAR=15.8
Pod documented
498 10 my ($self) = @_;
499 10 my $https = $ENV{HTTPS};
500 10 my $port = $self->_fromCgiOrModPerl('get_server_port', 'SERVER_PORT');
501 # my $scheme = (defined($https) and lc($https) eq 'on') ? 'https' : 'http';
502 # $scheme = 'https' if defined($port) and $port == 443;
503 10 my $scheme = $self->getProtocol;
504 10 my $host = $self->getHost;
505 10 my $host_url = "$scheme://$host";
506  
507 10 ▶ if ($port != 80 and $port != 443) {
Branch: 1 branch
BranchTrueFalseTotal
if ($port != 80 and $port != 443) 0 10 10
Condition33%1/3
$port != 80 and $port != 443
!ll&&!rl&&r
1000
MC/DC0%0/2
$port != 80 and $port != 443
$port != 80 $port != 443
Truth table33%1/3
A and B
A= $port != 80B= $port != 443
ABresult
0X0
100
111
508 0 ▶ $host_url .= ":$port" unless $host_url =~ /:\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $host_url =~ /:\d+$/ 0 0 0
509 }
510
511 10 return $host_url;
512 }
513 6 *get_self_ref_host_url = \&getSelfRefHostUrl;
514 6 *get_self_host_url = \&getSelfRefHostUrl;
515  
516 =pod
517  
518 =head2 getSelfRefUrl()
519  
520 Returns a url referencing the current script (without any query
521 string).
522  
523 Aliases: get_self_ref_url
524  
525 =cut
526 5 ▶ sub getSelfRefUrl {
Subroutine getSelfRefUrl: called CC=1 SCAR=0.0
Pod documented
527 5 my ($self) = @_;
528 5 return $self->getSelfRefHostUrl . $self->getSelfRefUri;
529 }
530 6 *get_self_ref_url = \&getSelfRefUrl;
531  
532 =pod
533  
534 =head2 getSelfRefUri()
535  
536 Returns the current URI.
537  
538 Aliases: get_self_ref_uri()
539  
540 =cut
541 9 ▶ sub getSelfRefUri {
Subroutine getSelfRefUri: called CC=5 SCAR=20.9
Pod documented
542 9 my ($self) = @_;
543 9 my $uri;
544 9 ▶ if ($self->_isModPerl) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_isModPerl) { } 0 9 9
elsif ($self->_isCgi) { } 9 0 9
545 0 my $r = $self->_getApacheRequest;
546 0 ▶ $uri = $r->uri || $r->path_info;
Condition0%0/3
$r->uri || $r->path_info
l!l&&r!l&&!r
000
MC/DC0%0/2
$r->uri || $r->path_info
$r->uri $r->path_info
Truth table0%0/3
A || B
A= $r->uriB= $r->path_info
ABresult
1X1
011
000
547 } elsif ($self->_isCgi) {
548 9 ▶ $uri = $ENV{REQUEST_URI} || $ENV{PATH_INFO};
Condition33%1/3
$ENV{'REQUEST_URI'} || $ENV{'PATH_INFO'}
l!l&&r!l&&!r
900
MC/DC0%0/2
$ENV{'REQUEST_URI'} || $ENV{'PATH_INFO'}
$ENV{'REQUEST_URI'} $ENV{'PATH_INFO'}
Truth table33%1/3
A || B
A= $ENV{'REQUEST_URI'}B= $ENV{'PATH_INFO'}
ABresult
1X1
011
000
549 }
550
551 9 $uri =~ s/^(.*?)\?.*$/$1/;
552 9 return $uri;
553 }
554 6 *get_self_ref_uri = \&getSelfRefUri;
555  
556 =pod
557  
558 =head2 getSelfRefUrlWithQuery()
559  
560 Returns a url referencing the current script along with any
561 query string parameters passed via a GET method.
562  
563 Aliases: get_self_ref_url_with_query()
564  
565 =cut
566 1 ▶ sub getSelfRefUrlWithQuery {
Subroutine getSelfRefUrlWithQuery: called CC=5 SCAR=20.4
Pod documented
567 1 my ($self) = @_;
568  
569 1 my $url = $self->getSelfRefUrl;
570 1 my $query_str;
571 1 ▶ if ($self->_isModPerl) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_isModPerl) { } 0 1 1
572 0 my $r = $self->_getApacheRequest;
573 0 ▶ $query_str = $r ? $r->args : $ENV{QUERY_STRING};
Branch: 1 branch
BranchTrueFalseTotal
$r ? : 0 0 0
574 }
575 else {
576 1 $query_str = $ENV{QUERY_STRING};
577 }
578 1 ▶ if (defined($query_str) and $query_str ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $query_str and $query_str ne "") 1 0 1
Condition33%1/3
defined $query_str and $query_str ne ""
!ll&&!rl&&r
001
MC/DC0%0/2
defined $query_str and $query_str ne ""
defined $query_str $query_str ne ""
Truth table33%1/3
A and B
A= defined $query_strB= $query_str ne ""
ABresult
0X0
100
111
579 1 return $url . '?' . $query_str;
580 }
581 0 return $url;
582 }
583 6 *get_self_ref_url_with_query = \&getSelfRefUrlWithQuery;
584  
585 =pod
586  
587 =head2 getSelfRefUrlWithParams($params, $sep)
588  
589 Returns a url reference the current script along with the given
590 hash of parameters added onto the end of url as a query string.
591  
592 If the optional $sep parameter is passed, it is used as the
593 parameter separator instead of ';', unless the URL already
594 contains '&' chars, in which case it will use '&' for the
595 separator.
596  
597 Aliases: get_self_ref_url_with_params()
598  
599 =cut
600 # added for 0.06
601 2 ▶ sub getSelfRefUrlWithParams {
Subroutine getSelfRefUrlWithParams: called CC=1 SCAR=0.0
Pod documented
602 2 my ($self, $args, $sep) = @_;
603  
604 2 return $self->addParamsToUrl($self->getSelfRefUrl, $args, $sep);
605 }
606 6 *get_self_ref_url_with_params = \&getSelfRefUrlWithParams;
607  
608 =pod
609  
610 =head2 getSelfRefUrlDir()
611  
612 Returns a url referencing the directory part of the current url.
613  
614 Aliases: get_self_ref_url_dir()
615  
616 =cut
617 1 ▶ sub getSelfRefUrlDir {
Subroutine getSelfRefUrlDir: called CC=1 SCAR=0.0
Pod documented
618 1 my ($self) = @_;
619 1 my $url = $self->getSelfRefUrl;
620 1 $url =~ s{^(.+?)\?.*$}{$1};
621 1 $url =~ s{/[^/]+$}{};
622 1 return $url;
623 }
624 6 *get_self_ref_url_dir = \&getSelfRefUrlDir;
625  
626 =pod
627  
628 =head2 convertRelativeUrlWithParams($relative_url, $params, $sep)
629  
630 Converts a relative URL to an absolute one based on the current
631 URL, then adds the parameters in the given hash $params as a
632 query string.
633  
634 If the optional $sep parameter is passed, it is used as the
635 parameter separator instead of ';', unless the URL already
636 contains '&' chars, in which case it will use '&' for the
637 separator.
638  
639 Aliases: convertRelativeUrlWithArgs(), convert_relative_url_with_params(),
640 convert_relative_url_with_args()
641  
642 =cut
643 # Takes $rel_url as a url relative to the current directory,
644 # e.g., a script name, and adds the given cgi params to it.
645 # added for v0.05
646 3 ▶ sub convertRelativeUrlWithParams {
Subroutine convertRelativeUrlWithParams: called CC=3 SCAR=11.0
Pod documented
647 3 my ($self, $rel_url, $args, $sep) = @_;
648 3 my $host_url = $self->getSelfRefHostUrl;
649 3 my $uri = $self->getSelfRefUri;
650 3 $uri =~ s{^(.+?)\?.*$}{$1};
651 3 $uri =~ s{/[^/]+$}{};
652  
653 3 ▶ if ($rel_url =~ m{^/}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rel_url =~ m[^/]) { } 0 3 3
654 0 $uri = $rel_url;
655 } else {
656 3 ▶ while ($rel_url =~ m{^\.\./}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rel_url =~ m[^\.\./]) 2 3 5
657 2 $rel_url =~ s{^\.\./}{}; # pop dir off front
658 2 $uri =~ s{/[^/]+$}{}; # pop dir off end
659 }
660 3 $uri .= '/' . $rel_url;
661 }
662  
663 3 return $self->addParamsToUrl($host_url . $uri, $args, $sep);
664 }
665 6 *convertRelativeUrlWithArgs = \&convertRelativeUrlWithParams;
666 6 *convert_relative_url_with_params = \&convertRelativeUrlWithParams;
667 6 *convert_relative_url_with_args = \&convertRelativeUrlWithParams;
668  
669 =pod
670  
671 =head2 addParamsToUrl($url, $param_hash, $sep)
672  
673 Takes a url and reference to a hash of parameters to be added
674 onto the url as a query string and returns a url with those
675 parameters. It checks whether or not the url already contains a
676 query string and modifies it accordingly. If you want to add a
677 multivalued parameter, pass it as a reference to an array
678 containing all the values.
679  
680 If the optional $sep parameter is passed, it is used as the
681 parameter separator instead of ';', unless the URL already
682 contains '&' chars, in which case it will use '&' for the
683 separator.
684  
685 Aliases: add_params_to_url()
686  
687 =cut
688 10 ▶ sub addParamsToUrl {
Subroutine addParamsToUrl: called CC=8 SCAR=21.1
Pod documented
689 10 my ($self, $url, $param_hash, $sep) = @_;
690 10 ▶ return $url unless ref($param_hash) eq 'HASH' and %$param_hash;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $param_hash eq "HASH" and %$param_hash 0 10 10
Condition33%1/3
ref $param_hash eq "HASH" and %$param_hash
!ll&&!rl&&r
0010
MC/DC0%0/2
ref $param_hash eq "HASH" and %$param_hash
ref $param_hash eq "HASH" %$param_hash
Truth table33%1/3
A and B
A= ref $param_hash eq "HASH"B= %$param_hash
ABresult
0X0
100
111
691 10 ▶ $sep = ';' unless defined($sep) and $sep ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sep and $sep ne "" 7 3 10
Condition66%2/3
defined $sep and $sep ne ""
!ll&&!rl&&r
703
MC/DC50%1/2
defined $sep and $sep ne ""
defined $sep $sep ne ""
Truth table66%2/3
A and B
A= defined $sepB= $sep ne ""
ABresult
0X0
100
111
692 10 ▶ if ($url =~ /^([^?]+)\?(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url =~ /^([^?]+)\?(.*)$/) { } 3 7 10
693 3 my $query = $2;
694 # if query uses & for separator, then keep it consistent
695 3 ▶ if ($query =~ /\&/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($query =~ /\&/) 1 2 3
696 1 $sep = '&';
697 }
698 3 ▶ $url .= $sep unless $url =~ /\?$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $url =~ /\?$/ 2 1 3
699 } else {
700 7 $url .= '?';
701 }
702  
703 10 $url .= $self->urlEncodeVars($param_hash, $sep);
704 10 return $url;
705 }
706 6 *add_params_to_url = \&addParamsToUrl;
707  
708 1 ▶ sub _getRawCookie {
Subroutine _getRawCookie: called CC=7 SCAR=31.2
709 1 my $self = shift;
710  
711 1 ▶ if ($self->_isModPerl) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_isModPerl) { } 0 1 1
712 0 my $r = $self->_getApacheRequest;
713 0 ▶ return $r ? $r->headers_in()->{Cookie} : ($ENV{HTTP_COOKIE} || $ENV{COOKIE} || '');
Branch: 1 branch
BranchTrueFalseTotal
$r ? : 0 0 0
Condition0%0/5
$ENV{"HTTP_COOKIE"} or $ENV{"COOKIE"} or ""
l!l
00
$ENV{"HTTP_COOKIE"} or $ENV{"COOKIE"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{"HTTP_COOKIE"} or $ENV{"COOKIE"} or ""
$ENV{"HTTP_COOKIE"} $ENV{"COOKIE"}
Truth table0%0/3
A or B
A= $ENV{"HTTP_COOKIE"}B= $ENV{"COOKIE"}
ABresult
1X1
011
000
714 }
715 else {
716 1 ▶ return $ENV{HTTP_COOKIE} || $ENV{COOKIE} || '';
Condition40%2/5
$ENV{'HTTP_COOKIE'} || $ENV{'COOKIE'} || ''
l!l
10
$ENV{"HTTP_COOKIE"} or $ENV{"COOKIE"}
l!l&&r!l&&!r
100
MC/DC0%0/2
$ENV{'HTTP_COOKIE'} || $ENV{'COOKIE'} || ''
$ENV{"HTTP_COOKIE"} $ENV{"COOKIE"}
Truth table33%1/3
A or B
A= $ENV{"HTTP_COOKIE"}B= $ENV{"COOKIE"}
ABresult
1X1
011
000
717 }
718 }
719  
720 =pod
721  
722 =head2 getParsedCookies()
723  
724 Parses the cookies passed to the server. Returns a hash of
725 key/value pairs representing the cookie names and values.
726  
727 Aliases: get_parsed_cookies
728  
729 =cut
730 1 ▶ sub getParsedCookies {
Subroutine getParsedCookies: called CC=1 SCAR=0.0
Pod documented
731 1 my ($self) = @_;
732 1 my %cookies = map { (map { $self->urlDecode($_) } split(/=/, $_, 2)) }
733 split(/;\s*/, $self->_getRawCookie);
734 1 return \%cookies;
735 }
736 6 *get_parsed_cookies = \&getParsedCookies;
737  
738 # added for v0.06
739 # for compatibility with CGI.pm
740 # may want to create an object here
741 0 ▶ sub cookie {
Subroutine cookie: not called CC=4 SCAR=30.0
Pod undocumented
742 0 my ($self, @args) = @_;
743 0 my $map_list = [ 'name', [ 'value', 'values' ], 'path', 'expires', 'domain', 'secure' ];
744 0 my $params = $self->_parse_sub_params($map_list, \@args);
745 0 ▶ if (exists($$params{value})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params->{'value'}) { } 0 0 0
746 0 return $params;
747 } else {
748 0 my $cookies = $self->getParsedCookies;
749 0 ▶ if ($cookies and %$cookies) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cookies and %$cookies) 0 0 0
Condition0%0/3
$cookies and %$cookies
!ll&&!rl&&r
000
MC/DC0%0/2
$cookies and %$cookies
$cookies %$cookies
Truth table0%0/3
A and B
A= $cookiesB= %$cookies
ABresult
0X0
100
111
750 0 return $$cookies{$$params{name}};
751 }
752 0 return '';
753 }
754 0 return $params;
755 }
756  
757 # =pod
758  
759 # =head2 parse({ max_post_size => $max_bytes })
760  
761 # Parses the CGI parameters. GET and POST (both url-encoded and
762 # multipart/form-data encodings), including file uploads, are
763 # supported. If the request method is POST, you may pass a
764 # maximum number of bytes to accept via POST. This can be used to
765 # limit the size of file uploads, for example.
766  
767 # =cut
768 0 ▶ sub parse {
Subroutine parse: not called CC=7 SCAR=40.3
Pod undocumented
769 0 my ($self, $args) = @_;
770  
771 0 ▶ return 1 if $$self{_already_parsed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_already_parsed"} 0 0 0
772 0 $$self{_already_parsed} = 1;
773  
774 0 ▶ $args = {} unless ref($args) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args eq "HASH" 0 0 0
775  
776 0 ▶ if ($self->_isModPerl) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_isModPerl) { } 0 0 0
elsif (not $ENV{'GATEWAY_INTERFACE'}) { } 0 0 0
777 # If running under mod_perl, grab the GET or POST data
778 0 my $rv = $self->_modPerlParse($args);
779 0 ▶ return $rv if $rv;
Branch: 1 branch
BranchTrueFalseTotal
if $rv 0 0 0
780 } elsif (not $ENV{'GATEWAY_INTERFACE'}) {
781 # Not CGI, so must be commandline
782 0 ▶ if (scalar(@ARGV)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @ARGV) 0 0 0
783 0 return $self->_cmdLineParse(\@ARGV);
784 }
785 }
786  
787  
788 # check for mod_perl - GATEWAY_INTERFACE =~ m{^CGI-Perl/}
789 # check for PerlEx - GATEWAY_INTERFACE =~ m{^CGI-PerlEx}
790  
791 0 return $self->_cgiParse($args);
792 }
793  
794 0 ▶ sub _cmdLineParse {
Subroutine _cmdLineParse: not called CC=3 SCAR=24.8
795 0 my $self = shift;
796 0 my $args = shift;
797  
798 0 my %params;
799 0 foreach my $arg (@$args) {
800 0 ▶ if ($arg =~ /^([^=]+)=(.*)$/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ /^([^=]+)=(.*)$/s) { } 0 0 0
801 0 my $key = $1;
802 0 my $val = $2;
803 0 $params{$key} = $val;
804 }
805 else {
806 # bad param, drop them all
807 0 return;
808 }
809 }
810  
811 0 $self->{_params} = \%params;
812
813 0 return 1;
814 }
815
816 0 ▶ sub _cgiParse {
Subroutine _cgiParse: not called CC=14 SCAR=53.5
817 0 my $self = shift;
818 0 my $args = shift;
819
820 0 my $method = lc($ENV{REQUEST_METHOD});
821 0 ▶ my $content_length = $ENV{CONTENT_LENGTH} || 0;
Condition0%0/2
$ENV{'CONTENT_LENGTH'} || 0
l!l
00
MC/DC0%0/1
$ENV{'CONTENT_LENGTH'} || 0
$ENV{'CONTENT_LENGTH'}
Truth table0%0/2
A
A= $ENV{'CONTENT_LENGTH'}
Aresult
11
00
822  
823 0 ▶ if ($method eq 'post') {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "post") 0 0 0
824 0 ▶ my $max_size = $$args{max_post_size} || $$self{_max_post_size};
Condition0%0/3
$args->{'max_post_size'} || $self->{'_max_post_size'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{'max_post_size'} || $self->{'_max_post_size'}
$args->{'max_post_size'} $self->{'_max_post_size'}
Truth table0%0/3
A || B
A= $args->{'max_post_size'}B= $self->{'_max_post_size'}
ABresult
1X1
011
000
825 0 ▶ $max_size = 0 unless defined($max_size);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $max_size 0 0 0
826 0 ▶ if ($max_size > 0 and $content_length > $max_size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_size > 0 and $content_length > $max_size) 0 0 0
Condition0%0/3
$max_size > 0 and $content_length > $max_size
!ll&&!rl&&r
000
MC/DC0%0/2
$max_size > 0 and $content_length > $max_size
$max_size > 0 $content_length > $max_size
Truth table0%0/3
A and B
A= $max_size > 0B= $content_length > $max_size
ABresult
0X0
100
111
827 0 return undef;
828 }
829 }
830  
831 0 ▶ if ($method eq 'post' and $ENV{CONTENT_TYPE} =~ m|^multipart/form-data|) {
Branch: 3 branches
BranchTrueFalseTotal
if ($method eq 'post' and $ENV{'CONTENT_TYPE'} =~ m[^multipart/form-data]) { } 0 0 0
elsif ($method eq 'get' or $method eq 'head') { } 0 0 0
elsif ($method eq 'post') { } 0 0 0
Condition0%0/3
$method eq "post" and $ENV{"CONTENT_TYPE"} =~ m[^multipart/form-data]
!ll&&!rl&&r
000
MC/DC0%0/2
$method eq "post" and $ENV{"CONTENT_TYPE"} =~ m[^multipart/form-data]
$method eq "post" $ENV{"CONTENT_TYPE"} =~ m[^multipart/form-data]
Truth table0%0/3
A and B
A= $method eq "post"B= $ENV{"CONTENT_TYPE"} =~ m[^multipart/form-data]
ABresult
0X0
100
111
832 0 ▶ if ($ENV{CONTENT_TYPE} =~ /boundary=(\"?)([^\";,]+)\1/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{'CONTENT_TYPE'} =~ /boundary=(\"?)([^\";,]+)\1/) { } 0 0 0
833 0 my $boundary = $2;
834 0 $self->_readMultipartData($boundary, $content_length, \*STDIN);
835 } else {
836 0 return undef;
837 }
838 ▶ } elsif ($method eq 'get' or $method eq 'head') {
Condition0%0/3
$method eq "get" or $method eq "head"
l!l&&r!l&&!r
000
MC/DC0%0/2
$method eq "get" or $method eq "head"
$method eq "get" $method eq "head"
Truth table0%0/3
A or B
A= $method eq "get"B= $method eq "head"
ABresult
1X1
011
000
839 0 my $query_string = $ENV{QUERY_STRING};
840 0 $self->_parseParams($query_string);
841 } elsif ($method eq 'post') {
842 0 my $query_string;
843 0 ▶ $self->_readPostData(\*STDIN, \$query_string, $content_length) if $content_length > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $content_length > 0 0 0 0
844 0 $self->_parseParams($query_string);
845 # FIXME: may want to append anything in query string
846 # to POST data, so can do a post with an action that
847 # contains a query string.
848 }
849  
850 0 return 1;
851 }
852  
853 0 ▶ sub _modPerlParse {
Subroutine _modPerlParse: not called CC=16 SCAR=56.1
854 0 my $self = shift;
855 0 my $args = shift;
856  
857 0 my $r;
858 0 ▶ if ($self->_getMasonObject) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_getMasonObject) { } 0 0 0
elsif ($r = $self->_getApacheRequest) { } 0 0 0
859 0 $self->{_params} = $self->_getMasonArgs;
860 0 my $method = $self->getRequestMethod;
861 0 ▶ if (lc($method) eq 'post' and $self->getContentType =~ m|^multipart/form-data|) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $method eq "post" and $self->getContentType =~ m[^multipart/form-data]) 0 0 0
Condition0%0/3
lc $method eq "post" and $self->getContentType =~ m[^multipart/form-data]
!ll&&!rl&&r
000
MC/DC0%0/2
lc $method eq "post" and $self->getContentType =~ m[^multipart/form-data]
lc $method eq "post" $self->getContentType =~ m[^multipart/form-data]
Truth table0%0/3
A and B
A= lc $method eq "post"B= $self->getContentType =~ m[^multipart/form-data]
ABresult
0X0
100
111
862 0 $r = $self->_getApacheRequest;
863 0 my @uploads = $r->upload; # $r is really an Apache::Request obj in this case
864 0 ▶ if (@uploads) {
Branch: 1 branch
BranchTrueFalseTotal
if (@uploads) 0 0 0
865 # make a copy so we don't mess around with Mason
866 0 %{$self->{_params}} = %{$self->{_params}};
867 0 foreach my $upload (@uploads) {
868 0 my $field_name = $upload->name;
869 0 my $fh = $upload->fh;
870 # seek($fh, 0, 0);
871 0 my $filename = $upload->filename;
872 0 my $cgi_style_fh =
873 CGI::Utils::UploadFile->new_from_handle($filename, $fh);
874 0 $self->{_params}->{$field_name} = $cgi_style_fh;
875 0 my $info = { 'Content-Type' => $upload->type };
876 0 $self->{_upload_info}->{$filename} = $info;
877 }
878 }
879 }
880 0 return 1;
881 } elsif ($r = $self->_getApacheRequest) {
882 0 my $query_string = $r->args;
883 0 $self->_parseParams($query_string);
884 0 my $method = $self->getRequestMethod;
885 0 ▶ if (lc($method) eq 'post') {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $method eq "post") 0 0 0
886 0 ▶ unless (defined $CGI::Utils::Has_Apache_Request) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $CGI::Utils::Has_Apache_Request) 0 0 0
887 0 local($SIG{__DIE__});
888 0 if (MP2) {
889 eval 'require Apache2::Request';
890 # my $apr = Apache2::RequestUtil->request($r)
891 } else {
892 0 eval 'require Apache::Request';
893 }
894 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
895 0 $CGI::Utils::Has_Apache_Request = 0;
896 } else {
897 0 $CGI::Utils::Has_Apache_Request = 1;
898 }
899 }
900  
901 0 ▶ if ($CGI::Utils::Has_Apache_Request) {
Branch: 2 branches
BranchTrueFalseTotal
if ($CGI::Utils::Has_Apache_Request) { } 0 0 0
elsif ($self->_isCgi) { } 0 0 0
902 0 my $apr = Apache::Request->new($r);
903 0 my $cur_params = $self->{_params};
904 0 my @params = $apr->param;
905 0 foreach my $key (@params) {
906 0 my @vals = $apr->param($key);
907 0 ▶ if (scalar(@vals) > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @vals > 1) { } 0 0 0
908 0 $cur_params->{$key} = \@vals;
909 } else {
910 0 $cur_params->{$key} = $vals[0];
911 }
912 }
913  
914 0 ▶ if ($self->getContentType =~ m|^multipart/form-data|) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->getContentType =~ m[^multipart/form-data]) 0 0 0
915 0 my @uploads = $apr->upload;
916 0 foreach my $upload (@uploads) {
917 0 my $field_name = $upload->name;
918 0 my $fh = $upload->fh;
919 0 my $filename = $upload->filename;
920 0 my $cgi_style_fh =
921 CGI::Utils::UploadFile->new_from_handle($filename, $fh);
922 0 $self->{_params}->{$field_name} = $cgi_style_fh;
923 0 my $info = { 'Content-Type' => $upload->type };
924 0 $self->{_upload_info}->{$filename} = $info;
925 }
926 }
927 } elsif ($self->_isCgi) {
928 # Using the perl-script handler that provides
929 # a CGI environment under mod_perl. So fall
930 # back to getting everything from the CGI
931 # environment.
932 0 return $self->_cgiParse($args);
933 } else {
934 0 return undef;
935 }
936 }
937  
938 0 return 1;
939 }
940  
941 0 return undef;
942 }
943  
944 =pod
945  
946 =head2 param($name)
947  
948 Returns the CGI parameter with name $name. If called in array
949 context, it returns an array. In scalar context, it returns an
950 array reference for multivalued fields, and a scalar for
951 single-valued fields.
952  
953 =cut
954 0 ▶ sub param {
Subroutine param: not called CC=6 SCAR=37.4
Pod documented
955 0 my ($self, $name) = @_;
956 0 $self->parse;
957
958 0 ▶ if (scalar(@_) == 1 and wantarray()) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 1 and wantarray) 0 0 0
Condition0%0/3
scalar @_ == 1 and wantarray
!ll&&!rl&&r
000
MC/DC0%0/2
scalar @_ == 1 and wantarray
scalar @_ == 1 wantarray
Truth table0%0/3
A and B
A= scalar @_ == 1B= wantarray
ABresult
0X0
100
111
959 0 my $params = $$self{_params};
960 0 my $order = $$self{_param_order};
961 0 return grep { exists($$params{$_}) } @$order;
962 }
963 0 ▶ return undef unless defined($name);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 0 0
964 0 my $val = $$self{_params}{$name};
965  
966 0 ▶ if (wantarray()) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
967 0 ▶ return ref($val) eq 'ARRAY' ? @$val : ($val);
Branch: 1 branch
BranchTrueFalseTotal
ref $val eq 'ARRAY' ? : 0 0 0
968 } else {
969 0 return $val;
970 }
971 }
972  
973 =pod
974  
975 =head2 getVars($delimiter)
976  
977 Also Vars() to be compatible with CGI.pm. Returns a reference
978 to a tied hash containing key/value pairs corresponding to each
979 CGI parameter. For multivalued fields, the value is an array
980 ref, with each element being one of the values. If you pass in
981 a value for the delimiter, multivalued fields will be returned
982 as a string of values delimited by the delimiter you passed in.
983  
984 Aliases: vars(), Vars(), get_args(), args()
985  
986 =cut
987 0 ▶ sub getVars {
Subroutine getVars: not called CC=12 SCAR=50.5
Pod documented
988 0 my ($self, $multivalue_delimiter) = @_;
989 0 ▶ if (defined($$self{_multivalue_delimiter}) and $$self{_multivalue_delimiter} ne '') {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->{'_multivalue_delimiter'} and $self->{'_multivalue_delimiter'} ne '') { } 0 0 0
elsif (defined $multivalue_delimiter and $multivalue_delimiter ne '') { } 0 0 0
Condition0%0/3
defined $self->{"_multivalue_delimiter"} and $self->{"_multivalue_delimiter"} ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"_multivalue_delimiter"} and $self->{"_multivalue_delimiter"} ne ""
defined $self->{"_multivalue_delimiter"} $self->{"_multivalue_delimiter"} ne ""
Truth table0%0/3
A and B
A= defined $self->{"_multivalue_delimiter"}B= $self->{"_multivalue_delimiter"} ne ""
ABresult
0X0
100
111
990 $multivalue_delimiter = $$self{_multivalue_delimiter}
991 0 ▶ if not defined($multivalue_delimiter) or $multivalue_delimiter eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $multivalue_delimiter or $multivalue_delimiter eq "" 0 0 0
Condition0%0/3
not defined $multivalue_delimiter or $multivalue_delimiter eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $multivalue_delimiter or $multivalue_delimiter eq ""
not defined $multivalue_delimiter $multivalue_delimiter eq ""
Truth table0%0/3
not(A) or B
A= not defined $multivalue_delimiterB= $multivalue_delimiter eq ""
ABresult
1X1
011
000
992 ▶ } elsif (defined($multivalue_delimiter) and $multivalue_delimiter ne '') {
Condition0%0/3
defined $multivalue_delimiter and $multivalue_delimiter ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $multivalue_delimiter and $multivalue_delimiter ne ""
defined $multivalue_delimiter $multivalue_delimiter ne ""
Truth table0%0/3
A and B
A= defined $multivalue_delimiterB= $multivalue_delimiter ne ""
ABresult
0X0
100
111
993 0 $$self{_multivalue_delimiter} = $multivalue_delimiter;
994 }
995  
996 0 $self->parse;
997
998 0 ▶ if (wantarray()) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 0 0 0
999 0 my $params = $$self{_params};
1000 0 my %vars = %$params;
1001 0 foreach my $key (keys %vars) {
1002 0 ▶ if (ref($vars{$key}) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $vars{$key} eq "ARRAY") 0 0 0
1003 0 ▶ if ($multivalue_delimiter ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($multivalue_delimiter ne '') { } 0 0 0
1004 0 $vars{$key} = join($multivalue_delimiter, @{$vars{$key}});
1005 } else {
1006 0 my @copy = @{$vars{$key}};
1007 0 $vars{$key} = \@copy;
1008 }
1009 }
1010 }
1011 0 return %vars;
1012 }
1013
1014 0 my $vars = $$self{_vars_hash};
1015 0 ▶ return $vars if $vars;
Branch: 1 branch
BranchTrueFalseTotal
if $vars 0 0 0
1016  
1017 0 my %vars;
1018 0 tie %vars, 'CGI::Utils', $self;
1019  
1020 0 return \%vars;
1021 }
1022 6 *vars = \&getVars;
1023 6 *Vars = \&getVars;
1024 6 *get_vars = \&getVars;
1025 6 *get_args = \&getVars;
1026 6 *args = \&getVars;
1027  
1028 =pod
1029  
1030 # Other information provided by the CGI environment
1031  
1032 =head2 getPathInfo(), path_info(), get_path_info();
1033  
1034 Returns additional virtual path information from the URL (if
1035 any) after your script.
1036  
1037 =cut
1038 # added for 0.06
1039 0 ▶ sub getPathInfo {
Subroutine getPathInfo: not called CC=4 SCAR=30.0
Pod documented
1040 0 my ($self) = @_;
1041 0 ▶ return $$self{_path_info} if defined($$self{_path_info});
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_path_info"} 0 0 0
1042
1043 0 my $r = $self->_getApacheRequest;
1044  
1045 0 ▶ my $path_info = $r ? $r->path_info : (defined($ENV{PATH_INFO}) ? $ENV{PATH_INFO} : '');
Branch: 2 branches
BranchTrueFalseTotal
$r ? : 0 0 0
defined $ENV{'PATH_INFO'} ? : 0 0 0
1046 0 $$self{_path_info} = $path_info;
1047 0 return $path_info;
1048 }
1049 6 *path_info = \&getPathInfo;
1050 6 *get_path_info = \&getPathInfo;
1051  
1052 =pod
1053  
1054 =head2 getRemoteAddr(), remote_addr(), get_remote_addr()
1055  
1056 Returns the dotted decimal representation of the remote client's
1057 IP address.
1058  
1059 =cut
1060 # added for v0.07
1061 0 ▶ sub getRemoteAddr {
Subroutine getRemoteAddr: not called CC=1 SCAR=6.9
Pod documented
1062 0 my $self = shift;
1063 0 return $self->_fromCgiOrModPerlConnection('remote_ip', 'REMOTE_ADDR');
1064 }
1065 6 *remote_addr = \&getRemoteAddr;
1066 6 *get_remote_addr = \&getRemoteAddr;
1067  
1068 =pod
1069  
1070 =head2 getRemoteHost(), remote_host(), get_remote_host()
1071  
1072 Returns the name of the remote host, or its IP address if the
1073 name is unavailable.
1074  
1075 =cut
1076 # added for v0.07
1077 0 ▶ sub getRemoteHost {
Subroutine getRemoteHost: not called CC=3 SCAR=24.8
Pod documented
1078 0 my $self = shift;
1079  
1080 0 my $host = $self->_fromCgiOrModPerl('remote_host', 'REMOTE_HOST');
1081 0 ▶ unless (defined($host) and $host ne '') {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $host and $host ne "") 0 0 0
Condition0%0/3
defined $host and $host ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $host and $host ne ""
defined $host $host ne ""
Truth table0%0/3
A and B
A= defined $hostB= $host ne ""
ABresult
0X0
100
111
1082 0 $host = $self->_fromCgiOrModPerlConnection('remote_ip', 'REMOTE_ADDR');
1083 }
1084  
1085 0 return $host;
1086 }
1087 6 *remote_host = \&getRemoteHost;
1088 6 *get_remote_host = \&getRemoteHost;
1089  
1090 =pod
1091  
1092 =head2 getHost(), host(), virtual_host(), get_host()
1093  
1094 Returns the name of the host in the URL being accessed. This is
1095 sent as the Host header by the web browser.
1096  
1097 =cut
1098 # added for v0.07
1099 10 ▶ sub getHost {
Subroutine getHost: called CC=1 SCAR=0.0
Pod documented
1100 10 my $self = shift;
1101 10 return $self->_fromCgiOrModPerl('hostname', 'HTTP_HOST');
1102 }
1103 6 *host = \&getHost;
1104 6 *virtual_host = \&getHost;
1105 6 *get_host = \&getHost;
1106  
1107 =pod
1108  
1109 =head2 getReferer(), referer(), get_referer(), getReferrer(), referrer(), get_referrer()
1110  
1111 Returns the referring URL.
1112  
1113 =cut
1114 # added for v0.07
1115 0 ▶ sub getReferer {
Subroutine getReferer: not called CC=1 SCAR=6.9
Pod documented
1116 0 my $self = shift;
1117  
1118 0 return $self->_getHttpHeader('Referer');
1119 }
1120 6 *referer = \&getReferer;
1121 6 *get_referer = \&getReferer;
1122 6 *getReferrer = \&getReferer;
1123 6 *referrer = \&getReferer;
1124 6 *get_referrer = \&getReferer;
1125  
1126 =pod
1127  
1128 =head2 getProtocol(), protocol(), get_protocol()
1129  
1130 Returns the protocol, i.e., http or https.
1131  
1132 =cut
1133 # added for v0.07
1134 10 ▶ sub getProtocol {
Subroutine getProtocol: called CC=5 SCAR=16.8
Pod documented
1135 10 my $self = shift;
1136 10 my $https = $ENV{HTTPS};
1137 10 ▶ my $proto = (defined($https) and lc($https) eq 'on') ? 'https' : 'http';
Branch: 1 branch
BranchTrueFalseTotal
defined $https && lc $https eq 'on' ? : 4 6 10
Condition66%2/3
defined $https and lc $https eq "on"
!ll&&!rl&&r
604
MC/DC50%1/2
defined $https and lc $https eq "on"
defined $https lc $https eq "on"
Truth table66%2/3
A and B
A= defined $httpsB= lc $https eq "on"
ABresult
0X0
100
111
1138 10 my $port = $self->_fromCgiOrModPerl('get_server_port', 'SERVER_PORT');
1139 10 ▶ $proto = 'https' if defined($port) and $port == 443;
Branch: 1 branch
BranchTrueFalseTotal
if defined $port and $port == 443 0 10 10
Condition33%1/3
defined $port and $port == 443
!ll&&!rl&&r
0100
MC/DC0%0/2
defined $port and $port == 443
defined $port $port == 443
Truth table33%1/3
A and B
A= defined $portB= $port == 443
ABresult
0X0
100
111
1140
1141 10 return $proto;
1142 }
1143 6 *protocol = \&getProtocol;
1144 6 *get_protocol = \&getProtocol;
1145  
1146 =pod
1147  
1148 =head2 getRequestMethod(), request_method(), get_request_method()
1149  
1150 Returns the request method, i.e., GET, POST, HEAD, or PUT.
1151  
1152 =cut
1153 # added for 0.06
1154 0 ▶ sub getRequestMethod {
Subroutine getRequestMethod: not called CC=1 SCAR=6.9
Pod documented
1155 0 my $self = shift;
1156 0 return $self->_fromCgiOrModPerl('method', 'REQUEST_METHOD');
1157 }
1158 6 *request_method = \&getRequestMethod;
1159 6 *get_request_method = \&getRequestMethod;
1160  
1161 =pod
1162  
1163 =head2 getContentType(), content_type(), get_content_type()
1164  
1165 Returns the content type.
1166  
1167 =cut
1168 # added for 0.06
1169 0 ▶ sub getContentType {
Subroutine getContentType: not called CC=2 SCAR=17.9
Pod documented
1170 0 my $self = shift;
1171 0 ▶ if ($self->_isModPerl) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_isModPerl) { } 0 0 0
1172 0 return $self->_getHttpHeader('Content-Type');
1173 } else {
1174 0 return $ENV{CONTENT_TYPE};
1175 }
1176 }
1177 6 *content_type = \&getContentType;
1178 6 *get_content_type = \&getContentType;
1179  
1180 =pod
1181  
1182 =head2 getPathTranslated(), path_translated(), get_path_translated()
1183  
1184 Returns the physical path information if provided in the CGI environment.
1185  
1186 =cut
1187 # added for 0.06
1188 0 ▶ sub getPathTranslated {
Subroutine getPathTranslated: not called CC=1 SCAR=6.9
Pod documented
1189 0 my $self = shift;
1190 0 return $self->_fromCgiOrModPerl('filename', 'PATH_TRANSLATED');
1191 }
1192 6 *path_translated = \&getPathTranslated;
1193 6 *get_path_translated = \&getPathTranslated;
1194  
1195 =pod
1196  
1197 =head2 getQueryString(), query_string(), get_query_string()
1198  
1199 Returns a query string created from the current parameters.
1200  
1201 =cut
1202 # create a query string from current CGI params
1203 # added for 0.06
1204 0 ▶ sub getQueryString {
Subroutine getQueryString: not called CC=1 SCAR=6.9
Pod documented
1205 0 my ($self) = @_;
1206 0 my $fields = $self->getVars;
1207 0 return $self->urlEncodeVars($fields);
1208 }
1209 6 *query_string = \&getQueryString;
1210 6 *get_query_string = \&getQueryString;
1211  
1212 =pod
1213  
1214 =head2 getHeader(@args)
1215  
1216 Generates HTTP headers. Standard arguments are content_type,
1217 cookie, target, expires, and charset. These should be passed as
1218 name/value pairs. If only one argument is passed, it is assumed
1219 to be the 'content_type' argument. If no values are passed, the
1220 content type is assumed to be 'text/html'. The charset defaults
1221 to ISO-8859-1. A hash reference can also be passed. E.g.,
1222  
1223 print $cgi_obj->getHeader({ content_type => 'text/html', expires => '+3d' });
1224  
1225 The names 'content-type', and 'type' are aliases for
1226 'content_type'. The arguments may also be passed CGI.pm style
1227 with a '-' in front, e.g.
1228  
1229 print $cgi_obj->getHeader( -content_type => 'text/html', -expires => '+3d' );
1230  
1231 Cookies may be passed with the 'cookies' key either as a string,
1232 a hash ref, or as a CGI::Cookies object, e.g.
1233  
1234 my $cookie = { name => 'my_cookie', value => 'cookie_val' };
1235 print $cgi_obj->getHeader(cookies => $cookie);
1236  
1237 You may also pass an array of cookies, e.g.,
1238  
1239 print $cgi_obj->getHeader(cookies => [ $cookie1, $cookie2 ]);
1240  
1241 Aliases: header(), get_header
1242  
1243 =cut
1244 0 ▶ sub getHeader {
Subroutine getHeader: not called CC=28 SCAR=67.0
Pod documented
1245 0 my ($self, @args) = @_;
1246 0 my $arg_count = scalar(@args);
1247 0 ▶ if ($arg_count == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg_count == 0) 0 0 0
1248 0 return "Content-Type: text/html\r\n\r\n";
1249 }
1250 0 ▶ if ($arg_count == 1 and ref($args[0]) ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg_count == 1 and ref $args[0] ne "HASH") 0 0 0
Condition0%0/3
$arg_count == 1 and ref $args[0] ne "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
$arg_count == 1 and ref $args[0] ne "HASH"
$arg_count == 1 ref $args[0] ne "HASH"
Truth table0%0/3
A and B
A= $arg_count == 1B= ref $args[0] ne "HASH"
ABresult
0X0
100
111
1251 # content-type provided
1252 0 return "Content-Type: $args[0]\r\n\r\n";
1253 }
1254  
1255 0 my $map_list = [ [ 'type', 'content-type', 'content_type' ],
1256 'status',
1257 [ 'cookie', 'cookies' ],
1258 'target', 'expires', 'nph', 'charset', 'attachment',
1259 'mod_perl',
1260 ];
1261 0 my ($params, $extras) = $self->_parse_sub_params($map_list, \@args);
1262
1263 0 ▶ my $charset = $$params{charset} || 'ISO-8859-1';
Condition0%0/2
$params->{'charset'} || 'ISO-8859-1'
l!l
00
MC/DC0%0/1
$params->{'charset'} || 'ISO-8859-1'
$params->{'charset'}
Truth table0%0/2
A
A= $params->{'charset'}
Aresult
11
00
1264 0 my $content_type = $$params{type};
1265 0 ▶ $content_type ||= 'text/html' unless defined($content_type);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content_type 0 0 0
Condition0%0/2
$content_type ||= "text/html"
l!l
00
MC/DC0%0/1
$content_type ||= "text/html"
$content_type
Truth table0%0/2
A
A= $content_type
Aresult
11
00
1266 0 ▶ $content_type .= "; charset=$charset"
Branch: 1 branch
BranchTrueFalseTotal
if $content_type =~ /^text/ and not $content_type =~ /\bcharset\b/ 0 0 0
Condition0%0/3
$content_type =~ /^text/ and not $content_type =~ /\bcharset\b/
!ll&&!rl&&r
000
MC/DC0%0/2
$content_type =~ /^text/ and not $content_type =~ /\bcharset\b/
$content_type =~ /^text/ not $content_type =~ /\bcharset\b/
Truth table0%0/3
A and not(B)
A= $content_type =~ /^text/B= not $content_type =~ /\bcharset\b/
ABresult
0X0
100
111
1267 if $content_type =~ /^text/ and $content_type !~ /\bcharset\b/;
1268  
1269 # FIXME: handle NPH stuff
1270  
1271 0 my $headers = [];
1272 0 ▶ push @$headers, "Status: $$params{status}" if defined($$params{status});
Branch: 1 branch
BranchTrueFalseTotal
if defined $params->{"status"} 0 0 0
1273 0 ▶ push @$headers, "Window-Target: $$params{target}" if defined($$params{target});
Branch: 1 branch
BranchTrueFalseTotal
if defined $params->{"target"} 0 0 0
1274
1275 0 my $cookies = $$params{cookie};
1276 0 ▶ if (defined($cookies) and $cookies) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cookies and $cookies) 0 0 0
Condition0%0/3
defined $cookies and $cookies
!ll&&!rl&&r
000
MC/DC0%0/2
defined $cookies and $cookies
defined $cookies $cookies
Truth table0%0/3
A and B
A= defined $cookiesB= $cookies
ABresult
0X0
100
111
1277 0 ▶ my $cookie_array = ref($cookies) eq 'ARRAY' ? $cookies : [ $cookies ];
Branch: 1 branch
BranchTrueFalseTotal
ref $cookies eq 'ARRAY' ? : 0 0 0
1278 0 foreach my $cookie (@$cookie_array) {
1279 # handle plain strings as well as CGI::Cookie objects and hashes
1280 0 my $str = '';
1281 0 ▶ if (UNIVERSAL::isa($cookie, 'CGI::Cookie')) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($cookie, 'CGI::Cookie')) { } 0 0 0
elsif (ref $cookie eq 'HASH') { } 0 0 0
1282 0 $str = $cookie->as_string;
1283 } elsif (ref($cookie) eq 'HASH') {
1284 0 $str = $self->_createCookieStrFromHash($cookie);
1285 } else {
1286 0 $str = $cookie;
1287 }
1288 0 ▶ push @$headers, "Set-Cookie: $str" unless $str eq '';
Branch: 1 branch
BranchTrueFalseTotal
unless $str eq "" 0 0 0
1289 }
1290 }
1291  
1292 0 ▶ if (defined($$params{expires})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params->{"expires"}) 0 0 0
1293 0 my $expire = $self->_canonicalizeHttpDate($$params{expires});
1294 0 push @$headers, "Expires: $expire";
1295 }
1296  
1297 0 ▶ if (defined($$params{expires}) or (defined($cookies) and $cookies)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params->{"expires"} or defined $cookies and $cookies) 0 0 0
Condition0%0/6
defined $params->{"expires"} or defined $cookies and $cookies
l!l&&r!l&&!r
000
defined $cookies and $cookies
!ll&&!rl&&r
000
MC/DC0%0/3
defined $params->{"expires"} or defined $cookies and $cookies
defined $params->{"expires"} defined $cookies $cookies
Truth table0%0/4
A or B and C
A= defined $params->{"expires"}B= defined $cookiesC= $cookies
ABCresult
1XX1
0111
00X0
0100
1298 0 push @$headers, "Date: " . $self->_canonicalizeHttpDate(0);
1299 }
1300
1301 push @$headers, qq{Content-Disposition: attachment; filename="$$params{attachment}"}
1302 0 ▶ if defined($$params{attachment});
Branch: 1 branch
BranchTrueFalseTotal
if defined $params->{"attachment"} 0 0 0
1303 0 ▶ push @$headers, "Content-Type: $content_type" if defined($content_type) and $content_type ne '';
Branch: 1 branch
BranchTrueFalseTotal
if defined $content_type and $content_type ne "" 0 0 0
Condition0%0/3
defined $content_type and $content_type ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $content_type and $content_type ne ""
defined $content_type $content_type ne ""
Truth table0%0/3
A and B
A= defined $content_typeB= $content_type ne ""
ABresult
0X0
100
111
1304  
1305 0 ▶ if ($params->{mod_perl}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params->{"mod_perl"}) 0 0 0
1306 0 my $header_list = [];
1307
1308 0 foreach my $field (sort keys %$extras) {
1309 0 my $val = $$extras{$field};
1310 0 $field =~ s/\b(.)/\U$1/g;
1311 0 $field = ucfirst($field);
1312 0 push @$header_list, [ $field, $val ];
1313 }
1314  
1315 0 return $header_list;
1316 }
1317
1318 0 foreach my $field (sort keys %$extras) {
1319 0 my $val = $$extras{$field};
1320 0 $field =~ s/\b(.)/\U$1/g;
1321 0 $field = ucfirst($field);
1322 0 push @$headers, "$field: $val";
1323 }
1324
1325 # FIXME: make line endings work on windoze
1326 0 return join("\r\n", @$headers) . "\r\n\r\n";
1327 }
1328 6 *header = \&getHeader;
1329 6 *get_header = \&getHeader;
1330  
1331 =pod
1332  
1333 =head2 sendHeader(@args)
1334  
1335 Like getHeader() above, except sends it. Under mod_perl, this
1336 sends the header(s) via the Apache request object. In a CGI
1337 environment, this prints the header(s) to STDOUT.
1338  
1339 Aliases: send_header()
1340  
1341 =cut
1342 0 ▶ sub sendHeader {
Subroutine sendHeader: not called CC=14 SCAR=53.5
Pod documented
1343 0 my ($self, @args) = @_;
1344 0 my $mod_perl = 0;
1345 0 my $r;
1346 0 ▶ if ($self->_isModPerl and $r = $self->_getApacheRequest) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_isModPerl and $r = $self->_getApacheRequest) 0 0 0
Condition0%0/3
$self->_isModPerl and $r = $self->_getApacheRequest
!ll&&!rl&&r
000
MC/DC0%0/2
$self->_isModPerl and $r = $self->_getApacheRequest
$self->_isModPerl $r = $self->_getApacheRequest
Truth table0%0/3
A and B
A= $self->_isModPerlB= $r = $self->_getApacheRequest
ABresult
0X0
100
111
1347 0 $mod_perl = 1;
1348 }
1349
1350 0 my $arg_count = scalar(@args);
1351 0 ▶ if ($arg_count == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg_count == 0) 0 0 0
1352 0 ▶ if ($mod_perl) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mod_perl) { } 0 0 0
1353 0 $r->err_header_out('Content-Type' => 'text/html');
1354 } else {
1355 0 print STDOUT "Content-Type: text/html\r\n\r\n";
1356 }
1357 0 return 1;
1358 }
1359
1360 0 ▶ if ($arg_count == 1 and ref($args[0]) ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg_count == 1 and ref $args[0] ne "HASH") 0 0 0
Condition0%0/3
$arg_count == 1 and ref $args[0] ne "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
$arg_count == 1 and ref $args[0] ne "HASH"
$arg_count == 1 ref $args[0] ne "HASH"
Truth table0%0/3
A and B
A= $arg_count == 1B= ref $args[0] ne "HASH"
ABresult
0X0
100
111
1361 # content-type provided
1362 0 ▶ if ($mod_perl) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mod_perl) { } 0 0 0
1363 0 $r->err_header_out('Content-Type' => $args[0]);
1364 } else {
1365 0 print STDOUT "Content-Type: $args[0]\r\n\r\n";
1366 }
1367
1368 0 return 1;
1369 }
1370  
1371 0 ▶ unless ($mod_perl) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($mod_perl) 0 0 0
1372 0 my $str = $self->getHeader(@args);
1373 0 print STDOUT $str;
1374 0 return 1;
1375 }
1376  
1377 0 ▶ return undef unless $r;
Branch: 1 branch
BranchTrueFalseTotal
unless $r 0 0 0
1378  
1379 0 my $headers = [];
1380 0 ▶ if (ref($args[0]) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args[0] eq 'HASH') { } 0 0 0
1381 0 my %args = %{$args[0]};
1382 0 $args{mod_perl} = 1;
1383 0 $headers = $self->getHeader(\%args);
1384 } else {
1385 0 push @args, 'mod_perl', 1;
1386 0 $headers = $self->getHeader(@args);
1387 }
1388  
1389 0 my $rv = $self->apache_ok;
1390 0 foreach my $header (@$headers) {
1391 0 ▶ if (lc($header->[0]) eq 'set-cookie') {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $header->[0] eq 'set-cookie') { } 0 0 0
1392 0 $r->err_headers_out()->add(@$header);
1393 }
1394 else {
1395 0 ▶ if (lc($header->[0]) eq 'location') {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $header->[0] eq "location") 0 0 0
1396 0 $rv = $self->apache_redirect;
1397 }
1398 0 $r->err_header_out(@$header);
1399 }
1400 }
1401  
1402 0 return $rv;
1403 }
1404 6 *send_header = \&sendHeader;
1405  
1406 0 ▶ sub load_apache_constants {
Subroutine load_apache_constants: not called CC=2 SCAR=17.9
Pod undocumented
1407 0 ▶ unless (defined $CGI::Utils::Loaded_Apache_Constants) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $CGI::Utils::Loaded_Apache_Constants) 0 0 0
1408 0 local($SIG{__DIE__});
1409 0 eval q{
1410 use mod_perl;
1411 use constant MP2 => $mod_perl::VERSION >= 1.99;
1412 if (defined(MP2)) {
1413 if (MP2) {
1414 require Apache2;
1415 require Apache::Const;
1416 }
1417 else {
1418 require Apache::Constants;
1419 }
1420 $CGI::Utils::Loaded_Apache_Constants = 1;
1421 }
1422 };
1423 }
1424 }
1425
1426  
1427 =pod
1428  
1429 =head2 getRedirect($url)
1430  
1431 Returns the header required to do a redirect. This method also
1432 accepts named arguments, e.g.,
1433  
1434 print $cgi_obj->getRedirect(url => $url, status => 302,
1435 cookie => \%cookie_params);
1436  
1437 You may also pass a cookies argument as in getHeader().
1438  
1439 Aliases: redirect()
1440  
1441 =cut
1442 0 ▶ sub getRedirect {
Subroutine getRedirect: not called CC=2 SCAR=17.9
Pod documented
1443 0 my ($self, @args) = @_;
1444 0 my $map_list = [ [ 'location', 'uri', 'url' ],
1445 'status',
1446 [ 'cookie', 'cookies' ],
1447 'target',
1448 ];
1449 0 my ($params, $extras) = $self->_parse_sub_params($map_list, \@args);
1450 0 ▶ $params->{status} = 302 unless $params->{status};
Branch: 1 branch
BranchTrueFalseTotal
unless $params->{"status"} 0 0 0
1451 0 return $self->header({ type => '', %$params, %$extras });
1452 }
1453 6 *redirect = \&getRedirect;
1454  
1455 =pod
1456  
1457 =head2 sendRedirect($url)
1458  
1459 Like getRedirect(), but in a CGI environment the output is sent
1460 to STDOUT, and in a mod_perl environment, the appropriate
1461 headers are set. The return value is 1 for a CGI environment
1462 when successful, and Apache::Constants::REDIRECT in a mod_perl
1463 environment, so you can do something like
1464  
1465 return $utils->sendRedirect($url)
1466  
1467 n a mod_perl handler.
1468  
1469 Aliases: send_redirect()
1470  
1471 =cut
1472 0 ▶ sub send_redirect {
Subroutine send_redirect: not called CC=2 SCAR=17.9
Pod undocumented
1473 0 my ($self, @args) = @_;
1474 0 my $map_list = [ [ 'location', 'uri', 'url' ],
1475 'status',
1476 [ 'cookie', 'cookies' ],
1477 'target',
1478 ];
1479 0 my ($params, $extras) = $self->_parse_sub_params($map_list, \@args);
1480 0 ▶ $params->{status} = 302 unless $params->{status};
Branch: 1 branch
BranchTrueFalseTotal
unless $params->{"status"} 0 0 0
1481 0 return $self->send_header({ type => '', %$params, %$extras });
1482 }
1483 6 *sendRedirect = \&send_redirect;
1484  
1485 =pod
1486  
1487 =head2 getLocalRedirect(), local_redirect(), get_local_redirect()
1488  
1489 Like getRedirect(), except that the redirect URL is converted
1490 from relative to absolute, including the host.
1491  
1492 =cut
1493 # Added for v0.07
1494 0 ▶ sub getLocalRedirect {
Subroutine getLocalRedirect: not called CC=2 SCAR=17.9
Pod documented
1495 0 my ($self, @args) = @_;
1496 0 my $map_list = [ [ 'location', 'uri', 'url' ],
1497 'status',
1498 [ 'cookie', 'cookies' ],
1499 'target',
1500 ];
1501 0 my ($params, $extras) = $self->_parse_sub_params($map_list, \@args);
1502 0 ▶ unless ($params->{location} =~ m{^https?://}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($params->{"location"} =~ m[^https?://]) 0 0 0
1503 0 $params->{location} = $self->convertRelativeUrlWithParams($params->{location}, {});
1504 }
1505 0 return $self->getRedirect(%$params);
1506 }
1507 6 *local_redirect = \&getLocalRedirect;
1508 6 *get_local_redirect = \&getLocalRedirect;
1509  
1510 =pod
1511  
1512 =head2 getCookieString(\%hash), get_cookie_string(\%hash);
1513  
1514 Returns a string to pass as the value of a 'Set-Cookie' header.
1515  
1516 =cut
1517 0 ▶ sub getCookieString {
Subroutine getCookieString: not called CC=1 SCAR=6.9
Pod documented
1518 0 my ($self, $hash) = @_;
1519 0 return $self->_createCookieStrFromHash($hash);
1520 }
1521 6 *get_cookie_string = \&getCookieString;
1522  
1523 =pod
1524  
1525 =head2 getSetCookieString(\%params), getSetCookieString([ \%params1, \%params2 ])
1526  
1527 Returns a string to pass as the 'Set-Cookie' header(s), including
1528 the line ending(s). Also accepts a simple hash with key/value pairs.
1529  
1530 =cut
1531 0 ▶ sub getSetCookieString {
Subroutine getSetCookieString: not called CC=7 SCAR=40.3
Pod documented
1532 0 my ($self, $cookies) = @_;
1533 0 ▶ if (ref($cookies) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cookies eq "HASH") 0 0 0
1534 0 my $array = [ map { { name => $_, value => $cookies->{$_} } } keys %$cookies ];
1535 0 $cookies = $array;
1536 }
1537 0 ▶ my $cookie_array = ref($cookies) eq 'ARRAY' ? $cookies : [ $cookies ];
Branch: 1 branch
BranchTrueFalseTotal
ref $cookies eq 'ARRAY' ? : 0 0 0
1538  
1539 0 my $headers = [];
1540 0 foreach my $cookie (@$cookie_array) {
1541 # handle plain strings as well as CGI::Cookie objects and hashes
1542 0 my $str = '';
1543 0 ▶ if (UNIVERSAL::isa($cookie, 'CGI::Cookie')) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($cookie, 'CGI::Cookie')) { } 0 0 0
elsif (ref $cookie eq 'HASH') { } 0 0 0
1544 0 $str = $cookie->as_string;
1545 } elsif (ref($cookie) eq 'HASH') {
1546 0 $str = $self->_createCookieStrFromHash($cookie);
1547 } else {
1548 0 $str = $cookie;
1549 }
1550 0 ▶ push @$headers, "Set-Cookie: $str" unless $str eq '';
Branch: 1 branch
BranchTrueFalseTotal
unless $str eq "" 0 0 0
1551 }
1552  
1553 # FIXME: make line endings work on windoze
1554 0 return join("\r\n", @$headers) . "\r\n";
1555 }
1556 6 *get_set_cookie_string = \&getSetCookieString;
1557  
1558 =pod
1559  
1560 =head2 setCookie(\%params), set_cookie(\%params);
1561  
1562 Sets the cookie generated by getCookieString. That is, in a
1563 mod_perl environment, it adds an outgoing header to set the
1564 cookie. In a CGI environment, it prints the value of
1565 getSetCookieString to STDOUT (including the end-of-line
1566 sequence).
1567  
1568 =cut
1569 0 ▶ sub setCookie {
Subroutine setCookie: not called CC=2 SCAR=17.9
Pod documented
1570 0 my $self = shift;
1571 0 my $params = shift;
1572  
1573 0 my $str = $self->_createCookieStrFromHash($params);
1574 0 my $r = $self->_getApacheRequest;
1575  
1576 0 ▶ if ($r) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r) { } 0 0 0
1577 0 $r->err_headers_out()->add('Set-Cookie' => $str);
1578 }
1579 else {
1580 0 print STDOUT "Set-Cookie: $str\r\n";
1581 }
1582 }
1583 6 *set_cookie = \&setCookie;
1584
1585 0 ▶ sub _createCookieStrFromHash {
Subroutine _createCookieStrFromHash: not called CC=8 SCAR=42.8
1586 0 my ($self, $hash) = @_;
1587 0 my $pairs = [];
1588  
1589 0 my $map_list = [ 'name', [ 'value', 'values', 'val' ],
1590 'path', 'expires', 'domain', 'secure',
1591 ];
1592 0 my $params = $self->_parse_sub_params($map_list, [ $hash ]);
1593  
1594 0 my $value = $$params{value};
1595 0 ▶ if (my $ref = ref($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref = ref $value) { } 0 0 0
1596 0 ▶ if ($ref eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref eq 'ARRAY') { } 0 0 0
elsif ($ref eq 'HASH') { } 0 0 0
1597 0 $value = join('&', map { $self->urlEncode($_) } @$value);
1598 } elsif ($ref eq 'HASH') {
1599 0 $value = join('&', map { $self->urlEncode($_) } %$value);
1600 }
1601 } else {
1602 0 $value = $self->urlEncode($value);
1603 }
1604 0 push @$pairs, qq{$$params{name}=$value};
1605  
1606 0 ▶ my $path = $$params{path} || '/';
Condition0%0/2
$params->{'path'} || '/'
l!l
00
MC/DC0%0/1
$params->{'path'} || '/'
$params->{'path'}
Truth table0%0/2
A
A= $params->{'path'}
Aresult
11
00
1607 0 push @$pairs, qq{path=$path};
1608
1609 0 ▶ push @$pairs, qq{domain=$$params{domain}} if $$params{domain};
Branch: 1 branch
BranchTrueFalseTotal
if $params->{"domain"} 0 0 0
1610  
1611 0 ▶ if ($$params{expires}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params->{"expires"}) 0 0 0
1612 0 my $expire = $self->_canonicalizeCookieDate($$params{expires});
1613 0 push @$pairs, qq{expires=$expire};
1614 }
1615  
1616 0 ▶ push @$pairs, qq{secure} if $$params{secure};
Branch: 1 branch
BranchTrueFalseTotal
if $params->{"secure"} 0 0 0
1617  
1618 0 return join('; ', @$pairs);
1619 }
1620
1621 0 ▶ sub _canonicalizeCookieDate {
Subroutine _canonicalizeCookieDate: not called CC=1 SCAR=6.9
1622 0 my ($self, $expire) = @_;
1623 0 return $self->_canonicalizeDate('-', $expire);
1624 }
1625
1626 0 ▶ sub _canonicalizeHttpDate {
Subroutine _canonicalizeHttpDate: not called CC=3 SCAR=24.8
1627 0 my ($self, $expire) = @_;
1628 0 return $self->_canonicalizeDate(' ', $expire);
1629
1630 0 my $time = $self->_get_expire_time_from_offset($expire);
1631 0 ▶ return $time unless $time =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $time =~ /^\d+$/ 0 0 0
1632  
1633 0 my $wdays = [ qw(Sun Mon Tue Wed Thu Fri Sat) ];
1634 0 my $months = [ qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec) ];
1635
1636 0 my $sep = ' ';
1637  
1638 0 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime($time);
1639 0 ▶ $year += 1900 unless $year > 1000;
Branch: 1 branch
BranchTrueFalseTotal
unless $year > 1000 0 0 0
1640 0 return sprintf "%s, %02d$sep%s$sep%04d %02d:%02d:%02d GMT",
1641 $$wdays[$wday], $mday, $$months[$mon], $year, $hour, $min, $sec;
1642 }
1643  
1644 0 ▶ sub _canonicalizeDate {
Subroutine _canonicalizeDate: not called CC=3 SCAR=24.8
1645 0 my ($self, $sep, $expire) = @_;
1646 0 my $time = $self->_get_expire_time_from_offset($expire);
1647 0 ▶ return $time unless $time =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $time =~ /^\d+$/ 0 0 0
1648  
1649 0 my $wdays = [ qw(Sun Mon Tue Wed Thu Fri Sat) ];
1650 0 my $months = [ qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec) ];
1651  
1652 0 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime($time);
1653 0 ▶ $year += 1900 unless $year > 1000;
Branch: 1 branch
BranchTrueFalseTotal
unless $year > 1000 0 0 0
1654 0 return sprintf "%s, %02d$sep%s$sep%04d %02d:%02d:%02d GMT",
1655 $$wdays[$wday], $mday, $$months[$mon], $year, $hour, $min, $sec;
1656  
1657 }
1658  
1659 0 ▶ sub _get_expire_time_from_offset {
Subroutine _get_expire_time_from_offset: not called CC=6 SCAR=37.4
1660 0 my ($self, $offset) = @_;
1661 0 my $ret_offset = 0;
1662 0 ▶ if (not $offset or lc($offset) eq 'now') {
Branch: 3 branches
BranchTrueFalseTotal
if (not $offset or lc $offset eq 'now') { } 0 0 0
elsif ($offset =~ /^\d+$/) { } 0 0 0
elsif ($offset =~ /^([-+]?(?:\d+|\d*\.\d*))([mhdMy]?)/) { } 0 0 0
Condition0%0/3
not $offset or lc $offset eq "now"
l!l&&r!l&&!r
000
MC/DC0%0/2
not $offset or lc $offset eq "now"
not $offset lc $offset eq "now"
Truth table0%0/3
not(A) or B
A= not $offsetB= lc $offset eq "now"
ABresult
1X1
011
000
1663 0 $ret_offset = 0;
1664 } elsif ($offset =~ /^\d+$/) {
1665 0 return $offset;
1666 } elsif ($offset =~ /^([-+]?(?:\d+|\d*\.\d*))([mhdMy]?)/) {
1667 0 my $map = { 's' => 1,
1668 'm' => 60,
1669 'h' => 60 * 60,
1670 'd' => 60 * 60 * 24,
1671 'M' => 60 * 60 * 24 * 30,
1672 'y' => 60 * 60 * 24 * 365,
1673 };
1674 0 ▶ $ret_offset = ($$map{$2} || 1) * $1;
Condition0%0/2
$map->{$2} || 1
l!l
00
MC/DC0%0/1
$map->{$2} || 1
$map->{$2}
Truth table0%0/2
A
A= $map->{$2}
Aresult
11
00
1675 } else {
1676 0 $ret_offset = $offset;
1677 }
1678  
1679 0 return time() + $ret_offset;
1680 }
1681
1682 # canonicalize parameters so we can be compatible with CGI.pm
1683 0 ▶ sub _parse_sub_params {
Subroutine _parse_sub_params: not called CC=15 SCAR=54.8
1684 0 my ($self, $map_list, $args) = @_;
1685  
1686 0 my $arg_count = scalar(@$args);
1687 0 ▶ return {} if $arg_count == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $arg_count == 0 0 0 0
1688  
1689 0 my $hash;
1690 0 ▶ if ($arg_count == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg_count == 1) { } 0 0 0
1691 0 ▶ if (ref($$args[0]) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args->[0] eq 'HASH') { } 0 0 0
1692 0 $hash = $$args[0];
1693 } else {
1694 0 my $rv;
1695 0 ▶ if (ref($$map_list[0]) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $map_list->[0] eq 'ARRAY') { } 0 0 0
1696 0 $rv = { $$map_list[0][0] => $$args[0] };
1697 } else {
1698 0 $rv = { $$map_list[0] => $$args[0] };
1699 }
1700 0 ▶ return wantarray ? ($rv, {}) : $rv;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
1701 }
1702 } else {
1703 0 $hash = { @$args };
1704 }
1705  
1706 0 my $return_hash = {};
1707 0 my $found = {};
1708 0 foreach my $key (keys %$hash) {
1709 0 my $orig_key = $key;
1710 0 $key =~ s/^-{1,2}//;
1711 0 $key = lc($key);
1712 0 foreach my $e (@$map_list) {
1713 0 ▶ if (ref($e) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $e eq 'ARRAY') { } 0 0 0
1714 0 my $canon_key = $$e[0];
1715 0 foreach my $e2 (@$e) {
1716 0 ▶ if ($e2 eq $key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e2 eq $key) 0 0 0
1717 0 $$return_hash{$canon_key} = $$hash{$orig_key};
1718 0 $$found{$orig_key} = 1;
1719 }
1720 }
1721 } else {
1722 0 ▶ if ($e eq $key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e eq $key) 0 0 0
1723 0 $$return_hash{$e} = $$hash{$orig_key};
1724 0 $$found{$orig_key} = 1;
1725 }
1726 }
1727 }
1728 }
1729  
1730 0 my $left_overs = {};
1731 0 ▶ while (my ($key, $value) = each %$hash) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$hash) 0 0 0
1732 0 ▶ $$left_overs{$key} = $value unless exists($$found{$key});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $found->{$key} 0 0 0
1733 }
1734  
1735 0 ▶ return wantarray ? ($return_hash, $left_overs) : $return_hash;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
1736 }
1737
1738 0 ▶ sub TIEHASH {
Subroutine TIEHASH: not called CC=1 SCAR=6.9
1739 0 my ($proto, $obj) = @_;
1740 0 return $obj;
1741 }
1742  
1743 0 ▶ sub STORE {
Subroutine STORE: not called CC=1 SCAR=6.9
1744 0 my ($self, $key, $val) = @_;
1745 0 my $params = $$self{_params};
1746 # FIXME: memory leak here - need to compress the array if has empty slots
1747 # push(@{$$self{_param_order}}, $key) unless exists($$params{$key});
1748 0 $$params{$key} = $val;
1749 }
1750  
1751 0 ▶ sub FETCH {
Subroutine FETCH: not called CC=3 SCAR=24.8
1752 0 my ($self, $key) = @_;
1753 0 my $params = $$self{_params};
1754 0 my $val = $$params{$key};
1755 0 ▶ if (ref($val) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val eq "ARRAY") 0 0 0
1756 0 my $delimiter = $$self{_multivalue_delimiter};
1757 0 ▶ $val = join($delimiter, @$val) unless $delimiter eq '';
Branch: 1 branch
BranchTrueFalseTotal
unless $delimiter eq "" 0 0 0
1758 }
1759 0 return $val;
1760 }
1761  
1762 0 ▶ sub FIRSTKEY {
Subroutine FIRSTKEY: not called CC=1 SCAR=6.9
1763 0 my ($self) = @_;
1764 0 my @keys = keys %{$$self{_params}};
1765 0 $$self{_keys} = \@keys;
1766 0 return shift @keys;
1767 }
1768  
1769 0 ▶ sub NEXTKEY {
Subroutine NEXTKEY: not called CC=1 SCAR=6.9
1770 0 my ($self) = @_;
1771 0 return shift(@{$$self{_keys}});
1772 }
1773  
1774 0 ▶ sub EXISTS {
Subroutine EXISTS: not called CC=1 SCAR=6.9
1775 0 my ($self, $key) = @_;
1776 0 my $params = $$self{_params};
1777 0 return exists($$params{$key});
1778 }
1779  
1780 0 ▶ sub DELETE {
Subroutine DELETE: not called CC=1 SCAR=6.9
1781 0 my ($self, $key) = @_;
1782 0 my $params = $$self{_params};
1783 0 delete $$params{$key};
1784 }
1785  
1786 0 ▶ sub CLEAR {
Subroutine CLEAR: not called CC=1 SCAR=6.9
1787 0 my ($self) = @_;
1788 0 %{$$self{_params}} = ();
1789 }
1790  
1791 0 ▶ sub _parseParams {
Subroutine _parseParams: not called CC=1 SCAR=6.9
1792 0 my ($self, $query_string) = @_;
1793 0 ($$self{_params}, $$self{_param_order}) = $self->urlDecodeVars($query_string);
1794 }
1795  
1796 0 ▶ sub _readPostData {
Subroutine _readPostData: not called CC=1 SCAR=6.9
1797 0 my ($self, $fh, $buf, $len) = @_;
1798 0 return CORE::read($fh, $$buf, $len);
1799 }
1800  
1801 0 ▶ sub _readMultipartData {
Subroutine _readMultipartData: not called CC=8 SCAR=42.8
1802 0 my ($self, $boundary, $content_length, $fh) = @_;
1803 0 my $line;
1804 0 my $eol = $self->_getEndOfLineSeq;
1805 0 my $end_char = substr($eol, -1, 1);
1806 0 my $buf;
1807 0 my $len = 1024;
1808 0 my $amt_read = 0;
1809 0 my $sep = "--$boundary$eol";
1810  
1811 0 my $params = {};
1812 0 my $param_order = [];
1813  
1814 0 ▶ while (my $size = $self->_read($fh, $buf, $len, 0, $end_char)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $size = $self->_read($fh, $buf, $len, 0, $end_char)) 0 0 0
1815 0 $amt_read += $size;
1816 0 ▶ if ($buf eq $sep) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buf eq $sep) 0 0 0
1817 0 last;
1818 }
1819 0 ▶ last unless $amt_read < $content_length;
Branch: 1 branch
BranchTrueFalseTotal
unless $amt_read < $content_length 0 0 0
1820 }
1821  
1822 0 ▶ while ($amt_read < $content_length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($amt_read < $content_length) 0 0 0
1823 0 my ($headers, $amt) = $self->_readMultipartHeader($fh);
1824 0 $amt_read += $amt;
1825 0 my $disp = $$headers{'content-disposition'};
1826 0 my ($type, @fields) = split /;\s*/, $disp;
1827 0 my %disp_fields = map { s/^(\")(.+)\1$/$2/; $_ } map { split(/=/, $_, 2) } @fields;
1828 0 my $name = $disp_fields{name};
1829 0 my ($body, $body_size) = $self->_readMultipartBody($boundary, $fh, $headers, \%disp_fields);
1830 0 $amt_read += $body_size;
1831  
1832 0 ▶ next if $name eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "" 0 0 0
1833  
1834 0 ▶ if (exists($$params{$name})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params->{$name}) { } 0 0 0
1835 0 my $val = $$params{$name};
1836 0 ▶ if (ref($val) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val eq 'ARRAY') { } 0 0 0
1837 0 push @$val, $body;
1838 } else {
1839 0 my $array = [ $val, $body ];
1840 0 $$params{$name} = $array;
1841 }
1842 } else {
1843 0 $$params{$name} = $body;
1844 0 push @$param_order, $name;
1845 }
1846  
1847 }
1848  
1849 0 $$self{_params} = $params;
1850 0 $$self{_param_order} = $param_order;
1851  
1852 0 return 1;
1853 }
1854  
1855 0 ▶ sub _readMultipartBody {
Subroutine _readMultipartBody: not called CC=7 SCAR=40.3
1856 0 my ($self, $boundary, $fh, $headers, $disposition_fields) = @_;
1857  
1858 0 local($^W) = 0; # turn off lame warnings
1859
1860 0 ▶ if ($$disposition_fields{filename} ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($disposition_fields->{"filename"} ne "") 0 0 0
1861 0 return $self->_readMultipartBodyToFile($boundary, $fh, $headers, $disposition_fields);
1862 }
1863
1864 0 my $amt_read = 0;
1865 0 my $eol = $self->_getEndOfLineSeq;
1866 0 my $end_char = substr($eol, -1, 1);
1867 0 my $buf;
1868 my $body;
1869  
1870 0 ▶ while (my $size = $self->_read($fh, $buf, 4096, 0, $end_char)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $size = $self->_read($fh, $buf, 4096, 0, $end_char)) 0 0 0
1871 0 $amt_read += $size;
1872 0 ▶ if (substr($buf, -1, 1) eq $end_char and $buf =~ /^--$boundary(?:--)?$eol$/
Branch: 1 branch
BranchTrueFalseTotal
if (substr($buf, -1, 1) eq $end_char and $buf =~ /^--$boundary(?:--)?$eol$/ and $body =~ /$eol$/) 0 0 0
Condition0%0/6
substr($buf, -1, 1) eq $end_char and $buf =~ /^--$boundary(?:--)?$eol$/ and $body =~ /$eol$/
!ll&&!rl&&r
000
substr($buf, -1, 1) eq $end_char and $buf =~ /^--$boundary(?:--)?$eol$/
!ll&&!rl&&r
000
MC/DC0%0/3
substr($buf, -1, 1) eq $end_char and $buf =~ /^--$boundary(?:--)?$eol$/ and $body =~ /$eol$/
substr($buf, -1, 1) eq $end_char $buf =~ /^--$boundary(?:--)?$eol$/ $body =~ /$eol$/
Truth table0%0/4
A and B and C
A= substr($buf, -1, 1) eq $end_charB= $buf =~ /^--$boundary(?:--)?$eol$/C= $body =~ /$eol$/
ABCresult
0XX0
10X0
1100
1111
1873 and $body =~ /$eol$/
1874 ) {
1875 0 $body =~ s/$eol$//;
1876 0 last;
1877 }
1878 0 $body .= $buf;
1879 }
1880  
1881 0 ▶ return wantarray ? ($body, $amt_read) : $body;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
1882 }
1883  
1884 0 ▶ sub _readMultipartBodyToFile {
Subroutine _readMultipartBodyToFile: not called CC=7 SCAR=40.3
1885 0 my ($self, $boundary, $fh, $headers, $disposition_fields) = @_;
1886  
1887 0 my $amt_read = 0;
1888 0 my $body;
1889 0 my $eol = $self->_getEndOfLineSeq;
1890 0 my $end_char = substr($eol, -1, 1);
1891 0 my $buf = '';
1892 0 my $buf2 = '';
1893  
1894 0 my $file_name = $$disposition_fields{filename};
1895 0 my $info = { 'Content-Type' => $$headers{'content-type'} };
1896 0 $$self{_upload_info}{$file_name} = $info;
1897  
1898 0 my $out_fh = CGI::Utils::UploadFile->new_tmpfile($file_name);
1899
1900 0 ▶ while (my $size = $self->_read($fh, $buf, 4096, 0, $end_char)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $size = $self->_read($fh, $buf, 4096, 0, $end_char)) 0 0 0
1901 0 $amt_read += $size;
1902 0 ▶ if (substr($buf, -1, 1) eq $end_char and $buf =~ /^--$boundary(?:--)?$eol$/
Branch: 1 branch
BranchTrueFalseTotal
if (substr($buf, -1, 1) eq $end_char and $buf =~ /^--$boundary(?:--)?$eol$/ and $buf2 =~ /$eol$/) 0 0 0
Condition0%0/6
substr($buf, -1, 1) eq $end_char and $buf =~ /^--$boundary(?:--)?$eol$/ and $buf2 =~ /$eol$/
!ll&&!rl&&r
000
substr($buf, -1, 1) eq $end_char and $buf =~ /^--$boundary(?:--)?$eol$/
!ll&&!rl&&r
000
MC/DC0%0/3
substr($buf, -1, 1) eq $end_char and $buf =~ /^--$boundary(?:--)?$eol$/ and $buf2 =~ /$eol$/
substr($buf, -1, 1) eq $end_char $buf =~ /^--$boundary(?:--)?$eol$/ $buf2 =~ /$eol$/
Truth table0%0/4
A and B and C
A= substr($buf, -1, 1) eq $end_charB= $buf =~ /^--$boundary(?:--)?$eol$/C= $buf2 =~ /$eol$/
ABCresult
0XX0
10X0
1100
1111
1903 and $buf2 =~ /$eol$/
1904 ) {
1905 0 $buf2 =~ s/$eol$//;
1906 0 $buf = '';
1907 0 print $out_fh $buf2;
1908 0 last;
1909 }
1910 0 print $out_fh $buf2;
1911 0 $buf2 = $buf;
1912 0 $buf = '';
1913 }
1914 0 ▶ if ($buf ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($buf ne "") 0 0 0
1915 0 print $out_fh $buf;
1916 }
1917 0 select((select($out_fh), $| = 1)[0]);
1918 0 seek($out_fh, 0, 0); # seek back to beginning of file
1919
1920 0 ▶ return wantarray ? ($out_fh, $amt_read) : $out_fh;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
1921 }
1922  
1923 =pod
1924  
1925 =head2 uploadInfo($file_name)
1926  
1927 Returns a reference to a hash containing the header information
1928 sent along with a file upload.
1929  
1930 =cut
1931 # provided for compatibility with CGI.pm
1932 0 ▶ sub uploadInfo {
Subroutine uploadInfo: not called CC=1 SCAR=6.9
Pod documented
1933 0 my ($self, $file_name) = @_;
1934 0 $self->parse;
1935 0 return $$self{_upload_info}{$file_name};
1936 }
1937  
1938 0 ▶ sub _readMultipartHeader {
Subroutine _readMultipartHeader: not called CC=7 SCAR=40.3
1939 0 my ($self, $fh) = @_;
1940 0 my $amt_read = 0;
1941 0 my $eol = $self->_getEndOfLineSeq;
1942 0 my $end_char = substr($eol, -1, 1);
1943 0 my $buf;
1944 my $header_str;
1945 0 ▶ while (my $size = $self->_read($fh, $buf, 4096, 0, $end_char)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $size = $self->_read($fh, $buf, 4096, 0, $end_char)) 0 0 0
1946 0 $amt_read += $size;
1947 0 ▶ last if $buf eq $eol;
Branch: 1 branch
BranchTrueFalseTotal
if $buf eq $eol 0 0 0
1948 0 $header_str .= $buf;
1949 }
1950  
1951 0 my $headers = {};
1952 0 my $last_header;
1953 0 foreach my $line (split($eol, $header_str)) {
1954 0 ▶ if ($line =~ /^(\S+):\s*(.+)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /^(\S+):\s*(.+)$/) { } 0 0 0
elsif ($line =~ /^\s+/) { } 0 0 0
1955 0 $last_header = lc($1);
1956 0 $$headers{$last_header} = $2;
1957 } elsif ($line =~ /^\s+/) {
1958 0 $$headers{$last_header} .= $eol . $line;
1959 }
1960 }
1961  
1962 0 ▶ return wantarray ? ($headers, $amt_read) : $headers;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
1963 }
1964  
1965 0 ▶ sub _getEndOfLineSeq {
Subroutine _getEndOfLineSeq: not called CC=1 SCAR=6.9
1966 0 return "\x0d\x0a"; # "\015\012" in octal
1967 }
1968  
1969 0 ▶ sub _read {
Subroutine _read: not called CC=6 SCAR=37.4
1970 0 my ($self, $fh, $buf, $len, $offset, $end_char) = @_;
1971 0 ▶ return '' if $len == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $len == 0 0 0 0
1972 0 my $cur_len = 0;
1973 0 my $buffer;
1974 0 my $buf_ref = \$buffer;
1975 0 my $char;
1976 0 ▶ while (defined($char = CORE::getc($fh))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($char = getc $fh)) 0 0 0
1977 0 $$buf_ref .= $char;
1978 0 $cur_len++;
1979 0 ▶ if ($char eq $end_char or $cur_len == $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($char eq $end_char or $cur_len == $len) 0 0 0
Condition0%0/3
$char eq $end_char or $cur_len == $len
l!l&&r!l&&!r
000
MC/DC0%0/2
$char eq $end_char or $cur_len == $len
$char eq $end_char $cur_len == $len
Truth table0%0/3
A or B
A= $char eq $end_charB= $cur_len == $len
ABresult
1X1
011
000
1980 0 ▶ if ($offset > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset > 0) { } 0 0 0
1981 0 substr($_[2], $offset, $cur_len) = $$buf_ref;
1982 } else {
1983 0 $_[2] = $$buf_ref;
1984 }
1985 0 return $cur_len;
1986 }
1987 }
1988 0 return 0;
1989 }
1990  
1991 =pod
1992  
1993 =head1 Apache constants under mod_perl
1994  
1995 Shortcut methods are provided for returning Apache constants
1996 under mod_perl. The methods figure out if they are running under
1997 mod_perl 1 or 2 and make the appropriate call to get the right
1998 constant back, e.g., Apache::Constants::OK() versus Apache::OK().
1999 The methods are created on the fly using AUTOLOAD. The method
2000 names are in the format apache_$name where $name is the
2001 lowercased constant name, e.g., $utils->apache_ok,
2002 $utils->apache_forbidden. See
2003 L<http://perl.apache.org/docs/1.0/api/Apache/Constants.html> for
2004 a list of constants available.
2005  
2006 =cut
2007
2008 3 ▶ sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=4 SCAR=22.6
2009 3 my $self = shift;
2010 3 (my $method = $AUTOLOAD) =~ s{\A.*\:\:([^:]+)\Z}{$1};
2011  
2012 3 ▶ if ($method eq 'DESTROY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "DESTROY") 3 0 3
2013 3 return;
2014 }
2015  
2016 0 ▶ if ($method =~ /\Aapache_(.+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method =~ /\Aapache_(.+)/) 0 0 0
2017 0 my $const = uc($1);
2018 0 eval "sub $method "
2019 . "{ return MP2 ? Apache\:\:$const() : Apache\:\:Constants\:\:$const(); }";
2020 0 ▶ unless ($@) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 0 0 0
2021 0 return $self->$method;
2022 }
2023  
2024 0 return;
2025 }
2026  
2027 0 die "no such method $method in package " . __PACKAGE__;
2028 }
2029 }
2030  
2031 6 1;
2032  
2033 =pod
2034  
2035 =head1 EXPORTS
2036  
2037 You can export methods into your namespace in the usual way.
2038 All of the util methods are available for export, e.g.,
2039 getSelfRefUrl(), addParamsToUrl(), etc. Beware, however, that
2040 these methods expect to be called as methods. You can also use
2041 the tag :all_utils to import all of the util methods into your
2042 namespace. This allows for incorporating these methods into
2043 your class without having to inherit from CGI::Utils.
2044  
2045 =head1 ACKNOWLEDGEMENTS
2046  
2047 Other people who have contributed ideas and/or code for this module:
2048  
2049 Kevin Wilson
2050  
2051 =head1 AUTHOR
2052  
2053 Don Owens <don@regexguy.com>
2054  
2055 =head1 COPYRIGHT
2056  
2057 Copyright (c) 2003-2008 Don Owens
2058  
2059 All rights reserved. This program is free software; you can
2060 redistribute it and/or modify it under the same terms as Perl
2061 itself.
2062  
2063 =head1 VERSION
2064  
2065 0.12
2066  
2067 =cut
↑ Summary Utils/UploadFile.pm »