| 1 | package HTTP::Entity::Parser::MultiPart; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 6 | ▶ | use HTTP::MultiPartParser; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 6 | ▶ | use File::Temp qw/tempfile/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 6 | ▶ | use Carp qw//; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 6 | ▶ | use Fcntl ":seek"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | # | ||||||||||||||||||||||||||||
| 11 | # copy from https://gist.github.com/chansen/7163968 | ||||||||||||||||||||||||||||
| 12 | # | ||||||||||||||||||||||||||||
| 13 | sub extract_form_data { | ||||||||||||||||||||||||||||
| 14 | 176 | ▶ | local $_ = shift; | ||||||||||||||||||||||||||
|
Subroutine
extract_form_data: called CC=12 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 15 | # Fast exit for common form-data disposition | ||||||||||||||||||||||||||||
| 16 | 176 | ▶ | if (/\A form-data; \s name="((?:[^"]|\\")*)" (?: ;\s filename="((?:[^"]|\\")*)" )? \z/x) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 17 | 148 | return ($1, $2); | |||||||||||||||||||||||||||
| 18 | } | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | # disposition type must be form-data | ||||||||||||||||||||||||||||
| 21 | s/\A \s* form-data \s* ; //xi | ||||||||||||||||||||||||||||
| 22 | 28 | ▶ | or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 26 | my (%p, $k, $v); | |||||||||||||||||||||||||||
| 25 | 26 | ▶ | while (length) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 49 | s/ ^ \s+ //x; | |||||||||||||||||||||||||||
| 27 | 49 | s/ \s+ $ //x; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | # skip empty parameters and unknown tokens | ||||||||||||||||||||||||||||
| 30 | 49 | ▶ | next if s/^ [^\s"=;]* \s* ; //x; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | # parameter name (token) | ||||||||||||||||||||||||||||
| 33 | 46 | ▶ | s/^ ([^\s"=;]+) \s* = \s* //x | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | or return; | ||||||||||||||||||||||||||||
| 35 | 44 | $k = lc $1; | |||||||||||||||||||||||||||
| 36 | # quoted parameter value | ||||||||||||||||||||||||||||
| 37 | 44 | ▶ | if (s/^ "((?:[^"]|\\")*)" \s* (?: ; | $) //x) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 38 | 28 | $v = $1; | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | # unquoted parameter value (token) | ||||||||||||||||||||||||||||
| 41 | elsif (s/^ ([^\s";]*) \s* (?: ; | $) //x) { | ||||||||||||||||||||||||||||
| 42 | 15 | $v = $1; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | else { | ||||||||||||||||||||||||||||
| 45 | 1 | return; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | 43 | ▶ | if ($k eq 'name' || $k eq 'filename') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$k eq "name" or $k eq "filename"
MC/DC100%2/2
$k eq "name" or $k eq "filename"
$k eq "name"
$k eq "filename"
Truth table100%3/3
A or B
A= $k eq "name"B= $k eq "filename"
| |||||||||||||||||||||||||||||
| 48 | 36 | ▶ | return () if exists $p{$k}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 34 | $p{$k} = $v; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | 21 | ▶ | return exists $p{name} ? @p{qw(name filename)} : (); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | sub new { | ||||||||||||||||||||||||||||
| 56 | 63 | ▶ | my ($class, $env, $opts) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=14.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 63 | my $self = bless { }, $class; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 63 | my @uploads; | |||||||||||||||||||||||||||
| 61 | my @params; | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 63 | ▶ | unless (defined $env->{CONTENT_TYPE}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 0 | Carp::croak("Missing CONTENT_TYPE in PSGI env"); | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | 63 | ▶ | unless ( $env->{CONTENT_TYPE} =~ /boundary=\"?([^\";]+)\"?/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 0 | Carp::croak("Invalid boundary in content_type: $env->{CONTENT_TYPE}"); | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | 63 | my $boundary = $1; | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 63 | my $part; | |||||||||||||||||||||||||||
| 73 | my $parser = HTTP::MultiPartParser->new( | ||||||||||||||||||||||||||||
| 74 | boundary => $boundary, | ||||||||||||||||||||||||||||
| 75 | on_header => sub { | ||||||||||||||||||||||||||||
| 76 | 176 | ▶ | my ($headers) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 176 | my $disposition; | |||||||||||||||||||||||||||
| 79 | 176 | foreach (@$headers) { | |||||||||||||||||||||||||||
| 80 | 176 | ▶ | if (/\A Content-Disposition: [\x09\x20]* (.*)/xi) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 176 | $disposition = $1; | |||||||||||||||||||||||||||
| 82 | 176 | last; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 176 | ▶ | (defined $disposition) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | or die q/Content-Disposition header is missing in part/; | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 176 | my ($disposition_name, $disposition_filename) = extract_form_data($disposition); | |||||||||||||||||||||||||||
| 90 | 176 | ▶ | defined $disposition_name | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | or die q/Parameter 'name' is missing from Content-Disposition header/; | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 168 | $part = { | |||||||||||||||||||||||||||
| 94 | name => $disposition_name, | ||||||||||||||||||||||||||||
| 95 | headers => $headers, | ||||||||||||||||||||||||||||
| 96 | }; | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 168 | ▶ | if ( defined $disposition_filename ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 88 | $part->{filename} = $disposition_filename; | |||||||||||||||||||||||||||
| 100 | 88 | ▶ | $self->{tempdir} ||= do { | ||||||||||||||||||||||||||
|
Condition66%2/3
$self->{"tempdir"} ||= do {
my $dir = "File::Temp"->newdir("XXXXX", "TMPDIR", 1, "CLEANUP", 1);
push @{$$env{"http.entity.parser.multipart.tempdir"};}, $dir;
$dir
}
MC/DC0%0/2
$self->{"tempdir"} ||= do {
my $dir = "File::Temp"->newdir("XXXXX", "TMPDIR", 1, "CLEANUP", 1);
push @{$$env{"http.entity.parser.multipart.tempdir"};}, $dir;
$dir
}
$self->{"tempdir"}
do {
my $dir = "File::Temp"->newdir("XXXXX", "TMPDIR", 1, "CLEANUP", 1);
push @{$$env{"http.entity.parser.multipart.tempdir"};}, $dir;
$dir
}
Truth table66%2/3
A ||= B
A= $self->{"tempdir"}B= do {
my $dir = "File::Temp"->newdir("XXXXX", "TMPDIR", 1, "CLEANUP", 1);
push @{$$env{"http.entity.parser.multipart.tempdir"};}, $dir;
$dir
}
| |||||||||||||||||||||||||||||
| 101 | 42 | my $dir = File::Temp->newdir('XXXXX', TMPDIR => 1, CLEANUP => 1); | |||||||||||||||||||||||||||
| 102 | # Temporary dirs will remove after the request. | ||||||||||||||||||||||||||||
| 103 | 42 | push @{$env->{'http.entity.parser.multipart.tempdir'}}, $dir; | |||||||||||||||||||||||||||
| 104 | 42 | $dir; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | }; | ||||||||||||||||||||||||||||
| 107 | 88 | my ($tempfh, $tempname) = tempfile(UNLINK => 0, DIR => $self->{tempdir}); | |||||||||||||||||||||||||||
| 108 | 88 | $part->{fh} = $tempfh; | |||||||||||||||||||||||||||
| 109 | 88 | $part->{tempname} = $tempname; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | }, | ||||||||||||||||||||||||||||
| 112 | on_body => sub { | ||||||||||||||||||||||||||||
| 113 | 234 | ▶ | my ($chunk, $final) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 234 | my $fh = $part->{fh}; | |||||||||||||||||||||||||||
| 116 | 234 | ▶ | if ($fh) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 138 | ▶ | print $fh $chunk | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | or die qq/Could not write to file handle: '$!'/; | ||||||||||||||||||||||||||||
| 119 | 138 | ▶ | if ($final && $part->{filename} ne "" ) { # compatible with HTTP::Body | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$final and $part->{"filename"} ne ""
MC/DC100%2/2
$final and $part->{"filename"} ne ""
$final
$part->{"filename"} ne ""
Truth table100%3/3
A and B
A= $finalB= $part->{"filename"} ne ""
| |||||||||||||||||||||||||||||
| 120 | 71 | ▶ | seek($fh, 0, SEEK_SET) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | or die qq/Could not rewind file handle: '$!'/; | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 140 | my @headers = map { split(/\s*:\s*/, $_, 2) } | |||||||||||||||||||||||||||
| 124 | 71 | @{$part->{headers}}; | |||||||||||||||||||||||||||
| 125 | push @uploads, $part->{name}, { | ||||||||||||||||||||||||||||
| 126 | name => $part->{name}, | ||||||||||||||||||||||||||||
| 127 | headers => \@headers, | ||||||||||||||||||||||||||||
| 128 | size => -s $part->{fh}, | ||||||||||||||||||||||||||||
| 129 | filename => $part->{filename}, | ||||||||||||||||||||||||||||
| 130 | tempname => $part->{tempname}, | ||||||||||||||||||||||||||||
| 131 | 71 | }; | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | } else { | ||||||||||||||||||||||||||||
| 134 | 96 | $part->{data} .= $chunk; | |||||||||||||||||||||||||||
| 135 | 96 | ▶ | if ($final) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 80 | push @params, $part->{name}, $part->{data}; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | }, | ||||||||||||||||||||||||||||
| 140 | 63 | ▶ | $opts->{on_error} ? (on_error => $opts->{on_error}) : (), | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | ); | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 63 | $self->{parser} = $parser; | |||||||||||||||||||||||||||
| 144 | 63 | $self->{params} = \@params; | |||||||||||||||||||||||||||
| 145 | 63 | $self->{uploads} = \@uploads; | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 63 | return $self; | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | sub add { | ||||||||||||||||||||||||||||
| 151 | 6987 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
add: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 152 | 6987 | ▶ | $self->{parser}->parse($_[0]) if defined $_[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | sub finalize { | ||||||||||||||||||||||||||||
| 156 | 55 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
finalize: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 157 | 55 | (delete $self->{parser})->finish(); | |||||||||||||||||||||||||||
| 158 | 53 | return ($self->{params}, $self->{uploads}); | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 6 | 1; | |||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | __END__ | ||||||||||||||||||||||||||||