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
31 my ($class, $ctx, $cb) = @_;
37 base_url => base_url($ctx),
42 my ($class, $ctx, $code, $cb) = @_;
43 my $h = [ 'Content-Type', 'text/html; charset=UTF-8' ];
44 my $self = $class->new($ctx, $cb);
45 $self->{gzf} = gzf_maybe($h, $ctx->{env});
51 my $ctx = $self->{ctx};
52 my $ibx = $ctx->{-inbox};
53 my $desc = ascii_html($ibx->description);
54 my $title = delete($ctx->{-title_html}) // $desc;
55 my $upfx = $ctx->{-upfx} || '';
56 my $help = $upfx.'_/text/help';
57 my $color = $upfx.'_/text/color';
58 my $atom = $ctx->{-atom} || $upfx.'new.atom';
59 my $top = "<b>$desc</b>";
60 my $links = "<a\nhref=\"$help\">help</a> / ".
61 "<a\nhref=\"$color\">color</a> / ".
62 "<a\nhref=\"$atom\">Atom feed</a>";
64 my $q_val = delete($ctx->{-q_value_html}) // '';
65 $q_val = qq(\nvalue="$q_val") if $q_val ne '';
66 # XXX gross, for SearchView.pm
67 my $extra = delete($ctx->{-extra_form_html}) // '';
68 my $action = $upfx eq '' ? './' : $upfx;
69 $top = qq{<form\naction="$action"><pre>$top} .
70 qq{\n<input\nname=q\ntype=text$q_val />} .
72 qq{<input\ntype=submit\nvalue=search />} .
76 $top = '<pre>' . $top . "\n" . $links . '</pre>';
78 "<html><head><title>$title</title>" .
79 qq(<link\nrel=alternate\ntitle="Atom feed"\n).
80 qq(href="$atom"\ntype="application/atom+xml"/>) .
81 $ctx->{www}->style($upfx) .
82 '</head><body>'. $top . (delete($ctx->{-html_tip}) // '');
87 my $u = prurl($env, $CODE_URL);
88 qq(AGPL code for this site: git clone <a\nhref="$u">$u</a>)
93 my $urls = 'Archives are clonable:';
94 my $ctx = $self->{ctx};
95 my $ibx = $ctx->{-inbox};
96 my $desc = ascii_html($ibx->description);
99 my $http = $self->{base_url};
100 my $max = $ibx->max_git_epoch;
101 my $dir = (split(m!/!, $http))[-1];
102 my %seen = ($http => 1);
103 if (defined($max)) { # v2
104 for my $i (0..$max) {
105 # old parts my be deleted:
106 -d "$ibx->{inboxdir}/git/$i.git" or next;
107 my $url = "$http/$i";
109 push @urls, "$url $dir/git/$i.git";
115 # FIXME: epoch splits can be different in other repositories,
116 # use the "cloneurl" file as-is for now:
117 foreach my $u (@{$ibx->cloneurl}) {
119 push @urls, $u =~ /\Ahttps?:/ ? qq(<a\nhref="$u">$u</a>) : $u;
122 if (defined($max) || scalar(@urls) > 1) {
124 join("\n", map { "\tgit clone --mirror $_" } @urls);
126 $urls .= " git clone --mirror $urls[0]";
129 my $addrs = $ibx->{address};
130 $addrs = join(' ', @$addrs) if ref($addrs) eq 'ARRAY';
134 # If you have public-inbox 1.1+ installed, you may
135 # initialize and index your mirror using the following commands:
136 public-inbox-init -V2 $ibx->{name} $dir/ $http \\
138 public-inbox-index $dir
144 my $cfg_link = ($ctx->{-upfx} // '').'_/text/config/raw';
145 $urls .= qq(\nExample <a\nhref="$cfg_link">config snippet</a> for mirrors\n);
146 my @nntp = map { qq(<a\nhref="$_">$_</a>) } @{$ibx->nntp_url};
149 $urls .= @nntp == 1 ? 'Newsgroup' : 'Newsgroups are';
150 $urls .= ' available over NNTP:';
151 $urls .= "\n\t" . join("\n\t", @nntp) . "\n";
153 if ($urls =~ m!\b[^:]+://\w+\.onion/!) {
154 $urls .= "\n note: .onion URLs require Tor: ";
155 $urls .= qq[<a\nhref="$TOR_URL">$TOR_URL</a>];
157 '<hr><pre>'.join("\n\n",
160 code_footer($ctx->{env})
161 ).'</pre></body></html>';
164 # callback for HTTP.pm (and any other PSGI servers)
167 my $nr = $self->{nr}++;
172 } elsif (my $middle = $self->{cb}) {
173 $middle->($nr, $self->{ctx});
175 } // (delete($self->{cb}) ? _html_end($self) : undef);
177 # gzf may be GzipFilter, `undef' or `0'
178 my $gzf = $self->{gzf} or return $buf;
180 return $gzf->translate($buf) if defined $buf;
181 $self->{gzf} = 0; # next call to ->getline returns $buf (== undef)
182 $gzf->translate(undef);
185 sub html_oneshot ($$;$) {
186 my ($ctx, $code, $sref) = @_;
189 base_url => base_url($ctx),
192 my $h = [ 'Content-Type' => 'text/html; charset=UTF-8',
193 'Content-Length' => undef ];
194 if (my $gzf = gzf_maybe($h, $ctx->{env})) {
195 $gzf->zmore(_html_top($self));
196 $gzf->zmore($$sref) if $sref;
197 $x[0] = $gzf->zflush(_html_end($self));
198 $h->[3] = length($x[0]);
200 @x = (_html_top($self), $sref ? $$sref : (), _html_end($self));
201 $h->[3] += bytes::length($_) for @x;