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;
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 = read($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 if (fileno($input) >= 0) {
137 } else { # FIXME untested
138 $in = IO::File->new_tmpfile;
140 my $r = $input->read($buf, 8192);
141 unless (defined $r) {
142 $err->print('error reading input: ', $!, "\n");
149 $in->sysseek(0, SEEK_SET);
152 unless (pipe($rpipe, $wpipe)) {
153 $err->print('error creating pipe', $!, "\n");
156 my $pid = fork; # TODO: vfork under Linux...
157 unless (defined $pid) {
158 $err->print('error forking: ', $!, "\n");
162 # GIT_HTTP_EXPORT_ALL, GIT_COMMITTER_NAME, GIT_COMMITTER_EMAIL
163 # may be set in the server-process and are passed as-is
164 foreach my $name (qw(QUERY_STRING
165 REMOTE_USER REMOTE_ADDR
166 HTTP_CONTENT_ENCODING
170 my $val = $env->{$name};
171 $ENV{$name} = $val if defined $val;
173 # $ENV{GIT_PROJECT_ROOT} = $git->{git_dir};
174 $ENV{GIT_HTTP_EXPORT_ALL} = '1';
175 $ENV{PATH_TRANSLATED} = "$git->{git_dir}/$path";
176 dup2(fileno($in), 0) or die "redirect stdin failed: $!\n";
177 dup2(fileno($wpipe), 1) or die "redirect stdout failed: $!\n";
178 my @cmd = qw(git http-backend);
179 exec(@cmd) or die 'exec `' . join(' ', @cmd). "' failed: $!\n";
187 while (defined(my $line = <$rpipe>)) {
188 if ($line =~ /\AStatus:\s*(\d+)/) {
193 push @h, split(/:\s*/, $line, 2);
197 return if $code == 403;
200 my $fh = $cb->([ $code, \@h ]);
202 my $r = sysread($rpipe, $buf, 8192);
203 die "$!\n" unless defined $r;