]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WwwStream.pm
www_stream: fix stupid typo :x
[public-inbox.git] / lib / PublicInbox / WwwStream.pm
1 # Copyright (C) 2016 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 #
4 # HTML body stream for which yields getline+close methods
5 package PublicInbox::WwwStream;
6 use strict;
7 use warnings;
8 use PublicInbox::Hval qw(ascii_html);
9 use URI;
10 use constant PI_URL => 'https://public-inbox.org/';
11
12 sub new {
13         my ($class, $ctx, $cb) = @_;
14         bless { nr => 0, cb => $cb, ctx => $ctx }, $class;
15 }
16
17 sub response {
18         my ($class, $ctx, $code, $cb) = @_;
19         [ $code, [ 'Content-Type', 'text/html; charset=UTF-8' ],
20           $class->new($ctx, $cb) ]
21 }
22
23 sub _html_top ($) {
24         my ($self) = @_;
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>)";
33         if ($obj->search) {
34                 my $q_val = $ctx->{-q_value_html};
35                 if (defined $q_val && $q_val ne '') {
36                         $q_val = qq(\nvalue="$q_val" );
37                 } else {
38                         $q_val = '';
39                 }
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/>} .
45                           $extra .
46                           qq{<input\ntype=submit\nvalue=search />} .
47                           q{</pre></form>}
48         } else {
49                 $top = '<pre>' . $top . '</pre>';
50         }
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;
56 }
57
58 sub _html_end {
59         my ($self) = @_;
60         my $urls = 'Archives are clone-able:';
61         my $ctx = $self->{ctx};
62         my $obj = $ctx->{-inbox};
63         my $desc = ascii_html($obj->description);
64
65         # FIXME: cleanup
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 );
73         my @urls = ($http);
74         foreach my $u (@{$obj->cloneurl}) {
75                 next if $seen{$u};
76                 $seen{$u} = 1;
77                 push @urls, $u =~ /\Ahttps?:/ ? qq(<a\nhref="$u">$u</a>) : $u;
78         }
79         if (scalar(@urls) == 1) {
80                 $urls .= " git clone --mirror $http";
81         } else {
82                 $urls .= "\n" .
83                         join("\n", map { "\tgit clone --mirror $_" } @urls);
84         }
85         my $url = PublicInbox::Hval::prurl($ctx->{env}, PI_URL);
86         '<pre>'.join("\n",
87                 '- ' . $desc,
88                 $urls,
89                 'Archived served using code from public-inbox:',
90                 qq(\tgit clone <a\nhref="$url">$url</a> public-inbox),
91         ).'</pre></body></html>';
92 }
93
94 sub getline {
95         my ($self) = @_;
96         my $nr = $self->{nr}++;
97
98         return _html_top($self) if $nr == 0;
99
100         if (my $mid = $self->{cb}) { # middle
101                 $mid = $mid->($nr, $self->{ctx}) and return $mid;
102         }
103
104         delete $self->{cb} ? _html_end($self) : undef;
105 }
106
107 sub close {}
108
109 1;