| 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")
MC/DC0%0/1
$config->{"root"} // die("Static middleware requires 'root' option")
$config->{"root"}
Truth table50%1/2
A
A= $config->{"root"}
| |||||||||||||||||||||||||||||||||||||||||||
| 167 | 22 | ▶ | $self->{path} = $config->{path} // qr{^/}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$config->{"path"} // qr"^/"
MC/DC100%1/1
$config->{"path"} // qr"^/"
$config->{"path"}
Truth table100%2/2
A
A= $config->{"path"}
| |||||||||||||||||||||||||||||||||||||||||||
| 168 | 22 | ▶ | $self->{pass_through} = $config->{pass_through} // 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$config->{"pass_through"} // 0
MC/DC100%1/1
$config->{"pass_through"} // 0
$config->{"pass_through"}
Truth table100%2/2
A
A= $config->{"pass_through"}
| |||||||||||||||||||||||||||||||||||||||||||
| 169 | 22 | ▶ | $self->{index} = $config->{index} // ['index.html', 'index.htm']; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$config->{"index"} // ["index.html", "index.htm"]
MC/DC0%0/1
$config->{"index"} // ["index.html", "index.htm"]
$config->{"index"}
Truth table50%1/2
A
A= $config->{"index"}
| |||||||||||||||||||||||||||||||||||||||||||
| 170 | 22 | $self->{encoding} = $config->{encoding}; | |||||||||||||||||||||||||||||||||||||||||
| 171 | 22 | ▶ | $self->{handle_ranges} = $config->{handle_ranges} // 1; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$config->{"handle_ranges"} // 1
MC/DC100%1/1
$config->{"handle_ranges"} // 1
$config->{"handle_ranges"}
Truth table100%2/2
A
A= $config->{"handle_ranges"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition83%5/6
$scope->{"type"} ne "http" or $scope->{"method"} ne "GET" and $scope->{"method"} ne "HEAD"
$scope->{"method"} ne "GET" and $scope->{"method"} ne "HEAD"
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 193 | 20 | ▶ | unless ($path =~ $path_match) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 200 | 0 | await $app->($scope, $receive, $send); | |||||||||||||||||||||||||||||||||||||||||
| 201 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||||||||||||||||
| 203 | 2 | ▶ | if (defined $result && !ref($result) && $result ne '1') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
defined $result and not ref $result and $result ne 1
defined $result and not ref $result
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition50%3/6
defined $path and $path ne "" and not $path =~ m[^/]
defined $path and $path ne ""
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[^/]
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$file_path and $self->_is_safe_path($file_path)
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)
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 224 | 4 | ▶ | if ($self->{pass_through}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 234 | 1 | my $index_file = $self->_find_index($file_path); | |||||||||||||||||||||||||||||||||||||||||
| 235 | 1 | ▶ | if ($index_file) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 236 | 1 | $file_path = $index_file; | |||||||||||||||||||||||||||||||||||||||||
| 237 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 238 | 0 | ▶ | if ($self->{pass_through}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$if_none_match and $if_none_match eq $etag
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 283 | 16 | my ($start, $end, $is_range) = $self->_parse_range($range_header, $size); | |||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||
| 285 | 16 | ▶ | if ($is_range && !defined $start) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$is_range and not defined $start
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$part eq "" or $part eq "."
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 "."
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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 // ""
MC/DC0%0/1
$ext // ""
$ext
Truth table50%1/2
A
A= $ext
| |||||||||||||||||||||||||||||||||||||||||||
| 431 | 16 | ▶ | return $MIME_TYPES{$ext} // 'application/octet-stream'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$MIME_TYPES{$ext} // "application/octet-stream"
MC/DC0%0/1
$MIME_TYPES{$ext} // "application/octet-stream"
$MIME_TYPES{$ext}
Truth table50%1/2
A
A= $MIME_TYPES{$ext}
| |||||||||||||||||||||||||||||||||||||||||||
| 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"} // []
MC/DC0%0/1
$scope->{"headers"} // []
$scope->{"headers"}
Truth table50%1/2
A
A= $scope->{"headers"}
| |||||||||||||||||||||||||||||||||||||||||||
| 439 | 12 | ▶ | return $h->[1] if lc($h->[0]) eq $name; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||||||||||||||||
| 449 | # Parse "bytes=start-end" format | ||||||||||||||||||||||||||||||||||||||||||
| 450 | 5 | ▶ | if ($range_header =~ /^bytes=(\d*)-(\d*)$/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 451 | 5 | my ($start, $end) = ($1, $2); | |||||||||||||||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||||||||||||||||
| 453 | 5 | ▶ | if ($start eq '' && $end ne '') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
$start eq "" and $end ne ""
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 ""
| |||||||||||||||||||||||||||||||||||||||||||
| 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 ""
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 ""
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$start > $end or $start >= $size
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
| |||||||||||||||||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||||||||||||||||
| 469 | 4 | ▶ | $end = $size - 1 if $end >= $size; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||