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