Mojo/Content/MultiPart.pm

filter: Statementstmt 100% 100 / 100 Branchbran 92.5% 50 / 54 Conditioncond 86.6% 13 / 15 MC/DCmcdc 66.6% 6 / 9 Subroutinesub 100% 14 / 14 Podpod 100% 7 / 7 totaltotal 95.4% 190 / 199 CC 36 SCAR 18.9
CC
36
Cov
96%
_read
11.0
get_body_chunk
11.0
build_boundary
5.0
CRAP 6.6
« Mojo/JSON.pm ↑ Summary Mojo/Message.pm »
1 package Mojo::Content::MultiPart;
2 30 ▶use Mojo::Base 'Mojo::Content';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 30 ▶use Mojo::Util qw(b64_encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 29 ▶has parts => sub { [] };
Subroutine __ANON__: called CC=1 SCAR=0.0
7  
8 24 ▶sub body_contains {
Subroutine body_contains: called CC=4 SCAR=13.9
Pod documented
9 24 my ($self, $chunk) = @_;
10 24 ▶ ($_->headers_contain($chunk) or $_->body_contains($chunk)) and return 1 for @{$self->parts};
Branch: 1 branch
BranchTrueFalseTotal
$_->headers_contain($chunk) || $_->body_contains($chunk) and return 1 14 20 34
Condition100%3/3
$_->headers_contain($chunk) or $_->body_contains($chunk)
l!l&&r!l&&!r
11320
MC/DC100%2/2
$_->headers_contain($chunk) or $_->body_contains($chunk)
$_->headers_contain($chunk) $_->body_contains($chunk)
Truth table100%3/3
A or B
A= $_->headers_contain($chunk)B= $_->body_contains($chunk)
ABresult
1X1
011
000
11 10 return undef;
12 }
13  
14 30 ▶sub body_size {
Subroutine body_size: called CC=3 SCAR=11.0
Pod documented
15 30 my $self = shift;
16  
17 # Check for existing Content-Length header
18 30 ▶ if (my $len = $self->headers->content_length) { return $len }
Branch: 1 branch
BranchTrueFalseTotal
if (my $len = $self->headers->content_length) 23 7 30
19  
20 # Calculate length of whole body
21 7 my $len = my $boundary_len = length($self->build_boundary) + 6;
22 7 $len += $_->header_size + $_->body_size + $boundary_len for @{$self->parts};
23  
24 7 return $len;
25 }
26  
27 19 ▶sub build_boundary {
Subroutine build_boundary: called CC=5 SCAR=16.1
Pod documented
28 19 my $self = shift;
29  
30 # Check for existing boundary
31 19 my $boundary;
32 19 ▶ return $boundary if defined($boundary = $self->boundary);
Branch: 1 branch
BranchTrueFalseTotal
if defined($boundary = $self->boundary) 16 3 19
33  
34 # Generate and check boundary
35 3 my $size = 1;
36 3 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$self->body_contains($boundary) and do { do { $boundary = &b64_encode(join("", map(chr(rand 256), 1 .. $size++ * 3))); $boundary =~ s/\W/X/gu } } 0 3 3
37 3 $boundary = b64_encode join('', map chr(rand 256), 1 .. $size++ * 3);
38 3 $boundary =~ s/\W/X/g;
39 } while $self->body_contains($boundary);
40  
41 # Add boundary to Content-Type header
42 3 my $headers = $self->headers;
43 3 my ($before, $after) = ('multipart/mixed', '');
44 3 ▶ ($before, $after) = ($1, $2) if ($headers->content_type // '') =~ m!^(.*multipart/[^;]+)(.*)$!;
Branch: 1 branch
BranchTrueFalseTotal
if ($headers->content_type // "") =~ m[^(.*multipart/[^;]+)(.*)$]u 2 1 3
Condition100%2/2
$headers->content_type // ""
l!l
21
MC/DC100%1/1
$headers->content_type // ""
$headers->content_type
Truth table100%2/2
A
A= $headers->content_type
Aresult
11
00
45 3 $headers->content_type("$before; boundary=$boundary$after");
46  
47 3 return $boundary;
48 }
49  
50 2 ▶sub clone {
Subroutine clone: called CC=2 SCAR=7.1
Pod documented
51 2 my $self = shift;
52 2 ▶ return undef unless my $clone = $self->SUPER::clone();
Branch: 1 branch
BranchTrueFalseTotal
unless my $clone = $self->SUPER::clone 0 2 2
53 2 return $clone->parts($self->parts);
54 }
55  
56 65 ▶sub get_body_chunk {
Subroutine get_body_chunk: called CC=11 SCAR=24.0
Pod documented
57 65 my ($self, $offset) = @_;
58  
59 # Body generator
60 65 ▶ return $self->generate_body_chunk($offset) if $self->is_dynamic;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_dynamic 0 65 65
61  
62 # First boundary
63 65 ▶ my $boundary = $self->{boundary} //= $self->build_boundary;
Condition66%2/3
$self->{"boundary"} //= $self->build_boundary
l!l&&r!l&&!r
5780
MC/DC0%0/2
$self->{"boundary"} //= $self->build_boundary
$self->{"boundary"} $self->build_boundary
Truth table66%2/3
A //= B
A= $self->{"boundary"}B= $self->build_boundary
ABresult
1X1
011
000
64 65 my $boundary_len = length($boundary) + 6;
65 65 my $len = $boundary_len - 2;
66 65 ▶ return substr "--$boundary\x0d\x0a", $offset if $len > $offset;
Branch: 1 branch
BranchTrueFalseTotal
if $len > $offset 7 58 65
67  
68 # Skip parts that have already been processed
69 58 my $start = 0;
70 58 ▶ ($len, $start) = ($self->{last_len}, $self->{last_part} + 1) if $self->{offset} && $offset > $self->{offset};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"offset"} and $offset > $self->{"offset"} 29 29 58
Condition100%3/3
$self->{"offset"} and $offset > $self->{"offset"}
!ll&&!rl&&r
27229
MC/DC100%2/2
$self->{"offset"} and $offset > $self->{"offset"}
$self->{"offset"} $offset > $self->{"offset"}
Truth table100%3/3
A and B
A= $self->{"offset"}B= $offset > $self->{"offset"}
ABresult
0X0
100
111
71  
72 # Prepare content part by part
73 58 my $parts = $self->parts;
74 58 ▶ for (my $i = $start; $i < @$parts; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$parts) 75 5 80
75 75 my $part = $parts->[$i];
76  
77 # Headers
78 75 my $header_len = $part->header_size;
79 75 ▶ return $part->get_header_chunk($offset - $len) if ($len + $header_len) > $offset;
Branch: 1 branch
BranchTrueFalseTotal
if $len + $header_len > $offset 18 57 75
80 57 $len += $header_len;
81  
82 # Content
83 57 my $content_len = $part->body_size;
84 57 ▶ return $part->get_body_chunk($offset - $len) if ($len + $content_len) > $offset;
Branch: 1 branch
BranchTrueFalseTotal
if $len + $content_len > $offset 14 43 57
85 43 $len += $content_len;
86  
87 # Boundary
88 43 ▶ if ($#$parts == $i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parts->$#* == $i) 15 28 43
89 15 $boundary .= '--';
90 15 $boundary_len += 2;
91 }
92 43 ▶ return substr "\x0d\x0a--$boundary\x0d\x0a", $offset - $len if ($len + $boundary_len) > $offset;
Branch: 1 branch
BranchTrueFalseTotal
if $len + $boundary_len > $offset 21 22 43
93 22 $len += $boundary_len;
94  
95 22 @{$self}{qw(last_len last_part offset)} = ($len, $i, $offset);
96 }
97 }
98  
99 66 ▶sub is_multipart {1}
Subroutine is_multipart: called CC=1 SCAR=0.0
Pod documented
100  
101 52 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
102 52 my $self = shift->SUPER::new(@_);
103 52 $self->on(read => \&_read);
104 52 return $self;
105 }
106  
107 200 ▶sub _parse_multipart_body {
Subroutine _parse_multipart_body: called CC=3 SCAR=11.0
108 200 my ($self, $boundary) = @_;
109  
110 # Whole part in buffer
111 200 my $pos = index $self->{multipart}, "\x0d\x0a--$boundary";
112 200 ▶ if ($pos < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < 0) 127 73 200
113 127 my $len = length($self->{multipart}) - (length($boundary) + 8);
114 127 ▶ return undef unless $len > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $len > 0 44 83 127
115  
116 # Store chunk
117 83 my $chunk = substr $self->{multipart}, 0, $len, '';
118 83 $self->parts->[-1] = $self->parts->[-1]->parse($chunk);
119 83 return undef;
120 }
121  
122 # Store chunk
123 73 my $chunk = substr $self->{multipart}, 0, $pos, '';
124 73 $self->parts->[-1] = $self->parts->[-1]->parse($chunk);
125 73 return !!($self->{multi_state} = 'multipart_boundary');
126 }
127  
128 104 ▶sub _parse_multipart_boundary {
Subroutine _parse_multipart_boundary: called CC=3 SCAR=11.0
129 104 my ($self, $boundary) = @_;
130  
131 # Boundary begins
132 104 ▶ if ((index $self->{multipart}, "\x0d\x0a--$boundary\x0d\x0a") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($self->{"multipart"}, "\r\n--$boundary\r\n") == 0) 74 30 104
133 74 substr $self->{multipart}, 0, length($boundary) + 6, '';
134  
135 # New part
136 74 my $part = Mojo::Content::Single->new(relaxed => 1);
137 74 $self->emit(part => $part);
138 74 push @{$self->parts}, $part;
139 74 return !!($self->{multi_state} = 'multipart_body');
140 }
141  
142 # Boundary ends
143 30 my $end = "\x0d\x0a--$boundary--";
144 30 ▶ if ((index $self->{multipart}, $end) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($self->{"multipart"}, $end) == 0) 24 6 30
145 24 substr $self->{multipart}, 0, length $end, '';
146 24 $self->{multi_state} = 'finished';
147 }
148  
149 30 return undef;
150 }
151  
152 43 ▶sub _parse_multipart_preamble {
Subroutine _parse_multipart_preamble: called CC=2 SCAR=6.9
153 43 my ($self, $boundary) = @_;
154  
155 # No boundary yet
156 43 ▶ return undef if (my $pos = index $self->{multipart}, "--$boundary") < 0;
Branch: 1 branch
BranchTrueFalseTotal
if (my $pos = index($self->{"multipart"}, "--$boundary")) < 0 17 26 43
157  
158 # Replace preamble with carriage return and line feed
159 26 substr $self->{multipart}, 0, $pos, "\x0d\x0a";
160  
161 # Parse boundary
162 26 return !!($self->{multi_state} = 'multipart_boundary');
163 }
164  
165 176 ▶sub _read {
Subroutine _read: called CC=11 SCAR=24.0
166 176 my ($self, $chunk) = @_;
167  
168 176 $self->{multipart} .= $chunk;
169 176 my $boundary = $self->boundary;
170 176 ▶ until (($self->{multi_state} //= 'multipart_preamble') eq 'finished') {
Branch: 1 branch
BranchTrueFalseTotal
unless (($self->{"multi_state"} //= "multipart_preamble") eq "finished") 347 2 349
Condition100%2/2
$self->{"multi_state"} //= "multipart_preamble"
l!l
32227
MC/DC100%1/1
$self->{"multi_state"} //= "multipart_preamble"
$self->{"multi_state"}
Truth table100%2/2
A
A= $self->{"multi_state"}
Aresult
11
00
171  
172 # Preamble
173 347 ▶ if ($self->{multi_state} eq 'multipart_preamble') { last unless $self->_parse_multipart_preamble($boundary) }
Branch: 4 branches
BranchTrueFalseTotal
if ($self->{'multi_state'} eq 'multipart_preamble') { } 43 304 347
elsif ($self->{'multi_state'} eq 'multipart_boundary') { } 104 200 304
elsif ($self->{'multi_state'} eq 'multipart_body') { } 200 0 200
unless $self->_parse_multipart_preamble($boundary) 17 26 43
174  
175 # Boundary
176 104 ▶ elsif ($self->{multi_state} eq 'multipart_boundary') { last unless $self->_parse_multipart_boundary($boundary) }
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_parse_multipart_boundary($boundary) 30 74 104
177  
178 # Body
179 200 ▶ elsif ($self->{multi_state} eq 'multipart_body') { last unless $self->_parse_multipart_body($boundary) }
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_parse_multipart_body($boundary) 127 73 200
180 }
181  
182 # Check buffer size
183 176 ▶ @$self{qw(state limit)} = ('finished', 1) if length($self->{multipart} // '') > $self->max_buffer_size;
Branch: 1 branch
BranchTrueFalseTotal
if length($self->{"multipart"} // "") > $self->max_buffer_size 1 175 176
Condition50%1/2
$self->{"multipart"} // ""
l!l
1760
MC/DC0%0/1
$self->{"multipart"} // ""
$self->{"multipart"}
Truth table50%1/2
A
A= $self->{"multipart"}
Aresult
11
00
184 }
185  
186 30 1;
187  
188 =encoding utf8
189  
190 =head1 NAME
191  
192 Mojo::Content::MultiPart - HTTP multipart content
193  
194 =head1 SYNOPSIS
195  
196 use Mojo::Content::MultiPart;
197  
198 my $multi = Mojo::Content::MultiPart->new;
199 $multi->parse('Content-Type: multipart/mixed; boundary=---foobar');
200 my $single = $multi->parts->[4];
201  
202 =head1 DESCRIPTION
203  
204 L<Mojo::Content::MultiPart> is a container for HTTP multipart content, based on L<RFC
205 7230|https://tools.ietf.org/html/rfc7230>, L<RFC 7231|https://tools.ietf.org/html/rfc7231> and L<RFC
206 2388|https://tools.ietf.org/html/rfc2388>.
207  
208 =head1 EVENTS
209  
210 L<Mojo::Content::MultiPart> inherits all events from L<Mojo::Content> and can emit the following new ones.
211  
212 =head2 part
213  
214 $multi->on(part => sub ($multi, $single) {...});
215  
216 Emitted when a new L<Mojo::Content::Single> part starts.
217  
218 $multi->on(part => sub ($multi, $single) {
219 return unless $single->headers->content_disposition =~ /name="([^"]+)"/;
220 say "Field: $1";
221 });
222  
223 =head1 ATTRIBUTES
224  
225 L<Mojo::Content::MultiPart> inherits all attributes from L<Mojo::Content> and implements the following new ones.
226  
227 =head2 parts
228  
229 my $parts = $multi->parts;
230 $multi = $multi->parts([Mojo::Content::Single->new]);
231  
232 Content parts embedded in this multipart content, usually L<Mojo::Content::Single> objects.
233  
234 =head1 METHODS
235  
236 L<Mojo::Content::MultiPart> inherits all methods from L<Mojo::Content> and implements the following new ones.
237  
238 =head2 body_contains
239  
240 my $bool = $multi->body_contains('foobarbaz');
241  
242 Check if content parts contain a specific string.
243  
244 =head2 body_size
245  
246 my $size = $multi->body_size;
247  
248 Content size in bytes.
249  
250 =head2 build_boundary
251  
252 my $boundary = $multi->build_boundary;
253  
254 Generate a suitable boundary for content and add it to C<Content-Type> header.
255  
256 =head2 clone
257  
258 my $clone = $multi->clone;
259  
260 Return a new L<Mojo::Content::MultiPart> object cloned from this content if possible, otherwise return C<undef>.
261  
262 =head2 get_body_chunk
263  
264 my $bytes = $multi->get_body_chunk(0);
265  
266 Get a chunk of content starting from a specific position. Note that it might not be possible to get the same chunk
267 twice if content was generated dynamically.
268  
269 =head2 is_multipart
270  
271 my $bool = $multi->is_multipart;
272  
273 True, this is a L<Mojo::Content::MultiPart> object.
274  
275 =head2 new
276  
277 my $multi = Mojo::Content::MultiPart->new;
278 my $multi
279 = Mojo::Content::MultiPart->new(parts => [Mojo::Content::Single->new]);
280 my $multi
281 = Mojo::Content::MultiPart->new({parts => [Mojo::Content::Single->new]});
282  
283 Construct a new L<Mojo::Content::MultiPart> object and subscribe to event L<Mojo::Content/"read"> with default content
284 parser.
285  
286 =head1 SEE ALSO
287  
288 L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
289  
290 =cut
« Mojo/JSON.pm ↑ Summary Mojo/Message.pm »