use base qw(Danga::Socket);
use fields qw(httpd env rbuf input_left remote_addr remote_port);
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,
};
+# 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
$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;
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;
+ $self->{env} = undef;
+ $self->SUPER::close(@_);
+}
+
# for graceful shutdown in PublicInbox::Daemon:
sub busy () {
my ($self) = @_;