]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/GitHTTPBackend.pm
71b7a8f1b1c12278730dd8c8cd64cf6f3a93d415
[public-inbox.git] / lib / PublicInbox / GitHTTPBackend.pm
1 # Copyright (C) 2016 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # when no endpoints match, fallback to this and serve a static file
5 # or smart HTTP
6 package PublicInbox::GitHTTPBackend;
7 use strict;
8 use warnings;
9 use Fcntl qw(:seek);
10 use POSIX qw(dup2);
11
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];
17
18 my @binary = qw!
19         objects/[a-f0-9]{2}/[a-f0-9]{38}
20         objects/pack/pack-[a-f0-9]{40}\.(?:pack|idx)
21         !;
22
23 our $ANY = join('|', @binary, @text);
24 my $BIN = join('|', @binary);
25 my $TEXT = join('|', @text);
26
27 sub r {
28         [ $_[0] , [qw(Content-Type text/plain Content-Length 0) ], [] ]
29 }
30
31 sub serve {
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);
36                 return $ok if $ok;
37         }
38
39         my $type;
40         if ($path =~ /\A(?:$BIN)\z/o) {
41                 $type = 'application/octet-stream';
42         } elsif ($path =~ /\A(?:$TEXT)\z/o) {
43                 $type = 'text/plain';
44         } else {
45                 return r(404);
46         }
47         my $f = "$git->{git_dir}/$path";
48         return r(404) unless -f $f && -r _;
49         my @st = stat(_);
50         my $size = $st[7];
51
52         # TODO: If-Modified-Since and Last-Modified
53         open my $in, '<', $f or return r(404);
54         my $code = 200;
55         my $len = $size;
56         my @h;
57
58         my $env = $cgi->{env} || \%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);
62                 if ($code == 416) {
63                         push @h, 'Content-Range', "bytes */$size";
64                         return [ 416, \@h, [] ];
65                 }
66         }
67
68         push @h, 'Content-Type', $type, 'Content-Length', $len;
69         sub {
70                 my ($res) = @_; # Plack callback
71                 my $fh = $res->([ $code, \@h ]);
72                 my $buf;
73                 my $n = 8192;
74                 while ($len > 0) {
75                         $n = $len if $len < $n;
76                         my $r = read($in, $buf, $n);
77                         last if (!defined($r) || $r <= 0);
78                         $len -= $r;
79                         $fh->write($buf);
80                 }
81                 $fh->close;
82         }
83 }
84
85 sub prepare_range {
86         my ($cgi, $in, $h, $beg, $end, $size) = @_;
87         my $code = 200;
88         my $len = $size;
89         if ($beg eq '') {
90                 if ($end ne '') { # "bytes=-$end" => last N bytes
91                         $beg = $size - $end;
92                         $beg = 0 if $beg < 0;
93                         $end = $size - 1;
94                         $code = 206;
95                 } else {
96                         $code = 416;
97                 }
98         } else {
99                 if ($beg > $size) {
100                         $code = 416;
101                 } elsif ($end eq '' || $end >= $size) {
102                         $end = $size - 1;
103                         $code = 206;
104                 } elsif ($end < $size) {
105                         $code = 206;
106                 } else {
107                         $code = 416;
108                 }
109         }
110         if ($code == 206) {
111                 $len = $end - $beg + 1;
112                 if ($len <= 0) {
113                         $code = 416;
114                 } else {
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";
118
119                         # FIXME: Plack::Middleware::Deflater bug?
120                         if (my $env = $cgi->{env}) {
121                                 $env->{'psgix.no-compress'} = 1;
122                         }
123                 }
124         }
125         ($code, $len);
126 }
127
128 # returns undef if 403 so it falls back to dumb HTTP
129 sub serve_smart {
130         my ($cgi, $git, $path) = @_;
131         my $env = $cgi->{env};
132
133         my $input = $env->{'psgi.input'};
134         my $buf;
135         my $in;
136         my $err = $env->{'psgi.errors'};
137         if (fileno($input) >= 0) { # FIXME untested
138                 $in = $input;
139         } else {
140                 $in = IO::File->new_tmpfile;
141                 while (1) {
142                         my $r = $input->read($buf, 8192);
143                         unless (defined $r) {
144                                 $err->print('error reading input: ', $!, "\n");
145                                 return r(500);
146                         }
147                         last if ($r == 0);
148                         $in->write($buf);
149                 }
150                 $in->flush;
151                 $in->sysseek(0, SEEK_SET);
152         }
153         my $out = IO::File->new_tmpfile;
154         my $pid = fork; # TODO: vfork under Linux...
155         unless (defined $pid) {
156                 $err->print('error forking: ', $!, "\n");
157                 return r(500);
158         }
159         if ($pid == 0) {
160                 # GIT_HTTP_EXPORT_ALL, GIT_COMMITTER_NAME, GIT_COMMITTER_EMAIL
161                 # may be set in the server-process and are passed as-is
162                 foreach my $name (qw(QUERY_STRING
163                                         REMOTE_USER REMOTE_ADDR
164                                         HTTP_CONTENT_ENCODING
165                                         CONTENT_TYPE
166                                         SERVER_PROTOCOL
167                                         REQUEST_METHOD)) {
168                         my $val = $env->{$name};
169                         $ENV{$name} = $val if defined $val;
170                 }
171                 # $ENV{GIT_PROJECT_ROOT} = $git->{git_dir};
172                 $ENV{GIT_HTTP_EXPORT_ALL} = '1';
173                 $ENV{PATH_TRANSLATED} = "$git->{git_dir}/$path";
174                 dup2(fileno($in), 0) or die "redirect stdin failed: $!\n";
175                 dup2(fileno($out), 1) or die "redirect stdout failed: $!\n";
176                 my @cmd = qw(git http-backend);
177                 exec(@cmd) or die 'exec `' . join(' ', @cmd). "' failed: $!\n";
178         }
179
180         if (waitpid($pid, 0) != $pid) {
181                 $err->print("git http-backend ($git->{git_dir}): ", $?, "\n");
182                 return r(500);
183         }
184         $in = undef;
185         $out->seek(0, SEEK_SET);
186         my @h;
187         my $code = 200;
188         {
189                 local $/ = "\r\n";
190                 while (defined(my $line = <$out>)) {
191                         if ($line =~ /\AStatus:\s*(\d+)/) {
192                                 $code = $1;
193                         } else {
194                                 chomp $line;
195                                 last if $line eq '';
196                                 push @h, split(/:\s*/, $line, 2);
197                         }
198                 }
199         }
200         return if $code == 403;
201         sub {
202                 my ($cb) = @_;
203                 my $fh = $cb->([ $code, \@h ]);
204                 while (1) {
205                         my $r = $out->read($buf, 8192);
206                         die "$!\n" unless defined $r;
207                         last if ($r == 0);
208                         $fh->write($buf);
209                 }
210                 $fh->close;
211         }
212 }
213
214 1;