]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/GitHTTPBackend.pm
bundle Danga::Socket and Sys::Syscall
[public-inbox.git] / lib / PublicInbox / GitHTTPBackend.pm
index f6528c45908a22577214ad53aa1da69da8dc10ef..09411048db25a8d4fcb41237d4285fa46abd9960 100644 (file)
@@ -67,7 +67,7 @@ sub err ($@) {
 
 sub drop_client ($) {
        if (my $io = $_[0]->{'psgix.io'}) {
-               $io->close; # this is Danga::Socket::close
+               $io->close; # this is PublicInbox::DS::close
        }
 }
 
@@ -80,46 +80,26 @@ sub cache_one_year {
                'Cache-Control', 'public, max-age=31536000';
 }
 
-sub serve_dumb {
-       my ($env, $git, $path) = @_;
-
-       my @h;
-       my $type;
-       if ($path =~ m!\Aobjects/[a-f0-9]{2}/[a-f0-9]{38}\z!) {
-               $type = 'application/x-git-loose-object';
-               cache_one_year(\@h);
-       } elsif ($path =~ m!\Aobjects/pack/pack-[a-f0-9]{40}\.pack\z!) {
-               $type = 'application/x-git-packed-objects';
-               cache_one_year(\@h);
-       } elsif ($path =~ m!\Aobjects/pack/pack-[a-f0-9]{40}\.idx\z!) {
-               $type = 'application/x-git-packed-objects-toc';
-               cache_one_year(\@h);
-       } elsif ($path =~ /\A(?:$TEXT)\z/o) {
-               $type = 'text/plain';
-               push @h, @no_cache;
-       } else {
-               return r(404);
-       }
-
-       my $f = $git->{git_dir} . '/' . $path;
+sub static_result ($$$$) {
+       my ($env, $h, $f, $type) = @_;
        return r(404) unless -f $f && -r _; # just in case it's a FIFO :P
-       my $size = -s _;
 
        # TODO: If-Modified-Since and Last-Modified?
        open my $in, '<', $f or return r(404);
+       my $size = -s $in;
        my $len = $size;
        my $code = 200;
-       push @h, 'Content-Type', $type;
+       push @$h, 'Content-Type', $type;
        if (($env->{HTTP_RANGE} || '') =~ /\bbytes=(\d*)-(\d*)\z/) {
-               ($code, $len) = prepare_range($env, $in, \@h, $1, $2, $size);
+               ($code, $len) = prepare_range($env, $in, $h, $1, $2, $size);
                if ($code == 416) {
-                       push @h, 'Content-Range', "bytes */$size";
-                       return [ 416, \@h, [] ];
+                       push @$h, 'Content-Range', "bytes */$size";
+                       return [ 416, $h, [] ];
                }
        }
-       push @h, 'Content-Length', $len;
+       push @$h, 'Content-Length', $len;
        my $n = 65536;
-       [ $code, \@h, Plack::Util::inline_object(close => sub { close $in },
+       [ $code, $h, Plack::Util::inline_object(close => sub { close $in },
                getline => sub {
                        return if $len == 0;
                        $n = $len if $len < $n;
@@ -138,6 +118,30 @@ sub serve_dumb {
                })]
 }
 
+sub serve_dumb {
+       my ($env, $git, $path) = @_;
+
+       my $h = [];
+       my $type;
+       if ($path =~ m!\Aobjects/[a-f0-9]{2}/[a-f0-9]{38}\z!) {
+               $type = 'application/x-git-loose-object';
+               cache_one_year($h);
+       } elsif ($path =~ m!\Aobjects/pack/pack-[a-f0-9]{40}\.pack\z!) {
+               $type = 'application/x-git-packed-objects';
+               cache_one_year($h);
+       } elsif ($path =~ m!\Aobjects/pack/pack-[a-f0-9]{40}\.idx\z!) {
+               $type = 'application/x-git-packed-objects-toc';
+               cache_one_year($h);
+       } elsif ($path =~ /\A(?:$TEXT)\z/o) {
+               $type = 'text/plain';
+               push @$h, @no_cache;
+       } else {
+               return r(404);
+       }
+
+       static_result($env, $h, "$git->{git_dir}/$path", $type);
+}
+
 sub prepare_range {
        my ($env, $in, $h, $beg, $end, $size) = @_;
        my $code = 200;
@@ -201,8 +205,8 @@ sub serve_smart {
        my $qsp = PublicInbox::Qspawn->new([qw(git http-backend)], \%env, $rdr);
        $qsp->psgi_return($env, $limiter, sub {
                my ($r, $bref) = @_;
-               $r = parse_cgi_headers($bref) or return; # incomplete headers
-               $r->[0] == 403 ? serve_dumb($env, $git, $path) : $r;
+               my $res = parse_cgi_headers($r, $bref) or return; # incomplete
+               $res->[0] == 403 ? serve_dumb($env, $git, $path) : $res;
        });
 }
 
@@ -247,12 +251,13 @@ sub input_prepare {
 }
 
 sub parse_cgi_headers {
-       my ($bref) = @_;
-       $$bref =~ s/\A(.*?)\r\n\r\n//s or return;
+       my ($r, $bref) = @_;
+       return r(500) unless defined $r && $r >= 0;
+       $$bref =~ s/\A(.*?)\r?\n\r?\n//s or return $r == 0 ? r(500) : undef;
        my $h = $1;
        my $code = 200;
        my @h;
-       foreach my $l (split(/\r\n/, $h)) {
+       foreach my $l (split(/\r?\n/, $h)) {
                my ($k, $v) = split(/:\s*/, $l, 2);
                if ($k =~ /\AStatus\z/i) {
                        ($code) = ($v =~ /\b(\d+)\b/);