1 # Copyright (C) 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 $ctx->{-res_hdr} = [ 'Content-Type' => 'text/html; charset=UTF-8' ];
31 $ctx->{gz} = PublicInbox::GzipFilter::gz_or_noop($ctx->{-res_hdr},
33 bless $ctx, __PACKAGE__;
36 sub async_eml { # for async_blob_cb
38 $ctx->write($ctx->{cb}->($ctx, $eml));
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>);
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> / ).
63 qq(\nhref="${upfx}_/text/mirror/">mirror</a>$code / ).
64 qq(<a\nhref="$atom">Atom feed</a>);
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 />} .
74 qq{<input\ntype=submit\nvalue=search />} .
78 $top = '<pre>' . $top . "\n" . $links . '</pre>';
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}) // '');
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");
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>);
107 $ret[0] .= qq[\n\t$cr_name.git (no URL configured)];
110 @ret; # may be empty, this sub is called as an arg for join()
115 my $upfx = $ctx->{-upfx} || '';
116 my $m = "${upfx}_/text/mirror/";
118 if ($ctx->{ibx}->can('cloneurl')) {
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
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) {
130 as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).
132 } elsif ($has_nntp) {
134 as well as URLs for NNTP newsgroup(s).
138 as well as URLs for IMAP folder(s).
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.
150 '<hr><pre>'.join("\n\n", coderepos($ctx), $x).'</pre></body></html>'
153 # callback for HTTP.pm (and any other PSGI servers)
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;
161 return $ctx->translate($cb->($ctx, $eml));
163 return $ctx->translate($x);
167 $ctx->zflush(_html_end($ctx));
170 sub html_done ($;@) {
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 ] ]
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 $ctx->{base_url} // do {
185 $ctx->zmore(html_top($ctx));
186 $ctx->{base_url} = base_url($ctx);
188 my $bdy = $ctx->zflush(@_[2..$#_], _html_end($ctx));
189 $res_hdr->[3] = length($bdy);
190 [ $code, $res_hdr, [ $bdy ] ]
194 my ($http) = @_; # PublicInbox::HTTP
195 my $ctx = $http->{forward} or return;
197 if (my $smsg = $ctx->{smsg} = $ctx->{cb}->($ctx)) {
198 $ctx->smsg_blob($smsg);
200 $ctx->write(_html_end($ctx));
201 $ctx->close; # GzipFilter->close
210 $ctx->psgi_response(200, delete $ctx->{-res_hdr});
215 $ctx->{base_url} = base_url($ctx);
216 my $h = $ctx->{-res_hdr} = ['Content-Type', 'text/html; charset=UTF-8'];
217 $ctx->{gz} = PublicInbox::GzipFilter::gz_or_noop($h, $ctx->{env});
218 bless $ctx, __PACKAGE__;
219 $ctx->zmore(html_top($ctx));