From: Norbert Buchmuller Date: Tue, 7 Jul 2009 21:39:27 +0000 (+0000) Subject: Cleaned up _redirect_uri() - replaced manual query param processing with manipulation... X-Git-Tag: v0.07~12 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Plugin-RequireSSL.git;a=commitdiff_plain;h=61b3173919e7c140ba0160a57971d48e249c2f46 Cleaned up _redirect_uri() - replaced manual query param processing with manipulation of URI objects. --- diff --git a/Changes b/Changes index ad933c3..16afe3d 100644 --- a/Changes +++ b/Changes @@ -5,6 +5,7 @@ Revision history for Perl extension Catalyst::Plugin::RequireSSL - Fix POD coverage (t0m) - Added allow_ssl() (norbi) - Added detach_on_redirect config option (norbi) + - Cleaned up _redirect_uri() (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 edf613f..e737237 100644 --- a/lib/Catalyst/Plugin/RequireSSL.pm +++ b/lib/Catalyst/Plugin/RequireSSL.pm @@ -81,35 +81,18 @@ sub setup { sub _redirect_uri { my ( $c, $type ) = @_; - # XXX: Cat needs a $c->req->host method... - # until then, strip off the leading protocol from base if ( !$c->config->{require_ssl}->{$type} ) { - my $host = $c->req->base; - $host =~ s/^http(s?):\/\///; - $c->config->{require_ssl}->{$type} = $host; + my $req_uri = $c->req->uri; + $c->config->{require_ssl}->{$type} = + join(':', $req_uri->host, $req_uri->_port); } - if ( $c->config->{require_ssl}->{$type} !~ /\/$/xms ) { - $c->config->{require_ssl}->{$type} .= '/'; - } + $c->config->{require_ssl}->{$type} =~ s/\/+$//; + + my $redir = $c->req->uri->clone; + $redir->scheme($type); + $redir->host_port($c->config->{require_ssl}->{$type}); - my $redir - = $type . '://' . $c->config->{require_ssl}->{$type} . $c->req->path; - - if ( scalar $c->req->param ) { - my @params; - foreach my $arg ( sort keys %{ $c->req->params } ) { - if ( ref $c->req->params->{$arg} ) { - my $list = $c->req->params->{$arg}; - push @params, map { "$arg=" . $_ } sort @{$list}; - } - else { - push @params, "$arg=" . $c->req->params->{$arg}; - } - } - $redir .= '?' . join( '&', @params ); - } - if ( $c->config->{require_ssl}->{no_cache} ) { delete $c->config->{require_ssl}->{$type}; } diff --git a/t/04ssl.t b/t/04ssl.t index 21e05b5..818bb9d 100644 --- a/t/04ssl.t +++ b/t/04ssl.t @@ -17,7 +17,7 @@ is( $res->header('location'), 'https://localhost/ssl/secured', 'redirect uri ok' isnt( $res->content, 'Secured', 'no content displayed on secure page, ok' ); # test redirection params -ok( $res = request('http://localhost/ssl/secured?a=2&a=1&b=3&c=4'), 'request ok' ); +ok( $res = request('http://localhost/ssl/secured?a=1&a=2&b=3&c=4'), 'request ok' ); is( $res->header('location'), 'https://localhost/ssl/secured?a=1&a=2&b=3&c=4', 'redirect with params ok' ); # test that it does not redirect for actions where SSL mode is optional @@ -47,7 +47,7 @@ SKIP: is( $res->header('location'), 'http://localhost/ssl/unsecured', 'redirect uri ok' ); # test redirection params - ok( $res = request('https://localhost/ssl/unsecured?a=2&a=1&b=3&c=4'), 'request ok' ); + ok( $res = request('https://localhost/ssl/unsecured?a=1&a=2&b=3&c=4'), 'request ok' ); is( $res->header('location'), 'http://localhost/ssl/unsecured?a=1&a=2&b=3&c=4', 'redirect with params ok' ); # test that it does not redirect for actions where SSL mode is optional