From 433f6127f648e83b8f12d7b232ff726a34a85beb Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sat, 21 Mar 2020 02:03:46 +0000 Subject: wwwstream: introduce oneshot API to avoid ->getline 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/WwwStream.pm | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) (limited to 'lib/PublicInbox/WwwStream.pm') diff --git a/lib/PublicInbox/WwwStream.pm b/lib/PublicInbox/WwwStream.pm index 3a867ec3..2dd8b157 100644 --- a/lib/PublicInbox/WwwStream.pm +++ b/lib/PublicInbox/WwwStream.pm @@ -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; -- cgit v1.2.3-24-ge0c7