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 # TODO: make configurable, but keep in mind it's better to have
14 # multiple -httpd worker processes which are already scaled to
15 # the proper number of CPUs and memory. git-pack-objects(1) may
16 # also use threads and bust memory limits, too, so I recommend
17 # limiting threads to 1 (via `pack.threads` knob in git) for serving.
21 # n.b. serving "description" and "cloneurl" should be innocuous enough to
22 # not cause problems. serving "config" might...
23 my @text = qw[HEAD info/refs
24 objects/info/(?:http-alternates|alternates|packs)
25 cloneurl description];
28 objects/[a-f0-9]{2}/[a-f0-9]{38}
29 objects/pack/pack-[a-f0-9]{40}\.(?:pack|idx)
32 our $ANY = join('|', @binary, @text);
33 my $BIN = join('|', @binary);
34 my $TEXT = join('|', @text);
37 [ $_[0] , [qw(Content-Type text/plain Content-Length 0) ], [] ]
41 my ($cgi, $git, $path) = @_;
42 return serve_dumb($cgi, $git, $path) if $nr_running >= $LIMIT;
44 my $service = $cgi->param('service') || '';
45 if ($service =~ /\Agit-\w+-pack\z/ || $path =~ /\Agit-\w+-pack\z/) {
46 my $ok = serve_smart($cgi, $git, $path);
50 serve_dumb($cgi, $git, $path);
54 my ($cgi, $git, $path) = @_;
57 if ($path =~ /\A(?:$BIN)\z/o) {
58 $type = 'application/octet-stream';
59 } elsif ($path =~ /\A(?:$TEXT)\z/o) {
64 my $f = "$git->{git_dir}/$path";
65 return r(404) unless -f $f && -r _;
69 # TODO: If-Modified-Since and Last-Modified
70 open my $in, '<', $f or return r(404);
75 my $env = $cgi->{env};
76 my $range = $env->{HTTP_RANGE};
77 if (defined $range && $range =~ /\bbytes=(\d*)-(\d*)\z/) {
78 ($code, $len) = prepare_range($cgi, $in, \@h, $1, $2, $size);
80 push @h, 'Content-Range', "bytes */$size";
81 return [ 416, \@h, [] ];
85 push @h, 'Content-Type', $type, 'Content-Length', $len;
87 my ($res) = @_; # Plack callback
88 my $fh = $res->([ $code, \@h ]);
92 $n = $len if $len < $n;
93 my $r = sysread($in, $buf, $n);
94 last if (!defined($r) || $r <= 0);
98 die "$f truncated with $len bytes remaining\n" if $len;
104 my ($cgi, $in, $h, $beg, $end, $size) = @_;
108 if ($end ne '') { # "bytes=-$end" => last N bytes
110 $beg = 0 if $beg < 0;
119 } elsif ($end eq '' || $end >= $size) {
122 } elsif ($end < $size) {
129 $len = $end - $beg + 1;
133 seek($in, $beg, SEEK_SET) or return [ 500, [], [] ];
134 push @$h, qw(Accept-Ranges bytes Content-Range);
135 push @$h, "bytes $beg-$end/$size";
137 # FIXME: Plack::Middleware::Deflater bug?
138 $cgi->{env}->{'psgix.no-compress'} = 1;
144 # returns undef if 403 so it falls back to dumb HTTP
146 my ($cgi, $git, $path) = @_;
147 my $env = $cgi->{env};
149 my $input = $env->{'psgi.input'};
152 my $err = $env->{'psgi.errors'};
153 my $fd = eval { fileno($input) };
154 if (defined $fd && $fd >= 0) {
157 $in = input_to_file($env) or return r(500);
160 unless (pipe($rpipe, $wpipe)) {
161 $err->print("error creating pipe: $! - going static\n");
165 # GIT_COMMITTER_NAME, GIT_COMMITTER_EMAIL
166 # may be set in the server-process and are passed as-is
167 foreach my $name (qw(QUERY_STRING
168 REMOTE_USER REMOTE_ADDR
169 HTTP_CONTENT_ENCODING
173 my $val = $env->{$name};
174 $env{$name} = $val if defined $val;
176 my $git_dir = $git->{git_dir};
177 $env{GIT_HTTP_EXPORT_ALL} = '1';
178 $env{PATH_TRANSLATED} = "$git_dir/$path";
179 my %rdr = ( 0 => fileno($in), 1 => fileno($wpipe) );
180 my $pid = spawn([qw(git http-backend)], \%env, \%rdr);
181 unless (defined $pid) {
182 $err->print("error spawning: $! - going static\n");
185 $wpipe = $in = undef;
187 my ($vin, $fh, $res);
195 # _may_ be Danga::Socket::close via
196 # PublicInbox::HTTPD::Async::close:
202 my $e = $pid == waitpid($pid, 0) ?
203 $? : "PID:$pid still running?";
205 $err->print("http-backend ($git_dir): $e\n");
206 if (my $io = $env->{'psgix.io'}) {
212 my $dumb = serve_dumb($cgi, $git, $path);
213 ref($dumb) eq 'ARRAY' ? $res->($dumb) : $dumb->($res);
216 if ($!{EAGAIN} || $!{EINTR}) {
217 select($vin, undef, undef, undef) if defined $vin;
218 # $vin is undef on async, so this is a noop on EAGAIN
223 $err->print("git http-backend ($git_dir): $e\n");
225 my $cb = sub { # read git-http-backend output and stream to client
226 my $r = $rpipe ? $rpipe->sysread($buf, 8192, length($buf)) : 0;
227 return $fail->() unless defined $r;
228 return $end->() if $r == 0; # EOF
229 if ($fh) { # stream body from git-http-backend to HTTP client
232 } elsif ($buf =~ s/\A(.*?)\r\n\r\n//s) { # parse headers
236 foreach my $l (split(/\r\n/, $h)) {
237 my ($k, $v) = split(/:\s*/, $l, 2);
238 if ($k =~ /\AStatus\z/i) {
239 ($code) = ($v =~ /\b(\d+)\b/);
245 # smart cloning disabled, serve dumbly
246 # in $end since we never undef $res in here
247 } else { # write response header:
248 $fh = $res->([ $code, \@h ]);
253 } # else { keep reading ... }
255 if (my $async = $env->{'pi-httpd.async'}) {
256 # $async is PublicInbox::HTTPD::Async->new($rpipe, $cb)
257 $rpipe = $async->($rpipe, $cb);
258 sub { ($res) = @_ } # let Danga::Socket handle the rest.
259 } else { # synchronous loop for other PSGI servers
261 vec($vin, fileno($rpipe), 1) = 1;
264 while ($rpipe) { $cb->() }
271 my $in = IO::File->new_tmpfile;
272 my $input = $env->{'psgi.input'};
275 my $r = $input->read($buf, 8192);
276 unless (defined $r) {
277 my $err = $env->{'psgi.errors'};
278 $err->print("error reading input: $!\n");
285 $in->sysseek(0, SEEK_SET);