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;
13 use HTTP::Date qw(time2str);
16 { no warnings 'once'; *try_cat = *PublicInbox::Inbox::try_cat };
19 my ($self, $env, $hide_key) = @_;
21 $self->{pi_config}->each_inbox(sub {
23 push @list, $ibx unless $ibx->{-hide}->{$hide_key};
28 sub list_match_domain ($$$) {
29 my ($self, $env, $hide_key) = @_;
31 my $host = $env->{HTTP_HOST} // $env->{SERVER_NAME};
32 $host =~ s/:[0-9]+\z//;
33 my $re = qr!\A(?:https?:)?//\Q$host\E(?::[0-9]+)?/!i;
34 $self->{pi_config}->each_inbox(sub {
36 if (!$ibx->{-hide}->{$hide_key} && $ibx->{url} =~ $re) {
43 sub list_404 ($$) { [] }
48 'match=domain' => *list_match_domain,
53 my ($pi_config, $k, $default) = @_;
54 my $v = $pi_config->{lc $k} // $default;
57 `$v' is not a valid value for `$k'
58 $k be one of `all', `match=domain', or `404'
65 my ($class, $www) = @_;
66 my $pi_config = $www->{pi_config};
68 pi_config => $pi_config,
69 style => $www->style("\0"),
70 www_cb => set_cb($pi_config, 'publicInbox.wwwListing', 404),
71 manifest_cb => set_cb($pi_config, 'publicInbox.grokManifest',
77 my ($mtime, $ibx, $env) = @_;
78 my $ts = PublicInbox::View::fmt_ts($mtime);
79 my $url = PublicInbox::Hval::prurl($env, $ibx->{url});
84 if (defined(my $info_url = $ibx->{info_url})) {
85 $tmp .= "\n$info_url";
91 my ($env, $list) = @_;
92 my $title = 'public-inbox';
96 $title .= ' - listing';
99 # Schwartzian transform since Inbox->modified is expensive
102 } map { [ $_->modified, $_ ] } @$list;
104 my $tmp = join("\n", map { ibx_entry(@$_, $env) } @$list);
105 my $l = PublicInbox::Linkify->new;
107 $out = '<pre>'.$l->linkify_2(ascii_html($tmp)).'</pre><hr>';
109 $out = "<html><head><title>$title</title></head><body>" . $out;
110 $out .= '<pre>'. PublicInbox::WwwStream::code_footer($env) .
111 '</pre></body></html>';
113 my $h = [ 'Content-Type', 'text/html; charset=UTF-8' ];
114 [ $code, $h, [ $out ] ];
119 for my $mod (qw(JSON::MaybeXS JSON JSON::PP)) {
120 eval "require $mod" or next;
121 # ->ascii encodes non-ASCII to "\uXXXX"
122 return $mod->new->ascii(1);
127 sub fingerprint ($) {
129 my $fh = $git->popen('show-ref') or
130 die "popen($git->{git_dir} show-ref) failed: $!";
132 my $dig = Digest::SHA->new(1);
133 while (read($fh, my $buf, 65536)) {
137 return if $?; # empty, uninitialized git repo
141 sub manifest_add ($$;$$) {
142 my ($manifest, $ibx, $epoch, $default_desc) = @_;
143 my $url_path = "/$ibx->{name}";
144 my $git_dir = $ibx->{inboxdir};
145 if (defined $epoch) {
146 $git_dir .= "/git/$epoch.git";
147 $url_path .= "/git/$epoch.git";
149 return unless -d $git_dir;
150 my $git = PublicInbox::Git->new($git_dir);
151 my $fingerprint = fingerprint($git) or return; # no empty repos
153 chomp(my $owner = $git->qx('config', 'gitweb.owner'));
154 chomp(my $desc = try_cat("$git_dir/description"));
155 $owner = undef if $owner eq '';
156 $desc = 'Unnamed repository' if $desc eq '';
158 # templates/hooks--update.sample and git-multimail in git.git
159 # only match "Unnamed repository", not the full contents of
160 # templates/this--description in git.git
161 if ($desc =~ /\AUnnamed repository/) {
162 $desc = "$default_desc [epoch $epoch]" if defined($epoch);
166 chomp(my $alt = try_cat("$git_dir/objects/info/alternates"));
168 # n.b.: GitPython doesn't seem to handle comments or C-quoted
169 # strings like native git does; and we don't for now, either.
170 my @alt = split(/\n+/, $alt);
172 # grokmirror only supports 1 alternate for "reference",
173 if (scalar(@alt) == 1) {
174 my $objdir = "$git_dir/objects";
175 $reference = File::Spec->rel2abs($alt[0], $objdir);
176 $reference =~ s!/[^/]+/?\z!!; # basename
179 $manifest->{-abs2urlpath}->{$git_dir} = $url_path;
180 my $modified = $git->modified;
181 if ($modified > $manifest->{-mtime}) {
182 $manifest->{-mtime} = $modified;
184 $manifest->{$url_path} = {
186 reference => $reference,
187 description => $desc,
188 modified => $modified,
189 fingerprint => $fingerprint,
195 my ($env, $list) = @_;
196 eval { require IO::Compress::Gzip } or return [ 404, [], [] ];
198 my $manifest = { -abs2urlpath => {}, -mtime => 0 };
199 for my $ibx (@$list) {
200 if (defined(my $max = $ibx->max_git_epoch)) {
201 my $desc = $ibx->description;
202 for my $epoch (0..$max) {
203 manifest_add($manifest, $ibx, $epoch, $desc);
206 manifest_add($manifest, $ibx);
209 my $abs2urlpath = delete $manifest->{-abs2urlpath};
210 my $mtime = delete $manifest->{-mtime};
211 while (my ($url_path, $repo) = each %$manifest) {
212 defined(my $abs = $repo->{reference}) or next;
213 $repo->{reference} = $abs2urlpath->{$abs};
216 IO::Compress::Gzip::gzip(\(($json ||= _json())->encode($manifest)) =>
219 [ 200, [ qw(Content-Type application/gzip),
220 'Last-Modified', time2str($mtime),
221 'Content-Length', bytes::length($out) ], [ $out ] ];
224 # not really a stand-alone PSGI app, but maybe it could be...
226 my ($self, $env) = @_;
228 if ($env->{PATH_INFO} eq '/manifest.js.gz') {
229 # grokmirror uses relative paths, so it's domain-dependent
230 my $list = $self->{manifest_cb}->($self, $env, 'manifest');
233 my $list = $self->{www_cb}->($self, $env, 'www');