1 # Copyright (C) 2016 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
6 package PublicInbox::GitHTTPBackend;
11 use HTTP::Date qw(time2str);
12 use HTTP::Status qw(status_message);
13 use PublicInbox::Qspawn;
15 # n.b. serving "description" and "cloneurl" should be innocuous enough to
16 # not cause problems. serving "config" might...
17 my @text = qw[HEAD info/refs
18 objects/info/(?:http-alternates|alternates|packs)
19 cloneurl description];
22 objects/[a-f0-9]{2}/[a-f0-9]{38}
23 objects/pack/pack-[a-f0-9]{40}\.(?:pack|idx)
26 our $ANY = join('|', @binary, @text, 'git-upload-pack');
27 my $BIN = join('|', @binary);
28 my $TEXT = join('|', @text);
30 my @no_cache = ('Expires', 'Fri, 01 Jan 1980 00:00:00 GMT',
32 'Cache-Control', 'no-cache, max-age=0, must-revalidate');
35 my ($code, $msg) = @_;
36 $msg ||= status_message($code);
37 my $len = length($msg);
38 [ $code, [qw(Content-Type text/plain Content-Length), $len, @no_cache],
43 my ($env, $git, $path) = @_;
45 # Documentation/technical/http-protocol.txt in git.git
46 # requires one and exactly one query parameter:
47 if ($env->{QUERY_STRING} =~ /\Aservice=git-\w+-pack\z/ ||
48 $path =~ /\Agit-\w+-pack\z/) {
49 my $ok = serve_smart($env, $git, $path);
53 serve_dumb($env, $git, $path);
58 $env->{'psgi.errors'}->print(@msg, "\n");
62 if (my $io = $_[0]->{'psgix.io'}) {
63 $io->close; # this is Danga::Socket::close
68 my ($env, $git, $path) = @_;
72 if ($path =~ /\A(?:$BIN)\z/o) {
73 $type = 'application/octet-stream';
74 push @h, 'Expires', time2str(time + 31536000);
75 push @h, 'Cache-Control', 'public, max-age=31536000';
76 } elsif ($path =~ /\A(?:$TEXT)\z/o) {
83 my $f = (ref $git ? $git->{git_dir} : $git) . '/' . $path;
84 return r(404) unless -f $f && -r _; # just in case it's a FIFO :P
88 # TODO: If-Modified-Since and Last-Modified?
89 open my $in, '<', $f or return r(404);
92 push @h, 'Content-Type', $type;
93 if (($env->{HTTP_RANGE} || '') =~ /\bbytes=(\d*)-(\d*)\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, $in, $h, $beg, $end, $size) = @_;
126 if ($end ne '') { # "bytes=-$end" => last N bytes
128 $beg = 0 if $beg < 0;
137 } elsif ($end eq '' || $end >= $size) {
140 } elsif ($end < $size) {
147 $len = $end - $beg + 1;
151 sysseek($in, $beg, SEEK_SET) or return [ 500, [], [] ];
152 push @$h, qw(Accept-Ranges bytes Content-Range);
153 push @$h, "bytes $beg-$end/$size";
155 # FIXME: Plack::Middleware::Deflater bug?
156 $env->{'psgix.no-compress'} = 1;
162 # returns undef if 403 so it falls back to dumb HTTP
164 my ($env, $git, $path) = @_;
165 my $in = $env->{'psgi.input'};
166 my $fd = eval { fileno($in) };
167 unless (defined $fd && $fd >= 0) {
168 $in = input_to_file($env) or return r(500);
171 # GIT_COMMITTER_NAME, GIT_COMMITTER_EMAIL
172 # may be set in the server-process and are passed as-is
173 foreach my $name (qw(QUERY_STRING
174 REMOTE_USER REMOTE_ADDR
175 HTTP_CONTENT_ENCODING
179 my $val = $env->{$name};
180 $env{$name} = $val if defined $val;
182 my $git_dir = ref $git ? $git->{git_dir} : $git;
183 $env{GIT_HTTP_EXPORT_ALL} = '1';
184 $env{PATH_TRANSLATED} = "$git_dir/$path";
185 my %rdr = ( 0 => fileno($in) );
186 my $x = PublicInbox::Qspawn->new([qw(git http-backend)], \%env, \%rdr);
189 if (my $err = $x->finish) {
190 err($env, "git http-backend ($git_dir): $err");
192 $fh->close if $fh; # async-only
195 # Danga::Socket users, we queue up the read_enable callback to
196 # fire after pending writes are complete:
199 my $r = sysread($rpipe, $buf, 1024, length($buf));
200 return if !defined($r) && ($!{EINTR} || $!{EAGAIN});
201 return r(500, 'http-backend error') unless $r;
202 $r = parse_cgi_headers(\$buf) or return; # incomplete headers
203 $r->[0] == 403 ? serve_dumb($env, $git, $path) : $r;
206 my $async = $env->{'pi-httpd.async'};
207 my $io = $env->{'psgix.io'};
209 my $r = $rd_hdr->() or return;
211 if (scalar(@$r) == 3) { # error:
213 $async->close; # calls rpipe->close
222 return $async->async_pass($io, $fh, \$buf);
225 # for synchronous PSGI servers
226 require PublicInbox::GetlineBody;
227 $r->[2] = PublicInbox::GetlineBody->new($rpipe, $end, $buf);
233 # hopefully this doesn't break any middlewares,
234 # holding the input here is a waste of FDs and memory
235 $env->{'psgi.input'} = undef;
237 $x->start(sub { # may run later, much later...
241 $async = $async->($rpipe, $cb, $end);
242 } else { # generic PSGI
243 $cb->() while $rd_hdr;
251 my $in = IO::File->new_tmpfile;
252 my $input = $env->{'psgi.input'};
255 my $r = $input->read($buf, 8192);
256 unless (defined $r) {
257 err($env, "error reading input: $!");
264 $in->sysseek(0, SEEK_SET);
268 sub parse_cgi_headers {
270 $$bref =~ s/\A(.*?)\r\n\r\n//s or return;
274 foreach my $l (split(/\r\n/, $h)) {
275 my ($k, $v) = split(/:\s*/, $l, 2);
276 if ($k =~ /\AStatus\z/i) {
277 ($code) = ($v =~ /\b(\d+)\b/);