1 # Copyright (C) 2016 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
5 package PublicInbox::WwwStream;
8 use PublicInbox::Hval qw(ascii_html);
10 use constant PI_URL => 'https://public-inbox.org/public-inbox.git';
13 my ($class, $ctx, $cb) = @_;
14 bless { nr => 0, cb => $cb, ctx => $ctx }, $class;
18 my ($class, $ctx, $code, $cb) = @_;
19 [ $code, [ 'Content-Type', 'text/html; charset=UTF-8' ],
20 $class->new($ctx, $cb) ]
25 my $ctx = $self->{ctx};
26 my $obj = $ctx->{-inbox};
27 my $desc = ascii_html($obj->description);
28 my $title = $ctx->{-title_html} || $desc;
29 my $upfx = $ctx->{-upfx} || '';
30 my $atom = $ctx->{-atom} || $upfx.'new.atom';
31 my $tip = $ctx->{-html_tip} || '';
32 my $top = "<b>$desc</b> (<a\nhref=\"$atom\">Atom feed</a>)";
34 my $q_val = $ctx->{-q_value_html};
35 if (defined $q_val && $q_val ne '') {
36 $q_val = qq(\nvalue="$q_val" );
40 # XXX gross, for SearchView.pm
41 my $extra = $ctx->{-extra_form_html} || '';
42 my $action = $upfx eq '' ? './' : $upfx;
43 $top = qq{<form\naction="$action"><pre>$top} .
44 qq{ <input\nname=q\ntype=text$q_val/>} .
46 qq{<input\ntype=submit\nvalue=search />} .
49 $top = '<pre>' . $top . '</pre>';
51 "<html><head><title>$title</title>" .
52 "<link\nrel=alternate\ntitle=\"Atom feed\"\n".
53 "href=\"$atom\"\ntype=\"application/atom+xml\"/>" .
54 PublicInbox::Hval::STYLE .
55 "</head><body>". $top . $tip;
60 my $urls = 'Archives are clone-able:';
61 my $ctx = $self->{ctx};
62 my $obj = $ctx->{-inbox};
63 my $desc = ascii_html($obj->description);
66 my $env = $ctx->{env};
67 my $scheme = $env->{'psgi.url_scheme'};
68 my $host_port = $env->{HTTP_HOST} ||
69 "$env->{SERVER_NAME}:$env->{SERVER_PORT}";
70 my $http = "$scheme://$host_port".($env->{SCRIPT_NAME} || '/');
71 $http = URI->new($http . $obj->{name})->canonical->as_string;
72 my %seen = ( $http => 1 );
74 foreach my $u (@{$obj->cloneurl}) {
77 push @urls, $u =~ /\Ahttps?:/ ? qq(<a\nhref="$u">$u</a>) : $u;
79 if (scalar(@urls) == 1) {
80 $urls .= " git clone --mirror $http";
83 join("\n", map { "\tgit clone --mirror $_" } @urls);
85 my $url = PublicInbox::Hval::prurl($ctx->{env}, PI_URL);
89 'Archived served using code from public-inbox:',
91 ).'</pre></body></html>';
96 my $nr = $self->{nr}++;
98 return _html_top($self) if $nr == 0;
100 if (my $mid = $self->{cb}) { # middle
101 $mid = $mid->($nr, $self->{ctx}) and return $mid;
104 delete $self->{cb} ? _html_end($self) : undef;