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