]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WwwStream.pm
a88ff9721e941898932d02a9b7f560acdc7fc018
[public-inbox.git] / lib / PublicInbox / WwwStream.pm
1 # Copyright (C) 2016-2021 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 v5.10.1;
11 use parent qw(Exporter PublicInbox::GzipFilter);
12 our @EXPORT_OK = qw(html_oneshot);
13 use PublicInbox::Hval qw(ascii_html prurl ts2str);
14
15 our $CODE_URL = [ qw(
16 http://7fh6tueqddpjyxjmgtdiueylzoqt6pt7hec3pukyptlmohoowvhde4yd.onion/public-inbox.git
17 https://public-inbox.org/public-inbox.git) ];
18
19 sub base_url ($) {
20         my $ctx = shift;
21         my $base_url = $ctx->{ibx}->base_url($ctx->{env});
22         chop $base_url; # no trailing slash for clone
23         $base_url;
24 }
25
26 sub init {
27         my ($ctx, $cb) = @_;
28         $ctx->{cb} = $cb;
29         $ctx->{base_url} = base_url($ctx);
30         bless $ctx, __PACKAGE__;
31 }
32
33 sub async_eml { # for async_blob_cb
34         my ($ctx, $eml) = @_;
35         $ctx->{http_out}->write($ctx->translate($ctx->{cb}->($ctx, $eml)));
36 }
37
38 sub html_top ($) {
39         my ($ctx) = @_;
40         my $ibx = $ctx->{ibx};
41         my $desc = ascii_html($ibx->description);
42         my $title = delete($ctx->{-title_html}) // $desc;
43         my $upfx = $ctx->{-upfx} || '';
44         my $atom = $ctx->{-atom} || $upfx.'new.atom';
45         my $top = "<b>$desc</b>";
46         if (my $t_max = $ctx->{-t_max}) {
47                 $t_max = ts2str($t_max);
48                 $top = qq(<a\nhref="$upfx?t=$t_max">$top</a>);
49         # we had some kind of query, link to /$INBOX/?t=YYYYMMDDhhmmss
50         } elsif ($ctx->{qp}->{t}) {
51                 $top = qq(<a\nhref="./">$top</a>);
52         } elsif (length($upfx)) {
53                 $top = qq(<a\nhref="$upfx">$top</a>);
54         }
55         my $code = $ibx->{coderepo} ? qq( / <a\nhref=#code>code</a>) : '';
56         # id=mirror must exist for legacy bookmarks
57         my $links = qq(<a\nhref="${upfx}_/text/help/">help</a> / ).
58                         qq(<a\nhref="${upfx}_/text/color/">color</a> / ).
59                         qq(<a\nid=mirror) .
60                         qq(\nhref="${upfx}_/text/mirror/">mirror</a>$code / ).
61                         qq(<a\nhref="$atom">Atom feed</a>);
62         if ($ibx->isrch) {
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 coderepos ($) {
85         my ($ctx) = @_;
86         my $cr = $ctx->{ibx}->{coderepo} // return ();
87         my $cfg = $ctx->{www}->{pi_cfg};
88         my $upfx = ($ctx->{-upfx} // ''). '../';
89         my @ret;
90         for my $cr_name (@$cr) {
91                 $ret[0] //= <<EOF;
92 <a id=code>Code repositories for project(s) associated with this inbox:
93 EOF
94                 my $urls = $cfg->get_all("coderepo.$cr_name.cgiturl");
95                 if ($urls) {
96                         for (@$urls) {
97                                 # relative or absolute URL?, prefix relative
98                                 # "foo.git" with appropriate number of "../"
99                                 my $u = m!\A(?:[a-z\+]+:)?//! ? $_ : $upfx.$_;
100                                 $u = ascii_html(prurl($ctx->{env}, $u));
101                                 $ret[0] .= qq(\n\t<a\nhref="$u">$u</a>);
102                         }
103                 } else {
104                         $ret[0] .= qq[\n\t$cr_name.git (no URL configured)];
105                 }
106         }
107         @ret; # may be empty, this sub is called as an arg for join()
108 }
109
110 sub _html_end {
111         my ($ctx) = @_;
112         my $upfx = $ctx->{-upfx} || '';
113         my $m = "${upfx}_/text/mirror/";
114         my $x;
115         if ($ctx->{ibx}->can('cloneurl')) {
116                 $x = <<EOF;
117 This is a public inbox, see <a
118 href="$m">mirroring instructions</a>
119 on how to clone and mirror all data and code used for this inbox
120 EOF
121         } else {
122                 $x = <<EOF;
123 This is an external index of several public inboxes,
124 see <a href="$m">mirroring instructions</a> on how to clone and mirror
125 all data and code used by this external index.
126 EOF
127         }
128         chomp $x;
129         '<hr><pre>'.join("\n\n", coderepos($ctx), $x).'</pre></body></html>'
130 }
131
132 # callback for HTTP.pm (and any other PSGI servers)
133 sub getline {
134         my ($ctx) = @_;
135         my $cb = $ctx->{cb} or return;
136         while (defined(my $x = $cb->($ctx))) { # x = smsg or scalar non-ref
137                 if (ref($x)) { # smsg
138                         my $eml = $ctx->{ibx}->smsg_eml($x) or next;
139                         $ctx->{smsg} = $x;
140                         return $ctx->translate($cb->($ctx, $eml));
141                 } else { # scalar
142                         return $ctx->translate($x);
143                 }
144         }
145         delete $ctx->{cb};
146         $ctx->zflush(_html_end($ctx));
147 }
148
149 sub html_oneshot ($$;$) {
150         my ($ctx, $code, $sref) = @_;
151         my $res_hdr = [ 'Content-Type' => 'text/html; charset=UTF-8',
152                 'Content-Length' => undef ];
153         bless $ctx, __PACKAGE__;
154         $ctx->{gz} = PublicInbox::GzipFilter::gz_or_noop($res_hdr, $ctx->{env});
155         $ctx->{base_url} //= do {
156                 $ctx->zmore(html_top($ctx));
157                 base_url($ctx);
158         };
159         $ctx->zmore($$sref) if $sref;
160         my $bdy = $ctx->zflush(_html_end($ctx));
161         $res_hdr->[3] = length($bdy);
162         [ $code, $res_hdr, [ $bdy ] ]
163 }
164
165 sub async_next ($) {
166         my ($http) = @_; # PublicInbox::HTTP
167         my $ctx = $http->{forward} or return;
168         eval {
169                 if (my $smsg = $ctx->{smsg} = $ctx->{cb}->($ctx)) {
170                         $ctx->smsg_blob($smsg);
171                 } else {
172                         $ctx->{http_out}->write(
173                                         $ctx->translate(_html_end($ctx)));
174                         $ctx->close; # GzipFilter->close
175                 }
176         };
177         warn "E: $@" if $@;
178 }
179
180 sub aresponse {
181         my ($ctx, $code, $cb) = @_;
182         my $res_hdr = [ 'Content-Type' => 'text/html; charset=UTF-8' ];
183         init($ctx, $cb);
184         $ctx->psgi_response($code, $res_hdr);
185 }
186
187 1;