| 1 | package HTTP::Body::MultiPart; | ||||||||||||||||||||||||||||
| 2 | 25 | $HTTP::Body::MultiPart::VERSION = '1.25'; | |||||||||||||||||||||||||||
| 3 | 25 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 25 | ▶ | use base 'HTTP::Body'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 25 | ▶ | use bytes; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 25 | ▶ | use IO::File; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 25 | ▶ | use File::Temp 0.14; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 25 | ▶ | use File::Spec; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | =head1 NAME | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | HTTP::Body::MultiPart - HTTP Body Multipart Parser | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | use HTTP::Body::MultiPart; | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | HTTP Body Multipart Parser for multipart/form-data bodies. | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | A part with a non-empty filename in its Content-Disposition is an upload: its | ||||||||||||||||||||||||||||
| 24 | data is written to a temporary file as it arrives, and the part is stored with | ||||||||||||||||||||||||||||
| 25 | L<HTTP::Body/upload> once it is complete. Every other part is stored with | ||||||||||||||||||||||||||||
| 26 | L<HTTP::Body/param> and L<HTTP::Body/part_data>, except a part with an empty | ||||||||||||||||||||||||||||
| 27 | filename (a file field left empty), which is dropped. | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | See L<HTTP::Body/Limits> for the limits on parts and their headers, and | ||||||||||||||||||||||||||||
| 30 | L<HTTP::Body/CONFIGURING UPLOAD TEMPORARY FILES> for the temporary files. | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | =head1 METHODS | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | =over 4 | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | =item init | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | Takes the boundary from the content type, croaking if it has none, and starts | ||||||||||||||||||||||||||||
| 39 | the parser in the preamble state. | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | =cut | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 45 | ▶ | sub init { | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||||||||||||||||
| 44 | 45 | my $self = shift; | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 45 | ▶ | unless ( $self->content_type =~ /boundary=\"?([^\";]+)\"?/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 0 | my $content_type = $self->content_type; | |||||||||||||||||||||||||||
| 48 | 0 | Carp::croak("Invalid boundary in content_type: '$content_type'"); | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 45 | $self->{boundary} = $1; | |||||||||||||||||||||||||||
| 52 | 45 | $self->{state} = 'preamble'; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 45 | return $self; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | =item spin | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | Runs the parse method of the current state (parse_preamble, parse_boundary, | ||||||||||||||||||||||||||||
| 60 | parse_header or parse_body) until one of them needs more data or the body is | ||||||||||||||||||||||||||||
| 61 | done. | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | =cut | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 1142 | ▶ | sub spin { | ||||||||||||||||||||||||||
|
Subroutine
spin: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||||||||
| 66 | 1142 | my $self = shift; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 1142 | while (1) { | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 4777 | ▶ | if ( $self->{state} =~ /^(preamble|boundary|header|body)$/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 4777 | my $method = "parse_$1"; | |||||||||||||||||||||||||||
| 72 | 4777 | ▶ | return unless $self->$method; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | else { | ||||||||||||||||||||||||||||
| 76 | 0 | Carp::croak('Unknown state'); | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | =item boundary | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | The boundary parameter of the content type. | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | =cut | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 5839 | ▶ | sub boundary { | ||||||||||||||||||||||||||
|
Subroutine
boundary: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 88 | 5839 | return shift->{boundary}; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | =item boundary_begin | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | C<--> followed by the boundary. | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | =cut | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 5839 | ▶ | sub boundary_begin { | ||||||||||||||||||||||||||
|
Subroutine
boundary_begin: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 98 | 5839 | return "--" . shift->boundary; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | =item boundary_end | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | boundary_begin followed by C<-->. | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | =cut | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 119 | ▶ | sub boundary_end { | ||||||||||||||||||||||||||
|
Subroutine
boundary_end: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 108 | 119 | return shift->boundary_begin . "--"; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | =item crlf | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | The line ending of a multipart body, C<"\x0D\x0A">. | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | =cut | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 6383 | ▶ | sub crlf () { | ||||||||||||||||||||||||||
|
Subroutine
crlf: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 118 | 6383 | return "\x0d\x0a"; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | =item delimiter_begin | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | crlf followed by boundary_begin: the delimiter that ends the data of a part. | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | =cut | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 3663 | ▶ | sub delimiter_begin { | ||||||||||||||||||||||||||
|
Subroutine
delimiter_begin: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 128 | 3663 | my $self = shift; | |||||||||||||||||||||||||||
| 129 | 3663 | return $self->crlf . $self->boundary_begin; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | =item delimiter_end | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | crlf followed by boundary_end: the close delimiter that ends the body. | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | =cut | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 119 | ▶ | sub delimiter_end { | ||||||||||||||||||||||||||
|
Subroutine
delimiter_end: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 139 | 119 | my $self = shift; | |||||||||||||||||||||||||||
| 140 | 119 | return $self->crlf . $self->boundary_end; | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | =item parse_preamble | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | Drops the preamble before the first boundary and moves on to the boundary | ||||||||||||||||||||||||||||
| 146 | state. Until the boundary is found, only as many bytes are kept as could be | ||||||||||||||||||||||||||||
| 147 | its start. | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | =cut | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 1051 | ▶ | sub parse_preamble { | ||||||||||||||||||||||||||
|
Subroutine
parse_preamble: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 152 | 1051 | my $self = shift; | |||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 1051 | my $index = index( $self->{buffer}, $self->boundary_begin ); | |||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 1051 | ▶ | unless ( $index >= 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | # keep only what may be the start of a dash-boundary split across chunks | ||||||||||||||||||||||||||||
| 158 | 1006 | my $keep = length( $self->boundary_begin ) - 1; | |||||||||||||||||||||||||||
| 159 | 1006 | ▶ | substr( $self->{buffer}, 0, -$keep, '' ) if length( $self->{buffer} ) > $keep; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | 1006 | return 0; | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | # replace preamble with CRLF so we can match dash-boundary as delimiter | ||||||||||||||||||||||||||||
| 164 | 45 | substr( $self->{buffer}, 0, $index, $self->crlf ); | |||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 45 | $self->{state} = 'boundary'; | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 45 | return 1; | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | =item parse_boundary | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | Reads a delimiter line. A delimiter starts the next part and the header | ||||||||||||||||||||||||||||
| 174 | state; a close delimiter ends the body, and whatever follows it (padding, an | ||||||||||||||||||||||||||||
| 175 | epilogue) is ignored. Croaks on a line that is neither, and when the body has | ||||||||||||||||||||||||||||
| 176 | more than C<$HTTP::Body::MultiPart::max_parts> parts. | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | =cut | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 25 | our $max_parts = 1000; | |||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 1247 | ▶ | sub parse_boundary { | ||||||||||||||||||||||||||
|
Subroutine
parse_boundary: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 183 | 1247 | my $self = shift; | |||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 1247 | ▶ | if ( index( $self->{buffer}, $self->delimiter_begin . $self->crlf ) == 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 1200 | ▶ | if ( $max_parts && ++$self->{part_count} > $max_parts ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$HTTP::Body::MultiPart::max_parts and ++$self->{"part_count"} > $HTTP::Body::MultiPart::max_parts
MC/DC50%1/2
$HTTP::Body::MultiPart::max_parts and ++$self->{"part_count"} > $HTTP::Body::MultiPart::max_parts
$HTTP::Body::MultiPart::max_parts
++$self->{"part_count"} > $HTTP::Body::MultiPart::max_parts
Truth table66%2/3
A and B
A= $HTTP::Body::MultiPart::max_partsB= ++$self->{"part_count"} > $HTTP::Body::MultiPart::max_parts
| |||||||||||||||||||||||||||||
| 188 | 1 | Carp::croak("Too many parts in multipart body (limit $max_parts)"); | |||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 1199 | substr( $self->{buffer}, 0, length( $self->delimiter_begin ) + 2, '' ); | |||||||||||||||||||||||||||
| 192 | 1199 | $self->{part} = {}; | |||||||||||||||||||||||||||
| 193 | 1199 | $self->{state} = 'header'; | |||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 1199 | return 1; | |||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 47 | ▶ | if ( index( $self->{buffer}, $self->delimiter_end . $self->crlf ) == 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 36 | substr( $self->{buffer}, 0, length( $self->delimiter_end ) + 2, '' ); | |||||||||||||||||||||||||||
| 201 | 36 | $self->{part} = {}; | |||||||||||||||||||||||||||
| 202 | 36 | $self->{state} = 'done'; | |||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 36 | return 0; | |||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | 11 | ▶ | if ( length( $self->{buffer} ) >= length( $self->delimiter_end ) + 2 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 208 | # a close delimiter ends the body whatever follows it (padding, epilogue) | ||||||||||||||||||||||||||||
| 209 | 2 | ▶ | unless ( index( $self->{buffer}, $self->delimiter_end ) == 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 210 | 1 | Carp::croak('Invalid multipart boundary delimiter'); | |||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||
| 212 | 1 | substr( $self->{buffer}, 0, length( $self->delimiter_end ), '' ); | |||||||||||||||||||||||||||
| 213 | 1 | $self->{part} = {}; | |||||||||||||||||||||||||||
| 214 | 1 | $self->{state} = 'done'; | |||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | 10 | return 0; | |||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | =item parse_header | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | Reads a part's header block, up to the blank line, into C<< $part->{headers} >>, | ||||||||||||||||||||||||||||
| 223 | keyed by field name with the first letter of each word upper-cased; a field | ||||||||||||||||||||||||||||
| 224 | that occurs more than once gets an array reference. A continuation line is | ||||||||||||||||||||||||||||
| 225 | joined to the line before it, and a line that is no header field is dropped. | ||||||||||||||||||||||||||||
| 226 | Croaks when the block exceeds C<$HTTP::Body::MultiPart::max_header_size> bytes | ||||||||||||||||||||||||||||
| 227 | or C<$HTTP::Body::MultiPart::max_header_lines> lines. | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | =cut | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | 25 | our $max_header_size = 32 * 1024; | |||||||||||||||||||||||||||
| 232 | 25 | our $max_header_lines = 100; | |||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | 1262 | ▶ | sub parse_header { | ||||||||||||||||||||||||||
|
Subroutine
parse_header: called CC=15 SCAR=27.8
Pod
documented
| |||||||||||||||||||||||||||||
| 235 | 1262 | my $self = shift; | |||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | 1262 | my $crlf = $self->crlf; | |||||||||||||||||||||||||||
| 238 | 1262 | my $index = index( $self->{buffer}, $crlf . $crlf ); | |||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | # Without the blank line yet, the last 3 bytes may be its start. | ||||||||||||||||||||||||||||
| 241 | 1262 | ▶ | my $size = $index >= 0 ? $index : length( $self->{buffer} ) - 3; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 242 | 1262 | ▶ | if ( $max_header_size && $size > $max_header_size ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$HTTP::Body::MultiPart::max_header_size and $size > $HTTP::Body::MultiPart::max_header_size
MC/DC50%1/2
$HTTP::Body::MultiPart::max_header_size and $size > $HTTP::Body::MultiPart::max_header_size
$HTTP::Body::MultiPart::max_header_size
$size > $HTTP::Body::MultiPart::max_header_size
Truth table66%2/3
A and B
A= $HTTP::Body::MultiPart::max_header_sizeB= $size > $HTTP::Body::MultiPart::max_header_size
| |||||||||||||||||||||||||||||
| 243 | 2 | Carp::croak("Multipart part header exceeds $max_header_size bytes"); | |||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | 1260 | ▶ | unless ( $index >= 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 247 | 63 | return 0; | |||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | 1197 | my $header = substr( $self->{buffer}, 0, $index ); | |||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | 1197 | ▶ | if ( $max_header_lines && ( $header =~ tr/\n// ) >= $max_header_lines ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$HTTP::Body::MultiPart::max_header_lines and $header =~ tr/\n// >= $HTTP::Body::MultiPart::max_header_lines
MC/DC50%1/2
$HTTP::Body::MultiPart::max_header_lines and $header =~ tr/\n// >= $HTTP::Body::MultiPart::max_header_lines
$HTTP::Body::MultiPart::max_header_lines
$header =~ tr/\n// >= $HTTP::Body::MultiPart::max_header_lines
Truth table66%2/3
A and B
A= $HTTP::Body::MultiPart::max_header_linesB= $header =~ tr/\n// >= $HTTP::Body::MultiPart::max_header_lines
| |||||||||||||||||||||||||||||
| 253 | 1 | Carp::croak("Multipart part header exceeds $max_header_lines lines"); | |||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | 1196 | substr( $self->{buffer}, 0, $index + 4, '' ); | |||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | 1196 | my @headers; | |||||||||||||||||||||||||||
| 259 | 1196 | for ( split /$crlf/, $header ) { | |||||||||||||||||||||||||||
| 260 | 1286 | ▶ | if (s/^[ \t]+//) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 261 | # a continuation line; with no line to continue it is dropped | ||||||||||||||||||||||||||||
| 262 | 1 | ▶ | $headers[-1] .= $_ if @headers; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||
| 264 | else { | ||||||||||||||||||||||||||||
| 265 | 1285 | push @headers, $_; | |||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | 1196 | my $token = qr/[^][\x00-\x1f\x7f()<>@,;:\\"\/?={} \t]+/; | |||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | 1196 | for my $header (@headers) { | |||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | # A line that is no header field is dropped: without a match $1 would | ||||||||||||||||||||||||||||
| 274 | # still hold whatever the last successful match captured. | ||||||||||||||||||||||||||||
| 275 | 1285 | ▶ | next unless $header =~ s/^($token):[\t ]*//; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | 1284 | ( my $field = $1 ) =~ s/\b(\w)/uc($1)/eg; | |||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | 1284 | ▶ | if ( exists $self->{part}->{headers}->{$field} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 280 | 0 | for ( $self->{part}->{headers}->{$field} ) { | |||||||||||||||||||||||||||
| 281 | 0 | ▶ | $_ = [$_] unless ref($_) eq "ARRAY"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 282 | 0 | push( @$_, $header ); | |||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||
| 284 | } | ||||||||||||||||||||||||||||
| 285 | else { | ||||||||||||||||||||||||||||
| 286 | 1284 | $self->{part}->{headers}->{$field} = $header; | |||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | 1196 | $self->{state} = 'body'; | |||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | 1196 | return 1; | |||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | =item parse_body | ||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | Passes a part's data on to handler as it arrives, holding back only as many | ||||||||||||||||||||||||||||
| 298 | bytes as could be the start of the next delimiter. At the delimiter it sets | ||||||||||||||||||||||||||||
| 299 | C<< $part->{done} >>, calls handler a last time and moves on to the boundary | ||||||||||||||||||||||||||||
| 300 | state. | ||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | =cut | ||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | 1217 | ▶ | sub parse_body { | ||||||||||||||||||||||||||
|
Subroutine
parse_body: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 305 | 1217 | my $self = shift; | |||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | 1217 | my $index = index( $self->{buffer}, $self->delimiter_begin ); | |||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | 1217 | ▶ | if ( $index < 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | # make sure we have enough buffer to detect end delimiter | ||||||||||||||||||||||||||||
| 312 | 22 | my $length = length( $self->{buffer} ) - ( length( $self->delimiter_end ) + 2 ); | |||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | 22 | ▶ | unless ( $length > 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 315 | 15 | return 0; | |||||||||||||||||||||||||||
| 316 | } | ||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | 7 | $self->{part}->{data} .= substr( $self->{buffer}, 0, $length, '' ); | |||||||||||||||||||||||||||
| 319 | 7 | $self->{part}->{size} += $length; | |||||||||||||||||||||||||||
| 320 | 7 | $self->{part}->{done} = 0; | |||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | 7 | $self->handler( $self->{part} ); | |||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||
| 324 | 7 | return 0; | |||||||||||||||||||||||||||
| 325 | } | ||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | 1195 | $self->{part}->{data} .= substr( $self->{buffer}, 0, $index, '' ); | |||||||||||||||||||||||||||
| 328 | 1195 | $self->{part}->{size} += $index; | |||||||||||||||||||||||||||
| 329 | 1195 | $self->{part}->{done} = 1; | |||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | 1195 | $self->handler( $self->{part} ); | |||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||
| 333 | 1195 | $self->{state} = 'boundary'; | |||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | 1195 | return 1; | |||||||||||||||||||||||||||
| 336 | } | ||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||
| 338 | =item handler | ||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||
| 340 | Called by parse_body with the part each time data arrives. On the first call | ||||||||||||||||||||||||||||
| 341 | it takes the part's name and filename from its Content-Disposition, unless a | ||||||||||||||||||||||||||||
| 342 | subclass has named the part already, and strips any NUL from the filename. A | ||||||||||||||||||||||||||||
| 343 | part with a non-empty filename gets a temporary file, and its data is written | ||||||||||||||||||||||||||||
| 344 | there as it arrives. Once the part is done it is stored as described under | ||||||||||||||||||||||||||||
| 345 | L</DESCRIPTION>. Croaks when the temporary file can not be written. | ||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | =cut | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | # The default is also kept here: while $basename_regexp still holds it, the | ||||||||||||||||||||||||||||
| 350 | # suffix is found by _default_suffix, a string search with the same result. | ||||||||||||||||||||||||||||
| 351 | 25 | my $default_basename_regexp = qr/[^.]+(\.[^\\\/]+)$/; | |||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | 25 | our $basename_regexp = $default_basename_regexp; | |||||||||||||||||||||||||||
| 354 | 25 | our $file_temp_suffix = '.upload'; | |||||||||||||||||||||||||||
| 355 | 25 | our $file_temp_template; | |||||||||||||||||||||||||||
| 356 | 25 | our %file_temp_parameters; | |||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | 1202 | ▶ | sub handler { | ||||||||||||||||||||||||||
|
Subroutine
handler: called CC=20 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 359 | 1202 | my ( $self, $part ) = @_; | |||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||
| 361 | 1202 | ▶ | unless ( exists $part->{name} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | 1191 | my $disposition = $part->{headers}->{'Content-Disposition'}; | |||||||||||||||||||||||||||
| 364 | 1191 | my ($name) = $disposition =~ / name="?([^\";]+)"?/; | |||||||||||||||||||||||||||
| 365 | 1191 | my ($filename) = $disposition =~ / filename="?([^\"]*)"?/; | |||||||||||||||||||||||||||
| 366 | # Need to match empty filenames above, so this part is flagged as an upload type | ||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | 1191 | $part->{name} = $name; | |||||||||||||||||||||||||||
| 369 | 1191 | ▶ | $part->{filename} = $filename if defined $filename; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 370 | } | ||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||
| 372 | # A file part gets its temporary file on the first call, also when a | ||||||||||||||||||||||||||||
| 373 | # subclass has named the part and set its filename itself. | ||||||||||||||||||||||||||||
| 374 | 1202 | ▶ | if ( defined $part->{filename} && !exists $part->{tempname} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $part->{"filename"} and not exists $part->{"tempname"}
MC/DC100%2/2
defined $part->{"filename"} and not exists $part->{"tempname"}
defined $part->{"filename"}
not exists $part->{"tempname"}
Truth table100%3/3
A and not(B)
A= defined $part->{"filename"}B= not exists $part->{"tempname"}
| |||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | # A NUL in the filename truncates the path for anything downstream that | ||||||||||||||||||||||||||||
| 377 | # hands it to the C layer, so strip it before the name is used or stored. | ||||||||||||||||||||||||||||
| 378 | 102 | $part->{filename} =~ s/\0//g; | |||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | 102 | ▶ | if ( $part->{filename} ne "" ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 381 | 86 | my $suffix = $file_temp_suffix; | |||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | 86 | ▶ | unless ($suffix) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 384 | 24 | my $basename = (File::Spec->splitpath($part->{filename}))[2]; | |||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||
| 386 | # A suffix can be no longer than a file name, so only the end | ||||||||||||||||||||||||||||
| 387 | # of a long basename is looked at. On the whole of a hostile | ||||||||||||||||||||||||||||
| 388 | # one a regexp like the default backtracks in time quadratic | ||||||||||||||||||||||||||||
| 389 | # in its length: seconds per part, once for every part. | ||||||||||||||||||||||||||||
| 390 | 24 | ▶ | $basename = substr( $basename, -255 ) if length($basename) > 255; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||
| 392 | 24 | ▶ | if ( ref $basename_regexp && $basename_regexp == $default_basename_regexp ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $HTTP::Body::MultiPart::basename_regexp and $HTTP::Body::MultiPart::basename_regexp == $default_basename_regexp
MC/DC50%1/2
ref $HTTP::Body::MultiPart::basename_regexp and $HTTP::Body::MultiPart::basename_regexp == $default_basename_regexp
ref $HTTP::Body::MultiPart::basename_regexp
$HTTP::Body::MultiPart::basename_regexp == $default_basename_regexp
Truth table66%2/3
A and B
A= ref $HTTP::Body::MultiPart::basename_regexpB= $HTTP::Body::MultiPart::basename_regexp == $default_basename_regexp
| |||||||||||||||||||||||||||||
| 393 | 14 | $suffix = _default_suffix($basename); | |||||||||||||||||||||||||||
| 394 | } | ||||||||||||||||||||||||||||
| 395 | else { | ||||||||||||||||||||||||||||
| 396 | 10 | ▶ | $suffix = $basename =~ $basename_regexp ? $1 : q{}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 397 | } | ||||||||||||||||||||||||||||
| 398 | } | ||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||
| 400 | 86 | ▶ | my $fh = File::Temp->new( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 401 | UNLINK => 0, DIR => $self->tmpdir, SUFFIX => $suffix, | ||||||||||||||||||||||||||||
| 402 | ( $file_temp_template ? ( TEMPLATE => $file_temp_template ) : () ), | ||||||||||||||||||||||||||||
| 403 | %file_temp_parameters, | ||||||||||||||||||||||||||||
| 404 | ); | ||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||
| 406 | 86 | $part->{fh} = $fh; | |||||||||||||||||||||||||||
| 407 | 86 | $part->{tempname} = $fh->filename; | |||||||||||||||||||||||||||
| 408 | } | ||||||||||||||||||||||||||||
| 409 | } | ||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||
| 411 | 1202 | ▶ | if ( $part->{fh} && ( my $length = length( $part->{data} ) ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$part->{"fh"} and my $length = length $part->{"data"}
MC/DC100%2/2
$part->{"fh"} and my $length = length $part->{"data"}
$part->{"fh"}
my $length = length $part->{"data"}
Truth table100%3/3
A and B
A= $part->{"fh"}B= my $length = length $part->{"data"}
| |||||||||||||||||||||||||||||
| 412 | 85 | ▶ | $part->{fh}->write( substr( $part->{data}, 0, $length, '' ), $length ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 413 | or Carp::croak("Can't write to temporary file: $!"); | ||||||||||||||||||||||||||||
| 414 | } | ||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | 1202 | ▶ | if ( $part->{done} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | 1195 | ▶ | if ( exists $part->{filename} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 419 | 101 | ▶ | if ( $part->{filename} ne "" ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 420 | # A buffered write error often surfaces only here, at close. | ||||||||||||||||||||||||||||
| 421 | 85 | ▶ | if ( defined $part->{fh} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 422 | $part->{fh}->close | ||||||||||||||||||||||||||||
| 423 | 85 | ▶ | or Carp::croak("Can't close temporary file: $!"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 424 | } | ||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||
| 426 | 85 | delete @{$part}{qw[ data done fh ]}; | |||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||
| 428 | 85 | $self->upload( $part->{name}, $part ); | |||||||||||||||||||||||||||
| 429 | } | ||||||||||||||||||||||||||||
| 430 | } | ||||||||||||||||||||||||||||
| 431 | # If we have more than the content-disposition, we need to create a | ||||||||||||||||||||||||||||
| 432 | # data key so that we don't waste the headers. | ||||||||||||||||||||||||||||
| 433 | else { | ||||||||||||||||||||||||||||
| 434 | 1094 | $self->param( $part->{name}, $part->{data} ); | |||||||||||||||||||||||||||
| 435 | 1094 | $self->part_data( $part->{name}, $part ) | |||||||||||||||||||||||||||
| 436 | } | ||||||||||||||||||||||||||||
| 437 | } | ||||||||||||||||||||||||||||
| 438 | } | ||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||
| 440 | # The suffix the default $basename_regexp captures, found without a regexp, | ||||||||||||||||||||||||||||
| 441 | # in time linear in the basename's length. It starts at the first dot after | ||||||||||||||||||||||||||||
| 442 | # the last slash or backslash that follows a byte other than a dot, and runs | ||||||||||||||||||||||||||||
| 443 | # to the end; a dot that is the last byte gives no suffix. | ||||||||||||||||||||||||||||
| 444 | 100035 | ▶ | sub _default_suffix { | ||||||||||||||||||||||||||
|
Subroutine
_default_suffix: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 445 | 100035 | my ($basename) = @_; | |||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||
| 447 | 100035 | my $last = length($basename) - 1; | |||||||||||||||||||||||||||
| 448 | 100035 | my $dot = rindex( $basename, '/' ); | |||||||||||||||||||||||||||
| 449 | 100035 | my $back = rindex( $basename, '\\' ); | |||||||||||||||||||||||||||
| 450 | 100035 | ▶ | $dot = $back if $back > $dot; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||
| 452 | 100035 | ▶ | while ( ( $dot = index( $basename, '.', $dot + 1 ) ) >= 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 453 | 58709 | ▶ | return q{} if $dot == $last; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 454 | 42639 | ▶ | return substr( $basename, $dot ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$dot > 0 and substr($basename, $dot - 1, 1) ne "."
MC/DC100%2/2
$dot > 0 and substr($basename, $dot - 1, 1) ne "."
$dot > 0
substr($basename, $dot - 1, 1) ne "."
Truth table100%3/3
A and B
A= $dot > 0B= substr($basename, $dot - 1, 1) ne "."
| |||||||||||||||||||||||||||||
| 455 | if $dot > 0 && substr( $basename, $dot - 1, 1 ) ne '.'; | ||||||||||||||||||||||||||||
| 456 | } | ||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||
| 458 | 53789 | return q{}; | |||||||||||||||||||||||||||
| 459 | } | ||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||
| 461 | =back | ||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||
| 463 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||
| 465 | See L<HTTP::Body> | ||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||
| 467 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||
| 469 | Christian Hansen, C<ch@ngmedia.com> | ||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||
| 471 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||
| 473 | This library is free software . You can redistribute it and/or modify | ||||||||||||||||||||||||||||
| 474 | it under the same terms as perl itself. | ||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||
| 476 | =cut | ||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||
| 478 | 25 | 1; | |||||||||||||||||||||||||||