1 # Copyright (C) 2016-2018 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;
11 use HTTP::Date qw(time2str);
12 use HTTP::Status qw(status_message);
14 use PublicInbox::Qspawn;
16 # 32 is same as the git-daemon connection limit
17 my $default_limiter = PublicInbox::Qspawn::Limiter->new(32);
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];
26 objects/[a-f0-9]{2}/[a-f0-9]{38}
27 objects/pack/pack-[a-f0-9]{40}\.(?:pack|idx)
30 our $ANY = join('|', @binary, @text, 'git-upload-pack');
31 my $BIN = join('|', @binary);
32 my $TEXT = join('|', @text);
34 my @no_cache = ('Expires', 'Fri, 01 Jan 1980 00:00:00 GMT',
36 'Cache-Control', 'no-cache, max-age=0, must-revalidate');
39 my ($code, $msg) = @_;
40 $msg ||= status_message($code);
41 my $len = length($msg);
42 [ $code, [qw(Content-Type text/plain Content-Length), $len, @no_cache],
47 my ($env, $git, $path) = @_;
49 # XXX compatibility... ugh, can we stop supporting this?
50 $git = PublicInbox::Git->new($git) unless ref($git);
52 # Documentation/technical/http-protocol.txt in git.git
53 # requires one and exactly one query parameter:
54 if ($env->{QUERY_STRING} =~ /\Aservice=git-[A-Za-z0-9_]+-pack\z/ ||
55 $path =~ /\Agit-[A-Za-z0-9_]+-pack\z/) {
56 my $ok = serve_smart($env, $git, $path);
60 serve_dumb($env, $git, $path);
65 $env->{'psgi.errors'}->print(@msg, "\n");
69 if (my $io = $_[0]->{'psgix.io'}) {
70 $io->close; # this is PublicInbox::DS::close
78 my $t = time + 31536000;
79 push @$h, 'Expires', $t == $prev ? $exp : ($exp = time2str($prev = $t)),
80 'Cache-Control', 'public, max-age=31536000';
83 sub static_result ($$$$) {
84 my ($env, $h, $f, $type) = @_;
85 return r(404) unless -f $f && -r _; # just in case it's a FIFO :P
87 # TODO: If-Modified-Since and Last-Modified?
88 open my $in, '<', $f or return r(404);
92 push @$h, 'Content-Type', $type;
93 if (($env->{HTTP_RANGE} || '') =~ /\bbytes=([0-9]*)-([0-9]*)\z/) {
94 ($code, $len) = prepare_range($env, $in, $h, $1, $2, $size);
96 push @$h, 'Content-Range', "bytes */$size";
97 return [ 416, $h, [] ];
100 push @$h, 'Content-Length', $len;
102 [ $code, $h, Plack::Util::inline_object(close => sub { close $in },
105 $n = $len if $len < $n;
106 my $r = sysread($in, my $buf, $n);
108 err($env, "$f read error: $!");
110 err($env, "$f EOF with $len bytes left");
122 my ($env, $git, $path) = @_;
126 if ($path =~ m!\Aobjects/[a-f0-9]{2}/[a-f0-9]{38}\z!) {
127 $type = 'application/x-git-loose-object';
129 } elsif ($path =~ m!\Aobjects/pack/pack-[a-f0-9]{40}\.pack\z!) {
130 $type = 'application/x-git-packed-objects';
132 } elsif ($path =~ m!\Aobjects/pack/pack-[a-f0-9]{40}\.idx\z!) {
133 $type = 'application/x-git-packed-objects-toc';
135 } elsif ($path =~ /\A(?:$TEXT)\z/o) {
136 $type = 'text/plain';
142 static_result($env, $h, "$git->{git_dir}/$path", $type);
146 my ($env, $in, $h, $beg, $end, $size) = @_;
150 if ($end ne '') { # "bytes=-$end" => last N bytes
152 $beg = 0 if $beg < 0;
161 } elsif ($end eq '' || $end >= $size) {
164 } elsif ($end < $size) {
171 $len = $end - $beg + 1;
175 sysseek($in, $beg, SEEK_SET) or return [ 500, [], [] ];
176 push @$h, qw(Accept-Ranges bytes Content-Range);
177 push @$h, "bytes $beg-$end/$size";
179 # FIXME: Plack::Middleware::Deflater bug?
180 $env->{'psgix.no-compress'} = 1;
186 # returns undef if 403 so it falls back to dumb HTTP
188 my ($env, $git, $path) = @_;
190 # GIT_COMMITTER_NAME, GIT_COMMITTER_EMAIL
191 # may be set in the server-process and are passed as-is
192 foreach my $name (qw(QUERY_STRING
193 REMOTE_USER REMOTE_ADDR
194 HTTP_CONTENT_ENCODING
198 my $val = $env->{$name};
199 $env{$name} = $val if defined $val;
201 my $limiter = $git->{-httpbackend_limiter} || $default_limiter;
202 $env{GIT_HTTP_EXPORT_ALL} = '1';
203 $env{PATH_TRANSLATED} = "$git->{git_dir}/$path";
204 my $rdr = input_prepare($env) or return r(500);
205 my $qsp = PublicInbox::Qspawn->new([qw(git http-backend)], \%env, $rdr);
206 $qsp->psgi_return($env, $limiter, sub {
208 my $res = parse_cgi_headers($r, $bref) or return; # incomplete
209 $res->[0] == 403 ? serve_dumb($env, $git, $path) : $res;
216 my $input = $env->{'psgi.input'};
217 my $fd = eval { fileno($input) };
218 if (defined $fd && $fd >= 0) {
221 open(my $in, '+>', undef);
222 unless (defined $in) {
223 err($env, "could not open temporary file: $!");
228 my $r = $input->read($buf, 8192);
229 unless (defined $r) {
230 err($env, "error reading input: $!");
236 my $w = syswrite($in, $buf, $r, $off);
241 err($env, "error writing temporary file: $!");
246 unless (defined(sysseek($in, 0, SEEK_SET))) {
247 err($env, "error seeking temporary file: $!");
250 { 0 => fileno($in), -hold => $in };
253 sub parse_cgi_headers {
255 return r(500) unless defined $r && $r >= 0;
256 $$bref =~ s/\A(.*?)\r?\n\r?\n//s or return $r == 0 ? r(500) : undef;
260 foreach my $l (split(/\r?\n/, $h)) {
261 my ($k, $v) = split(/:\s*/, $l, 2);
262 if ($k =~ /\AStatus\z/i) {
263 ($code) = ($v =~ /\b([0-9]+)\b/);