From 19151f1b6a04532a1bc3f86fa945a10be6849b8d Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Fri, 5 Apr 2019 20:04:26 +0000 Subject: cgit: serve static css, logo, favicon directly We can reduce the configuration needed to run cgit by reusing the static file handling logic of the dumb git HTTP protocol. I hate logos and icons, so don't expect public-inbox.org or 80x24.org to ever have those to waste users' bandwidth with :P But I expect other users to find this useful. --- lib/PublicInbox/GitHTTPBackend.pm | 62 +++++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 29 deletions(-) (limited to 'lib/PublicInbox/GitHTTPBackend.pm') diff --git a/lib/PublicInbox/GitHTTPBackend.pm b/lib/PublicInbox/GitHTTPBackend.pm index cd8cdf84..57944a06 100644 --- a/lib/PublicInbox/GitHTTPBackend.pm +++ b/lib/PublicInbox/GitHTTPBackend.pm @@ -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; -- cgit v1.2.3-24-ge0c7