lib/PublicInbox/HTTP.pm | 16 +++++++++++++--- diff --git a/lib/PublicInbox/HTTP.pm b/lib/PublicInbox/HTTP.pm index aaae7ab5c41424c10dd98a83cf7c56e68a3db2cc..17e7447545ca145a262bc055d60c338dcbcf050a 100644 --- a/lib/PublicInbox/HTTP.pm +++ b/lib/PublicInbox/HTTP.pm @@ -71,7 +71,10 @@ # (they are rarely-used and git (as of 2.7.2) does not use them) return quit($self, 400) if $r == -1 || $env{HTTP_TRAILER}; return $self->watch_read(1) if $r < 0; # incomplete $self->{rbuf} = substr($self->{rbuf}, $r); + my $len = input_prepare($self, \%env); + defined $len or return write_err($self); # EMFILE/ENFILE + $len ? event_read_input($self) : app_dispatch($self); } @@ -116,7 +119,10 @@ if (my $host = $env->{HTTP_HOST}) { $host =~ s/:(\d+)\z// and $env->{SERVER_PORT} = $1; $env->{SERVER_NAME} = $host; } - sysseek($env->{'psgi.input'}, 0, SEEK_SET) or die "input seek failed: $!"; + + sysseek($env->{'psgi.input'}, 0, SEEK_SET) or + die "BUG: psgi.input seek failed: $!"; + my $res = Plack::Util::run_app($self->{httpd}->{app}, $env); eval { if (ref($res) eq 'CODE') { @@ -222,13 +228,17 @@ my $len = $env->{CONTENT_LENGTH}; if ($len) { $input = IO::File->new_tmpfile; } elsif (env_chunked($env)) { - $input = IO::File->new_tmpfile; $len = CHUNK_START; + $input = IO::File->new_tmpfile; } + + # TODO: expire idle clients on ENFILE / EMFILE + return unless $input; + binmode $input; $env->{'psgi.input'} = $input; $self->{env} = $env; - $self->{input_left} = $len; + $self->{input_left} = $len || 0; } sub env_chunked { ($_[0]->{HTTP_TRANSFER_ENCODING} || '') =~ /\bchunked\b/i }