1 # Copyright (C) 2016-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # HTML body stream for which yields getline+close methods
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;
12 use base qw(Exporter);
13 our @EXPORT_OK = qw(html_oneshot);
14 use bytes (); # length
15 use PublicInbox::Hval qw(ascii_html prurl);
16 our $TOR_URL = 'https://www.torproject.org/';
17 our $CODE_URL = 'https://public-inbox.org/public-inbox.git';
19 # noop for HTTP.pm (and any other PSGI servers)
24 my $base_url = $ctx->{-inbox}->base_url($ctx->{env});
25 chop $base_url; # no trailing slash for clone
30 my ($class, $ctx, $cb) = @_;
36 base_url => base_url($ctx),
41 my ($class, $ctx, $code, $cb) = @_;
42 [ $code, [ 'Content-Type', 'text/html; charset=UTF-8' ],
43 $class->new($ctx, $cb) ]
48 my $ctx = $self->{ctx};
49 my $ibx = $ctx->{-inbox};
50 my $desc = ascii_html($ibx->description);
51 my $title = delete($ctx->{-title_html}) // $desc;
52 my $upfx = $ctx->{-upfx} || '';
53 my $help = $upfx.'_/text/help';
54 my $color = $upfx.'_/text/color';
55 my $atom = $ctx->{-atom} || $upfx.'new.atom';
56 my $top = "<b>$desc</b>";
57 my $links = "<a\nhref=\"$help\">help</a> / ".
58 "<a\nhref=\"$color\">color</a> / ".
59 "<a\nhref=\"$atom\">Atom feed</a>";
61 my $q_val = delete($ctx->{-q_value_html}) // '';
62 $q_val = qq(\nvalue="$q_val") if $q_val ne '';
63 # XXX gross, for SearchView.pm
64 my $extra = delete($ctx->{-extra_form_html}) // '';
65 my $action = $upfx eq '' ? './' : $upfx;
66 $top = qq{<form\naction="$action"><pre>$top} .
67 qq{\n<input\nname=q\ntype=text$q_val />} .
69 qq{<input\ntype=submit\nvalue=search />} .
73 $top = '<pre>' . $top . "\n" . $links . '</pre>';
75 "<html><head><title>$title</title>" .
76 qq(<link\nrel=alternate\ntitle="Atom feed"\n).
77 qq(href="$atom"\ntype="application/atom+xml"/>) .
78 $ctx->{www}->style($upfx) .
79 '</head><body>'. $top . (delete($ctx->{-html_tip}) // '');
84 my $u = prurl($env, $CODE_URL);
85 qq(AGPL code for this site: git clone <a\nhref="$u">$u</a>)
90 my $urls = 'Archives are clonable:';
91 my $ctx = $self->{ctx};
92 my $ibx = $ctx->{-inbox};
93 my $desc = ascii_html($ibx->description);
96 my $http = $self->{base_url};
97 my $max = $ibx->max_git_epoch;
98 my $dir = (split(m!/!, $http))[-1];
99 my %seen = ($http => 1);
100 if (defined($max)) { # v2
101 for my $i (0..$max) {
102 # old parts my be deleted:
103 -d "$ibx->{inboxdir}/git/$i.git" or next;
104 my $url = "$http/$i";
106 push @urls, "$url $dir/git/$i.git";
112 # FIXME: epoch splits can be different in other repositories,
113 # use the "cloneurl" file as-is for now:
114 foreach my $u (@{$ibx->cloneurl}) {
116 push @urls, $u =~ /\Ahttps?:/ ? qq(<a\nhref="$u">$u</a>) : $u;
119 if (defined($max) || scalar(@urls) > 1) {
121 join("\n", map { "\tgit clone --mirror $_" } @urls);
123 $urls .= " git clone --mirror $urls[0]";
126 my $addrs = $ibx->{address};
127 $addrs = join(' ', @$addrs) if ref($addrs) eq 'ARRAY';
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 \\
135 public-inbox-index $dir
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};
146 $urls .= @nntp == 1 ? 'Newsgroup' : 'Newsgroups are';
147 $urls .= ' available over NNTP:';
148 $urls .= "\n\t" . join("\n\t", @nntp) . "\n";
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>];
154 '<hr><pre>'.join("\n\n",
157 code_footer($ctx->{env})
158 ).'</pre></body></html>';
161 # callback for HTTP.pm (and any other PSGI servers)
164 my $nr = $self->{nr}++;
166 return _html_top($self) if $nr == 0;
168 if (my $middle = $self->{cb}) {
169 $middle = $middle->($nr, $self->{ctx}) and return $middle;
172 delete $self->{cb} ? _html_end($self) : undef;
175 sub html_oneshot ($$;$) {
176 my ($ctx, $code, $sref) = @_;
179 base_url => base_url($ctx),
181 my @x = (_html_top($self), $sref ? $$sref : (), _html_end($self));
183 $len += bytes::length($_) for @x;
185 'Content-Type' => 'text/html; charset=UTF-8',
186 'Content-Length' => $len