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.
use PublicInbox::GitHTTPBackend;
use PublicInbox::Git;
# not bothering with Exporter for a one-off
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;
*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 ($) {
use Plack::MIME;
sub locate_cgit ($) {
use Fcntl qw(:seek);
use IO::Handle;
use HTTP::Date qw(time2str);
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::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 is same as the git-daemon connection limit
my $default_limiter = PublicInbox::Qspawn::Limiter->new(32);
my $BIN = join('|', @binary);
my $TEXT = join('|', @text);
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) = @_;
sub serve {
my ($env, $git, $path) = @_;
cache_one_year($h);
} elsif ($path =~ /\A(?:$TEXT)\z/o) {
$type = 'text/plain';
cache_one_year($h);
} elsif ($path =~ /\A(?:$TEXT)\z/o) {
$type = 'text/plain';
} else {
return r(404);
}
$path = "$git->{git_dir}/$path";
} 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
}
sub git_parse_hdr { # {parse_hdr} for Qspawn
require PublicInbox::Git;
use PublicInbox::GitHTTPBackend;
use PublicInbox::UserContent;
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\.\-]*)!;
# TODO: consider a routing tree now that we have more endpoints:
our $INBOX_RE = qr!\A/([\w\-][\w\.\-]*)!;
}
}
elsif ($method !~ /\AGET|HEAD\z/) {
}
}
elsif ($method !~ /\AGET|HEAD\z/) {
- return r(405, 'Method Not Allowed');
}
# top-level indices and feeds
}
# top-level indices and feeds
require PublicInbox::ExtMsg;
return PublicInbox::ExtMsg::ext_msg($ctx);
}
require PublicInbox::ExtMsg;
return PublicInbox::ExtMsg::ext_msg($ctx);
}
-# simple response for errors
-sub r { [ $_[0], ['Content-Type' => 'text/plain'], [ join(' ', @_, "\n") ] ] }
-
sub news_cgit_fallback ($) {
my ($ctx) = @_;
my $www = $ctx->{www};
sub news_cgit_fallback ($) {
my ($ctx) = @_;
my $www = $ctx->{www};
use strict;
use warnings;
use bytes (); # only for bytes::length
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 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.
# 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)
# 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)
package PublicInbox::WwwStatic;
use strict;
package PublicInbox::WwwStatic;
use strict;
+use parent qw(Exporter);
use Fcntl qw(:seek);
use HTTP::Date qw(time2str);
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) = @_;
sub prepare_range {
my ($env, $in, $h, $beg, $end, $size) = @_;
if ($len <= 0) {
$code = 416;
} else {
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";
push @$h, qw(Accept-Ranges bytes Content-Range);
push @$h, "bytes $beg-$end/$size";
$env->{'psgix.no-compress'} = 1;
}
}
$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) = @_;
($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;
open my $in, '<', $path or return;
my $size = -s $in;
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);
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 {
}
push @$h, 'Content-Length', $len, 'Last-Modified', $mtime;
my $body = bless {