From: Tomas Doran Date: Fri, 28 Oct 2011 21:24:30 +0000 (+0100) Subject: Move preparing the body into the request, almost works. X-Git-Tag: 5.90008~16^2~16 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=commitdiff_plain;h=398f13dbce1fdbedc6718282fe0f581cb2935798 Move preparing the body into the request, almost works. --- diff --git a/lib/Catalyst.pm b/lib/Catalyst.pm index 8cc25d1..f464d31 100644 --- a/lib/Catalyst.pm +++ b/lib/Catalyst.pm @@ -46,7 +46,16 @@ has state => (is => 'rw', default => 0); has stats => (is => 'rw'); has action => (is => 'rw'); has counter => (is => 'rw', default => sub { {} }); -has request => (is => 'rw', default => sub { $_[0]->request_class->new({}) }, required => 1, lazy => 1); +has request => ( + is => 'rw', + default => sub { + my $self = shift; + my %p; + $p{_uploadtmp} = $self->_uploadtmp if $self->_has_uploadtmp; + $self->request_class->new(\%p); + }, + lazy => 1, +); has response => (is => 'rw', default => sub { $_[0]->response_class->new({}) }, required => 1, lazy => 1); has namespace => (is => 'rw'); @@ -1983,6 +1992,11 @@ etc.). =cut +has _uploadtmp => ( + is => 'ro', + predicate => '_has_uploadtmp', +); + sub prepare { my ( $class, @arguments ) = @_; @@ -1991,7 +2005,8 @@ sub prepare { # into the application. $class->context_class( ref $class || $class ) unless $class->context_class; - my $c = $class->context_class->new({}); + my $uploadtmp = $class->config->{uploadtmp}; + my $c = $class->context_class->new({ $uploadtmp ? (_uploadtmp => $uploadtmp) : ()}); # For on-demand data $c->request->_context($c); diff --git a/lib/Catalyst/Engine.pm b/lib/Catalyst/Engine.pm index 7111eec..e5a5b23 100644 --- a/lib/Catalyst/Engine.pm +++ b/lib/Catalyst/Engine.pm @@ -374,33 +374,7 @@ sets up the L object body using L sub prepare_body { my ( $self, $c ) = @_; - my $appclass = ref($c) || $c; - my $request = $c->request; - if ( my $length = $request->_read_length ) { - unless ( $request->_body ) { - my $type = $request->header('Content-Type'); - $request->_body(HTTP::Body->new( $type, $length )); - $request->_body->cleanup(1); # Make extra sure! - $request->_body->tmpdir( $appclass->config->{uploadtmp} ) - if exists $appclass->config->{uploadtmp}; - } - - # Check for definedness as you could read '0' - while ( defined ( my $buffer = $self->read($c) ) ) { - $c->prepare_body_chunk($buffer); - } - - # paranoia against wrong Content-Length header - my $remaining = $length - $c->request->_read_position; - if ( $remaining > 0 ) { - Catalyst::Exception->throw( - "Wrong Content-Length value: $length" ); - } - } - else { - # Defined but will cause all body code to be skipped - $c->request->_body(0); - } + $c->request->prepare_body; } =head2 $self->prepare_body_chunk($c) @@ -409,10 +383,11 @@ Add a chunk to the request body. =cut +# XXX - Can this be deleted? sub prepare_body_chunk { my ( $self, $c, $chunk ) = @_; - $c->request->_body->add($chunk); + $c->request->prepare_body_chunk($chunk); } =head2 $self->prepare_body_parameters($c) @@ -424,9 +399,7 @@ Sets up parameters from body. sub prepare_body_parameters { my ( $self, $c ) = @_; - return unless $c->request->_body; - - $c->request->body_parameters( $c->request->_body->param ); + $c->request->prepare_body_parameters; } =head2 $self->prepare_connection($c) @@ -494,7 +467,7 @@ sub prepare_parameters { my ( $self, $c ) = @_; my $request = $c->request; - my $parameters = $request->parameters; + my $parameters = {}; my $body_parameters = $request->body_parameters; my $query_parameters = $request->query_parameters; # We copy, no references @@ -512,6 +485,7 @@ sub prepare_parameters { } $parameters->{$name} = @values > 1 ? \@values : $values[0]; } + $request->{parameters} = $parameters; # FIXME } =head2 $self->prepare_path($c) @@ -626,7 +600,6 @@ sub prepare_query_parameters { $query{$param} = $value; } } - $c->request->query_parameters( \%query ); } diff --git a/lib/Catalyst/Request.pm b/lib/Catalyst/Request.pm index 1f7465c..4ff3f10 100644 --- a/lib/Catalyst/Request.pm +++ b/lib/Catalyst/Request.pm @@ -105,6 +105,7 @@ has parameters => ( required => 1, lazy => 1, default => sub { {} }, + predicate => '_has_prepared_parameters', ); # TODO: @@ -115,11 +116,83 @@ has parameters => ( # these lazy build from there and kill all the direct hash access # in Catalyst.pm and Engine.pm? -before $_ => sub { +before parameters => sub { my ($self) = @_; - my $context = $self->_context || return; - $context->prepare_body; -} for qw/parameters body_parameters/; + $self->prepare_body; + $self->_context->engine->prepare_parameters($self->_context); +}; +before body_parameters => sub { + my ($self) = @_; + $self->prepare_body; + $self->prepare_body_parameters; +}; + +=head2 $self->prepare_body() + +sets up the L object body using L + +=cut + +has _uploadtmp => ( + is => 'ro', + predicate => '_has_uploadtmp', +); + +sub prepare_body { + my ( $self ) = @_; + + if ( my $length = $self->_read_length ) { + unless ( $self->_body ) { + my $type = $self->header('Content-Type'); + $self->_body(HTTP::Body->new( $type, $length )); + $self->_body->cleanup(1); # Make extra sure! + $self->_body->tmpdir( $self->_uploadtmp ) + if $self->_has_uploadtmp; + } + + # Check for definedness as you could read '0' + while ( defined ( my $buffer = $self->read() ) ) { + $self->prepare_body_chunk($buffer); + } + + # paranoia against wrong Content-Length header + my $remaining = $length - $self->_read_position; + if ( $remaining > 0 ) { + Catalyst::Exception->throw( + "Wrong Content-Length value: $length" ); + } + } + else { + # Defined but will cause all body code to be skipped + $self->_body(0); + } +} + +=head2 $self->prepare_body_chunk() + +Add a chunk to the request body. + +=cut + +sub prepare_body_chunk { + my ( $self, $chunk ) = @_; + + $self->_body->add($chunk); +} + +=head2 $self->prepare_body_parameters() + +Sets up parameters from body. + +=cut + +sub prepare_body_parameters { + my ( $self ) = @_; + + return unless $self->_body; + + $self->{body_parameters} = $self->_body->param; # FIXME!! Recursion here. +} around parameters => sub { my ($orig, $self, $params) = @_; @@ -153,7 +226,7 @@ has _body => ( # and provide a custom reader.. sub body { my $self = shift; - $self->_context->prepare_body(); + $self->prepare_body(); croak 'body is a reader' if scalar @_; return blessed $self->_body ? $self->_body->body : $self->_body; } diff --git a/t/aggregate/live_engine_request_body_demand.t b/t/aggregate/live_engine_request_body_demand.t index b032f63..5646769 100644 --- a/t/aggregate/live_engine_request_body_demand.t +++ b/t/aggregate/live_engine_request_body_demand.t @@ -59,7 +59,6 @@ SKIP: ok( my $response = request($request), 'Request' ); ok( $response->is_success, 'Response Successful 2xx' ); - { no strict 'refs'; ok(