From 02f9b34f398bef722159cd54a629441f861d37b7 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Wed, 1 Jan 2020 10:38:56 +0000 Subject: wwwstatic: move r(...) functions here Remove redundant "r" functions for generating short error responses. These responses will no longer be cached by clients, which is probably a good thing since most errors ought to be transient, anyways. This also fixes error responses for our cgit wrapper when static files are missing. --- lib/PublicInbox/WwwStatic.pm | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) (limited to 'lib/PublicInbox/WwwStatic.pm') diff --git a/lib/PublicInbox/WwwStatic.pm b/lib/PublicInbox/WwwStatic.pm index b8efcf62..c605e64f 100644 --- a/lib/PublicInbox/WwwStatic.pm +++ b/lib/PublicInbox/WwwStatic.pm @@ -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,7 @@ sub prepare_range { if ($len <= 0) { $code = 416; } else { - sysseek($in, $beg, SEEK_SET) or return [ 500, [], [] ]; + sysseek($in, $beg, SEEK_SET) or return r(500); push @$h, qw(Accept-Ranges bytes Content-Range); push @$h, "bytes $beg-$end/$size"; @@ -44,12 +59,16 @@ 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; @@ -64,10 +83,7 @@ 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 { -- cgit v1.2.3-24-ge0c7