]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WwwListing.pm
wwwlisting: fix spelling and clarify sub location
[public-inbox.git] / lib / PublicInbox / WwwListing.pm
1 # Copyright (C) 2019 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # Provide an HTTP-accessible listing of inboxes.
5 # Used by PublicInbox::WWW
6 package PublicInbox::WwwListing;
7 use strict;
8 use warnings;
9 use PublicInbox::Hval qw(ascii_html);
10 use PublicInbox::Linkify;
11 use PublicInbox::View;
12 use bytes ();
13 use HTTP::Date qw(time2str);
14 require Digest::SHA;
15 require File::Spec;
16 { no warnings 'once'; *try_cat = *PublicInbox::Inbox::try_cat };
17
18 sub list_all ($$$) {
19         my ($self, $env, $hide_key) = @_;
20         my @list;
21         $self->{pi_config}->each_inbox(sub {
22                 my ($ibx) = @_;
23                 push @list, $ibx unless $ibx->{-hide}->{$hide_key};
24         });
25         \@list;
26 }
27
28 sub list_match_domain ($$$) {
29         my ($self, $env, $hide_key) = @_;
30         my @list;
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 {
35                 my ($ibx) = @_;
36                 if (!$ibx->{-hide}->{$hide_key} && $ibx->{url} =~ $re) {
37                         push @list, $ibx;
38                 }
39         });
40         \@list;
41 }
42
43 sub list_404 ($$) { [] }
44
45 # TODO: +cgit
46 my %VALID = (
47         all => *list_all,
48         'match=domain' => *list_match_domain,
49         404 => *list_404,
50 );
51
52 sub set_cb ($$$) {
53         my ($pi_config, $k, $default) = @_;
54         my $v = $pi_config->{lc $k} // $default;
55         $VALID{$v} || do {
56                 warn <<"";
57 `$v' is not a valid value for `$k'
58 $k be one of `all', `match=domain', or `404'
59
60                 $VALID{$default};
61         };
62 }
63
64 sub new {
65         my ($class, $www) = @_;
66         my $pi_config = $www->{pi_config};
67         bless {
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',
72                                         'match=domain'),
73         }, $class;
74 }
75
76 sub ibx_entry {
77         my ($mtime, $ibx, $env) = @_;
78         my $ts = PublicInbox::View::fmt_ts($mtime);
79         my $url = PublicInbox::Hval::prurl($env, $ibx->{url});
80         my $tmp = <<"";
81 * $ts - $url
82   ${\$ibx->description}
83
84         if (defined(my $info_url = $ibx->{info_url})) {
85                 $tmp .= "\n$info_url";
86         }
87         $tmp;
88 }
89
90 sub html ($$) {
91         my ($env, $list) = @_;
92         my $title = 'public-inbox';
93         my $out = '';
94         my $code = 404;
95         if (@$list) {
96                 $title .= ' - listing';
97                 $code = 200;
98
99                 # Schwartzian transform since Inbox->modified is expensive
100                 @$list = sort {
101                         $b->[0] <=> $a->[0]
102                 } map { [ $_->modified, $_ ] } @$list;
103
104                 my $tmp = join("\n", map { ibx_entry(@$_, $env) } @$list);
105                 my $l = PublicInbox::Linkify->new;
106                 $l->linkify_1($tmp);
107                 $out = '<pre>'.$l->linkify_2(ascii_html($tmp)).'</pre><hr>';
108         }
109         $out = "<html><head><title>$title</title></head><body>" . $out;
110         $out .= '<pre>'. PublicInbox::WwwStream::code_footer($env) .
111                 '</pre></body></html>';
112
113         my $h = [ 'Content-Type', 'text/html; charset=UTF-8' ];
114         [ $code, $h, [ $out ] ];
115 }
116
117 my $json;
118 sub _json () {
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);
123         }
124         die;
125 }
126
127 sub fingerprint ($) {
128         my ($git) = @_;
129         my $fh = $git->popen('show-ref') or
130                 die "popen($git->{git_dir} show-ref) failed: $!";
131
132         my $dig = Digest::SHA->new(1);
133         while (read($fh, my $buf, 65536)) {
134                 $dig->add($buf);
135         }
136         close $fh;
137         return if $?; # empty, uninitialized git repo
138         $dig->hexdigest;
139 }
140
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";
148         }
149         return unless -d $git_dir;
150         my $git = PublicInbox::Git->new($git_dir);
151         my $fingerprint = fingerprint($git) or return; # no empty repos
152
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 '';
157
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);
163         }
164
165         my $reference;
166         chomp(my $alt = try_cat("$git_dir/objects/info/alternates"));
167         if ($alt) {
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);
171
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
177                 }
178         }
179         $manifest->{-abs2urlpath}->{$git_dir} = $url_path;
180         my $modified = $git->modified;
181         if ($modified > $manifest->{-mtime}) {
182                 $manifest->{-mtime} = $modified;
183         }
184         $manifest->{$url_path} = {
185                 owner => $owner,
186                 reference => $reference,
187                 description => $desc,
188                 modified => $modified,
189                 fingerprint => $fingerprint,
190         };
191 }
192
193 # manifest.js.gz
194 sub js ($$) {
195         my ($env, $list) = @_;
196         eval { require IO::Compress::Gzip } or return [ 404, [], [] ];
197
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);
204                         }
205                 } else {
206                         manifest_add($manifest, $ibx);
207                 }
208         }
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};
214         }
215         my $out;
216         IO::Compress::Gzip::gzip(\(($json ||= _json())->encode($manifest)) =>
217                                  \$out);
218         $manifest = undef;
219         [ 200, [ qw(Content-Type application/gzip),
220                  'Last-Modified', time2str($mtime),
221                  'Content-Length', bytes::length($out) ], [ $out ] ];
222 }
223
224 # not really a stand-alone PSGI app, but maybe it could be...
225 sub call {
226         my ($self, $env) = @_;
227
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');
231                 js($env, $list);
232         } else { # /
233                 my $list = $self->{www_cb}->($self, $env, 'www');
234                 html($env, $list);
235         }
236 }
237
238 1;