blib/lib/PAGI/Request/MultiPartHandler.pm

filter: Statementstmt 96.6% 115 / 119 Branchbran 77.0% 37 / 48 Conditioncond 45.6% 21 / 46 MC/DCmcdc 7.1% 2 / 28 Subroutinesub 93.3% 14 / 15 Podpod 0.0% 0 / 2 totaltotal 73.2% 189 / 258 CC 44 SCAR 22.5
CC
44
Cov
81%
new
19.9
__ANON__
14.1
__ANON__
9.1
CRAP 9.5
« lib/PAGI/Middleware/Auth/Bearer.pm ↑ Summary blib/lib/PAGI/Lifespan.pm »
1 package PAGI::Request::MultiPartHandler;
2 25 $PAGI::Request::MultiPartHandler::VERSION = '0.002002';
3 25 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 25 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 25 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 25 ▶use HTTP::MultiPartParser;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 25 ▶use Hash::MultiValue;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 25 ▶use PAGI::Request::Upload;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 25 ▶use File::Temp qw(tempfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # Default limits
13 25 our $MAX_FIELD_SIZE = 1 * 1024 * 1024; # 1MB per form field (non-file parts)
14 25 our $MAX_FILE_SIZE = 10 * 1024 * 1024; # 10MB per file upload
15 25 our $SPOOL_THRESHOLD = 64 * 1024; # 64KB before spooling to disk
16 25 our $MAX_FILES = 20;
17 25 our $MAX_FIELDS = 1000;
18  
19 19 ▶sub new {
Subroutine new: called CC=11 SCAR=29.9
Pod undocumented
20 19 my ($class, %args) = @_;
21  
22 die "boundary parameter is required"
23 19 ▶ unless defined $args{boundary} && length $args{boundary};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"boundary"} and length $args{"boundary"} 0 19 19
Condition33%1/3
defined $args{"boundary"} and length $args{"boundary"}
!ll&&!rl&&r
0019
MC/DC0%0/2
defined $args{"boundary"} and length $args{"boundary"}
defined $args{"boundary"} length $args{"boundary"}
Truth table33%1/3
A and B
A= defined $args{"boundary"}B= length $args{"boundary"}
ABresult
0X0
100
111
24 die "receive parameter is required"
25 19 ▶ unless defined $args{receive};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"receive"} 0 19 19
26  
27 return bless {
28 boundary => $args{boundary},
29 receive => $args{receive},
30 max_field_size => $args{max_field_size} // $MAX_FIELD_SIZE,
31 max_file_size => $args{max_file_size} // $MAX_FILE_SIZE,
32 spool_threshold => $args{spool_threshold} // $SPOOL_THRESHOLD,
33 max_files => $args{max_files} // $MAX_FILES,
34 max_fields => $args{max_fields} // $MAX_FIELDS,
35 19 ▶ temp_dir => $args{temp_dir} // $ENV{TMPDIR} // '/tmp',
Condition55%11/20
$args{"max_field_size"} // $PAGI::Request::MultiPartHandler::MAX_FIELD_SIZE
l!l&&r!l&&!r
4150
$args{"max_file_size"} // $PAGI::Request::MultiPartHandler::MAX_FILE_SIZE
l!l&&r!l&&!r
4150
$args{"spool_threshold"} // $PAGI::Request::MultiPartHandler::SPOOL_THRESHOLD
l!l&&r!l&&!r
0190
$args{"max_files"} // $PAGI::Request::MultiPartHandler::MAX_FILES
l!l&&r!l&&!r
1180
$args{"max_fields"} // $PAGI::Request::MultiPartHandler::MAX_FIELDS
l!l&&r!l&&!r
1180
($args{"temp_dir"} // $ENV{"TMPDIR"}) // "/tmp"
l!l
019
$args{"temp_dir"} // $ENV{"TMPDIR"}
l!l&&r!l&&!r
0019
MC/DC0%0/2
$args{"max_field_size"} // $PAGI::Request::MultiPartHandler::MAX_FIELD_SIZE
$args{"max_field_size"} $PAGI::Request::MultiPartHandler::MAX_FIELD_SIZE
MC/DC0%0/2
$args{"max_file_size"} // $PAGI::Request::MultiPartHandler::MAX_FILE_SIZE
$args{"max_file_size"} $PAGI::Request::MultiPartHandler::MAX_FILE_SIZE
MC/DC0%0/2
$args{"spool_threshold"} // $PAGI::Request::MultiPartHandler::SPOOL_THRESHOLD
$args{"spool_threshold"} $PAGI::Request::MultiPartHandler::SPOOL_THRESHOLD
MC/DC0%0/2
$args{"max_files"} // $PAGI::Request::MultiPartHandler::MAX_FILES
$args{"max_files"} $PAGI::Request::MultiPartHandler::MAX_FILES
MC/DC0%0/2
$args{"max_fields"} // $PAGI::Request::MultiPartHandler::MAX_FIELDS
$args{"max_fields"} $PAGI::Request::MultiPartHandler::MAX_FIELDS
MC/DC0%0/2
($args{"temp_dir"} // $ENV{"TMPDIR"}) // "/tmp"
$args{"temp_dir"} $ENV{"TMPDIR"}
Truth table66%2/3
A // B
A= $args{"max_field_size"}B= $PAGI::Request::MultiPartHandler::MAX_FIELD_SIZE
ABresult
1X1
011
000
Truth table66%2/3
A // B
A= $args{"max_file_size"}B= $PAGI::Request::MultiPartHandler::MAX_FILE_SIZE
ABresult
1X1
011
000
Truth table33%1/3
A // B
A= $args{"spool_threshold"}B= $PAGI::Request::MultiPartHandler::SPOOL_THRESHOLD
ABresult
1X1
011
000
Truth table66%2/3
A // B
A= $args{"max_files"}B= $PAGI::Request::MultiPartHandler::MAX_FILES
ABresult
1X1
011
000
Truth table66%2/3
A // B
A= $args{"max_fields"}B= $PAGI::Request::MultiPartHandler::MAX_FIELDS
ABresult
1X1
011
000
Truth table33%1/3
A // B
A= $args{"temp_dir"}B= $ENV{"TMPDIR"}
ABresult
1X1
011
000
36 }, $class;
37 }
38  
39 19 ▶async sub parse {
Subroutine parse: called CC=8 SCAR=21.1
Pod undocumented
40 19 my $self = shift;
41  
42 19 my @form_pairs;
43 my @upload_pairs;
44 19 my @temp_files; # Track for cleanup on error
45 19 my $file_count = 0;
46 19 my $field_count = 0;
47  
48 # Cleanup handler for error cases
49 5 ▶ my $cleanup = sub {
Subroutine __ANON__: called CC=4 SCAR=26.4
50 5 for my $path (@temp_files) {
51 0 ▶ unlink $path if $path && -f $path;
Branch: 1 branch
BranchTrueFalseTotal
if $path and -f $path 0 0 0
Condition0%0/3
$path and -f $path
!ll&&!rl&&r
000
MC/DC0%0/2
$path and -f $path
$path -f $path
Truth table0%0/3
A and B
A= $pathB= -f $path
ABresult
0X0
100
111
52 }
53 19 };
54  
55 # Current part state
56 19 my $current_headers;
57 19 my $current_data = '';
58 19 my $current_fh;
59 my $current_temp_path;
60 19 my $current_size = 0;
61 19 my $current_is_file = 0; # Track if current part is a file upload
62  
63 49 ▶ my $finish_part = sub {
Subroutine __ANON__: called CC=8 SCAR=20.8
64 49 ▶ return unless $current_headers;
Branch: 1 branch
BranchTrueFalseTotal
unless $current_headers 19 30 49
65  
66 30 my $disposition = _parse_content_disposition($current_headers);
67 30 ▶ my $name = $disposition->{name} // '';
Condition50%1/2
$disposition->{"name"} // ""
l!l
300
MC/DC0%0/1
$disposition->{"name"} // ""
$disposition->{"name"}
Truth table50%1/2
A
A= $disposition->{"name"}
Aresult
11
00
68 30 my $filename = $disposition->{filename};
69 30 ▶ my $content_type = $current_headers->{'content-type'} // 'text/plain';
Condition100%2/2
$current_headers->{"content-type"} // "text/plain"
l!l
1515
MC/DC100%1/1
$current_headers->{"content-type"} // "text/plain"
$current_headers->{"content-type"}
Truth table100%2/2
A
A= $current_headers->{"content-type"}
Aresult
11
00
70  
71 30 ▶ if (defined $filename) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filename) { } 18 12 30
72 # File upload
73 18 $file_count++;
74 die "Too many files (max $self->{max_files})"
75 18 ▶ if $file_count > $self->{max_files};
Branch: 1 branch
BranchTrueFalseTotal
if $file_count > $self->{"max_files"} 1 17 18
76  
77 17 my $upload;
78 17 ▶ if ($current_fh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_fh) { } 1 16 17
79 1 close $current_fh;
80 1 $upload = PAGI::Request::Upload->new(
81 field_name => $name,
82 filename => $filename,
83 content_type => $content_type,
84 temp_path => $current_temp_path,
85 size => $current_size,
86 );
87 } else {
88 16 $upload = PAGI::Request::Upload->new(
89 field_name => $name,
90 filename => $filename,
91 content_type => $content_type,
92 data => $current_data,
93 );
94 }
95 17 push @upload_pairs, $name, $upload;
96 } else {
97 # Regular form field
98 12 $field_count++;
99 die "Too many fields (max $self->{max_fields})"
100 12 ▶ if $field_count > $self->{max_fields};
Branch: 1 branch
BranchTrueFalseTotal
if $field_count > $self->{"max_fields"} 1 11 12
101  
102 11 push @form_pairs, $name, $current_data;
103 }
104  
105 # Reset state
106 28 $current_headers = undef;
107 28 $current_data = '';
108 28 $current_fh = undef;
109 28 $current_temp_path = undef;
110 28 $current_size = 0;
111 28 $current_is_file = 0;
112 19 };
113  
114 # Wrap parsing in eval for cleanup on error
115 19 eval {
116 my $parser = HTTP::MultiPartParser->new(
117 boundary => $self->{boundary},
118  
119 33 ▶ on_header => sub {
Subroutine __ANON__: called CC=5 SCAR=16.2
120 33 my ($headers) = @_;
121 33 $finish_part->(); # Finish previous part if any
122  
123 # Parse headers into hash - $headers is an arrayref of header lines
124 33 $current_headers = {};
125 33 for my $line (@$headers) {
126 49 ▶ if ($line =~ /^([^:]+):\s*(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^([^:]+):\s*(.*)$/) 49 0 49
127 49 $current_headers->{lc($1)} = $2;
128 }
129 }
130  
131 # Detect if this part is a file upload (has filename in Content-Disposition)
132 33 ▶ my $cd = $current_headers->{'content-disposition'} // '';
Condition50%1/2
$current_headers->{"content-disposition"} // ""
l!l
330
MC/DC0%0/1
$current_headers->{"content-disposition"} // ""
$current_headers->{"content-disposition"}
Truth table50%1/2
A
A= $current_headers->{"content-disposition"}
Aresult
11
00
133 33 ▶ $current_is_file = ($cd =~ /filename=/i) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$cd =~ /filename=/i ? : 19 14 33
134 },
135  
136 33 ▶ on_body => sub {
Subroutine __ANON__: called CC=9 SCAR=22.1
137 33 my ($chunk) = @_;
138 33 $current_size += length($chunk);
139  
140 # Use different size limits for files vs form fields
141 my $max_size = $current_is_file
142 ? $self->{max_file_size}
143 33 ▶ : $self->{max_field_size};
Branch: 1 branch
BranchTrueFalseTotal
$current_is_file ? : 19 14 33
144 33 ▶ my $part_type = $current_is_file ? 'File upload' : 'Form field';
Branch: 1 branch
BranchTrueFalseTotal
$current_is_file ? : 19 14 33
145 33 ▶ die "$part_type too large (max $max_size bytes)"
Branch: 1 branch
BranchTrueFalseTotal
if $current_size > $max_size 3 30 33
146 if $current_size > $max_size;
147  
148 # Check if we need to spool to disk
149 30 ▶ if (!$current_fh && $current_size > $self->{spool_threshold}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $current_fh and $current_size > $self->{"spool_threshold"}) 1 29 30
Condition66%2/3
not $current_fh and $current_size > $self->{"spool_threshold"}
!ll&&!rl&&r
0291
MC/DC50%1/2
not $current_fh and $current_size > $self->{"spool_threshold"}
not $current_fh $current_size > $self->{"spool_threshold"}
Truth table66%2/3
not(A) and B
A= not $current_fhB= $current_size > $self->{"spool_threshold"}
ABresult
0X0
100
111
150 # Spool to temp file
151 ($current_fh, $current_temp_path) = tempfile(
152 DIR => $self->{temp_dir},
153 1 UNLINK => 0,
154 );
155 1 push @temp_files, $current_temp_path; # Track for cleanup
156 1 binmode($current_fh);
157 1 ▶ print $current_fh $current_data
Branch: 1 branch
BranchTrueFalseTotal
print $current_fh $current_data or die "Failed to write to temp file: $!" 1 0 1
158 or die "Failed to write to temp file: $!";
159 1 $current_data = '';
160 }
161  
162 30 ▶ if ($current_fh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_fh) { } 1 29 30
163 1 ▶ print $current_fh $chunk
Branch: 1 branch
BranchTrueFalseTotal
print $current_fh $chunk or die "Failed to write to temp file: $!" 1 0 1
164 or die "Failed to write to temp file: $!";
165 } else {
166 29 $current_data .= $chunk;
167 }
168 },
169  
170 0 ▶ on_error => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
171 0 my ($error) = @_;
172 0 die "Multipart parse error: $error";
173 },
174 19 );
175  
176 # Feed chunks from receive
177 19 my $receive = $self->{receive};
178 19 while (1) {
179 26 my $message = await $receive->();
180 26 ▶ last unless $message && $message->{type};
Branch: 1 branch
BranchTrueFalseTotal
unless $message and $message->{"type"} 0 26 26
Condition33%1/3
$message and $message->{"type"}
!ll&&!rl&&r
0026
MC/DC0%0/2
$message and $message->{"type"}
$message $message->{"type"}
Truth table33%1/3
A and B
A= $messageB= $message->{"type"}
ABresult
0X0
100
111
181 26 ▶ last if $message->{type} eq 'http.disconnect';
Branch: 1 branch
BranchTrueFalseTotal
if $message->{"type"} eq "http.disconnect" 0 26 26
182  
183 26 ▶ if (defined $message->{body} && length $message->{body}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $message->{"body"} and length $message->{"body"}) 26 0 26
Condition33%1/3
defined $message->{"body"} and length $message->{"body"}
!ll&&!rl&&r
0026
MC/DC0%0/2
defined $message->{"body"} and length $message->{"body"}
defined $message->{"body"} length $message->{"body"}
Truth table33%1/3
A and B
A= defined $message->{"body"}B= length $message->{"body"}
ABresult
0X0
100
111
184 26 $parser->parse($message->{body});
185 }
186  
187 23 ▶ last unless $message->{more};
Branch: 1 branch
BranchTrueFalseTotal
unless $message->{"more"} 16 7 23
188 }
189  
190 16 $parser->finish;
191 16 $finish_part->(); # Handle last part
192 };
193 19 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 5 14 19
194 5 $cleanup->();
195 5 die $err;
196 }
197  
198 return (
199 14 Hash::MultiValue->new(@form_pairs),
200 Hash::MultiValue->new(@upload_pairs),
201 );
202 25 }
203  
204 30 ▶sub _parse_content_disposition {
Subroutine _parse_content_disposition: called CC=5 SCAR=18.1
205 30 my ($headers) = @_;
206 30 ▶ my $cd = $headers->{'content-disposition'} // '';
Condition50%1/2
$headers->{"content-disposition"} // ""
l!l
300
MC/DC0%0/1
$headers->{"content-disposition"} // ""
$headers->{"content-disposition"}
Truth table50%1/2
A
A= $headers->{"content-disposition"}
Aresult
11
00
207  
208 30 my %result;
209  
210 # Parse name="value" pairs
211 30 ▶ while ($cd =~ /(\w+)="([^"]*)"/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cd =~ /(\w+)="([^"]*)"/g) 48 30 78
212 48 $result{$1} = $2;
213 }
214 # Also handle unquoted values
215 30 ▶ while ($cd =~ /(\w+)=([^;\s"]+)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cd =~ /(\w+)=([^;\s"]+)/g) 0 30 30
216 0 ▶ $result{$1} //= $2;
Condition0%0/3
$result{$1} //= $2
l!l&&r!l&&!r
000
MC/DC0%0/2
$result{$1} //= $2
$result{$1} $2
Truth table0%0/3
A //= B
A= $result{$1}B= $2
ABresult
1X1
011
000
217 }
218  
219 30 return \%result;
220 }
221  
222 25 1;
223  
224 __END__
« lib/PAGI/Middleware/Auth/Bearer.pm ↑ Summary blib/lib/PAGI/Lifespan.pm »