blib/lib/HTTP/MultiPartParser.pm

filter: Statementstmt 88.1% 111 / 126 Branchbran 75.0% 66 / 88 Conditioncond 25.9% 7 / 27 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 85.7% 12 / 14 Podpod 100% 5 / 5 totaltotal 73.3% 201 / 274 SCAR 45.8
CC
56
Cov
76%
new
102.3
__ANON__
25.3
__ANON__
7.0
CRAP 97.5
↑ Summary
1 package HTTP::MultiPartParser;
2 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 BEGIN {
6 4 ▶ our $VERSION = '0.01';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 }
8  
9 4 ▶use Carp qw[];
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Scalar::Util qw[];
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 4 my $_mk_parser;
13  
14 # RFC2046
15 4 my $ValidBoundary = qr<\A [0-9A-Za-z'()+_,-./:=?]+ \z>x;
16  
17 sub new {
18 27 ▶ my ($class, %params) = @_;
Subroutine new: called CC=21 SCAR=46.3
Pod documented
19  
20 27 my $self = {
21 on_error => \&Carp::croak,
22 max_header_size => 32 * 1024,
23 max_preamble_size => 32 * 1024,
24 on_header_as => 'lines',
25 };
26  
27 27 ▶ while (my ($p, $v) = each %params) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %params) 96 27 123
28 96 ▶ if ($p eq 'boundary') {
Branch: 4 branches
BranchTrueFalseTotal
if ($p eq 'boundary') { } 27 69 96
elsif ($p eq 'on_header' or $p eq 'on_body' or $p eq 'on_error') { } 69 0 69
elsif ($p eq 'max_header_size' or $p eq 'max_preamble_size') { } 0 0 0
elsif ($p eq 'on_header_as') { } 0 0 0
29 27 ▶ Carp::croak(q/Parameter 'boundary' is not a valid boundary value/)
Branch: 1 branch
BranchTrueFalseTotal
unless ref \$v eq "SCALAR" and defined $v and $v =~ /$ValidBoundary/ 0 27 27
Condition33%2/6
ref \$v eq "SCALAR" and defined $v and $v =~ /$ValidBoundary/
!ll&&!rl&&r
0027
ref \$v eq "SCALAR" and defined $v
!ll&&!rl&&r
0027
MC/DC0%0/3
ref \$v eq "SCALAR" and defined $v and $v =~ /$ValidBoundary/
ref \$v eq "SCALAR" defined $v $v =~ /$ValidBoundary/
Truth table25%1/4
A and B and C
A= ref \$v eq "SCALAR"B= defined $vC= $v =~ /$ValidBoundary/
ABCresult
0XX0
10X0
1100
1111
30 unless ref \$v eq 'SCALAR' && defined $v && $v =~ $ValidBoundary;
31 27 $self->{boundary} = $v;
32 }
33 ▶ elsif ( $p eq 'on_header'
Condition83%5/6
$p eq "on_header" or $p eq "on_body" or $p eq "on_error"
l!l&&r!l&&!r
54150
$p eq "on_header" or $p eq "on_body"
l!l&&r!l&&!r
272715
MC/DC0%0/3
$p eq "on_header" or $p eq "on_body" or $p eq "on_error"
$p eq "on_header" $p eq "on_body" $p eq "on_error"
Truth table75%3/4
A or B or C
A= $p eq "on_header"B= $p eq "on_body"C= $p eq "on_error"
ABCresult
1XX1
01X1
0011
0000
34 || $p eq 'on_body'
35 || $p eq 'on_error') {
36 69 ▶ Carp::croak(qq/Parameter '$p' is not a CODE reference/)
Branch: 1 branch
BranchTrueFalseTotal
unless ref $v eq "CODE" 0 69 69
37 unless ref $v eq 'CODE';
38 69 $self->{$p} = $v;
39 }
40 ▶ elsif ( $p eq 'max_header_size'
Condition0%0/3
$p eq "max_header_size" or $p eq "max_preamble_size"
l!l&&r!l&&!r
000
MC/DC0%0/2
$p eq "max_header_size" or $p eq "max_preamble_size"
$p eq "max_header_size" $p eq "max_preamble_size"
Truth table0%0/3
A or B
A= $p eq "max_header_size"B= $p eq "max_preamble_size"
ABresult
1X1
011
000
41 || $p eq 'max_preamble_size') {
42 0 ▶ Carp::croak(qq/Parameter '$p' is not a positive integer/)
Branch: 1 branch
BranchTrueFalseTotal
unless ref \$v eq "SCALAR" and defined $v and $v =~ /\A [1-9][0-9]* \z/x 0 0 0
Condition0%0/6
ref \$v eq "SCALAR" and defined $v and $v =~ /\A [1-9][0-9]* \z/x
!ll&&!rl&&r
000
ref \$v eq "SCALAR" and defined $v
!ll&&!rl&&r
000
MC/DC0%0/3
ref \$v eq "SCALAR" and defined $v and $v =~ /\A [1-9][0-9]* \z/x
ref \$v eq "SCALAR" defined $v $v =~ /\A [1-9][0-9]* \z/x
Truth table0%0/4
A and B and C
A= ref \$v eq "SCALAR"B= defined $vC= $v =~ /\A [1-9][0-9]* \z/x
ABCresult
0XX0
10X0
1100
1111
43 unless ref \$v eq 'SCALAR' && defined $v && $v =~ /\A [1-9][0-9]* \z/x;
44 0 $self->{$p} = $v;
45 }
46 elsif ($p eq 'on_header_as') {
47 0 ▶ Carp::croak(q/Parameter 'on_header_as' must be either 'unparsed' or 'lines'/)
Branch: 1 branch
BranchTrueFalseTotal
unless ref \$v eq "SCALAR" and defined $v and $v =~ /\A (?: unparsed | lines) \z/x 0 0 0
Condition0%0/6
ref \$v eq "SCALAR" and defined $v and $v =~ /\A (?: unparsed | lines) \z/x
!ll&&!rl&&r
000
ref \$v eq "SCALAR" and defined $v
!ll&&!rl&&r
000
MC/DC0%0/3
ref \$v eq "SCALAR" and defined $v and $v =~ /\A (?: unparsed | lines) \z/x
ref \$v eq "SCALAR" defined $v $v =~ /\A (?: unparsed | lines) \z/x
Truth table0%0/4
A and B and C
A= ref \$v eq "SCALAR"B= defined $vC= $v =~ /\A (?: unparsed | lines) \z/x
ABCresult
0XX0
10X0
1100
1111
48 unless ref \$v eq 'SCALAR' && defined $v && $v =~ /\A (?: unparsed | lines) \z/x;
49 0 $self->{on_header_as} = $v;
50 }
51 else {
52 0 Carp::croak(qq/Unknown parameter '$p' passed to constructor/);
53 }
54 }
55  
56 27 for my $p (qw(boundary on_header on_body)) {
57 Carp::croak(qq/Mandatory parameter '$p' is missing/)
58 81 ▶ unless exists $self->{$p};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{$p} 0 81 81
59 }
60  
61 27 bless $self, $class;
62 27 $self->{parser} = $_mk_parser->($self);
63 27 return $self;
64 }
65  
66 sub parse {
67 38 ▶ @_ == 2 || Carp::croak(q/Usage: $parser->parse($octets)/);
Subroutine parse: called CC=2 SCAR=7.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or Carp::croak("Usage: \$parser->parse(\$octets)") 0 38 38
68 38 return $_[0]->{parser}->($_[1]);
69 }
70  
71 sub finish {
72 27 ▶ @_ == 1 || Carp::croak(q/Usage: $parser->finish()/);
Subroutine finish: called CC=2 SCAR=7.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or Carp::croak("Usage: \$parser->finish()") 0 27 27
73 27 return $_[0]->{parser}->('', 1);
74 }
75  
76 sub reset {
77 0 ▶ @_ == 1 || Carp::croak(q/Usage: $parser->reset()/);
Subroutine reset: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or Carp::croak("Usage: \$parser->reset()") 0 0 0
78 0 $_[0]->{parser} = $_mk_parser->($_[0]);
79 0 $_[0]->{aborted} = !!0;
80 }
81  
82 sub is_aborted {
83 0 ▶ @_ == 1 || Carp::croak(q/Usage: $parser->is_aborted()/);
Subroutine is_aborted: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or Carp::croak("Usage: \$parser->is_aborted()") 0 0 0
84 0 return $_[0]->{aborted};
85 }
86  
87 sub CRLF () { "\x0D\x0A" }
88 sub TRUE () { !!1 }
89 sub FALSE () { !!0 }
90  
91 sub STATE_PREAMBLE () { 1 }
92 sub STATE_BOUNDARY () { 2 }
93 sub STATE_HEADER () { 3 }
94 sub STATE_BODY () { 4 }
95 sub STATE_EPILOGUE () { 5 }
96  
97 $_mk_parser = sub {
98 27 ▶ Scalar::Util::weaken(my $self = $_[0]);
Subroutine __ANON__: called CC=2 SCAR=6.9
99  
100 # RFC 2616 3.7.2 Multipart Types
101 # The message body is itself a protocol element and MUST therefore use only
102 # CRLF to represent line breaks between body-parts.
103 27 my $boundary = $self->{boundary};
104 27 my $boundary_preamble = '--' . $boundary;
105 27 my $boundary_delimiter = CRLF . '--' . $boundary;
106  
107 27 my $chunk = '';
108 27 my $buffer = '';
109 27 my $state = STATE_PREAMBLE;
110 27 my $finish = FALSE;
111 27 my $aborted = FALSE;
112
113 27 my $on_header = $self->{on_header};
114 27 my $on_body = $self->{on_body};
115 my $on_error = sub {
116 11 ▶ $aborted = $self->{aborted} = TRUE;
Subroutine __ANON__: called CC=1 SCAR=0.0
117 11 goto $self->{on_error};
118 27 };
119
120 27 ▶ if ($self->{on_header_as} eq 'lines') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"on_header_as"} eq "lines") 27 0 27
121 $on_header = sub {
122 115 ▶ my @headers;
Subroutine __ANON__: called CC=7 SCAR=19.5
123 115 for (split /\x0D\x0A/, $_[0]) {
124 170 ▶ if (/\A [^\x00-\x1F\x7F:]+ : /x) {
Branch: 2 branches
BranchTrueFalseTotal
if (/\A [^\x00-\x1F\x7F:]+ : /x) { } 160 10 170
elsif (s/\A [\x09\x20]+ //x) { } 9 1 10
125 160 push @headers, $_;
126 }
127 elsif (s/\A [\x09\x20]+ //x) {
128 9 ▶ if (!@headers) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@headers) 2 7 9
129 2 $on_error->(q/Continuation line seen before first header/);
130 2 return;
131 }
132 7 ▶ next unless length;
Branch: 1 branch
BranchTrueFalseTotal
unless length $_ 2 5 7
133 5 ▶ $headers[-1] .= ' ' unless $headers[-1] =~ /[\x09\x20]\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $headers[-1] =~ /[\x09\x20]\z/ 3 2 5
134 5 $headers[-1] .= $_;
135 }
136 else {
137 1 $on_error->(q/Malformed header line/);
138 1 return;
139 }
140 }
141 112 $self->{on_header}->(\@headers);
142 27 };
143 }
144
145 return sub {
146 65 ▶ $buffer .= $_[0];
Subroutine __ANON__: called CC=25 SCAR=32.3
147 65 $finish = $_[1];
148  
149 65 ▶ while (!$aborted) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($aborted) 445 9 454
150 445 ▶ if ($state == STATE_PREAMBLE) {
Branch: 5 branches
BranchTrueFalseTotal
if ($state == 1) { } 28 417 445
elsif ($state == 2) { } 139 278 417
elsif ($state == 3) { } 121 157 278
elsif ($state == 4) { } 124 33 157
elsif ($state == 5) { } 33 0 33
151 28 my $pos = index($buffer, $boundary_preamble);
152 28 ▶ if ($pos < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < 0) 2 26 28
153 2 ▶ if (length $buffer > $self->{max_preamble_size}) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $buffer > $self->{"max_preamble_size"}) 0 2 2
154 0 $on_error->(q/Size of preamble exceeds maximum allowed/);
155 0 last;
156 }
157 2 ▶ $finish && $on_error->(q/End of stream encountered while parsing preamble/);
Branch: 1 branch
BranchTrueFalseTotal
$finish and &$on_error("End of stream encountered while parsing preamble") 1 1 2
158 2 last;
159 }
160 26 substr($buffer, 0, $pos + 2 + length $boundary, '');
161 26 $state = STATE_BOUNDARY;
162 }
163 elsif ($state == STATE_BOUNDARY) {
164 139 ▶ if (length $buffer < 2) {
Branch: 3 branches
BranchTrueFalseTotal
if (length $buffer < 2) { } 2 137 139
elsif (substr($buffer, 0, 2) eq "\r\n") { } 116 21 137
elsif (substr($buffer, 0, 2) eq '--') { } 20 1 21
165 2 ▶ $finish && $on_error->(q/End of stream encountered while parsing boundary/);
Branch: 1 branch
BranchTrueFalseTotal
$finish and &$on_error("End of stream encountered while parsing boundary") 1 1 2
166 2 last;
167 }
168 elsif (substr($buffer, 0, 2) eq CRLF) {
169 116 substr($buffer, 0, 2, '');
170 116 $state = STATE_HEADER;
171 }
172 elsif (substr($buffer, 0, 2) eq '--') {
173 20 ▶ if (length $buffer < 4) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $buffer < 4) { } 2 18 20
elsif (substr($buffer, 2, 2) eq "\r\n") { } 17 1 18
174 2 ▶ $finish && $on_error->(q/End of stream encountered while parsing closing boundary/);
Branch: 1 branch
BranchTrueFalseTotal
$finish and &$on_error("End of stream encountered while parsing closing boundary") 1 1 2
175 2 last;
176 }
177 elsif (substr($buffer, 2, 2) eq CRLF) {
178 17 substr($buffer, 0, 4, '');
179 17 $state = STATE_EPILOGUE;
180 }
181 else {
182 1 $on_error->(q/Closing boundary does not terminate with CRLF/);
183 1 last;
184 }
185 }
186 else {
187 1 $on_error->(q/Boundary does not terminate with CRLF or hyphens/);
188 1 last;
189 }
190 }
191 elsif ($state == STATE_HEADER) {
192 121 my $pos = index($buffer, CRLF . CRLF);
193 121 ▶ if ($pos < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < 0) 6 115 121
194 6 ▶ if (length $buffer > $self->{max_header_size}) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $buffer > $self->{"max_header_size"}) 0 6 6
195 0 $on_error->(q/Size of part header exceeds maximum allowed/);
196 0 last;
197 }
198 6 ▶ $finish && $on_error->(q/End of stream encountered while parsing part header/);
Branch: 1 branch
BranchTrueFalseTotal
$finish and &$on_error("End of stream encountered while parsing part header") 1 5 6
199 6 last;
200 }
201  
202 115 $chunk = substr($buffer, 0, $pos + 4, '');
203 115 $state = STATE_BODY;
204 115 $on_header->($chunk);
205 }
206 elsif ($state == STATE_BODY) {
207 124 my $take = index($buffer, $boundary_delimiter);
208 124 ▶ if ($take < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($take < 0) { } 13 111 124
209 13 $take = length($buffer) - (6 + length $boundary);
210 13 ▶ if ($take <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($take <= 0) 9 4 13
211 9 ▶ $finish && $on_error->(q/End of stream encountered while parsing part body/);
Branch: 1 branch
BranchTrueFalseTotal
$finish and &$on_error("End of stream encountered while parsing part body") 1 8 9
212 9 last;
213 }
214 }
215 else {
216 111 $state = STATE_BOUNDARY;
217 }
218  
219 115 $chunk = substr($buffer, 0, $take, '');
220  
221 115 ▶ if ($state == STATE_BOUNDARY) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state == 2) 111 4 115
222 111 substr($buffer, 0, 4 + length $boundary, '');
223 }
224  
225 115 $on_body->($chunk, $state == STATE_BOUNDARY);
226 }
227 # RFC 2616 3.7.2 Multipart Types
228 # Unlike in RFC 2046, the epilogue of any multipart message MUST be
229 # empty; HTTP applications MUST NOT transmit the epilogue (even if the
230 # original multipart contains an epilogue). These restrictions exist in
231 # order to preserve the self-delimiting nature of a multipart message-
232 # body, wherein the "end" of the message-body is indicated by the
233 # ending multipart boundary.
234 elsif ($state == STATE_EPILOGUE) {
235 33 ▶ (length $buffer == 0)
Branch: 1 branch
BranchTrueFalseTotal
length $buffer == 0 or &$on_error("Nonempty epilogue") 1 32 33
236 || $on_error->(q/Nonempty epilogue/);
237 33 last;
238 }
239 else {
240 0 Carp::croak(qq/panic: unknown state: $state/);
241 }
242 }
243 65 return !$aborted;
244 27 };
245 4 };
246  
247 4 1;
↑ Summary