]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WwwStream.pm
wwwstream: drop tor2web URL
[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 _html_end {
74         my ($self) = @_;
75         my $urls = 'Archives are clonable:';
76         my $ctx = $self->{ctx};
77         my $ibx = $ctx->{-inbox};
78         my $desc = ascii_html($ibx->description);
79
80         my (%seen, @urls);
81         my $http = $ibx->base_url($ctx->{env});
82         chop $http; # no trailing slash for clone
83         my $part = $ibx->max_git_part;
84         my $dir = (split(m!/!, $http))[-1];
85         if (defined($part)) { # v2
86                 $seen{$http} = 1;
87                 for my $i (0..$part) {
88                         # old parts my be deleted:
89                         -d "$ibx->{mainrepo}/git/$i.git" or next;
90                         my $url = "$http/$i";
91                         $seen{$url} = 1;
92                         push @urls, "$url $dir/git/$i.git";
93                 }
94         } else { # v1
95                 $seen{$http} = 1;
96                 push @urls, $http;
97         }
98
99         # FIXME: partitioning in can be different in other repositories,
100         # use the "cloneurl" file as-is for now:
101         foreach my $u (@{$ibx->cloneurl}) {
102                 next if $seen{$u};
103                 $seen{$u} = 1;
104                 push @urls, $u =~ /\Ahttps?:/ ? qq(<a\nhref="$u">$u</a>) : $u;
105         }
106
107         if (defined($part) || scalar(@urls) > 1) {
108                 $urls .= "\n" .
109                         join("\n", map { "\tgit clone --mirror $_" } @urls);
110         } else {
111                 $urls .= " git clone --mirror $urls[0]";
112         }
113         if (defined $part) {
114                 my $addrs = $ibx->{address};
115                 $addrs = join(' ', @$addrs) if ref($addrs) eq 'ARRAY';
116                 $urls .=  <<EOF
117
118
119         # If you have public-inbox 1.1+ installed, you may
120         # initialize and index your mirror using the following commands:
121         public-inbox-init -V2 $ibx->{name} $dir/ $http \\
122                 $addrs
123         public-inbox-index $dir
124 EOF
125         }
126         my @nntp = map { qq(<a\nhref="$_">$_</a>) } @{$ibx->nntp_url};
127         if (@nntp) {
128                 $urls .= "\n\n";
129                 $urls .= @nntp == 1 ? 'Newsgroup' : 'Newsgroups are';
130                 $urls .= ' available over NNTP:';
131                 $urls .= "\n\t" . join("\n\t", @nntp) . "\n";
132         }
133         if ($urls =~ m!\b[^:]+://\w+\.onion/!) {
134                 $urls .= "\n note: .onion URLs require Tor: ";
135                 $urls .= qq[<a\nhref="$TOR_URL">$TOR_URL</a>];
136         }
137         my $url = PublicInbox::Hval::prurl($ctx->{env}, $CODE_URL);
138         '<hr><pre>'.join("\n\n",
139                 $desc,
140                 $urls,
141                 'AGPL code for this site: '.
142                 qq(git clone <a\nhref="$url">$url</a> $PROJECT)
143         ).'</pre></body></html>';
144 }
145
146 # callback for HTTP.pm (and any other PSGI servers)
147 sub getline {
148         my ($self) = @_;
149         my $nr = $self->{nr}++;
150
151         return _html_top($self) if $nr == 0;
152
153         if (my $middle = $self->{cb}) {
154                 $middle = $middle->($nr, $self->{ctx}) and return $middle;
155         }
156
157         delete $self->{cb} ? _html_end($self) : undef;
158 }
159
160 1;