]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WwwStream.pm
www: support cloning individual v2 git partitions
[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 package PublicInbox::WwwStream;
6 use strict;
7 use warnings;
8 use PublicInbox::Hval qw(ascii_html);
9 use URI;
10 our $TOR_URL = 'https://www.torproject.org/';
11 our $TOR2WEB_URL = 'https://www.tor2web.org/';
12 our $CODE_URL = 'https://public-inbox.org/';
13 our $PROJECT = 'public-inbox';
14
15 # noop for HTTP.pm (and any other PSGI servers)
16 sub close {}
17
18 sub new {
19         my ($class, $ctx, $cb) = @_;
20         bless { nr => 0, cb => $cb || *close, ctx => $ctx }, $class;
21 }
22
23 sub response {
24         my ($class, $ctx, $code, $cb) = @_;
25         [ $code, [ 'Content-Type', 'text/html; charset=UTF-8' ],
26           $class->new($ctx, $cb) ]
27 }
28
29 sub _html_top ($) {
30         my ($self) = @_;
31         my $ctx = $self->{ctx};
32         my $obj = $ctx->{-inbox};
33         my $desc = ascii_html($obj->description);
34         my $title = $ctx->{-title_html} || $desc;
35         my $upfx = $ctx->{-upfx} || '';
36         my $help = $upfx.'_/text/help';
37         my $atom = $ctx->{-atom} || $upfx.'new.atom';
38         my $tip = $ctx->{-html_tip} || '';
39         my $top = "<b>$desc</b>";
40         my $links = "<a\nhref=\"$help\">help</a> / ".
41                         "<a\nhref=\"$atom\">Atom feed</a>";
42         if ($obj->search) {
43                 my $q_val = $ctx->{-q_value_html};
44                 if (defined $q_val && $q_val ne '') {
45                         $q_val = qq(\nvalue="$q_val");
46                 } else {
47                         $q_val = '';
48                 }
49                 # XXX gross, for SearchView.pm
50                 my $extra = $ctx->{-extra_form_html} || '';
51                 my $action = $upfx eq '' ? './' : $upfx;
52                 $top = qq{<form\naction="$action"><pre>$top} .
53                           qq{\n<input\nname=q\ntype=text$q_val />} .
54                           $extra .
55                           qq{<input\ntype=submit\nvalue=search />} .
56                           ' ' . $links .
57                           q{</pre></form>}
58         } else {
59                 $top = '<pre>' . $top . "\n" . $links . '</pre>';
60         }
61         "<html><head><title>$title</title>" .
62                 "<link\nrel=alternate\ntitle=\"Atom feed\"\n".
63                 "href=\"$atom\"\ntype=\"application/atom+xml\"/>" .
64                 PublicInbox::Hval::STYLE .
65                 "</head><body>". $top . $tip;
66 }
67
68 sub _html_end {
69         my ($self) = @_;
70         my $urls = 'Archives are clonable:';
71         my $ctx = $self->{ctx};
72         my $obj = $ctx->{-inbox};
73         my $desc = ascii_html($obj->description);
74
75         my (%seen, @urls);
76         my $http = $obj->base_url($ctx->{env});
77         chop $http; # no trailing slash
78         my $part = $obj->max_git_part;
79         if (defined($part)) { # v2
80                 # most recent partition first:
81                 for (; $part >= 0; $part--) {
82                         my $url = "$http/$part";
83                         $seen{$url} = 1;
84                         push @urls, $url;
85                 }
86         } else { # v1
87                 $seen{$http} = 1;
88                 push @urls, $http;
89         }
90
91         # FIXME: partitioning in can be different in other repositories,
92         # use the "cloneurl" file as-is for now:
93         foreach my $u (@{$obj->cloneurl}) {
94                 next if $seen{$u};
95                 $seen{$u} = 1;
96                 push @urls, $u =~ /\Ahttps?:/ ? qq(<a\nhref="$u">$u</a>) : $u;
97         }
98
99         if (scalar(@urls) == 1) {
100                 $urls .= " git clone --mirror $urls[0]";
101         } else {
102                 $urls .= "\n" .
103                         join("\n", map { "\tgit clone --mirror $_" } @urls);
104         }
105
106         my @nntp = map { qq(<a\nhref="$_">$_</a>) } @{$obj->nntp_url};
107         if (@nntp) {
108                 $urls .= "\n\n";
109                 $urls .= @nntp == 1 ? 'Newsgroup' : 'Newsgroups are';
110                 $urls .= ' available over NNTP:';
111                 $urls .= "\n\t" . join("\n\t", @nntp) . "\n";
112         }
113         if ($urls =~ m!\b[^:]+://\w+\.onion/!) {
114                 $urls .= "\n note: .onion URLs require Tor: ";
115                 $urls .= qq[<a\nhref="$TOR_URL">$TOR_URL</a>];
116                 if ($TOR2WEB_URL) {
117                         $urls .= "\n       or Tor2web: ";
118                         $urls .= qq[<a\nhref="$TOR2WEB_URL">$TOR2WEB_URL</a>];
119                 }
120         }
121         my $url = PublicInbox::Hval::prurl($ctx->{env}, $CODE_URL);
122         '<hr><pre>'.join("\n\n",
123                 $desc,
124                 $urls,
125                 'AGPL code for this site: '.
126                 qq(git clone <a\nhref="$url">$url</a> $PROJECT)
127         ).'</pre></body></html>';
128 }
129
130 # callback for HTTP.pm (and any other PSGI servers)
131 sub getline {
132         my ($self) = @_;
133         my $nr = $self->{nr}++;
134
135         return _html_top($self) if $nr == 0;
136
137         if (my $middle = $self->{cb}) {
138                 $middle = $middle->($nr, $self->{ctx}) and return $middle;
139         }
140
141         delete $self->{cb} ? _html_end($self) : undef;
142 }
143
144 1;