Tiny/Multipart.pm

filter: Statementstmt 96.4% 108 / 112 Branchbran 77.7% 56 / 72 Conditioncond 78.0% 32 / 41 MC/DCmcdc 56.5% 13 / 23 Subroutinesub 100% 6 / 6 Podpod 100% 2 / 2 totaltotal 84.7% 217 / 256 CC 52 SCAR 39.2
CC
52
Cov
87%
parse_multipart_form_data
57.4
extract_multipart_boundary
3.0
BEGIN
1.0
CRAP 50.6
« Tiny/_Debug.pm ↑ Summary Tiny.pm »
1 package CGI::Tiny::Multipart;
2 # ABSTRACT: Tiny multipart/form-data form parser
3  
4 # This file is part of CGI::Tiny which is released under:
5 # The Artistic License 2.0 (GPL Compatible)
6 # See the documentation for CGI::Tiny for full license details.
7  
8 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 our $VERSION = '1.003';
13  
14 2 our @EXPORT_OK = qw(extract_multipart_boundary parse_multipart_form_data);
15  
16 2 ▶use constant DEFAULT_REQUEST_BODY_BUFFER => 262144;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 18 ▶sub extract_multipart_boundary {
Subroutine extract_multipart_boundary: called CC=3 SCAR=11.0
Pod documented
19 18 my ($content_type) = @_;
20 18 my ($boundary_quoted, $boundary_unquoted) = $content_type =~ m/;\s*boundary\s*=\s*(?:"((?:\\[\\"]|[^"])+)"|([^";]+))/i;
21 18 ▶ $boundary_quoted =~ s/\\([\\"])/$1/g if defined $boundary_quoted;
Branch: 1 branch
BranchTrueFalseTotal
if defined $boundary_quoted 5 13 18
22 18 ▶ return defined $boundary_quoted ? $boundary_quoted : $boundary_unquoted;
Branch: 1 branch
BranchTrueFalseTotal
defined $boundary_quoted ? : 5 13 18
23 }
24  
25 25 ▶sub parse_multipart_form_data {
Subroutine parse_multipart_form_data: called CC=50 SCAR=40.5
Pod documented
26 25 my ($input, $length, $boundary, $options) = @_;
27 25 ▶ $options ||= {};
Condition100%2/2
$options ||= {}
l!l
187
MC/DC100%1/1
$options ||= {}
$options
Truth table100%2/2
A
A= $options
Aresult
11
00
28 25 my $input_is_scalar = ref $input eq 'SCALAR';
29 25 ▶ binmode $input unless $input_is_scalar;
Branch: 1 branch
BranchTrueFalseTotal
unless $input_is_scalar 8 17 25
30 25 ▶ my $remaining = 0 + ($length || 0);
Condition50%1/2
$length || 0
l!l
250
MC/DC0%0/1
$length || 0
$length
Truth table50%1/2
A
A= $length
Aresult
11
00
31 25 my $next_boundary = "\r\n--$boundary\r\n";
32 25 my $end_boundary = "\r\n--$boundary--";
33 25 ▶ my $buffer_size = 0 + ($options->{buffer_size} || DEFAULT_REQUEST_BODY_BUFFER);
Condition100%2/2
$options->{'buffer_size'} || 262144
l!l
124
MC/DC100%1/1
$options->{'buffer_size'} || 262144
$options->{'buffer_size'}
Truth table100%2/2
A
A= $options->{'buffer_size'}
Aresult
11
00
34 25 my $buffer = "\r\n";
35 25 my (%state, @parts, $current);
36 25 ▶ READER: while ($remaining > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remaining > 0) 25 3 28
37 25 ▶ if ($input_is_scalar) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input_is_scalar) { } 17 8 25
38 17 $buffer .= substr $$input, 0, $remaining;
39 17 $remaining = 0;
40 } else {
41 8 ▶ my $chunk = $remaining < $buffer_size ? $remaining : $buffer_size;
Branch: 1 branch
BranchTrueFalseTotal
$remaining < $buffer_size ? : 8 0 8
42 8 ▶ last unless my $read = read $input, $buffer, $chunk, length $buffer;
Branch: 1 branch
BranchTrueFalseTotal
unless my $read = read($input, $buffer, $chunk, length $buffer) 0 8 8
43 8 $remaining -= $read;
44 }
45  
46 25 ▶ unless ($state{parsing_headers} or $state{parsing_body}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($state{"parsing_headers"} or $state{"parsing_body"}) 25 0 25
Condition33%1/3
$state{"parsing_headers"} or $state{"parsing_body"}
l!l&&r!l&&!r
0025
MC/DC0%0/2
$state{"parsing_headers"} or $state{"parsing_body"}
$state{"parsing_headers"} $state{"parsing_body"}
Truth table33%1/3
A or B
A= $state{"parsing_headers"}B= $state{"parsing_body"}
ABresult
1X1
011
000
47 25 my $next_pos = index $buffer, $next_boundary;
48 25 my $end_pos = index $buffer, $end_boundary;
49 25 ▶ if ($next_pos >= 0 and ($end_pos < 0 or $end_pos > $next_pos)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($next_pos >= 0 and $end_pos < 0 || $end_pos > $next_pos) { } 22 3 25
elsif ($end_pos >= 0) { } 1 2 3
Condition100%6/6
$next_pos >= 0 and $end_pos < 0 || $end_pos > $next_pos
!ll&&!rl&&r
2122
$end_pos < 0 or $end_pos > $next_pos
l!l&&r!l&&!r
1211
MC/DC100%3/3
$next_pos >= 0 and $end_pos < 0 || $end_pos > $next_pos
$next_pos >= 0 $end_pos < 0 $end_pos > $next_pos
Truth table100%4/4
A and B or C
A= $next_pos >= 0B= $end_pos < 0C= $end_pos > $next_pos
ABCresult
0XX0
1000
11X1
1011
50 22 substr $buffer, 0, $next_pos + length($next_boundary), '';
51 22 $state{parsing_headers} = 1;
52 22 push @parts, $current = {headers => {}, name => undef, filename => undef, size => 0};
53 } elsif ($end_pos >= 0) {
54 1 $state{done} = 1;
55 1 last; # end of multipart data
56 } else {
57 2 next; # read more to find start of multipart data
58 }
59 }
60  
61 22 ▶ while (length $buffer) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $buffer) 234 1 235
62 234 ▶ if ($state{parsing_headers}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state{'parsing_headers'}) { } 118 116 234
63 118 ▶ while ((my $pos = index $buffer, "\r\n") >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $pos = index($buffer, "\r\n")) >= 0) 295 0 295
64 295 ▶ if ($pos == 0) { # end of headers
Branch: 1 branch
BranchTrueFalseTotal
if ($pos == 0) 116 179 295
65 116 $state{parsing_headers} = 0;
66 116 $state{parsing_body} = 1;
67 116 $state{parsed_optional_crlf} = 0;
68 116 last;
69 }
70  
71 179 my $header = substr $buffer, 0, $pos + 2, '';
72 179 my ($name, $value) = split /\s*:\s*/, $header, 2;
73 179 ▶ return undef unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 2 177 179
74 177 $value =~ s/\s*\z//;
75  
76 177 $current->{headers}{lc $name} = $value;
77 177 ▶ if (lc $name eq 'content-disposition') {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $name eq "content-disposition") 115 62 177
78 115 ▶ while ($value =~ m/;\s*([^=\s]+)\s*=\s*(?:"((?:\\[\\"]|[^"])*)"|([^";]*))/ig) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /;\s*([^=\s]+)\s*=\s*(?:"((?:\\[\\"]|[^"])*)"|([^";]*))/gi) 160 115 275
79 160 my ($field_name, $field_quoted, $field_unquoted) = ($1, $2, $3);
80 160 ▶ next unless lc $field_name eq 'name' or lc $field_name eq 'filename';
Branch: 1 branch
BranchTrueFalseTotal
unless lc $field_name eq "name" or lc $field_name eq "filename" 0 160 160
Condition66%2/3
lc $field_name eq "name" or lc $field_name eq "filename"
l!l&&r!l&&!r
115450
MC/DC0%0/2
lc $field_name eq "name" or lc $field_name eq "filename"
lc $field_name eq "name" lc $field_name eq "filename"
Truth table66%2/3
A or B
A= lc $field_name eq "name"B= lc $field_name eq "filename"
ABresult
1X1
011
000
81 160 ▶ $field_quoted =~ s/\\([\\"])/$1/g if defined $field_quoted;
Branch: 1 branch
BranchTrueFalseTotal
if defined $field_quoted 143 17 160
82 160 ▶ $current->{lc $field_name} = defined $field_quoted ? $field_quoted : $field_unquoted;
Branch: 1 branch
BranchTrueFalseTotal
defined $field_quoted ? : 143 17 160
83 }
84 }
85 }
86 116 ▶ next READER if $state{parsing_headers}; # read more to find end of headers
Branch: 1 branch
BranchTrueFalseTotal
if $state{"parsing_headers"} 0 116 116
87 } else {
88 116 my $append = '';
89 116 my $next_pos = index $buffer, $next_boundary;
90 116 my $end_pos = index $buffer, $end_boundary;
91 116 ▶ if ($next_pos >= 0 and ($end_pos < 0 or $end_pos > $next_pos)) {
Branch: 3 branches
BranchTrueFalseTotal
if ($next_pos >= 0 and $end_pos < 0 || $end_pos > $next_pos) { } 97 19 116
elsif ($end_pos >= 0) { } 19 0 19
elsif (length $buffer > length($next_boundary) + 2) { } 0 0 0
Condition66%4/6
$next_pos >= 0 and $end_pos < 0 || $end_pos > $next_pos
!ll&&!rl&&r
19097
$end_pos < 0 or $end_pos > $next_pos
l!l&&r!l&&!r
1960
MC/DC33%1/3
$next_pos >= 0 and $end_pos < 0 || $end_pos > $next_pos
$next_pos >= 0 $end_pos < 0 $end_pos > $next_pos
Truth table75%3/4
A and B or C
A= $next_pos >= 0B= $end_pos < 0C= $end_pos > $next_pos
ABCresult
0XX0
1000
11X1
1011
92 97 ▶ if (!$state{parsed_optional_crlf} and $next_pos >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $state{"parsed_optional_crlf"} and $next_pos >= 2) 84 13 97
Condition66%2/3
not $state{"parsed_optional_crlf"} and $next_pos >= 2
!ll&&!rl&&r
01384
MC/DC50%1/2
not $state{"parsed_optional_crlf"} and $next_pos >= 2
not $state{"parsed_optional_crlf"} $next_pos >= 2
Truth table66%2/3
not(A) and B
A= not $state{"parsed_optional_crlf"}B= $next_pos >= 2
ABresult
0X0
100
111
93 84 substr $buffer, 0, 2, '';
94 84 $next_pos -= 2;
95 84 $state{parsed_optional_crlf} = 1;
96 }
97 97 $append = substr $buffer, 0, $next_pos, '';
98 97 substr $buffer, 0, length($next_boundary), '';
99 97 $state{parsing_body} = 0;
100 97 $state{parsing_headers} = 1;
101 } elsif ($end_pos >= 0) {
102 19 ▶ if (!$state{parsed_optional_crlf} and $end_pos >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $state{"parsed_optional_crlf"} and $end_pos >= 2) 19 0 19
Condition33%1/3
not $state{"parsed_optional_crlf"} and $end_pos >= 2
!ll&&!rl&&r
0019
MC/DC0%0/2
not $state{"parsed_optional_crlf"} and $end_pos >= 2
not $state{"parsed_optional_crlf"} $end_pos >= 2
Truth table33%1/3
not(A) and B
A= not $state{"parsed_optional_crlf"}B= $end_pos >= 2
ABresult
0X0
100
111
103 19 substr $buffer, 0, 2, '';
104 19 $end_pos -= 2;
105 19 $state{parsed_optional_crlf} = 1;
106 }
107 19 $append = substr $buffer, 0, $end_pos; # no replacement, we're done here
108 19 $state{parsing_body} = 0;
109 19 $state{done} = 1;
110 } elsif (length($buffer) > length($next_boundary) + 2) {
111 0 ▶ if (!$state{parsed_optional_crlf}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($state{"parsed_optional_crlf"}) 0 0 0
112 0 substr $buffer, 0, 2, '';
113 0 $state{parsed_optional_crlf} = 1;
114 }
115 0 $append = substr $buffer, 0, length($buffer) - length($next_boundary), '';
116 }
117  
118 116 $current->{size} += length $append;
119 116 ▶ unless (defined $current->{filename} and $options->{discard_files}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $current->{"filename"} and $options->{"discard_files"}) 103 13 116
Condition100%3/3
defined $current->{"filename"} and $options->{"discard_files"}
!ll&&!rl&&r
713213
MC/DC100%2/2
defined $current->{"filename"} and $options->{"discard_files"}
defined $current->{"filename"} $options->{"discard_files"}
Truth table100%3/3
A and B
A= defined $current->{"filename"}B= $options->{"discard_files"}
ABresult
0X0
100
111
120 103 ▶ if ($options->{parse_as_files} or (defined $current->{filename} and !defined $options->{parse_as_files})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{'parse_as_files'} or defined $current->{'filename'} and not defined $options->{'parse_as_files'}) { } 41 62 103
Condition100%6/6
$options->{"parse_as_files"} or defined $current->{"filename"} and not defined $options->{"parse_as_files"}
l!l&&r!l&&!r
231862
defined $current->{"filename"} and not defined $options->{"parse_as_files"}
!ll&&!rl&&r
55718
MC/DC100%3/3
$options->{"parse_as_files"} or defined $current->{"filename"} and not defined $options->{"parse_as_files"}
$options->{"parse_as_files"} defined $current->{"filename"} not defined $options->{"parse_as_files"}
Truth table100%4/4
A or B and not(C)
A= $options->{"parse_as_files"}B= defined $current->{"filename"}C= not defined $options->{"parse_as_files"}
ABCresult
1XX1
0111
00X0
0100
121 41 my $is_eof = !$state{parsing_body};
122 41 ▶ if (defined $options->{on_file_buffer}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options->{'on_file_buffer'}) { } 12 29 41
123 12 $options->{on_file_buffer}->($append, my $dummy = $current, $is_eof);
124 } else {
125 # create temp file even if empty
126 29 ▶ unless (defined $current->{file}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $current->{"file"}) 29 0 29
127 29 require File::Temp;
128 29 ▶ $current->{file} = File::Temp->new(@{$options->{tempfile_args} || []});
Condition100%2/2
$options->{'tempfile_args'} || []
l!l
128
MC/DC100%1/1
$options->{'tempfile_args'} || []
$options->{'tempfile_args'}
Truth table100%2/2
A
A= $options->{'tempfile_args'}
Aresult
11
00
129 29 binmode $current->{file};
130 }
131 29 print {$current->{file}} $append;
132 29 ▶ if ($is_eof) { # finalize temp file
Branch: 1 branch
BranchTrueFalseTotal
if ($is_eof) 29 0 29
133 29 $current->{file}->flush;
134 29 seek $current->{file}, 0, 0;
135 }
136 }
137 } else {
138 62 ▶ $current->{content} = '' unless defined $current->{content};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $current->{"content"} 62 0 62
139 62 $current->{content} .= $append;
140 }
141 }
142  
143 116 ▶ last READER if $state{done}; # end of multipart data
Branch: 1 branch
BranchTrueFalseTotal
if $state{"done"} 19 97 116
144 97 ▶ next READER if $state{parsing_body}; # read more to find end of part
Branch: 1 branch
BranchTrueFalseTotal
if $state{"parsing_body"} 0 97 97
145  
146 # new part started
147 97 push @parts, $current = {headers => {}, name => undef, filename => undef, size => 0};
148 }
149 }
150 }
151 23 ▶ return undef unless $state{done};
Branch: 1 branch
BranchTrueFalseTotal
unless $state{"done"} 3 20 23
152  
153 20 return \@parts;
154 }
155  
156 2 1;
« Tiny/_Debug.pm ↑ Summary Tiny.pm »