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