]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/GetlineBody.pm
www: update internal docs
[public-inbox.git] / lib / PublicInbox / GetlineBody.pm
index 750a8c536183e0abeb2cd0eba60b54ab341e6de4..988bc63f41d59bac209931af65bc64d05deedd0b 100644 (file)
@@ -1,13 +1,11 @@
-# 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>
 
 # Wrap a pipe or file for PSGI streaming response bodies and calls the
 # end callback when the object goes out-of-scope.
 # This depends on rpipe being _blocking_ on getline.
 #
-# public-inbox-httpd favors "getline" response bodies to take a
-# "pull"-based approach to feeding slow clients (as opposed to a
-# more common "push" model)
+# This is only used by generic PSGI servers and not public-inbox-httpd
 package PublicInbox::GetlineBody;
 use strict;
 use warnings;
@@ -18,8 +16,8 @@ sub new {
                rpipe => $rpipe,
                end => $end,
                end_arg => $end_arg,
-               buf => $buf,
-               filter => $filter || 0,
+               initial_buf => $buf,
+               filter => $filter,
        }, $class;
 }
 
@@ -30,19 +28,18 @@ sub DESTROY { $_[0]->close }
 
 sub getline {
        my ($self) = @_;
-       my $filter = $self->{filter};
-       return if $filter == -1; # last call was EOF
-
-       my $buf = delete $self->{buf}; # initial buffer
-       $buf = $self->{rpipe}->getline unless defined $buf;
-       $self->{filter} = -1 unless defined $buf; # set EOF for next call
-       $filter ? $filter->($buf) : $buf;
+       my $rpipe = $self->{rpipe} or return; # EOF was set on previous call
+       my $buf = delete($self->{initial_buf}) // $rpipe->getline;
+       delete($self->{rpipe}) unless defined $buf; # set EOF for next call
+       if (my $filter = $self->{filter}) {
+               $buf = $filter->translate($buf);
+       }
+       $buf;
 }
 
 sub close {
        my ($self) = @_;
-       my ($rpipe, $end, $end_arg) = delete @$self{qw(rpipe end end_arg)};
-       close $rpipe if $rpipe;
+       my ($end, $end_arg) = delete @$self{qw(end end_arg)};
        $end->($end_arg) if $end;
 }