]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/GitHTTPBackend.pm
No ext_urls
[public-inbox.git] / lib / PublicInbox / GitHTTPBackend.pm
index 8883ec347c18adef02a3abba7d80b7afab530c88..744324294917df02bd07166a8d4e9423714b871d 100644 (file)
@@ -1,18 +1,18 @@
-# Copyright (C) 2016-2019 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;
+use IO::Handle; # ->flush
 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);
+use Carp ();
 
 # 32 is same as the git-daemon connection limit
 my $default_limiter = PublicInbox::Qspawn::Limiter->new(32);
@@ -23,27 +23,12 @@ 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);
 
-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) = @_;
 
@@ -58,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;
@@ -77,23 +59,23 @@ 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) {
                $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
@@ -111,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)) {
@@ -130,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;
@@ -179,7 +143,22 @@ 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 $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);
+               # for ->psgi_return_init_cb
+               $ctx->{env}->{'qspawn.wcb'} = $wcb;
+               $res; # CODE or ARRAY ref
+       } else {
+               [ $code, \@h ]
+       }
 }
 
 1;