Pure.pm

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