]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/ViewVCS.pm
viewvcs: stream_blob_parse_hdr: fix BIN_DETECT retries
[public-inbox.git] / lib / PublicInbox / ViewVCS.pm
1 # Copyright (C) 2019-2020 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 qw(html_oneshot);
22 use PublicInbox::Linkify;
23 use PublicInbox::Tmpfile;
24 use PublicInbox::Hval qw(ascii_html to_filename);
25 my $hl = eval {
26         require PublicInbox::HlMod;
27         PublicInbox::HlMod->new;
28 };
29
30 my %QP_MAP = ( A => 'oid_a', B => 'oid_b', a => 'path_a', b => 'path_b' );
31 our $MAX_SIZE = 1024 * 1024; # TODO: configurable
32 my $BIN_DETECT = 8000; # same as git
33
34 sub html_page ($$$) {
35         my ($ctx, $code, $strref) = @_;
36         my $wcb = delete $ctx->{-wcb};
37         $ctx->{-upfx} = '../../'; # from "/$INBOX/$OID/s/"
38         my $res = html_oneshot($ctx, $code, $strref);
39         $wcb ? $wcb->($res) : $res;
40 }
41
42 sub stream_blob_parse_hdr { # {parse_hdr} for Qspawn
43         my ($r, $bref, $ctx) = @_;
44         my ($res, $logref) = delete @$ctx{qw(-res -logref)};
45         my ($git, $oid, $type, $size, $di) = @$res;
46         my @cl = ('Content-Length', $size);
47         if (!defined $r) { # error
48                 html_page($ctx, 500, $logref);
49         } elsif (index($$bref, "\0") >= 0) {
50                 [200, [qw(Content-Type application/octet-stream), @cl] ];
51         } else {
52                 my $n = bytes::length($$bref);
53                 if ($n >= $BIN_DETECT || $n == $size) {
54                         return [200, [ 'Content-Type',
55                                 'text/plain; charset=UTF-8', @cl ] ];
56                 }
57                 if ($r == 0) {
58                         warn "premature EOF on $oid $$logref\n";
59                         return html_page($ctx, 500, $logref);
60                 }
61                 @$ctx{qw(-res -logref)} = ($res, $logref);
62                 undef; # bref keeps growing
63         }
64 }
65
66 sub stream_large_blob ($$$$) {
67         my ($ctx, $res, $logref, $fn) = @_;
68         $ctx->{-logref} = $logref;
69         $ctx->{-res} = $res;
70         my ($git, $oid, $type, $size, $di) = @$res;
71         my $cmd = ['git', "--git-dir=$git->{git_dir}", 'cat-file', $type, $oid];
72         my $qsp = PublicInbox::Qspawn->new($cmd);
73         my $env = $ctx->{env};
74         $env->{'qspawn.wcb'} = delete $ctx->{-wcb};
75         $qsp->psgi_return($env, undef, \&stream_blob_parse_hdr, $ctx);
76 }
77
78 sub show_other_result ($$) {
79         my ($bref, $ctx) = @_;
80         my ($qsp, $logref) = delete @$ctx{qw(-qsp -logref)};
81         if (my $err = $qsp->{err}) {
82                 utf8::decode($$err);
83                 $$logref .= "git show error: $err";
84                 return html_page($ctx, 500, $logref);
85         }
86         my $l = PublicInbox::Linkify->new;
87         utf8::decode($$bref);
88         $$bref = '<pre>'. $l->to_html($$bref);
89         $$bref .= '</pre><hr>' . $$logref;
90         html_page($ctx, 200, $bref);
91 }
92
93 sub show_other ($$$$) {
94         my ($ctx, $res, $logref, $fn) = @_;
95         my ($git, $oid, $type, $size) = @$res;
96         if ($size > $MAX_SIZE) {
97                 $$logref = "$oid is too big to show\n" . $$logref;
98                 return html_page($ctx, 200, $logref);
99         }
100         my $cmd = ['git', "--git-dir=$git->{git_dir}",
101                 qw(show --encoding=UTF-8 --no-color --no-abbrev), $oid ];
102         my $qsp = PublicInbox::Qspawn->new($cmd);
103         my $env = $ctx->{env};
104         $ctx->{-qsp} = $qsp;
105         $ctx->{-logref} = $logref;
106         $qsp->psgi_qx($env, undef, \&show_other_result, $ctx);
107 }
108
109 # user_cb for SolverGit, called as: user_cb->($result_or_error, $uarg)
110 sub solve_result {
111         my ($res, $ctx) = @_;
112         my ($log, $hints, $fn) = delete @$ctx{qw(log hints fn)};
113
114         unless (seek($log, 0, 0)) {
115                 $ctx->{env}->{'psgi.errors'}->print("seek(log): $!\n");
116                 return html_page($ctx, 500, \'seek error');
117         }
118         $log = do { local $/; <$log> };
119
120         my $ref = ref($res);
121         my $l = PublicInbox::Linkify->new;
122         $log = '<pre>debug log:</pre><hr /><pre>' .
123                 $l->to_html($log) . '</pre>';
124
125         $res or return html_page($ctx, 404, \$log);
126         $ref eq 'ARRAY' or return html_page($ctx, 500, \$log);
127
128         my ($git, $oid, $type, $size, $di) = @$res;
129         return show_other($ctx, $res, \$log, $fn) if $type ne 'blob';
130         my $path = to_filename($di->{path_b} || $hints->{path_b} || 'blob');
131         my $raw_link = "(<a\nhref=$path>raw</a>)";
132         if ($size > $MAX_SIZE) {
133                 return stream_large_blob($ctx, $res, \$log, $fn) if defined $fn;
134                 $log = "<pre><b>Too big to show, download available</b>\n" .
135                         "$oid $type $size bytes $raw_link</pre>" . $log;
136                 return html_page($ctx, 200, \$log);
137         }
138
139         my $blob = $git->cat_file($oid);
140         if (!$blob) { # WTF?
141                 my $e = "Failed to retrieve generated blob ($oid)";
142                 $ctx->{env}->{'psgi.errors'}->print("$e ($git->{git_dir})\n");
143                 $log = "<pre><b>$e</b></pre>" . $log;
144                 return html_page($ctx, 500, \$log);
145         }
146
147         my $bin = index(substr($$blob, 0, $BIN_DETECT), "\0") >= 0;
148         if (defined $fn) {
149                 my $h = [ 'Content-Length', $size, 'Content-Type' ];
150                 push(@$h, ($bin ? 'application/octet-stream' : 'text/plain'));
151                 return delete($ctx->{-wcb})->([200, $h, [ $$blob ]]);
152         }
153
154         if ($bin) {
155                 $log = "<pre>$oid $type $size bytes (binary)" .
156                         " $raw_link</pre>" . $log;
157                 return html_page($ctx, 200, \$log);
158         }
159
160         # TODO: detect + convert to ensure validity
161         utf8::decode($$blob);
162         my $nl = ($$blob =~ s/\r?\n/\n/sg);
163         my $pad = length($nl);
164
165         $l->linkify_1($$blob);
166         my $ok = $hl->do_hl($blob, $path) if $hl;
167         if ($ok) {
168                 $blob = $ok;
169         } else {
170                 $$blob = ascii_html($$blob);
171         }
172
173         # using some of the same CSS class names and ids as cgit
174         $log = "<pre>$oid $type $size bytes $raw_link</pre>" .
175                 "<hr /><table\nclass=blob>".
176                 "<tr><td\nclass=linenumbers><pre>" . join('', map {
177                         sprintf("<a id=n$_ href=#n$_>% ${pad}u</a>\n", $_)
178                 } (1..$nl)) . '</pre></td>' .
179                 '<td><pre> </pre></td>'. # pad for non-CSS users
180                 "<td\nclass=lines><pre\nstyle='white-space:pre'><code>" .
181                 $l->linkify_2($$blob) .
182                 '</code></pre></td></tr></table>' . $log;
183
184         html_page($ctx, 200, \$log);
185 }
186
187 sub show ($$;$) {
188         my ($ctx, $oid_b, $fn) = @_;
189         my $qp = $ctx->{qp};
190         my $hints = $ctx->{hints} = {};
191         while (my ($from, $to) = each %QP_MAP) {
192                 defined(my $v = $qp->{$from}) or next;
193                 $hints->{$to} = $v;
194         }
195
196         $ctx->{'log'} = tmpfile("solve.$oid_b");
197         $ctx->{fn} = $fn;
198         my $solver = PublicInbox::SolverGit->new($ctx->{-inbox},
199                                                 \&solve_result, $ctx);
200         # PSGI server will call this immediately and give us a callback (-wcb)
201         sub {
202                 $ctx->{-wcb} = $_[0]; # HTTP write callback
203                 $solver->solve($ctx->{env}, $ctx->{log}, $oid_b, $hints);
204         };
205 }
206
207 1;