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