]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WwwStream.pm
Merge commit 'mem'
[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 $TOR2WEB_URL = 'https://www.tor2web.org/';
16 our $CODE_URL = 'https://public-inbox.org/';
17 our $PROJECT = 'public-inbox';
18
19 # noop for HTTP.pm (and any other PSGI servers)
20 sub close {}
21
22 sub new {
23         my ($class, $ctx, $cb) = @_;
24         bless { nr => 0, cb => $cb || *close, ctx => $ctx }, $class;
25 }
26
27 sub response {
28         my ($class, $ctx, $code, $cb) = @_;
29         [ $code, [ 'Content-Type', 'text/html; charset=UTF-8' ],
30           $class->new($ctx, $cb) ]
31 }
32
33 sub _html_top ($) {
34         my ($self) = @_;
35         my $ctx = $self->{ctx};
36         my $obj = $ctx->{-inbox};
37         my $desc = ascii_html($obj->description);
38         my $title = $ctx->{-title_html} || $desc;
39         my $upfx = $ctx->{-upfx} || '';
40         my $help = $upfx.'_/text/help';
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=\"$atom\">Atom feed</a>";
46         if ($obj->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                 PublicInbox::Hval::STYLE .
69                 "</head><body>". $top . $tip;
70 }
71
72 sub _html_end {
73         my ($self) = @_;
74         my $urls = 'Archives are clonable:';
75         my $ctx = $self->{ctx};
76         my $obj = $ctx->{-inbox};
77         my $desc = ascii_html($obj->description);
78
79         my (%seen, @urls);
80         my $http = $obj->base_url($ctx->{env});
81         chop $http; # no trailing slash for clone
82         my $part = $obj->max_git_part;
83         my $dir = (split(m!/!, $http))[-1];
84         if (defined($part)) { # v2
85                 $seen{$http} = 1;
86                 for my $i (0..$part) {
87                         # old parts my be deleted:
88                         -d "$obj->{mainrepo}/git/$i.git" or next;
89                         my $url = "$http/$i";
90                         $seen{$url} = 1;
91                         push @urls, "$url $dir/git/$i.git";
92                 }
93         } else { # v1
94                 $seen{$http} = 1;
95                 push @urls, $http;
96         }
97
98         # FIXME: partitioning in can be different in other repositories,
99         # use the "cloneurl" file as-is for now:
100         foreach my $u (@{$obj->cloneurl}) {
101                 next if $seen{$u};
102                 $seen{$u} = 1;
103                 push @urls, $u =~ /\Ahttps?:/ ? qq(<a\nhref="$u">$u</a>) : $u;
104         }
105
106         if (defined($part) || scalar(@urls) > 1) {
107                 $urls .= "\n" .
108                         join("\n", map { "\tgit clone --mirror $_" } @urls);
109         } else {
110                 $urls .= " git clone --mirror $urls[0]";
111         }
112         if (defined $part) {
113                 my $addrs = $obj->{address};
114                 $addrs = join(' ', @$addrs) if ref($addrs) eq 'ARRAY';
115                 $urls .=  <<EOF
116
117
118         # If you have public-inbox 1.1+ installed, you may
119         # initialize and index your mirror using the following commands:
120         public-inbox-init -V2 $obj->{name} $dir/ $http \\
121                 $addrs
122         public-inbox-index $dir
123 EOF
124         }
125         my @nntp = map { qq(<a\nhref="$_">$_</a>) } @{$obj->nntp_url};
126         if (@nntp) {
127                 $urls .= "\n\n";
128                 $urls .= @nntp == 1 ? 'Newsgroup' : 'Newsgroups are';
129                 $urls .= ' available over NNTP:';
130                 $urls .= "\n\t" . join("\n\t", @nntp) . "\n";
131         }
132         if ($urls =~ m!\b[^:]+://\w+\.onion/!) {
133                 $urls .= "\n note: .onion URLs require Tor: ";
134                 $urls .= qq[<a\nhref="$TOR_URL">$TOR_URL</a>];
135                 if ($TOR2WEB_URL) {
136                         $urls .= "\n       or Tor2web: ";
137                         $urls .= qq[<a\nhref="$TOR2WEB_URL">$TOR2WEB_URL</a>];
138                 }
139         }
140         my $url = PublicInbox::Hval::prurl($ctx->{env}, $CODE_URL);
141         '<hr><pre>'.join("\n\n",
142                 $desc,
143                 $urls,
144                 'AGPL code for this site: '.
145                 qq(git clone <a\nhref="$url">$url</a> $PROJECT)
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;