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 gzf_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 my $h = [ 'Content-Type', 'text/html; charset=UTF-8' ];
45 my $self = $class->new($ctx, $cb);
46 $self->{gzf} = gzf_maybe($h, $ctx->{env});
52 my $ctx = $self->{ctx};
53 my $ibx = $ctx->{-inbox};
54 my $desc = ascii_html($ibx->description);
55 my $title = delete($ctx->{-title_html}) // $desc;
56 my $upfx = $ctx->{-upfx} || '';
57 my $help = $upfx.'_/text/help';
58 my $color = $upfx.'_/text/color';
59 my $atom = $ctx->{-atom} || $upfx.'new.atom';
60 my $top = "<b>$desc</b>";
61 my $links = "<a\nhref=\"$help\">help</a> / ".
62 "<a\nhref=\"$color\">color</a> / ".
63 "<a\nhref=\"$atom\">Atom feed</a>";
65 my $q_val = delete($ctx->{-q_value_html}) // '';
66 $q_val = qq(\nvalue="$q_val") if $q_val ne '';
67 # XXX gross, for SearchView.pm
68 my $extra = delete($ctx->{-extra_form_html}) // '';
69 my $action = $upfx eq '' ? './' : $upfx;
70 $top = qq{<form\naction="$action"><pre>$top} .
71 qq{\n<input\nname=q\ntype=text$q_val />} .
73 qq{<input\ntype=submit\nvalue=search />} .
77 $top = '<pre>' . $top . "\n" . $links . '</pre>';
79 "<html><head><title>$title</title>" .
80 qq(<link\nrel=alternate\ntitle="Atom feed"\n).
81 qq(href="$atom"\ntype="application/atom+xml"/>) .
82 $ctx->{www}->style($upfx) .
83 '</head><body>'. $top . (delete($ctx->{-html_tip}) // '');
88 my $u = prurl($env, $CODE_URL);
89 qq(AGPL code for this site: git clone <a\nhref="$u">$u</a>)
94 my $urls = 'Archives are clonable:';
95 my $ctx = $self->{ctx};
96 my $ibx = $ctx->{-inbox};
97 my $desc = ascii_html($ibx->description);
100 my $http = $self->{base_url};
101 my $max = $ibx->max_git_epoch;
102 my $dir = (split(m!/!, $http))[-1];
103 my %seen = ($http => 1);
104 if (defined($max)) { # v2
105 for my $i (0..$max) {
106 # old parts my be deleted:
107 -d "$ibx->{inboxdir}/git/$i.git" or next;
108 my $url = "$http/$i";
110 push @urls, "$url $dir/git/$i.git";
116 # FIXME: epoch splits can be different in other repositories,
117 # use the "cloneurl" file as-is for now:
118 foreach my $u (@{$ibx->cloneurl}) {
120 push @urls, $u =~ /\Ahttps?:/ ? qq(<a\nhref="$u">$u</a>) : $u;
123 if (defined($max) || scalar(@urls) > 1) {
125 join("\n", map { "\tgit clone --mirror $_" } @urls);
127 $urls .= " git clone --mirror $urls[0]";
130 my $addrs = $ibx->{address};
131 $addrs = join(' ', @$addrs) if ref($addrs) eq 'ARRAY';
135 # If you have public-inbox 1.1+ installed, you may
136 # initialize and index your mirror using the following commands:
137 public-inbox-init -V2 $ibx->{name} $dir/ $http \\
139 public-inbox-index $dir
145 my $cfg_link = ($ctx->{-upfx} // '').'_/text/config/raw';
146 $urls .= qq(\nExample <a\nhref="$cfg_link">config snippet</a> for mirrors\n);
147 my @nntp = map { qq(<a\nhref="$_">$_</a>) } @{$ibx->nntp_url};
150 $urls .= @nntp == 1 ? 'Newsgroup' : 'Newsgroups are';
151 $urls .= ' available over NNTP:';
152 $urls .= "\n\t" . join("\n\t", @nntp) . "\n";
154 if ($urls =~ m!\b[^:]+://\w+\.onion/!) {
155 $urls .= "\n note: .onion URLs require Tor: ";
156 $urls .= qq[<a\nhref="$TOR_URL">$TOR_URL</a>];
158 '<hr><pre>'.join("\n\n",
161 code_footer($ctx->{env})
162 ).'</pre></body></html>';
165 # callback for HTTP.pm (and any other PSGI servers)
168 my $nr = $self->{nr}++;
173 } elsif (my $middle = $self->{cb}) {
174 $middle->($nr, $self->{ctx});
176 } // (delete($self->{cb}) ? _html_end($self) : undef);
178 # gzf may be GzipFilter, `undef' or `0'
179 my $gzf = $self->{gzf} or return $buf;
181 return $gzf->translate($buf) if defined $buf;
182 $self->{gzf} = 0; # next call to ->getline returns $buf (== undef)
183 $gzf->translate(undef);
186 sub html_oneshot ($$;$) {
187 my ($ctx, $code, $sref) = @_;
190 base_url => base_url($ctx),
193 my $h = [ 'Content-Type' => 'text/html; charset=UTF-8' ];
194 if (my $gz = gzip_maybe($h, $ctx->{env})) {
195 my $err = $gz->deflate(_html_top($self), $x[0]);
196 die "gzip->deflate: $err" if $err != Z_OK;
198 $err = $gz->deflate($sref, $x[0]);
199 die "gzip->deflate: $err" if $err != Z_OK;
201 $err = $gz->deflate(_html_end($self), $x[0]);
202 die "gzip->deflate: $err" if $err != Z_OK;
203 $err = $gz->flush($x[0], Z_FINISH);
204 die "gzip->flush: $err" if $err != Z_OK;
206 @x = (_html_top($self), $sref ? $$sref : (), _html_end($self));
210 $len += bytes::length($_) for @x;
211 push @$h, 'Content-Length', $len;