]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WwwStream.pm
wwwstream: use parent.pm and no warnings
[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 parent qw(Exporter);
12 our @EXPORT_OK = qw(html_oneshot);
13 use bytes (); # length
14 use PublicInbox::Hval qw(ascii_html prurl);
15 use PublicInbox::GzipFilter qw(gzf_maybe);
16 our $TOR_URL = 'https://www.torproject.org/';
17 our $CODE_URL = 'https://public-inbox.org/public-inbox.git';
18
19 # noop for HTTP.pm (and any other PSGI servers)
20 sub close {}
21
22 sub base_url ($) {
23         my $ctx = shift;
24         my $base_url = $ctx->{-inbox}->base_url($ctx->{env});
25         chop $base_url; # no trailing slash for clone
26         $base_url;
27 }
28
29 sub init {
30         my ($ctx, $cb) = @_;
31         $ctx->{cb} = $cb;
32         $ctx->{base_url} = base_url($ctx);
33         bless $ctx, __PACKAGE__;
34 }
35
36 sub response {
37         my ($ctx, $code, $cb) = @_;
38         my $h = [ 'Content-Type', 'text/html; charset=UTF-8' ];
39         init($ctx, $cb);
40         $ctx->{gzf} = gzf_maybe($h, $ctx->{env});
41         [ $code, $h, $ctx ]
42 }
43
44 sub html_top ($) {
45         my ($ctx) = @_;
46         my $ibx = $ctx->{-inbox};
47         my $desc = ascii_html($ibx->description);
48         my $title = delete($ctx->{-title_html}) // $desc;
49         my $upfx = $ctx->{-upfx} || '';
50         my $help = $upfx.'_/text/help';
51         my $color = $upfx.'_/text/color';
52         my $atom = $ctx->{-atom} || $upfx.'new.atom';
53         my $top = "<b>$desc</b>";
54         my $links = "<a\nhref=\"$help\">help</a> / ".
55                         "<a\nhref=\"$color\">color</a> / ".
56                         "<a\nhref=\"$atom\">Atom feed</a>";
57         if ($ibx->search) {
58                 my $q_val = delete($ctx->{-q_value_html}) // '';
59                 $q_val = qq(\nvalue="$q_val") if $q_val ne '';
60                 # XXX gross, for SearchView.pm
61                 my $extra = delete($ctx->{-extra_form_html}) // '';
62                 my $action = $upfx eq '' ? './' : $upfx;
63                 $top = qq{<form\naction="$action"><pre>$top} .
64                           qq{\n<input\nname=q\ntype=text$q_val />} .
65                           $extra .
66                           qq{<input\ntype=submit\nvalue=search />} .
67                           ' ' . $links .
68                           q{</pre></form>}
69         } else {
70                 $top = '<pre>' . $top . "\n" . $links . '</pre>';
71         }
72         "<html><head><title>$title</title>" .
73                 qq(<link\nrel=alternate\ntitle="Atom feed"\n).
74                 qq(href="$atom"\ntype="application/atom+xml"/>) .
75                 $ctx->{www}->style($upfx) .
76                 '</head><body>'. $top . (delete($ctx->{-html_tip}) // '');
77 }
78
79 sub code_footer ($) {
80         my ($env) = @_;
81         my $u = prurl($env, $CODE_URL);
82         qq(AGPL code for this site: git clone <a\nhref="$u">$u</a>)
83 }
84
85 sub _html_end {
86         my ($ctx) = @_;
87         my $urls = 'Archives are clonable:';
88         my $ibx = $ctx->{-inbox};
89         my $desc = ascii_html($ibx->description);
90
91         my @urls;
92         my $http = $ctx->{base_url};
93         my $max = $ibx->max_git_epoch;
94         my $dir = (split(m!/!, $http))[-1];
95         my %seen = ($http => 1);
96         if (defined($max)) { # v2
97                 for my $i (0..$max) {
98                         # old parts my be deleted:
99                         -d "$ibx->{inboxdir}/git/$i.git" or next;
100                         my $url = "$http/$i";
101                         $seen{$url} = 1;
102                         push @urls, "$url $dir/git/$i.git";
103                 }
104         } else { # v1
105                 push @urls, $http;
106         }
107
108         # FIXME: epoch splits can be different in other repositories,
109         # use the "cloneurl" file as-is for now:
110         foreach my $u (@{$ibx->cloneurl}) {
111                 next if $seen{$u}++;
112                 push @urls, $u =~ /\Ahttps?:/ ? qq(<a\nhref="$u">$u</a>) : $u;
113         }
114
115         if (defined($max) || scalar(@urls) > 1) {
116                 $urls .= "\n" .
117                         join("\n", map { "\tgit clone --mirror $_" } @urls);
118         } else {
119                 $urls .= " git clone --mirror $urls[0]";
120         }
121         if (defined $max) {
122                 my $addrs = $ibx->{address};
123                 $addrs = join(' ', @$addrs) if ref($addrs) eq 'ARRAY';
124                 $urls .=  <<EOF
125
126
127         # If you have public-inbox 1.1+ installed, you may
128         # initialize and index your mirror using the following commands:
129         public-inbox-init -V2 $ibx->{name} $dir/ $http \\
130                 $addrs
131         public-inbox-index $dir
132 EOF
133         } else { # v1
134                 $urls .= "\n";
135         }
136
137         my $cfg_link = ($ctx->{-upfx} // '').'_/text/config/raw';
138         $urls .= qq(\nExample <a\nhref="$cfg_link">config snippet</a> for mirrors\n);
139         my @nntp = map { qq(<a\nhref="$_">$_</a>) } @{$ibx->nntp_url};
140         if (@nntp) {
141                 $urls .= "\n";
142                 $urls .= @nntp == 1 ? 'Newsgroup' : 'Newsgroups are';
143                 $urls .= ' available over NNTP:';
144                 $urls .= "\n\t" . join("\n\t", @nntp) . "\n";
145         }
146         if ($urls =~ m!\b[^:]+://\w+\.onion/!) {
147                 $urls .= "\n note: .onion URLs require Tor: ";
148                 $urls .= qq[<a\nhref="$TOR_URL">$TOR_URL</a>];
149         }
150         '<hr><pre>'.join("\n\n",
151                 $desc,
152                 $urls,
153                 code_footer($ctx->{env})
154         ).'</pre></body></html>';
155 }
156
157 # callback for HTTP.pm (and any other PSGI servers)
158 sub getline {
159         my ($ctx) = @_;
160         my $cb = $ctx->{cb};
161         my $buf = $cb->($ctx) if $cb;
162         $buf //= delete($ctx->{cb}) ? _html_end($ctx) : undef;
163
164         # gzf may be GzipFilter, `undef' or `0'
165         my $gzf = $ctx->{gzf} or return $buf;
166
167         return $gzf->translate($buf) if defined $buf;
168         $ctx->{gzf} = 0; # next call to ->getline returns $buf (== undef)
169         $gzf->translate(undef);
170 }
171
172 sub html_oneshot ($$;$) {
173         my ($ctx, $code, $sref) = @_;
174         $ctx->{base_url} = base_url($ctx);
175         bless $ctx, __PACKAGE__;
176         my @x;
177         my $h = [ 'Content-Type' => 'text/html; charset=UTF-8',
178                 'Content-Length' => undef ];
179         if (my $gzf = gzf_maybe($h, $ctx->{env})) {
180                 $gzf->zmore(html_top($ctx));
181                 $gzf->zmore($$sref) if $sref;
182                 $x[0] = $gzf->zflush(_html_end($ctx));
183                 $h->[3] = length($x[0]);
184         } else {
185                 @x = (html_top($ctx), $sref ? $$sref : (), _html_end($ctx));
186                 $h->[3] += bytes::length($_) for @x;
187         }
188         [ $code, $h, \@x ]
189 }
190
191 1;