1 # Copyright (C) 2016-2019 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # Wrap a pipe or file for PSGI streaming response bodies and calls the
5 # end callback when the object goes out-of-scope.
6 # This depends on rpipe being _blocking_ on getline.
8 # public-inbox-httpd favors "getline" response bodies to take a
9 # "pull"-based approach to feeding slow clients (as opposed to a
10 # more common "push" model)
11 package PublicInbox::GetlineBody;
16 my ($class, $rpipe, $end, $end_arg, $buf) = @_;
26 # close should always be called after getline returns undef,
27 # but a client aborting a connection can ruin our day; so lets
28 # hope our underlying PSGI server does not leak references, here.
29 sub DESTROY { $_[0]->close }
33 my $filter = $self->{filter};
34 return if $filter == -1; # last call was EOF
36 my $buf = delete $self->{buf}; # initial buffer
37 $buf = $self->{rpipe}->getline unless defined $buf;
38 $self->{filter} = -1 unless defined $buf; # set EOF for next call
44 my ($rpipe, $end, $end_arg) = delete @$self{qw(rpipe end end_arg)};
45 close $rpipe if $rpipe;
46 $end->($end_arg) if $end;