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