outline for PSGI middleware
[catagits/Catalyst-Runtime.git] / lib / Catalyst.pm
index 0dd64a0..d0c2f27 100644 (file)
@@ -113,7 +113,7 @@ __PACKAGE__->stats_class('Catalyst::Stats');
 
 # Remember to update this in Catalyst::Runtime as well!
 
-our $VERSION = '5.90030';
+our $VERSION = '5.90042';
 
 sub import {
     my ( $class, @arguments ) = @_;
@@ -1793,6 +1793,14 @@ sub finalize {
         $c->log->error($error);
     }
 
+    # Support skipping finalize for psgix.io style 'jailbreak'.  Used to support
+    # stuff like cometd and websockets
+    
+    if($c->request->has_io_fh) {
+      $c->log_response;
+      return;
+    }
+
     # Allow engine to handle finalize flow (for POE)
     my $engine = $c->engine;
     if ( my $code = $engine->can('finalize') ) {
@@ -1820,7 +1828,7 @@ sub finalize {
     $c->log_response;
 
     if ($c->use_stats) {
-        my $elapsed = sprintf '%f', $c->stats->elapsed;
+        my $elapsed = $c->stats->elapsed;
         my $av = $elapsed == 0 ? '??' : sprintf '%.3f', 1 / $elapsed;
         $c->log->info(
             "Request took ${elapsed}s ($av/s)\n" . $c->stats->report . "\n" );
@@ -2718,6 +2726,11 @@ sub setup_engine {
     return;
 }
 
+## This exists just to supply a prebuild psgi app for mod_perl and for the 
+## build in server support (back compat support for pre psgi port behavior).
+## This is so that we don't build a new psgi app for each request when using
+## the mod_perl handler or the built in servers (http and fcgi, etc).
+
 sub _finalized_psgi_app {
     my ($app) = @_;
 
@@ -2729,6 +2742,12 @@ sub _finalized_psgi_app {
     return $app->_psgi_app;
 }
 
+## Look for a psgi file like 'myapp_web.psgi' (if the app is MyApp::Web) in the
+## home directory and load that and return it (just assume it is doing the 
+## right thing :) ).  If that does not exist, call $app->psgi_app, wrap that
+## in default_middleware and return it ( this is for backward compatibility
+## with pre psgi port behavior ).
+
 sub _setup_psgi_app {
     my ($app) = @_;
 
@@ -2983,13 +3002,17 @@ the plugin name does not begin with C<Catalyst::Plugin::>.
         $plugins = [ grep {
             m/Unicode::Encoding/ ? do {
                 $class->log->warn(
-                    'Unicode::Encoding plugin is now part of core,'
+                    'Unicode::Encoding plugin is auto-applied,'
                     . ' please remove this from your appclass'
+                    . ' and make sure to define "encoding" config'
                 );
+                unless (exists $class->config->{'encoding'}) {
+                  $class->config->{'encoding'} = 'UTF-8';
+                }
                 () }
                 : $_
         } @$plugins ];
-        unshift @$plugins, $class->_default_plugins;
+        push @$plugins, $class->_default_plugins;
         $plugins = Data::OptList::mkopt($plugins || []);
 
         my @plugins = map {
@@ -3021,6 +3044,27 @@ the plugin name does not begin with C<Catalyst::Plugin::>.
     }
 }
 
+sub setup_middleware { }
+
+our @registered_middlewares = ();
+
+## A normalized read only list of PSGI Middleware
+sub registered_middlewares { @registered_middlewares }
+
+## Normalize incoming middleware and hold it
+sub register_middleware {
+    my($self, $mw, @args) = @_;
+
+    if (ref $mw ne 'CODE') {
+        my $mw_class = Plack::Util::load_class($mw, 'Plack::Middleware');
+        $mw = sub { $mw_class->wrap($_[0], @args) };
+    }
+
+    push @registered_middlewares, $mw;
+}
+
+sub apply_registered_middleware { }
+
 =head2 $c->stack
 
 Returns an arrayref of the internal execution stack (actions that are
@@ -3181,7 +3225,7 @@ is having paths rewritten into it (e.g. as a .cgi/fcgi in a public_html director
 at other URIs than that which the app is 'normally' based at with C<mod_rewrite>), the resolution of
 C<< $c->request->base >> will be incorrect.
 
-=back
+=back 
 
 =item *
 
@@ -3191,6 +3235,23 @@ C<using_frontend_proxy> - See L</PROXY SUPPORT>.
 
 C<encoding> - See L</ENCODING>
 
+=item *
+
+C<abort_chain_on_error_fix>
+
+When there is an error in an action chain, the default behavior is to continue
+processing the remaining actions and then catch the error upon chain end.  This
+can lead to running actions when the application is in an unexpected state.  If
+you have this issue, setting this config value to true will promptly exit a
+chain when there is an error raised in any action (thus terminating the chain 
+early.)
+
+use like:
+
+    __PACKAGE__->config(abort_chain_on_error_fix => 1);
+
+In the future this might become the default behavior.
+
 =back
 
 =head1 INTERNAL ACTIONS