basic support for delayed writes/async with docs
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Engine.pm
index 67e4dab..3b7c7fa 100644 (file)
@@ -54,7 +54,22 @@ See L<Catalyst>.
 
 =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.
+
+By default we do not close the writer object in case we are in an event loop
+and there is deferred activity.  However if you have some sloppy code that is
+closing over an unweakened context ($c) this could lead to the writer NEVER
+being closed.  In versions of Catalyst 5.90030 and older, we used to forcibly
+close the writer in this method, but we no longer do that since it prevented us
+from introducing proper asynchronous support in Catalyst core.  If you have old
+code that is leaking context but was otherwise working and you don't want to fix
+your memory leaks (is really the best idea) you can force enable the old
+behavior (and lose asynchronous support) by setting the global configuration key
+C<aggressively_close_writer_on_finalize_body> to true.  See L<Catalyst::Upgrading>
+for more if you have this issue.
 
 =cut
 
@@ -75,9 +90,11 @@ sub finalize_body {
         $self->write( $c, $body );
     }
 
-    my $res = $c->response;
-    $res->_writer->close;
-    $res->_clear_writer;
+    if($c->config->{aggressively_close_writer_on_finalize_body}) {
+      my $res = $c->response;
+      $res->_writer->close;
+      $res->_clear_writer;
+    }
 
     return;
 }
@@ -181,7 +198,6 @@ sub finalize_error {
         $name  = "<h1>$name</h1>";
 
         # 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 +348,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 +411,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 +541,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 +560,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});
@@ -592,6 +612,18 @@ sub prepare_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])
 
 Reads from the input stream by calling C<< $self->read_chunk >>.
@@ -619,15 +651,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 +690,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 +702,12 @@ sub build_psgi_app {
 
         return sub {
             my ($respond) = @_;
+            confess("Did not get a response callback for writer, cannot continiue") 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 +750,6 @@ the same terms as Perl itself.
 
 =cut
 
+__PACKAGE__->meta->make_immutable;
+
 1;