]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/WwwStream.pm
extmsg: use WwwResponse::oneshot
[public-inbox.git] / lib / PublicInbox / WwwStream.pm
index a4ba1fff6a35d298818e4d6981477f27e99bd56d..fceef745d0c4ec2291448a2ff131a68880030530 100644 (file)
@@ -1,4 +1,4 @@
-# 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
@@ -16,16 +16,21 @@ 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;
 }
 
@@ -164,4 +169,15 @@ sub getline {
        delete $self->{cb} ? _html_end($self) : undef;
 }
 
+sub oneshot {
+       my ($ctx, $code, $strref) = @_;
+       my $self = bless {
+               ctx => $ctx,
+               base_url => base_url($ctx),
+       }, __PACKAGE__;
+       [ $code, [ 'Content-Type', 'text/html; charset=UTF-8' ], [
+               _html_top($self), $strref ? $$strref : (), _html_end($self)
+       ] ]
+}
+
 1;