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 HTTP::Parser::XS qw(parse_http_request); # supports pure Perl fallback
+use Plack::HTTPParser qw(parse_http_request); # XS or pure Perl
use HTTP::Status qw(status_message);
use HTTP::Date qw(time2str);
use IO::File;
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...
+our $MAX_REQUEST_BUFFER = $ENV{GIT_HTTP_MAX_REQUEST_BUFFER} ||
+ (10 * 1024 * 1024);
+
my $null_io = IO::File->new('/dev/null', '<');
my $http_date;
my $prev = 0;
return recv_err($self, $r, $len) unless $r;
# continue looping if $r > 0;
}
- app_dispatch($self);
+ app_dispatch($self, $input);
}
-sub app_dispatch ($) {
- my ($self) = @_;
+sub app_dispatch {
+ my ($self, $input) = @_;
$self->watch_read(0);
my $env = $self->{env};
$env->{REMOTE_ADDR} = $self->{remote_addr};
$host =~ s/:(\d+)\z// and $env->{SERVER_PORT} = $1;
$env->{SERVER_NAME} = $host;
}
-
- sysseek($env->{'psgi.input'}, 0, SEEK_SET) or
+ if (defined $input) {
+ sysseek($input, 0, SEEK_SET) or
die "BUG: psgi.input seek failed: $!";
-
+ }
+ # note: NOT $self->{sock}, we want our close (+ Danga::Socket::close),
+ # to do proper cleanup:
+ $env->{'psgix.io'} = $self; # only for ->close
my $res = Plack::Util::run_app($self->{httpd}->{app}, $env);
eval {
if (ref($res) eq 'CODE') {
if ($k =~ /\ATransfer-Encoding\z/i && $v =~ /\bchunked\b/i) {
$chunked = 1;
}
-
$h .= "$k: $v\r\n";
}
my $conn = $env->{HTTP_CONNECTION} || '';
- my $alive = (defined($len) || $chunked) &&
- ($proto eq 'HTTP/1.1' && $conn !~ /\bclose\b/i) ||
- ($conn =~ /\bkeep-alive\b/i);
+ 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";
} else {
$self->write($h);
}
- ($alive, $chunked);
+ $alive;
}
sub response_write {
my ($self, $env, $res) = @_;
- my ($alive, $chunked) = response_header_write($self, $env, $res);
- my $write = sub { $self->write($_[0]) };
+ my $alive = response_header_write($self, $env, $res);
+
+ # middlewares such as Deflater may write empty strings
+ my $write = sub { $self->write(\($_[0])) if $_[0] ne '' };
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
+ $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);
$self->write($_[1]);
}
+my $pipelineq = [];
+my $next_tick;
+sub process_pipelineq () {
+ $next_tick = undef;
+ my $q = $pipelineq;
+ $pipelineq = [];
+ rbuf_process($_) foreach @$q;
+}
+
# overrides existing Danga::Socket method
sub event_write {
my ($self) = @_;
if ($self->{rbuf} eq '') { # wait for next request
$self->watch_read(1);
} else { # avoid recursion for pipelined requests
- Danga::Socket->AddTimer(0, sub { rbuf_process($self) });
+ push @$pipelineq, $self;
+ $next_tick ||= Danga::Socket->AddTimer(0, *process_pipelineq);
}
}
my $input = $null_io;
my $len = $env->{CONTENT_LENGTH};
if ($len) {
+ if ($len > $MAX_REQUEST_BUFFER) {
+ quit($self, 413);
+ return;
+ }
$input = IO::File->new_tmpfile;
} elsif (env_chunked($env)) {
$len = CHUNK_START;
# TODO: expire idle clients on ENFILE / EMFILE
return unless $input;
- binmode $input;
$env->{'psgi.input'} = $input;
$self->{env} = $env;
$self->{input_left} = $len || 0;
sub write_err {
my ($self) = @_;
- my $err = $self->{env}->{'psgi.errors'};
+ my $err = $self->{httpd}->{env}->{'psgi.errors'};
my $msg = $! || '(zero write)';
$err->print("error buffering to input: $msg\n");
quit($self, 500);
$self->{input_left} = $len;
return;
}
- my $err = $self->{env}->{'psgi.errors'};
+ my $err = $self->{httpd}->{env}->{'psgi.errors'};
$err->print("error reading for input: $! ($len bytes remaining)\n");
quit($self, 500);
}
while (1) { # chunk start
if ($len == CHUNK_ZEND) {
- return app_dispatch($self) if $$rbuf =~ s/\A\r\n//s;
+ $$rbuf =~ s/\A\r\n//s and
+ return app_dispatch($self, $input);
return quit($self, 400) if length($$rbuf) > 2;
}
if ($len == CHUNK_END) {
if ($len == CHUNK_START) {
if ($$rbuf =~ s/\A([a-f0-9]+).*?\r\n//i) {
$len = hex $1;
+ if (($len + -s $input) > $MAX_REQUEST_BUFFER) {
+ return quit($self, 413);
+ }
} elsif (length($$rbuf) > CHUNK_MAX_HDR) {
return quit($self, 400);
}
sub event_hup { $_[0]->close }
sub event_err { $_[0]->close }
+sub close {
+ my $self = shift;
+ my $forward = $self->{forward};
+ $forward->close if $forward;
+ $self->{forward} = $self->{env} = undef;
+ $self->SUPER::close(@_);
+}
+
# for graceful shutdown in PublicInbox::Daemon:
sub busy () {
my ($self) = @_;