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));
43 my $ibx = $ctx->{ibx} // $ctx->{git};
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}) // '');
87 sub inboxes { () } # TODO
91 $ctx->{ibx} // return inboxes($ctx);
92 my $cr = $ctx->{ibx}->{coderepo} // return ();
93 my $upfx = ($ctx->{-upfx} // ''). '../';
94 my $pfx = $ctx->{base_url} //= $ctx->base_url;
95 my $up = $upfx =~ tr!/!/!;
96 $pfx =~ s!/[^/]+\z!/! for (1..$up);
97 $pfx .= '/' if substr($pfx, -1, 1) ne '/';
98 my $buf = '<a id=code>' .
99 'Code repositories for project(s) associated with this '.
100 $ctx->{ibx}->thing_type . "\n";
101 for my $git (@{$ctx->{www}->{pi_cfg}->repo_objs($ctx->{ibx})}) {
102 for ($git->pub_urls($ctx->{env})) {
103 my $u = m!\A(?:[a-z\+]+:)?//!i ? $_ : $pfx.$_;
104 $u = ascii_html(prurl($ctx->{env}, $u));
105 $buf .= qq(\n\t<a\nhref="$u">$u</a>);
113 my $upfx = $ctx->{-upfx} || '';
114 my $m = "${upfx}_/text/mirror/";
116 if ($ctx->{ibx} && $ctx->{ibx}->can('cloneurl')) {
118 This is a public inbox, see <a
119 href="$m">mirroring instructions</a>
120 for how to clone and mirror all data and code used for this inbox
122 my $has_nntp = @{$ctx->{ibx}->nntp_url($ctx)};
123 my $has_imap = @{$ctx->{ibx}->imap_url($ctx)};
124 if ($has_nntp || $has_imap) {
125 substr($x, -1, 1) = ";\n"; # s/\n/;\n
126 if ($has_nntp && $has_imap) {
128 as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).
130 } elsif ($has_nntp) {
132 as well as URLs for NNTP newsgroup(s).
136 as well as URLs for IMAP folder(s).
140 } elsif ($ctx->{ibx}) { # extindex
142 This is an external index of several public inboxes,
143 see <a href="$m">mirroring instructions</a> on how to clone and mirror
144 all data and code used by this external index.
146 } elsif ($ctx->{git}) { # coderepo
147 $x = join('', map { "git clone $_\n" }
148 @{$ctx->{git}->cloneurl($ctx->{env})});
151 '<hr><pre>'.join("\n\n", coderepos($ctx), $x).'</pre></body></html>'
154 # callback for HTTP.pm (and any other PSGI servers)
157 my $cb = $ctx->{cb} or return;
158 while (defined(my $x = $cb->($ctx))) { # x = smsg or scalar non-ref
159 if (ref($x)) { # smsg
160 my $eml = $ctx->{ibx}->smsg_eml($x) or next;
162 return $ctx->translate($cb->($ctx, $eml));
164 return $ctx->translate($x);
168 $ctx->zflush(_html_end($ctx));
171 sub html_done ($;@) {
173 my $bdy = $ctx->zflush(@_[1..$#_], _html_end($ctx));
174 my $res_hdr = delete $ctx->{-res_hdr};
175 push @$res_hdr, 'Content-Length', length($bdy);
176 [ 200, $res_hdr, [ $bdy ] ]
179 sub html_oneshot ($$;@) {
180 my ($ctx, $code) = @_[0, 1];
181 my $res_hdr = [ 'Content-Type' => 'text/html; charset=UTF-8',
182 'Content-Length' => undef ];
183 bless $ctx, __PACKAGE__;
184 $ctx->{gz} = PublicInbox::GzipFilter::gz_or_noop($res_hdr, $ctx->{env});
186 $ctx->{base_url} // do {
187 @top = html_top($ctx);
188 $ctx->{base_url} = base_url($ctx);
190 my $bdy = $ctx->zflush(@top, @_[2..$#_], _html_end($ctx));
191 $res_hdr->[3] = length($bdy);
192 [ $code, $res_hdr, [ $bdy ] ]
196 my ($http) = @_; # PublicInbox::HTTP
197 my $ctx = $http->{forward} or return;
199 if (my $smsg = $ctx->{smsg} = $ctx->{cb}->($ctx)) {
200 $ctx->smsg_blob($smsg);
202 $ctx->write(_html_end($ctx));
203 $ctx->close; # GzipFilter->close
212 $ctx->psgi_response(200, delete $ctx->{-res_hdr});
217 $ctx->{base_url} = base_url($ctx);
218 my $h = $ctx->{-res_hdr} = ['Content-Type', 'text/html; charset=UTF-8'];
219 $ctx->{gz} = PublicInbox::GzipFilter::gz_or_noop($h, $ctx->{env});
220 bless $ctx, __PACKAGE__;
221 print { $ctx->zfh } html_top($ctx);
226 $ctx->{git}->cleanup if $ctx->{git} && $ctx->{git}->{-tmp};