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
5 # This can serve Smart HTTP in the future.
6 package PublicInbox::GitHTTPDumb;
11 # n.b. serving "description" and "cloneurl" should be innocuous enough to
12 # not cause problems. serving "config" might...
13 my @text = qw[HEAD info/refs
14 objects/info/(?:http-alternates|alternates|packs)
15 cloneurl description];
18 objects/[a-f0-9]{2}/[a-f0-9]{38}
19 objects/pack/pack-[a-f0-9]{40}\.(?:pack|idx)
22 our $ANY = join('|', @binary, @text);
23 my $BIN = join('|', @binary);
24 my $TEXT = join('|', @text);
27 [ $_[0] , [qw(Content-Type text/plain Content-Length 0) ], [] ]
31 my ($cgi, $git, $path) = @_;
33 if ($path =~ /\A(?:$BIN)\z/o) {
34 $type = 'application/octet-stream';
35 } elsif ($path =~ /\A(?:$TEXT)\z/o) {
40 my $f = "$git->{git_dir}/$path";
41 return r(404) unless -f $f && -r _;
45 # TODO: If-Modified-Since and Last-Modified
46 open my $in, '<', $f or return r(404);
51 my $env = $cgi->{env} || \%ENV;
52 my $range = $env->{HTTP_RANGE};
53 if (defined $range && $range =~ /\bbytes=(\d*)-(\d*)\z/) {
54 ($code, $len) = prepare_range($cgi, $in, \@h, $1, $2, $size);
56 push @h, 'Content-Range', "bytes */$size";
57 return [ 416, \@h, [] ];
61 push @h, 'Content-Type', $type, 'Content-Length', $len;
63 my ($res) = @_; # Plack callback
64 my $fh = $res->([ $code, \@h ]);
68 $n = $len if $len < $n;
69 my $r = read($in, $buf, $n);
70 last if (!defined($r) || $r <= 0);
79 my ($cgi, $in, $h, $beg, $end, $size) = @_;
83 if ($end ne '') { # "bytes=-$end" => last N bytes
94 } elsif ($end eq '' || $end >= $size) {
97 } elsif ($end < $size) {
104 $len = $end - $beg + 1;
108 seek($in, $beg, SEEK_SET) or return [ 500, [], [] ];
109 push @$h, qw(Accept-Ranges bytes Content-Range);
110 push @$h, "bytes $beg-$end/$size";
112 # FIXME: Plack::Middleware::Deflater bug?
113 if (my $env = $cgi->{env}) {
114 $env->{'psgix.no-compress'} = 1;