Body.pm

filter: Statementstmt 97.7% 173 / 177 Branchbran 89.2% 91 / 102 Conditioncond 77.4% 24 / 31 MC/DCmcdc 64.7% 11 / 17 Subroutinesub 91.6% 22 / 24 Podpod 100% 17 / 17 totaltotal 91.8% 338 / 368 CC 68 SCAR 24.9
CC
68
Cov
93%
add
28.1
DESTROY
9.2
new
8.4
CRAP 12.0
↑ Summary Body/MultiPart.pm »
1 package HTTP::Body;
2 25 $HTTP::Body::VERSION = '1.25';
3 25 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 25 ▶use Carp qw[ ];
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 25 our $TYPES = {
8 'application/octet-stream' => 'HTTP::Body::OctetStream',
9 'application/x-www-form-urlencoded' => 'HTTP::Body::UrlEncoded',
10 'multipart/form-data' => 'HTTP::Body::MultiPart',
11 'multipart/related' => 'HTTP::Body::XFormsMultipart',
12 'application/xml' => 'HTTP::Body::XForms',
13 'application/json' => 'HTTP::Body::OctetStream',
14 };
15  
16 25 require HTTP::Body::OctetStream;
17 25 require HTTP::Body::UrlEncoded;
18 25 require HTTP::Body::MultiPart;
19 25 require HTTP::Body::XFormsMultipart;
20 25 require HTTP::Body::XForms;
21  
22 25 ▶use HTTP::Headers;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 25 ▶use HTTP::Message;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 =head1 NAME
26  
27 HTTP::Body - HTTP Body Parser
28  
29 =head1 SYNOPSIS
30  
31 use HTTP::Body;
32  
33 sub handler : method {
34 my ( $class, $r ) = @_;
35  
36 my $content_type = $r->headers_in->get('Content-Type');
37 my $content_length = $r->headers_in->get('Content-Length');
38  
39 my $body = HTTP::Body->new( $content_type, $content_length );
40 my $length = $content_length;
41  
42 while ( $length ) {
43  
44 $r->read( my $buffer, ( $length < 8192 ) ? $length : 8192 );
45  
46 $length -= length($buffer);
47  
48 $body->add($buffer);
49 }
50  
51 my $uploads = $body->upload; # hashref
52 my $params = $body->param; # hashref
53 my $param_order = $body->param_order; # arrayref
54 my $raw = $body->body; # IO::Handle
55 }
56  
57 =head1 DESCRIPTION
58  
59 HTTP::Body parses chunks of HTTP POST data and supports
60 application/octet-stream, application/json, application/x-www-form-urlencoded,
61 multipart/form-data, and XForms submissions (multipart/related and
62 application/xml). A body of any other content type is handled like
63 application/octet-stream.
64  
65 Chunked bodies are supported by not passing a length value to new().
66  
67 It is currently used by L<Catalyst>, L<Dancer>, L<Maypole>, L<Web::Simple> and
68 L<Jedi>.
69  
70 =head1 NOTES
71  
72 When parsing multipart bodies, temporary files are created to store any
73 uploaded files. You must delete these temporary files yourself after
74 processing them, or set $body->cleanup(1) to automatically delete them at
75 DESTROY-time. The temporary file of a file part that is still incomplete
76 at DESTROY-time is never returned by upload, so it is always deleted.
77  
78 =head2 Limits
79  
80 To stop hostile input from exhausting memory or CPU, add() enforces a few
81 limits and croaks when one is exceeded. Each is a package variable you can
82 raise to suit your forms, or set to 0 (or undef) to disable.
83  
84 =over 4
85  
86 =item $HTTP::Body::MultiPart::max_parts
87  
88 =item $HTTP::Body::UrlEncoded::max_params
89  
90 The largest number of parts in a multipart body, and of fields in a urlencoded
91 body (counted as C<&> and C<;> separators, empty fields included); more make
92 add croak. The default for both is 1000.
93  
94 =item $HTTP::Body::MultiPart::max_header_size
95  
96 =item $HTTP::Body::MultiPart::max_header_lines
97  
98 The largest header block a multipart part may have, in bytes and in lines; a
99 larger one makes add croak. The defaults are 32768 and 100.
100  
101 =item $HTTP::Body::max_chunk_line_size
102  
103 The largest chunk-size line (size, extensions and CRLF) of a chunked body, in
104 bytes; a longer one makes add croak. The default is 1024. A chunked body
105 whose framing is malformed (a line that is no chunk-size line, a size that does
106 not fit an integer, chunk data not followed by CRLF) makes add croak as well.
107  
108 =item $HTTP::Body::max_trailer_size
109  
110 =item $HTTP::Body::max_trailer_lines
111  
112 The largest trailer of a chunked body (its header lines and the blank line
113 that ends it), in bytes, and the most header lines it may have; a larger one
114 makes add croak. The defaults are 32768 and 100.
115  
116 =back
117  
118 =head1 METHODS
119  
120 =over 4
121  
122 =item new
123  
124 Constructor. Takes content type and content length as parameters,
125 returns a L<HTTP::Body> object.
126  
127 =cut
128  
129 84 ▶sub new {
Subroutine new: called CC=8 SCAR=21.2
Pod documented
130 84 my ( $class, $content_type, $content_length ) = @_;
131  
132 84 ▶ unless ( @_ >= 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_ >= 2) 0 84 84
133 0 Carp::croak( $class, '->new( $content_type, [ $content_length ] )' );
134 }
135  
136 84 my $type;
137 my $earliest_index;
138 84 foreach my $supported ( keys %{$TYPES} ) {
139 504 my $index = index( lc($content_type), $supported );
140 504 ▶ if ($index >= 0 && (!defined $earliest_index || $index < $earliest_index)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index >= 0 and !defined($earliest_index) || $index < $earliest_index) 82 422 504
Condition50%3/6
$index >= 0 and !defined($earliest_index) || $index < $earliest_index
!ll&&!rl&&r
422082
not defined $earliest_index or $index < $earliest_index
l!l&&r!l&&!r
8200
MC/DC33%1/3
$index >= 0 and !defined($earliest_index) || $index < $earliest_index
$index >= 0 not defined $earliest_index $index < $earliest_index
Truth table50%2/4
A and not(B) or C
A= $index >= 0B= not defined $earliest_indexC= $index < $earliest_index
ABCresult
0XX0
1000
11X1
1011
141 82 $type = $supported;
142 82 $earliest_index = $index;
143 }
144 }
145  
146 84 ▶ my $body = $TYPES->{ $type || 'application/octet-stream' };
Condition100%2/2
$type || 'application/octet-stream'
l!l
822
MC/DC100%1/1
$type || 'application/octet-stream'
$type
Truth table100%2/2
A
A= $type
Aresult
11
00
147  
148 84 ▶ my $self = {
Branch: 1 branch
BranchTrueFalseTotal
defined $content_length ? : 57 27 84
149 cleanup => 0,
150 buffer => '',
151 chunk_buffer => '',
152 chunk_remaining => 0,
153 chunk_crlf => 0,
154 chunk_eof => 0,
155 chunk_trailer => undef,
156 chunk_trailer_lines => 0,
157 body => undef,
158 chunked => !defined $content_length,
159 content_length => defined $content_length ? $content_length : -1,
160 content_type => $content_type,
161 length => 0,
162 param => {},
163 param_order => [],
164 state => 'buffering',
165 upload => {},
166 part_data => {},
167 owner => _owner(),
168 tmpdir => File::Spec->tmpdir(),
169 };
170  
171 84 bless( $self, $body );
172  
173 84 return $self->init;
174 }
175  
176 # A forked child or a new thread gets a copy of the object, not of the upload.
177 168 ▶sub _owner {
Subroutine _owner: called CC=2 SCAR=7.6
178 168 ▶ return $$ . ' ' . ( $INC{'threads.pm'} ? threads->tid : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$INC{'threads.pm'} ? : 0 168 168
179 }
180  
181 84 ▶sub DESTROY {
Subroutine DESTROY: called CC=9 SCAR=22.1
182 84 my $self = shift;
183  
184 # An unfinished file part is not in upload(); at global destruction its fh may be gone.
185 84 ▶ if ( $self->{owner} eq _owner() && $self->{part} && exists $self->{part}->{fh} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"owner"} eq _owner() and $self->{"part"} and exists $self->{"part"}{"fh"}) 1 83 84
Condition83%5/6
$self->{"owner"} eq _owner() and $self->{"part"} and exists $self->{"part"}{"fh"}
!ll&&!rl&&r
39441
$self->{"owner"} eq _owner() and $self->{"part"}
!ll&&!rl&&r
03945
MC/DC66%2/3
$self->{"owner"} eq _owner() and $self->{"part"} and exists $self->{"part"}{"fh"}
$self->{"owner"} eq _owner() $self->{"part"} exists $self->{"part"}{"fh"}
Truth table75%3/4
A and B and C
A= $self->{"owner"} eq _owner()B= $self->{"part"}C= exists $self->{"part"}{"fh"}
ABCresult
0XX0
10X0
1100
1111
186 1 my $fh = delete $self->{part}->{fh};
187 1 ▶ close $fh if $fh;
Branch: 1 branch
BranchTrueFalseTotal
if $fh 1 0 1
188 1 unlink $self->{part}->{tempname};
189 }
190  
191 84 ▶ if ( $self->{cleanup} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"cleanup"}) 21 63 84
192 21 my @temps = ();
193 21 for my $upload ( values %{ $self->{upload} } ) {
194 52 ▶ push @temps, map { $_->{tempname} || () }
Branch: 1 branch
BranchTrueFalseTotal
ref $upload eq 'ARRAY' ? : 6 40 46
Condition33%1/3
$_->{'tempname'} || ()
l!l&&r!l&&!r
5200
MC/DC0%0/2
$_->{'tempname'} || ()
$_->{'tempname'} ()
Truth table33%1/3
A || B
A= $_->{'tempname'}B= ()
ABresult
1X1
011
000
195 46 ( ref $upload eq 'ARRAY' ? @{$upload} : $upload );
196 }
197
198 21 unlink map { $_ } grep { -e $_ } @temps;
199 }
200 }
201  
202 =item add
203  
204 Feeds the next piece of the body to the parser, which parses as much of it as
205 it can unless it is done already. Data beyond the content length is ignored.
206  
207 With a content length, add returns the number of bytes still expected as a
208 negative number, and 0 once the body is complete. For a chunked body it
209 returns nothing.
210  
211 add croaks when one of the limits under L</Limits> is exceeded, and when the
212 framing of a chunked body is malformed.
213  
214 =cut
215  
216 # chunk-size [ chunk-ext ] CRLF, matched against one line at a time. The
217 # atomic group keeps the size digits from backtracking into the rest of the
218 # line, and the line length limit bounds what the rest can cost.
219 25 my $chunk_size_line = qr/^(?>([\da-fA-F]+))[^\n]*\x0D\x0A\z/;
220  
221 25 our $max_chunk_line_size = 1024;
222 25 our $max_trailer_size = 32 * 1024;
223 25 our $max_trailer_lines = 100;
224  
225 1637 ▶sub add {
Subroutine add: called CC=28 SCAR=33.4
Pod documented
226 1637 my $self = shift;
227  
228 1637 ▶ if ( $self->{chunked} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"chunked"}) 487 1150 1637
229  
230 # Nothing after the blank line that ends the trailer belongs to the
231 # body: it must neither be reparsed nor pile up in the buffer.
232 487 ▶ return if $self->{chunk_eof};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"chunk_eof"} 2 485 487
233  
234 485 ▶ $self->{chunk_buffer} .= $_[0] if defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] 485 0 485
235  
236 485 ▶ while ( !$self->{chunk_eof} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"chunk_eof"}) 1067 0 1067
237  
238 # Inside a chunk: pass its data on as it arrives, so that a chunk
239 # never needs as much memory as it is long.
240 1067 ▶ if ( $self->{chunk_remaining} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"chunk_remaining"}) 106 961 1067
241 106 my $take = length $self->{chunk_buffer};
242 106 ▶ $take = $self->{chunk_remaining} if $take > $self->{chunk_remaining};
Branch: 1 branch
BranchTrueFalseTotal
if $take > $self->{"chunk_remaining"} 46 60 106
243  
244 # wait for more calls to add()
245 106 ▶ return unless $take;
Branch: 1 branch
BranchTrueFalseTotal
unless $take 31 75 106
246  
247 75 my $data = substr( $self->{chunk_buffer}, 0, $take, '' );
248 75 $self->{chunk_remaining} -= $take;
249 75 $self->{length} += $take;
250  
251 # Once the body parser is done (multipart is, at its close
252 # delimiter) the rest of the body is consumed but not kept.
253 75 ▶ unless ( $self->{state} eq 'done' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"state"} eq "done") 65 10 75
254 65 $self->{buffer} .= $data;
255 65 $self->spin;
256 }
257  
258 75 next;
259 }
260  
261 # The CRLF that ends the data of the chunk just read.
262 961 ▶ if ( $self->{chunk_crlf} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"chunk_crlf"}) 55 906 961
263 55 ▶ return if length( $self->{chunk_buffer} ) < 2;
Branch: 1 branch
BranchTrueFalseTotal
if CORE::length $self->{"chunk_buffer"} < 2 6 49 55
264  
265 49 ▶ unless ( substr( $self->{chunk_buffer}, 0, 2, '' ) eq "\x0D\x0A" ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (substr($self->{"chunk_buffer"}, 0, 2, "") eq "\r\n") 1 48 49
266 1 Carp::croak('Chunk data not followed by CRLF');
267 }
268  
269 48 $self->{chunk_crlf} = 0;
270 }
271  
272 # After the last chunk: the trailer, header lines up to a blank
273 # line that ends the body. It is collected until then.
274 954 ▶ if ( defined $self->{chunk_trailer} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"chunk_trailer"}) 855 99 954
275 my $line = $self->_chunk_line(
276 $max_trailer_size ? $max_trailer_size - length $self->{chunk_trailer} : undef,
277 855 ▶ "Chunked body trailer exceeds $max_trailer_size bytes",
Branch: 1 branch
BranchTrueFalseTotal
$HTTP::Body::max_trailer_size ? : 453 402 855
278 );
279  
280 # wait for the rest of the line
281 853 ▶ return unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 402 451 853
282  
283 451 ▶ if ( $line =~ /^\x0D?\x0A\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\x0D?\x0A\z/) 11 440 451
284 11 ▶ if ( my $message = HTTP::Message->parse( $self->{chunk_trailer} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $message = "HTTP::Message"->parse($self->{"chunk_trailer"})) 11 0 11
285 11 $self->{trailing_headers} = $message->headers;
286 }
287  
288 11 $self->{chunk_buffer} = '';
289 11 $self->{chunk_trailer} = undef;
290 11 $self->{chunk_eof} = 1;
291  
292 11 return;
293 }
294  
295 # HTTP::Headers takes time quadratic in the number of fields
296 # of one name, so the number of lines is limited as well.
297 440 ▶ if ( $max_trailer_lines && ++$self->{chunk_trailer_lines} > $max_trailer_lines ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($HTTP::Body::max_trailer_lines and ++$self->{"chunk_trailer_lines"} > $HTTP::Body::max_trailer_lines) 1 439 440
Condition100%3/3
$HTTP::Body::max_trailer_lines and ++$self->{"chunk_trailer_lines"} > $HTTP::Body::max_trailer_lines
!ll&&!rl&&r
2002391
MC/DC100%2/2
$HTTP::Body::max_trailer_lines and ++$self->{"chunk_trailer_lines"} > $HTTP::Body::max_trailer_lines
$HTTP::Body::max_trailer_lines ++$self->{"chunk_trailer_lines"} > $HTTP::Body::max_trailer_lines
Truth table100%3/3
A and B
A= $HTTP::Body::max_trailer_linesB= ++$self->{"chunk_trailer_lines"} > $HTTP::Body::max_trailer_lines
ABresult
0X0
100
111
298 1 Carp::croak("Chunked body trailer exceeds $max_trailer_lines lines");
299 }
300  
301 439 $self->{chunk_trailer} .= $line;
302  
303 # trailing_headers parses the trailer again when asked
304 439 delete $self->{trailing_headers};
305  
306 439 next;
307 }
308  
309 # A chunk-size line.
310 99 ▶ my $line = $self->_chunk_line(
Condition100%2/2
$HTTP::Body::max_chunk_line_size || undef
l!l
972
MC/DC100%1/1
$HTTP::Body::max_chunk_line_size || undef
$HTTP::Body::max_chunk_line_size
Truth table100%2/2
A
A= $HTTP::Body::max_chunk_line_size
Aresult
11
00
311 $max_chunk_line_size || undef,
312 "Chunk size line exceeds $max_chunk_line_size bytes",
313 );
314  
315 # wait for the rest of the line
316 97 ▶ return unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 26 71 97
317  
318 71 ▶ unless ( $line =~ $chunk_size_line ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($line =~ /$chunk_size_line/) 2 69 71
319 2 Carp::croak('Invalid chunk size line');
320 }
321  
322 # hex() of a size that does not fit an integer warns and returns a
323 # float, which no buffer length would ever reach.
324 69 ( my $digits = $1 ) =~ s/^0+//;
325 69 ▶ if ( length($digits) > 15 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (CORE::length $digits > 15) 1 68 69
326 1 Carp::croak('Chunk size too large');
327 }
328  
329 68 my $chunk_len = hex($digits);
330  
331 68 ▶ if ( $chunk_len == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chunk_len == 0) 18 50 68
332 # End of data: set content_length equal to the amount of data
333 # we read, so the spin methods can finish up.
334 18 $self->{content_length} = $self->{length};
335  
336 18 ▶ unless ( $self->{state} eq 'done' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"state"} eq "done") 17 1 18
337 17 $self->spin;
338 }
339  
340 # The trailer follows, up to a blank line.
341 18 $self->{chunk_trailer} = '';
342  
343 18 next;
344 }
345  
346 50 $self->{chunk_remaining} = $chunk_len;
347 50 $self->{chunk_crlf} = 1;
348 }
349  
350 0 return;
351 }
352
353 1150 my $cl = $self->content_length;
354  
355 1150 ▶ if ( defined $_[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) 1150 0 1150
356 # A copy: the caller's buffer is left alone, and may be read-only.
357 1150 my $data = $_[0];
358 1150 $self->{length} += length($data);
359  
360 # Don't allow buffer data to exceed content-length
361 1150 ▶ if ( $self->{length} > $cl ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"length"} > $cl) 8 1142 1150
362 8 $data = substr $data, 0, $cl - $self->{length};
363 8 $self->{length} = $cl;
364 }
365  
366 1150 $self->{buffer} .= $data;
367 }
368  
369 1150 ▶ unless ( $self->state eq 'done' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->state eq "done") 1150 0 1150
370 1150 $self->spin;
371 }
372  
373 1143 return ( $self->length - $cl );
374 }
375  
376 # Takes the next line, LF included, off the chunk buffer, or returns nothing
377 # while it is incomplete. Its length is checked before it is complete, so
378 # that an endless line can not grow the buffer: longer than $room bytes, it
379 # croaks with $error. An undefined $room is no limit.
380 954 ▶sub _chunk_line {
Subroutine _chunk_line: called CC=5 SCAR=16.1
381 954 my ( $self, $room, $error ) = @_;
382  
383 954 my $eol = index( $self->{chunk_buffer}, "\x0A" );
384 954 ▶ my $line_len = $eol >= 0 ? $eol + 1 : length $self->{chunk_buffer};
Branch: 1 branch
BranchTrueFalseTotal
$eol >= 0 ? : 524 430 954
385  
386 954 ▶ Carp::croak($error) if defined $room && $line_len > $room;
Branch: 1 branch
BranchTrueFalseTotal
if defined $room and $line_len > $room 4 950 954
Condition100%3/3
defined $room and $line_len > $room
!ll&&!rl&&r
4045464
MC/DC100%2/2
defined $room and $line_len > $room
defined $room $line_len > $room
Truth table100%3/3
A and B
A= defined $roomB= $line_len > $room
ABresult
0X0
100
111
387  
388 950 ▶ return if $eol < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $eol < 0 428 522 950
389  
390 522 return substr( $self->{chunk_buffer}, 0, $line_len, '' );
391 }
392  
393 =item body
394  
395 accessor for the body.
396  
397 =cut
398  
399 80 ▶sub body {
Subroutine body: called CC=2 SCAR=6.9
Pod documented
400 80 my $self = shift;
401 80 ▶ $self->{body} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 9 71 80
402 80 return $self->{body};
403 }
404  
405 =item chunked
406  
407 Returns 1 if the request is chunked.
408  
409 =cut
410  
411 0 ▶sub chunked {
Subroutine chunked: not called CC=1 SCAR=6.9
Pod documented
412 0 return shift->{chunked};
413 }
414  
415 =item cleanup
416  
417 Set to 1 to enable automatic deletion of temporary files at DESTROY-time.
418  
419 =cut
420  
421 21 ▶sub cleanup {
Subroutine cleanup: called CC=2 SCAR=7.1
Pod documented
422 21 my $self = shift;
423 21 ▶ $self->{cleanup} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 21 0 21
424 21 return $self->{cleanup};
425 }
426  
427 =item content_length
428  
429 Returns the content-length for the body data if known.
430 Returns -1 if the request is chunked.
431  
432 =cut
433  
434 1261 ▶sub content_length {
Subroutine content_length: called CC=1 SCAR=0.0
Pod documented
435 1261 return shift->{content_length};
436 }
437  
438 =item content_type
439  
440 Returns the content-type of the body data.
441  
442 =cut
443  
444 47 ▶sub content_type {
Subroutine content_type: called CC=1 SCAR=0.0
Pod documented
445 47 return shift->{content_type};
446 }
447  
448 =item init
449  
450 return self.
451  
452 =cut
453  
454 40 ▶sub init {
Subroutine init: called CC=1 SCAR=0.0
Pod documented
455 40 return $_[0];
456 }
457  
458 =item length
459  
460 Returns the number of bytes of the body read so far, up to the content length.
461 Of a chunked body only the chunk data counts.
462  
463 =cut
464  
465 1257 ▶sub length {
Subroutine length: called CC=1 SCAR=0.0
Pod documented
466 1257 return shift->{length};
467 }
468  
469 =item trailing_headers
470  
471 If a chunked request body had trailing headers, trailing_headers will
472 return an HTTP::Headers object populated with those headers. The trailer is
473 read up to the blank line that ends it, also when it arrives in later calls to
474 add than the last, zero-size chunk; until then, trailing_headers returns the
475 header lines that have arrived so far. The body itself is done at the last
476 chunk.
477  
478 =cut
479  
480 14 ▶sub trailing_headers {
Subroutine trailing_headers: called CC=5 SCAR=16.2
Pod documented
481 14 my $self = shift;
482  
483 # A trailer still being read gives the lines that have arrived of it, as
484 # some clients never send its blank line. It is parsed when asked for,
485 # not again for every line that arrives.
486 14 ▶ if ( !$self->{trailing_headers}
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"trailing_headers"} and defined $self->{"chunk_trailer"} and CORE::length $self->{"chunk_trailer"}) 3 11 14
Condition83%5/6
not $self->{"trailing_headers"} and defined $self->{"chunk_trailer"} and CORE::length $self->{"chunk_trailer"}
!ll&&!rl&&r
1013
not $self->{"trailing_headers"} and defined $self->{"chunk_trailer"}
!ll&&!rl&&r
1004
MC/DC66%2/3
not $self->{"trailing_headers"} and defined $self->{"chunk_trailer"} and CORE::length $self->{"chunk_trailer"}
not $self->{"trailing_headers"} defined $self->{"chunk_trailer"} CORE::length $self->{"chunk_trailer"}
Truth table75%3/4
not(A) and B and C
A= not $self->{"trailing_headers"}B= defined $self->{"chunk_trailer"}C= CORE::length $self->{"chunk_trailer"}
ABCresult
0XX0
10X0
1100
1111
487 && defined $self->{chunk_trailer}
488 && length $self->{chunk_trailer} )
489 {
490 3 ▶ if ( my $message = HTTP::Message->parse( $self->{chunk_trailer} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $message = "HTTP::Message"->parse($self->{"chunk_trailer"})) 3 0 3
491 3 $self->{trailing_headers} = $message->headers;
492 }
493 }
494  
495 14 return $self->{trailing_headers};
496 }
497  
498 =item spin
499  
500 Abstract method to spin the io handle.
501  
502 =cut
503  
504 0 ▶sub spin {
Subroutine spin: not called CC=1 SCAR=6.9
Pod documented
505 0 Carp::croak('Define abstract method spin() in implementation');
506 }
507  
508 =item state
509  
510 Returns the current state of the parser.
511  
512 =cut
513  
514 1186 ▶sub state {
Subroutine state: called CC=2 SCAR=6.9
Pod documented
515 1186 my $self = shift;
516 1186 ▶ $self->{state} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 3 1183 1186
517 1186 return $self->{state};
518 }
519  
520 =item param
521  
522 Get/set body parameters.
523  
524 =cut
525  
526 2175 ▶sub param {
Subroutine param: called CC=5 SCAR=16.1
Pod documented
527 2175 my $self = shift;
528  
529 2175 ▶ if ( @_ == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) 2138 37 2175
530  
531 2138 my ( $name, $value ) = @_;
532  
533 2138 ▶ if ( exists $self->{param}->{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'param'}{$name}) { } 1018 1120 2138
534 1018 for ( $self->{param}->{$name} ) {
535 1018 ▶ $_ = [$_] unless ref($_) eq "ARRAY";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_ eq "ARRAY" 20 998 1018
536 1018 push( @$_, $value );
537 }
538 }
539 else {
540 1120 $self->{param}->{$name} = $value;
541 }
542  
543 2138 push @{$self->{param_order}}, $name;
544 }
545  
546 2175 return $self->{param};
547 }
548  
549 =item upload
550  
551 Get/set file uploads.
552  
553 =cut
554  
555 167 ▶sub upload {
Subroutine upload: called CC=5 SCAR=16.1
Pod documented
556 167 my $self = shift;
557  
558 167 ▶ if ( @_ == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) 85 82 167
559  
560 85 my ( $name, $upload ) = @_;
561  
562 85 ▶ if ( exists $self->{upload}->{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'upload'}{$name}) { } 15 70 85
563 15 for ( $self->{upload}->{$name} ) {
564 15 ▶ $_ = [$_] unless ref($_) eq "ARRAY";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_ eq "ARRAY" 15 0 15
565 15 push( @$_, $upload );
566 }
567 }
568 else {
569 70 $self->{upload}->{$name} = $upload;
570 }
571 }
572  
573 167 return $self->{upload};
574 }
575  
576 =item part_data
577  
578 Just like 'param' but gives you a hash of the full data associated with the
579 part in a multipart type POST/PUT. Example:
580  
581 {
582 data => "test",
583 done => 1,
584 headers => {
585 "Content-Disposition" => "form-data; name=\"arg2\"",
586 "Content-Type" => "text/plain"
587 },
588 name => "arg2",
589 size => 4
590 }
591  
592 =cut
593  
594 1095 ▶sub part_data {
Subroutine part_data: called CC=5 SCAR=16.1
Pod documented
595 1095 my $self = shift;
596  
597 1095 ▶ if ( @_ == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) 1094 1 1095
598  
599 1094 my ( $name, $data ) = @_;
600  
601 1094 ▶ if ( exists $self->{part_data}->{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'part_data'}{$name}) { } 1016 78 1094
602 1016 for ( $self->{part_data}->{$name} ) {
603 1016 ▶ $_ = [$_] unless ref($_) eq "ARRAY";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_ eq "ARRAY" 18 998 1016
604 1016 push( @$_, $data );
605 }
606 }
607 else {
608 78 $self->{part_data}->{$name} = $data;
609 }
610 }
611  
612 1095 return $self->{part_data};
613 }
614  
615 =item tmpdir
616  
617 Specify a different path for temporary files. Defaults to the system temporary path.
618  
619 =cut
620  
621 123 ▶sub tmpdir {
Subroutine tmpdir: called CC=2 SCAR=6.9
Pod documented
622 123 my $self = shift;
623 123 ▶ $self->{tmpdir} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 32 91 123
624 123 return $self->{tmpdir};
625 }
626  
627 =item param_order
628  
629 Returns the array ref of the param keys in the order how they appeared on the body
630  
631 =cut
632  
633 21 ▶sub param_order {
Subroutine param_order: called CC=1 SCAR=0.0
Pod documented
634 21 return shift->{param_order};
635 }
636  
637 =back
638  
639 =head1 CONFIGURING UPLOAD TEMPORARY FILES
640  
641 By default, the temporary file that holds an uploaded file is given the
642 extension C<.upload> instead of the extension of the uploaded file itself.
643 The following package variables control how these temporary files are created.
644  
645 =over 4
646  
647 =item $HTTP::Body::MultiPart::file_temp_suffix
648  
649 The extension given to every multipart upload file; the default is C<.upload>.
650 Set it to undef to keep the extension of the uploaded file instead, as releases
651 before 1.23 did.
652  
653 =item $HTTP::Body::MultiPart::basename_regexp
654  
655 The regexp used to work out the uploaded file's extension. It only matters
656 when C<file_temp_suffix> is undefined, and is matched against the last 255
657 bytes of the uploaded file's basename, which is as long as a suffix can be.
658 The default, C<qr/[^.]+(\.[^\\\/]+)$/>, is not run as a regexp: as long as the
659 variable still holds it, a string search with the same result is used.
660  
661 =item $HTTP::Body::MultiPart::file_temp_template
662  
663 Passed through to the L<File::Temp> TEMPLATE parameter. There is no special
664 default in our module.
665  
666 =item %HTTP::Body::MultiPart::file_temp_parameters
667  
668 Extra settings for the L<File::Temp> invocation. These override every other
669 setting.
670  
671 =back
672  
673 =head1 SUPPORT
674  
675 =over 4
676  
677 =item * GitHub Issues
678  
679 L<https://github.com/libwww-perl/HTTP-Body/issues>
680  
681 =item * MetaCPAN
682  
683 L<https://metacpan.org/release/HTTP-Body>
684  
685 =item * Source Code
686  
687 L<https://github.com/libwww-perl/HTTP-Body>
688  
689 =item * IRC
690  
691 C<#catalyst> on C<irc.perl.org> for users, C<#catalyst-dev> for contributors
692  
693 =back
694  
695 Note: This project was formerly hosted on the Shadowcat Systems git server.
696 The repository has moved to L<GitHub|https://github.com/libwww-perl/HTTP-Body>.
697  
698 =head1 AUTHOR
699  
700 Christian Hansen, C<christian.hansen@mac.com>
701  
702 Sebastian Riedel, C<kraihx@googlemail.com>
703  
704 Andy Grundman, C<andy@hybridized.org>
705  
706 =head1 MAINTAINER
707  
708 Torsten Raudssus, C<torsten@raudssus.de>
709  
710 =head1 CONTRIBUTORS
711  
712 Simon Elliott C<cpan@papercreatures.com>
713  
714 Kent Fredric C<kentnl@cpan.org>
715  
716 Christian Walde C<walde.christian@gmail.com>
717  
718 =head1 LICENSE
719  
720 This library is free software. You can redistribute it and/or modify
721 it under the same terms as perl itself.
722  
723 =cut
724  
725 25 1;
↑ Summary Body/MultiPart.pm »