Body.pm

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