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