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