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);
40 serve_dumb($cgi, $git, $path);
44 my ($cgi, $git, $path) = @_;
47 if ($path =~ /\A(?:$BIN)\z/o) {
48 $type = 'application/octet-stream';
49 } elsif ($path =~ /\A(?:$TEXT)\z/o) {
54 my $f = "$git->{git_dir}/$path";
55 return r(404) unless -f $f && -r _;
59 # TODO: If-Modified-Since and Last-Modified
60 open my $in, '<', $f or return r(404);
65 my $env = $cgi->{env};
66 my $range = $env->{HTTP_RANGE};
67 if (defined $range && $range =~ /\bbytes=(\d*)-(\d*)\z/) {
68 ($code, $len) = prepare_range($cgi, $in, \@h, $1, $2, $size);
70 push @h, 'Content-Range', "bytes */$size";
71 return [ 416, \@h, [] ];
75 push @h, 'Content-Type', $type, 'Content-Length', $len;
77 my ($res) = @_; # Plack callback
78 my $fh = $res->([ $code, \@h ]);
82 $n = $len if $len < $n;
83 my $r = sysread($in, $buf, $n);
84 last if (!defined($r) || $r <= 0);
93 my ($cgi, $in, $h, $beg, $end, $size) = @_;
97 if ($end ne '') { # "bytes=-$end" => last N bytes
108 } elsif ($end eq '' || $end >= $size) {
111 } elsif ($end < $size) {
118 $len = $end - $beg + 1;
122 seek($in, $beg, SEEK_SET) or return [ 500, [], [] ];
123 push @$h, qw(Accept-Ranges bytes Content-Range);
124 push @$h, "bytes $beg-$end/$size";
126 # FIXME: Plack::Middleware::Deflater bug?
127 $cgi->{env}->{'psgix.no-compress'} = 1;
133 # returns undef if 403 so it falls back to dumb HTTP
135 my ($cgi, $git, $path) = @_;
136 my $env = $cgi->{env};
138 my $input = $env->{'psgi.input'};
141 my $err = $env->{'psgi.errors'};
142 my $fd = eval { fileno($input) };
143 if (defined $fd && $fd >= 0) {
146 $in = input_to_file($env) or return r(500);
149 unless (pipe($rpipe, $wpipe)) {
150 $err->print("error creating pipe: $! - going static\n");
154 # GIT_COMMITTER_NAME, GIT_COMMITTER_EMAIL
155 # may be set in the server-process and are passed as-is
156 foreach my $name (qw(QUERY_STRING
157 REMOTE_USER REMOTE_ADDR
158 HTTP_CONTENT_ENCODING
162 my $val = $env->{$name};
163 $env{$name} = $val if defined $val;
165 my $git_dir = $git->{git_dir};
166 $env{GIT_HTTP_EXPORT_ALL} = '1';
167 $env{PATH_TRANSLATED} = "$git_dir/$path";
168 my %rdr = ( 0 => fileno($in), 1 => fileno($wpipe) );
169 my $pid = spawn([qw(git http-backend)], \%env, \%rdr);
170 unless (defined $pid) {
171 $err->print("error spawning: $! - going static\n");
174 $wpipe = $in = undef;
176 my ($vin, $fh, $res);
183 $rpipe->close; # _may_ be Danga::Socket::close
186 if (defined $pid && $pid != waitpid($pid, 0)) {
187 $err->print("git http-backend ($git_dir): $?\n");
192 my $dumb = serve_dumb($cgi, $git, $path);
193 ref($dumb) eq 'ARRAY' ? $res->($dumb) : $dumb->($res);
197 if ($e eq 'EAGAIN') {
198 select($vin, undef, undef, undef) if defined $vin;
199 # $vin is undef on async, so this is a noop on EAGAIN
203 $err->print("git http-backend ($git_dir): $e\n");
205 my $cb = sub { # read git-http-backend output and stream to client
206 my $r = $rpipe ? $rpipe->sysread($buf, 8192, length($buf)) : 0;
207 return $fail->($!{EAGAIN} ? 'EAGAIN' : $!) unless defined $r;
208 return $end->() if $r == 0; # EOF
209 if ($fh) { # stream body from git-http-backend to HTTP client
212 } elsif ($buf =~ s/\A(.*?)\r\n\r\n//s) { # parse headers
216 foreach my $l (split(/\r\n/, $h)) {
217 my ($k, $v) = split(/:\s*/, $l, 2);
218 if ($k =~ /\AStatus\z/i) {
219 ($code) = ($v =~ /\b(\d+)\b/);
225 # smart cloning disabled, serve dumbly
226 # in $end since we never undef $res in here
227 } else { # write response header:
228 $fh = $res->([ $code, \@h ]);
233 } # else { keep reading ... }
235 if (my $async = $env->{'pi-httpd.async'}) {
236 $rpipe = $async->($rpipe, $cb);
237 sub { ($res) = @_ } # let Danga::Socket handle the rest.
238 } else { # synchronous loop for other PSGI servers
240 vec($vin, fileno($rpipe), 1) = 1;
243 while ($rpipe) { $cb->() }
250 my $in = IO::File->new_tmpfile;
251 my $input = $env->{'psgi.input'};
254 my $r = $input->read($buf, 8192);
255 unless (defined $r) {
256 my $err = $env->{'psgi.errors'};
257 $err->print("error reading input: $!\n");
264 $in->sysseek(0, SEEK_SET);