Permalink
Browse files

PSGI: don't try to read over the supplied Content-Length if it's known.

This fixes a potential issue with webservers such as uWSGI/PSGI plugin
where input->read is mapped to qa real socket and could block.
  • Loading branch information...
1 parent fce7094 commit b12cf95d4a99287fe167716a467d433e1f54bc6c @miyagawa committed May 5, 2011
Showing with 5 additions and 1 deletion.
  1. +5 −1 lib/Mojo/Server/PSGI.pm
View
6 lib/Mojo/Server/PSGI.pm
@@ -21,10 +21,14 @@ sub run {
$tx->local_port($env->{SERVER_PORT});
# Request body
+ my $cl = $env->{CONTENT_LENGTH};
while (!$req->is_done) {
- my $read = $env->{'psgi.input'}->read(my $buffer, CHUNK_SIZE, 0);
+ my $chunk = ($cl && $cl < CHUNK_SIZE) ? $cl : CHUNK_SIZE;
+ my $read = $env->{'psgi.input'}->read(my $buffer, $chunk, 0);
last unless $read;
$req->parse($buffer);
+ $cl -= $read;
+ last if $cl <= 0;
}
# Handle

0 comments on commit b12cf95

Please sign in to comment.