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