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 prurl);
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;
21 my ($list, $hide_key) = @$arg;
22 push @$list, $ibx unless $ibx->{-hide}->{$hide_key};
26 my ($self, $env, $hide_key) = @_;
28 $self->{pi_config}->each_inbox(\&list_all_i, [ $list, $hide_key ]);
32 sub list_match_domain_i {
34 my ($list, $hide_key, $re) = @$arg;
35 if (!$ibx->{-hide}->{$hide_key} && grep($re, @{$ibx->{url}})) {
40 sub list_match_domain ($$$) {
41 my ($self, $env, $hide_key) = @_;
43 my $host = $env->{HTTP_HOST} // $env->{SERVER_NAME};
44 $host =~ s/:[0-9]+\z//;
45 my $arg = [ $list, $hide_key,
46 qr!\A(?:https?:)?//\Q$host\E(?::[0-9]+)?/!i ];
47 $self->{pi_config}->each_inbox(\&list_match_domain_i, $arg);
51 sub list_404 ($$) { [] }
56 'match=domain' => *list_match_domain,
61 my ($pi_config, $k, $default) = @_;
62 my $v = $pi_config->{lc $k} // $default;
65 `$v' is not a valid value for `$k'
66 $k be one of `all', `match=domain', or `404'
73 my ($class, $www) = @_;
74 my $pi_config = $www->{pi_config};
76 pi_config => $pi_config,
77 style => $www->style("\0"),
78 www_cb => set_cb($pi_config, 'publicInbox.wwwListing', 404),
79 manifest_cb => set_cb($pi_config, 'publicInbox.grokManifest',
85 my ($mtime, $ibx, $env) = @_;
86 my $ts = PublicInbox::View::fmt_ts($mtime);
87 my $url = prurl($env, $ibx->{url});
92 if (defined(my $info_url = $ibx->{infourl})) {
93 $tmp .= ' ' . prurl($env, $info_url) . "\n";
99 my ($env, $list) = @_;
100 my $title = 'public-inbox';
104 $title .= ' - listing';
107 # Schwartzian transform since Inbox->modified is expensive
110 } map { [ $_->modified, $_ ] } @$list;
112 my $tmp = join("\n", map { ibx_entry(@$_, $env) } @$list);
113 my $l = PublicInbox::Linkify->new;
114 $out = '<pre>'.$l->to_html($tmp).'</pre><hr>';
116 $out = "<html><head><title>$title</title></head><body>" . $out;
117 $out .= '<pre>'. PublicInbox::WwwStream::code_footer($env) .
118 '</pre></body></html>';
120 my $h = [ 'Content-Type', 'text/html; charset=UTF-8' ];
121 [ $code, $h, [ $out ] ];
126 for my $mod (qw(JSON::MaybeXS JSON JSON::PP)) {
127 eval "require $mod" or next;
128 # ->ascii encodes non-ASCII to "\uXXXX"
129 return $mod->new->ascii(1);
134 sub fingerprint ($) {
136 # TODO: convert to qspawn for fairness when there's
138 my ($fh, $pid) = $git->popen('show-ref');
139 my $dig = Digest::SHA->new(1);
140 while (read($fh, my $buf, 65536)) {
145 return if $?; # empty, uninitialized git repo
149 sub manifest_add ($$;$$) {
150 my ($manifest, $ibx, $epoch, $default_desc) = @_;
151 my $url_path = "/$ibx->{name}";
152 my $git_dir = $ibx->{inboxdir};
153 if (defined $epoch) {
154 $git_dir .= "/git/$epoch.git";
155 $url_path .= "/git/$epoch.git";
157 return unless -d $git_dir;
158 my $git = PublicInbox::Git->new($git_dir);
159 my $fingerprint = fingerprint($git) or return; # no empty repos
161 chomp(my $owner = $git->qx('config', 'gitweb.owner'));
162 chomp(my $desc = try_cat("$git_dir/description"));
163 $owner = undef if $owner eq '';
164 $desc = 'Unnamed repository' if $desc eq '';
166 # templates/hooks--update.sample and git-multimail in git.git
167 # only match "Unnamed repository", not the full contents of
168 # templates/this--description in git.git
169 if ($desc =~ /\AUnnamed repository/) {
170 $desc = "$default_desc [epoch $epoch]" if defined($epoch);
174 chomp(my $alt = try_cat("$git_dir/objects/info/alternates"));
176 # n.b.: GitPython doesn't seem to handle comments or C-quoted
177 # strings like native git does; and we don't for now, either.
178 my @alt = split(/\n+/, $alt);
180 # grokmirror only supports 1 alternate for "reference",
181 if (scalar(@alt) == 1) {
182 my $objdir = "$git_dir/objects";
183 $reference = File::Spec->rel2abs($alt[0], $objdir);
184 $reference =~ s!/[^/]+/?\z!!; # basename
187 $manifest->{-abs2urlpath}->{$git_dir} = $url_path;
188 my $modified = $git->modified;
189 if ($modified > $manifest->{-mtime}) {
190 $manifest->{-mtime} = $modified;
192 $manifest->{$url_path} = {
194 reference => $reference,
195 description => $desc,
196 modified => $modified,
197 fingerprint => $fingerprint,
203 my ($env, $list) = @_;
204 eval { require IO::Compress::Gzip } or return [ 404, [], [] ];
206 my $manifest = { -abs2urlpath => {}, -mtime => 0 };
207 for my $ibx (@$list) {
208 if (defined(my $max = $ibx->max_git_epoch)) {
209 my $desc = $ibx->description;
210 for my $epoch (0..$max) {
211 manifest_add($manifest, $ibx, $epoch, $desc);
214 manifest_add($manifest, $ibx);
217 my $abs2urlpath = delete $manifest->{-abs2urlpath};
218 my $mtime = delete $manifest->{-mtime};
219 while (my ($url_path, $repo) = each %$manifest) {
220 defined(my $abs = $repo->{reference}) or next;
221 $repo->{reference} = $abs2urlpath->{$abs};
224 IO::Compress::Gzip::gzip(\(($json ||= _json())->encode($manifest)) =>
227 [ 200, [ qw(Content-Type application/gzip),
228 'Last-Modified', time2str($mtime),
229 'Content-Length', bytes::length($out) ], [ $out ] ];
232 # not really a stand-alone PSGI app, but maybe it could be...
234 my ($self, $env) = @_;
236 if ($env->{PATH_INFO} eq '/manifest.js.gz') {
237 # grokmirror uses relative paths, so it's domain-dependent
238 my $list = $self->{manifest_cb}->($self, $env, 'manifest');
241 my $list = $self->{www_cb}->($self, $env, 'www');