Skip to content

Commit

Permalink
fixed redirect to redirect immediately
Browse files Browse the repository at this point in the history
  • Loading branch information
Al Newkirk authored and fcuny committed Nov 8, 2010
1 parent b36b78c commit d7a1d9e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
10 changes: 10 additions & 0 deletions lib/Dancer.pm
Expand Up @@ -662,6 +662,16 @@ You can also force Dancer to return a specific 300-ish HTTP response code:
get '/old/:resource', sub {
redirect '/new/'.params->{resource}, 301;
};
It is important to note that issuing a redirect by itself does not exit and
redirect immediately, redirection is deferred until after the current route
or filter has been processed. To exit and redirect immediately, use the return
function, e.g.
get '/restricted', sub {
return redirect '/login' if accessDenied();
return 'Welcome to the restricted section';
};
=head2 request
Expand Down
8 changes: 8 additions & 0 deletions lib/Dancer/Renderer.pm
Expand Up @@ -118,6 +118,14 @@ sub get_action_response {
return get_action_response();
}

# redirect immediately - skip route execution
if ($Dancer::Response::CURRENT->{status}){
if ($Dancer::Response::CURRENT->{status} == 302 ||
$Dancer::Response::CURRENT->{status} == 301) {
return serialize_response_if_needed(Dancer::Response->current);
}
}

# execute the action
if ($handler) {

Expand Down
3 changes: 2 additions & 1 deletion lib/Dancer/Response.pm
Expand Up @@ -27,7 +27,8 @@ sub new {
}

# a singleton to store the current response
my $CURRENT = Dancer::Response->new();
# made public so status can be checked, etc
our $CURRENT = Dancer::Response->new();

# the accessor returns a copy of the singleton
# after having purged it.
Expand Down

0 comments on commit d7a1d9e

Please sign in to comment.