]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WwwStream.pm
gzipfilter: replace Compress::Raw::Deflate usages
[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 PublicInbox::GzipFilter qw(gzf_maybe);
17 our $TOR_URL = 'https://www.torproject.org/';
18 our $CODE_URL = 'https://public-inbox.org/public-inbox.git';
19
20 # noop for HTTP.pm (and any other PSGI servers)
21 sub close {}
22
23 sub base_url ($) {
24         my $ctx = shift;
25         my $base_url = $ctx->{-inbox}->base_url($ctx->{env});
26         chop $base_url; # no trailing slash for clone
27         $base_url;
28 }
29
30 sub new {
31         my ($class, $ctx, $cb) = @_;
32
33         bless {
34                 nr => 0,
35                 cb => $cb,
36                 ctx => $ctx,
37                 base_url => base_url($ctx),
38         }, $class;
39 }
40
41 sub response {
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});
46         [ $code, $h, $self ]
47 }
48
49 sub _html_top ($) {
50         my ($self) = @_;
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>";
63         if ($ibx->search) {
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 />} .
71                           $extra .
72                           qq{<input\ntype=submit\nvalue=search />} .
73                           ' ' . $links .
74                           q{</pre></form>}
75         } else {
76                 $top = '<pre>' . $top . "\n" . $links . '</pre>';
77         }
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}) // '');
83 }
84
85 sub code_footer ($) {
86         my ($env) = @_;
87         my $u = prurl($env, $CODE_URL);
88         qq(AGPL code for this site: git clone <a\nhref="$u">$u</a>)
89 }
90
91 sub _html_end {
92         my ($self) = @_;
93         my $urls = 'Archives are clonable:';
94         my $ctx = $self->{ctx};
95         my $ibx = $ctx->{-inbox};
96         my $desc = ascii_html($ibx->description);
97
98         my @urls;
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";
108                         $seen{$url} = 1;
109                         push @urls, "$url $dir/git/$i.git";
110                 }
111         } else { # v1
112                 push @urls, $http;
113         }
114
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}) {
118                 next if $seen{$u}++;
119                 push @urls, $u =~ /\Ahttps?:/ ? qq(<a\nhref="$u">$u</a>) : $u;
120         }
121
122         if (defined($max) || scalar(@urls) > 1) {
123                 $urls .= "\n" .
124                         join("\n", map { "\tgit clone --mirror $_" } @urls);
125         } else {
126                 $urls .= " git clone --mirror $urls[0]";
127         }
128         if (defined $max) {
129                 my $addrs = $ibx->{address};
130                 $addrs = join(' ', @$addrs) if ref($addrs) eq 'ARRAY';
131                 $urls .=  <<EOF
132
133
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 \\
137                 $addrs
138         public-inbox-index $dir
139 EOF
140         } else { # v1
141                 $urls .= "\n";
142         }
143
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};
147         if (@nntp) {
148                 $urls .= "\n";
149                 $urls .= @nntp == 1 ? 'Newsgroup' : 'Newsgroups are';
150                 $urls .= ' available over NNTP:';
151                 $urls .= "\n\t" . join("\n\t", @nntp) . "\n";
152         }
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>];
156         }
157         '<hr><pre>'.join("\n\n",
158                 $desc,
159                 $urls,
160                 code_footer($ctx->{env})
161         ).'</pre></body></html>';
162 }
163
164 # callback for HTTP.pm (and any other PSGI servers)
165 sub getline {
166         my ($self) = @_;
167         my $nr = $self->{nr}++;
168
169         my $buf = do {
170                 if ($nr == 0) {
171                         _html_top($self);
172                 } elsif (my $middle = $self->{cb}) {
173                         $middle->($nr, $self->{ctx});
174                 }
175         } // (delete($self->{cb}) ? _html_end($self) : undef);
176
177         # gzf may be GzipFilter, `undef' or `0'
178         my $gzf = $self->{gzf} or return $buf;
179
180         return $gzf->translate($buf) if defined $buf;
181         $self->{gzf} = 0; # next call to ->getline returns $buf (== undef)
182         $gzf->translate(undef);
183 }
184
185 sub html_oneshot ($$;$) {
186         my ($ctx, $code, $sref) = @_;
187         my $self = bless {
188                 ctx => $ctx,
189                 base_url => base_url($ctx),
190         }, __PACKAGE__;
191         my @x;
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]);
199         } else {
200                 @x = (_html_top($self), $sref ? $$sref : (), _html_end($self));
201                 $h->[3] += bytes::length($_) for @x;
202         }
203         [ $code, $h, \@x ]
204 }
205
206 1;