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>
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);
13 our $TOR_URL = 'https://www.torproject.org/';
14 our $CODE_URL = 'https://public-inbox.org/';
15 our $PROJECT = 'public-inbox';
17 # noop for HTTP.pm (and any other PSGI servers)
21 my ($class, $ctx, $cb) = @_;
22 bless { nr => 0, cb => $cb || *close, ctx => $ctx }, $class;
26 my ($class, $ctx, $code, $cb) = @_;
27 [ $code, [ 'Content-Type', 'text/html; charset=UTF-8' ],
28 $class->new($ctx, $cb) ]
33 my $ctx = $self->{ctx};
34 my $ibx = $ctx->{-inbox};
35 my $desc = ascii_html($ibx->description);
36 my $title = $ctx->{-title_html} || $desc;
37 my $upfx = $ctx->{-upfx} || '';
38 my $help = $upfx.'_/text/help';
39 my $color = $upfx.'_/text/color';
40 my $atom = $ctx->{-atom} || $upfx.'new.atom';
41 my $tip = $ctx->{-html_tip} || '';
42 my $top = "<b>$desc</b>";
43 my $links = "<a\nhref=\"$help\">help</a> / ".
44 "<a\nhref=\"$color\">color</a> / ".
45 "<a\nhref=\"$atom\">Atom feed</a>";
47 my $q_val = $ctx->{-q_value_html};
48 if (defined $q_val && $q_val ne '') {
49 $q_val = qq(\nvalue="$q_val");
53 # XXX gross, for SearchView.pm
54 my $extra = $ctx->{-extra_form_html} || '';
55 my $action = $upfx eq '' ? './' : $upfx;
56 $top = qq{<form\naction="$action"><pre>$top} .
57 qq{\n<input\nname=q\ntype=text$q_val />} .
59 qq{<input\ntype=submit\nvalue=search />} .
63 $top = '<pre>' . $top . "\n" . $links . '</pre>';
65 "<html><head><title>$title</title>" .
66 "<link\nrel=alternate\ntitle=\"Atom feed\"\n".
67 "href=\"$atom\"\ntype=\"application/atom+xml\"/>" .
68 $ctx->{www}->style($upfx) .
69 "</head><body>". $top . $tip;
74 my $u = PublicInbox::Hval::prurl($env, $CODE_URL);
75 qq(AGPL code for this site: git clone <a\nhref="$u">$u</a> $PROJECT)
80 my $urls = 'Archives are clonable:';
81 my $ctx = $self->{ctx};
82 my $ibx = $ctx->{-inbox};
83 my $desc = ascii_html($ibx->description);
86 my $http = $ibx->base_url($ctx->{env});
87 chop $http; # no trailing slash for clone
88 my $max = $ibx->max_git_epoch;
89 my $dir = (split(m!/!, $http))[-1];
90 if (defined($max)) { # v2
93 # old parts my be deleted:
94 -d "$ibx->{mainrepo}/git/$i.git" or next;
97 push @urls, "$url $dir/git/$i.git";
104 # FIXME: epoch splits can be different in other repositories,
105 # use the "cloneurl" file as-is for now:
106 foreach my $u (@{$ibx->cloneurl}) {
109 push @urls, $u =~ /\Ahttps?:/ ? qq(<a\nhref="$u">$u</a>) : $u;
112 if (defined($max) || scalar(@urls) > 1) {
114 join("\n", map { "\tgit clone --mirror $_" } @urls);
116 $urls .= " git clone --mirror $urls[0]";
119 my $addrs = $ibx->{address};
120 $addrs = join(' ', @$addrs) if ref($addrs) eq 'ARRAY';
124 # If you have public-inbox 1.1+ installed, you may
125 # initialize and index your mirror using the following commands:
126 public-inbox-init -V2 $ibx->{name} $dir/ $http \\
128 public-inbox-index $dir
131 my @nntp = map { qq(<a\nhref="$_">$_</a>) } @{$ibx->nntp_url};
134 $urls .= @nntp == 1 ? 'Newsgroup' : 'Newsgroups are';
135 $urls .= ' available over NNTP:';
136 $urls .= "\n\t" . join("\n\t", @nntp) . "\n";
138 if ($urls =~ m!\b[^:]+://\w+\.onion/!) {
139 $urls .= "\n note: .onion URLs require Tor: ";
140 $urls .= qq[<a\nhref="$TOR_URL">$TOR_URL</a>];
142 '<hr><pre>'.join("\n\n",
145 code_footer($ctx->{env})
146 ).'</pre></body></html>';
149 # callback for HTTP.pm (and any other PSGI servers)
152 my $nr = $self->{nr}++;
154 return _html_top($self) if $nr == 0;
156 if (my $middle = $self->{cb}) {
157 $middle = $middle->($nr, $self->{ctx}) and return $middle;
160 delete $self->{cb} ? _html_end($self) : undef;