1 # Copyright (C) all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # when no endpoints match, fallback to this and serve a static file
5 # or smart HTTP. This is our wrapper for git-http-backend(1)
6 package PublicInbox::GitHTTPBackend;
10 use IO::Handle; # ->flush
11 use HTTP::Date qw(time2str);
12 use PublicInbox::Qspawn;
13 use PublicInbox::Tmpfile;
14 use PublicInbox::WwwStatic qw(r @NO_CACHE);
17 # 32 is same as the git-daemon connection limit
18 my $default_limiter = PublicInbox::Qspawn::Limiter->new(32);
20 # n.b. serving "description" and "cloneurl" should be innocuous enough to
21 # not cause problems. serving "config" might...
22 my @text = qw[HEAD info/refs info/attributes
23 objects/info/(?:http-alternates|alternates|packs)
24 cloneurl description];
26 my @binary = ('objects/[a-f0-9]{2}/[a-f0-9]{38,62}',
27 'objects/pack/pack-[a-f0-9]{40,64}\.(?:pack|idx)');
29 our $ANY = join('|', @binary, @text, 'git-upload-pack');
30 my $BIN = join('|', @binary);
31 my $TEXT = join('|', @text);
34 my ($env, $git, $path) = @_;
36 # Documentation/technical/http-protocol.txt in git.git
37 # requires one and exactly one query parameter:
38 if ($env->{QUERY_STRING} =~ /\Aservice=git-[A-Za-z0-9_]+-pack\z/ ||
39 $path =~ /\Agit-[A-Za-z0-9_]+-pack\z/) {
40 my $ok = serve_smart($env, $git, $path);
44 serve_dumb($env, $git, $path);
47 sub ucarp { Carp::carp(@_); undef }
53 my $t = time + 31536000;
54 push @$h, 'Expires', $t == $prev ? $exp : ($exp = time2str($prev = $t)),
55 'Cache-Control', 'public, max-age=31536000';
59 my ($env, $git, $path) = @_;
63 if ($path =~ m!\Aobjects/[a-f0-9]{2}/[a-f0-9]{38,62}\z!) {
64 $type = 'application/x-git-loose-object';
66 } elsif ($path =~ m!\Aobjects/pack/pack-[a-f0-9]{40,64}\.pack\z!) {
67 $type = 'application/x-git-packed-objects';
69 } elsif ($path =~ m!\Aobjects/pack/pack-[a-f0-9]{40,64}\.idx\z!) {
70 $type = 'application/x-git-packed-objects-toc';
72 } elsif ($path =~ /\A(?:$TEXT)\z/o) {
78 $path = "$git->{git_dir}/$path";
79 PublicInbox::WwwStatic::response($env, $h, $path, $type);
82 sub git_parse_hdr { # {parse_hdr} for Qspawn
83 my ($r, $bref, $dumb_args) = @_;
84 my $res = parse_cgi_headers($r, $bref) or return; # incomplete
85 $res->[0] == 403 ? serve_dumb(@$dumb_args) : $res;
88 # returns undef if 403 so it falls back to dumb HTTP
90 my ($env, $git, $path) = @_;
92 # GIT_COMMITTER_NAME, GIT_COMMITTER_EMAIL
93 # may be set in the server-process and are passed as-is
94 foreach my $name (qw(QUERY_STRING
95 REMOTE_USER REMOTE_ADDR
101 my $val = $env->{$name};
102 $env{$name} = $val if defined $val;
104 my $limiter = $git->{-httpbackend_limiter} || $default_limiter;
105 $env{GIT_HTTP_EXPORT_ALL} = '1';
106 $env{PATH_TRANSLATED} = "$git->{git_dir}/$path";
107 my $rdr = input_prepare($env) or return r(500);
108 my $qsp = PublicInbox::Qspawn->new([qw(git http-backend)], \%env, $rdr);
109 $qsp->psgi_return($env, $limiter, \&git_parse_hdr, [$env, $git, $path]);
115 my $input = $env->{'psgi.input'};
116 my $fd = eval { fileno($input) };
117 return { 0 => $fd } if (defined $fd && $fd >= 0);
118 my $id = "git-http.input.$env->{REMOTE_ADDR}:$env->{REMOTE_PORT}";
119 my $in = tmpfile($id) // return ucarp("tmpfile: $!");
122 my $r = $input->read($buf, 8192) // return ucarp("read $!");
124 print $in $buf // return ucarp("print: $!");
126 # ensure it's visible to git-http-backend(1):
127 $in->flush // return ucarp("flush: $!");
128 sysseek($in, 0, SEEK_SET) // return ucarp($env, "seek: $!");
132 sub parse_cgi_headers {
133 my ($r, $bref, $ctx) = @_;
134 return r(500) unless defined $r && $r >= 0;
135 $$bref =~ s/\A(.*?)\r?\n\r?\n//s or return $r == 0 ? r(500) : undef;
139 foreach my $l (split(/\r?\n/, $h)) {
140 my ($k, $v) = split(/:\s*/, $l, 2);
141 if ($k =~ /\AStatus\z/i) {
142 ($code) = ($v =~ /\b([0-9]+)\b/);
148 # fallback to WwwCoderepo if cgit 404s. Duplicating $ctx prevents
149 # ->finalize from the current Qspawn from using qspawn.wcb
150 if ($code == 404 && $ctx->{www} && !$ctx->{_coderepo_tried}++) {
152 $ctx{env} = +{ %{$ctx->{env}} };
153 delete $ctx->{env}->{'qspawn.wcb'};
154 $ctx->{env}->{'plack.skip-deflater'} = 1; # prevent 2x gzip
155 my $res = $ctx->{www}->coderepo->srv(\%ctx);
156 if (ref($res) eq 'CODE') {
157 $res->(delete $ctx{env}->{'qspawn.wcb'});
158 } else { # ref($res) eq 'ARRAY'
159 $ctx->{env}->{'qspawn.wcb'} = $ctx{env}->{'qspawn.wcb'};
161 $res; # non ARRAY ref for ->psgi_return_init_cb