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 $TOR2WEB_URL = 'https://www.tor2web.org/';
16 our $CODE_URL = 'https://public-inbox.org/';
17 our $PROJECT = 'public-inbox';
19 # noop for HTTP.pm (and any other PSGI servers)
23 my ($class, $ctx, $cb) = @_;
24 bless { nr => 0, cb => $cb || *close, ctx => $ctx }, $class;
28 my ($class, $ctx, $code, $cb) = @_;
29 [ $code, [ 'Content-Type', 'text/html; charset=UTF-8' ],
30 $class->new($ctx, $cb) ]
35 my $ctx = $self->{ctx};
36 my $obj = $ctx->{-inbox};
37 my $desc = ascii_html($obj->description);
38 my $title = $ctx->{-title_html} || $desc;
39 my $upfx = $ctx->{-upfx} || '';
40 my $help = $upfx.'_/text/help';
41 my $color = $upfx.'_/text/color';
42 my $atom = $ctx->{-atom} || $upfx.'new.atom';
43 my $tip = $ctx->{-html_tip} || '';
44 my $top = "<b>$desc</b>";
45 my $links = "<a\nhref=\"$help\">help</a> / ".
46 "<a\nhref=\"$color\">color</a> / ".
47 "<a\nhref=\"$atom\">Atom feed</a>";
49 my $q_val = $ctx->{-q_value_html};
50 if (defined $q_val && $q_val ne '') {
51 $q_val = qq(\nvalue="$q_val");
55 # XXX gross, for SearchView.pm
56 my $extra = $ctx->{-extra_form_html} || '';
57 my $action = $upfx eq '' ? './' : $upfx;
58 $top = qq{<form\naction="$action"><pre>$top} .
59 qq{\n<input\nname=q\ntype=text$q_val />} .
61 qq{<input\ntype=submit\nvalue=search />} .
65 $top = '<pre>' . $top . "\n" . $links . '</pre>';
67 "<html><head><title>$title</title>" .
68 "<link\nrel=alternate\ntitle=\"Atom feed\"\n".
69 "href=\"$atom\"\ntype=\"application/atom+xml\"/>" .
70 $ctx->{www}->style($upfx) .
71 "</head><body>". $top . $tip;
76 my $urls = 'Archives are clonable:';
77 my $ctx = $self->{ctx};
78 my $obj = $ctx->{-inbox};
79 my $desc = ascii_html($obj->description);
82 my $http = $obj->base_url($ctx->{env});
83 chop $http; # no trailing slash for clone
84 my $part = $obj->max_git_part;
85 my $dir = (split(m!/!, $http))[-1];
86 if (defined($part)) { # v2
88 for my $i (0..$part) {
89 # old parts my be deleted:
90 -d "$obj->{mainrepo}/git/$i.git" or next;
93 push @urls, "$url $dir/git/$i.git";
100 # FIXME: partitioning in can be different in other repositories,
101 # use the "cloneurl" file as-is for now:
102 foreach my $u (@{$obj->cloneurl}) {
105 push @urls, $u =~ /\Ahttps?:/ ? qq(<a\nhref="$u">$u</a>) : $u;
108 if (defined($part) || scalar(@urls) > 1) {
110 join("\n", map { "\tgit clone --mirror $_" } @urls);
112 $urls .= " git clone --mirror $urls[0]";
115 my $addrs = $obj->{address};
116 $addrs = join(' ', @$addrs) if ref($addrs) eq 'ARRAY';
120 # If you have public-inbox 1.1+ installed, you may
121 # initialize and index your mirror using the following commands:
122 public-inbox-init -V2 $obj->{name} $dir/ $http \\
124 public-inbox-index $dir
127 my @nntp = map { qq(<a\nhref="$_">$_</a>) } @{$obj->nntp_url};
130 $urls .= @nntp == 1 ? 'Newsgroup' : 'Newsgroups are';
131 $urls .= ' available over NNTP:';
132 $urls .= "\n\t" . join("\n\t", @nntp) . "\n";
134 if ($urls =~ m!\b[^:]+://\w+\.onion/!) {
135 $urls .= "\n note: .onion URLs require Tor: ";
136 $urls .= qq[<a\nhref="$TOR_URL">$TOR_URL</a>];
138 $urls .= "\n or Tor2web: ";
139 $urls .= qq[<a\nhref="$TOR2WEB_URL">$TOR2WEB_URL</a>];
142 my $url = PublicInbox::Hval::prurl($ctx->{env}, $CODE_URL);
143 '<hr><pre>'.join("\n\n",
146 'AGPL code for this site: '.
147 qq(git clone <a\nhref="$url">$url</a> $PROJECT)
148 ).'</pre></body></html>';
151 # callback for HTTP.pm (and any other PSGI servers)
154 my $nr = $self->{nr}++;
156 return _html_top($self) if $nr == 0;
158 if (my $middle = $self->{cb}) {
159 $middle = $middle->($nr, $self->{ctx}) and return $middle;
162 delete $self->{cb} ? _html_end($self) : undef;