]> Sergey Matveev's repositories - public-inbox.git/commitdiff
wwwstream: introduce oneshot API to avoid ->getline
authorEric Wong <e@yhbt.net>
Sat, 21 Mar 2020 02:03:46 +0000 (02:03 +0000)
committerEric Wong <e@yhbt.net>
Wed, 25 Mar 2020 01:48:35 +0000 (01:48 +0000)
The ->getline API is only useful for limiting memory use when
streaming responses containing multiple emails or log messages.
However it's unnecessary complexity and overhead for callers
(PublicInbox::HTTP) when there's only a single message.

lib/PublicInbox/ViewVCS.pm
lib/PublicInbox/WwwStream.pm

index 2f8e1c4f6531413e8bec34b5929678bdf8f86ab8..6714e67cdabf6556c97aca3b40e0c48c94332040 100644 (file)
@@ -31,17 +31,11 @@ my %QP_MAP = ( A => 'oid_a', B => 'oid_b', a => 'path_a', b => 'path_b' );
 our $MAX_SIZE = 1024 * 1024; # TODO: configurable
 my $BIN_DETECT = 8000; # same as git
 
-sub html_i { # WwwStream::getline callback
-       my ($nr, $ctx) =  @_;
-       $nr == 1 ? ${delete $ctx->{obuf}} : undef;
-}
-
 sub html_page ($$$) {
        my ($ctx, $code, $strref) = @_;
        my $wcb = delete $ctx->{-wcb};
        $ctx->{-upfx} = '../../'; # from "/$INBOX/$OID/s/"
-       $ctx->{obuf} = $strref;
-       my $res = PublicInbox::WwwStream->response($ctx, $code, \&html_i);
+       my $res = PublicInbox::WwwStream::oneshot($ctx, $code, $strref);
        $wcb ? $wcb->($res) : $res;
 }
 
index 3a867ec3a0de9f28592fbb782166d5943d86ae73..2dd8b157ddc7334d49a95fdd97fc7ea47563f59c 100644 (file)
@@ -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,
                ctx => $ctx,
-               base_url => $base_url,
+               base_url => base_url($ctx),
        }, $class;
 }
 
@@ -164,4 +169,14 @@ 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, _html_end($self) ] ]
+}
+
 1;