| 1 | package Catalyst::Plugin::Redirect; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | |||||||||||||||
| 5 | 1 | our $VERSION = '0.02'; | |||||||||||||
| 6 | |||||||||||||||
| 7 | 1 | my($Revision) = '$Id: Redirect.pm,v 1.4 2006/01/07 13:44:47 Sho Exp $'; | |||||||||||||
| 8 | |||||||||||||||
| 9 | |||||||||||||||
| 10 | |||||||||||||||
| 11 | =head1 NAME | ||||||||||||||
| 12 | |||||||||||||||
| 13 | Catalyst::Plugin::Redirect - Redirect for Catalyst used easily is offered. | ||||||||||||||
| 14 | |||||||||||||||
| 15 | =head1 SYNOPSIS | ||||||||||||||
| 16 | |||||||||||||||
| 17 | use Catalyst 'Redirect'; | ||||||||||||||
| 18 | |||||||||||||||
| 19 | $c->get_baseurl; | ||||||||||||||
| 20 | |||||||||||||||
| 21 | $c->redirect('redirect_url'); | ||||||||||||||
| 22 | $c->redirect('/redirect_url'); | ||||||||||||||
| 23 | $c->redirect('http://www.perl.org/'); | ||||||||||||||
| 24 | |||||||||||||||
| 25 | |||||||||||||||
| 26 | =head1 DESCRIPTION | ||||||||||||||
| 27 | |||||||||||||||
| 28 | Redirect for Catalyst used easily is offered. | ||||||||||||||
| 29 | |||||||||||||||
| 30 | =head1 METHODS | ||||||||||||||
| 31 | |||||||||||||||
| 32 | =over 2 | ||||||||||||||
| 33 | |||||||||||||||
| 34 | =item get_baseurl | ||||||||||||||
| 35 | |||||||||||||||
| 36 | Basic URL of your application is returned. | ||||||||||||||
| 37 | If your application is executed by "http://myhost/myapp/" | ||||||||||||||
| 38 | it returns "/myapp/" . | ||||||||||||||
| 39 | |||||||||||||||
| 40 | =back | ||||||||||||||
| 41 | |||||||||||||||
| 42 | =cut | ||||||||||||||
| 43 | |||||||||||||||
| 44 | 0 | ▶ | sub get_baseurl { | ||||||||||||
|
Subroutine
get_baseurl: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 45 | 0 | my $c = shift; | |||||||||||||
| 46 | 0 | my $base = $c->req->base; | |||||||||||||
| 47 | 0 | my $host = $c->req->base->host; | |||||||||||||
| 48 | 0 | my $port = $c->req->base->port; | |||||||||||||
| 49 | 0 | $base =~ s!^https?://$host:$port!!; | |||||||||||||
| 50 | 0 | $base =~ s!^https?://$host!!; | |||||||||||||
| 51 | 0 | return $base; | |||||||||||||
| 52 | } | ||||||||||||||
| 53 | |||||||||||||||
| 54 | =over 2 | ||||||||||||||
| 55 | |||||||||||||||
| 56 | =item redirect | ||||||||||||||
| 57 | |||||||||||||||
| 58 | $c->redirect('redirect_url'); | ||||||||||||||
| 59 | $c->res->redirect('redirect_url') is executed. | ||||||||||||||
| 60 | |||||||||||||||
| 61 | $c->redirect('/redirect_url'); | ||||||||||||||
| 62 | $c->res->redirect($c->get_baseurl.'redirect_url') is executed. | ||||||||||||||
| 63 | |||||||||||||||
| 64 | $c->redirect('http://www.perl.org/'); | ||||||||||||||
| 65 | $c->res->redirect('http://www.perl.org/') is executed. | ||||||||||||||
| 66 | |||||||||||||||
| 67 | =back | ||||||||||||||
| 68 | |||||||||||||||
| 69 | =cut | ||||||||||||||
| 70 | |||||||||||||||
| 71 | 0 | ▶ | sub redirect { | ||||||||||||
|
Subroutine
redirect: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||
| 72 | 0 | my $c = shift; | |||||||||||||
| 73 | |||||||||||||||
| 74 | 0 | ▶ | if (@_) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 75 | 0 | my $location = shift; | |||||||||||||
| 76 | 0 | ▶ | my $status = shift || 302; | ||||||||||||
|
Condition0%0/2
shift() || 302
MC/DC0%0/1
shift() || 302
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||
| 77 | 0 | ▶ | if ($location =~ m!^https?://!) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 78 | 0 | return $c->res->redirect($location,$status); | |||||||||||||
| 79 | } elsif ($location =~ m!^/!) { | ||||||||||||||
| 80 | 0 | my $base = $c->get_baseurl; | |||||||||||||
| 81 | 0 | $location = $base . $location; | |||||||||||||
| 82 | 0 | $location =~ s!//!/!g; | |||||||||||||
| 83 | 0 | return $c->res->redirect($location, $status); | |||||||||||||
| 84 | } else { | ||||||||||||||
| 85 | 0 | return $c->res->redirect($location,$status); | |||||||||||||
| 86 | } | ||||||||||||||
| 87 | } | ||||||||||||||
| 88 | } | ||||||||||||||
| 89 | |||||||||||||||
| 90 | =BUGS | ||||||||||||||
| 91 | |||||||||||||||
| 92 | When Reverse Proxy is used, get_baseurl returns the backend server's base. | ||||||||||||||
| 93 | For example, "/" will be returned when http://www.mydomain.com/myapp/ is a proxy for http://appserver.local.server/. | ||||||||||||||
| 94 | |||||||||||||||
| 95 | =head1 SEE ALSO | ||||||||||||||
| 96 | |||||||||||||||
| 97 | L<Catalyst> | ||||||||||||||
| 98 | |||||||||||||||
| 99 | =head1 AUTHOR | ||||||||||||||
| 100 | |||||||||||||||
| 101 | Shota Takayama, C<shot[atmark]bindstorm.jp> | ||||||||||||||
| 102 | |||||||||||||||
| 103 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||
| 104 | |||||||||||||||
| 105 | This program is free software, you can redistribute it and/or modify it under | ||||||||||||||
| 106 | the same terms as Perl itself. | ||||||||||||||
| 107 | |||||||||||||||
| 108 | =cut | ||||||||||||||
| 109 | |||||||||||||||
| 110 | |||||||||||||||
| 111 | 1 | 1; | |||||||||||||