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