]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/GetlineBody.pm
git-http-backend: fix aborts for generic PSGI clone
[public-inbox.git] / lib / PublicInbox / GetlineBody.pm
diff --git a/lib/PublicInbox/GetlineBody.pm b/lib/PublicInbox/GetlineBody.pm
new file mode 100644 (file)
index 0000000..4f8765b
--- /dev/null
@@ -0,0 +1,31 @@
+# Copyright (C) 2016 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
+# end callback when the object goes out-of-scope.
+# This depends on rpipe being _blocking_ on getline.
+package PublicInbox::GetlineBody;
+use strict;
+use warnings;
+
+sub new {
+       my ($class, $rpipe, $end, $buf) = @_;
+       bless { rpipe => $rpipe, end => $end, buf => $buf }, $class;
+}
+
+sub DESTROY { $_[0]->close }
+
+sub getline {
+       my ($self) = @_;
+       my $buf = delete $self->{buf};
+       defined $buf ? $buf : $self->{rpipe}->getline;
+}
+
+sub close {
+       my ($self) = @_;
+       delete $self->{rpipe};
+       my $end = delete $self->{end} or return;
+       $end->();
+}
+
+1;