]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/GitHTTPBackend.pm
githttpbackend: remove ancient compatibility check
[public-inbox.git] / lib / PublicInbox / GitHTTPBackend.pm
1 # Copyright (C) 2016-2019 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 warnings;
9 use Fcntl qw(:seek);
10 use IO::Handle;
11 use HTTP::Date qw(time2str);
12 use HTTP::Status qw(status_message);
13 use PublicInbox::Qspawn;
14 use PublicInbox::Tmpfile;
15 use PublicInbox::WwwStatic;
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 my @no_cache = ('Expires', 'Fri, 01 Jan 1980 00:00:00 GMT',
36                 'Pragma', 'no-cache',
37                 'Cache-Control', 'no-cache, max-age=0, must-revalidate');
38
39 sub r ($;$) {
40         my ($code, $msg) = @_;
41         $msg ||= status_message($code);
42         my $len = length($msg);
43         [ $code, [qw(Content-Type text/plain Content-Length), $len, @no_cache],
44                 [$msg] ]
45 }
46
47 sub serve {
48         my ($env, $git, $path) = @_;
49
50         # Documentation/technical/http-protocol.txt in git.git
51         # requires one and exactly one query parameter:
52         if ($env->{QUERY_STRING} =~ /\Aservice=git-[A-Za-z0-9_]+-pack\z/ ||
53                                 $path =~ /\Agit-[A-Za-z0-9_]+-pack\z/) {
54                 my $ok = serve_smart($env, $git, $path);
55                 return $ok if $ok;
56         }
57
58         serve_dumb($env, $git, $path);
59 }
60
61 sub err ($@) {
62         my ($env, @msg) = @_;
63         $env->{'psgi.errors'}->print(@msg, "\n");
64 }
65
66 my $prev = 0;
67 my $exp;
68 sub cache_one_year {
69         my ($h) = @_;
70         my $t = time + 31536000;
71         push @$h, 'Expires', $t == $prev ? $exp : ($exp = time2str($prev = $t)),
72                 'Cache-Control', 'public, max-age=31536000';
73 }
74
75 sub serve_dumb {
76         my ($env, $git, $path) = @_;
77
78         my $h = [];
79         my $type;
80         if ($path =~ m!\Aobjects/[a-f0-9]{2}/[a-f0-9]{38}\z!) {
81                 $type = 'application/x-git-loose-object';
82                 cache_one_year($h);
83         } elsif ($path =~ m!\Aobjects/pack/pack-[a-f0-9]{40}\.pack\z!) {
84                 $type = 'application/x-git-packed-objects';
85                 cache_one_year($h);
86         } elsif ($path =~ m!\Aobjects/pack/pack-[a-f0-9]{40}\.idx\z!) {
87                 $type = 'application/x-git-packed-objects-toc';
88                 cache_one_year($h);
89         } elsif ($path =~ /\A(?:$TEXT)\z/o) {
90                 $type = 'text/plain';
91                 push @$h, @no_cache;
92         } else {
93                 return r(404);
94         }
95         $path = "$git->{git_dir}/$path";
96         PublicInbox::WwwStatic::response($env, $h, $path, $type) // r(404);
97 }
98
99 sub git_parse_hdr { # {parse_hdr} for Qspawn
100         my ($r, $bref, $dumb_args) = @_;
101         my $res = parse_cgi_headers($r, $bref) or return; # incomplete
102         $res->[0] == 403 ? serve_dumb(@$dumb_args) : $res;
103 }
104
105 # returns undef if 403 so it falls back to dumb HTTP
106 sub serve_smart {
107         my ($env, $git, $path) = @_;
108         my %env = %ENV;
109         # GIT_COMMITTER_NAME, GIT_COMMITTER_EMAIL
110         # may be set in the server-process and are passed as-is
111         foreach my $name (qw(QUERY_STRING
112                                 REMOTE_USER REMOTE_ADDR
113                                 HTTP_CONTENT_ENCODING
114                                 CONTENT_TYPE
115                                 SERVER_PROTOCOL
116                                 REQUEST_METHOD)) {
117                 my $val = $env->{$name};
118                 $env{$name} = $val if defined $val;
119         }
120         my $limiter = $git->{-httpbackend_limiter} || $default_limiter;
121         $env{GIT_HTTP_EXPORT_ALL} = '1';
122         $env{PATH_TRANSLATED} = "$git->{git_dir}/$path";
123         my $rdr = input_prepare($env) or return r(500);
124         my $qsp = PublicInbox::Qspawn->new([qw(git http-backend)], \%env, $rdr);
125         $qsp->psgi_return($env, $limiter, \&git_parse_hdr, [$env, $git, $path]);
126 }
127
128 sub input_prepare {
129         my ($env) = @_;
130
131         my $input = $env->{'psgi.input'};
132         my $fd = eval { fileno($input) };
133         if (defined $fd && $fd >= 0) {
134                 return { 0 => $fd };
135         }
136         my $id = "git-http.input.$env->{REMOTE_ADDR}:$env->{REMOTE_PORT}";
137         my $in = tmpfile($id);
138         unless (defined $in) {
139                 err($env, "could not open temporary file: $!");
140                 return;
141         }
142         my $buf;
143         while (1) {
144                 my $r = $input->read($buf, 8192);
145                 unless (defined $r) {
146                         err($env, "error reading input: $!");
147                         return;
148                 }
149                 last if $r == 0;
150                 unless (print $in $buf) {
151                         err($env, "error writing temporary file: $!");
152                         return;
153                 }
154         }
155         # ensure it's visible to git-http-backend(1):
156         unless ($in->flush) {
157                 err($env, "error writing temporary file: $!");
158                 return;
159         }
160         unless (defined(sysseek($in, 0, SEEK_SET))) {
161                 err($env, "error seeking temporary file: $!");
162                 return;
163         }
164         { 0 => $in };
165 }
166
167 sub parse_cgi_headers {
168         my ($r, $bref) = @_;
169         return r(500) unless defined $r && $r >= 0;
170         $$bref =~ s/\A(.*?)\r?\n\r?\n//s or return $r == 0 ? r(500) : undef;
171         my $h = $1;
172         my $code = 200;
173         my @h;
174         foreach my $l (split(/\r?\n/, $h)) {
175                 my ($k, $v) = split(/:\s*/, $l, 2);
176                 if ($k =~ /\AStatus\z/i) {
177                         ($code) = ($v =~ /\b([0-9]+)\b/);
178                 } else {
179                         push @h, $k, $v;
180                 }
181         }
182         [ $code, \@h ]
183 }
184
185 1;