blib/lib/HTTP/Entity/Parser/MultiPart.pm

filter: Statementstmt 97.7% 88 / 90 Branchbran 82.6% 38 / 46 Conditioncond 88.8% 8 / 9 MC/DCmcdc 66.6% 4 / 6 Subroutinesub 100% 12 / 12 Podpod 0.0% 0 / 4 totaltotal 89.8% 150 / 167 SCAR 33.4
CC
28
Cov
92%
extract_form_data
12.0
__ANON__
7.0
__ANON__
7.0
CRAP 28.3
↑ Summary blib/lib/HTTP/Entity/Parser.pm »
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
BranchTrueFalseTotal
if (/\A form-data; \s name="((?:[^"]|\\")*)" (?: ;\s filename="((?:[^"]|\\")*)" )? \z/x) 148 28 176
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
BranchTrueFalseTotal
s/\A \s* form-data \s* ; //xi or return 2 26 28
23  
24 26 my (%p, $k, $v);
25 26 ▶ while (length) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $_) 49 21 70
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
BranchTrueFalseTotal
if s/^ [^\s"=;]* \s* ; //x 3 46 49
31  
32 # parameter name (token)
33 46 ▶ s/^ ([^\s"=;]+) \s* = \s* //x
Branch: 1 branch
BranchTrueFalseTotal
s/^ ([^\s"=;]+) \s* = \s* //x or return 2 44 46
34 or return;
35 44 $k = lc $1;
36 # quoted parameter value
37 44 ▶ if (s/^ "((?:[^"]|\\")*)" \s* (?: ; | $) //x) {
Branch: 2 branches
BranchTrueFalseTotal
if (s/^ "((?:[^"]|\\")*)" \s* (?: ; | $) //x) { } 28 16 44
elsif (s/^ ([^\s";]*) \s* (?: ; | $) //x) { } 15 1 16
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
BranchTrueFalseTotal
if ($k eq "name" or $k eq "filename") 36 7 43
Condition100%3/3
$k eq "name" or $k eq "filename"
l!l&&r!l&&!r
23137
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"
ABresult
1X1
011
000
48 36 ▶ return () if exists $p{$k};
Branch: 1 branch
BranchTrueFalseTotal
if exists $p{$k} 2 34 36
49 34 $p{$k} = $v;
50 }
51 }
52 21 ▶ return exists $p{name} ? @p{qw(name filename)} : ();
Branch: 1 branch
BranchTrueFalseTotal
exists $p{'name'} ? : 20 1 21
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
BranchTrueFalseTotal
unless (defined $env->{"CONTENT_TYPE"}) 0 63 63
64 0 Carp::croak("Missing CONTENT_TYPE in PSGI env");
65 }
66 63 ▶ unless ( $env->{CONTENT_TYPE} =~ /boundary=\"?([^\";]+)\"?/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($env->{"CONTENT_TYPE"} =~ /boundary=\"?([^\";]+)\"?/) 0 63 63
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
BranchTrueFalseTotal
if (/\A Content-Disposition: [\x09\x20]* (.*)/xi) 176 0 176
81 176 $disposition = $1;
82 176 last;
83 }
84 }
85  
86 176 ▶ (defined $disposition)
Branch: 1 branch
BranchTrueFalseTotal
defined $disposition or die "Content-Disposition header is missing in part" 0 176 176
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
BranchTrueFalseTotal
defined $disposition_name or die "Parameter 'name' is missing from Content-Disposition header" 8 168 176
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
BranchTrueFalseTotal
if (defined $disposition_filename) 88 80 168
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 }
l!l&&r!l&&!r
46420
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 }
ABresult
1X1
011
000
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
BranchTrueFalseTotal
if ($fh) { } 138 96 234
117 138 ▶ print $fh $chunk
Branch: 1 branch
BranchTrueFalseTotal
print $fh $chunk or die "Could not write to file handle: '$!'" 0 138 138
118 or die qq/Could not write to file handle: '$!'/;
119 138 ▶ if ($final && $part->{filename} ne "" ) { # compatible with HTTP::Body
Branch: 1 branch
BranchTrueFalseTotal
if ($final and $part->{"filename"} ne "") 71 67 138
Condition100%3/3
$final and $part->{"filename"} ne ""
!ll&&!rl&&r
501771
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 ""
ABresult
0X0
100
111
120 71 ▶ seek($fh, 0, SEEK_SET)
Branch: 1 branch
BranchTrueFalseTotal
seek $fh, 0, 0 or die "Could not rewind file handle: '$!'" 0 71 71
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
BranchTrueFalseTotal
if ($final) 80 16 96
136 80 push @params, $part->{name}, $part->{data};
137 }
138 }
139 },
140 63 ▶ $opts->{on_error} ? (on_error => $opts->{on_error}) : (),
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'on_error'} ? : 0 63 63
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
BranchTrueFalseTotal
if defined $_[0] 6987 0 6987
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__
↑ Summary blib/lib/HTTP/Entity/Parser.pm »