]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/ViewVCS.pm
viewvcs: cleanup utf8 handling
[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 PublicInbox::SolverGit;
20 use PublicInbox::WwwStream;
21 use PublicInbox::Linkify;
22 use PublicInbox::Hval qw(ascii_html to_filename src_escape);
23 my $hl = eval {
24         require PublicInbox::HlMod;
25         PublicInbox::HlMod->new;
26 };
27
28 # we need to trigger highlight::CodeGenerator::deleteInstance
29 # in HlMod::DESTROY before the rest of Perl shuts down to avoid
30 # a segfault at shutdown
31 END { $hl = undef };
32
33 my %QP_MAP = ( A => 'oid_a', B => 'oid_b', a => 'path_a', b => 'path_b' );
34 my $max_size = 1024 * 1024; # TODO: configurable
35 my $BIN_DETECT = 8000; # same as git
36
37 sub html_page ($$$) {
38         my ($ctx, $code, $strref) = @_;
39         my $wcb = delete $ctx->{-wcb};
40         $ctx->{-upfx} = '../../'; # from "/$INBOX/$OID/s/"
41         my $res = PublicInbox::WwwStream->response($ctx, $code, sub {
42                 my ($nr, undef) =  @_;
43                 $nr == 1 ? $$strref : undef;
44         });
45         $wcb ? $wcb->($res) : $res;
46 }
47
48 sub stream_large_blob ($$$$) {
49         my ($ctx, $res, $logref, $fn) = @_;
50         my ($git, $oid, $type, $size, $di) = @$res;
51         my $cmd = ['git', "--git-dir=$git->{git_dir}", 'cat-file', $type, $oid];
52         my $qsp = PublicInbox::Qspawn->new($cmd);
53         my @cl = ('Content-Length', $size);
54         my $env = $ctx->{env};
55         $env->{'qspawn.wcb'} = delete $ctx->{-wcb};
56         $qsp->psgi_return($env, undef, sub {
57                 my ($r, $bref) = @_;
58                 if (!defined $r) { # error
59                         html_page($ctx, 500, $logref);
60                 } elsif (index($$bref, "\0") >= 0) {
61                         my $ct = 'application/octet-stream';
62                         [200, ['Content-Type', $ct, @cl ] ];
63                 } else {
64                         my $n = bytes::length($$bref);
65                         if ($n >= $BIN_DETECT || $n == $size) {
66                                 my $ct = 'text/plain; charset=UTF-8';
67                                 return [200, ['Content-Type', $ct, @cl] ];
68                         }
69                         undef; # bref keeps growing
70                 }
71         });
72 }
73
74 sub solve_result {
75         my ($ctx, $res, $log, $hints, $fn) = @_;
76
77         unless (seek($log, 0, 0)) {
78                 $ctx->{env}->{'psgi.errors'}->print("seek(log): $!\n");
79                 return html_page($ctx, 500, \'seek error');
80         }
81         $log = do { local $/; <$log> };
82
83         my $ref = ref($res);
84         my $l = PublicInbox::Linkify->new;
85         $l->linkify_1($log);
86         $log = '<pre>debug log:</pre><hr /><pre>' .
87                 $l->linkify_2(ascii_html($log)) . '</pre>';
88
89         $res or return html_page($ctx, 404, \$log);
90         $ref eq 'ARRAY' or return html_page($ctx, 500, \$log);
91
92         my ($git, $oid, $type, $size, $di) = @$res;
93         my $path = to_filename($di->{path_b} || $hints->{path_b} || 'blob');
94         my $raw_link = "(<a\nhref=$path>raw</a>)";
95         if ($size > $max_size) {
96                 return stream_large_blob($ctx, $res, \$log, $fn) if defined $fn;
97                 $log = "<pre><b>Too big to show, download available</b>\n" .
98                         "$oid $type $size bytes $raw_link</pre>" . $log;
99                 return html_page($ctx, 500, \$log);
100         }
101
102         my $blob = $git->cat_file($oid);
103         if (!$blob) { # WTF?
104                 my $e = "Failed to retrieve generated blob ($oid)";
105                 $ctx->{env}->{'psgi.errors'}->print("$e ($git->{git_dir})\n");
106                 $log = "<pre><b>$e</b></pre>" . $log;
107                 return html_page($ctx, 500, \$log);
108         }
109
110         my $binary = index($$blob, "\0") >= 0;
111         if (defined $fn) {
112                 my $h = [ 'Content-Length', $size, 'Content-Type' ];
113                 push(@$h, ($binary ? 'application/octet-stream' : 'text/plain'));
114                 return delete($ctx->{-wcb})->([200, $h, [ $$blob ]]);
115         }
116
117         if ($binary) {
118                 $log = "<pre>$oid $type $size bytes (binary)" .
119                         " $raw_link</pre>" . $log;
120                 return html_page($ctx, 200, \$log);
121         }
122
123         # TODO: detect + convert to ensure validity
124         utf8::decode($$blob);
125         my $nl = ($$blob =~ tr/\n/\n/);
126         my $pad = length($nl);
127
128         $l->linkify_1($$blob);
129         my $ok = $hl->do_hl($blob, $path) if $hl;
130         if ($ok) {
131                 src_escape($$ok);
132                 $blob = $ok;
133         } else {
134                 $$blob = ascii_html($$blob);
135         }
136
137         # using some of the same CSS class names and ids as cgit
138         $log = "<pre>$oid $type $size bytes $raw_link</pre>" .
139                 "<hr /><table\nclass=blob>".
140                 "<tr><td\nclass=linenumbers><pre>" . join('', map {
141                         sprintf("<a id=n$_ href=#n$_>% ${pad}u</a>\n", $_)
142                 } (1..$nl)) . '</pre></td>' .
143                 '<td><pre> </pre></td>'. # pad for non-CSS users
144                 "<td\nclass=lines><pre\nstyle='white-space:pre'><code>" .
145                 $l->linkify_2($$blob) .
146                 '</code></pre></td></tr></table>' . $log;
147
148         html_page($ctx, 200, \$log);
149 }
150
151 sub show ($$;$) {
152         my ($ctx, $oid_b, $fn) = @_;
153         my $qp = $ctx->{qp};
154         my $hints = {};
155         while (my ($from, $to) = each %QP_MAP) {
156                 defined(my $v = $qp->{$from}) or next;
157                 $hints->{$to} = $v;
158         }
159
160         open my $log, '+>', undef or die "open: $!";
161         my $solver = PublicInbox::SolverGit->new($ctx->{-inbox}, sub {
162                 solve_result($ctx, $_[0], $log, $hints, $fn);
163         });
164
165         # PSGI server will call this and give us a callback
166         sub {
167                 $ctx->{-wcb} = $_[0]; # HTTP write callback
168                 $solver->solve($ctx->{env}, $log, $oid_b, $hints);
169         };
170 }
171
172 1;