use strict;
use warnings;
use base qw(Danga::Socket);
-use fields qw(httpd env rbuf input_left remote_addr remote_port);
+use fields qw(httpd env rbuf input_left remote_addr remote_port forward);
use Fcntl qw(:seek);
use Plack::HTTPParser qw(parse_http_request); # XS or pure Perl
use HTTP::Status qw(status_message);
CHUNK_MAX_HDR => 256,
};
+# FIXME: duplicated code with NNTP.pm
+my $WEAKEN = {}; # string(inbox) -> inbox
+my $WEAKTIMER;
+sub weaken_task () {
+ $WEAKTIMER = undef;
+ $_->weaken_all for values %$WEAKEN;
+ $WEAKEN = {};
+}
+
# Use the same configuration parameter as git since this is primarily
# a slow-client sponge for git-http-backend
# TODO: support per-respository http.maxRequestBuffer somehow...
my $conn = $env->{HTTP_CONNECTION} || '';
my $term = defined($len) || $chunked;
- my $alive = $term &&
- (($proto eq 'HTTP/1.1' && $conn !~ /\bclose\b/i) ||
- ($conn =~ /\bkeep-alive\b/i));
-
- $h .= 'Connection: ' . ($alive ? 'keep-alive' : 'close');
- $h .= "\r\nDate: " . http_date() . "\r\n\r\n";
+ my $prot_persist = ($proto eq 'HTTP/1.1') && ($conn !~ /\bclose\b/i);
+ my $alive;
+ if (!$term && $prot_persist) { # auto-chunk
+ $chunked = $alive = 2;
+ $h .= "Transfer-Encoding: chunked\r\n";
+ # no need for "Connection: keep-alive" with HTTP/1.1
+ } elsif ($term && ($prot_persist || ($conn =~ /\bkeep-alive\b/i))) {
+ $alive = 1;
+ $h .= "Connection: keep-alive\r\n";
+ } else {
+ $h .= "Connection: close\r\n";
+ }
+ $h .= 'Date: ' . http_date() . "\r\n\r\n";
if (($len || $chunked) && $env->{REQUEST_METHOD} ne 'HEAD') {
more($self, $h);
$alive;
}
+# middlewares such as Deflater may write empty strings
+sub chunked_wcb ($) {
+ my ($self) = @_;
+ sub {
+ return if $_[0] eq '';
+ more($self, sprintf("%x\r\n", bytes::length($_[0])));
+ more($self, $_[0]);
+ $self->write("\r\n");
+ }
+}
+
+sub identity_wcb ($) {
+ my ($self) = @_;
+ sub { $self->write(\($_[0])) if $_[0] ne '' }
+}
+
sub response_write {
my ($self, $env, $res) = @_;
my $alive = response_header_write($self, $env, $res);
- my $write = sub { $self->write($_[0]) };
+
+ my $write = $alive == 2 ? chunked_wcb($self) : identity_wcb($self);
my $close = sub {
+ $self->write("0\r\n\r\n") if $alive == 2;
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
+ $WEAKEN->{"$obj"} = $obj;
+ $WEAKTIMER ||= Danga::Socket->AddTimer(60, *weaken_task);
}
$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_size}) {
+ $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);
sub close {
my $self = shift;
- $self->{env} = undef;
+ my $forward = $self->{forward};
+ $forward->close if $forward;
+ $self->{forward} = $self->{env} = undef;
$self->SUPER::close(@_);
}
-sub write ($$) : method {
- my PublicInbox::HTTP $self = $_[0];
- return 1 if (defined($_[1]) && ref($_[1]) eq '' && $_[1] eq '');
-
- $self->SUPER::write($_[1]);
-}
-
# for graceful shutdown in PublicInbox::Daemon:
sub busy () {
my ($self) = @_;