From: Tomas Doran Date: Fri, 28 Oct 2011 09:30:33 +0000 (+0100) Subject: Move actual reading into request X-Git-Tag: 5.90008~16^2~17 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=commitdiff_plain;h=f083854e255c33d3e82befa09152284127d426c1 Move actual reading into request --- diff --git a/lib/Catalyst.pm b/lib/Catalyst.pm index 71a5e56..8cc25d1 100644 --- a/lib/Catalyst.pm +++ b/lib/Catalyst.pm @@ -2405,7 +2405,7 @@ $c->request. You must handle all body parsing yourself. =cut -sub read { my $c = shift; return $c->engine->read( $c, @_ ) } +sub read { my $c = shift; return $c->request->read( @_ ) } =head2 $c->run diff --git a/lib/Catalyst/Engine.pm b/lib/Catalyst/Engine.pm index 9a6c287..7111eec 100644 --- a/lib/Catalyst/Engine.pm +++ b/lib/Catalyst/Engine.pm @@ -713,30 +713,7 @@ Maintains the read_length and read_position counters as data is read. sub read { my ( $self, $c, $maxlength ) = @_; - my $request = $c->request; - my $remaining = $request->_read_length - $request->_read_position; - $maxlength ||= $CHUNKSIZE; - - # Are we done reading? - if ( $remaining <= 0 ) { - return; - } - - my $readlen = ( $remaining > $maxlength ) ? $maxlength : $remaining; - my $rc = $self->read_chunk( $c, my $buffer, $readlen ); - if ( defined $rc ) { - if (0 == $rc) { # Nothing more to read even though Content-Length - # said there should be. - return; - } - my $request = $c->request; - $request->_read_position( $request->_read_position + $rc ); - return $buffer; - } - else { - Catalyst::Exception->throw( - message => "Unknown error reading input: $!" ); - } + $c->request->read($maxlength); } =head2 $self->read_chunk($c, \$buffer, $length) diff --git a/lib/Catalyst/Request.pm b/lib/Catalyst/Request.pm index b712e47..1f7465c 100644 --- a/lib/Catalyst/Request.pm +++ b/lib/Catalyst/Request.pm @@ -50,10 +50,38 @@ has headers => ( has _context => ( is => 'rw', weak_ref => 1, - handles => ['read'], # XXX FIXME! clearer => '_clear_context', ); +# Amount of data to read from input on each pass +our $CHUNKSIZE = 64 * 1024; + +sub read { + my ($self, $maxlength) = @_; + my $remaining = $self->_read_length - $self->_read_position; + $maxlength ||= $CHUNKSIZE; + + # Are we done reading? + if ( $remaining <= 0 ) { + return; + } + + my $readlen = ( $remaining > $maxlength ) ? $maxlength : $remaining; + my $rc = $self->read_chunk( my $buffer, $readlen ); + if ( defined $rc ) { + if (0 == $rc) { # Nothing more to read even though Content-Length + # said there should be. + return; + } + $self->_read_position( $self->_read_position + $rc ); + return $buffer; + } + else { + Catalyst::Exception->throw( + message => "Unknown error reading input: $!" ); + } +} + sub read_chunk { my $self = shift; return $self->env->{'psgi.input'}->read(@_);