]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WwwStatic.pm
wwwstatic: move r(...) functions here
[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                         sysseek($in, $beg, SEEK_SET) or return r(500);
55                         push @$h, qw(Accept-Ranges bytes Content-Range);
56                         push @$h, "bytes $beg-$end/$size";
57
58                         # FIXME: Plack::Middleware::Deflater bug?
59                         $env->{'psgix.no-compress'} = 1;
60                 }
61         }
62         if ($code == 416) {
63                 push @$h, 'Content-Range', "bytes */$size";
64                 return [ 416, $h, [] ];
65         }
66         ($code, $len);
67 }
68
69 sub response {
70         my ($env, $h, $path, $type) = @_;
71         return r(404) unless -f $path && -r _; # just in case it's a FIFO :P
72
73         open my $in, '<', $path or return;
74         my $size = -s $in;
75         my $mtime = time2str((stat(_))[9]);
76
77         if (my $ims = $env->{HTTP_IF_MODIFIED_SINCE}) {
78                 return [ 304, [], [] ] if $mtime eq $ims;
79         }
80
81         my $len = $size;
82         my $code = 200;
83         push @$h, 'Content-Type', $type;
84         if (($env->{HTTP_RANGE} || '') =~ /\bbytes=([0-9]*)-([0-9]*)\z/) {
85                 ($code, $len) = prepare_range($env, $in, $h, $1, $2, $size);
86                 return $code if ref($code);
87         }
88         push @$h, 'Content-Length', $len, 'Last-Modified', $mtime;
89         my $body = bless {
90                 initial_rd => 65536,
91                 len => $len,
92                 in => $in,
93                 path => $path,
94                 env => $env,
95         }, __PACKAGE__;
96         [ $code, $h, $body ];
97 }
98
99 # called by PSGI servers:
100 sub getline {
101         my ($self) = @_;
102         my $len = $self->{len} or return; # undef, tells server we're done
103         my $n = delete($self->{initial_rd}) // 8192;
104         $n = $len if $len < $n;
105         my $r = sysread($self->{in}, my $buf, $n);
106         if (defined $r && $r > 0) { # success!
107                 $self->{len} = $len - $r;
108                 return $buf;
109         }
110         my $m = defined $r ? "EOF with $len bytes left" : "read error: $!";
111         my $env = $self->{env};
112         $env->{'psgi.errors'}->print("$self->{path} $m\n");
113
114         # drop the client on error
115         if (my $io = $env->{'psgix.io'}) {
116                 $io->close; # this is likely PublicInbox::DS::close
117         } else { # for some PSGI servers w/o psgix.io
118                 die "dropping client socket\n";
119         }
120         undef;
121 }
122
123 sub close {} # noop, just let everything go out-of-scope
124
125 1;