blib/lib/PAGI/Middleware/Static.pm untested

filter: Statementstmt 0.0% 0 / 196 Branchbran 0.0% 0 / 64 Conditioncond 0.0% 0 / 56 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 345 CC 33 SCAR 53.2
CC
33
Cov
0%
wrap
462.0
_parse_range
56.0
_resolve_dots
20.0
CRAP 204.7
1 package PAGI::Middleware::Static;
2 $PAGI::Middleware::Static::VERSION = '0.002001';
3 use strict;
4 use warnings;
5 use parent 'PAGI::Middleware';
6 use Future::AsyncAwait;
7 use File::Spec;
8 use Digest::MD5 'md5_hex';
9 use Fcntl ':mode';
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 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 sub _init {
164 my ($self, $config) = @_;
165  
166 $self->{root} = $config->{root} // die "Static middleware requires 'root' option";
167 $self->{path} = $config->{path} // qr{^/};
168 $self->{pass_through} = $config->{pass_through} // 0;
169 $self->{index} = $config->{index} // ['index.html', 'index.htm'];
170 $self->{encoding} = $config->{encoding};
171 $self->{handle_ranges} = $config->{handle_ranges} // 1;
172  
173 # Normalize root path
174 $self->{root} = File::Spec->rel2abs($self->{root});
175 }
176  
177 sub wrap {
178 my ($self, $app) = @_;
179  
180 return async sub {
181 my ($scope, $receive, $send) = @_;
182 # Only handle HTTP GET/HEAD requests
183 if ($scope->{type} ne 'http' || ($scope->{method} ne 'GET' && $scope->{method} ne 'HEAD')) {
184 await $app->($scope, $receive, $send);
185 return;
186 }
187  
188 my $path = $scope->{path};
189  
190 # Check if path matches our pattern
191 my $path_match = $self->{path};
192 if (ref($path_match) eq 'Regexp') {
193 unless ($path =~ $path_match) {
194 await $app->($scope, $receive, $send);
195 return;
196 }
197 } elsif (ref($path_match) eq 'CODE') {
198 my $result = $path_match->($path);
199 unless ($result) {
200 await $app->($scope, $receive, $send);
201 return;
202 }
203 if (defined $result && !ref($result) && $result ne '1') {
204 $path = $result;
205 }
206 }
207  
208 # Normalize rewritten paths to start with /
209 if (defined $path && $path ne '' && $path !~ m{^/}) {
210 $path = '/' . $path;
211 }
212  
213 # Build file path
214 my $file_path = $self->_resolve_path($path);
215  
216 # Check for path traversal
217 unless ($file_path && $self->_is_safe_path($file_path)) {
218 await $self->_send_error($send, 403, 'Forbidden');
219 return;
220 }
221  
222 # Check if file exists
223 unless (-e $file_path) {
224 if ($self->{pass_through}) {
225 await $app->($scope, $receive, $send);
226 return;
227 }
228 await $self->_send_error($send, 404, 'Not Found');
229 return;
230 }
231  
232 # Handle directory with index files
233 if (-d $file_path) {
234 my $index_file = $self->_find_index($file_path);
235 if ($index_file) {
236 $file_path = $index_file;
237 } else {
238 if ($self->{pass_through}) {
239 await $app->($scope, $receive, $send);
240 return;
241 }
242 await $self->_send_error($send, 404, 'Not Found');
243 return;
244 }
245 }
246  
247 # Get file stats
248 my @stat = stat($file_path);
249 unless (@stat) {
250 await $self->_send_error($send, 500, 'Cannot stat file');
251 return;
252 }
253  
254 my $size = $stat[7];
255 my $mtime = $stat[9];
256  
257 # Generate ETag
258 my $etag = $self->_generate_etag($file_path, $size, $mtime);
259  
260 # Check If-None-Match for 304 response
261 my $if_none_match = $self->_get_header($scope, 'if-none-match');
262 if ($if_none_match && $if_none_match eq $etag) {
263 await $send->({
264 type => 'http.response.start',
265 status => 304,
266 headers => [
267 ['etag', $etag],
268 ],
269 });
270 await $send->({
271 type => 'http.response.body',
272 body => '',
273 more => 0,
274 });
275 return;
276 }
277  
278 # Get MIME type
279 my $content_type = $self->_get_mime_type($file_path);
280  
281 # Check for Range request (only if handle_ranges is enabled)
282 my $range_header = $self->{handle_ranges} ? $self->_get_header($scope, 'range') : undef;
283 my ($start, $end, $is_range) = $self->_parse_range($range_header, $size);
284  
285 if ($is_range && !defined $start) {
286 # Invalid range
287 await $self->_send_error($send, 416, 'Range Not Satisfiable');
288 return;
289 }
290  
291 # Build headers
292 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 my $status;
300 my $body_size;
301  
302 if ($is_range) {
303 $status = 206;
304 $body_size = $end - $start + 1;
305 push @headers, ['content-range', "bytes $start-$end/$size"];
306 push @headers, ['content-length', $body_size];
307 } else {
308 $status = 200;
309 $body_size = $size;
310 push @headers, ['content-length', $size];
311 }
312  
313 # Send response start
314 await $send->({
315 type => 'http.response.start',
316 status => $status,
317 headers => \@headers,
318 });
319  
320 # For HEAD requests, don't send body
321 if ($scope->{method} eq 'HEAD') {
322 await $send->({
323 type => 'http.response.body',
324 body => '',
325 more => 0,
326 });
327 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 if ($is_range) {
333 await $send->({
334 type => 'http.response.body',
335 file => $file_path,
336 offset => $start,
337 length => $body_size,
338 });
339 }
340 else {
341 await $send->({
342 type => 'http.response.body',
343 file => $file_path,
344 });
345 }
346 };
347 }
348  
349 sub _resolve_path {
350 my ($self, $url_path) = @_;
351  
352 # Path is already URL-decoded by the server, so no decoding here
353 my $decoded = $url_path;
354  
355 # Remove query string
356 $decoded =~ s/\?.*//;
357  
358 # Combine with root (use manual concat to preserve .. for security check)
359 my $root = $self->{root};
360 $root =~ s{/$}{}; # Remove trailing slash from root
361 return $root . $decoded;
362 }
363  
364 sub _is_safe_path {
365 my ($self, $file_path) = @_;
366  
367 my $root = $self->{root};
368  
369 # Manually resolve the path to handle .. without requiring file to exist
370 my $abs_path = $self->_resolve_dots($file_path);
371 return 0 unless defined $abs_path;
372  
373 # Normalize both paths
374 $abs_path =~ s{/+}{/}g;
375 $root =~ s{/+}{/}g;
376 $root =~ s{/$}{};
377 $abs_path =~ s{/$}{};
378  
379 # Path must start with root
380 return $abs_path =~ m{^\Q$root\E(?:/|$)};
381 }
382  
383 sub _resolve_dots {
384 my ($self, $path) = @_;
385  
386 # Split path into components
387 my @parts = split m{/}, $path;
388 my @resolved;
389  
390 for my $part (@parts) {
391 if ($part eq '' || $part eq '.') {
392 # Skip empty and current dir
393 next;
394 } elsif ($part eq '..') {
395 # Go up one directory
396 if (@resolved) {
397 pop @resolved;
398 }
399 # If we can't go up, the path is invalid (would escape root)
400 } else {
401 push @resolved, $part;
402 }
403 }
404  
405 # Reconstruct absolute path
406 return '/' . join('/', @resolved);
407 }
408  
409 sub _find_index {
410 my ($self, $dir_path) = @_;
411  
412 for my $index (@{$self->{index}}) {
413 my $index_path = File::Spec->catfile($dir_path, $index);
414 return $index_path if -f $index_path;
415 }
416 return;
417 }
418  
419 sub _generate_etag {
420 my ($self, $file_path, $size, $mtime) = @_;
421  
422 my $data = "$file_path:$size:$mtime";
423 return '"' . md5_hex($data) . '"';
424 }
425  
426 sub _get_mime_type {
427 my ($self, $file_path) = @_;
428  
429 my ($ext) = $file_path =~ /\.([^.]+)$/;
430 $ext = lc($ext // '');
431 return $MIME_TYPES{$ext} // 'application/octet-stream';
432 }
433  
434 sub _get_header {
435 my ($self, $scope, $name) = @_;
436  
437 $name = lc($name);
438 for my $h (@{$scope->{headers} // []}) {
439 return $h->[1] if lc($h->[0]) eq $name;
440 }
441 return;
442 }
443  
444 sub _parse_range {
445 my ($self, $range_header, $size) = @_;
446  
447 return (undef, undef, 0) unless $range_header;
448  
449 # Parse "bytes=start-end" format
450 if ($range_header =~ /^bytes=(\d*)-(\d*)$/) {
451 my ($start, $end) = ($1, $2);
452  
453 if ($start eq '' && $end ne '') {
454 # Suffix range: last N bytes
455 $start = $size - $end;
456 $end = $size - 1;
457 } elsif ($start ne '' && $end eq '') {
458 # From start to end
459 $start = int($start);
460 $end = $size - 1;
461 } else {
462 $start = int($start);
463 $end = int($end);
464 }
465  
466 # Validate range
467 return (undef, undef, 1) if $start > $end || $start >= $size;
468  
469 $end = $size - 1 if $end >= $size;
470  
471 return ($start, $end, 1);
472 }
473  
474 return (undef, undef, 0);
475 }
476  
477 sub _format_http_date {
478 my ($self, $epoch) = @_;
479  
480 my @days = qw(Sun Mon Tue Wed Thu Fri Sat);
481 my @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
482 my @t = gmtime($epoch);
483 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 async sub _send_error {
489 my ($self, $send, $status, $message) = @_;
490  
491 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 await $send->({
500 type => 'http.response.body',
501 body => $message,
502 more => 0,
503 });
504 }
505  
506 1;
507  
508 __END__