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 Compress::Raw::Zlib qw(Z_FINISH Z_OK);
17 use PublicInbox::GzipFilter qw(gzip_maybe);
18 our $TOR_URL = 'https://www.torproject.org/';
19 our $CODE_URL = 'https://public-inbox.org/public-inbox.git';
21 # noop for HTTP.pm (and any other PSGI servers)
26 my $base_url = $ctx->{-inbox}->base_url($ctx->{env});
27 chop $base_url; # no trailing slash for clone
32 my ($class, $ctx, $cb) = @_;
38 base_url => base_url($ctx),
43 my ($class, $ctx, $code, $cb) = @_;
44 [ $code, [ 'Content-Type', 'text/html; charset=UTF-8' ],
45 $class->new($ctx, $cb) ]
50 my $ctx = $self->{ctx};
51 my $ibx = $ctx->{-inbox};
52 my $desc = ascii_html($ibx->description);
53 my $title = delete($ctx->{-title_html}) // $desc;
54 my $upfx = $ctx->{-upfx} || '';
55 my $help = $upfx.'_/text/help';
56 my $color = $upfx.'_/text/color';
57 my $atom = $ctx->{-atom} || $upfx.'new.atom';
58 my $top = "<b>$desc</b>";
59 my $links = "<a\nhref=\"$help\">help</a> / ".
60 "<a\nhref=\"$color\">color</a> / ".
61 "<a\nhref=\"$atom\">Atom feed</a>";
63 my $q_val = delete($ctx->{-q_value_html}) // '';
64 $q_val = qq(\nvalue="$q_val") if $q_val ne '';
65 # XXX gross, for SearchView.pm
66 my $extra = delete($ctx->{-extra_form_html}) // '';
67 my $action = $upfx eq '' ? './' : $upfx;
68 $top = qq{<form\naction="$action"><pre>$top} .
69 qq{\n<input\nname=q\ntype=text$q_val />} .
71 qq{<input\ntype=submit\nvalue=search />} .
75 $top = '<pre>' . $top . "\n" . $links . '</pre>';
77 "<html><head><title>$title</title>" .
78 qq(<link\nrel=alternate\ntitle="Atom feed"\n).
79 qq(href="$atom"\ntype="application/atom+xml"/>) .
80 $ctx->{www}->style($upfx) .
81 '</head><body>'. $top . (delete($ctx->{-html_tip}) // '');
86 my $u = prurl($env, $CODE_URL);
87 qq(AGPL code for this site: git clone <a\nhref="$u">$u</a>)
92 my $urls = 'Archives are clonable:';
93 my $ctx = $self->{ctx};
94 my $ibx = $ctx->{-inbox};
95 my $desc = ascii_html($ibx->description);
98 my $http = $self->{base_url};
99 my $max = $ibx->max_git_epoch;
100 my $dir = (split(m!/!, $http))[-1];
101 my %seen = ($http => 1);
102 if (defined($max)) { # v2
103 for my $i (0..$max) {
104 # old parts my be deleted:
105 -d "$ibx->{inboxdir}/git/$i.git" or next;
106 my $url = "$http/$i";
108 push @urls, "$url $dir/git/$i.git";
114 # FIXME: epoch splits can be different in other repositories,
115 # use the "cloneurl" file as-is for now:
116 foreach my $u (@{$ibx->cloneurl}) {
118 push @urls, $u =~ /\Ahttps?:/ ? qq(<a\nhref="$u">$u</a>) : $u;
121 if (defined($max) || scalar(@urls) > 1) {
123 join("\n", map { "\tgit clone --mirror $_" } @urls);
125 $urls .= " git clone --mirror $urls[0]";
128 my $addrs = $ibx->{address};
129 $addrs = join(' ', @$addrs) if ref($addrs) eq 'ARRAY';
133 # If you have public-inbox 1.1+ installed, you may
134 # initialize and index your mirror using the following commands:
135 public-inbox-init -V2 $ibx->{name} $dir/ $http \\
137 public-inbox-index $dir
143 my $cfg_link = ($ctx->{-upfx} // '').'_/text/config/raw';
144 $urls .= qq(\nExample <a\nhref="$cfg_link">config snippet</a> for mirrors\n);
145 my @nntp = map { qq(<a\nhref="$_">$_</a>) } @{$ibx->nntp_url};
148 $urls .= @nntp == 1 ? 'Newsgroup' : 'Newsgroups are';
149 $urls .= ' available over NNTP:';
150 $urls .= "\n\t" . join("\n\t", @nntp) . "\n";
152 if ($urls =~ m!\b[^:]+://\w+\.onion/!) {
153 $urls .= "\n note: .onion URLs require Tor: ";
154 $urls .= qq[<a\nhref="$TOR_URL">$TOR_URL</a>];
156 '<hr><pre>'.join("\n\n",
159 code_footer($ctx->{env})
160 ).'</pre></body></html>';
163 # callback for HTTP.pm (and any other PSGI servers)
166 my $nr = $self->{nr}++;
168 return _html_top($self) if $nr == 0;
170 if (my $middle = $self->{cb}) {
171 $middle = $middle->($nr, $self->{ctx}) and return $middle;
174 delete $self->{cb} ? _html_end($self) : undef;
177 sub html_oneshot ($$;$) {
178 my ($ctx, $code, $sref) = @_;
181 base_url => base_url($ctx),
184 my @h = ('Content-Type' => 'text/html; charset=UTF-8');
185 if (my $gz = gzip_maybe($ctx->{env})) {
186 my $err = $gz->deflate(_html_top($self), $x[0]);
187 die "gzip->deflate: $err" if $err != Z_OK;
189 $err = $gz->deflate($sref, $x[0]);
190 die "gzip->deflate: $err" if $err != Z_OK;
192 $err = $gz->deflate(_html_end($self), $x[0]);
193 die "gzip->deflate: $err" if $err != Z_OK;
194 $err = $gz->flush($x[0], Z_FINISH);
195 die "gzip->flush: $err" if $err != Z_OK;
196 push @h, qw(Vary Accept-Encoding Content-Encoding gzip);
198 @x = (_html_top($self), $sref ? $$sref : (), _html_end($self));
202 $len += bytes::length($_) for @x;
203 push @h, 'Content-Length', $len;