X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=lib%2FPublicInbox%2FGetlineBody.pm;h=6becaaf5fc6af0b22d9bd3847e36257e83be8845;hb=a71cb67a1237c450a9cbbd6738c5af3b73ba4c61;hp=bcabc04a62645dc37292d2386f9292256e776840;hpb=65e3cc8f6cc73e45db827cbeee4ccecbf1502496;p=public-inbox.git diff --git a/lib/PublicInbox/GetlineBody.pm b/lib/PublicInbox/GetlineBody.pm index bcabc04a..6becaaf5 100644 --- a/lib/PublicInbox/GetlineBody.pm +++ b/lib/PublicInbox/GetlineBody.pm @@ -1,4 +1,4 @@ -# Copyright (C) 2016-2019 all contributors +# Copyright (C) 2016-2020 all contributors # License: AGPL-3.0+ # Wrap a pipe or file for PSGI streaming response bodies and calls the @@ -13,13 +13,13 @@ use strict; use warnings; sub new { - my ($class, $rpipe, $end, $end_arg, $buf) = @_; + my ($class, $rpipe, $end, $end_arg, $buf, $filter) = @_; bless { rpipe => $rpipe, end => $end, end_arg => $end_arg, - buf => $buf, - filter => 0, + initial_buf => $buf, + filter => $filter, }, $class; } @@ -30,19 +30,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 + 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; }