X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=lib%2FPublicInbox%2FGetlineBody.pm;h=0e7812248e516798f067d4fa463620458819326c;hb=af0b0fb7a454470a32c452119d0392e0dedb3fe1;hp=0a922fd271ccd53426fe3caa18c11be105f3d8cf;hpb=cf439203c31836e4c48f632f902aa58db6924972;p=public-inbox.git diff --git a/lib/PublicInbox/GetlineBody.pm b/lib/PublicInbox/GetlineBody.pm index 0a922fd2..0e781224 100644 --- a/lib/PublicInbox/GetlineBody.pm +++ b/lib/PublicInbox/GetlineBody.pm @@ -1,24 +1,23 @@ -# Copyright (C) 2016-2018 all contributors +# Copyright (C) 2016-2021 all contributors # License: AGPL-3.0+ # 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; sub new { - my ($class, $rpipe, $end, $buf, $filter) = @_; + my ($class, $rpipe, $end, $end_arg, $buf, $filter) = @_; bless { rpipe => $rpipe, end => $end, - buf => $buf, - filter => $filter || 0, + end_arg => $end_arg, + initial_buf => $buf, + filter => $filter, }, $class; } @@ -29,21 +28,19 @@ 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 = delete $self->{rpipe}; - close $rpipe if $rpipe; - my $end = delete $self->{end}; - $end->() if $end; + my ($end, $end_arg) = delete @$self{qw(end end_arg)}; + $end->($end_arg) if $end; } 1;