]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/GetlineBody.pm
qspawn: reinstate filter support, add gzip filter
[public-inbox.git] / lib / PublicInbox / GetlineBody.pm
index ea07f3d6dd0de1d031012c62372754dbc91acd49..6becaaf5fc6af0b22d9bd3847e36257e83be8845 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2016-2018 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
@@ -13,8 +13,14 @@ use strict;
 use warnings;
 
 sub new {
-       my ($class, $rpipe, $end, $buf) = @_;
-       bless { rpipe => $rpipe, end => $end, buf => $buf }, $class;
+       my ($class, $rpipe, $end, $end_arg, $buf, $filter) = @_;
+       bless {
+               rpipe => $rpipe,
+               end => $end,
+               end_arg => $end_arg,
+               initial_buf => $buf,
+               filter => $filter,
+       }, $class;
 }
 
 # close should always be called after getline returns undef,
@@ -24,16 +30,19 @@ sub DESTROY { $_[0]->close }
 
 sub getline {
        my ($self) = @_;
-       my $buf = delete $self->{buf}; # initial buffer
-       defined $buf ? $buf : $self->{rpipe}->getline;
+       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;