]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WwwStream.pm
Merge remote-tracking branch 'origin/newspeak' into xcpdb
[public-inbox.git] / lib / PublicInbox / WwwStream.pm
1 # Copyright (C) 2016-2018 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);
13 our $TOR_URL = 'https://www.torproject.org/';
14 our $CODE_URL = 'https://public-inbox.org/';
15 our $PROJECT = 'public-inbox';
16
17 # noop for HTTP.pm (and any other PSGI servers)
18 sub close {}
19
20 sub new {
21         my ($class, $ctx, $cb) = @_;
22         bless { nr => 0, cb => $cb || *close, ctx => $ctx }, $class;
23 }
24
25 sub response {
26         my ($class, $ctx, $code, $cb) = @_;
27         [ $code, [ 'Content-Type', 'text/html; charset=UTF-8' ],
28           $class->new($ctx, $cb) ]
29 }
30
31 sub _html_top ($) {
32         my ($self) = @_;
33         my $ctx = $self->{ctx};
34         my $ibx = $ctx->{-inbox};
35         my $desc = ascii_html($ibx->description);
36         my $title = $ctx->{-title_html} || $desc;
37         my $upfx = $ctx->{-upfx} || '';
38         my $help = $upfx.'_/text/help';
39         my $color = $upfx.'_/text/color';
40         my $atom = $ctx->{-atom} || $upfx.'new.atom';
41         my $tip = $ctx->{-html_tip} || '';
42         my $top = "<b>$desc</b>";
43         my $links = "<a\nhref=\"$help\">help</a> / ".
44                         "<a\nhref=\"$color\">color</a> / ".
45                         "<a\nhref=\"$atom\">Atom feed</a>";
46         if ($ibx->search) {
47                 my $q_val = $ctx->{-q_value_html};
48                 if (defined $q_val && $q_val ne '') {
49                         $q_val = qq(\nvalue="$q_val");
50                 } else {
51                         $q_val = '';
52                 }
53                 # XXX gross, for SearchView.pm
54                 my $extra = $ctx->{-extra_form_html} || '';
55                 my $action = $upfx eq '' ? './' : $upfx;
56                 $top = qq{<form\naction="$action"><pre>$top} .
57                           qq{\n<input\nname=q\ntype=text$q_val />} .
58                           $extra .
59                           qq{<input\ntype=submit\nvalue=search />} .
60                           ' ' . $links .
61                           q{</pre></form>}
62         } else {
63                 $top = '<pre>' . $top . "\n" . $links . '</pre>';
64         }
65         "<html><head><title>$title</title>" .
66                 "<link\nrel=alternate\ntitle=\"Atom feed\"\n".
67                 "href=\"$atom\"\ntype=\"application/atom+xml\"/>" .
68                 $ctx->{www}->style($upfx) .
69                 "</head><body>". $top . $tip;
70 }
71
72 sub code_footer ($) {
73         my ($env) = @_;
74         my $u = PublicInbox::Hval::prurl($env, $CODE_URL);
75         qq(AGPL code for this site: git clone <a\nhref="$u">$u</a> $PROJECT)
76 }
77
78 sub _html_end {
79         my ($self) = @_;
80         my $urls = 'Archives are clonable:';
81         my $ctx = $self->{ctx};
82         my $ibx = $ctx->{-inbox};
83         my $desc = ascii_html($ibx->description);
84
85         my (%seen, @urls);
86         my $http = $ibx->base_url($ctx->{env});
87         chop $http; # no trailing slash for clone
88         my $max = $ibx->max_git_epoch;
89         my $dir = (split(m!/!, $http))[-1];
90         if (defined($max)) { # v2
91                 $seen{$http} = 1;
92                 for my $i (0..$max) {
93                         # old parts my be deleted:
94                         -d "$ibx->{mainrepo}/git/$i.git" or next;
95                         my $url = "$http/$i";
96                         $seen{$url} = 1;
97                         push @urls, "$url $dir/git/$i.git";
98                 }
99         } else { # v1
100                 $seen{$http} = 1;
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                 $seen{$u} = 1;
109                 push @urls, $u =~ /\Ahttps?:/ ? qq(<a\nhref="$u">$u</a>) : $u;
110         }
111
112         if (defined($max) || scalar(@urls) > 1) {
113                 $urls .= "\n" .
114                         join("\n", map { "\tgit clone --mirror $_" } @urls);
115         } else {
116                 $urls .= " git clone --mirror $urls[0]";
117         }
118         if (defined $max) {
119                 my $addrs = $ibx->{address};
120                 $addrs = join(' ', @$addrs) if ref($addrs) eq 'ARRAY';
121                 $urls .=  <<EOF
122
123
124         # If you have public-inbox 1.1+ installed, you may
125         # initialize and index your mirror using the following commands:
126         public-inbox-init -V2 $ibx->{name} $dir/ $http \\
127                 $addrs
128         public-inbox-index $dir
129 EOF
130         }
131         my @nntp = map { qq(<a\nhref="$_">$_</a>) } @{$ibx->nntp_url};
132         if (@nntp) {
133                 $urls .= "\n\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 ($self) = @_;
152         my $nr = $self->{nr}++;
153
154         return _html_top($self) if $nr == 0;
155
156         if (my $middle = $self->{cb}) {
157                 $middle = $middle->($nr, $self->{ctx}) and return $middle;
158         }
159
160         delete $self->{cb} ? _html_end($self) : undef;
161 }
162
163 1;