]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WwwStream.pm
www: use PerlIO::scalar (zfh) for buffering
[public-inbox.git] / lib / PublicInbox / WwwStream.pm
1 # Copyright (C) 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 for
5 # generic PSGI servers and callbacks for public-inbox-httpd.
6 #
7 # See PublicInbox::GzipFilter parent class for more info.
8 package PublicInbox::WwwStream;
9 use strict;
10 use v5.10.1;
11 use parent qw(Exporter PublicInbox::GzipFilter);
12 our @EXPORT_OK = qw(html_oneshot);
13 use PublicInbox::Hval qw(ascii_html prurl ts2str);
14
15 our $CODE_URL = [ qw(
16 http://7fh6tueqddpjyxjmgtdiueylzoqt6pt7hec3pukyptlmohoowvhde4yd.onion/public-inbox.git
17 https://public-inbox.org/public-inbox.git) ];
18
19 sub base_url ($) {
20         my $ctx = shift;
21         my $base_url = $ctx->{ibx}->base_url($ctx->{env});
22         chop $base_url; # no trailing slash for clone
23         $base_url;
24 }
25
26 sub init {
27         my ($ctx, $cb) = @_;
28         $ctx->{cb} = $cb;
29         $ctx->{base_url} = base_url($ctx);
30         $ctx->{-res_hdr} = [ 'Content-Type' => 'text/html; charset=UTF-8' ];
31         $ctx->{gz} = PublicInbox::GzipFilter::gz_or_noop($ctx->{-res_hdr},
32                                                         $ctx->{env});
33         bless $ctx, __PACKAGE__;
34 }
35
36 sub async_eml { # for async_blob_cb
37         my ($ctx, $eml) = @_;
38         $ctx->write($ctx->{cb}->($ctx, $eml));
39 }
40
41 sub html_top ($) {
42         my ($ctx) = @_;
43         my $ibx = $ctx->{ibx};
44         my $desc = ascii_html($ibx->description);
45         my $title = delete($ctx->{-title_html}) // $desc;
46         my $upfx = $ctx->{-upfx} || '';
47         my $atom = $ctx->{-atom} || $upfx.'new.atom';
48         my $top = "<b>$desc</b>";
49         if (my $t_max = $ctx->{-t_max}) {
50                 $t_max = ts2str($t_max);
51                 $top = qq(<a\nhref="$upfx?t=$t_max">$top</a>);
52         # we had some kind of query, link to /$INBOX/?t=YYYYMMDDhhmmss
53         } elsif ($ctx->{qp}->{t}) {
54                 $top = qq(<a\nhref="./">$top</a>);
55         } elsif (length($upfx)) {
56                 $top = qq(<a\nhref="$upfx">$top</a>);
57         }
58         my $code = $ibx->{coderepo} ? qq( / <a\nhref=#code>code</a>) : '';
59         # id=mirror must exist for legacy bookmarks
60         my $links = qq(<a\nhref="${upfx}_/text/help/">help</a> / ).
61                         qq(<a\nhref="${upfx}_/text/color/">color</a> / ).
62                         qq(<a\nid=mirror) .
63                         qq(\nhref="${upfx}_/text/mirror/">mirror</a>$code / ).
64                         qq(<a\nhref="$atom">Atom feed</a>);
65         if ($ibx->isrch) {
66                 my $q_val = delete($ctx->{-q_value_html}) // '';
67                 $q_val = qq(\nvalue="$q_val") if $q_val ne '';
68                 # XXX gross, for SearchView.pm
69                 my $extra = delete($ctx->{-extra_form_html}) // '';
70                 my $action = $upfx eq '' ? './' : $upfx;
71                 $top = qq{<form\naction="$action"><pre>$top} .
72                           qq{\n<input\nname=q\ntype=text$q_val />} .
73                           $extra .
74                           qq{<input\ntype=submit\nvalue=search />} .
75                           ' ' . $links .
76                           q{</pre></form>}
77         } else {
78                 $top = '<pre>' . $top . "\n" . $links . '</pre>';
79         }
80         "<html><head><title>$title</title>" .
81                 qq(<link\nrel=alternate\ntitle="Atom feed"\n).
82                 qq(href="$atom"\ntype="application/atom+xml"/>) .
83                 $ctx->{www}->style($upfx) .
84                 '</head><body>'. $top . (delete($ctx->{-html_tip}) // '');
85 }
86
87 sub coderepos ($) {
88         my ($ctx) = @_;
89         my $cr = $ctx->{ibx}->{coderepo} // return ();
90         my $cfg = $ctx->{www}->{pi_cfg};
91         my $upfx = ($ctx->{-upfx} // ''). '../';
92         my $pfx = $ctx->{base_url} //= $ctx->base_url;
93         my $up = $upfx =~ tr!/!/!;
94         $pfx =~ s!/[^/]+\z!/! for (1..$up);
95         my @ret = ('<a id=code>' .
96                 'Code repositories for project(s) associated with this '.
97                 $ctx->{ibx}->thing_type . "\n");
98         for my $cr_name (@$cr) {
99                 my $urls = $cfg->get_all("coderepo.$cr_name.cgiturl");
100                 if ($urls) {
101                         for (@$urls) {
102                                 my $u = m!\A(?:[a-z\+]+:)?//! ? $_ : $pfx.$_;
103                                 $u = ascii_html(prurl($ctx->{env}, $u));
104                                 $ret[0] .= qq(\n\t<a\nhref="$u">$u</a>);
105                         }
106                 } else {
107                         $ret[0] .= qq[\n\t$cr_name.git (no URL configured)];
108                 }
109         }
110         @ret; # may be empty, this sub is called as an arg for join()
111 }
112
113 sub _html_end {
114         my ($ctx) = @_;
115         my $upfx = $ctx->{-upfx} || '';
116         my $m = "${upfx}_/text/mirror/";
117         my $x;
118         if ($ctx->{ibx}->can('cloneurl')) {
119                 $x = <<EOF;
120 This is a public inbox, see <a
121 href="$m">mirroring instructions</a>
122 for how to clone and mirror all data and code used for this inbox
123 EOF
124                 my $has_nntp = @{$ctx->{ibx}->nntp_url($ctx)};
125                 my $has_imap = @{$ctx->{ibx}->imap_url($ctx)};
126                 if ($has_nntp || $has_imap) {
127                         substr($x, -1, 1) = ";\n"; # s/\n/;\n
128                         if ($has_nntp && $has_imap) {
129                                 $x .= <<EOM;
130 as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).
131 EOM
132                         } elsif ($has_nntp) {
133                                 $x .= <<EOM;
134 as well as URLs for NNTP newsgroup(s).
135 EOM
136                         } else {
137                                 $x .= <<EOM;
138 as well as URLs for IMAP folder(s).
139 EOM
140                         }
141                 }
142         } else {
143                 $x = <<EOF;
144 This is an external index of several public inboxes,
145 see <a href="$m">mirroring instructions</a> on how to clone and mirror
146 all data and code used by this external index.
147 EOF
148         }
149         chomp $x;
150         '<hr><pre>'.join("\n\n", coderepos($ctx), $x).'</pre></body></html>'
151 }
152
153 # callback for HTTP.pm (and any other PSGI servers)
154 sub getline {
155         my ($ctx) = @_;
156         my $cb = $ctx->{cb} or return;
157         while (defined(my $x = $cb->($ctx))) { # x = smsg or scalar non-ref
158                 if (ref($x)) { # smsg
159                         my $eml = $ctx->{ibx}->smsg_eml($x) or next;
160                         $ctx->{smsg} = $x;
161                         return $ctx->translate($cb->($ctx, $eml));
162                 } else { # scalar
163                         return $ctx->translate($x);
164                 }
165         }
166         delete $ctx->{cb};
167         $ctx->zflush(_html_end($ctx));
168 }
169
170 sub html_done ($;@) {
171         my $ctx = $_[0];
172         my $bdy = $ctx->zflush(@_[1..$#_], _html_end($ctx));
173         my $res_hdr = delete $ctx->{-res_hdr};
174         push @$res_hdr, 'Content-Length', length($bdy);
175         [ 200, $res_hdr, [ $bdy ] ]
176 }
177
178 sub html_oneshot ($$;@) {
179         my ($ctx, $code) = @_[0, 1];
180         my $res_hdr = [ 'Content-Type' => 'text/html; charset=UTF-8',
181                 'Content-Length' => undef ];
182         bless $ctx, __PACKAGE__;
183         $ctx->{gz} = PublicInbox::GzipFilter::gz_or_noop($res_hdr, $ctx->{env});
184         my @top;
185         $ctx->{base_url} // do {
186                 @top = html_top($ctx);
187                 $ctx->{base_url} = base_url($ctx);
188         };
189         my $bdy = $ctx->zflush(@top, @_[2..$#_], _html_end($ctx));
190         $res_hdr->[3] = length($bdy);
191         [ $code, $res_hdr, [ $bdy ] ]
192 }
193
194 sub async_next ($) {
195         my ($http) = @_; # PublicInbox::HTTP
196         my $ctx = $http->{forward} or return;
197         eval {
198                 if (my $smsg = $ctx->{smsg} = $ctx->{cb}->($ctx)) {
199                         $ctx->smsg_blob($smsg);
200                 } else {
201                         $ctx->write(_html_end($ctx));
202                         $ctx->close; # GzipFilter->close
203                 }
204         };
205         warn "E: $@" if $@;
206 }
207
208 sub aresponse {
209         my ($ctx, $cb) = @_;
210         init($ctx, $cb);
211         $ctx->psgi_response(200, delete $ctx->{-res_hdr});
212 }
213
214 sub html_init {
215         my ($ctx) = @_;
216         $ctx->{base_url} = base_url($ctx);
217         my $h = $ctx->{-res_hdr} = ['Content-Type', 'text/html; charset=UTF-8'];
218         $ctx->{gz} = PublicInbox::GzipFilter::gz_or_noop($h, $ctx->{env});
219         bless $ctx, __PACKAGE__;
220         print { $ctx->zfh } html_top($ctx);
221 }
222
223 1;