1 # Copyright (C) 2016-2020 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;
10 use parent qw(Exporter PublicInbox::GzipFilter);
11 our @EXPORT_OK = qw(html_oneshot);
12 use bytes (); # length
13 use PublicInbox::Hval qw(ascii_html prurl ts2str);
14 our $TOR_URL = 'https://www.torproject.org/';
15 our $CODE_URL = [ qw(http://ou63pmih66umazou.onion/public-inbox.git
16 https://public-inbox.org/public-inbox.git) ];
20 my $base_url = $ctx->{ibx}->base_url($ctx->{env});
21 chop $base_url; # no trailing slash for clone
28 $ctx->{base_url} = base_url($ctx);
29 bless $ctx, __PACKAGE__;
32 sub async_eml { # for async_blob_cb
34 $ctx->{http_out}->write($ctx->translate($ctx->{cb}->($ctx, $eml)));
39 my $ibx = $ctx->{ibx};
40 my $desc = ascii_html($ibx->description);
41 my $title = delete($ctx->{-title_html}) // $desc;
42 my $upfx = $ctx->{-upfx} || '';
43 my $help = $upfx.'_/text/help';
44 my $color = $upfx.'_/text/color';
45 my $atom = $ctx->{-atom} || $upfx.'new.atom';
46 my $top = "<b>$desc</b>";
47 if (my $t_max = $ctx->{-t_max}) {
48 $t_max = ts2str($t_max);
49 $top = qq(<a\nhref="$upfx?t=$t_max">$top</a>);
50 # we had some kind of query, link to /$INBOX/?t=YYYYMMDDhhmmss
51 } elsif ($ctx->{qp}->{t}) {
52 $top = qq(<a\nhref="./">$top</a>);
54 my $links = qq(<a\nhref="$help">help</a> / ).
55 qq(<a\nhref="$color">color</a> / ).
56 qq(<a\nhref=#mirror>mirror</a> / ).
57 qq(<a\nhref="$atom">Atom feed</a>);
59 my $q_val = delete($ctx->{-q_value_html}) // '';
60 $q_val = qq(\nvalue="$q_val") if $q_val ne '';
61 # XXX gross, for SearchView.pm
62 my $extra = delete($ctx->{-extra_form_html}) // '';
63 my $action = $upfx eq '' ? './' : $upfx;
64 $top = qq{<form\naction="$action"><pre>$top} .
65 qq{\n<input\nname=q\ntype=text$q_val />} .
67 qq{<input\ntype=submit\nvalue=search />} .
71 $top = '<pre>' . $top . "\n" . $links . '</pre>';
73 "<html><head><title>$title</title>" .
74 qq(<link\nrel=alternate\ntitle="Atom feed"\n).
75 qq(href="$atom"\ntype="application/atom+xml"/>) .
76 $ctx->{www}->style($upfx) .
77 '</head><body>'. $top . (delete($ctx->{-html_tip}) // '');
82 my $cr = $ctx->{ibx}->{coderepo} // return ();
83 my $cfg = $ctx->{www}->{pi_cfg};
84 my $upfx = ($ctx->{-upfx} // ''). '../';
86 for my $cr_name (@$cr) {
87 my $urls = $cfg->{"coderepo.$cr_name.cgiturl"} // next;
89 code repositories for the project(s) associated with this inbox:
92 # relative or absolute URL?, prefix relative "foo.git"
93 # with appropriate number of "../"
94 my $u = m!\A(?:[a-z\+]+:)?//! ? $_ : $upfx.$_;
95 $u = ascii_html(prurl($ctx->{env}, $u));
96 $ret[0] .= qq(\n\t<a\nhref="$u">$u</a>);
99 @ret; # may be empty, this sub is called as an arg for join()
102 sub code_footer ($) {
104 my $u = prurl($env, $CODE_URL);
105 qq(AGPL code for this site: git clone <a\nhref="$u">$u</a>)
112 id=mirror>This inbox may be cloned and mirrored by anyone:</a>
115 my $ibx = $ctx->{ibx};
116 my $desc = ascii_html($ibx->description);
119 my $http = $ctx->{base_url};
120 my $max = $ibx->max_git_epoch;
121 my $dir = (split(m!/!, $http))[-1];
122 my %seen = ($http => 1);
123 if (defined($max)) { # v2
124 for my $i (0..$max) {
125 # old epochs my be deleted:
126 -d "$ibx->{inboxdir}/git/$i.git" or next;
127 my $url = "$http/$i";
129 push @urls, "$url $dir/git/$i.git";
131 my $nr = scalar(@urls);
133 $urls .= "\n\t# this inbox consists of $nr epochs:";
134 $urls[0] .= "\t# oldest";
135 $urls[-1] .= "\t# newest";
141 # FIXME: epoch splits can be different in other repositories,
142 # use the "cloneurl" file as-is for now:
143 foreach my $u (@{$ibx->cloneurl}) {
145 push @urls, $u =~ /\Ahttps?:/ ? qq(<a\nhref="$u">$u</a>) : $u;
148 $urls .= "\n" . join('', map { "\tgit clone --mirror $_\n" } @urls);
149 if (my $addrs = $ibx->{address}) {
150 $addrs = join(' ', @$addrs) if ref($addrs) eq 'ARRAY';
151 my $v = defined $max ? '-V2' : '-V1';
154 # If you have public-inbox 1.1+ installed, you may
155 # initialize and index your mirror using the following commands:
156 public-inbox-init $v $ibx->{name} $dir/ $http \\
158 public-inbox-index $dir
161 my $cfg_link = ($ctx->{-upfx} // '').'_/text/config/raw';
165 href="$cfg_link">config snippet</a> for mirrors.
167 my @nntp = map { qq(<a\nhref="$_">$_</a>) } @{$ibx->nntp_url};
169 $urls .= @nntp == 1 ? 'Newsgroup' : 'Newsgroups are';
170 $urls .= ' available over NNTP:';
171 $urls .= "\n\t" . join("\n\t", @nntp) . "\n";
173 if ($urls =~ m!\b[^:]+://\w+\.onion/!) {
174 $urls .= " note: .onion URLs require Tor: ";
175 $urls .= qq[<a\nhref="$TOR_URL">$TOR_URL</a>];
177 '<hr><pre>'.join("\n\n",
181 code_footer($ctx->{env})
182 ).'</pre></body></html>';
185 # callback for HTTP.pm (and any other PSGI servers)
188 my $cb = $ctx->{cb} or return;
189 while (defined(my $x = $cb->($ctx))) { # x = smsg or scalar non-ref
190 if (ref($x)) { # smsg
191 my $eml = $ctx->{ibx}->smsg_eml($x) or next;
193 return $ctx->translate($cb->($ctx, $eml));
195 return $ctx->translate($x);
199 $ctx->zflush(_html_end($ctx));
202 sub html_oneshot ($$;$) {
203 my ($ctx, $code, $sref) = @_;
204 my $res_hdr = [ 'Content-Type' => 'text/html; charset=UTF-8',
205 'Content-Length' => undef ];
206 bless $ctx, __PACKAGE__;
207 $ctx->{gz} = PublicInbox::GzipFilter::gz_or_noop($res_hdr, $ctx->{env});
208 $ctx->{base_url} //= do {
209 $ctx->zmore(html_top($ctx));
212 $ctx->zmore($$sref) if $sref;
213 my $bdy = $ctx->zflush(_html_end($ctx));
214 $res_hdr->[3] = bytes::length($bdy);
215 [ $code, $res_hdr, [ $bdy ] ]
219 my ($http) = @_; # PublicInbox::HTTP
220 my $ctx = $http->{forward} or return;
222 if (my $smsg = $ctx->{smsg} = $ctx->{cb}->($ctx)) {
223 $ctx->smsg_blob($smsg);
225 $ctx->{http_out}->write(
226 $ctx->translate(_html_end($ctx)));
227 $ctx->close; # GzipFilter->close
234 my ($ctx, $code, $cb) = @_;
235 my $res_hdr = [ 'Content-Type' => 'text/html; charset=UTF-8' ];
237 $ctx->psgi_response($code, $res_hdr);