From: Norbert Buchmuller Date: Tue, 7 Jul 2009 21:27:00 +0000 (+0000) Subject: Implemented detach_on_redirect config option. X-Git-Tag: v0.07~13 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Plugin-RequireSSL.git;a=commitdiff_plain;h=794abe2a200320318b087365f8141750a92f0647 Implemented detach_on_redirect config option. --- diff --git a/Changes b/Changes index c34ae97..ad933c3 100644 --- a/Changes +++ b/Changes @@ -4,6 +4,7 @@ Revision history for Perl extension Catalyst::Plugin::RequireSSL - Fix Perl Critic test for hard tabs (t0m) - Fix POD coverage (t0m) - Added allow_ssl() (norbi) + - Added detach_on_redirect config option (norbi) 0.06 2007-03-06 11:00:00 - Added no_cache config option to support wildcard SSL certificates. diff --git a/lib/Catalyst/Plugin/RequireSSL.pm b/lib/Catalyst/Plugin/RequireSSL.pm index 6aafbc4..edf613f 100644 --- a/lib/Catalyst/Plugin/RequireSSL.pm +++ b/lib/Catalyst/Plugin/RequireSSL.pm @@ -21,6 +21,7 @@ sub require_ssl { else { $c->_ssl_strip_output(1); $c->res->redirect( $redir ); + $c->detach if $c->config->{require_ssl}->{detach_on_redirect}; } } } @@ -134,6 +135,7 @@ Catalyst::Plugin::RequireSSL - Force SSL mode on select pages http => 'www.mydomain.com', remain_in_ssl => 0, no_cache => 0, + detach_on_redirect => 1, }; # in any controller methods that should be secured @@ -182,6 +184,13 @@ users will be redirected back to non-SSL mode as soon as possible. If you have a wildcard certificate you will need to set this option if you are using multiple domains on one instance of Catalyst. + detach_on_redirect + +By default C<< $c->require_ssl >> only calls C<< $c->response->redirect >> but +does not stop request processing (so it returns and subsequent statements are +run). This is probably not what you want. If you set this option to a true +value C<< $c->require_ssl >> will call C<< $c->detach >> when it redirects. + =head1 METHODS =head2 require_ssl diff --git a/t/08detach_on_redirect.t b/t/08detach_on_redirect.t new file mode 100644 index 0000000..aec2493 --- /dev/null +++ b/t/08detach_on_redirect.t @@ -0,0 +1,30 @@ +#!perl + +use strict; +use warnings; + +use FindBin; +use lib "$FindBin::Bin/lib"; + +use Test::More tests => 4; +use Catalyst::Test 'TestApp'; + +{ + TestApp->config->{require_ssl}->{detach_on_redirect} = 0; + + # test an SSL redirect + ok( my $res = request('http://localhost/ssl/test_detach'), 'request ok' ); + is( $res->header('location'), 'http://www.mydomain.com/redirect_from_the_action', + 'the action did the redirect after $c->require_ssl' + ); +} + +{ + TestApp->config->{require_ssl}->{detach_on_redirect} = 1; + + # test an SSL redirect + ok( my $res = request('http://localhost/ssl/test_detach'), 'request ok' ); + is( $res->header('location'), 'https://localhost/ssl/test_detach', + 'the action finished in $c->require_ssl' + ); +} diff --git a/t/lib/TestApp/Controller/SSL.pm b/t/lib/TestApp/Controller/SSL.pm index 4d9ebf7..6dae48f 100644 --- a/t/lib/TestApp/Controller/SSL.pm +++ b/t/lib/TestApp/Controller/SSL.pm @@ -25,4 +25,15 @@ sub maybe_secured : Local { $c->res->output( 'Maybe secured' ); } +sub test_detach : Local { + my ( $self, $c ) = @_; + + $c->require_ssl; + + $c->res->redirect('http://www.mydomain.com/redirect_from_the_action'); + + $c->res->output( 'Test detach' ); +} + + 1;