lib/PAGI/Middleware/Static.pm

filter: Statementstmt 91.4% 161 / 176 Branchbran 79.6% 51 / 64 Conditioncond 65.4% 36 / 55 MC/DCmcdc 45.1% 14 / 31 Subroutinesub 100% 20 / 20 Podpod 100% 1 / 1 totaltotal 81.5% 283 / 347 CC 57 SCAR 28.7
CC
57
Cov
84%
__ANON__
36.5
_parse_range
12.1
_init
6.0
CRAP 17.6
« blib/lib/PAGI/Middleware/Lint.pm ↑ Summary blib/lib/PAGI/Middleware/Maintenance.pm »
1 package PAGI::Middleware::Static;
2 1 $PAGI::Middleware::Static::VERSION = '0.002002';
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use parent 'PAGI::Middleware';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Digest::MD5 'md5_hex';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Fcntl ':mode';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =head1 NAME
12  
13 PAGI::Middleware::Static - Static file serving middleware
14  
15 =head1 SYNOPSIS
16  
17 use PAGI::Middleware::Builder;
18  
19 my $app = builder {
20 enable 'Static',
21 root => '/var/www/static',
22 path => qr{^/static/},
23 pass_through => 1;
24 $my_app;
25 };
26  
27 # Rewrite /static/... to /...
28 my $app = builder {
29 enable 'Static',
30 root => '/var/www/static',
31 path => sub {
32 my ($path) = @_;
33 return unless $path =~ m{^/static/};
34 $path =~ s{^/static}{/};
35 return $path; # Rewrite via return value
36 };
37 $my_app;
38 };
39  
40 # Legacy (in-place) rewrite - still supported
41 my $app = builder {
42 enable 'Static',
43 root => '/var/www/static',
44 path => sub { $_[0] =~ s{^/static}{/} };
45 $my_app;
46 };
47  
48 =head1 DESCRIPTION
49  
50 PAGI::Middleware::Static serves static files from a specified directory.
51 It includes path traversal protection, MIME type detection, ETag support
52 for caching, and Range request support for partial content.
53  
54 =head1 CONFIGURATION
55  
56 =over 4
57  
58 =item * root (required)
59  
60 The root directory to serve files from.
61  
62 =item * path (default: qr{^/})
63  
64 A regex or coderef to match request paths. Only matching paths are handled.
65  
66 When C<path> is a coderef, it can also rewrite the request path:
67  
68 =over 4
69  
70 =item * Return a string to use as the rewritten path.
71  
72 =item * Return a true value (e.g., C<1>) to match without rewriting.
73  
74 =item * Return a false value to skip static handling.
75  
76 =back
77  
78 In-place mutation of C<$_[0]> is still supported for compatibility, but
79 returning the rewritten path is preferred.
80  
81 =item * pass_through (default: 0)
82  
83 If true, pass requests to inner app when file not found instead of returning 404.
84  
85 =item * index (default: ['index.html', 'index.htm'])
86  
87 Array of index file names to try for directory requests.
88  
89 =item * encoding (default: undef)
90  
91 If set, look for pre-compressed files with this extension (e.g., 'gz' for .gz files).
92  
93 =item * handle_ranges (default: 1)
94  
95 When enabled (default), the middleware processes Range request headers and returns
96 206 Partial Content responses. Set to 0 to ignore Range headers and always
97 return the full file.
98  
99 B<When to disable Range handling:>
100  
101 When using L<PAGI::Middleware::XSendfile> with a reverse proxy (Nginx, Apache),
102 you should disable range handling. The proxy will handle Range requests more
103 efficiently using its native sendfile implementation:
104  
105 my $app = builder {
106 enable 'XSendfile',
107 type => 'X-Accel-Redirect',
108 mapping => { '/var/www/files/' => '/protected/' };
109 enable 'Static',
110 root => '/var/www/files',
111 handle_ranges => 0; # Let proxy handle Range requests
112 $my_app;
113 };
114  
115 =back
116  
117 =cut
118  
119 # MIME type mapping
120 1 my %MIME_TYPES = (
121 html => 'text/html',
122 htm => 'text/html',
123 css => 'text/css',
124 js => 'application/javascript',
125 json => 'application/json',
126 xml => 'application/xml',
127 txt => 'text/plain',
128 csv => 'text/csv',
129  
130 # Images
131 png => 'image/png',
132 jpg => 'image/jpeg',
133 jpeg => 'image/jpeg',
134 gif => 'image/gif',
135 svg => 'image/svg+xml',
136 ico => 'image/x-icon',
137 webp => 'image/webp',
138  
139 # Fonts
140 woff => 'font/woff',
141 woff2 => 'font/woff2',
142 ttf => 'font/ttf',
143 otf => 'font/otf',
144 eot => 'application/vnd.ms-fontobject',
145  
146 # Documents
147 pdf => 'application/pdf',
148 zip => 'application/zip',
149 gz => 'application/gzip',
150 tar => 'application/x-tar',
151  
152 # Media
153 mp3 => 'audio/mpeg',
154 mp4 => 'video/mp4',
155 webm => 'video/webm',
156 ogg => 'audio/ogg',
157 wav => 'audio/wav',
158  
159 # Default
160 bin => 'application/octet-stream',
161 );
162  
163 22 ▶sub _init {
Subroutine _init: called CC=6 SCAR=18.0
164 22 my ($self, $config) = @_;
165  
166 22 ▶ $self->{root} = $config->{root} // die "Static middleware requires 'root' option";
Condition50%1/2
$config->{"root"} // die("Static middleware requires 'root' option")
l!l
220
MC/DC0%0/1
$config->{"root"} // die("Static middleware requires 'root' option")
$config->{"root"}
Truth table50%1/2
A
A= $config->{"root"}
Aresult
11
00
167 22 ▶ $self->{path} = $config->{path} // qr{^/};
Condition100%2/2
$config->{"path"} // qr"^/"
l!l
220
MC/DC100%1/1
$config->{"path"} // qr"^/"
$config->{"path"}
Truth table100%2/2
A
A= $config->{"path"}
Aresult
11
00
168 22 ▶ $self->{pass_through} = $config->{pass_through} // 0;
Condition100%2/2
$config->{"pass_through"} // 0
l!l
121
MC/DC100%1/1
$config->{"pass_through"} // 0
$config->{"pass_through"}
Truth table100%2/2
A
A= $config->{"pass_through"}
Aresult
11
00
169 22 ▶ $self->{index} = $config->{index} // ['index.html', 'index.htm'];
Condition50%1/2
$config->{"index"} // ["index.html", "index.htm"]
l!l
022
MC/DC0%0/1
$config->{"index"} // ["index.html", "index.htm"]
$config->{"index"}
Truth table50%1/2
A
A= $config->{"index"}
Aresult
11
00
170 22 $self->{encoding} = $config->{encoding};
171 22 ▶ $self->{handle_ranges} = $config->{handle_ranges} // 1;
Condition100%2/2
$config->{"handle_ranges"} // 1
l!l
220
MC/DC100%1/1
$config->{"handle_ranges"} // 1
$config->{"handle_ranges"}
Truth table100%2/2
A
A= $config->{"handle_ranges"}
Aresult
11
00
172  
173 # Normalize root path
174 22 $self->{root} = File::Spec->rel2abs($self->{root});
175 }
176  
177 22 ▶sub wrap {
Subroutine wrap: called CC=1 SCAR=0.1
Pod documented
178 22 my ($self, $app) = @_;
179  
180 23 ▶ return async sub {
Subroutine __ANON__: called CC=30 SCAR=36.0
181 23 my ($scope, $receive, $send) = @_;
182 # Only handle HTTP GET/HEAD requests
183 23 ▶ if ($scope->{type} ne 'http' || ($scope->{method} ne 'GET' && $scope->{method} ne 'HEAD')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scope->{"type"} ne "http" or $scope->{"method"} ne "GET" and $scope->{"method"} ne "HEAD") 1 22 23
Condition83%5/6
$scope->{"type"} ne "http" or $scope->{"method"} ne "GET" and $scope->{"method"} ne "HEAD"
l!l&&r!l&&!r
0122
$scope->{"method"} ne "GET" and $scope->{"method"} ne "HEAD"
!ll&&!rl&&r
2111
MC/DC66%2/3
$scope->{"type"} ne "http" or $scope->{"method"} ne "GET" and $scope->{"method"} ne "HEAD"
$scope->{"type"} ne "http" $scope->{"method"} ne "GET" $scope->{"method"} ne "HEAD"
Truth table75%3/4
A or B and C
A= $scope->{"type"} ne "http"B= $scope->{"method"} ne "GET"C= $scope->{"method"} ne "HEAD"
ABCresult
1XX1
0111
00X0
0100
184 1 await $app->($scope, $receive, $send);
185 1 return;
186 }
187  
188 22 my $path = $scope->{path};
189  
190 # Check if path matches our pattern
191 22 my $path_match = $self->{path};
192 22 ▶ if (ref($path_match) eq 'Regexp') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $path_match eq 'Regexp') { } 20 2 22
elsif (ref $path_match eq 'CODE') { } 2 0 2
193 20 ▶ unless ($path =~ $path_match) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($path =~ /$path_match/) 0 20 20
194 0 await $app->($scope, $receive, $send);
195 0 return;
196 }
197 } elsif (ref($path_match) eq 'CODE') {
198 2 my $result = $path_match->($path);
199 2 ▶ unless ($result) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result) 0 2 2
200 0 await $app->($scope, $receive, $send);
201 0 return;
202 }
203 2 ▶ if (defined $result && !ref($result) && $result ne '1') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $result and not ref $result and $result ne 1) 1 1 2
Condition50%3/6
defined $result and not ref $result and $result ne 1
!ll&&!rl&&r
011
defined $result and not ref $result
!ll&&!rl&&r
002
MC/DC33%1/3
defined $result and not ref $result and $result ne 1
defined $result not ref $result $result ne 1
Truth table50%2/4
A and not(B) and C
A= defined $resultB= not ref $resultC= $result ne 1
ABCresult
0XX0
10X0
1100
1111
204 1 $path = $result;
205 }
206 }
207  
208 # Normalize rewritten paths to start with /
209 22 ▶ if (defined $path && $path ne '' && $path !~ m{^/}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $path and $path ne "" and not $path =~ m[^/]) 1 21 22
Condition50%3/6
defined $path and $path ne "" and not $path =~ m[^/]
!ll&&!rl&&r
0211
defined $path and $path ne ""
!ll&&!rl&&r
0022
MC/DC33%1/3
defined $path and $path ne "" and not $path =~ m[^/]
defined $path $path ne "" not $path =~ m[^/]
Truth table50%2/4
A and B and not(C)
A= defined $pathB= $path ne ""C= not $path =~ m[^/]
ABCresult
0XX0
10X0
1100
1111
210 1 $path = '/' . $path;
211 }
212  
213 # Build file path
214 22 my $file_path = $self->_resolve_path($path);
215  
216 # Check for path traversal
217 22 ▶ unless ($file_path && $self->_is_safe_path($file_path)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($file_path and $self->_is_safe_path($file_path)) 1 21 22
Condition66%2/3
$file_path and $self->_is_safe_path($file_path)
!ll&&!rl&&r
0121
MC/DC50%1/2
$file_path and $self->_is_safe_path($file_path)
$file_path $self->_is_safe_path($file_path)
Truth table66%2/3
A and B
A= $file_pathB= $self->_is_safe_path($file_path)
ABresult
0X0
100
111
218 1 await $self->_send_error($send, 403, 'Forbidden');
219 1 return;
220 }
221  
222 # Check if file exists
223 21 ▶ unless (-e $file_path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $file_path) 4 17 21
224 4 ▶ if ($self->{pass_through}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"pass_through"}) 1 3 4
225 1 await $app->($scope, $receive, $send);
226 1 return;
227 }
228 3 await $self->_send_error($send, 404, 'Not Found');
229 3 return;
230 }
231  
232 # Handle directory with index files
233 17 ▶ if (-d $file_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $file_path) 1 16 17
234 1 my $index_file = $self->_find_index($file_path);
235 1 ▶ if ($index_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index_file) { } 1 0 1
236 1 $file_path = $index_file;
237 } else {
238 0 ▶ if ($self->{pass_through}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"pass_through"}) 0 0 0
239 0 await $app->($scope, $receive, $send);
240 0 return;
241 }
242 0 await $self->_send_error($send, 404, 'Not Found');
243 0 return;
244 }
245 }
246  
247 # Get file stats
248 17 my @stat = stat($file_path);
249 17 ▶ unless (@stat) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@stat) 0 17 17
250 0 await $self->_send_error($send, 500, 'Cannot stat file');
251 0 return;
252 }
253  
254 17 my $size = $stat[7];
255 17 my $mtime = $stat[9];
256  
257 # Generate ETag
258 17 my $etag = $self->_generate_etag($file_path, $size, $mtime);
259  
260 # Check If-None-Match for 304 response
261 17 my $if_none_match = $self->_get_header($scope, 'if-none-match');
262 17 ▶ if ($if_none_match && $if_none_match eq $etag) {
Branch: 1 branch
BranchTrueFalseTotal
if ($if_none_match and $if_none_match eq $etag) 1 16 17
Condition66%2/3
$if_none_match and $if_none_match eq $etag
!ll&&!rl&&r
1601
MC/DC50%1/2
$if_none_match and $if_none_match eq $etag
$if_none_match $if_none_match eq $etag
Truth table66%2/3
A and B
A= $if_none_matchB= $if_none_match eq $etag
ABresult
0X0
100
111
263 1 await $send->({
264 type => 'http.response.start',
265 status => 304,
266 headers => [
267 ['etag', $etag],
268 ],
269 });
270 1 await $send->({
271 type => 'http.response.body',
272 body => '',
273 more => 0,
274 });
275 1 return;
276 }
277  
278 # Get MIME type
279 16 my $content_type = $self->_get_mime_type($file_path);
280  
281 # Check for Range request (only if handle_ranges is enabled)
282 16 ▶ my $range_header = $self->{handle_ranges} ? $self->_get_header($scope, 'range') : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'handle_ranges'} ? : 15 1 16
283 16 my ($start, $end, $is_range) = $self->_parse_range($range_header, $size);
284  
285 16 ▶ if ($is_range && !defined $start) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_range and not defined $start) 1 15 16
Condition100%3/3
$is_range and not defined $start
!ll&&!rl&&r
1141
MC/DC100%2/2
$is_range and not defined $start
$is_range not defined $start
Truth table100%3/3
A and not(B)
A= $is_rangeB= not defined $start
ABresult
0X0
100
111
286 # Invalid range
287 1 await $self->_send_error($send, 416, 'Range Not Satisfiable');
288 1 return;
289 }
290  
291 # Build headers
292 15 my @headers = (
293 ['content-type', $content_type],
294 ['etag', $etag],
295 ['last-modified', $self->_format_http_date($mtime)],
296 ['accept-ranges', 'bytes'],
297 );
298  
299 15 my $status;
300 my $body_size;
301  
302 15 ▶ if ($is_range) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_range) { } 4 11 15
303 4 $status = 206;
304 4 $body_size = $end - $start + 1;
305 4 push @headers, ['content-range', "bytes $start-$end/$size"];
306 4 push @headers, ['content-length', $body_size];
307 } else {
308 11 $status = 200;
309 11 $body_size = $size;
310 11 push @headers, ['content-length', $size];
311 }
312  
313 # Send response start
314 15 await $send->({
315 type => 'http.response.start',
316 status => $status,
317 headers => \@headers,
318 });
319  
320 # For HEAD requests, don't send body
321 15 ▶ if ($scope->{method} eq 'HEAD') {
Branch: 1 branch
BranchTrueFalseTotal
if ($scope->{"method"} eq "HEAD") 1 14 15
322 1 await $send->({
323 type => 'http.response.body',
324 body => '',
325 more => 0,
326 });
327 1 return;
328 }
329  
330 # Use file response for efficient streaming (sendfile or worker pool)
331 # This also enables XSendfile middleware to intercept the response
332 14 ▶ if ($is_range) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_range) { } 4 10 14
333 4 await $send->({
334 type => 'http.response.body',
335 file => $file_path,
336 offset => $start,
337 length => $body_size,
338 });
339 }
340 else {
341 10 await $send->({
342 type => 'http.response.body',
343 file => $file_path,
344 });
345 }
346 22 };
347 }
348  
349 22 ▶sub _resolve_path {
Subroutine _resolve_path: called CC=1 SCAR=0.0
350 22 my ($self, $url_path) = @_;
351  
352 # Path is already URL-decoded by the server, so no decoding here
353 22 my $decoded = $url_path;
354  
355 # Remove query string
356 22 $decoded =~ s/\?.*//;
357  
358 # Combine with root (use manual concat to preserve .. for security check)
359 22 my $root = $self->{root};
360 22 $root =~ s{/$}{}; # Remove trailing slash from root
361 22 return $root . $decoded;
362 }
363  
364 22 ▶sub _is_safe_path {
Subroutine _is_safe_path: called CC=2 SCAR=6.9
365 22 my ($self, $file_path) = @_;
366  
367 22 my $root = $self->{root};
368  
369 # Manually resolve the path to handle .. without requiring file to exist
370 22 my $abs_path = $self->_resolve_dots($file_path);
371 22 ▶ return 0 unless defined $abs_path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $abs_path 0 22 22
372  
373 # Normalize both paths
374 22 $abs_path =~ s{/+}{/}g;
375 22 $root =~ s{/+}{/}g;
376 22 $root =~ s{/$}{};
377 22 $abs_path =~ s{/$}{};
378  
379 # Path must start with root
380 22 return $abs_path =~ m{^\Q$root\E(?:/|$)};
381 }
382  
383 22 ▶sub _resolve_dots {
Subroutine _resolve_dots: called CC=6 SCAR=18.0
384 22 my ($self, $path) = @_;
385  
386 # Split path into components
387 22 my @parts = split m{/}, $path;
388 22 my @resolved;
389  
390 22 for my $part (@parts) {
391 208 ▶ if ($part eq '' || $part eq '.') {
Branch: 2 branches
BranchTrueFalseTotal
if ($part eq '' or $part eq '.') { } 23 185 208
elsif ($part eq '..') { } 3 182 185
Condition66%2/3
$part eq "" or $part eq "."
l!l&&r!l&&!r
230185
MC/DC50%1/2
$part eq "" or $part eq "."
$part eq "" $part eq "."
Truth table66%2/3
A or B
A= $part eq ""B= $part eq "."
ABresult
1X1
011
000
392 # Skip empty and current dir
393 23 next;
394 } elsif ($part eq '..') {
395 # Go up one directory
396 3 ▶ if (@resolved) {
Branch: 1 branch
BranchTrueFalseTotal
if (@resolved) 3 0 3
397 3 pop @resolved;
398 }
399 # If we can't go up, the path is invalid (would escape root)
400 } else {
401 182 push @resolved, $part;
402 }
403 }
404  
405 # Reconstruct absolute path
406 22 return '/' . join('/', @resolved);
407 }
408  
409 1 ▶sub _find_index {
Subroutine _find_index: called CC=3 SCAR=11.4
410 1 my ($self, $dir_path) = @_;
411  
412 1 for my $index (@{$self->{index}}) {
413 1 my $index_path = File::Spec->catfile($dir_path, $index);
414 1 ▶ return $index_path if -f $index_path;
Branch: 1 branch
BranchTrueFalseTotal
if -f $index_path 1 0 1
415 }
416 0 return;
417 }
418  
419 17 ▶sub _generate_etag {
Subroutine _generate_etag: called CC=1 SCAR=0.0
420 17 my ($self, $file_path, $size, $mtime) = @_;
421  
422 17 my $data = "$file_path:$size:$mtime";
423 17 return '"' . md5_hex($data) . '"';
424 }
425  
426 16 ▶sub _get_mime_type {
Subroutine _get_mime_type: called CC=3 SCAR=11.4
427 16 my ($self, $file_path) = @_;
428  
429 16 my ($ext) = $file_path =~ /\.([^.]+)$/;
430 16 ▶ $ext = lc($ext // '');
Condition50%1/2
$ext // ""
l!l
160
MC/DC0%0/1
$ext // ""
$ext
Truth table50%1/2
A
A= $ext
Aresult
11
00
431 16 ▶ return $MIME_TYPES{$ext} // 'application/octet-stream';
Condition50%1/2
$MIME_TYPES{$ext} // "application/octet-stream"
l!l
160
MC/DC0%0/1
$MIME_TYPES{$ext} // "application/octet-stream"
$MIME_TYPES{$ext}
Truth table50%1/2
A
A= $MIME_TYPES{$ext}
Aresult
11
00
432 }
433  
434 32 ▶sub _get_header {
Subroutine _get_header: called CC=4 SCAR=13.9
435 32 my ($self, $scope, $name) = @_;
436  
437 32 $name = lc($name);
438 32 ▶ for my $h (@{$scope->{headers} // []}) {
Condition50%1/2
$scope->{"headers"} // []
l!l
320
MC/DC0%0/1
$scope->{"headers"} // []
$scope->{"headers"}
Truth table50%1/2
A
A= $scope->{"headers"}
Aresult
11
00
439 12 ▶ return $h->[1] if lc($h->[0]) eq $name;
Branch: 1 branch
BranchTrueFalseTotal
if lc $h->[0] eq $name 6 6 12
440 }
441 26 return;
442 }
443  
444 16 ▶sub _parse_range {
Subroutine _parse_range: called CC=10 SCAR=25.0
445 16 my ($self, $range_header, $size) = @_;
446  
447 16 ▶ return (undef, undef, 0) unless $range_header;
Branch: 1 branch
BranchTrueFalseTotal
unless $range_header 11 5 16
448  
449 # Parse "bytes=start-end" format
450 5 ▶ if ($range_header =~ /^bytes=(\d*)-(\d*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($range_header =~ /^bytes=(\d*)-(\d*)$/) 5 0 5
451 5 my ($start, $end) = ($1, $2);
452  
453 5 ▶ if ($start eq '' && $end ne '') {
Branch: 2 branches
BranchTrueFalseTotal
if ($start eq '' and $end ne '') { } 1 4 5
elsif ($start ne '' and $end eq '') { } 0 4 4
Condition66%2/3
$start eq "" and $end ne ""
!ll&&!rl&&r
401
MC/DC50%1/2
$start eq "" and $end ne ""
$start eq "" $end ne ""
Truth table66%2/3
A and B
A= $start eq ""B= $end ne ""
ABresult
0X0
100
111
454 # Suffix range: last N bytes
455 1 $start = $size - $end;
456 1 $end = $size - 1;
457 ▶ } elsif ($start ne '' && $end eq '') {
Condition33%1/3
$start ne "" and $end eq ""
!ll&&!rl&&r
040
MC/DC0%0/2
$start ne "" and $end eq ""
$start ne "" $end eq ""
Truth table33%1/3
A and B
A= $start ne ""B= $end eq ""
ABresult
0X0
100
111
458 # From start to end
459 0 $start = int($start);
460 0 $end = $size - 1;
461 } else {
462 4 $start = int($start);
463 4 $end = int($end);
464 }
465  
466 # Validate range
467 5 ▶ return (undef, undef, 1) if $start > $end || $start >= $size;
Branch: 1 branch
BranchTrueFalseTotal
if $start > $end or $start >= $size 1 4 5
Condition66%2/3
$start > $end or $start >= $size
l!l&&r!l&&!r
014
MC/DC50%1/2
$start > $end or $start >= $size
$start > $end $start >= $size
Truth table66%2/3
A or B
A= $start > $endB= $start >= $size
ABresult
1X1
011
000
468  
469 4 ▶ $end = $size - 1 if $end >= $size;
Branch: 1 branch
BranchTrueFalseTotal
if $end >= $size 0 4 4
470  
471 4 return ($start, $end, 1);
472 }
473  
474 0 return (undef, undef, 0);
475 }
476  
477 15 ▶sub _format_http_date {
Subroutine _format_http_date: called CC=1 SCAR=0.0
478 15 my ($self, $epoch) = @_;
479  
480 15 my @days = qw(Sun Mon Tue Wed Thu Fri Sat);
481 15 my @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
482 15 my @t = gmtime($epoch);
483 15 return sprintf("%s, %02d %s %04d %02d:%02d:%02d GMT",
484 $days[$t[6]], $t[3], $months[$t[4]], $t[5] + 1900,
485 $t[2], $t[1], $t[0]);
486 }
487  
488 5 ▶async sub _send_error {
Subroutine _send_error: called CC=1 SCAR=0.0
489 5 my ($self, $send, $status, $message) = @_;
490  
491 5 await $send->({
492 type => 'http.response.start',
493 status => $status,
494 headers => [
495 ['content-type', 'text/plain'],
496 ['content-length', length($message)],
497 ],
498 });
499 5 await $send->({
500 type => 'http.response.body',
501 body => $message,
502 more => 0,
503 });
504 1 }
505  
506 1 1;
507  
508 __END__
« blib/lib/PAGI/Middleware/Lint.pm ↑ Summary blib/lib/PAGI/Middleware/Maintenance.pm »