]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WwwListing.pm
www: manifest.js.gz generation no longer hogs event loop
[public-inbox.git] / lib / PublicInbox / WwwListing.pm
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>
3
4 # Provide an HTTP-accessible listing of inboxes.
5 # Used by PublicInbox::WWW
6 package PublicInbox::WwwListing;
7 use strict;
8 use PublicInbox::Hval qw(ascii_html prurl fmt_ts);
9 use PublicInbox::Linkify;
10 use PublicInbox::GzipFilter qw(gzf_maybe);
11 use PublicInbox::ManifestJsGz;
12 use bytes (); # bytes::length
13
14 sub list_all_i {
15         my ($ibx, $arg) = @_;
16         my ($list, $hide_key) = @$arg;
17         push @$list, $ibx unless $ibx->{-hide}->{$hide_key};
18 }
19
20 sub list_all ($$$) {
21         my ($self, $env, $hide_key) = @_;
22         my $list = [];
23         $self->{pi_config}->each_inbox(\&list_all_i, [ $list, $hide_key ]);
24         $list;
25 }
26
27 sub list_match_domain_i {
28         my ($ibx, $arg) = @_;
29         my ($list, $hide_key, $re) = @$arg;
30         if (!$ibx->{-hide}->{$hide_key} && grep(/$re/, @{$ibx->{url}})) {
31                 push @$list, $ibx;
32         }
33 }
34
35 sub list_match_domain ($$$) {
36         my ($self, $env, $hide_key) = @_;
37         my $list = [];
38         my $host = $env->{HTTP_HOST} // $env->{SERVER_NAME};
39         $host =~ s/:[0-9]+\z//;
40         my $arg = [ $list, $hide_key,
41                 qr!\A(?:https?:)?//\Q$host\E(?::[0-9]+)?/!i ];
42         $self->{pi_config}->each_inbox(\&list_match_domain_i, $arg);
43         $list;
44 }
45
46 sub list_404 ($$) { [] }
47
48 # TODO: +cgit
49 my %VALID = (
50         all => \&list_all,
51         'match=domain' => \&list_match_domain,
52         404 => \&list_404,
53 );
54
55 sub set_cb ($$$) {
56         my ($pi_config, $k, $default) = @_;
57         my $v = $pi_config->{lc $k} // $default;
58         $VALID{$v} || do {
59                 warn <<"";
60 `$v' is not a valid value for `$k'
61 $k be one of `all', `match=domain', or `404'
62
63                 $VALID{$default};
64         };
65 }
66
67 sub new {
68         my ($class, $www) = @_;
69         my $pi_config = $www->{pi_config};
70         bless {
71                 pi_config => $pi_config,
72                 style => $www->style("\0"),
73                 www_cb => set_cb($pi_config, 'publicInbox.wwwListing', 404),
74                 manifest_cb => set_cb($pi_config, 'publicInbox.grokManifest',
75                                         'match=domain'),
76         }, $class;
77 }
78
79 sub ibx_entry {
80         my ($mtime, $ibx, $env) = @_;
81         my $ts = fmt_ts($mtime);
82         my $url = prurl($env, $ibx->{url});
83         my $tmp = <<"";
84 * $ts - $url
85   ${\$ibx->description}
86
87         if (defined(my $info_url = $ibx->{infourl})) {
88                 $tmp .= '  ' . prurl($env, $info_url) . "\n";
89         }
90         $tmp;
91 }
92
93 sub html ($$) {
94         my ($env, $list) = @_;
95         my $h = [ 'Content-Type', 'text/html; charset=UTF-8',
96                         'Content-Length', undef ];
97         my $gzf = gzf_maybe($h, $env);
98         $gzf->zmore('<html><head><title>' .
99                                 'public-inbox listing</title>' .
100                                 '</head><body><pre>');
101         my $code = 404;
102         if (@$list) {
103                 $code = 200;
104                 # Schwartzian transform since Inbox->modified is expensive
105                 @$list = sort {
106                         $b->[0] <=> $a->[0]
107                 } map { [ $_->modified, $_ ] } @$list;
108
109                 my $tmp = join("\n", map { ibx_entry(@$_, $env) } @$list);
110                 my $l = PublicInbox::Linkify->new;
111                 $gzf->zmore($l->to_html($tmp));
112         } else {
113                 $gzf->zmore('no inboxes, yet');
114         }
115         my $out = $gzf->zflush('</pre><hr><pre>'.
116                                 PublicInbox::WwwStream::code_footer($env) .
117                                 '</pre></body></html>');
118         $h->[3] = bytes::length($out);
119         [ $code, $h, [ $out ] ];
120 }
121
122 # not really a stand-alone PSGI app, but maybe it could be...
123 sub call {
124         my ($self, $env) = @_;
125
126         if ($env->{PATH_INFO} eq '/manifest.js.gz') {
127                 # grokmirror uses relative paths, so it's domain-dependent
128                 my $list = $self->{manifest_cb}->($self, $env, 'manifest');
129                 PublicInbox::ManifestJsGz::response($env, $list);
130         } else { # /
131                 my $list = $self->{www_cb}->($self, $env, 'www');
132                 html($env, $list);
133         }
134 }
135
136 1;