Pure.pm

filter: Statementstmt 55.2% 162 / 293 Branchbran 39.6% 50 / 126 Conditioncond 55.5% 15 / 27 MC/DCmcdc 29.4% 5 / 17 Subroutinesub 81.2% 26 / 32 Podpod 100% 10 / 10 totaltotal 53.0% 268 / 505 SCAR 67.8
CC
82
Cov
51%
_parse_multipart
182.0
upload
110.0
_save_tmpfile
90.0
CRAP 878.1
↑ Summary Pure/Save.pm »
1 package CGI::Pure;
2  
3 # Pragmas.
4 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 # Modules.
8 14 ▶use CGI::Deurl::XS qw(parse_query_string);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 14 ▶use Class::Utils qw(set_params);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 14 ▶use Encode qw(decode_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 14 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 14 ▶use Error::Pure qw(err);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 14 ▶use List::MoreUtils qw(none);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 14 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 14 ▶use URI::Escape qw(uri_escape uri_escape_utf8 uri_unescape);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 # Constants.
18 14 Readonly::Scalar my $EMPTY_STR => q{};
19 14 Readonly::Scalar my $POST_MAX => 102_400;
20 14 Readonly::Scalar my $POST_MAX_NO_LIMIT => -1;
21 14 Readonly::Scalar my $BLOCK_SIZE => 4_096;
22 14 Readonly::Array my @PAR_SEP => (q{&}, q{;});
23  
24 # Version.
25 14 our $VERSION = 0.07;
26  
27 # Constructor.
28 sub new {
29 34 ▶ my ($class, @params) = @_;
Subroutine new: called CC=2 SCAR=6.9
Pod documented
30  
31 # Create object.
32 34 my $self = bless {}, $class;
33  
34 # CRLF separator.
35 34 $self->{'crlf'} = undef;
36  
37 # Disable upload.
38 34 $self->{'disable_upload'} = 1;
39  
40 # Init.
41 34 $self->{'init'} = undef;
42  
43 # Parameter separator.
44 34 $self->{'par_sep'} = q{&};
45  
46 # Use a post max of 100K ($POST_MAX),
47 # set to -1 ($POST_MAX_NO_LIMIT) for no limits.
48 34 $self->{'post_max'} = $POST_MAX;
49  
50 # Save query data from server.
51 34 $self->{'save_query_data'} = 0;
52  
53 # UTF8 CGI params.
54 34 $self->{'utf8'} = 1;
55  
56 # Process params.
57 34 set_params($self, @params);
58  
59 # Check to parameter separator.
60 33 ▶ if (none { $_ eq $self->{'par_sep'} } @PAR_SEP) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (&none(sub { $_ eq $self->{"par_sep"}; } , @PAR_SEP)) 1 32 33
61 1 err "Bad parameter separator '$self->{'par_sep'}'.";
62 }
63  
64 # Global object variables.
65 32 $self->_global_variables;
66  
67 # Initialization.
68 32 my $init = $self->{'init'};
69 32 delete $self->{'init'};
70 32 $self->_initialize($init);
71  
72 # Object.
73 32 return $self;
74 }
75  
76 # Append param value.
77 sub append_param {
78 9 ▶ my ($self, $param, @values) = @_;
Subroutine append_param: called CC=3 SCAR=12.0
Pod documented
79  
80 # Clean from undefined values.
81 9 my @new_values = _remove_undef(@values);
82  
83 9 ▶ $self->_add_param($param, ((defined $new_values[0] and ref $new_values[0])
Branch: 1 branch
BranchTrueFalseTotal
defined $new_values[0] && ref $new_values[0] ? : 0 9 9
Condition33%1/3
defined $new_values[0] and ref $new_values[0]
!ll&&!rl&&r
090
MC/DC0%0/2
defined $new_values[0] and ref $new_values[0]
defined $new_values[0] ref $new_values[0]
Truth table33%1/3
A and B
A= defined $new_values[0]B= ref $new_values[0]
ABresult
0X0
100
111
84 ? $new_values[0] : [@new_values]));
85 9 return $self->param($param);
86 }
87  
88 # Clone class to my class.
89 sub clone {
90 2 ▶ my ($self, $class) = @_;
Subroutine clone: called CC=2 SCAR=6.9
Pod documented
91 2 foreach my $param ($class->param) {
92 3 $self->param($param, $class->param($param));
93 }
94 2 return;
95 }
96  
97 # Delete param.
98 sub delete_param {
99 4 ▶ my ($self, $param) = @_;
Subroutine delete_param: called CC=2 SCAR=6.9
Pod documented
100 4 ▶ if (! defined $self->{'.parameters'}->{$param}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{".parameters"}{$param}) 1 3 4
101 1 return;
102 }
103 3 delete $self->{'.parameters'}->{$param};
104 3 return 1;
105 }
106  
107 # Delete all params.
108 sub delete_all_params {
109 2 ▶ my $self = shift;
Subroutine delete_all_params: called CC=1 SCAR=0.0
Pod documented
110 2 delete $self->{'.parameters'};
111 2 $self->{'.parameters'} = {};
112 2 return;
113 }
114  
115 # Return param[s]. If sets parameters, than overwrite.
116 sub param {
117 76 ▶ my ($self, $param, @values) = @_;
Subroutine param: called CC=6 SCAR=17.9
Pod documented
118  
119 # Return list of all params.
120 76 ▶ if (! defined $param) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $param) 25 51 76
121 25 return sort keys %{$self->{'.parameters'}};
122 }
123  
124 # Clean from undefined values.
125 51 my @new_values = _remove_undef(@values);
126  
127 # Return values for $param.
128 51 ▶ if (! @new_values) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @new_values) { } 42 9 51
129 42 ▶ if (! exists $self->{'.parameters'}->{$param}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{".parameters"}{$param}) 4 38 42
130 4 return ();
131 }
132  
133 # Values exists, than sets them.
134 } else {
135 9 ▶ $self->_add_param($param, (ref $new_values[0] eq 'ARRAY'
Branch: 1 branch
BranchTrueFalseTotal
ref $new_values[0] eq 'ARRAY' ? : 2 7 9
136 ? $new_values[0] : [@new_values]), 'overwrite');
137 }
138  
139 # Return values of param, or first value of param.
140 22 return wantarray ? sort @{$self->{'.parameters'}->{$param}}
141 47 ▶ : $self->{'.parameters'}->{$param}->[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 22 25 47
142 }
143  
144 # Gets query data from server.
145 sub query_data {
146 5 ▶ my $self = shift;
Subroutine query_data: called CC=2 SCAR=6.9
Pod documented
147 5 ▶ if ($self->{'save_query_data'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'save_query_data'}) { } 4 1 5
148 4 return $self->{'.query_data'};
149 } else {
150 1 return 'Not saved query data.';
151 }
152 }
153  
154 # Return actual query string.
155 sub query_string {
156 4 ▶ my $self = shift;
Subroutine query_string: called CC=3 SCAR=11.0
Pod documented
157 4 my @pairs;
158 4 foreach my $param ($self->param) {
159 5 foreach my $value ($self->param($param)) {
160 11 push @pairs, $self->_uri_escape($param).q{=}.
161 $self->_uri_escape($value);
162 }
163 }
164 4 return join $self->{'par_sep'}, @pairs;
165 }
166  
167 # Upload file from tmp.
168 sub upload {
169 0 ▶ my ($self, $filename, $writefile) = @_;
Subroutine upload: not called CC=10 SCAR=47.0
Pod documented
170 0 ▶ if ($ENV{'CONTENT_TYPE'} !~ m/^multipart\/form-data/ismx) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ENV{"CONTENT_TYPE"} =~ m[^multipart/form-data]imsx) 0 0 0
171 0 err 'File uploads only work if you specify '.
172 'enctype="multipart/form-data" in your form.';
173 }
174 0 ▶ if (! $filename) {;
Branch: 1 branch
BranchTrueFalseTotal
unless ($filename) 0 0 0
175 0 ▶ if ($writefile) {
Branch: 1 branch
BranchTrueFalseTotal
if ($writefile) 0 0 0
176 0 err 'No filename submitted for upload to '.
177 "'$writefile'.";
178 }
179 return $self->{'.filehandles'}
180 0 ▶ ? keys %{$self->{'.filehandles'}} : ();
Branch: 1 branch
BranchTrueFalseTotal
$self->{'.filehandles'} ? : 0 0 0
181 }
182 0 my $fh = $self->{'.filehandles'}->{$filename};
183 0 ▶ if ($fh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fh) { } 0 0 0
184  
185 # Get ready for reading.
186 0 seek $fh, 0, 0;
187  
188 0 ▶ if (! $writefile) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($writefile) 0 0 0
189 0 return $fh;
190 }
191 0 binmode $fh;
192 0 my $buffer;
193 my $out;
194 0 ▶ if (! open $out, '>', $writefile) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $out, ">", $writefile) 0 0 0
195 0 err "Cannot write file '$writefile': $!.";
196 }
197 0 binmode $out;
198 0 ▶ while (read $fh, $buffer, $BLOCK_SIZE) {
Branch: 1 branch
BranchTrueFalseTotal
if (read $fh, $buffer, $BLOCK_SIZE) 0 0 0
199 0 print {$out} $buffer;
200 }
201 0 ▶ if (! close $out) {
Branch: 1 branch
BranchTrueFalseTotal
unless (close $out) 0 0 0
202 0 err "Cannot close file '$writefile': $!.";
203 }
204 0 $self->{'.filehandles'}->{$filename} = undef;
205 0 undef $fh;
206 } else {
207 0 err "No filehandle for '$filename'. ".
208 'Are uploads enabled (disable_upload = 0)? '.
209 'Is post_max big enough?';
210 }
211 0 return;
212 }
213  
214 # Return informations from uploaded files.
215 sub upload_info {
216 0 ▶ my ($self, $filename, $info) = @_;
Subroutine upload_info: not called CC=4 SCAR=30.0
Pod documented
217 0 ▶ if ($ENV{'CONTENT_TYPE'} !~ m/^multipart\/form-data/ismx) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ENV{"CONTENT_TYPE"} =~ m[^multipart/form-data]imsx) 0 0 0
218 0 err 'File uploads only work if you '.
219 'specify enctype="multipart/form-data" in your '.
220 'form.';
221 }
222 0 ▶ if (! $filename) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($filename) 0 0 0
223 0 return keys %{$self->{'.tmpfiles'}};
224 }
225 0 ▶ if ($info =~ m/mime/ims) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info =~ /mime/ism) 0 0 0
226 0 return $self->{'.tmpfiles'}->{$filename}->{'mime'}
227 }
228 0 return $self->{'.tmpfiles'}->{$filename}->{'size'};
229 }
230  
231 # Adding param.
232 sub _add_param {
233 47 ▶ my ($self, $param, $value, $overwrite) = @_;
Subroutine _add_param: called CC=6 SCAR=18.0
234 47 ▶ if (! defined $param) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $param) 0 47 47
235 0 return ();
236 }
237 47 ▶ if ($overwrite
Branch: 1 branch
BranchTrueFalseTotal
if ($overwrite or not exists $self->{".parameters"}{$param}) 45 2 47
Condition100%3/3
$overwrite or not exists $self->{".parameters"}{$param}
l!l&&r!l&&!r
9362
MC/DC100%2/2
$overwrite or not exists $self->{".parameters"}{$param}
$overwrite not exists $self->{".parameters"}{$param}
Truth table100%3/3
A or not(B)
A= $overwriteB= not exists $self->{".parameters"}{$param}
ABresult
1X1
011
000
238 || ! exists $self->{'.parameters'}->{$param}) {
239  
240 45 $self->{'.parameters'}->{$param} = [];
241 }
242 47 ▶ my @values = ref $value eq 'ARRAY' ? @{$value} : ($value);
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'ARRAY' ? : 32 15 47
243 47 foreach my $value (@values) {
244 79 push @{$self->{'.parameters'}->{$param}}, $value;
245 }
246 47 return;
247 }
248  
249 # Common parsing from any methods..
250 sub _common_parse {
251 21 ▶ my $self = shift;
Subroutine _common_parse: called CC=18 SCAR=33.2
252 21 my $data;
253  
254 # Information from server.
255 21 ▶ my $type = $ENV{'CONTENT_TYPE'} || 'No CONTENT_TYPE received';
Condition50%1/2
$ENV{'CONTENT_TYPE'} || 'No CONTENT_TYPE received'
l!l
021
MC/DC0%0/1
$ENV{'CONTENT_TYPE'} || 'No CONTENT_TYPE received'
$ENV{'CONTENT_TYPE'}
Truth table50%1/2
A
A= $ENV{'CONTENT_TYPE'}
Aresult
11
00
256 21 ▶ my $length = $ENV{'CONTENT_LENGTH'} || 0;
Condition100%2/2
$ENV{'CONTENT_LENGTH'} || 0
l!l
516
MC/DC100%1/1
$ENV{'CONTENT_LENGTH'} || 0
$ENV{'CONTENT_LENGTH'}
Truth table100%2/2
A
A= $ENV{'CONTENT_LENGTH'}
Aresult
11
00
257 21 ▶ my $method = $ENV{'REQUEST_METHOD'} || 'No REQUEST_METHOD received';
Condition100%2/2
$ENV{'REQUEST_METHOD'} || 'No REQUEST_METHOD received'
l!l
714
MC/DC100%1/1
$ENV{'REQUEST_METHOD'} || 'No REQUEST_METHOD received'
$ENV{'REQUEST_METHOD'}
Truth table100%2/2
A
A= $ENV{'REQUEST_METHOD'}
Aresult
11
00
258  
259 # Multipart form data.
260 21 ▶ if ($length && $type =~ m/^multipart\/form-data/imsx) {
Branch: 3 branches
BranchTrueFalseTotal
if ($length and $type =~ m[^multipart/form-data]imsx) { } 0 21 21
elsif ($method eq 'POST') { } 2 19 21
elsif ($method eq 'GET' or $method eq 'HEAD') { } 5 14 19
Condition66%2/3
$length and $type =~ m[^multipart/form-data]imsx
!ll&&!rl&&r
1650
MC/DC0%0/2
$length and $type =~ m[^multipart/form-data]imsx
$length $type =~ m[^multipart/form-data]imsx
Truth table66%2/3
A and B
A= $lengthB= $type =~ m[^multipart/form-data]imsx
ABresult
0X0
100
111
261  
262 # Get data_length, store data to internal structure.
263 0 my $got_data_length = $self->_parse_multipart;
264  
265 # Bad data length vs content_length.
266 0 ▶ if ($length != $got_data_length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length != $got_data_length) 0 0 0
267 0 err "500 Bad read! wanted $length, got ".
268 "$got_data_length.";
269 }
270  
271 0 return;
272  
273 # POST method.
274 } elsif ($method eq 'POST') {
275  
276 # Maximal post length is above my length.
277 2 ▶ if ($self->{'post_max'} != $POST_MAX_NO_LIMIT
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'post_max'} != $POST_MAX_NO_LIMIT and $length > $self->{'post_max'}) { } 0 2 2
elsif ($length) { } 2 0 2
Condition33%1/3
$self->{"post_max"} != $POST_MAX_NO_LIMIT and $length > $self->{"post_max"}
!ll&&!rl&&r
020
MC/DC0%0/2
$self->{"post_max"} != $POST_MAX_NO_LIMIT and $length > $self->{"post_max"}
$self->{"post_max"} != $POST_MAX_NO_LIMIT $length > $self->{"post_max"}
Truth table33%1/3
A and B
A= $self->{"post_max"} != $POST_MAX_NO_LIMITB= $length > $self->{"post_max"}
ABresult
0X0
100
111
278 and $length > $self->{'post_max'}) {
279  
280 0 err '413 Request entity too large: '.
281 "$length bytes on STDIN exceeds ".
282 'post_max !';
283  
284 # Get data.
285 } elsif ($length) {
286 2 read STDIN, $data, $length;
287 }
288  
289 # Save data for post.
290 2 ▶ if ($self->{'save_query_data'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"save_query_data"}) 1 1 2
291 1 $self->{'.query_data'} = $data;
292 }
293  
294 # Bad length of data.
295 2 ▶ if ($length != length $data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length != length $data) 0 2 2
296 0 err "500 Bad read! wanted $length, got ".
297 (length $data).q{.};
298 }
299  
300 # GET/HEAD method.
301 ▶ } elsif ($method eq 'GET' || $method eq 'HEAD') {
Condition66%2/3
$method eq "GET" or $method eq "HEAD"
l!l&&r!l&&!r
5014
MC/DC50%1/2
$method eq "GET" or $method eq "HEAD"
$method eq "GET" $method eq "HEAD"
Truth table66%2/3
A or B
A= $method eq "GET"B= $method eq "HEAD"
ABresult
1X1
011
000
302 5 ▶ $data = $ENV{'QUERY_STRING'} || $EMPTY_STR;
Condition33%1/3
$ENV{'QUERY_STRING'} || $EMPTY_STR
l!l&&r!l&&!r
500
MC/DC0%0/2
$ENV{'QUERY_STRING'} || $EMPTY_STR
$ENV{'QUERY_STRING'} $EMPTY_STR
Truth table33%1/3
A || B
A= $ENV{'QUERY_STRING'}B= $EMPTY_STR
ABresult
1X1
011
000
303 5 ▶ if ($self->{'save_query_data'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"save_query_data"}) 1 4 5
304 1 $self->{'.query_data'} .= $data;
305 }
306 }
307  
308 # Parse params.
309 21 ▶ if ($data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data) 7 14 21
310 7 $self->_parse_params($data);
311 }
312 21 return;
313 }
314  
315 # Define the CRLF sequence.
316 sub _crlf {
317 0 ▶ my $self = shift;
Subroutine _crlf: not called CC=3 SCAR=24.8
318  
319 # If not defined.
320 0 ▶ if (! defined $self->{'crlf'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"crlf"}) 0 0 0
321  
322 # VMS.
323 0 ▶ if ($OSNAME =~ m/VMS/ims) {
Branch: 1 branch
BranchTrueFalseTotal
if ($CGI::Pure::OSNAME =~ /VMS/ism) { } 0 0 0
324 0 $self->{'crlf'} = "\n";
325  
326 # EBCDIC systems.
327 } elsif ("\t" eq "\011") {
328 0 $self->{'crlf'} = "\015\012";
329  
330 # Other.
331 } else {
332 $self->{'crlf'} = "\r\n";
333 }
334 }
335  
336 # Return sequence.
337 0 return $self->{'crlf'};
338 }
339  
340 # Sets global object variables.
341 sub _global_variables {
342 32 ▶ my $self = shift;
Subroutine _global_variables: called CC=1 SCAR=0.0
343 32 $self->{'.parameters'} = {};
344 32 $self->{'.query_data'} = $EMPTY_STR;
345 32 return;
346 }
347  
348 # Initializating CGI::Pure with something input methods.
349 sub _initialize {
350 32 ▶ my ($self, $init) = @_;
Subroutine _initialize: called CC=5 SCAR=16.1
351  
352 # Initialize from QUERY_STRING, STDIN or @ARGV.
353 32 ▶ if (! defined $init) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $init) { } 21 11 32
elsif (ref $init eq 'HASH') { } 6 5 11
elsif (eval { do { $init->isa('CGI::Pure') } }) { } 1 4 5
354 21 $self->_common_parse;
355  
356 # Initialize from param hash.
357 } elsif (ref $init eq 'HASH') {
358 6 foreach my $param (keys %{$init}) {
359 12 $self->_add_param($param, $init->{$param});
360 }
361  
362 # Inicialize from CGI::Pure object.
363 # XXX Mod_perl?
364 5 } elsif (eval { $init->isa('CGI::Pure') }) {
365 1 $self->clone($init);
366  
367 # Initialize from a query string.
368 } else {
369 4 $self->_parse_params($init);
370 }
371  
372 32 return;
373 }
374  
375 # Parse multipart data.
376 sub _parse_multipart {
377 0 ▶ my $self = shift;
Subroutine _parse_multipart: not called CC=13 SCAR=52.0
378 0 my ($boundary) = $ENV{'CONTENT_TYPE'}
379 =~ /
380 boundary=
381 \"?([^\";,]+)\"?
382 /msx;
383 0 ▶ if (! $boundary) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($boundary) 0 0 0
384 0 err '400 No boundary supplied for multipart/form-data.';
385 }
386  
387 # BUG: IE 3.01 on the Macintosh uses just the boundary, forgetting
388 # the --
389 0 ▶ if (! exists $ENV{'HTTP_USER_AGENT'} || $ENV{'HTTP_USER_AGENT'} !~ m/
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $ENV{"HTTP_USER_AGENT"} and $ENV{"HTTP_USER_AGENT"} =~ / MSIE\s+ 3\.0[12]; \s* Mac /imsx) 0 0 0
Condition0%0/3
exists $ENV{"HTTP_USER_AGENT"} and $ENV{"HTTP_USER_AGENT"} =~ / MSIE\s+ 3\.0[12]; \s* Mac /imsx
!ll&&!rl&&r
000
MC/DC0%0/2
exists $ENV{"HTTP_USER_AGENT"} and $ENV{"HTTP_USER_AGENT"} =~ / MSIE\s+ 3\.0[12]; \s* Mac /imsx
exists $ENV{"HTTP_USER_AGENT"} $ENV{"HTTP_USER_AGENT"} =~ / MSIE\s+ 3\.0[12]; \s* Mac /imsx
Truth table0%0/3
A and B
A= exists $ENV{"HTTP_USER_AGENT"}B= $ENV{"HTTP_USER_AGENT"} =~ / MSIE\s+ 3\.0[12]; \s* Mac /imsx
ABresult
0X0
100
111
390 MSIE\s+
391 3\.0[12];
392 \s*
393 Mac
394 /imsx) {
395  
396 0 $boundary = q{--}.$boundary;
397 }
398  
399 0 $boundary = quotemeta $boundary;
400 0 my $got_data_length = 0;
401 0 my $data = $EMPTY_STR;
402 0 my $read;
403 0 my $CRLF = $self->_crlf;
404  
405 READ:
406 0 ▶ while (read STDIN, $read, $BLOCK_SIZE) {
Branch: 1 branch
BranchTrueFalseTotal
if (read STDIN, $read, $BLOCK_SIZE) 0 0 0
407  
408 # Adding post data.
409 0 ▶ if ($self->{'save_query_data'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"save_query_data"}) 0 0 0
410 0 $self->{'.query_data'} .= $read;
411 }
412  
413 0 $data .= $read;
414 0 $got_data_length += length $read;
415  
416 BOUNDARY:
417 0 ▶ while ($data =~ m/^$boundary$CRLF/ms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ /^$boundary$CRLF/ms) 0 0 0
418 0 my $header;
419  
420 # Get header, delimited by first two CRLFs we see.
421 0 ▶ if ($data !~ m/^([\040-\176$CRLF]+?$CRLF$CRLF)/ms) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data =~ /^([\040-\176$CRLF]+?$CRLF$CRLF)/ms) 0 0 0
422 0 next READ;
423 }
424 # XXX Proc tohle nemuze byt? /x tam dela nejake potize.
425 # if ($data !~ m/^(
426 # [\040-\176$CRLF]+?
427 # $CRLF
428 # $CRLF
429 # )/msx) {
430 #
431 # next READ;
432 # }
433 0 $header = $1;
434  
435 # Unhold header per RFC822.
436 0 (my $unfold = $1) =~ s/$CRLF\s+/\ /gms;
437  
438 0 my ($param) = $unfold =~ m/
439 form-data;
440 \s+
441 name="?([^\";]*)"?
442 /msx;
443 0 my ($filename) = $unfold =~ m/
444 name="?\Q$param\E"?;
445 \s+
446 filename="?([^\"]*)"?
447 /msx;
448 0 ▶ if ($filename) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename) 0 0 0
449 0 my ($mime) = $unfold =~ m/
450 Content-Type:
451 \s+
452 ([-\w\/]+)
453 /imsx;
454  
455 # Trim off header.
456 0 $data =~ s/^\Q$header\E//ms;
457  
458 0 ($got_data_length, $data, my $fh, my $size)
459 = $self->_save_tmpfile($boundary,
460 $filename, $got_data_length, $data);
461  
462 0 $self->_add_param($param, $filename);
463  
464 # Filehandle.
465 0 ▶ if ($fh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fh) 0 0 0
466 0 $self->{'.filehandles'}->{$filename}
467 = $fh;
468 }
469  
470 # Information about file.
471 0 ▶ if ($size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size) 0 0 0
472 0 $self->{'.tmpfiles'}->{$filename} = {
473 'size' => $size,
474 'mime' => $mime,
475 };
476 }
477 0 next BOUNDARY;
478 }
479 0 ▶ if ($data !~ s/^\Q$header\E(.*?)$CRLF(?=$boundary)//s) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data =~ s/^\Q$header\E(.*?)$CRLF(?=$boundary)//s) 0 0 0
480 0 next READ;
481 }
482 # XXX /x
483 # if ($data !~ s/^
484 # \Q$header\E
485 # (.*?)
486 # $CRLF
487 # (?=$boundary)
488 # //msx) {
489 #
490 # next READ;
491 # }
492 0 my $param_value;
493 0 ▶ if ($self->{'utf8'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'utf8'}) { } 0 0 0
494 0 $param_value = decode_utf8($1);
495 } else {
496 0 $param_value = $1;
497 }
498 0 $self->_add_param($param, $param_value);
499 }
500 }
501  
502 # Length of data.
503 0 return $got_data_length;
504 }
505  
506 # Parse params from data.
507 sub _parse_params {
508 13 ▶ my ($self, $data) = @_;
Subroutine _parse_params: called CC=6 SCAR=18.0
509 13 ▶ if (! defined $data) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data) 0 13 13
510 0 return ();
511 }
512  
513 # Parse params.
514 13 my $pairs_hr = parse_query_string($data);
515 13 foreach my $key (keys %{$pairs_hr}) {
516  
517 # Value processing.
518 17 my $value;
519 17 ▶ if ($self->{'utf8'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'utf8'}) { } 15 2 17
520 15 ▶ if (ref $pairs_hr->{$key} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $pairs_hr->{$key} eq 'ARRAY') { } 7 8 15
521 7 my @decoded = ();
522 7 foreach my $val (@{$pairs_hr->{$key}}) {
523 21 push @decoded, decode_utf8($val);
524 }
525 7 $value = \@decoded;
526 } else {
527 8 $value = decode_utf8($pairs_hr->{$key});
528 }
529 } else {
530 2 $value = $pairs_hr->{$key};
531 }
532  
533 # Add parameter.
534 17 $self->_add_param($key, $value);
535 }
536 13 return;
537 }
538  
539 # Remove undefined values.
540 sub _remove_undef {
541 60 ▶ my (@values) = @_;
Subroutine _remove_undef: called CC=3 SCAR=11.0
542 60 my @new_values;
543 60 foreach my $value (@values) {
544 21 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 20 1 21
545 20 push @new_values, $value;
546 }
547 }
548 60 return @new_values;
549 }
550  
551 # Save file from multiform.
552 sub _save_tmpfile {
553 0 ▶ my ($self, $boundary, $filename, $got_data_length, $data) = @_;
Subroutine _save_tmpfile: not called CC=9 SCAR=45.0
554 0 my $fh;
555 0 my $CRLF = $self->_crlf;
556 0 my $file_size = 0;
557 0 ▶ if ($self->{'disable_upload'}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'disable_upload'}) { } 0 0 0
elsif ($filename) { } 0 0 0
558 0 err '405 Not Allowed - File uploads are disabled.';
559 } elsif ($filename) {
560 0 eval {
561 0 require IO::File;
562 };
563 0 ▶ if ($EVAL_ERROR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($CGI::Pure::EVAL_ERROR) 0 0 0
564 0 err "500 IO::File is not available $EVAL_ERROR.";
565 }
566 0 $fh = new_tmpfile IO::File;
567 0 ▶ if (! $fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh) 0 0 0
568 0 err '500 IO::File can\'t create new temp_file.';
569 }
570 }
571 0 binmode $fh;
572 0 while (1) {
573 0 my $buffer = $data;
574 0 read STDIN, $data, $BLOCK_SIZE;
575 0 ▶ if (! $data) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data) 0 0 0
576 0 $data = $EMPTY_STR;
577 }
578 0 $got_data_length += length $data;
579 0 ▶ if ("$buffer$data" =~ m/$boundary/ms) {
Branch: 1 branch
BranchTrueFalseTotal
if ("$buffer$data" =~ /$boundary/ms) 0 0 0
580 0 $data = $buffer.$data;
581 0 last;
582 }
583  
584 # BUG: Fixed hanging bug if browser terminates upload part way.
585 0 ▶ if (! $data) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data) 0 0 0
586 0 undef $fh;
587 0 err '400 Malformed multipart, no terminating '.
588 'boundary.';
589 }
590  
591 # We do not have partial boundary so print to file if valid $fh.
592 0 print {$fh} $buffer;
593 0 $file_size += length $buffer;
594 }
595 0 $data =~ s/^
596 (.*?)
597 $CRLF
598 (?=$boundary)
599 //smx;
600  
601 # Print remainder of file if value $fh.
602 0 ▶ if ($1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($1) 0 0 0
603 0 print {$fh} $1;
604 0 $file_size += length $1;
605 }
606  
607 0 return $got_data_length, $data, $fh, $file_size;
608 }
609  
610 # Escapes uri.
611 sub _uri_escape {
612 22 ▶ my ($self, $string) = @_;
Subroutine _uri_escape: called CC=2 SCAR=7.2
613 22 ▶ if ($self->{'utf8'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'utf8'}) { } 22 0 22
614 22 $string = uri_escape_utf8($string);
615 } else {
616 0 $string = uri_escape($string);
617 }
618 22 $string =~ s/\ /\+/gsm;
619 22 return $string;
620 }
621  
622 # Unescapes uri.
623 sub _uri_unescape {
624 0 ▶ my ($self, $string) = @_;
Subroutine _uri_unescape: not called CC=1 SCAR=6.9
625 0 $string =~ s/\+/\ /gsm;
626 0 return uri_unescape($string);
627 }
628  
629 14 1;
630  
631 __END__
↑ Summary Pure/Save.pm »