X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=lib%2FPublicInbox%2FGetlineBody.pm;h=988bc63f41d59bac209931af65bc64d05deedd0b;hb=5afb24f06627641e3fed608e807b5ab628cda348;hp=5f32782886205641ac46dcbc7abd28949d0b0c32;hpb=3f779258173530ca88f31e1dc5332f951d2c44cd;p=public-inbox.git diff --git a/lib/PublicInbox/GetlineBody.pm b/lib/PublicInbox/GetlineBody.pm index 5f327828..988bc63f 100644 --- a/lib/PublicInbox/GetlineBody.pm +++ b/lib/PublicInbox/GetlineBody.pm @@ -1,16 +1,24 @@ -# Copyright (C) 2016 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 # end callback when the object goes out-of-scope. # This depends on rpipe being _blocking_ on getline. +# +# 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) = @_; - 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, @@ -20,16 +28,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;