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;
10 use PublicInbox::Spawn qw(spawn);
12 # n.b. serving "description" and "cloneurl" should be innocuous enough to
13 # not cause problems. serving "config" might...
14 my @text = qw[HEAD info/refs
15 objects/info/(?:http-alternates|alternates|packs)
16 cloneurl description];
19 objects/[a-f0-9]{2}/[a-f0-9]{38}
20 objects/pack/pack-[a-f0-9]{40}\.(?:pack|idx)
23 our $ANY = join('|', @binary, @text);
24 my $BIN = join('|', @binary);
25 my $TEXT = join('|', @text);
28 [ $_[0] , [qw(Content-Type text/plain Content-Length 0) ], [] ]
32 my ($cgi, $git, $path) = @_;
33 my $service = $cgi->param('service') || '';
34 if ($service =~ /\Agit-\w+-pack\z/ || $path =~ /\Agit-\w+-pack\z/) {
35 my $ok = serve_smart($cgi, $git, $path);
40 if ($path =~ /\A(?:$BIN)\z/o) {
41 $type = 'application/octet-stream';
42 } elsif ($path =~ /\A(?:$TEXT)\z/o) {
47 my $f = "$git->{git_dir}/$path";
48 return r(404) unless -f $f && -r _;
52 # TODO: If-Modified-Since and Last-Modified
53 open my $in, '<', $f or return r(404);
58 my $env = $cgi->{env};
59 my $range = $env->{HTTP_RANGE};
60 if (defined $range && $range =~ /\bbytes=(\d*)-(\d*)\z/) {
61 ($code, $len) = prepare_range($cgi, $in, \@h, $1, $2, $size);
63 push @h, 'Content-Range', "bytes */$size";
64 return [ 416, \@h, [] ];
68 push @h, 'Content-Type', $type, 'Content-Length', $len;
70 my ($res) = @_; # Plack callback
71 my $fh = $res->([ $code, \@h ]);
75 $n = $len if $len < $n;
76 my $r = sysread($in, $buf, $n);
77 last if (!defined($r) || $r <= 0);
86 my ($cgi, $in, $h, $beg, $end, $size) = @_;
90 if ($end ne '') { # "bytes=-$end" => last N bytes
101 } elsif ($end eq '' || $end >= $size) {
104 } elsif ($end < $size) {
111 $len = $end - $beg + 1;
115 seek($in, $beg, SEEK_SET) or return [ 500, [], [] ];
116 push @$h, qw(Accept-Ranges bytes Content-Range);
117 push @$h, "bytes $beg-$end/$size";
119 # FIXME: Plack::Middleware::Deflater bug?
120 $cgi->{env}->{'psgix.no-compress'} = 1;
126 # returns undef if 403 so it falls back to dumb HTTP
128 my ($cgi, $git, $path) = @_;
129 my $env = $cgi->{env};
131 my $input = $env->{'psgi.input'};
134 my $err = $env->{'psgi.errors'};
135 my $fd = eval { fileno($input) };
136 if (defined $fd && $fd >= 0) {
139 $in = input_to_file($env) or return r(500);
142 unless (pipe($rpipe, $wpipe)) {
143 $err->print("error creating pipe: $!\n");
147 # GIT_HTTP_EXPORT_ALL, GIT_COMMITTER_NAME, GIT_COMMITTER_EMAIL
148 # may be set in the server-process and are passed as-is
149 foreach my $name (qw(QUERY_STRING
150 REMOTE_USER REMOTE_ADDR
151 HTTP_CONTENT_ENCODING
155 my $val = $env->{$name};
156 $env{$name} = $val if defined $val;
158 my $git_dir = $git->{git_dir};
159 $env{GIT_HTTP_EXPORT_ALL} = '1';
160 $env{PATH_TRANSLATED} = "$git_dir/$path";
161 my %rdr = ( 0 => fileno($in), 1 => fileno($wpipe) );
162 my $pid = spawn([qw(git http-backend)], \%env, \%rdr);
163 unless (defined $pid) {
164 $err->print("error spawning: $!\n");
167 $wpipe = $in = undef;
169 my ($vin, $fh, $res);
175 $res->(r(500)) if $res;
178 $rpipe->close; # _may_ be Danga::Socket::close
184 return if $wpid == waitpid($wpid, 0);
185 $err->print("git http-backend ($git_dir): $?\n");
190 if ($e eq 'EAGAIN') {
191 select($vin, undef, undef, undef) if defined $vin;
192 # $vin is undef on async, so this is a noop on EAGAIN
196 $err->print("git http-backend ($git_dir): $e\n");
198 my $cb = sub { # read git-http-backend output and stream to client
199 my $r = $rpipe ? $rpipe->sysread($buf, 8192, length($buf)) : 0;
200 return $fail->($!{EAGAIN} ? 'EAGAIN' : $!) unless defined $r;
201 return $end->() if $r == 0; # EOF
202 if ($fh) { # stream body from git-http-backend to HTTP client
205 } elsif ($buf =~ s/\A(.*?)\r\n\r\n//s) { # parse headers
209 foreach my $l (split(/\r\n/, $h)) {
210 my ($k, $v) = split(/:\s*/, $l, 2);
211 if ($k =~ /\AStatus\z/i) {
212 ($code) = ($v =~ /\b(\d+)\b/);
217 # write response header:
218 $fh = $res->([ $code, \@h ]);
222 } # else { keep reading ... }
224 if (my $async = $env->{'pi-httpd.async'}) {
225 $rpipe = $async->($rpipe, $cb);
226 sub { ($res) = @_ } # let Danga::Socket handle the rest.
227 } else { # synchronous loop for other PSGI servers
229 vec($vin, fileno($rpipe), 1) = 1;
232 while ($rpipe) { $cb->() }
239 my $in = IO::File->new_tmpfile;
240 my $input = $env->{'psgi.input'};
243 my $r = $input->read($buf, 8192);
244 unless (defined $r) {
245 my $err = $env->{'psgi.errors'};
246 $err->print("error reading input: $!\n");
253 $in->sysseek(0, SEEK_SET);