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