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;
12 use base qw(Exporter);
13 our @EXPORT_OK = qw(html_oneshot);
14 use bytes (); # length
15 use PublicInbox::Hval qw(ascii_html prurl);
16 use PublicInbox::GzipFilter qw(gzf_maybe);
17 our $TOR_URL = 'https://www.torproject.org/';
18 our $CODE_URL = 'https://public-inbox.org/public-inbox.git';
20 # noop for HTTP.pm (and any other PSGI servers)
25 my $base_url = $ctx->{-inbox}->base_url($ctx->{env});
26 chop $base_url; # no trailing slash for clone
33 $ctx->{base_url} = base_url($ctx);
34 bless $ctx, __PACKAGE__;
38 my ($ctx, $code, $cb) = @_;
39 my $h = [ 'Content-Type', 'text/html; charset=UTF-8' ];
41 $ctx->{gzf} = gzf_maybe($h, $ctx->{env});
47 my $ibx = $ctx->{-inbox};
48 my $desc = ascii_html($ibx->description);
49 my $title = delete($ctx->{-title_html}) // $desc;
50 my $upfx = $ctx->{-upfx} || '';
51 my $help = $upfx.'_/text/help';
52 my $color = $upfx.'_/text/color';
53 my $atom = $ctx->{-atom} || $upfx.'new.atom';
54 my $top = "<b>$desc</b>";
55 my $links = "<a\nhref=\"$help\">help</a> / ".
56 "<a\nhref=\"$color\">color</a> / ".
57 "<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 $u = prurl($env, $CODE_URL);
83 qq(AGPL code for this site: git clone <a\nhref="$u">$u</a>)
88 my $urls = 'Archives are clonable:';
89 my $ibx = $ctx->{-inbox};
90 my $desc = ascii_html($ibx->description);
93 my $http = $ctx->{base_url};
94 my $max = $ibx->max_git_epoch;
95 my $dir = (split(m!/!, $http))[-1];
96 my %seen = ($http => 1);
97 if (defined($max)) { # v2
99 # old parts my be deleted:
100 -d "$ibx->{inboxdir}/git/$i.git" or next;
101 my $url = "$http/$i";
103 push @urls, "$url $dir/git/$i.git";
109 # FIXME: epoch splits can be different in other repositories,
110 # use the "cloneurl" file as-is for now:
111 foreach my $u (@{$ibx->cloneurl}) {
113 push @urls, $u =~ /\Ahttps?:/ ? qq(<a\nhref="$u">$u</a>) : $u;
116 if (defined($max) || scalar(@urls) > 1) {
118 join("\n", map { "\tgit clone --mirror $_" } @urls);
120 $urls .= " git clone --mirror $urls[0]";
123 my $addrs = $ibx->{address};
124 $addrs = join(' ', @$addrs) if ref($addrs) eq 'ARRAY';
128 # If you have public-inbox 1.1+ installed, you may
129 # initialize and index your mirror using the following commands:
130 public-inbox-init -V2 $ibx->{name} $dir/ $http \\
132 public-inbox-index $dir
138 my $cfg_link = ($ctx->{-upfx} // '').'_/text/config/raw';
139 $urls .= qq(\nExample <a\nhref="$cfg_link">config snippet</a> for mirrors\n);
140 my @nntp = map { qq(<a\nhref="$_">$_</a>) } @{$ibx->nntp_url};
143 $urls .= @nntp == 1 ? 'Newsgroup' : 'Newsgroups are';
144 $urls .= ' available over NNTP:';
145 $urls .= "\n\t" . join("\n\t", @nntp) . "\n";
147 if ($urls =~ m!\b[^:]+://\w+\.onion/!) {
148 $urls .= "\n note: .onion URLs require Tor: ";
149 $urls .= qq[<a\nhref="$TOR_URL">$TOR_URL</a>];
151 '<hr><pre>'.join("\n\n",
154 code_footer($ctx->{env})
155 ).'</pre></body></html>';
158 # callback for HTTP.pm (and any other PSGI servers)
162 my $buf = $cb->($ctx) if $cb;
163 $buf //= delete($ctx->{cb}) ? _html_end($ctx) : undef;
165 # gzf may be GzipFilter, `undef' or `0'
166 my $gzf = $ctx->{gzf} or return $buf;
168 return $gzf->translate($buf) if defined $buf;
169 $ctx->{gzf} = 0; # next call to ->getline returns $buf (== undef)
170 $gzf->translate(undef);
173 sub html_oneshot ($$;$) {
174 my ($ctx, $code, $sref) = @_;
175 $ctx->{base_url} = base_url($ctx);
176 bless $ctx, __PACKAGE__;
178 my $h = [ 'Content-Type' => 'text/html; charset=UTF-8',
179 'Content-Length' => undef ];
180 if (my $gzf = gzf_maybe($h, $ctx->{env})) {
181 $gzf->zmore(html_top($ctx));
182 $gzf->zmore($$sref) if $sref;
183 $x[0] = $gzf->zflush(_html_end($ctx));
184 $h->[3] = length($x[0]);
186 @x = (html_top($ctx), $sref ? $$sref : (), _html_end($ctx));
187 $h->[3] += bytes::length($_) for @x;