]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/GitHTTPBackend.pm
imap+nntp: share COMPRESS implementation
[public-inbox.git] / lib / PublicInbox / GitHTTPBackend.pm
1 # Copyright (C) 2016-2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
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;
7 use strict;
8 use v5.10.1;
9 use Fcntl qw(:seek);
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);
15 use Carp ();
16
17 # 32 is same as the git-daemon connection limit
18 my $default_limiter = PublicInbox::Qspawn::Limiter->new(32);
19
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];
25
26 my @binary = qw!
27         objects/[a-f0-9]{2}/[a-f0-9]{38}
28         objects/pack/pack-[a-f0-9]{40}\.(?:pack|idx)
29         !;
30
31 our $ANY = join('|', @binary, @text, 'git-upload-pack');
32 my $BIN = join('|', @binary);
33 my $TEXT = join('|', @text);
34
35 sub serve {
36         my ($env, $git, $path) = @_;
37
38         # Documentation/technical/http-protocol.txt in git.git
39         # requires one and exactly one query parameter:
40         if ($env->{QUERY_STRING} =~ /\Aservice=git-[A-Za-z0-9_]+-pack\z/ ||
41                                 $path =~ /\Agit-[A-Za-z0-9_]+-pack\z/) {
42                 my $ok = serve_smart($env, $git, $path);
43                 return $ok if $ok;
44         }
45
46         serve_dumb($env, $git, $path);
47 }
48
49 sub ucarp { Carp::carp(@_); undef }
50
51 my $prev = 0;
52 my $exp;
53 sub cache_one_year {
54         my ($h) = @_;
55         my $t = time + 31536000;
56         push @$h, 'Expires', $t == $prev ? $exp : ($exp = time2str($prev = $t)),
57                 'Cache-Control', 'public, max-age=31536000';
58 }
59
60 sub serve_dumb {
61         my ($env, $git, $path) = @_;
62
63         my $h = [];
64         my $type;
65         if ($path =~ m!\Aobjects/[a-f0-9]{2}/[a-f0-9]{38}\z!) {
66                 $type = 'application/x-git-loose-object';
67                 cache_one_year($h);
68         } elsif ($path =~ m!\Aobjects/pack/pack-[a-f0-9]{40}\.pack\z!) {
69                 $type = 'application/x-git-packed-objects';
70                 cache_one_year($h);
71         } elsif ($path =~ m!\Aobjects/pack/pack-[a-f0-9]{40}\.idx\z!) {
72                 $type = 'application/x-git-packed-objects-toc';
73                 cache_one_year($h);
74         } elsif ($path =~ /\A(?:$TEXT)\z/o) {
75                 $type = 'text/plain';
76                 push @$h, @NO_CACHE;
77         } else {
78                 return r(404);
79         }
80         $path = "$git->{git_dir}/$path";
81         PublicInbox::WwwStatic::response($env, $h, $path, $type);
82 }
83
84 sub git_parse_hdr { # {parse_hdr} for Qspawn
85         my ($r, $bref, $dumb_args) = @_;
86         my $res = parse_cgi_headers($r, $bref) or return; # incomplete
87         $res->[0] == 403 ? serve_dumb(@$dumb_args) : $res;
88 }
89
90 # returns undef if 403 so it falls back to dumb HTTP
91 sub serve_smart {
92         my ($env, $git, $path) = @_;
93         my %env = %ENV;
94         # GIT_COMMITTER_NAME, GIT_COMMITTER_EMAIL
95         # may be set in the server-process and are passed as-is
96         foreach my $name (qw(QUERY_STRING
97                                 REMOTE_USER REMOTE_ADDR
98                                 HTTP_CONTENT_ENCODING
99                                 HTTP_GIT_PROTOCOL
100                                 CONTENT_TYPE
101                                 SERVER_PROTOCOL
102                                 REQUEST_METHOD)) {
103                 my $val = $env->{$name};
104                 $env{$name} = $val if defined $val;
105         }
106         my $limiter = $git->{-httpbackend_limiter} || $default_limiter;
107         $env{GIT_HTTP_EXPORT_ALL} = '1';
108         $env{PATH_TRANSLATED} = "$git->{git_dir}/$path";
109         my $rdr = input_prepare($env) or return r(500);
110         my $qsp = PublicInbox::Qspawn->new([qw(git http-backend)], \%env, $rdr);
111         $qsp->psgi_return($env, $limiter, \&git_parse_hdr, [$env, $git, $path]);
112 }
113
114 sub input_prepare {
115         my ($env) = @_;
116
117         my $input = $env->{'psgi.input'};
118         my $fd = eval { fileno($input) };
119         return { 0 => $fd } if (defined $fd && $fd >= 0);
120         my $id = "git-http.input.$env->{REMOTE_ADDR}:$env->{REMOTE_PORT}";
121         my $in = tmpfile($id) // return ucarp("tmpfile: $!");
122         my $buf;
123         while (1) {
124                 my $r = $input->read($buf, 8192) // return ucarp("read $!");
125                 last if $r == 0;
126                 print $in $buf // return ucarp("print: $!");
127         }
128         # ensure it's visible to git-http-backend(1):
129         $in->flush // return ucarp("flush: $!");
130         sysseek($in, 0, SEEK_SET) // return ucarp($env, "seek: $!");
131         { 0 => $in };
132 }
133
134 sub parse_cgi_headers {
135         my ($r, $bref) = @_;
136         return r(500) unless defined $r && $r >= 0;
137         $$bref =~ s/\A(.*?)\r?\n\r?\n//s or return $r == 0 ? r(500) : undef;
138         my $h = $1;
139         my $code = 200;
140         my @h;
141         foreach my $l (split(/\r?\n/, $h)) {
142                 my ($k, $v) = split(/:\s*/, $l, 2);
143                 if ($k =~ /\AStatus\z/i) {
144                         ($code) = ($v =~ /\b([0-9]+)\b/);
145                 } else {
146                         push @h, $k, $v;
147                 }
148         }
149         [ $code, \@h ]
150 }
151
152 1;