From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.0 required=3.0 tests=ALL_TRUSTED,BAYES_00 shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id C2AE31F61D for ; Sat, 21 Mar 2020 02:03:54 +0000 (UTC) From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 03/11] wwwstream: introduce oneshot API to avoid ->getline Date: Sat, 21 Mar 2020 02:03:46 +0000 Message-Id: <20200321020354.9056-4-e@yhbt.net> In-Reply-To: <20200321020354.9056-1-e@yhbt.net> References: <20200321020354.9056-1-e@yhbt.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: 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 | 8 +------- lib/PublicInbox/WwwStream.pm | 21 ++++++++++++++++++--- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/lib/PublicInbox/ViewVCS.pm b/lib/PublicInbox/ViewVCS.pm index 2f8e1c4f..6714e67c 100644 --- a/lib/PublicInbox/ViewVCS.pm +++ b/lib/PublicInbox/ViewVCS.pm @@ -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; } 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;