]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WwwStream.pm
wwwstream: reduce object graph depth
[public-inbox.git] / lib / PublicInbox / WwwStream.pm
1 # Copyright (C) 2016-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 #
4 # HTML body stream for which yields getline+close methods
5 #
6 # public-inbox-httpd favors "getline" response bodies to take a
7 # "pull"-based approach to feeding slow clients (as opposed to a
8 # more common "push" model)
9 package PublicInbox::WwwStream;
10 use strict;
11 use warnings;
12 use base qw(Exporter);
13 our @EXPORT_OK = qw(html_oneshot);
14 use bytes (); # length
15 use PublicInbox::Hval qw(ascii_html prurl);
16 use PublicInbox::GzipFilter qw(gzf_maybe);
17 our $TOR_URL = 'https://www.torproject.org/';
18 our $CODE_URL = 'https://public-inbox.org/public-inbox.git';
19
20 # noop for HTTP.pm (and any other PSGI servers)
21 sub close {}
22
23 sub base_url ($) {
24         my $ctx = shift;
25         my $base_url = $ctx->{-inbox}->base_url($ctx->{env});
26         chop $base_url; # no trailing slash for clone
27         $base_url;
28 }
29
30 sub init {
31         my ($ctx, $cb) = @_;
32         $ctx->{cb} = $cb;
33         $ctx->{base_url} = base_url($ctx);
34         $ctx->{nr} = 0;
35         bless $ctx, __PACKAGE__;
36 }
37
38 sub response {
39         my ($ctx, $code, $cb) = @_;
40         my $h = [ 'Content-Type', 'text/html; charset=UTF-8' ];
41         init($ctx, $cb);
42         $ctx->{gzf} = gzf_maybe($h, $ctx->{env});
43         [ $code, $h, $ctx ]
44 }
45
46 sub _html_top ($) {
47         my ($ctx) = @_;
48         my $ibx = $ctx->{-inbox};
49         my $desc = ascii_html($ibx->description);
50         my $title = delete($ctx->{-title_html}) // $desc;
51         my $upfx = $ctx->{-upfx} || '';
52         my $help = $upfx.'_/text/help';
53         my $color = $upfx.'_/text/color';
54         my $atom = $ctx->{-atom} || $upfx.'new.atom';
55         my $top = "<b>$desc</b>";
56         my $links = "<a\nhref=\"$help\">help</a> / ".
57                         "<a\nhref=\"$color\">color</a> / ".
58                         "<a\nhref=\"$atom\">Atom feed</a>";
59         if ($ibx->search) {
60                 my $q_val = delete($ctx->{-q_value_html}) // '';
61                 $q_val = qq(\nvalue="$q_val") if $q_val ne '';
62                 # XXX gross, for SearchView.pm
63                 my $extra = delete($ctx->{-extra_form_html}) // '';
64                 my $action = $upfx eq '' ? './' : $upfx;
65                 $top = qq{<form\naction="$action"><pre>$top} .
66                           qq{\n<input\nname=q\ntype=text$q_val />} .
67                           $extra .
68                           qq{<input\ntype=submit\nvalue=search />} .
69                           ' ' . $links .
70                           q{</pre></form>}
71         } else {
72                 $top = '<pre>' . $top . "\n" . $links . '</pre>';
73         }
74         "<html><head><title>$title</title>" .
75                 qq(<link\nrel=alternate\ntitle="Atom feed"\n).
76                 qq(href="$atom"\ntype="application/atom+xml"/>) .
77                 $ctx->{www}->style($upfx) .
78                 '</head><body>'. $top . (delete($ctx->{-html_tip}) // '');
79 }
80
81 sub code_footer ($) {
82         my ($env) = @_;
83         my $u = prurl($env, $CODE_URL);
84         qq(AGPL code for this site: git clone <a\nhref="$u">$u</a>)
85 }
86
87 sub _html_end {
88         my ($ctx) = @_;
89         my $urls = 'Archives are clonable:';
90         my $ibx = $ctx->{-inbox};
91         my $desc = ascii_html($ibx->description);
92
93         my @urls;
94         my $http = $ctx->{base_url};
95         my $max = $ibx->max_git_epoch;
96         my $dir = (split(m!/!, $http))[-1];
97         my %seen = ($http => 1);
98         if (defined($max)) { # v2
99                 for my $i (0..$max) {
100                         # old parts my be deleted:
101                         -d "$ibx->{inboxdir}/git/$i.git" or next;
102                         my $url = "$http/$i";
103                         $seen{$url} = 1;
104                         push @urls, "$url $dir/git/$i.git";
105                 }
106         } else { # v1
107                 push @urls, $http;
108         }
109
110         # FIXME: epoch splits can be different in other repositories,
111         # use the "cloneurl" file as-is for now:
112         foreach my $u (@{$ibx->cloneurl}) {
113                 next if $seen{$u}++;
114                 push @urls, $u =~ /\Ahttps?:/ ? qq(<a\nhref="$u">$u</a>) : $u;
115         }
116
117         if (defined($max) || scalar(@urls) > 1) {
118                 $urls .= "\n" .
119                         join("\n", map { "\tgit clone --mirror $_" } @urls);
120         } else {
121                 $urls .= " git clone --mirror $urls[0]";
122         }
123         if (defined $max) {
124                 my $addrs = $ibx->{address};
125                 $addrs = join(' ', @$addrs) if ref($addrs) eq 'ARRAY';
126                 $urls .=  <<EOF
127
128
129         # If you have public-inbox 1.1+ installed, you may
130         # initialize and index your mirror using the following commands:
131         public-inbox-init -V2 $ibx->{name} $dir/ $http \\
132                 $addrs
133         public-inbox-index $dir
134 EOF
135         } else { # v1
136                 $urls .= "\n";
137         }
138
139         my $cfg_link = ($ctx->{-upfx} // '').'_/text/config/raw';
140         $urls .= qq(\nExample <a\nhref="$cfg_link">config snippet</a> for mirrors\n);
141         my @nntp = map { qq(<a\nhref="$_">$_</a>) } @{$ibx->nntp_url};
142         if (@nntp) {
143                 $urls .= "\n";
144                 $urls .= @nntp == 1 ? 'Newsgroup' : 'Newsgroups are';
145                 $urls .= ' available over NNTP:';
146                 $urls .= "\n\t" . join("\n\t", @nntp) . "\n";
147         }
148         if ($urls =~ m!\b[^:]+://\w+\.onion/!) {
149                 $urls .= "\n note: .onion URLs require Tor: ";
150                 $urls .= qq[<a\nhref="$TOR_URL">$TOR_URL</a>];
151         }
152         '<hr><pre>'.join("\n\n",
153                 $desc,
154                 $urls,
155                 code_footer($ctx->{env})
156         ).'</pre></body></html>';
157 }
158
159 # callback for HTTP.pm (and any other PSGI servers)
160 sub getline {
161         my ($ctx) = @_;
162         my $nr = $ctx->{nr}++;
163
164         my $buf = do {
165                 if ($nr == 0) {
166                         _html_top($ctx);
167                 } elsif (my $middle = $ctx->{cb}) {
168                         $middle->($nr, $ctx);
169                 }
170         } // (delete($ctx->{cb}) ? _html_end($ctx) : undef);
171
172         # gzf may be GzipFilter, `undef' or `0'
173         my $gzf = $ctx->{gzf} or return $buf;
174
175         return $gzf->translate($buf) if defined $buf;
176         $ctx->{gzf} = 0; # next call to ->getline returns $buf (== undef)
177         $gzf->translate(undef);
178 }
179
180 sub html_oneshot ($$;$) {
181         my ($ctx, $code, $sref) = @_;
182         $ctx->{base_url} = base_url($ctx);
183         bless $ctx, __PACKAGE__;
184         my @x;
185         my $h = [ 'Content-Type' => 'text/html; charset=UTF-8',
186                 'Content-Length' => undef ];
187         if (my $gzf = gzf_maybe($h, $ctx->{env})) {
188                 $gzf->zmore(_html_top($ctx));
189                 $gzf->zmore($$sref) if $sref;
190                 $x[0] = $gzf->zflush(_html_end($ctx));
191                 $h->[3] = length($x[0]);
192         } else {
193                 @x = (_html_top($ctx), $sref ? $$sref : (), _html_end($ctx));
194                 $h->[3] += bytes::length($_) for @x;
195         }
196         [ $code, $h, \@x ]
197 }
198
199 1;