]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WwwStream.pm
www*stream: gzip ->getline responses
[public-inbox.git] / lib / PublicInbox / WwwStream.pm
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>
3 #
4 # HTML body stream for which yields getline+close methods
5 #
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;
10 use strict;
11 use warnings;
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';
20
21 # noop for HTTP.pm (and any other PSGI servers)
22 sub close {}
23
24 sub base_url ($) {
25         my $ctx = shift;
26         my $base_url = $ctx->{-inbox}->base_url($ctx->{env});
27         chop $base_url; # no trailing slash for clone
28         $base_url;
29 }
30
31 sub new {
32         my ($class, $ctx, $cb) = @_;
33
34         bless {
35                 nr => 0,
36                 cb => $cb,
37                 ctx => $ctx,
38                 base_url => base_url($ctx),
39         }, $class;
40 }
41
42 sub response {
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});
47         [ $code, $h, $self ]
48 }
49
50 sub _html_top ($) {
51         my ($self) = @_;
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>";
64         if ($ibx->search) {
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 />} .
72                           $extra .
73                           qq{<input\ntype=submit\nvalue=search />} .
74                           ' ' . $links .
75                           q{</pre></form>}
76         } else {
77                 $top = '<pre>' . $top . "\n" . $links . '</pre>';
78         }
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}) // '');
84 }
85
86 sub code_footer ($) {
87         my ($env) = @_;
88         my $u = prurl($env, $CODE_URL);
89         qq(AGPL code for this site: git clone <a\nhref="$u">$u</a>)
90 }
91
92 sub _html_end {
93         my ($self) = @_;
94         my $urls = 'Archives are clonable:';
95         my $ctx = $self->{ctx};
96         my $ibx = $ctx->{-inbox};
97         my $desc = ascii_html($ibx->description);
98
99         my @urls;
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";
109                         $seen{$url} = 1;
110                         push @urls, "$url $dir/git/$i.git";
111                 }
112         } else { # v1
113                 push @urls, $http;
114         }
115
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}) {
119                 next if $seen{$u}++;
120                 push @urls, $u =~ /\Ahttps?:/ ? qq(<a\nhref="$u">$u</a>) : $u;
121         }
122
123         if (defined($max) || scalar(@urls) > 1) {
124                 $urls .= "\n" .
125                         join("\n", map { "\tgit clone --mirror $_" } @urls);
126         } else {
127                 $urls .= " git clone --mirror $urls[0]";
128         }
129         if (defined $max) {
130                 my $addrs = $ibx->{address};
131                 $addrs = join(' ', @$addrs) if ref($addrs) eq 'ARRAY';
132                 $urls .=  <<EOF
133
134
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 \\
138                 $addrs
139         public-inbox-index $dir
140 EOF
141         } else { # v1
142                 $urls .= "\n";
143         }
144
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};
148         if (@nntp) {
149                 $urls .= "\n";
150                 $urls .= @nntp == 1 ? 'Newsgroup' : 'Newsgroups are';
151                 $urls .= ' available over NNTP:';
152                 $urls .= "\n\t" . join("\n\t", @nntp) . "\n";
153         }
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>];
157         }
158         '<hr><pre>'.join("\n\n",
159                 $desc,
160                 $urls,
161                 code_footer($ctx->{env})
162         ).'</pre></body></html>';
163 }
164
165 # callback for HTTP.pm (and any other PSGI servers)
166 sub getline {
167         my ($self) = @_;
168         my $nr = $self->{nr}++;
169
170         my $buf = do {
171                 if ($nr == 0) {
172                         _html_top($self);
173                 } elsif (my $middle = $self->{cb}) {
174                         $middle->($nr, $self->{ctx});
175                 }
176         } // (delete($self->{cb}) ? _html_end($self) : undef);
177
178         # gzf may be GzipFilter, `undef' or `0'
179         my $gzf = $self->{gzf} or return $buf;
180
181         return $gzf->translate($buf) if defined $buf;
182         $self->{gzf} = 0; # next call to ->getline returns $buf (== undef)
183         $gzf->translate(undef);
184 }
185
186 sub html_oneshot ($$;$) {
187         my ($ctx, $code, $sref) = @_;
188         my $self = bless {
189                 ctx => $ctx,
190                 base_url => base_url($ctx),
191         }, __PACKAGE__;
192         my @x;
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;
197                 if ($sref) {
198                         $err = $gz->deflate($sref, $x[0]);
199                         die "gzip->deflate: $err" if $err != Z_OK;
200                 }
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;
205         } else {
206                 @x = (_html_top($self), $sref ? $$sref : (), _html_end($self));
207         }
208
209         my $len = 0;
210         $len += bytes::length($_) for @x;
211         push @$h, 'Content-Length', $len;
212         [ $code, $h, \@x ]
213 }
214
215 1;