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 PublicInbox::Spawn qw(spawn);
13 # n.b. serving "description" and "cloneurl" should be innocuous enough to
14 # not cause problems. serving "config" might...
15 my @text = qw[HEAD info/refs
16 objects/info/(?:http-alternates|alternates|packs)
17 cloneurl description];
20 objects/[a-f0-9]{2}/[a-f0-9]{38}
21 objects/pack/pack-[a-f0-9]{40}\.(?:pack|idx)
24 our $ANY = join('|', @binary, @text);
25 my $BIN = join('|', @binary);
26 my $TEXT = join('|', @text);
29 [ $_[0] , [qw(Content-Type text/plain Content-Length 0) ], [] ]
33 my ($cgi, $git, $path) = @_;
34 my $service = $cgi->param('service') || '';
35 if ($service =~ /\Agit-\w+-pack\z/ || $path =~ /\Agit-\w+-pack\z/) {
36 my $ok = serve_smart($cgi, $git, $path);
41 if ($path =~ /\A(?:$BIN)\z/o) {
42 $type = 'application/octet-stream';
43 } elsif ($path =~ /\A(?:$TEXT)\z/o) {
48 my $f = "$git->{git_dir}/$path";
49 return r(404) unless -f $f && -r _;
53 # TODO: If-Modified-Since and Last-Modified
54 open my $in, '<', $f or return r(404);
59 my $env = $cgi->{env};
60 my $range = $env->{HTTP_RANGE};
61 if (defined $range && $range =~ /\bbytes=(\d*)-(\d*)\z/) {
62 ($code, $len) = prepare_range($cgi, $in, \@h, $1, $2, $size);
64 push @h, 'Content-Range', "bytes */$size";
65 return [ 416, \@h, [] ];
69 push @h, 'Content-Type', $type, 'Content-Length', $len;
71 my ($res) = @_; # Plack callback
72 my $fh = $res->([ $code, \@h ]);
76 $n = $len if $len < $n;
77 my $r = sysread($in, $buf, $n);
78 last if (!defined($r) || $r <= 0);
87 my ($cgi, $in, $h, $beg, $end, $size) = @_;
91 if ($end ne '') { # "bytes=-$end" => last N bytes
102 } elsif ($end eq '' || $end >= $size) {
105 } elsif ($end < $size) {
112 $len = $end - $beg + 1;
116 seek($in, $beg, SEEK_SET) or return [ 500, [], [] ];
117 push @$h, qw(Accept-Ranges bytes Content-Range);
118 push @$h, "bytes $beg-$end/$size";
120 # FIXME: Plack::Middleware::Deflater bug?
121 $cgi->{env}->{'psgix.no-compress'} = 1;
127 # returns undef if 403 so it falls back to dumb HTTP
129 my ($cgi, $git, $path) = @_;
130 my $env = $cgi->{env};
132 my $input = $env->{'psgi.input'};
135 my $err = $env->{'psgi.errors'};
136 my $fd = eval { fileno($input) };
137 if (defined $fd && $fd >= 0) {
140 $in = input_to_file($env) or return r(500);
143 unless (pipe($rpipe, $wpipe)) {
144 $err->print("error creating pipe: $!\n");
148 # GIT_HTTP_EXPORT_ALL, GIT_COMMITTER_NAME, GIT_COMMITTER_EMAIL
149 # may be set in the server-process and are passed as-is
150 foreach my $name (qw(QUERY_STRING
151 REMOTE_USER REMOTE_ADDR
152 HTTP_CONTENT_ENCODING
156 my $val = $env->{$name};
157 $env{$name} = $val if defined $val;
159 my $git_dir = $git->{git_dir};
160 $env{GIT_HTTP_EXPORT_ALL} = '1';
161 $env{PATH_TRANSLATED} = "$git_dir/$path";
162 my %rdr = ( 0 => fileno($in), 1 => fileno($wpipe) );
163 my $pid = spawn([qw(git http-backend)], \%env, \%rdr);
164 unless (defined $pid) {
165 $err->print("error spawning: $!\n");
168 $wpipe = $in = undef;
170 my ($vin, $fh, $res);
176 $res->(r(500)) if $res;
179 $rpipe->close; # _may_ be Danga::Socket::close
185 return if $wpid == waitpid($wpid, 0);
186 $err->print("git http-backend ($git_dir): $?\n");
191 if ($e eq 'EAGAIN') {
192 select($vin, undef, undef, undef) if defined $vin;
193 # $vin is undef on async, so this is a noop on EAGAIN
197 $err->print("git http-backend ($git_dir): $e\n");
199 my $cb = sub { # read git-http-backend output and stream to client
200 my $r = $rpipe ? $rpipe->sysread($buf, 8192, length($buf)) : 0;
201 return $fail->($!{EAGAIN} ? 'EAGAIN' : $!) unless defined $r;
202 return $end->() if $r == 0; # EOF
203 if ($fh) { # stream body from git-http-backend to HTTP client
206 } elsif ($buf =~ s/\A(.*?)\r\n\r\n//s) { # parse headers
210 foreach my $l (split(/\r\n/, $h)) {
211 my ($k, $v) = split(/:\s*/, $l, 2);
212 if ($k =~ /\AStatus\z/i) {
213 ($code) = ($v =~ /\b(\d+)\b/);
218 # write response header:
219 $fh = $res->([ $code, \@h ]);
223 } # else { keep reading ... }
225 if (my $async = $env->{'pi-httpd.async'}) {
226 $rpipe = $async->($rpipe, $cb);
227 sub { ($res) = @_ } # let Danga::Socket handle the rest.
228 } else { # synchronous loop for other PSGI servers
230 vec($vin, fileno($rpipe), 1) = 1;
233 while ($rpipe) { $cb->() }
240 my $in = IO::File->new_tmpfile;
241 my $input = $env->{'psgi.input'};
244 my $r = $input->read($buf, 8192);
245 unless (defined $r) {
246 my $err = $env->{'psgi.errors'};
247 $err->print("error reading input: $!\n");
254 $in->sysseek(0, SEEK_SET);