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} // $ctx->{git})->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));
41 sub html_repo_top ($) {
43 my $git = $ctx->{git};
44 my $desc = ascii_html($git->description);
45 my $title = delete($ctx->{-title_html}) // $desc;
46 my $upfx = $ctx->{-upfx} // '';
47 my $atom = $ctx->{-atom} // (substr($upfx, -1) eq '/' ?
48 "${upfx}atom/" : "$upfx/atom/");
49 my $top = ascii_html($git->{nick});
50 $top = qq(<a\nhref="$upfx">$top</a>) if length($upfx);
52 <a href='$upfx#readme'>about</a> / <a
53 href='$upfx#heads'>heads</a> / <a
54 href='$upfx#tags'>tags</a>
57 my @url = PublicInbox::ViewVCS::ibx_url_for($ctx);
59 $ctx->{-has_srch} = 1;
60 my $base_url = base_url($ctx);
61 my ($pfx, $sfx) = ($base_url =~ m!\A(https?://[^/]+/)(.*)\z!i);
62 my $iupfx = '../' x (($sfx =~ tr!/!/!) + 1);
63 $pfx = ascii_html($pfx);
64 $pfx = qr/\A\Q$pfx\E/i;
67 my ($s, $u, $same_host);
68 my $q_val = delete($ctx->{-q_value_html}) // '';
69 $q_val = qq(\nvalue="$q_val") if $q_val ne '';
71 $u = $s = ascii_html($_);
72 substr($u, 0, 0, $iupfx) if $u !~ m!://!;
75 $top .= qq{<form\naction="$u"><pre>$tmp} .
76 qq{<input\nname=q type=text$q_val />} .
77 qq{<input type=submit\n} .
78 qq{value="search mail in `$s'"/>} .
83 $top = "<pre>$top</pre>";
85 "<html><head><title>$title</title>" .
86 qq(<link\nrel=alternate\ntitle="Atom feed"\n).
87 qq(href="$atom"\ntype="application/atom+xml"/>) .
88 $ctx->{www}->style($upfx) .
94 my $ibx = $ctx->{ibx} // return html_repo_top($ctx);
95 my $desc = ascii_html($ibx->description);
96 my $title = delete($ctx->{-title_html}) // $desc;
97 my $upfx = $ctx->{-upfx} || '';
98 my $atom = $ctx->{-atom} || $upfx.'new.atom';
99 my $top = "<b>$desc</b>";
100 if (my $t_max = $ctx->{-t_max}) {
101 $t_max = ts2str($t_max);
102 $top = qq(<a\nhref="$upfx?t=$t_max">$top</a>);
103 # we had some kind of query, link to /$INBOX/?t=YYYYMMDDhhmmss
104 } elsif ($ctx->{qp}->{t}) {
105 $top = qq(<a\nhref="./">$top</a>);
106 } elsif (length($upfx)) {
107 $top = qq(<a\nhref="$upfx">$top</a>);
109 my $code = $ibx->{coderepo} ? qq( / <a\nhref=#code>code</a>) : '';
110 # id=mirror must exist for legacy bookmarks
111 my $links = qq(<a\nhref="${upfx}_/text/help/">help</a> / ).
112 qq(<a\nhref="${upfx}_/text/color/">color</a> / ).
114 qq(\nhref="${upfx}_/text/mirror/">mirror</a>$code / ).
115 qq(<a\nhref="$atom">Atom feed</a>);
117 my $q_val = delete($ctx->{-q_value_html}) // '';
118 $q_val = qq(\nvalue="$q_val") if $q_val ne '';
119 # XXX gross, for SearchView.pm
120 my $extra = delete($ctx->{-extra_form_html}) // '';
121 my $action = $upfx eq '' ? './' : $upfx;
122 $top = qq{<form\naction="$action"><pre>$top} .
123 qq{\n<input\nname=q\ntype=text$q_val />} .
125 qq{<input\ntype=submit\nvalue=search />} .
129 $top = '<pre>' . $top . "\n" . $links . '</pre>';
131 "<html><head><title>$title</title>" .
132 qq(<link\nrel=alternate\ntitle="Atom feed"\n).
133 qq(href="$atom"\ntype="application/atom+xml"/>) .
134 $ctx->{www}->style($upfx) .
135 '</head><body>'. $top . (delete($ctx->{-html_tip}) // '');
138 sub inboxes { () } # TODO
142 $ctx->{ibx} // return inboxes($ctx);
143 my $cr = $ctx->{ibx}->{coderepo} // return ();
144 my $upfx = ($ctx->{-upfx} // ''). '../';
145 my $pfx = $ctx->{base_url} //= $ctx->base_url;
146 my $up = $upfx =~ tr!/!/!;
147 $pfx =~ s!/[^/]+\z!/! for (1..$up);
148 $pfx .= '/' if substr($pfx, -1, 1) ne '/';
149 my $buf = '<a id=code>' .
150 'Code repositories for project(s) associated with this '.
151 $ctx->{ibx}->thing_type . "\n";
152 for my $git (@{$ctx->{www}->{pi_cfg}->repo_objs($ctx->{ibx})}) {
153 for ($git->pub_urls($ctx->{env})) {
154 my $u = m!\A(?:[a-z\+]+:)?//!i ? $_ : $pfx.$_;
155 $u = ascii_html(prurl($ctx->{env}, $u));
156 $buf .= qq(\n\t<a\nhref="$u">$u</a>);
164 my $upfx = $ctx->{-upfx} || '';
165 my $m = "${upfx}_/text/mirror/";
167 if ($ctx->{ibx} && $ctx->{ibx}->can('cloneurl')) {
169 This is a public inbox, see <a
170 href="$m">mirroring instructions</a>
171 for how to clone and mirror all data and code used for this inbox
173 my $has_nntp = @{$ctx->{ibx}->nntp_url($ctx)};
174 my $has_imap = @{$ctx->{ibx}->imap_url($ctx)};
175 if ($has_nntp || $has_imap) {
176 substr($x, -1, 1) = ";\n"; # s/\n/;\n
177 if ($has_nntp && $has_imap) {
179 as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).
181 } elsif ($has_nntp) {
183 as well as URLs for NNTP newsgroup(s).
187 as well as URLs for IMAP folder(s).
191 } elsif ($ctx->{ibx}) { # extindex
193 This is an external index of several public inboxes,
194 see <a href="$m">mirroring instructions</a> on how to clone and mirror
195 all data and code used by this external index.
197 } elsif ($ctx->{git}) { # coderepo
198 $x = join('', map { "git clone $_\n" }
199 @{$ctx->{git}->cloneurl($ctx->{env})});
202 '<hr><pre>'.join("\n\n", coderepos($ctx), $x).'</pre></body></html>'
205 # callback for HTTP.pm (and any other PSGI servers)
208 my $cb = $ctx->{cb} or return;
209 while (defined(my $x = $cb->($ctx))) { # x = smsg or scalar non-ref
210 if (ref($x)) { # smsg
211 my $eml = $ctx->{ibx}->smsg_eml($x) or next;
213 return $ctx->translate($cb->($ctx, $eml));
215 return $ctx->translate($x);
219 $ctx->zflush(_html_end($ctx));
222 sub html_done ($;@) {
224 my $bdy = $ctx->zflush(@_[1..$#_], _html_end($ctx));
225 my $res_hdr = delete $ctx->{-res_hdr};
226 push @$res_hdr, 'Content-Length', length($bdy);
227 [ 200, $res_hdr, [ $bdy ] ]
230 sub html_oneshot ($$;@) {
231 my ($ctx, $code) = @_[0, 1];
232 my $res_hdr = [ 'Content-Type' => 'text/html; charset=UTF-8',
233 'Content-Length' => undef ];
234 bless $ctx, __PACKAGE__;
235 $ctx->{gz} = PublicInbox::GzipFilter::gz_or_noop($res_hdr, $ctx->{env});
237 $ctx->{base_url} // do {
238 @top = html_top($ctx);
239 $ctx->{base_url} = base_url($ctx);
241 my $bdy = $ctx->zflush(@top, @_[2..$#_], _html_end($ctx));
242 $res_hdr->[3] = length($bdy);
243 [ $code, $res_hdr, [ $bdy ] ]
247 my ($http) = @_; # PublicInbox::HTTP
248 my $ctx = $http->{forward} or return;
250 if (my $smsg = $ctx->{smsg} = $ctx->{cb}->($ctx)) {
251 $ctx->smsg_blob($smsg);
253 $ctx->write(_html_end($ctx));
254 $ctx->close; # GzipFilter->close
263 $ctx->psgi_response(200, delete $ctx->{-res_hdr});
268 $ctx->{base_url} = base_url($ctx);
269 my $h = $ctx->{-res_hdr} = ['Content-Type', 'text/html; charset=UTF-8'];
270 $ctx->{gz} = PublicInbox::GzipFilter::gz_or_noop($h, $ctx->{env});
271 bless $ctx, __PACKAGE__;
272 print { $ctx->zfh } html_top($ctx);
277 $ctx->{git}->cleanup if $ctx->{git} && $ctx->{git}->{-tmp};