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