]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WwwStream.pm
www_stream: add response wrapper sub
[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/README.html';
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                 $top = qq{<form\naction="$upfx"><pre>$top} .
43                           qq{ <input\nname=q\ntype=text$q_val/>} .
44                           $extra .
45                           qq{<input\ntype=submit\nvalue=search />} .
46                           q{</pre></form>}
47         } else {
48                 $top = '<pre>' . $top . '</pre>';
49         }
50         "<html><head><title>$title</title>" .
51                 "<link\nrel=alternate\ntitle=\"Atom feed\"\n".
52                 "href=\"$atom\"\ntype=\"application/atom+xml\"/>" .
53                 PublicInbox::Hval::STYLE .
54                 "</head><body>". $top . $tip;
55 }
56
57 sub _html_end {
58         my ($self) = @_;
59         my $urls = 'Archives are clone-able:';
60         my $ctx = $self->{ctx};
61         my $obj = $ctx->{-inbox};
62         my $desc = ascii_html($obj->description);
63
64         # FIXME: cleanup
65         my $env = $ctx->{env};
66         my $scheme = $env->{'psgi.url_scheme'};
67         my $host_port = $env->{HTTP_HOST} ||
68                         "$env->{SERVER_NAME}:$env->{SERVER_PORT}";
69         my $http = "$scheme://$host_port".($env->{SCRIPT_NAME} || '/');
70         $http = URI->new($http . $obj->{name})->canonical->as_string;
71         my %seen = ( $http => 1 );
72         my @urls = ($http);
73         foreach my $u (@{$obj->cloneurl}) {
74                 next if $seen{$u};
75                 $seen{$u} = 1;
76                 push @urls, $u =~ /\Ahttps?:/ ? qq(<a\nhref="$u">$u</a>) : $u;
77         }
78         if (scalar(@urls) == 1) {
79                 $urls .= " git clone --mirror $http";
80         } else {
81                 $urls .= "\n" .
82                         join("\n", map { "\tgit clone --mirror $_" } @urls);
83         }
84         my $url = PublicInbox::Hval::prurl($ctx->{env}, PI_URL);
85         '<pre>'.join("\n",
86                 '- ' . $desc,
87                 $urls,
88                 'served with software from public-inbox: '
89                         ."<a\nhref=\"$url\">$url</a>",
90         ).'</pre></body></html>';
91 }
92
93 sub getline {
94         my ($self) = @_;
95         my $nr = $self->{nr}++;
96
97         return _html_top($self) if $nr == 0;
98
99         if (my $mid = $self->{cb}) { # middle
100                 $mid = $mid->($nr, $self->{ctx}) and return $mid;
101         }
102
103         delete $self->{cb} ? _html_end($self) : undef;
104 }
105
106 sub close {}
107
108 1;