lib/PublicInbox/HTTP.pm | 26 ++++++++++++++++++++++---- diff --git a/lib/PublicInbox/HTTP.pm b/lib/PublicInbox/HTTP.pm index 1ef3fb31d8d6f8a61a1482860e33ddf5becc2b65..f69056f87932980cb8dffe2d41ae9d1c3be4297b 100644 --- a/lib/PublicInbox/HTTP.pm +++ b/lib/PublicInbox/HTTP.pm @@ -205,7 +205,7 @@ my $close = sub { if ($alive) { $self->event_write; # watch for readability if done } else { - $self->write(sub { $self->close }); + Danga::Socket::write($self, sub { $self->close }); } if (my $obj = $env->{'pi-httpd.inbox'}) { # grace period for reaping resources @@ -215,9 +215,27 @@ } $self->{env} = undef; }; - if (defined $res->[2]) { - Plack::Util::foreach($res->[2], $write); - $close->(); + if (defined(my $body = $res->[2])) { + if (ref $body eq 'ARRAY') { + $write->($_) foreach @$body; + $close->(); + } else { + my $pull; + $pull = sub { + local $/ = \8192; + while (defined(my $buf = $body->getline)) { + $write->($buf); + if ($self->{write_buf}) { + $self->write($pull); + return; + } + } + $pull = undef; + $body->close(); + $close->(); + }; + $pull->(); + } } else { # this is returned to the calling application: Plack::Util::inline_object(write => $write, close => $close);