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>
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 PublicInbox::Hval qw(ascii_html);
14 our $TOR_URL = 'https://www.torproject.org/';
15 our $CODE_URL = 'https://public-inbox.org/';
16 our $PROJECT = 'public-inbox';
18 # noop for HTTP.pm (and any other PSGI servers)
22 my ($class, $ctx, $cb) = @_;
23 bless { nr => 0, cb => $cb || *close, ctx => $ctx }, $class;
27 my ($class, $ctx, $code, $cb) = @_;
28 [ $code, [ 'Content-Type', 'text/html; charset=UTF-8' ],
29 $class->new($ctx, $cb) ]
34 my $ctx = $self->{ctx};
35 my $ibx = $ctx->{-inbox};
36 my $desc = ascii_html($ibx->description);
37 my $title = $ctx->{-title_html} || $desc;
38 my $upfx = $ctx->{-upfx} || '';
39 my $help = $upfx.'_/text/help';
40 my $color = $upfx.'_/text/color';
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=\"$color\">color</a> / ".
46 "<a\nhref=\"$atom\">Atom feed</a>";
48 my $q_val = $ctx->{-q_value_html};
49 if (defined $q_val && $q_val ne '') {
50 $q_val = qq(\nvalue="$q_val");
54 # XXX gross, for SearchView.pm
55 my $extra = $ctx->{-extra_form_html} || '';
56 my $action = $upfx eq '' ? './' : $upfx;
57 $top = qq{<form\naction="$action"><pre>$top} .
58 qq{\n<input\nname=q\ntype=text$q_val />} .
60 qq{<input\ntype=submit\nvalue=search />} .
64 $top = '<pre>' . $top . "\n" . $links . '</pre>';
66 "<html><head><title>$title</title>" .
67 "<link\nrel=alternate\ntitle=\"Atom feed\"\n".
68 "href=\"$atom\"\ntype=\"application/atom+xml\"/>" .
69 $ctx->{www}->style($upfx) .
70 "</head><body>". $top . $tip;
75 my $u = PublicInbox::Hval::prurl($env, $CODE_URL);
76 qq(AGPL code for this site: git clone <a\nhref="$u">$u</a> $PROJECT)
81 my $urls = 'Archives are clonable:';
82 my $ctx = $self->{ctx};
83 my $ibx = $ctx->{-inbox};
84 my $desc = ascii_html($ibx->description);
87 my $http = $ibx->base_url($ctx->{env});
88 chop $http; # no trailing slash for clone
89 my $part = $ibx->max_git_part;
90 my $dir = (split(m!/!, $http))[-1];
91 if (defined($part)) { # v2
93 for my $i (0..$part) {
94 # old parts my be deleted:
95 -d "$ibx->{mainrepo}/git/$i.git" or next;
98 push @urls, "$url $dir/git/$i.git";
105 # FIXME: partitioning in can be different in other repositories,
106 # use the "cloneurl" file as-is for now:
107 foreach my $u (@{$ibx->cloneurl}) {
110 push @urls, $u =~ /\Ahttps?:/ ? qq(<a\nhref="$u">$u</a>) : $u;
113 if (defined($part) || scalar(@urls) > 1) {
115 join("\n", map { "\tgit clone --mirror $_" } @urls);
117 $urls .= " git clone --mirror $urls[0]";
120 my $addrs = $ibx->{address};
121 $addrs = join(' ', @$addrs) if ref($addrs) eq 'ARRAY';
125 # If you have public-inbox 1.1+ installed, you may
126 # initialize and index your mirror using the following commands:
127 public-inbox-init -V2 $ibx->{name} $dir/ $http \\
129 public-inbox-index $dir
132 my @nntp = map { qq(<a\nhref="$_">$_</a>) } @{$ibx->nntp_url};
135 $urls .= @nntp == 1 ? 'Newsgroup' : 'Newsgroups are';
136 $urls .= ' available over NNTP:';
137 $urls .= "\n\t" . join("\n\t", @nntp) . "\n";
139 if ($urls =~ m!\b[^:]+://\w+\.onion/!) {
140 $urls .= "\n note: .onion URLs require Tor: ";
141 $urls .= qq[<a\nhref="$TOR_URL">$TOR_URL</a>];
143 '<hr><pre>'.join("\n\n",
146 code_footer($ctx->{env})
147 ).'</pre></body></html>';
150 # callback for HTTP.pm (and any other PSGI servers)
153 my $nr = $self->{nr}++;
155 return _html_top($self) if $nr == 0;
157 if (my $middle = $self->{cb}) {
158 $middle = $middle->($nr, $self->{ctx}) and return $middle;
161 delete $self->{cb} ? _html_end($self) : undef;