X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=lib%2FPublicInbox%2FGitHTTPBackend.pm;h=974a1831340c3581a3fc55a5e0b335b7055c22f3;hb=9ea503ef65426070303fe1929f456b3591d74d93;hp=d1132fb7492c8b5039532fdc224ac9d348abcceb;hpb=02f9b34f398bef722159cd54a629441f861d37b7;p=public-inbox.git diff --git a/lib/PublicInbox/GitHTTPBackend.pm b/lib/PublicInbox/GitHTTPBackend.pm index d1132fb7..974a1831 100644 --- a/lib/PublicInbox/GitHTTPBackend.pm +++ b/lib/PublicInbox/GitHTTPBackend.pm @@ -1,17 +1,18 @@ -# Copyright (C) 2016-2019 all contributors +# Copyright (C) all contributors # License: AGPL-3.0+ # 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; +use IO::Handle; # ->flush use HTTP::Date qw(time2str); 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); @@ -22,13 +23,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 +43,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 +59,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) { @@ -98,6 +93,7 @@ sub serve_smart { foreach my $name (qw(QUERY_STRING REMOTE_USER REMOTE_ADDR HTTP_CONTENT_ENCODING + HTTP_GIT_PROTOCOL CONTENT_TYPE SERVER_PROTOCOL REQUEST_METHOD)) { @@ -117,42 +113,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; @@ -166,7 +143,26 @@ sub parse_cgi_headers { push @h, $k, $v; } } - [ $code, \@h ] + + # fallback to WwwCoderepo if cgit 404s. Duplicating $ctx prevents + # ->finalize from the current Qspawn from using qspawn.wcb. + # This makes qspawn skip ->async_pass and causes + # PublicInbox::HTTPD::Async::event_step to close shortly after + if ($code == 404 && $ctx->{www} && !$ctx->{_coderepo_tried}++) { + my %ctx = %$ctx; + $ctx{env} = +{ %{$ctx->{env}} }; + delete $ctx->{env}->{'qspawn.wcb'}; + $ctx->{env}->{'plack.skip-deflater'} = 1; # prevent 2x gzip + my $res = $ctx->{www}->coderepo->srv(\%ctx); + if (ref($res) eq 'CODE') { + $res->(delete $ctx{env}->{'qspawn.wcb'}); + } else { # ref($res) eq 'ARRAY' + $ctx->{env}->{'qspawn.wcb'} = $ctx{env}->{'qspawn.wcb'}; + } + $res; # non ARRAY ref for ->psgi_return_init_cb + } else { + [ $code, \@h ] + } } 1;