Body.pm

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