about summary refs log tree commit homepage
path: root/lib/PublicInbox/WwwStream.pm
diff options
context:
space:
mode:
authorEric Wong <e@yhbt.net>2020-07-05 23:27:39 +0000
committerEric Wong <e@yhbt.net>2020-07-06 20:01:15 +0000
commit55263c56cf41c87f8977cd6a6be65ac07b5cea87 (patch)
treef3e711c2d82fd8bf1e179d7133bd89f7cc924067 /lib/PublicInbox/WwwStream.pm
parent52a02a813a46940530183ede4d4cc7028290cd8f (diff)
downloadpublic-inbox-55263c56cf41c87f8977cd6a6be65ac07b5cea87.tar.gz
wwwstream: reduce blob fetch paths for ->getline
This will make it easier to support asynchronous blob
retrievals.  The `$ctx->{nr}' counter is no longer implicitly
supplied since many users didn't care for it, so stack overhead
is slightly reduced.
Diffstat (limited to 'lib/PublicInbox/WwwStream.pm')
-rw-r--r--lib/PublicInbox/WwwStream.pm19
1 files changed, 6 insertions, 13 deletions
diff --git a/lib/PublicInbox/WwwStream.pm b/lib/PublicInbox/WwwStream.pm
index c80440d1..4d82cbb4 100644
--- a/lib/PublicInbox/WwwStream.pm
+++ b/lib/PublicInbox/WwwStream.pm
@@ -31,7 +31,6 @@ sub init {
         my ($ctx, $cb) = @_;
         $ctx->{cb} = $cb;
         $ctx->{base_url} = base_url($ctx);
-        $ctx->{nr} = 0;
         bless $ctx, __PACKAGE__;
 }
 
@@ -43,7 +42,7 @@ sub response {
         [ $code, $h, $ctx ]
 }
 
-sub _html_top ($) {
+sub html_top ($) {
         my ($ctx) = @_;
         my $ibx = $ctx->{-inbox};
         my $desc = ascii_html($ibx->description);
@@ -159,15 +158,9 @@ EOF
 # callback for HTTP.pm (and any other PSGI servers)
 sub getline {
         my ($ctx) = @_;
-        my $nr = $ctx->{nr}++;
-
-        my $buf = do {
-                if ($nr == 0) {
-                        _html_top($ctx);
-                } elsif (my $middle = $ctx->{cb}) {
-                        $middle->($nr, $ctx);
-                }
-        } // (delete($ctx->{cb}) ? _html_end($ctx) : undef);
+        my $cb = $ctx->{cb};
+        my $buf = $cb->($ctx) if $cb;
+        $buf //= delete($ctx->{cb}) ? _html_end($ctx) : undef;
 
         # gzf may be GzipFilter, `undef' or `0'
         my $gzf = $ctx->{gzf} or return $buf;
@@ -185,12 +178,12 @@ sub html_oneshot ($$;$) {
         my $h = [ 'Content-Type' => 'text/html; charset=UTF-8',
                 'Content-Length' => undef ];
         if (my $gzf = gzf_maybe($h, $ctx->{env})) {
-                $gzf->zmore(_html_top($ctx));
+                $gzf->zmore(html_top($ctx));
                 $gzf->zmore($$sref) if $sref;
                 $x[0] = $gzf->zflush(_html_end($ctx));
                 $h->[3] = length($x[0]);
         } else {
-                @x = (_html_top($ctx), $sref ? $$sref : (), _html_end($ctx));
+                @x = (html_top($ctx), $sref ? $$sref : (), _html_end($ctx));
                 $h->[3] += bytes::length($_) for @x;
         }
         [ $code, $h, \@x ]