]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/GetlineBody.pm
git-http-backend: fix aborts for generic PSGI clone
[public-inbox.git] / lib / PublicInbox / GetlineBody.pm
1 # Copyright (C) 2016 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 sub DESTROY { $_[0]->close }
17
18 sub getline {
19         my ($self) = @_;
20         my $buf = delete $self->{buf};
21         defined $buf ? $buf : $self->{rpipe}->getline;
22 }
23
24 sub close {
25         my ($self) = @_;
26         delete $self->{rpipe};
27         my $end = delete $self->{end} or return;
28         $end->();
29 }
30
31 1;