]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/WwwStatic.pm
wwwstatic: do not open() files for HEAD requests
[public-inbox.git] / lib / PublicInbox / WwwStatic.pm
index b8efcf625a184d03507161f570787f4547032f7a..093a79200c8ac580494b925524286ebc520e0e26 100644 (file)
@@ -3,8 +3,23 @@
 
 package PublicInbox::WwwStatic;
 use strict;
+use parent qw(Exporter);
 use Fcntl qw(:seek);
 use HTTP::Date qw(time2str);
+use HTTP::Status qw(status_message);
+our @EXPORT_OK = qw(@NO_CACHE r);
+
+our @NO_CACHE = ('Expires', 'Fri, 01 Jan 1980 00:00:00 GMT',
+               'Pragma', 'no-cache',
+               'Cache-Control', 'no-cache, max-age=0, must-revalidate');
+
+sub r ($;$) {
+       my ($code, $msg) = @_;
+       $msg ||= status_message($code);
+       [ $code, [ qw(Content-Type text/plain), 'Content-Length', length($msg),
+               @NO_CACHE ],
+         [ $msg ] ]
+}
 
 sub prepare_range {
        my ($env, $in, $h, $beg, $end, $size) = @_;
@@ -36,7 +51,9 @@ sub prepare_range {
                if ($len <= 0) {
                        $code = 416;
                } else {
-                       sysseek($in, $beg, SEEK_SET) or return [ 500, [], [] ];
+                       if ($in) {
+                               sysseek($in, $beg, SEEK_SET) or return r(500);
+                       }
                        push @$h, qw(Accept-Ranges bytes Content-Range);
                        push @$h, "bytes $beg-$end/$size";
 
@@ -44,15 +61,24 @@ sub prepare_range {
                        $env->{'psgix.no-compress'} = 1;
                }
        }
+       if ($code == 416) {
+               push @$h, 'Content-Range', "bytes */$size";
+               return [ 416, $h, [] ];
+       }
        ($code, $len);
 }
 
 sub response {
        my ($env, $h, $path, $type) = @_;
-       return unless -f $path && -r _; # just in case it's a FIFO :P
+       return r(404) unless -f $path && -r _; # just in case it's a FIFO :P
 
-       open my $in, '<', $path or return;
-       my $size = -s $in;
+       my ($size, $in);
+       if ($env->{REQUEST_METHOD} eq 'HEAD') {
+               $size = -s _;
+       } else { # GET, callers should've already filtered out other methods
+               open $in, '<', $path or return r(403);
+               $size = -s $in;
+       }
        my $mtime = time2str((stat(_))[9]);
 
        if (my $ims = $env->{HTTP_IF_MODIFIED_SINCE}) {
@@ -64,19 +90,16 @@ sub response {
        push @$h, 'Content-Type', $type;
        if (($env->{HTTP_RANGE} || '') =~ /\bbytes=([0-9]*)-([0-9]*)\z/) {
                ($code, $len) = prepare_range($env, $in, $h, $1, $2, $size);
-               if ($code == 416) {
-                       push @$h, 'Content-Range', "bytes */$size";
-                       return [ 416, $h, [] ];
-               }
+               return $code if ref($code);
        }
        push @$h, 'Content-Length', $len, 'Last-Modified', $mtime;
-       my $body = bless {
+       my $body = $in ? bless {
                initial_rd => 65536,
                len => $len,
                in => $in,
                path => $path,
                env => $env,
-       }, __PACKAGE__;
+       }, __PACKAGE__ : [];
        [ $code, $h, $body ];
 }