]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WwwStatic.pm
wwwstatic: do not open() files for HEAD requests
[public-inbox.git] / lib / PublicInbox / WwwStatic.pm
1 # Copyright (C) 2016-2019 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 package PublicInbox::WwwStatic;
5 use strict;
6 use parent qw(Exporter);
7 use Fcntl qw(:seek);
8 use HTTP::Date qw(time2str);
9 use HTTP::Status qw(status_message);
10 our @EXPORT_OK = qw(@NO_CACHE r);
11
12 our @NO_CACHE = ('Expires', 'Fri, 01 Jan 1980 00:00:00 GMT',
13                 'Pragma', 'no-cache',
14                 'Cache-Control', 'no-cache, max-age=0, must-revalidate');
15
16 sub r ($;$) {
17         my ($code, $msg) = @_;
18         $msg ||= status_message($code);
19         [ $code, [ qw(Content-Type text/plain), 'Content-Length', length($msg),
20                 @NO_CACHE ],
21           [ $msg ] ]
22 }
23
24 sub prepare_range {
25         my ($env, $in, $h, $beg, $end, $size) = @_;
26         my $code = 200;
27         my $len = $size;
28         if ($beg eq '') {
29                 if ($end ne '') { # "bytes=-$end" => last N bytes
30                         $beg = $size - $end;
31                         $beg = 0 if $beg < 0;
32                         $end = $size - 1;
33                         $code = 206;
34                 } else {
35                         $code = 416;
36                 }
37         } else {
38                 if ($beg > $size) {
39                         $code = 416;
40                 } elsif ($end eq '' || $end >= $size) {
41                         $end = $size - 1;
42                         $code = 206;
43                 } elsif ($end < $size) {
44                         $code = 206;
45                 } else {
46                         $code = 416;
47                 }
48         }
49         if ($code == 206) {
50                 $len = $end - $beg + 1;
51                 if ($len <= 0) {
52                         $code = 416;
53                 } else {
54                         if ($in) {
55                                 sysseek($in, $beg, SEEK_SET) or return r(500);
56                         }
57                         push @$h, qw(Accept-Ranges bytes Content-Range);
58                         push @$h, "bytes $beg-$end/$size";
59
60                         # FIXME: Plack::Middleware::Deflater bug?
61                         $env->{'psgix.no-compress'} = 1;
62                 }
63         }
64         if ($code == 416) {
65                 push @$h, 'Content-Range', "bytes */$size";
66                 return [ 416, $h, [] ];
67         }
68         ($code, $len);
69 }
70
71 sub response {
72         my ($env, $h, $path, $type) = @_;
73         return r(404) unless -f $path && -r _; # just in case it's a FIFO :P
74
75         my ($size, $in);
76         if ($env->{REQUEST_METHOD} eq 'HEAD') {
77                 $size = -s _;
78         } else { # GET, callers should've already filtered out other methods
79                 open $in, '<', $path or return r(403);
80                 $size = -s $in;
81         }
82         my $mtime = time2str((stat(_))[9]);
83
84         if (my $ims = $env->{HTTP_IF_MODIFIED_SINCE}) {
85                 return [ 304, [], [] ] if $mtime eq $ims;
86         }
87
88         my $len = $size;
89         my $code = 200;
90         push @$h, 'Content-Type', $type;
91         if (($env->{HTTP_RANGE} || '') =~ /\bbytes=([0-9]*)-([0-9]*)\z/) {
92                 ($code, $len) = prepare_range($env, $in, $h, $1, $2, $size);
93                 return $code if ref($code);
94         }
95         push @$h, 'Content-Length', $len, 'Last-Modified', $mtime;
96         my $body = $in ? bless {
97                 initial_rd => 65536,
98                 len => $len,
99                 in => $in,
100                 path => $path,
101                 env => $env,
102         }, __PACKAGE__ : [];
103         [ $code, $h, $body ];
104 }
105
106 # called by PSGI servers:
107 sub getline {
108         my ($self) = @_;
109         my $len = $self->{len} or return; # undef, tells server we're done
110         my $n = delete($self->{initial_rd}) // 8192;
111         $n = $len if $len < $n;
112         my $r = sysread($self->{in}, my $buf, $n);
113         if (defined $r && $r > 0) { # success!
114                 $self->{len} = $len - $r;
115                 return $buf;
116         }
117         my $m = defined $r ? "EOF with $len bytes left" : "read error: $!";
118         my $env = $self->{env};
119         $env->{'psgi.errors'}->print("$self->{path} $m\n");
120
121         # drop the client on error
122         if (my $io = $env->{'psgix.io'}) {
123                 $io->close; # this is likely PublicInbox::DS::close
124         } else { # for some PSGI servers w/o psgix.io
125                 die "dropping client socket\n";
126         }
127         undef;
128 }
129
130 sub close {} # noop, just let everything go out-of-scope
131
132 1;