1 # Copyright (C) 2016-2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # HTML body stream for which yields getline+close methods for
5 # generic PSGI servers and callbacks for public-inbox-httpd.
7 # See PublicInbox::GzipFilter parent class for more info.
8 package PublicInbox::WwwStream;
11 use parent qw(Exporter PublicInbox::GzipFilter);
12 our @EXPORT_OK = qw(html_oneshot);
13 use PublicInbox::Hval qw(ascii_html prurl ts2str);
16 http://7fh6tueqddpjyxjmgtdiueylzoqt6pt7hec3pukyptlmohoowvhde4yd.onion/public-inbox.git
17 https://public-inbox.org/public-inbox.git) ];
21 my $base_url = $ctx->{ibx}->base_url($ctx->{env});
22 chop $base_url; # no trailing slash for clone
29 $ctx->{base_url} = base_url($ctx);
30 bless $ctx, __PACKAGE__;
33 sub async_eml { # for async_blob_cb
35 $ctx->write($ctx->{cb}->($ctx, $eml));
40 my $ibx = $ctx->{ibx};
41 my $desc = ascii_html($ibx->description);
42 my $title = delete($ctx->{-title_html}) // $desc;
43 my $upfx = $ctx->{-upfx} || '';
44 my $atom = $ctx->{-atom} || $upfx.'new.atom';
45 my $top = "<b>$desc</b>";
46 if (my $t_max = $ctx->{-t_max}) {
47 $t_max = ts2str($t_max);
48 $top = qq(<a\nhref="$upfx?t=$t_max">$top</a>);
49 # we had some kind of query, link to /$INBOX/?t=YYYYMMDDhhmmss
50 } elsif ($ctx->{qp}->{t}) {
51 $top = qq(<a\nhref="./">$top</a>);
52 } elsif (length($upfx)) {
53 $top = qq(<a\nhref="$upfx">$top</a>);
55 my $code = $ibx->{coderepo} ? qq( / <a\nhref=#code>code</a>) : '';
56 # id=mirror must exist for legacy bookmarks
57 my $links = qq(<a\nhref="${upfx}_/text/help/">help</a> / ).
58 qq(<a\nhref="${upfx}_/text/color/">color</a> / ).
60 qq(\nhref="${upfx}_/text/mirror/">mirror</a>$code / ).
61 qq(<a\nhref="$atom">Atom feed</a>);
63 my $q_val = delete($ctx->{-q_value_html}) // '';
64 $q_val = qq(\nvalue="$q_val") if $q_val ne '';
65 # XXX gross, for SearchView.pm
66 my $extra = delete($ctx->{-extra_form_html}) // '';
67 my $action = $upfx eq '' ? './' : $upfx;
68 $top = qq{<form\naction="$action"><pre>$top} .
69 qq{\n<input\nname=q\ntype=text$q_val />} .
71 qq{<input\ntype=submit\nvalue=search />} .
75 $top = '<pre>' . $top . "\n" . $links . '</pre>';
77 "<html><head><title>$title</title>" .
78 qq(<link\nrel=alternate\ntitle="Atom feed"\n).
79 qq(href="$atom"\ntype="application/atom+xml"/>) .
80 $ctx->{www}->style($upfx) .
81 '</head><body>'. $top . (delete($ctx->{-html_tip}) // '');
86 my $cr = $ctx->{ibx}->{coderepo} // return ();
87 my $cfg = $ctx->{www}->{pi_cfg};
88 my $upfx = ($ctx->{-upfx} // ''). '../';
90 for my $cr_name (@$cr) {
92 <a id=code>Code repositories for project(s) associated with this inbox:
94 my $urls = $cfg->get_all("coderepo.$cr_name.cgiturl");
97 # relative or absolute URL?, prefix relative
98 # "foo.git" with appropriate number of "../"
99 my $u = m!\A(?:[a-z\+]+:)?//! ? $_ : $upfx.$_;
100 $u = ascii_html(prurl($ctx->{env}, $u));
101 $ret[0] .= qq(\n\t<a\nhref="$u">$u</a>);
104 $ret[0] .= qq[\n\t$cr_name.git (no URL configured)];
107 @ret; # may be empty, this sub is called as an arg for join()
112 my $upfx = $ctx->{-upfx} || '';
113 my $m = "${upfx}_/text/mirror/";
115 if ($ctx->{ibx}->can('cloneurl')) {
117 This is a public inbox, see <a
118 href="$m">mirroring instructions</a>
119 for how to clone and mirror all data and code used for this inbox
121 my $has_nntp = @{$ctx->{ibx}->nntp_url($ctx)};
122 my $has_imap = @{$ctx->{ibx}->imap_url($ctx)};
123 if ($has_nntp || $has_imap) {
124 substr($x, -1, 1) = ";\n"; # s/\n/;\n
125 if ($has_nntp && $has_imap) {
127 as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).
129 } elsif ($has_nntp) {
131 as well as URLs for NNTP newsgroup(s).
135 as well as URLs for IMAP folder(s).
141 This is an external index of several public inboxes,
142 see <a href="$m">mirroring instructions</a> on how to clone and mirror
143 all data and code used by this external index.
147 '<hr><pre>'.join("\n\n", coderepos($ctx), $x).'</pre></body></html>'
150 # callback for HTTP.pm (and any other PSGI servers)
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->{ibx}->smsg_eml($x) or next;
158 return $ctx->translate($cb->($ctx, $eml));
160 return $ctx->translate($x);
164 $ctx->zflush(_html_end($ctx));
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 $ctx->{base_url} = base_url($ctx);
177 $ctx->zmore($$sref) if $sref;
178 my $bdy = $ctx->zflush(_html_end($ctx));
179 $res_hdr->[3] = length($bdy);
180 [ $code, $res_hdr, [ $bdy ] ]
184 my ($http) = @_; # PublicInbox::HTTP
185 my $ctx = $http->{forward} or return;
187 if (my $smsg = $ctx->{smsg} = $ctx->{cb}->($ctx)) {
188 $ctx->smsg_blob($smsg);
190 $ctx->write(_html_end($ctx));
191 $ctx->close; # GzipFilter->close
198 my ($ctx, $code, $cb) = @_;
199 my $res_hdr = [ 'Content-Type' => 'text/html; charset=UTF-8' ];
201 $ctx->psgi_response($code, $res_hdr);