]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/GetlineBody.pm
update copyrights for 2018
[public-inbox.git] / lib / PublicInbox / GetlineBody.pm
1 # Copyright (C) 2016-2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
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.
7 package PublicInbox::GetlineBody;
8 use strict;
9 use warnings;
10
11 sub new {
12         my ($class, $rpipe, $end, $buf) = @_;
13         bless { rpipe => $rpipe, end => $end, buf => $buf }, $class;
14 }
15
16 # close should always be called after getline returns undef,
17 # but a client aborting a connection can ruin our day; so lets
18 # hope our underlying PSGI server does not leak references, here.
19 sub DESTROY { $_[0]->close }
20
21 sub getline {
22         my ($self) = @_;
23         my $buf = delete $self->{buf}; # initial buffer
24         defined $buf ? $buf : $self->{rpipe}->getline;
25 }
26
27 sub close {
28         my ($self) = @_;
29         my $rpipe = delete $self->{rpipe};
30         close $rpipe if $rpipe;
31         my $end = delete $self->{end};
32         $end->() if $end;
33 }
34
35 1;