X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=lib%2FPublicInbox%2FViewVCS.pm;h=eecc51e673b2aad8ad8fbfbca21326ed895be7b6;hb=ce4fe8f5144f7555ddd42b6a94ec602e042c6e43;hp=4a3896d4b689f257e8becc1db705d36c637f6545;hpb=1f1dd03147b2537bb641fed3ac31986f65defefd;p=public-inbox.git diff --git a/lib/PublicInbox/ViewVCS.pm b/lib/PublicInbox/ViewVCS.pm index 4a3896d4..eecc51e6 100644 --- a/lib/PublicInbox/ViewVCS.pm +++ b/lib/PublicInbox/ViewVCS.pm @@ -20,49 +20,84 @@ use Encode qw(find_encoding); use PublicInbox::SolverGit; use PublicInbox::WwwStream; use PublicInbox::Linkify; -use PublicInbox::Hval qw(ascii_html to_filename); +use PublicInbox::Hval qw(ascii_html to_filename src_escape); +my $hl = eval { + require PublicInbox::HlMod; + PublicInbox::HlMod->new; +}; + +# we need to trigger highlight::CodeGenerator::deleteInstance +# in HlMod::DESTROY before the rest of Perl shuts down to avoid +# a segfault at shutdown +END { $hl = undef }; + 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) = @_; + my $wcb = delete $ctx->{-wcb}; $ctx->{-upfx} = '../../'; # from "/$INBOX/$OID/s/" - PublicInbox::WwwStream->response($ctx, $code, sub { + my $res = PublicInbox::WwwStream->response($ctx, $code, sub { my ($nr, undef) = @_; $nr == 1 ? $$strref : undef; }); + $wcb ? $wcb->($res) : $res; } -sub show ($$;$) { - my ($ctx, $oid_b, $fn) = @_; - my $ibx = $ctx->{-inbox}; - my $inboxes = [ $ibx ]; - my $solver = PublicInbox::SolverGit->new($ibx->{-repo_objs}, $inboxes); - my $qp = $ctx->{qp}; - my $hints = {}; - while (my ($from, $to) = each %QP_MAP) { - defined(my $v = $qp->{$from}) or next; - $hints->{$to} = $v; - } +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.wcb'} = 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 + } + }); +} - open my $log, '+>', undef or die "open: $!"; - my $res = $solver->solve($log, $oid_b, $hints); +sub solve_result { + my ($ctx, $res, $log, $hints, $fn) = @_; - seek($log, 0, 0) or die "seek: $!"; + unless (seek($log, 0, 0)) { + $ctx->{env}->{'psgi.errors'}->print("seek(log): $!\n"); + return html_page($ctx, 500, \'seek error'); + } $log = do { local $/; <$log> }; + my $ref = ref($res); my $l = PublicInbox::Linkify->new; $l->linkify_1($log); $log = '
debug log:

' .
 		$l->linkify_2(ascii_html($log)) . '
'; $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) { - # TODO: stream the raw file if it's gigantic, at least - $log = '
Too big to show
' . $log; + return stream_large_blob($ctx, $res, \$log, $fn) if defined $fn; + $log = "
Too big to show, download available\n" .
+			"$oid $type $size bytes $raw_link
" . $log; return html_page($ctx, 500, \$log); } @@ -78,11 +113,9 @@ sub show ($$;$) { if ($fn) { my $h = [ 'Content-Length', $size, 'Content-Type' ]; push(@$h, ($binary ? 'application/octet-stream' : 'text/plain')); - return [ 200, $h, [ $$blob ]]; + 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; @@ -93,6 +126,16 @@ sub show ($$;$) { my $nl = ($$blob =~ tr/\n/\n/); my $pad = length($nl); + $l->linkify_1($$blob); + my $ok = $hl->do_hl($blob, $path) if $hl; + if ($ok) { + $$ok = $enc_utf8->decode($$ok); + src_escape($$ok); + $blob = $ok; + } else { + $$blob = ascii_html($$blob); + } + # using some of the same CSS class names and ids as cgit $log = "
$oid $type $size bytes $raw_link
" . "
". @@ -101,10 +144,31 @@ sub show ($$;$) { } (1..$nl)) . '' . '
 
'. # pad for non-CSS users "" . - ascii_html($$blob) . + $l->linkify_2($$blob) . '' . $log; html_page($ctx, 200, \$log); } +sub show ($$;$) { + my ($ctx, $oid_b, $fn) = @_; + my $qp = $ctx->{qp}; + my $hints = {}; + while (my ($from, $to) = each %QP_MAP) { + defined(my $v = $qp->{$from}) or next; + $hints->{$to} = $v; + } + + open my $log, '+>', undef or die "open: $!"; + my $solver = PublicInbox::SolverGit->new($ctx->{-inbox}, sub { + solve_result($ctx, $_[0], $log, $hints, $fn); + }); + + # PSGI server will call this and give us a callback + sub { + $ctx->{-wcb} = $_[0]; # HTTP write callback + $solver->solve($ctx->{env}, $log, $oid_b, $hints); + }; +} + 1;