]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/ViewVCS.pm
viewvcs: keep temporary Solver dir for large streams
[public-inbox.git] / lib / PublicInbox / ViewVCS.pm
1 # Copyright (C) 2019 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # show any VCS object, similar to "git show"
5 # FIXME: we only show blobs for now
6 #
7 # This can use a "solver" to reconstruct blobs based on git
8 # patches (with abbreviated OIDs in the header).  However, the
9 # abbreviated OIDs must match exactly what's in the original
10 # email (unless a normal code repo already has the blob).
11 #
12 # In other words, we can only reliably reconstruct blobs based
13 # on links generated by ViewDiff (and only if the emailed
14 # patches apply 100% cleanly to published blobs).
15
16 package PublicInbox::ViewVCS;
17 use strict;
18 use warnings;
19 use bytes (); # only for bytes::length
20 use PublicInbox::SolverGit;
21 use PublicInbox::WwwStream;
22 use PublicInbox::Linkify;
23 use PublicInbox::Hval qw(ascii_html to_filename);
24 my $hl = eval {
25         require PublicInbox::HlMod;
26         PublicInbox::HlMod->new;
27 };
28
29 my %QP_MAP = ( A => 'oid_a', B => 'oid_b', a => 'path_a', b => 'path_b' );
30 my $max_size = 1024 * 1024; # TODO: configurable
31 my $BIN_DETECT = 8000; # same as git
32
33 sub html_page ($$$) {
34         my ($ctx, $code, $strref) = @_;
35         my $wcb = delete $ctx->{-wcb};
36         $ctx->{-upfx} = '../../'; # from "/$INBOX/$OID/s/"
37         my $res = PublicInbox::WwwStream->response($ctx, $code, sub {
38                 my ($nr, undef) =  @_;
39                 $nr == 1 ? $$strref : undef;
40         });
41         $wcb ? $wcb->($res) : $res;
42 }
43
44 sub stream_large_blob ($$$$) {
45         my ($ctx, $res, $logref, $fn) = @_;
46         my ($git, $oid, $type, $size, $di) = @$res;
47         my $cmd = ['git', "--git-dir=$git->{git_dir}", 'cat-file', $type, $oid];
48         my $qsp = PublicInbox::Qspawn->new($cmd);
49         my @cl = ('Content-Length', $size);
50         my $env = $ctx->{env};
51         $env->{'public-inbox.tmpgit'} = $git; # for {-tmp}/File::Temp::Dir
52         $env->{'qspawn.wcb'} = delete $ctx->{-wcb};
53         $qsp->psgi_return($env, undef, sub {
54                 my ($r, $bref) = @_;
55                 if (!defined $r) { # error
56                         html_page($ctx, 500, $logref);
57                 } elsif (index($$bref, "\0") >= 0) {
58                         my $ct = 'application/octet-stream';
59                         [200, ['Content-Type', $ct, @cl ] ];
60                 } else {
61                         my $n = bytes::length($$bref);
62                         if ($n >= $BIN_DETECT || $n == $size) {
63                                 my $ct = 'text/plain; charset=UTF-8';
64                                 return [200, ['Content-Type', $ct, @cl] ];
65                         }
66                         if ($r == 0) {
67                                 warn "premature EOF on $oid $$logref\n";
68                                 return html_page($ctx, 500, $logref);
69                         }
70                         undef; # bref keeps growing
71                 }
72         });
73 }
74
75 sub show_other ($$$$) {
76         my ($ctx, $res, $logref, $fn) = @_;
77         my ($git, $oid, $type, $size) = @$res;
78         if ($size > $max_size) {
79                 $$logref = "$oid is too big to show\n" . $$logref;
80                 return html_page($ctx, 200, $logref);
81         }
82         my $cmd = ['git', "--git-dir=$git->{git_dir}",
83                 qw(show --encoding=UTF-8 --no-color --no-abbrev), $oid ];
84         my $qsp = PublicInbox::Qspawn->new($cmd);
85         my $env = $ctx->{env};
86         $qsp->psgi_qx($env, undef, sub {
87                 my ($bref) = @_;
88                 if (my $err = $qsp->{err}) {
89                         utf8::decode($$err);
90                         $$logref .= "git show error: $err";
91                         return html_page($ctx, 500, $logref);
92                 }
93                 my $l = PublicInbox::Linkify->new;
94                 utf8::decode($$bref);
95                 $l->linkify_1($$bref);
96                 $$bref = '<pre>'. $l->linkify_2(ascii_html($$bref));
97                 $$bref .= '</pre><hr>' . $$logref;
98                 html_page($ctx, 200, $bref);
99         });
100 }
101
102 sub solve_result {
103         my ($ctx, $res, $log, $hints, $fn) = @_;
104
105         unless (seek($log, 0, 0)) {
106                 $ctx->{env}->{'psgi.errors'}->print("seek(log): $!\n");
107                 return html_page($ctx, 500, \'seek error');
108         }
109         $log = do { local $/; <$log> };
110
111         my $ref = ref($res);
112         my $l = PublicInbox::Linkify->new;
113         $l->linkify_1($log);
114         $log = '<pre>debug log:</pre><hr /><pre>' .
115                 $l->linkify_2(ascii_html($log)) . '</pre>';
116
117         $res or return html_page($ctx, 404, \$log);
118         $ref eq 'ARRAY' or return html_page($ctx, 500, \$log);
119
120         my ($git, $oid, $type, $size, $di) = @$res;
121         return show_other($ctx, $res, \$log, $fn) if $type ne 'blob';
122         my $path = to_filename($di->{path_b} || $hints->{path_b} || 'blob');
123         my $raw_link = "(<a\nhref=$path>raw</a>)";
124         if ($size > $max_size) {
125                 return stream_large_blob($ctx, $res, \$log, $fn) if defined $fn;
126                 $log = "<pre><b>Too big to show, download available</b>\n" .
127                         "$oid $type $size bytes $raw_link</pre>" . $log;
128                 return html_page($ctx, 500, \$log);
129         }
130
131         my $blob = $git->cat_file($oid);
132         if (!$blob) { # WTF?
133                 my $e = "Failed to retrieve generated blob ($oid)";
134                 $ctx->{env}->{'psgi.errors'}->print("$e ($git->{git_dir})\n");
135                 $log = "<pre><b>$e</b></pre>" . $log;
136                 return html_page($ctx, 500, \$log);
137         }
138
139         my $bin = index(substr($$blob, 0, $BIN_DETECT), "\0") >= 0;
140         if (defined $fn) {
141                 my $h = [ 'Content-Length', $size, 'Content-Type' ];
142                 push(@$h, ($bin ? 'application/octet-stream' : 'text/plain'));
143                 return delete($ctx->{-wcb})->([200, $h, [ $$blob ]]);
144         }
145
146         if ($bin) {
147                 $log = "<pre>$oid $type $size bytes (binary)" .
148                         " $raw_link</pre>" . $log;
149                 return html_page($ctx, 200, \$log);
150         }
151
152         # TODO: detect + convert to ensure validity
153         utf8::decode($$blob);
154         my $nl = ($$blob =~ tr/\n/\n/);
155         my $pad = length($nl);
156
157         $l->linkify_1($$blob);
158         my $ok = $hl->do_hl($blob, $path) if $hl;
159         if ($ok) {
160                 $blob = $ok;
161         } else {
162                 $$blob = ascii_html($$blob);
163         }
164
165         # using some of the same CSS class names and ids as cgit
166         $log = "<pre>$oid $type $size bytes $raw_link</pre>" .
167                 "<hr /><table\nclass=blob>".
168                 "<tr><td\nclass=linenumbers><pre>" . join('', map {
169                         sprintf("<a id=n$_ href=#n$_>% ${pad}u</a>\n", $_)
170                 } (1..$nl)) . '</pre></td>' .
171                 '<td><pre> </pre></td>'. # pad for non-CSS users
172                 "<td\nclass=lines><pre\nstyle='white-space:pre'><code>" .
173                 $l->linkify_2($$blob) .
174                 '</code></pre></td></tr></table>' . $log;
175
176         html_page($ctx, 200, \$log);
177 }
178
179 sub show ($$;$) {
180         my ($ctx, $oid_b, $fn) = @_;
181         my $qp = $ctx->{qp};
182         my $hints = {};
183         while (my ($from, $to) = each %QP_MAP) {
184                 defined(my $v = $qp->{$from}) or next;
185                 $hints->{$to} = $v;
186         }
187
188         open my $log, '+>', undef or die "open: $!";
189         my $solver = PublicInbox::SolverGit->new($ctx->{-inbox}, sub {
190                 solve_result($ctx, $_[0], $log, $hints, $fn);
191         });
192
193         # PSGI server will call this and give us a callback
194         sub {
195                 $ctx->{-wcb} = $_[0]; # HTTP write callback
196                 $solver->solve($ctx->{env}, $log, $oid_b, $hints);
197         };
198 }
199
200 1;