diff options
Diffstat (limited to 'lib/PublicInbox/GitHTTPBackend.pm')
-rw-r--r-- | lib/PublicInbox/GitHTTPBackend.pm | 83 |
1 files changed, 36 insertions, 47 deletions
diff --git a/lib/PublicInbox/GitHTTPBackend.pm b/lib/PublicInbox/GitHTTPBackend.pm index b9a3e4f5..396aa783 100644 --- a/lib/PublicInbox/GitHTTPBackend.pm +++ b/lib/PublicInbox/GitHTTPBackend.pm @@ -1,20 +1,22 @@ -# Copyright (C) 2016-2021 all contributors <meta@public-inbox.org> +# Copyright (C) all contributors <meta@public-inbox.org> # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt> # when no endpoints match, fallback to this and serve a static file # or smart HTTP. This is our wrapper for git-http-backend(1) package PublicInbox::GitHTTPBackend; use strict; -use warnings; +use v5.10.1; use Fcntl qw(:seek); use IO::Handle; # ->flush use HTTP::Date qw(time2str); +use PublicInbox::Limiter; use PublicInbox::Qspawn; use PublicInbox::Tmpfile; use PublicInbox::WwwStatic qw(r @NO_CACHE); +use Carp (); # 32 is same as the git-daemon connection limit -my $default_limiter = PublicInbox::Qspawn::Limiter->new(32); +my $default_limiter = PublicInbox::Limiter->new(32); # n.b. serving "description" and "cloneurl" should be innocuous enough to # not cause problems. serving "config" might... @@ -22,13 +24,10 @@ my @text = qw[HEAD info/refs info/attributes objects/info/(?:http-alternates|alternates|packs) cloneurl description]; -my @binary = qw! - objects/[a-f0-9]{2}/[a-f0-9]{38} - objects/pack/pack-[a-f0-9]{40}\.(?:pack|idx) - !; +my @binary = ('objects/[a-f0-9]{2}/[a-f0-9]{38,62}', + 'objects/pack/pack-[a-f0-9]{40,64}\.(?:pack|idx)'); our $ANY = join('|', @binary, @text, 'git-upload-pack'); -my $BIN = join('|', @binary); my $TEXT = join('|', @text); sub serve { @@ -45,10 +44,7 @@ sub serve { serve_dumb($env, $git, $path); } -sub err ($@) { - my ($env, @msg) = @_; - $env->{'psgi.errors'}->print(@msg, "\n"); -} +sub ucarp { Carp::carp(@_); undef } my $prev = 0; my $exp; @@ -64,13 +60,13 @@ sub serve_dumb { my $h = []; my $type; - if ($path =~ m!\Aobjects/[a-f0-9]{2}/[a-f0-9]{38}\z!) { + if ($path =~ m!\Aobjects/[a-f0-9]{2}/[a-f0-9]{38,62}\z!) { $type = 'application/x-git-loose-object'; cache_one_year($h); - } elsif ($path =~ m!\Aobjects/pack/pack-[a-f0-9]{40}\.pack\z!) { + } elsif ($path =~ m!\Aobjects/pack/pack-[a-f0-9]{40,64}\.pack\z!) { $type = 'application/x-git-packed-objects'; cache_one_year($h); - } elsif ($path =~ m!\Aobjects/pack/pack-[a-f0-9]{40}\.idx\z!) { + } elsif ($path =~ m!\Aobjects/pack/pack-[a-f0-9]{40,64}\.idx\z!) { $type = 'application/x-git-packed-objects-toc'; cache_one_year($h); } elsif ($path =~ /\A(?:$TEXT)\z/o) { @@ -83,10 +79,10 @@ sub serve_dumb { PublicInbox::WwwStatic::response($env, $h, $path, $type); } -sub git_parse_hdr { # {parse_hdr} for Qspawn - my ($r, $bref, $dumb_args) = @_; +sub ghb_parse_hdr { # header parser for Qspawn + my ($r, $bref, @dumb_args) = @_; my $res = parse_cgi_headers($r, $bref) or return; # incomplete - $res->[0] == 403 ? serve_dumb(@$dumb_args) : $res; + $res->[0] == 403 ? serve_dumb(@dumb_args) : $res; } # returns undef if 403 so it falls back to dumb HTTP @@ -109,8 +105,9 @@ sub serve_smart { $env{GIT_HTTP_EXPORT_ALL} = '1'; $env{PATH_TRANSLATED} = "$git->{git_dir}/$path"; my $rdr = input_prepare($env) or return r(500); + $rdr->{quiet} = 1; my $qsp = PublicInbox::Qspawn->new([qw(git http-backend)], \%env, $rdr); - $qsp->psgi_return($env, $limiter, \&git_parse_hdr, [$env, $git, $path]); + $qsp->psgi_yield($env, $limiter, \&ghb_parse_hdr, $env, $git, $path); } sub input_prepare { @@ -118,42 +115,23 @@ sub input_prepare { my $input = $env->{'psgi.input'}; my $fd = eval { fileno($input) }; - if (defined $fd && $fd >= 0) { - return { 0 => $fd }; - } + return { 0 => $fd } if (defined $fd && $fd >= 0); my $id = "git-http.input.$env->{REMOTE_ADDR}:$env->{REMOTE_PORT}"; - my $in = tmpfile($id); - unless (defined $in) { - err($env, "could not open temporary file: $!"); - return; - } + my $in = tmpfile($id) // return ucarp("tmpfile: $!"); my $buf; while (1) { - my $r = $input->read($buf, 8192); - unless (defined $r) { - err($env, "error reading input: $!"); - return; - } + my $r = $input->read($buf, 8192) // return ucarp("read $!"); last if $r == 0; - unless (print $in $buf) { - err($env, "error writing temporary file: $!"); - return; - } + print $in $buf // return ucarp("print: $!"); } # ensure it's visible to git-http-backend(1): - unless ($in->flush) { - err($env, "error writing temporary file: $!"); - return; - } - unless (defined(sysseek($in, 0, SEEK_SET))) { - err($env, "error seeking temporary file: $!"); - return; - } + $in->flush // return ucarp("flush: $!"); + sysseek($in, 0, SEEK_SET) // return ucarp($env, "seek: $!"); { 0 => $in }; } -sub parse_cgi_headers { - my ($r, $bref) = @_; +sub parse_cgi_headers { # {parse_hdr} for Qspawn + my ($r, $bref, $ctx) = @_; return r(500) unless defined $r && $r >= 0; $$bref =~ s/\A(.*?)\r?\n\r?\n//s or return $r == 0 ? r(500) : undef; my $h = $1; @@ -167,7 +145,18 @@ sub parse_cgi_headers { push @h, $k, $v; } } - [ $code, \@h ] + + # fallback to WwwCoderepo if cgit 404s + if ($code == 404 && $ctx->{www} && !$ctx->{_coderepo_tried}++) { + my $wcb = delete $ctx->{env}->{'qspawn.wcb'}; + $ctx->{env}->{'plack.skip-deflater'} = 1; # prevent 2x gzip + $ctx->{env}->{'qspawn.fallback'} = $code; + my $res = $ctx->{www}->coderepo->srv($ctx); + $ctx->{env}->{'qspawn.wcb'} = $wcb; + $res; # CODE or ARRAY ref + } else { + [ $code, \@h ] + } } 1; |