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>
4 # Provide an HTTP-accessible listing of inboxes.
5 # Used by PublicInbox::WWW
6 package PublicInbox::WwwListing;
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);
19 use IO::Compress::Gzip qw(gzip);
20 *try_cat = \&PublicInbox::Inbox::try_cat;
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;
30 my ($list, $hide_key) = @$arg;
31 push @$list, $ibx unless $ibx->{-hide}->{$hide_key};
35 my ($self, $env, $hide_key) = @_;
37 $self->{pi_config}->each_inbox(\&list_all_i, [ $list, $hide_key ]);
41 sub list_match_domain_i {
43 my ($list, $hide_key, $re) = @$arg;
44 if (!$ibx->{-hide}->{$hide_key} && grep($re, @{$ibx->{url}})) {
49 sub list_match_domain ($$$) {
50 my ($self, $env, $hide_key) = @_;
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);
60 sub list_404 ($$) { [] }
65 'match=domain' => *list_match_domain,
70 my ($pi_config, $k, $default) = @_;
71 my $v = $pi_config->{lc $k} // $default;
74 `$v' is not a valid value for `$k'
75 $k be one of `all', `match=domain', or `404'
82 my ($class, $www) = @_;
83 my $pi_config = $www->{pi_config};
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',
94 my ($mtime, $ibx, $env) = @_;
95 my $ts = PublicInbox::View::fmt_ts($mtime);
96 my $url = prurl($env, $ibx->{url});
101 if (defined(my $info_url = $ibx->{infourl})) {
102 $tmp .= ' ' . prurl($env, $info_url) . "\n";
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 my $out = $gzf->zmore('<html><head><title>' .
113 'public-inbox listing</title>' .
114 '</head><body><pre>');
118 # Schwartzian transform since Inbox->modified is expensive
121 } map { [ $_->modified, $_ ] } @$list;
123 my $tmp = join("\n", map { ibx_entry(@$_, $env) } @$list);
124 my $l = PublicInbox::Linkify->new;
125 $out .= $gzf->zmore($l->to_html($tmp));
127 $out .= $gzf->zmore('no inboxes, yet');
129 $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 ] ];
136 sub fingerprint ($) {
138 # TODO: convert to qspawn for fairness when there's
140 my ($fh, $pid) = $git->popen('show-ref');
141 my $dig = Digest::SHA->new(1);
142 while (read($fh, my $buf, 65536)) {
147 return if $?; # empty, uninitialized git repo
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";
159 return unless -d $git_dir;
160 my $git = PublicInbox::Git->new($git_dir);
161 my $fingerprint = fingerprint($git) or return; # no empty repos
163 chomp(my $owner = $git->qx('config', 'gitweb.owner'));
164 chomp(my $desc = try_cat("$git_dir/description"));
165 utf8::decode($owner);
167 $owner = undef if $owner eq '';
168 $desc = 'Unnamed repository' if $desc eq '';
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);
178 chomp(my $alt = try_cat("$git_dir/objects/info/alternates"));
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);
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
191 $manifest->{-abs2urlpath}->{$git_dir} = $url_path;
192 my $modified = $git->modified;
193 if ($modified > $manifest->{-mtime}) {
194 $manifest->{-mtime} = $modified;
196 $manifest->{$url_path} = {
198 reference => $reference,
199 description => $desc,
200 modified => $modified,
201 fingerprint => $fingerprint,
207 my ($env, $list) = @_;
208 # $json won't be defined if IO::Compress::Gzip is missing
209 $json or return [ 404, [], [] ];
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);
219 manifest_add($manifest, $ibx);
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};
229 gzip(\($json->encode($manifest)) => \$out);
231 [ 200, [ qw(Content-Type application/gzip),
232 'Last-Modified', time2str($mtime),
233 'Content-Length', bytes::length($out) ], [ $out ] ];
236 # not really a stand-alone PSGI app, but maybe it could be...
238 my ($self, $env) = @_;
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');
245 my $list = $self->{www_cb}->($self, $env, 'www');