-# Copyright (C) 2016-2019 all contributors <meta@public-inbox.org>
+# Copyright (C) 2016-2020 all contributors <meta@public-inbox.org>
# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
#
# HTML body stream for which yields getline+close methods
package PublicInbox::WwwStream;
use strict;
use warnings;
+use base qw(Exporter);
+our @EXPORT_OK = qw(html_oneshot);
+use bytes (); # length
use PublicInbox::Hval qw(ascii_html prurl);
our $TOR_URL = 'https://www.torproject.org/';
our $CODE_URL = 'https://public-inbox.org/public-inbox.git';
# noop for HTTP.pm (and any other PSGI servers)
sub close {}
+sub base_url ($) {
+ my $ctx = shift;
+ my $base_url = $ctx->{-inbox}->base_url($ctx->{env});
+ chop $base_url; # no trailing slash for clone
+ $base_url;
+}
+
sub new {
my ($class, $ctx, $cb) = @_;
- my $base_url = $ctx->{-inbox}->base_url($ctx->{env});
- chop $base_url; # no trailing slash for clone
bless {
nr => 0,
- cb => $cb || \&close,
+ cb => $cb,
ctx => $ctx,
- base_url => $base_url,
+ base_url => base_url($ctx),
}, $class;
}
delete $self->{cb} ? _html_end($self) : undef;
}
+sub html_oneshot ($$;$) {
+ my ($ctx, $code, $sref) = @_;
+ my $self = bless {
+ ctx => $ctx,
+ base_url => base_url($ctx),
+ }, __PACKAGE__;
+ my @x = (_html_top($self), $sref ? $$sref : (), _html_end($self));
+ my $len = 0;
+ $len += bytes::length($_) for @x;
+ [ $code, [
+ 'Content-Type' => 'text/html; charset=UTF-8',
+ 'Content-Length' => $len
+ ], \@x ];
+}
+
1;