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
6 # public-inbox-httpd favors "getline" response bodies to take a
7 # "pull"-based approach to feeding slow clients (as opposed to a
8 # more common "push" model)
9 package PublicInbox::WwwStream;
11 use parent qw(Exporter PublicInbox::GzipFilter);
12 our @EXPORT_OK = qw(html_oneshot);
13 use bytes (); # length
14 use PublicInbox::Hval qw(ascii_html prurl);
15 our $TOR_URL = 'https://www.torproject.org/';
16 our $CODE_URL = 'https://public-inbox.org/public-inbox.git';
18 # noop for HTTP.pm (and any other PSGI servers)
23 my $base_url = $ctx->{-inbox}->base_url($ctx->{env});
24 chop $base_url; # no trailing slash for clone
31 $ctx->{base_url} = base_url($ctx);
32 bless $ctx, __PACKAGE__;
36 my ($ctx, $code, $cb) = @_;
37 my $res_hdr = [ 'Content-Type' => 'text/html; charset=UTF-8' ];
39 $ctx->{gz} = PublicInbox::GzipFilter::gz_or_noop($res_hdr, $ctx->{env});
40 [ $code, $res_hdr, $ctx ]
45 my $ibx = $ctx->{-inbox};
46 my $desc = ascii_html($ibx->description);
47 my $title = delete($ctx->{-title_html}) // $desc;
48 my $upfx = $ctx->{-upfx} || '';
49 my $help = $upfx.'_/text/help';
50 my $color = $upfx.'_/text/color';
51 my $atom = $ctx->{-atom} || $upfx.'new.atom';
52 my $top = "<b>$desc</b>";
53 my $links = "<a\nhref=\"$help\">help</a> / ".
54 "<a\nhref=\"$color\">color</a> / ".
55 "<a\nhref=\"$atom\">Atom feed</a>";
57 my $q_val = delete($ctx->{-q_value_html}) // '';
58 $q_val = qq(\nvalue="$q_val") if $q_val ne '';
59 # XXX gross, for SearchView.pm
60 my $extra = delete($ctx->{-extra_form_html}) // '';
61 my $action = $upfx eq '' ? './' : $upfx;
62 $top = qq{<form\naction="$action"><pre>$top} .
63 qq{\n<input\nname=q\ntype=text$q_val />} .
65 qq{<input\ntype=submit\nvalue=search />} .
69 $top = '<pre>' . $top . "\n" . $links . '</pre>';
71 "<html><head><title>$title</title>" .
72 qq(<link\nrel=alternate\ntitle="Atom feed"\n).
73 qq(href="$atom"\ntype="application/atom+xml"/>) .
74 $ctx->{www}->style($upfx) .
75 '</head><body>'. $top . (delete($ctx->{-html_tip}) // '');
80 my $u = prurl($env, $CODE_URL);
81 qq(AGPL code for this site: git clone <a\nhref="$u">$u</a>)
86 my $urls = 'Archives are clonable:';
87 my $ibx = $ctx->{-inbox};
88 my $desc = ascii_html($ibx->description);
91 my $http = $ctx->{base_url};
92 my $max = $ibx->max_git_epoch;
93 my $dir = (split(m!/!, $http))[-1];
94 my %seen = ($http => 1);
95 if (defined($max)) { # v2
97 # old parts my be deleted:
98 -d "$ibx->{inboxdir}/git/$i.git" or next;
101 push @urls, "$url $dir/git/$i.git";
107 # FIXME: epoch splits can be different in other repositories,
108 # use the "cloneurl" file as-is for now:
109 foreach my $u (@{$ibx->cloneurl}) {
111 push @urls, $u =~ /\Ahttps?:/ ? qq(<a\nhref="$u">$u</a>) : $u;
114 if (defined($max) || scalar(@urls) > 1) {
116 join("\n", map { "\tgit clone --mirror $_" } @urls);
118 $urls .= " git clone --mirror $urls[0]";
121 my $addrs = $ibx->{address};
122 $addrs = join(' ', @$addrs) if ref($addrs) eq 'ARRAY';
126 # If you have public-inbox 1.1+ installed, you may
127 # initialize and index your mirror using the following commands:
128 public-inbox-init -V2 $ibx->{name} $dir/ $http \\
130 public-inbox-index $dir
136 my $cfg_link = ($ctx->{-upfx} // '').'_/text/config/raw';
137 $urls .= qq(\nExample <a\nhref="$cfg_link">config snippet</a> for mirrors\n);
138 my @nntp = map { qq(<a\nhref="$_">$_</a>) } @{$ibx->nntp_url};
141 $urls .= @nntp == 1 ? 'Newsgroup' : 'Newsgroups are';
142 $urls .= ' available over NNTP:';
143 $urls .= "\n\t" . join("\n\t", @nntp) . "\n";
145 if ($urls =~ m!\b[^:]+://\w+\.onion/!) {
146 $urls .= "\n note: .onion URLs require Tor: ";
147 $urls .= qq[<a\nhref="$TOR_URL">$TOR_URL</a>];
149 '<hr><pre>'.join("\n\n",
152 code_footer($ctx->{env})
153 ).'</pre></body></html>';
156 # callback for HTTP.pm (and any other PSGI servers)
159 my $cb = $ctx->{cb} or return;
160 if (defined(my $buf = $cb->($ctx))) {
161 return $ctx->translate($buf);
164 $ctx->zflush(_html_end($ctx));
167 sub html_oneshot ($$;$) {
168 my ($ctx, $code, $sref) = @_;
169 $ctx->{base_url} = base_url($ctx);
170 bless $ctx, __PACKAGE__;
172 my $res_hdr = [ 'Content-Type' => 'text/html; charset=UTF-8',
173 'Content-Length' => undef ];
174 $ctx->{gz} = PublicInbox::GzipFilter::gz_or_noop($res_hdr, $ctx->{env});
175 $ctx->zmore(html_top($ctx));
176 $ctx->zmore($$sref) if $sref;
177 $bdy[0] = $ctx->zflush(_html_end($ctx));
178 $res_hdr->[3] = bytes::length($bdy[0]);
179 [ $code, $res_hdr, \@bdy ]