line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojo::Content::MultiPart; |
2
|
60
|
|
|
60
|
|
908
|
use Mojo::Base 'Mojo::Content'; |
|
60
|
|
|
|
|
2315
|
|
|
60
|
|
|
|
|
461
|
|
3
|
|
|
|
|
|
|
|
4
|
60
|
|
|
60
|
|
490
|
use Mojo::Util qw(b64_encode); |
|
60
|
|
|
|
|
192
|
|
|
60
|
|
|
|
|
110451
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
has parts => sub { [] }; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
sub body_contains { |
9
|
39
|
|
|
39
|
1
|
109
|
my ($self, $chunk) = @_; |
10
|
39
|
|
100
|
|
|
68
|
($_->headers_contain($chunk) or $_->body_contains($chunk)) and return 1 for @{$self->parts}; |
|
39
|
|
100
|
|
|
115
|
|
11
|
25
|
|
|
|
|
116
|
return undef; |
12
|
|
|
|
|
|
|
} |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub body_size { |
15
|
24
|
|
|
24
|
1
|
57
|
my $self = shift; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
# Check for existing Content-Length header |
18
|
24
|
100
|
|
|
|
73
|
if (my $len = $self->headers->content_length) { return $len } |
|
2
|
|
|
|
|
11
|
|
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
# Calculate length of whole body |
21
|
22
|
|
|
|
|
99
|
my $len = my $boundary_len = length($self->build_boundary) + 6; |
22
|
22
|
|
|
|
|
52
|
$len += $_->header_size + $_->body_size + $boundary_len for @{$self->parts}; |
|
22
|
|
|
|
|
64
|
|
23
|
|
|
|
|
|
|
|
24
|
22
|
|
|
|
|
148
|
return $len; |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub build_boundary { |
28
|
49
|
|
|
49
|
1
|
97
|
my $self = shift; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
# Check for existing boundary |
31
|
49
|
|
|
|
|
80
|
my $boundary; |
32
|
49
|
100
|
|
|
|
202
|
return $boundary if defined($boundary = $self->boundary); |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
# Generate and check boundary |
35
|
18
|
|
|
|
|
54
|
my $size = 1; |
36
|
18
|
|
|
|
|
42
|
do { |
37
|
18
|
|
|
|
|
245
|
$boundary = b64_encode join('', map chr(rand 256), 1 .. $size++ * 3); |
38
|
18
|
|
|
|
|
159
|
$boundary =~ s/\W/X/g; |
39
|
|
|
|
|
|
|
} while $self->body_contains($boundary); |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
# Add boundary to Content-Type header |
42
|
18
|
|
|
|
|
69
|
my $headers = $self->headers; |
43
|
18
|
|
|
|
|
58
|
my ($before, $after) = ('multipart/mixed', ''); |
44
|
18
|
100
|
100
|
|
|
59
|
($before, $after) = ($1, $2) if ($headers->content_type // '') =~ m!^(.*multipart/[^;]+)(.*)$!; |
45
|
18
|
|
|
|
|
116
|
$headers->content_type("$before; boundary=$boundary$after"); |
46
|
|
|
|
|
|
|
|
47
|
18
|
|
|
|
|
79
|
return $boundary; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub clone { |
51
|
2
|
|
|
2
|
1
|
4
|
my $self = shift; |
52
|
2
|
50
|
|
|
|
13
|
return undef unless my $clone = $self->SUPER::clone(); |
53
|
2
|
|
|
|
|
8
|
return $clone->parts($self->parts); |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub get_body_chunk { |
57
|
184
|
|
|
184
|
1
|
382
|
my ($self, $offset) = @_; |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
# Body generator |
60
|
184
|
50
|
|
|
|
497
|
return $self->generate_body_chunk($offset) if $self->is_dynamic; |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
# First boundary |
63
|
184
|
|
66
|
|
|
561
|
my $boundary = $self->{boundary} //= $self->build_boundary; |
64
|
184
|
|
|
|
|
351
|
my $boundary_len = length($boundary) + 6; |
65
|
184
|
|
|
|
|
295
|
my $len = $boundary_len - 2; |
66
|
184
|
100
|
|
|
|
439
|
return substr "--$boundary\x0d\x0a", $offset if $len > $offset; |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
# Skip parts that have already been processed |
69
|
162
|
|
|
|
|
257
|
my $start = 0; |
70
|
162
|
100
|
100
|
|
|
570
|
($len, $start) = ($self->{last_len}, $self->{last_part} + 1) if $self->{offset} && $offset > $self->{offset}; |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
# Prepare content part by part |
73
|
162
|
|
|
|
|
397
|
my $parts = $self->parts; |
74
|
162
|
|
|
|
|
509
|
for (my $i = $start; $i < @$parts; $i++) { |
75
|
192
|
|
|
|
|
330
|
my $part = $parts->[$i]; |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
# Headers |
78
|
192
|
|
|
|
|
448
|
my $header_len = $part->header_size; |
79
|
192
|
100
|
|
|
|
558
|
return $part->get_header_chunk($offset - $len) if ($len + $header_len) > $offset; |
80
|
146
|
|
|
|
|
246
|
$len += $header_len; |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
# Content |
83
|
146
|
|
|
|
|
379
|
my $content_len = $part->body_size; |
84
|
146
|
100
|
|
|
|
433
|
return $part->get_body_chunk($offset - $len) if ($len + $content_len) > $offset; |
85
|
101
|
|
|
|
|
162
|
$len += $content_len; |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
# Boundary |
88
|
101
|
100
|
|
|
|
237
|
if ($#$parts == $i) { |
89
|
47
|
|
|
|
|
104
|
$boundary .= '--'; |
90
|
47
|
|
|
|
|
78
|
$boundary_len += 2; |
91
|
|
|
|
|
|
|
} |
92
|
101
|
100
|
|
|
|
388
|
return substr "\x0d\x0a--$boundary\x0d\x0a", $offset - $len if ($len + $boundary_len) > $offset; |
93
|
52
|
|
|
|
|
89
|
$len += $boundary_len; |
94
|
|
|
|
|
|
|
|
95
|
52
|
|
|
|
|
132
|
@{$self}{qw(last_len last_part offset)} = ($len, $i, $offset); |
|
52
|
|
|
|
|
329
|
|
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
155
|
|
|
155
|
1
|
727
|
sub is_multipart {1} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
sub new { |
102
|
81
|
|
|
81
|
1
|
2966
|
my $self = shift->SUPER::new(@_); |
103
|
81
|
|
|
|
|
432
|
$self->on(read => \&_read); |
104
|
81
|
|
|
|
|
277
|
return $self; |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
sub _parse_multipart_body { |
108
|
304
|
|
|
304
|
|
598
|
my ($self, $boundary) = @_; |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
# Whole part in buffer |
111
|
304
|
|
|
|
|
972
|
my $pos = index $self->{multipart}, "\x0d\x0a--$boundary"; |
112
|
304
|
100
|
|
|
|
723
|
if ($pos < 0) { |
113
|
205
|
|
|
|
|
422
|
my $len = length($self->{multipart}) - (length($boundary) + 8); |
114
|
205
|
100
|
|
|
|
517
|
return undef unless $len > 0; |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
# Store chunk |
117
|
136
|
|
|
|
|
686
|
my $chunk = substr $self->{multipart}, 0, $len, ''; |
118
|
136
|
|
|
|
|
372
|
$self->parts->[-1] = $self->parts->[-1]->parse($chunk); |
119
|
136
|
|
|
|
|
457
|
return undef; |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
# Store chunk |
123
|
99
|
|
|
|
|
292
|
my $chunk = substr $self->{multipart}, 0, $pos, ''; |
124
|
99
|
|
|
|
|
295
|
$self->parts->[-1] = $self->parts->[-1]->parse($chunk); |
125
|
99
|
|
|
|
|
544
|
return !!($self->{multi_state} = 'multipart_boundary'); |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
sub _parse_multipart_boundary { |
129
|
144
|
|
|
144
|
|
287
|
my ($self, $boundary) = @_; |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
# Boundary begins |
132
|
144
|
100
|
|
|
|
576
|
if ((index $self->{multipart}, "\x0d\x0a--$boundary\x0d\x0a") == 0) { |
133
|
100
|
|
|
|
|
269
|
substr $self->{multipart}, 0, length($boundary) + 6, ''; |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
# New part |
136
|
100
|
|
|
|
|
316
|
my $part = Mojo::Content::Single->new(relaxed => 1); |
137
|
100
|
|
|
|
|
345
|
$self->emit(part => $part); |
138
|
100
|
|
|
|
|
153
|
push @{$self->parts}, $part; |
|
100
|
|
|
|
|
265
|
|
139
|
100
|
|
|
|
|
528
|
return !!($self->{multi_state} = 'multipart_body'); |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
# Boundary ends |
143
|
44
|
|
|
|
|
131
|
my $end = "\x0d\x0a--$boundary--"; |
144
|
44
|
100
|
|
|
|
189
|
if ((index $self->{multipart}, $end) == 0) { |
145
|
38
|
|
|
|
|
120
|
substr $self->{multipart}, 0, length $end, ''; |
146
|
38
|
|
|
|
|
85
|
$self->{multi_state} = 'finished'; |
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
|
149
|
44
|
|
|
|
|
169
|
return undef; |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
sub _parse_multipart_preamble { |
153
|
57
|
|
|
57
|
|
150
|
my ($self, $boundary) = @_; |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
# No boundary yet |
156
|
57
|
100
|
|
|
|
315
|
return undef if (my $pos = index $self->{multipart}, "--$boundary") < 0; |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
# Replace preamble with carriage return and line feed |
159
|
40
|
|
|
|
|
122
|
substr $self->{multipart}, 0, $pos, "\x0d\x0a"; |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
# Parse boundary |
162
|
40
|
|
|
|
|
213
|
return !!($self->{multi_state} = 'multipart_boundary'); |
163
|
|
|
|
|
|
|
} |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
sub _read { |
166
|
268
|
|
|
268
|
|
535
|
my ($self, $chunk) = @_; |
167
|
|
|
|
|
|
|
|
168
|
268
|
|
|
|
|
1301
|
$self->{multipart} .= $chunk; |
169
|
268
|
|
|
|
|
722
|
my $boundary = $self->boundary; |
170
|
268
|
|
100
|
|
|
1440
|
until (($self->{multi_state} //= 'multipart_preamble') eq 'finished') { |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
# Preamble |
173
|
505
|
100
|
|
|
|
1655
|
if ($self->{multi_state} eq 'multipart_preamble') { last unless $self->_parse_multipart_preamble($boundary) } |
|
57
|
100
|
|
|
|
189
|
|
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
# Boundary |
176
|
144
|
100
|
|
|
|
355
|
elsif ($self->{multi_state} eq 'multipart_boundary') { last unless $self->_parse_multipart_boundary($boundary) } |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
# Body |
179
|
304
|
100
|
|
|
|
688
|
elsif ($self->{multi_state} eq 'multipart_body') { last unless $self->_parse_multipart_body($boundary) } |
180
|
|
|
|
|
|
|
} |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
# Check buffer size |
183
|
268
|
100
|
50
|
|
|
966
|
@$self{qw(state limit)} = ('finished', 1) if length($self->{multipart} // '') > $self->max_buffer_size; |
184
|
|
|
|
|
|
|
} |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
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 is a container for HTTP multipart content, based on L
|
205
|
|
|
|
|
|
|
7230|https://tools.ietf.org/html/rfc7230>, L and L
|
206
|
|
|
|
|
|
|
2388|https://tools.ietf.org/html/rfc2388>. |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
=head1 EVENTS |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
L inherits all events from L 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 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 inherits all attributes from L 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 objects. |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
=head1 METHODS |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
L inherits all methods from L 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 header. |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
=head2 clone |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
my $clone = $multi->clone; |
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
Return a new L object cloned from this content if possible, otherwise return C. |
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 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 object and subscribe to event L with default content |
284
|
|
|
|
|
|
|
parser. |
285
|
|
|
|
|
|
|
|
286
|
|
|
|
|
|
|
=head1 SEE ALSO |
287
|
|
|
|
|
|
|
|
288
|
|
|
|
|
|
|
L, L, L. |
289
|
|
|
|
|
|
|
|
290
|
|
|
|
|
|
|
=cut |