1 # Copyright (C) 2019 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);
10 use PublicInbox::Linkify;
11 use PublicInbox::View;
12 use PublicInbox::Inbox;
14 use HTTP::Date qw(time2str);
17 *try_cat = \&PublicInbox::Inbox::try_cat;
20 my ($self, $env, $hide_key) = @_;
22 $self->{pi_config}->each_inbox(sub {
24 push @list, $ibx unless $ibx->{-hide}->{$hide_key};
29 sub list_match_domain ($$$) {
30 my ($self, $env, $hide_key) = @_;
32 my $host = $env->{HTTP_HOST} // $env->{SERVER_NAME};
33 $host =~ s/:[0-9]+\z//;
34 my $re = qr!\A(?:https?:)?//\Q$host\E(?::[0-9]+)?/!i;
35 $self->{pi_config}->each_inbox(sub {
37 if (!$ibx->{-hide}->{$hide_key} && $ibx->{url} =~ $re) {
44 sub list_404 ($$) { [] }
49 'match=domain' => *list_match_domain,
54 my ($pi_config, $k, $default) = @_;
55 my $v = $pi_config->{lc $k} // $default;
58 `$v' is not a valid value for `$k'
59 $k be one of `all', `match=domain', or `404'
66 my ($class, $www) = @_;
67 my $pi_config = $www->{pi_config};
69 pi_config => $pi_config,
70 style => $www->style("\0"),
71 www_cb => set_cb($pi_config, 'publicInbox.wwwListing', 404),
72 manifest_cb => set_cb($pi_config, 'publicInbox.grokManifest',
78 my ($mtime, $ibx, $env) = @_;
79 my $ts = PublicInbox::View::fmt_ts($mtime);
80 my $url = PublicInbox::Hval::prurl($env, $ibx->{url});
85 if (defined(my $info_url = $ibx->{info_url})) {
86 $tmp .= "\n$info_url";
92 my ($env, $list) = @_;
93 my $title = 'public-inbox';
97 $title .= ' - listing';
100 # Schwartzian transform since Inbox->modified is expensive
103 } map { [ $_->modified, $_ ] } @$list;
105 my $tmp = join("\n", map { ibx_entry(@$_, $env) } @$list);
106 my $l = PublicInbox::Linkify->new;
108 $out = '<pre>'.$l->linkify_2(ascii_html($tmp)).'</pre><hr>';
110 $out = "<html><head><title>$title</title></head><body>" . $out;
111 $out .= '<pre>'. PublicInbox::WwwStream::code_footer($env) .
112 '</pre></body></html>';
114 my $h = [ 'Content-Type', 'text/html; charset=UTF-8' ];
115 [ $code, $h, [ $out ] ];
120 for my $mod (qw(JSON::MaybeXS JSON JSON::PP)) {
121 eval "require $mod" or next;
122 # ->ascii encodes non-ASCII to "\uXXXX"
123 return $mod->new->ascii(1);
128 sub fingerprint ($) {
130 # TODO: convert to qspawn for fairness when there's
132 my ($fh, $pid) = $git->popen('show-ref') or
133 die "popen($git->{git_dir} show-ref) failed: $!";
135 my $dig = Digest::SHA->new(1);
136 while (read($fh, my $buf, 65536)) {
141 return if $?; # empty, uninitialized git repo
145 sub manifest_add ($$;$$) {
146 my ($manifest, $ibx, $epoch, $default_desc) = @_;
147 my $url_path = "/$ibx->{name}";
148 my $git_dir = $ibx->{inboxdir};
149 if (defined $epoch) {
150 $git_dir .= "/git/$epoch.git";
151 $url_path .= "/git/$epoch.git";
153 return unless -d $git_dir;
154 my $git = PublicInbox::Git->new($git_dir);
155 my $fingerprint = fingerprint($git) or return; # no empty repos
157 chomp(my $owner = $git->qx('config', 'gitweb.owner'));
158 chomp(my $desc = try_cat("$git_dir/description"));
159 $owner = undef if $owner eq '';
160 $desc = 'Unnamed repository' if $desc eq '';
162 # templates/hooks--update.sample and git-multimail in git.git
163 # only match "Unnamed repository", not the full contents of
164 # templates/this--description in git.git
165 if ($desc =~ /\AUnnamed repository/) {
166 $desc = "$default_desc [epoch $epoch]" if defined($epoch);
170 chomp(my $alt = try_cat("$git_dir/objects/info/alternates"));
172 # n.b.: GitPython doesn't seem to handle comments or C-quoted
173 # strings like native git does; and we don't for now, either.
174 my @alt = split(/\n+/, $alt);
176 # grokmirror only supports 1 alternate for "reference",
177 if (scalar(@alt) == 1) {
178 my $objdir = "$git_dir/objects";
179 $reference = File::Spec->rel2abs($alt[0], $objdir);
180 $reference =~ s!/[^/]+/?\z!!; # basename
183 $manifest->{-abs2urlpath}->{$git_dir} = $url_path;
184 my $modified = $git->modified;
185 if ($modified > $manifest->{-mtime}) {
186 $manifest->{-mtime} = $modified;
188 $manifest->{$url_path} = {
190 reference => $reference,
191 description => $desc,
192 modified => $modified,
193 fingerprint => $fingerprint,
199 my ($env, $list) = @_;
200 eval { require IO::Compress::Gzip } or return [ 404, [], [] ];
202 my $manifest = { -abs2urlpath => {}, -mtime => 0 };
203 for my $ibx (@$list) {
204 if (defined(my $max = $ibx->max_git_epoch)) {
205 my $desc = $ibx->description;
206 for my $epoch (0..$max) {
207 manifest_add($manifest, $ibx, $epoch, $desc);
210 manifest_add($manifest, $ibx);
213 my $abs2urlpath = delete $manifest->{-abs2urlpath};
214 my $mtime = delete $manifest->{-mtime};
215 while (my ($url_path, $repo) = each %$manifest) {
216 defined(my $abs = $repo->{reference}) or next;
217 $repo->{reference} = $abs2urlpath->{$abs};
220 IO::Compress::Gzip::gzip(\(($json ||= _json())->encode($manifest)) =>
223 [ 200, [ qw(Content-Type application/gzip),
224 'Last-Modified', time2str($mtime),
225 'Content-Length', bytes::length($out) ], [ $out ] ];
228 # not really a stand-alone PSGI app, but maybe it could be...
230 my ($self, $env) = @_;
232 if ($env->{PATH_INFO} eq '/manifest.js.gz') {
233 # grokmirror uses relative paths, so it's domain-dependent
234 my $list = $self->{manifest_cb}->($self, $env, 'manifest');
237 my $list = $self->{www_cb}->($self, $env, 'www');