]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WwwListing.pm
{gzip,noop}filter: ->zmore returns undef, always
[public-inbox.git] / lib / PublicInbox / WwwListing.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 # Provide an HTTP-accessible listing of inboxes.
5 # Used by PublicInbox::WWW
6 package PublicInbox::WwwListing;
7 use strict;
8 use warnings;
9 use PublicInbox::Hval qw(ascii_html prurl);
10 use PublicInbox::Linkify;
11 use PublicInbox::View;
12 use PublicInbox::Inbox;
13 use PublicInbox::NoopFilter;
14 use PublicInbox::GzipFilter qw(gzf_maybe);
15 use bytes (); # bytes::length
16 use HTTP::Date qw(time2str);
17 use Digest::SHA ();
18 use File::Spec ();
19 use IO::Compress::Gzip qw(gzip);
20 *try_cat = \&PublicInbox::Inbox::try_cat;
21 our $json;
22 for my $mod (qw(JSON::MaybeXS JSON JSON::PP)) {
23         eval "require $mod" or next;
24         # ->ascii encodes non-ASCII to "\uXXXX"
25         $json = $mod->new->ascii(1) and last;
26 }
27
28 sub list_all_i {
29         my ($ibx, $arg) = @_;
30         my ($list, $hide_key) = @$arg;
31         push @$list, $ibx unless $ibx->{-hide}->{$hide_key};
32 }
33
34 sub list_all ($$$) {
35         my ($self, $env, $hide_key) = @_;
36         my $list = [];
37         $self->{pi_config}->each_inbox(\&list_all_i, [ $list, $hide_key ]);
38         $list;
39 }
40
41 sub list_match_domain_i {
42         my ($ibx, $arg) = @_;
43         my ($list, $hide_key, $re) = @$arg;
44         if (!$ibx->{-hide}->{$hide_key} && grep($re, @{$ibx->{url}})) {
45                 push @$list, $ibx;
46         }
47 }
48
49 sub list_match_domain ($$$) {
50         my ($self, $env, $hide_key) = @_;
51         my $list = [];
52         my $host = $env->{HTTP_HOST} // $env->{SERVER_NAME};
53         $host =~ s/:[0-9]+\z//;
54         my $arg = [ $list, $hide_key,
55                 qr!\A(?:https?:)?//\Q$host\E(?::[0-9]+)?/!i ];
56         $self->{pi_config}->each_inbox(\&list_match_domain_i, $arg);
57         $list;
58 }
59
60 sub list_404 ($$) { [] }
61
62 # TODO: +cgit
63 my %VALID = (
64         all => *list_all,
65         'match=domain' => *list_match_domain,
66         404 => *list_404,
67 );
68
69 sub set_cb ($$$) {
70         my ($pi_config, $k, $default) = @_;
71         my $v = $pi_config->{lc $k} // $default;
72         $VALID{$v} || do {
73                 warn <<"";
74 `$v' is not a valid value for `$k'
75 $k be one of `all', `match=domain', or `404'
76
77                 $VALID{$default};
78         };
79 }
80
81 sub new {
82         my ($class, $www) = @_;
83         my $pi_config = $www->{pi_config};
84         bless {
85                 pi_config => $pi_config,
86                 style => $www->style("\0"),
87                 www_cb => set_cb($pi_config, 'publicInbox.wwwListing', 404),
88                 manifest_cb => set_cb($pi_config, 'publicInbox.grokManifest',
89                                         'match=domain'),
90         }, $class;
91 }
92
93 sub ibx_entry {
94         my ($mtime, $ibx, $env) = @_;
95         my $ts = PublicInbox::View::fmt_ts($mtime);
96         my $url = prurl($env, $ibx->{url});
97         my $tmp = <<"";
98 * $ts - $url
99   ${\$ibx->description}
100
101         if (defined(my $info_url = $ibx->{infourl})) {
102                 $tmp .= '  ' . prurl($env, $info_url) . "\n";
103         }
104         $tmp;
105 }
106
107 sub html ($$) {
108         my ($env, $list) = @_;
109         my $h = [ 'Content-Type', 'text/html; charset=UTF-8',
110                         'Content-Length', undef ];
111         my $gzf = gzf_maybe($h, $env) || PublicInbox::NoopFilter::new();
112         $gzf->zmore('<html><head><title>' .
113                                 'public-inbox listing</title>' .
114                                 '</head><body><pre>');
115         my $code = 404;
116         if (@$list) {
117                 $code = 200;
118                 # Schwartzian transform since Inbox->modified is expensive
119                 @$list = sort {
120                         $b->[0] <=> $a->[0]
121                 } map { [ $_->modified, $_ ] } @$list;
122
123                 my $tmp = join("\n", map { ibx_entry(@$_, $env) } @$list);
124                 my $l = PublicInbox::Linkify->new;
125                 $gzf->zmore($l->to_html($tmp));
126         } else {
127                 $gzf->zmore('no inboxes, yet');
128         }
129         my $out = $gzf->zflush('</pre><hr><pre>'.
130                                 PublicInbox::WwwStream::code_footer($env) .
131                                 '</pre></body></html>');
132         $h->[3] = bytes::length($out);
133         [ $code, $h, [ $out ] ];
134 }
135
136 sub fingerprint ($) {
137         my ($git) = @_;
138         # TODO: convert to qspawn for fairness when there's
139         # thousands of repos
140         my ($fh, $pid) = $git->popen('show-ref');
141         my $dig = Digest::SHA->new(1);
142         while (read($fh, my $buf, 65536)) {
143                 $dig->add($buf);
144         }
145         close $fh;
146         waitpid($pid, 0);
147         return if $?; # empty, uninitialized git repo
148         $dig->hexdigest;
149 }
150
151 sub manifest_add ($$;$$) {
152         my ($manifest, $ibx, $epoch, $default_desc) = @_;
153         my $url_path = "/$ibx->{name}";
154         my $git_dir = $ibx->{inboxdir};
155         if (defined $epoch) {
156                 $git_dir .= "/git/$epoch.git";
157                 $url_path .= "/git/$epoch.git";
158         }
159         return unless -d $git_dir;
160         my $git = PublicInbox::Git->new($git_dir);
161         my $fingerprint = fingerprint($git) or return; # no empty repos
162
163         chomp(my $owner = $git->qx('config', 'gitweb.owner'));
164         chomp(my $desc = try_cat("$git_dir/description"));
165         utf8::decode($owner);
166         utf8::decode($desc);
167         $owner = undef if $owner eq '';
168         $desc = 'Unnamed repository' if $desc eq '';
169
170         # templates/hooks--update.sample and git-multimail in git.git
171         # only match "Unnamed repository", not the full contents of
172         # templates/this--description in git.git
173         if ($desc =~ /\AUnnamed repository/) {
174                 $desc = "$default_desc [epoch $epoch]" if defined($epoch);
175         }
176
177         my $reference;
178         chomp(my $alt = try_cat("$git_dir/objects/info/alternates"));
179         if ($alt) {
180                 # n.b.: GitPython doesn't seem to handle comments or C-quoted
181                 # strings like native git does; and we don't for now, either.
182                 my @alt = split(/\n+/, $alt);
183
184                 # grokmirror only supports 1 alternate for "reference",
185                 if (scalar(@alt) == 1) {
186                         my $objdir = "$git_dir/objects";
187                         $reference = File::Spec->rel2abs($alt[0], $objdir);
188                         $reference =~ s!/[^/]+/?\z!!; # basename
189                 }
190         }
191         $manifest->{-abs2urlpath}->{$git_dir} = $url_path;
192         my $modified = $git->modified;
193         if ($modified > $manifest->{-mtime}) {
194                 $manifest->{-mtime} = $modified;
195         }
196         $manifest->{$url_path} = {
197                 owner => $owner,
198                 reference => $reference,
199                 description => $desc,
200                 modified => $modified,
201                 fingerprint => $fingerprint,
202         };
203 }
204
205 # manifest.js.gz
206 sub js ($$) {
207         my ($env, $list) = @_;
208         # $json won't be defined if IO::Compress::Gzip is missing
209         $json or return [ 404, [], [] ];
210
211         my $manifest = { -abs2urlpath => {}, -mtime => 0 };
212         for my $ibx (@$list) {
213                 if (defined(my $max = $ibx->max_git_epoch)) {
214                         my $desc = $ibx->description;
215                         for my $epoch (0..$max) {
216                                 manifest_add($manifest, $ibx, $epoch, $desc);
217                         }
218                 } else {
219                         manifest_add($manifest, $ibx);
220                 }
221         }
222         my $abs2urlpath = delete $manifest->{-abs2urlpath};
223         my $mtime = delete $manifest->{-mtime};
224         while (my ($url_path, $repo) = each %$manifest) {
225                 defined(my $abs = $repo->{reference}) or next;
226                 $repo->{reference} = $abs2urlpath->{$abs};
227         }
228         my $out;
229         gzip(\($json->encode($manifest)) => \$out);
230         $manifest = undef;
231         [ 200, [ qw(Content-Type application/gzip),
232                  'Last-Modified', time2str($mtime),
233                  'Content-Length', bytes::length($out) ], [ $out ] ];
234 }
235
236 # not really a stand-alone PSGI app, but maybe it could be...
237 sub call {
238         my ($self, $env) = @_;
239
240         if ($env->{PATH_INFO} eq '/manifest.js.gz') {
241                 # grokmirror uses relative paths, so it's domain-dependent
242                 my $list = $self->{manifest_cb}->($self, $env, 'manifest');
243                 js($env, $list);
244         } else { # /
245                 my $list = $self->{www_cb}->($self, $env, 'www');
246                 html($env, $list);
247         }
248 }
249
250 1;