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