X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FEngine.pm;h=2a1ed8728f64399da17988528f3cbd2e24e90836;hb=c368f69e3b35bea9adeb69c128fcf176b7a539e4;hp=4781e45300875b7021fac6a25f84a2f144111a61;hpb=258733f15e1e1ec4b4d92eda4b4471833890aced;p=catagits%2FCatalyst-Runtime.git diff --git a/lib/Catalyst/Engine.pm b/lib/Catalyst/Engine.pm index 4781e45..2a1ed87 100644 --- a/lib/Catalyst/Engine.pm +++ b/lib/Catalyst/Engine.pm @@ -13,6 +13,8 @@ use URI::QueryParam; use Plack::Loader; use Catalyst::EngineLoader; use Encode (); +use Plack::Request::Upload; +use Hash::MultiValue; use utf8; use namespace::clean -except => 'meta'; @@ -54,12 +56,20 @@ See L. =head2 $self->finalize_body($c) -Finalize body. Prints the response output. +Finalize body. Prints the response output as blocking stream if it looks like +a filehandle, otherwise write it out all in one go. If there is no body in +the response, we assume you are handling it 'manually', such as for nonblocking +style or asynchronous streaming responses. You do this by calling L<\write> +several times (which sends HTTP headers if needed) or you close over C<$response->write_fh>. + +See L and L for more. =cut sub finalize_body { my ( $self, $c ) = @_; + return if $c->response->has_write_fh; + my $body = $c->response->body; no warnings 'uninitialized'; if ( blessed($body) && $body->can('read') or ref($body) eq 'GLOB' ) { @@ -331,7 +341,7 @@ Allows engines to write headers to response sub finalize_headers { my ($self, $ctx) = @_; - $ctx->response->finalize_headers; + $ctx->finalize_headers unless $ctx->response->finalized_headers; return; } @@ -394,14 +404,17 @@ sub prepare_body_parameters { =head2 $self->prepare_parameters($c) -sets up parameters from query and post parameters. +Sets up parameters from query and post parameters. +If parameters have already been set up will clear +existing parameters and set up again. =cut sub prepare_parameters { my ( $self, $c ) = @_; - $c->request->parameters; + $c->request->_clear_parameters; + return $c->request->parameters; } =head2 $self->prepare_path($c) @@ -476,8 +489,13 @@ process the query string and extract query parameters. sub prepare_query_parameters { my ($self, $c) = @_; - my $env = $c->request->env; + + if(my $query_obj = $env->{'plack.request.query'}) { + $c->request->query_parameters($query_obj->as_hashref_mixed); + return; + } + my $query_string = exists $env->{QUERY_STRING} ? $env->{QUERY_STRING} : ''; @@ -485,7 +503,11 @@ sub prepare_query_parameters { # Check for keywords (no = signs) # (yes, index() is faster than a regex :)) if ( index( $query_string, '=' ) < 0 ) { - $c->request->query_keywords( $self->unescape_uri($query_string) ); + $c->request->query_keywords($self->unescape_uri($query_string)); + $env->{'plack.request.query'} ||= Hash::MultiValue->new( + map { (URI::Escape::uri_unescape($_), '') } + split(/\+/, $query_string, -1)); + return; } @@ -516,6 +538,8 @@ sub prepare_query_parameters { $query{$param} = $value; } } + + $env->{'plack.request.query'} ||= Hash::MultiValue->from_mixed(\%query); $c->request->query_parameters( \%query ); } @@ -540,6 +564,7 @@ Populate the context object from the request object. sub prepare_request { my ($self, $ctx, %args) = @_; + $ctx->log->psgienv($args{env}) if $ctx->log->can('psgienv'); $ctx->request->_set_env($args{env}); $self->_set_env($args{env}); # Nasty back compat! $ctx->response->_set_response_cb($args{response_cb}); @@ -557,6 +582,7 @@ sub prepare_uploads { my $uploads = $request->_body->upload; my $parameters = $request->parameters; + my @plack_uploads; foreach my $name (keys %$uploads) { my $files = $uploads->{$name}; my @uploads; @@ -571,9 +597,14 @@ sub prepare_uploads { filename => $upload->{filename}, ); push @uploads, $u; + + # Plack compatibility. + my %copy = (%$upload, headers=>$headers); + push @plack_uploads, $name, Plack::Request::Upload->new(%copy); } $request->uploads->{$name} = @uploads > 1 ? \@uploads : $uploads[0]; + # support access to the filename as a normal param my @filenames = map { $_->{filename} } @uploads; # append, if there's already params with this name @@ -589,6 +620,8 @@ sub prepare_uploads { $parameters->{$name} = @filenames > 1 ? \@filenames : $filenames[0]; } } + + $self->env->{'plack.request.upload'} ||= Hash::MultiValue->new(@plack_uploads); } =head2 $self->write($c, $buffer) @@ -681,7 +714,7 @@ sub build_psgi_app { return sub { my ($respond) = @_; - confess("Did not get a response callback for writer, cannot continiue") unless $respond; + confess("Did not get a response callback for writer, cannot continue") unless $respond; $app->handle_request(env => $env, response_cb => $respond); }; }; @@ -729,4 +762,6 @@ the same terms as Perl itself. =cut +__PACKAGE__->meta->make_immutable; + 1;