]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WwwStream.pm
www: move mirror instructions to /text/
[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         }
53         my $code = $ibx->{coderepo} ? qq( / <a\nhref=#code>code</a>) : '';
54         # id=mirror must exist for legacy bookmarks
55         my $links = qq(<a\nhref="${upfx}_/text/help/">help</a> / ).
56                         qq(<a\nhref="${upfx}_/text/color/">color</a> / ).
57                         qq(<a\nid=mirror) .
58                         qq(\nhref="${upfx}_/text/mirror/">mirror</a>$code / ).
59                         qq(<a\nhref="$atom">Atom feed</a>);
60         if ($ibx->isrch) {
61                 my $q_val = delete($ctx->{-q_value_html}) // '';
62                 $q_val = qq(\nvalue="$q_val") if $q_val ne '';
63                 # XXX gross, for SearchView.pm
64                 my $extra = delete($ctx->{-extra_form_html}) // '';
65                 my $action = $upfx eq '' ? './' : $upfx;
66                 $top = qq{<form\naction="$action"><pre>$top} .
67                           qq{\n<input\nname=q\ntype=text$q_val />} .
68                           $extra .
69                           qq{<input\ntype=submit\nvalue=search />} .
70                           ' ' . $links .
71                           q{</pre></form>}
72         } else {
73                 $top = '<pre>' . $top . "\n" . $links . '</pre>';
74         }
75         "<html><head><title>$title</title>" .
76                 qq(<link\nrel=alternate\ntitle="Atom feed"\n).
77                 qq(href="$atom"\ntype="application/atom+xml"/>) .
78                 $ctx->{www}->style($upfx) .
79                 '</head><body>'. $top . (delete($ctx->{-html_tip}) // '');
80 }
81
82 sub coderepos ($) {
83         my ($ctx) = @_;
84         my $cr = $ctx->{ibx}->{coderepo} // return ();
85         my $cfg = $ctx->{www}->{pi_cfg};
86         my $upfx = ($ctx->{-upfx} // ''). '../';
87         my @ret;
88         for my $cr_name (@$cr) {
89                 $ret[0] //= <<EOF;
90 <a id=code>code repositories for project(s) associated with this inbox:
91 EOF
92                 my $urls = $cfg->get_all("coderepo.$cr_name.cgiturl");
93                 if ($urls) {
94                         for (@$urls) {
95                                 # relative or absolute URL?, prefix relative
96                                 # "foo.git" with appropriate number of "../"
97                                 my $u = m!\A(?:[a-z\+]+:)?//! ? $_ : $upfx.$_;
98                                 $u = ascii_html(prurl($ctx->{env}, $u));
99                                 $ret[0] .= qq(\n\t<a\nhref="$u">$u</a>);
100                         }
101                 } else {
102                         $ret[0] .= qq[\n\t$cr_name.git (no URL configured)];
103                 }
104         }
105         @ret; # may be empty, this sub is called as an arg for join()
106 }
107
108 sub _html_end {
109         my ($ctx) = @_;
110         my @cr = coderepos($ctx);
111         scalar(@cr) ?
112                 '<hr><pre>'.join("\n\n", @cr).'</pre></body></html>' :
113                 '</body></html>';
114 }
115
116 # callback for HTTP.pm (and any other PSGI servers)
117 sub getline {
118         my ($ctx) = @_;
119         my $cb = $ctx->{cb} or return;
120         while (defined(my $x = $cb->($ctx))) { # x = smsg or scalar non-ref
121                 if (ref($x)) { # smsg
122                         my $eml = $ctx->{ibx}->smsg_eml($x) or next;
123                         $ctx->{smsg} = $x;
124                         return $ctx->translate($cb->($ctx, $eml));
125                 } else { # scalar
126                         return $ctx->translate($x);
127                 }
128         }
129         delete $ctx->{cb};
130         $ctx->zflush(_html_end($ctx));
131 }
132
133 sub html_oneshot ($$;$) {
134         my ($ctx, $code, $sref) = @_;
135         my $res_hdr = [ 'Content-Type' => 'text/html; charset=UTF-8',
136                 'Content-Length' => undef ];
137         bless $ctx, __PACKAGE__;
138         $ctx->{gz} = PublicInbox::GzipFilter::gz_or_noop($res_hdr, $ctx->{env});
139         $ctx->{base_url} //= do {
140                 $ctx->zmore(html_top($ctx));
141                 base_url($ctx);
142         };
143         $ctx->zmore($$sref) if $sref;
144         my $bdy = $ctx->zflush(_html_end($ctx));
145         $res_hdr->[3] = length($bdy);
146         [ $code, $res_hdr, [ $bdy ] ]
147 }
148
149 sub async_next ($) {
150         my ($http) = @_; # PublicInbox::HTTP
151         my $ctx = $http->{forward} or return;
152         eval {
153                 if (my $smsg = $ctx->{smsg} = $ctx->{cb}->($ctx)) {
154                         $ctx->smsg_blob($smsg);
155                 } else {
156                         $ctx->{http_out}->write(
157                                         $ctx->translate(_html_end($ctx)));
158                         $ctx->close; # GzipFilter->close
159                 }
160         };
161         warn "E: $@" if $@;
162 }
163
164 sub aresponse {
165         my ($ctx, $code, $cb) = @_;
166         my $res_hdr = [ 'Content-Type' => 'text/html; charset=UTF-8' ];
167         init($ctx, $cb);
168         $ctx->psgi_response($code, $res_hdr);
169 }
170
171 1;