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 $urls = 'Archives are clonable:';
76 my $ctx = $self->{ctx};
77 my $ibx = $ctx->{-inbox};
78 my $desc = ascii_html($ibx->description);
81 my $http = $ibx->base_url($ctx->{env});
82 chop $http; # no trailing slash for clone
83 my $part = $ibx->max_git_part;
84 my $dir = (split(m!/!, $http))[-1];
85 if (defined($part)) { # v2
87 for my $i (0..$part) {
88 # old parts my be deleted:
89 -d "$ibx->{mainrepo}/git/$i.git" or next;
92 push @urls, "$url $dir/git/$i.git";
99 # FIXME: partitioning in can be different in other repositories,
100 # use the "cloneurl" file as-is for now:
101 foreach my $u (@{$ibx->cloneurl}) {
104 push @urls, $u =~ /\Ahttps?:/ ? qq(<a\nhref="$u">$u</a>) : $u;
107 if (defined($part) || scalar(@urls) > 1) {
109 join("\n", map { "\tgit clone --mirror $_" } @urls);
111 $urls .= " git clone --mirror $urls[0]";
114 my $addrs = $ibx->{address};
115 $addrs = join(' ', @$addrs) if ref($addrs) eq 'ARRAY';
119 # If you have public-inbox 1.1+ installed, you may
120 # initialize and index your mirror using the following commands:
121 public-inbox-init -V2 $ibx->{name} $dir/ $http \\
123 public-inbox-index $dir
126 my @nntp = map { qq(<a\nhref="$_">$_</a>) } @{$ibx->nntp_url};
129 $urls .= @nntp == 1 ? 'Newsgroup' : 'Newsgroups are';
130 $urls .= ' available over NNTP:';
131 $urls .= "\n\t" . join("\n\t", @nntp) . "\n";
133 if ($urls =~ m!\b[^:]+://\w+\.onion/!) {
134 $urls .= "\n note: .onion URLs require Tor: ";
135 $urls .= qq[<a\nhref="$TOR_URL">$TOR_URL</a>];
137 my $url = PublicInbox::Hval::prurl($ctx->{env}, $CODE_URL);
138 '<hr><pre>'.join("\n\n",
141 'AGPL code for this site: '.
142 qq(git clone <a\nhref="$url">$url</a> $PROJECT)
143 ).'</pre></body></html>';
146 # callback for HTTP.pm (and any other PSGI servers)
149 my $nr = $self->{nr}++;
151 return _html_top($self) if $nr == 0;
153 if (my $middle = $self->{cb}) {
154 $middle = $middle->($nr, $self->{ctx}) and return $middle;
157 delete $self->{cb} ? _html_end($self) : undef;