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="$atom">Atom feed</a>);
113 my $q_val = delete($ctx->{-q_value_html}) // '';
114 $q_val = qq(\nvalue="$q_val") if $q_val ne '';
115 # XXX gross, for SearchView.pm
116 my $extra = delete($ctx->{-extra_form_html}) // '';
117 my $action = $upfx eq '' ? './' : $upfx;
118 $top = qq{<form\naction="$action"><pre>$top} .
119 qq{\n<input\nname=q\ntype=text$q_val />} .
121 qq{<input\ntype=submit\nvalue=search />} .
125 $top = '<pre>' . $top . "\n" . $links . '</pre>';
127 "<html><head><title>$title</title>" .
128 qq(<link\nrel=alternate\ntitle="Atom feed"\n).
129 qq(href="$atom"\ntype="application/atom+xml"/>) .
130 $ctx->{www}->style($upfx) .
131 '</head><body>'. $top . (delete($ctx->{-html_tip}) // '');
134 sub inboxes { () } # TODO
138 $ctx->{ibx} // return inboxes($ctx);
139 my $cr = $ctx->{ibx}->{coderepo} // return ();
140 my $upfx = ($ctx->{-upfx} // ''). '../';
141 my $pfx = $ctx->{base_url} //= $ctx->base_url;
142 my $up = $upfx =~ tr!/!/!;
143 $pfx =~ s!/[^/]+\z!/! for (1..$up);
144 $pfx .= '/' if substr($pfx, -1, 1) ne '/';
145 my $buf = '<a id=code>' .
146 'Code repositories for project(s) associated with this '.
147 $ctx->{ibx}->thing_type . "\n";
148 for my $git (@{$ctx->{www}->{pi_cfg}->repo_objs($ctx->{ibx})}) {
149 for ($git->pub_urls($ctx->{env})) {
150 my $u = m!\A(?:[a-z\+]+:)?//!i ? $_ : $pfx.$_;
151 $u = ascii_html(prurl($ctx->{env}, $u));
152 $buf .= qq(\n\t<a\nhref="$u">$u</a>);
159 return '</body></html>';
161 my $upfx = $ctx->{-upfx} || '';
162 my $m = "${upfx}_/text/mirror/";
164 if ($ctx->{ibx} && $ctx->{ibx}->can('cloneurl')) {
166 This is a public inbox, see <a
167 href="$m">mirroring instructions</a>
168 for how to clone and mirror all data and code used for this inbox
170 my $has_nntp = @{$ctx->{ibx}->nntp_url($ctx)};
171 my $has_imap = @{$ctx->{ibx}->imap_url($ctx)};
172 if ($has_nntp || $has_imap) {
173 substr($x, -1, 1) = ";\n"; # s/\n/;\n
174 if ($has_nntp && $has_imap) {
176 as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).
178 } elsif ($has_nntp) {
180 as well as URLs for NNTP newsgroup(s).
184 as well as URLs for IMAP folder(s).
188 } elsif ($ctx->{ibx}) { # extindex
190 This is an external index of several public inboxes,
191 see <a href="$m">mirroring instructions</a> on how to clone and mirror
192 all data and code used by this external index.
194 } elsif ($ctx->{git}) { # coderepo
195 $x = join('', map { "git clone $_\n" }
196 @{$ctx->{git}->cloneurl($ctx->{env})});
199 '<hr><pre>'.join("\n\n", coderepos($ctx), $x).'</pre></body></html>'
202 # callback for HTTP.pm (and any other PSGI servers)
205 my $cb = $ctx->{cb} or return;
206 while (defined(my $x = $cb->($ctx))) { # x = smsg or scalar non-ref
207 if (ref($x)) { # smsg
208 my $eml = $ctx->{ibx}->smsg_eml($x) or next;
210 return $ctx->translate($cb->($ctx, $eml));
212 return $ctx->translate($x);
216 $ctx->zflush(_html_end($ctx));
219 sub html_done ($;@) {
221 my $bdy = $ctx->zflush(@_[1..$#_], _html_end($ctx));
222 my $res_hdr = delete $ctx->{-res_hdr};
223 push @$res_hdr, 'Content-Length', length($bdy);
224 [ 200, $res_hdr, [ $bdy ] ]
227 sub html_oneshot ($$;@) {
228 my ($ctx, $code) = @_[0, 1];
229 my $res_hdr = [ 'Content-Type' => 'text/html; charset=UTF-8',
230 'Content-Length' => undef ];
231 bless $ctx, __PACKAGE__;
232 $ctx->{gz} = PublicInbox::GzipFilter::gz_or_noop($res_hdr, $ctx->{env});
234 $ctx->{base_url} // do {
235 @top = html_top($ctx);
236 $ctx->{base_url} = base_url($ctx);
238 my $bdy = $ctx->zflush(@top, @_[2..$#_], _html_end($ctx));
239 $res_hdr->[3] = length($bdy);
240 [ $code, $res_hdr, [ $bdy ] ]
244 my ($http) = @_; # PublicInbox::HTTP
245 my $ctx = $http->{forward} or return;
247 if (my $smsg = $ctx->{smsg} = $ctx->{cb}->($ctx)) {
248 $ctx->smsg_blob($smsg);
250 $ctx->write(_html_end($ctx));
251 $ctx->close; # GzipFilter->close
260 $ctx->psgi_response(200, delete $ctx->{-res_hdr});
265 $ctx->{base_url} = base_url($ctx);
266 my $h = $ctx->{-res_hdr} = ['Content-Type', 'text/html; charset=UTF-8'];
267 $ctx->{gz} = PublicInbox::GzipFilter::gz_or_noop($h, $ctx->{env});
268 bless $ctx, __PACKAGE__;
269 print { $ctx->zfh } html_top($ctx);
274 $ctx->{git}->cleanup if $ctx->{git} && $ctx->{git}->{-tmp};