]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WwwStream.pm
wwwstream: oneshot: perform gzip without middleware
[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);
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         [ $code, [ 'Content-Type', 'text/html; charset=UTF-8' ],
45           $class->new($ctx, $cb) ]
46 }
47
48 sub _html_top ($) {
49         my ($self) = @_;
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>";
62         if ($ibx->search) {
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 />} .
70                           $extra .
71                           qq{<input\ntype=submit\nvalue=search />} .
72                           ' ' . $links .
73                           q{</pre></form>}
74         } else {
75                 $top = '<pre>' . $top . "\n" . $links . '</pre>';
76         }
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}) // '');
82 }
83
84 sub code_footer ($) {
85         my ($env) = @_;
86         my $u = prurl($env, $CODE_URL);
87         qq(AGPL code for this site: git clone <a\nhref="$u">$u</a>)
88 }
89
90 sub _html_end {
91         my ($self) = @_;
92         my $urls = 'Archives are clonable:';
93         my $ctx = $self->{ctx};
94         my $ibx = $ctx->{-inbox};
95         my $desc = ascii_html($ibx->description);
96
97         my @urls;
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";
107                         $seen{$url} = 1;
108                         push @urls, "$url $dir/git/$i.git";
109                 }
110         } else { # v1
111                 push @urls, $http;
112         }
113
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}) {
117                 next if $seen{$u}++;
118                 push @urls, $u =~ /\Ahttps?:/ ? qq(<a\nhref="$u">$u</a>) : $u;
119         }
120
121         if (defined($max) || scalar(@urls) > 1) {
122                 $urls .= "\n" .
123                         join("\n", map { "\tgit clone --mirror $_" } @urls);
124         } else {
125                 $urls .= " git clone --mirror $urls[0]";
126         }
127         if (defined $max) {
128                 my $addrs = $ibx->{address};
129                 $addrs = join(' ', @$addrs) if ref($addrs) eq 'ARRAY';
130                 $urls .=  <<EOF
131
132
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 \\
136                 $addrs
137         public-inbox-index $dir
138 EOF
139         } else { # v1
140                 $urls .= "\n";
141         }
142
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};
146         if (@nntp) {
147                 $urls .= "\n";
148                 $urls .= @nntp == 1 ? 'Newsgroup' : 'Newsgroups are';
149                 $urls .= ' available over NNTP:';
150                 $urls .= "\n\t" . join("\n\t", @nntp) . "\n";
151         }
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>];
155         }
156         '<hr><pre>'.join("\n\n",
157                 $desc,
158                 $urls,
159                 code_footer($ctx->{env})
160         ).'</pre></body></html>';
161 }
162
163 # callback for HTTP.pm (and any other PSGI servers)
164 sub getline {
165         my ($self) = @_;
166         my $nr = $self->{nr}++;
167
168         return _html_top($self) if $nr == 0;
169
170         if (my $middle = $self->{cb}) {
171                 $middle = $middle->($nr, $self->{ctx}) and return $middle;
172         }
173
174         delete $self->{cb} ? _html_end($self) : undef;
175 }
176
177 sub html_oneshot ($$;$) {
178         my ($ctx, $code, $sref) = @_;
179         my $self = bless {
180                 ctx => $ctx,
181                 base_url => base_url($ctx),
182         }, __PACKAGE__;
183         my @x;
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;
188                 if ($sref) {
189                         $err = $gz->deflate($sref, $x[0]);
190                         die "gzip->deflate: $err" if $err != Z_OK;
191                 }
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);
197         } else {
198                 @x = (_html_top($self), $sref ? $$sref : (), _html_end($self));
199         }
200
201         my $len = 0;
202         $len += bytes::length($_) for @x;
203         push @h, 'Content-Length', $len;
204         [ $code, \@h, \@x ]
205 }
206
207 1;