]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/GitHTTPBackend.pm
d1132fb7492c8b5039532fdc224ac9d348abcceb
[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 PublicInbox::Qspawn;
13 use PublicInbox::Tmpfile;
14 use PublicInbox::WwwStatic qw(r @NO_CACHE);
15
16 # 32 is same as the git-daemon connection limit
17 my $default_limiter = PublicInbox::Qspawn::Limiter->new(32);
18
19 # n.b. serving "description" and "cloneurl" should be innocuous enough to
20 # not cause problems.  serving "config" might...
21 my @text = qw[HEAD info/refs info/attributes
22         objects/info/(?:http-alternates|alternates|packs)
23         cloneurl description];
24
25 my @binary = qw!
26         objects/[a-f0-9]{2}/[a-f0-9]{38}
27         objects/pack/pack-[a-f0-9]{40}\.(?:pack|idx)
28         !;
29
30 our $ANY = join('|', @binary, @text, 'git-upload-pack');
31 my $BIN = join('|', @binary);
32 my $TEXT = join('|', @text);
33
34 sub serve {
35         my ($env, $git, $path) = @_;
36
37         # Documentation/technical/http-protocol.txt in git.git
38         # requires one and exactly one query parameter:
39         if ($env->{QUERY_STRING} =~ /\Aservice=git-[A-Za-z0-9_]+-pack\z/ ||
40                                 $path =~ /\Agit-[A-Za-z0-9_]+-pack\z/) {
41                 my $ok = serve_smart($env, $git, $path);
42                 return $ok if $ok;
43         }
44
45         serve_dumb($env, $git, $path);
46 }
47
48 sub err ($@) {
49         my ($env, @msg) = @_;
50         $env->{'psgi.errors'}->print(@msg, "\n");
51 }
52
53 my $prev = 0;
54 my $exp;
55 sub cache_one_year {
56         my ($h) = @_;
57         my $t = time + 31536000;
58         push @$h, 'Expires', $t == $prev ? $exp : ($exp = time2str($prev = $t)),
59                 'Cache-Control', 'public, max-age=31536000';
60 }
61
62 sub serve_dumb {
63         my ($env, $git, $path) = @_;
64
65         my $h = [];
66         my $type;
67         if ($path =~ m!\Aobjects/[a-f0-9]{2}/[a-f0-9]{38}\z!) {
68                 $type = 'application/x-git-loose-object';
69                 cache_one_year($h);
70         } elsif ($path =~ m!\Aobjects/pack/pack-[a-f0-9]{40}\.pack\z!) {
71                 $type = 'application/x-git-packed-objects';
72                 cache_one_year($h);
73         } elsif ($path =~ m!\Aobjects/pack/pack-[a-f0-9]{40}\.idx\z!) {
74                 $type = 'application/x-git-packed-objects-toc';
75                 cache_one_year($h);
76         } elsif ($path =~ /\A(?:$TEXT)\z/o) {
77                 $type = 'text/plain';
78                 push @$h, @NO_CACHE;
79         } else {
80                 return r(404);
81         }
82         $path = "$git->{git_dir}/$path";
83         PublicInbox::WwwStatic::response($env, $h, $path, $type);
84 }
85
86 sub git_parse_hdr { # {parse_hdr} for Qspawn
87         my ($r, $bref, $dumb_args) = @_;
88         my $res = parse_cgi_headers($r, $bref) or return; # incomplete
89         $res->[0] == 403 ? serve_dumb(@$dumb_args) : $res;
90 }
91
92 # returns undef if 403 so it falls back to dumb HTTP
93 sub serve_smart {
94         my ($env, $git, $path) = @_;
95         my %env = %ENV;
96         # GIT_COMMITTER_NAME, GIT_COMMITTER_EMAIL
97         # may be set in the server-process and are passed as-is
98         foreach my $name (qw(QUERY_STRING
99                                 REMOTE_USER REMOTE_ADDR
100                                 HTTP_CONTENT_ENCODING
101                                 CONTENT_TYPE
102                                 SERVER_PROTOCOL
103                                 REQUEST_METHOD)) {
104                 my $val = $env->{$name};
105                 $env{$name} = $val if defined $val;
106         }
107         my $limiter = $git->{-httpbackend_limiter} || $default_limiter;
108         $env{GIT_HTTP_EXPORT_ALL} = '1';
109         $env{PATH_TRANSLATED} = "$git->{git_dir}/$path";
110         my $rdr = input_prepare($env) or return r(500);
111         my $qsp = PublicInbox::Qspawn->new([qw(git http-backend)], \%env, $rdr);
112         $qsp->psgi_return($env, $limiter, \&git_parse_hdr, [$env, $git, $path]);
113 }
114
115 sub input_prepare {
116         my ($env) = @_;
117
118         my $input = $env->{'psgi.input'};
119         my $fd = eval { fileno($input) };
120         if (defined $fd && $fd >= 0) {
121                 return { 0 => $fd };
122         }
123         my $id = "git-http.input.$env->{REMOTE_ADDR}:$env->{REMOTE_PORT}";
124         my $in = tmpfile($id);
125         unless (defined $in) {
126                 err($env, "could not open temporary file: $!");
127                 return;
128         }
129         my $buf;
130         while (1) {
131                 my $r = $input->read($buf, 8192);
132                 unless (defined $r) {
133                         err($env, "error reading input: $!");
134                         return;
135                 }
136                 last if $r == 0;
137                 unless (print $in $buf) {
138                         err($env, "error writing temporary file: $!");
139                         return;
140                 }
141         }
142         # ensure it's visible to git-http-backend(1):
143         unless ($in->flush) {
144                 err($env, "error writing temporary file: $!");
145                 return;
146         }
147         unless (defined(sysseek($in, 0, SEEK_SET))) {
148                 err($env, "error seeking temporary file: $!");
149                 return;
150         }
151         { 0 => $in };
152 }
153
154 sub parse_cgi_headers {
155         my ($r, $bref) = @_;
156         return r(500) unless defined $r && $r >= 0;
157         $$bref =~ s/\A(.*?)\r?\n\r?\n//s or return $r == 0 ? r(500) : undef;
158         my $h = $1;
159         my $code = 200;
160         my @h;
161         foreach my $l (split(/\r?\n/, $h)) {
162                 my ($k, $v) = split(/:\s*/, $l, 2);
163                 if ($k =~ /\AStatus\z/i) {
164                         ($code) = ($v =~ /\b([0-9]+)\b/);
165                 } else {
166                         push @h, $k, $v;
167                 }
168         }
169         [ $code, \@h ]
170 }
171
172 1;