lib/PublicInbox/ViewVCS.pm | 37 +++++++++++++++++++++++++++++++++---- diff --git a/lib/PublicInbox/ViewVCS.pm b/lib/PublicInbox/ViewVCS.pm index 85edf22fb6ccd2000cbc107c74ceb4d0d052b0ef..63731e9239169c82e54a8afd7368216efcf99715 100644 --- a/lib/PublicInbox/ViewVCS.pm +++ b/lib/PublicInbox/ViewVCS.pm @@ -34,6 +34,7 @@ my %QP_MAP = ( A => 'oid_a', B => 'oid_b', a => 'path_a', b => 'path_b' ); my $max_size = 1024 * 1024; # TODO: configurable my $enc_utf8 = find_encoding('UTF-8'); +my $BIN_DETECT = 8000; # same as git sub html_page ($$$) { my ($ctx, $code, $strref) = @_; @@ -43,7 +44,33 @@ my $res = PublicInbox::WwwStream->response($ctx, $code, sub { my ($nr, undef) = @_; $nr == 1 ? $$strref : undef; }); - $wcb->($res); + $wcb ? $wcb->($res) : $res; +} + +sub stream_large_blob ($$$$) { + my ($ctx, $res, $logref, $fn) = @_; + my ($git, $oid, $type, $size, $di) = @$res; + my $cmd = ['git', "--git-dir=$git->{git_dir}", 'cat-file', $type, $oid]; + my $qsp = PublicInbox::Qspawn->new($cmd); + my @cl = ('Content-Length', $size); + my $env = $ctx->{env}; + $env->{'qspawn.response'} = delete $ctx->{-wcb}; + $qsp->psgi_return($env, undef, sub { + my ($r, $bref) = @_; + if (!defined $r) { # error + html_page($ctx, 500, $logref); + } elsif (index($$bref, "\0") >= 0) { + my $ct = 'application/octet-stream'; + [200, ['Content-Type', $ct, @cl ] ]; + } else { + my $n = bytes::length($$bref); + if ($n >= $BIN_DETECT || $n == $size) { + my $ct = 'text/plain; charset=UTF-8'; + return [200, ['Content-Type', $ct, @cl] ]; + } + undef; # bref keeps growing + } + }); } sub solve_result { @@ -65,9 +92,13 @@ $res or return html_page($ctx, 404, \$log); $ref eq 'ARRAY' or return html_page($ctx, 500, \$log); my ($git, $oid, $type, $size, $di) = @$res; + my $path = to_filename($di->{path_b} || $hints->{path_b} || 'blob'); + my $raw_link = "(raw)"; if ($size > $max_size) { + return stream_large_blob($ctx, $res, \$log, $fn) if defined $fn; # TODO: stream the raw file if it's gigantic, at least - $log = '
Too big to show
' . $log; + $log = "
Too big to show, download available\n" .
+			"$oid $type $size bytes $raw_link
" . $log; return html_page($ctx, 500, \$log); } @@ -86,8 +117,6 @@ push(@$h, ($binary ? 'application/octet-stream' : 'text/plain')); return delete($ctx->{-wcb})->([200, $h, [ $$blob ]]); } - my $path = to_filename($di->{path_b} || $hints->{path_b} || 'blob'); - my $raw_link = "(raw)"; if ($binary) { $log = "
$oid $type $size bytes (binary)" .
 			" $raw_link
" . $log;