X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FEngine.pm;h=9f97a42859185782ddd51926d48248cc09899b9c;hb=191665f3c88f4b1f81e4198717077ac08d06bdb7;hp=67e4dab5fcc748217a74253521a8ea980be9e5ce;hpb=9c4288eaedee0bba8e4a123b4664bc07a4b5c24c;p=catagits%2FCatalyst-Runtime.git diff --git a/lib/Catalyst/Engine.pm b/lib/Catalyst/Engine.pm index 67e4dab..9f97a42 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' ) { @@ -181,7 +191,6 @@ sub finalize_error { $name = "

$name

"; # Don't show context in the dump - $c->req->_clear_context; $c->res->_clear_context; # Don't show body parser in the dump @@ -332,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; } @@ -395,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) @@ -522,7 +534,7 @@ sub prepare_query_parameters { =head2 $self->prepare_read($c) -prepare to read from the engine. +Prepare to read by initializing the Content-Length from headers. =cut @@ -541,6 +553,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}); @@ -558,6 +571,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; @@ -572,9 +586,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 @@ -590,6 +609,20 @@ 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) + +Writes the buffer to the client. + +=cut + +sub write { + my ( $self, $c, $buffer ) = @_; + + $c->response->write($buffer); } =head2 $self->read($c, [$maxlength]) @@ -619,15 +652,6 @@ sub read_chunk { return $ctx->request->read_chunk(@_); } -=head2 $self->read_length - -The length of input data to be read. This is obtained from the Content-Length -header. - -=head2 $self->read_position - -The amount of input data that has already been read. - =head2 $self->run($app, $server) Start the engine. Builds a PSGI application and calls the @@ -667,8 +691,7 @@ sub run { =head2 build_psgi_app ($app, @args) -Builds and returns a PSGI application closure, wrapping it in the reverse proxy -middleware if the using_frontend_proxy config setting is set. +Builds and returns a PSGI application closure. (Raw, not wrapped in middleware) =cut @@ -680,23 +703,12 @@ sub build_psgi_app { return sub { my ($respond) = @_; + confess("Did not get a response callback for writer, cannot continue") unless $respond; $app->handle_request(env => $env, response_cb => $respond); }; }; } -=head2 $self->write($c, $buffer) - -Writes the buffer to the client. - -=cut - -sub write { - my ( $self, $c, $buffer ) = @_; - - $c->response->write($buffer); -} - =head2 $self->unescape_uri($uri) Unescapes a given URI using the most efficient method available. Engines such @@ -739,4 +751,6 @@ the same terms as Perl itself. =cut +__PACKAGE__->meta->make_immutable; + 1;