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 bytes (); # bytes::length
14 use HTTP::Date qw(time2str);
17 *try_cat = \&PublicInbox::Inbox::try_cat;
19 if (eval { require IO::Compress::Gzip }) {
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;
29 my ($list, $hide_key) = @$arg;
30 push @$list, $ibx unless $ibx->{-hide}->{$hide_key};
34 my ($self, $env, $hide_key) = @_;
36 $self->{pi_config}->each_inbox(\&list_all_i, [ $list, $hide_key ]);
40 sub list_match_domain_i {
42 my ($list, $hide_key, $re) = @$arg;
43 if (!$ibx->{-hide}->{$hide_key} && grep($re, @{$ibx->{url}})) {
48 sub list_match_domain ($$$) {
49 my ($self, $env, $hide_key) = @_;
51 my $host = $env->{HTTP_HOST} // $env->{SERVER_NAME};
52 $host =~ s/:[0-9]+\z//;
53 my $arg = [ $list, $hide_key,
54 qr!\A(?:https?:)?//\Q$host\E(?::[0-9]+)?/!i ];
55 $self->{pi_config}->each_inbox(\&list_match_domain_i, $arg);
59 sub list_404 ($$) { [] }
64 'match=domain' => *list_match_domain,
69 my ($pi_config, $k, $default) = @_;
70 my $v = $pi_config->{lc $k} // $default;
73 `$v' is not a valid value for `$k'
74 $k be one of `all', `match=domain', or `404'
81 my ($class, $www) = @_;
82 my $pi_config = $www->{pi_config};
84 pi_config => $pi_config,
85 style => $www->style("\0"),
86 www_cb => set_cb($pi_config, 'publicInbox.wwwListing', 404),
87 manifest_cb => set_cb($pi_config, 'publicInbox.grokManifest',
93 my ($mtime, $ibx, $env) = @_;
94 my $ts = PublicInbox::View::fmt_ts($mtime);
95 my $url = prurl($env, $ibx->{url});
100 if (defined(my $info_url = $ibx->{infourl})) {
101 $tmp .= ' ' . prurl($env, $info_url) . "\n";
107 my ($env, $list) = @_;
108 my $title = 'public-inbox';
112 $title .= ' - listing';
115 # Schwartzian transform since Inbox->modified is expensive
118 } map { [ $_->modified, $_ ] } @$list;
120 my $tmp = join("\n", map { ibx_entry(@$_, $env) } @$list);
121 my $l = PublicInbox::Linkify->new;
122 $out = '<pre>'.$l->to_html($tmp).'</pre><hr>';
124 $out = "<html><head><title>$title</title></head><body>" . $out;
125 $out .= '<pre>'. PublicInbox::WwwStream::code_footer($env) .
126 '</pre></body></html>';
128 my $h = [ 'Content-Type', 'text/html; charset=UTF-8' ];
129 [ $code, $h, [ $out ] ];
132 sub fingerprint ($) {
134 # TODO: convert to qspawn for fairness when there's
136 my ($fh, $pid) = $git->popen('show-ref');
137 my $dig = Digest::SHA->new(1);
138 while (read($fh, my $buf, 65536)) {
143 return if $?; # empty, uninitialized git repo
147 sub manifest_add ($$;$$) {
148 my ($manifest, $ibx, $epoch, $default_desc) = @_;
149 my $url_path = "/$ibx->{name}";
150 my $git_dir = $ibx->{inboxdir};
151 if (defined $epoch) {
152 $git_dir .= "/git/$epoch.git";
153 $url_path .= "/git/$epoch.git";
155 return unless -d $git_dir;
156 my $git = PublicInbox::Git->new($git_dir);
157 my $fingerprint = fingerprint($git) or return; # no empty repos
159 chomp(my $owner = $git->qx('config', 'gitweb.owner'));
160 chomp(my $desc = try_cat("$git_dir/description"));
161 $owner = undef if $owner eq '';
162 $desc = 'Unnamed repository' if $desc eq '';
164 # templates/hooks--update.sample and git-multimail in git.git
165 # only match "Unnamed repository", not the full contents of
166 # templates/this--description in git.git
167 if ($desc =~ /\AUnnamed repository/) {
168 $desc = "$default_desc [epoch $epoch]" if defined($epoch);
172 chomp(my $alt = try_cat("$git_dir/objects/info/alternates"));
174 # n.b.: GitPython doesn't seem to handle comments or C-quoted
175 # strings like native git does; and we don't for now, either.
176 my @alt = split(/\n+/, $alt);
178 # grokmirror only supports 1 alternate for "reference",
179 if (scalar(@alt) == 1) {
180 my $objdir = "$git_dir/objects";
181 $reference = File::Spec->rel2abs($alt[0], $objdir);
182 $reference =~ s!/[^/]+/?\z!!; # basename
185 $manifest->{-abs2urlpath}->{$git_dir} = $url_path;
186 my $modified = $git->modified;
187 if ($modified > $manifest->{-mtime}) {
188 $manifest->{-mtime} = $modified;
190 $manifest->{$url_path} = {
192 reference => $reference,
193 description => $desc,
194 modified => $modified,
195 fingerprint => $fingerprint,
201 my ($env, $list) = @_;
202 # $json won't be defined if IO::Compress::Gzip is missing
203 $json or return [ 404, [], [] ];
205 my $manifest = { -abs2urlpath => {}, -mtime => 0 };
206 for my $ibx (@$list) {
207 if (defined(my $max = $ibx->max_git_epoch)) {
208 my $desc = $ibx->description;
209 for my $epoch (0..$max) {
210 manifest_add($manifest, $ibx, $epoch, $desc);
213 manifest_add($manifest, $ibx);
216 my $abs2urlpath = delete $manifest->{-abs2urlpath};
217 my $mtime = delete $manifest->{-mtime};
218 while (my ($url_path, $repo) = each %$manifest) {
219 defined(my $abs = $repo->{reference}) or next;
220 $repo->{reference} = $abs2urlpath->{$abs};
223 IO::Compress::Gzip::gzip(\($json->encode($manifest)) => \$out);
225 [ 200, [ qw(Content-Type application/gzip),
226 'Last-Modified', time2str($mtime),
227 'Content-Length', bytes::length($out) ], [ $out ] ];
230 # not really a stand-alone PSGI app, but maybe it could be...
232 my ($self, $env) = @_;
234 if ($env->{PATH_INFO} eq '/manifest.js.gz') {
235 # grokmirror uses relative paths, so it's domain-dependent
236 my $list = $self->{manifest_cb}->($self, $env, 'manifest');
239 my $list = $self->{www_cb}->($self, $env, 'www');