[Catalyst] Re: Redirects // Re: forwarding to chained actions

A. Pagaltzis pagaltzis at gmx.de
Fri Apr 27 21:30:01 GMT 2007


* Bill Moseley <moseley at hank.org> [2007-04-27 20:55]:
> sub post_redirect {
>     my ( $c, $location ) = @_;
>     my ($version) = $c->request->protocol =~ m/(\d+\.\d+)/;
> 
>     # Make location absolute
>     $location = $c->uri_for( $location )
>         unless $location =~ /http/;
> 
>     $c->res->redirect( $location, ( $version && $version > 1.0 ? 303 : 302 ));
> }

Various nitpickery corrected:

    sub post_redirect {
        my ( $c, $loc ) = @_;

        my $is_rel_uri = $loc !~ m{ :// }x;

        my $is_old_proto = do {
            my $p = $c->request->protocol;
            my ( $maj, $min ) = ( $p =~ m{ \A HTTP / 0* ( \d+ ) \. ( \d+ ) \z }x );
            not $maj or $maj == 1 and $min == 0;
        };

        $c->res->redirect(
            ( $is_rel_uri   ? $c->uri_for( $loc ) : $loc ),
            ( $is_old_proto ? 302 : 303 ),
        );
    }

Main change is because HTTP versions are not floats. (Someone
tell me, when doesn’t comparing version strings properly suck?)

-- 
*AUTOLOAD=*_;sub _{s/(.*)::(.*)/print$2,(",$\/"," ")[defined wantarray]/e;$1}
&Just->another->Perl->hack;
#Aristotle



More information about the Catalyst mailing list