]> Sergey Matveev's repositories - public-inbox.git/commitdiff
wwwstatic: move r(...) functions here
authorEric Wong <e@80x24.org>
Wed, 1 Jan 2020 10:38:56 +0000 (10:38 +0000)
committerEric Wong <e@80x24.org>
Wed, 1 Jan 2020 19:10:55 +0000 (19:10 +0000)
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/Cgit.pm
lib/PublicInbox/GitHTTPBackend.pm
lib/PublicInbox/WWW.pm
lib/PublicInbox/WwwHighlight.pm
lib/PublicInbox/WwwStatic.pm

index 3623943882fdb9f95a50c90d0d397497606af4a9..c0b1a73b625edb420ebae33a34cc8fd9be34f897 100644 (file)
@@ -10,13 +10,12 @@ use strict;
 use PublicInbox::GitHTTPBackend;
 use PublicInbox::Git;
 # not bothering with Exporter for a one-off
-*r = *PublicInbox::GitHTTPBackend::r;
 *input_prepare = *PublicInbox::GitHTTPBackend::input_prepare;
 *parse_cgi_headers = *PublicInbox::GitHTTPBackend::parse_cgi_headers;
 *serve = *PublicInbox::GitHTTPBackend::serve;
 use warnings;
 use PublicInbox::Qspawn;
-use PublicInbox::WwwStatic;
+use PublicInbox::WwwStatic qw(r);
 use Plack::MIME;
 
 sub locate_cgit ($) {
index 8883ec347c18adef02a3abba7d80b7afab530c88..d1132fb7492c8b5039532fdc224ac9d348abcceb 100644 (file)
@@ -9,10 +9,9 @@ use warnings;
 use Fcntl qw(:seek);
 use IO::Handle;
 use HTTP::Date qw(time2str);
-use HTTP::Status qw(status_message);
 use PublicInbox::Qspawn;
 use PublicInbox::Tmpfile;
-use PublicInbox::WwwStatic;
+use PublicInbox::WwwStatic qw(r @NO_CACHE);
 
 # 32 is same as the git-daemon connection limit
 my $default_limiter = PublicInbox::Qspawn::Limiter->new(32);
@@ -32,18 +31,6 @@ our $ANY = join('|', @binary, @text, 'git-upload-pack');
 my $BIN = join('|', @binary);
 my $TEXT = join('|', @text);
 
-my @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);
-       my $len = length($msg);
-       [ $code, [qw(Content-Type text/plain Content-Length), $len, @no_cache],
-               [$msg] ]
-}
-
 sub serve {
        my ($env, $git, $path) = @_;
 
@@ -88,12 +75,12 @@ sub serve_dumb {
                cache_one_year($h);
        } elsif ($path =~ /\A(?:$TEXT)\z/o) {
                $type = 'text/plain';
-               push @$h, @no_cache;
+               push @$h, @NO_CACHE;
        } else {
                return r(404);
        }
        $path = "$git->{git_dir}/$path";
-       PublicInbox::WwwStatic::response($env, $h, $path, $type) // r(404);
+       PublicInbox::WwwStatic::response($env, $h, $path, $type);
 }
 
 sub git_parse_hdr { # {parse_hdr} for Qspawn
index 13b66ee62be8413344435785809e861e82338930..99f9f1dc258d2af531349e22eec173c64abd4119 100644 (file)
@@ -22,6 +22,7 @@ use PublicInbox::MID qw(mid_escape);
 require PublicInbox::Git;
 use PublicInbox::GitHTTPBackend;
 use PublicInbox::UserContent;
+use PublicInbox::WwwStatic qw(r);
 
 # TODO: consider a routing tree now that we have more endpoints:
 our $INBOX_RE = qr!\A/([\w\-][\w\.\-]*)!;
@@ -83,7 +84,7 @@ sub call {
                }
        }
        elsif ($method !~ /\AGET|HEAD\z/) {
-               return r(405, 'Method Not Allowed');
+               return r(405);
        }
 
        # top-level indices and feeds
@@ -176,12 +177,9 @@ sub r404 {
                require PublicInbox::ExtMsg;
                return PublicInbox::ExtMsg::ext_msg($ctx);
        }
-       r(404, 'Not Found');
+       r(404);
 }
 
-# simple response for errors
-sub r { [ $_[0], ['Content-Type' => 'text/plain'], [ join(' ', @_, "\n") ] ] }
-
 sub news_cgit_fallback ($) {
        my ($ctx) = @_;
        my $www = $ctx->{www};
index bc349f8a7a1e633617be39ef6e8028af05a2290f..6312edaef19258497f69cc696763470cc1ed681d 100644 (file)
@@ -22,22 +22,15 @@ package PublicInbox::WwwHighlight;
 use strict;
 use warnings;
 use bytes (); # only for bytes::length
-use HTTP::Status qw(status_message);
 use parent qw(PublicInbox::HlMod);
 use PublicInbox::Linkify qw();
 use PublicInbox::Hval qw(ascii_html);
+use PublicInbox::WwwStatic qw(r);
 
 # TODO: support highlight(1) for distros which don't package the
 # SWIG extension.  Also, there may be admins who don't want to
 # have ugly SWIG-generated code in a long-lived Perl process.
 
-sub r ($) {
-       my ($code) = @_;
-       my $msg = status_message($code);
-       my $len = length($msg);
-       [ $code, [qw(Content-Type text/plain Content-Length), $len], [$msg] ]
-}
-
 # another slurp API hogging up all my memory :<
 # This is capped by whatever the PSGI server allows,
 # $ENV{GIT_HTTP_MAX_REQUEST_BUFFER} for PublicInbox::HTTP (10 MB)
index b8efcf625a184d03507161f570787f4547032f7a..c605e64f51b1aa3af868eaad4493e3d62830f7ff 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,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 {