File Coverage

blib/lib/Catalyst/Plugin/Redirect.pm
Criterion Covered Total %
statement 3 21 14.2
branch 0 6 0.0
condition 0 2 0.0
subroutine 1 3 33.3
pod 2 2 100.0
total 6 34 17.6


line stmt bran cond sub pod time code
1             package Catalyst::Plugin::Redirect;
2              
3 1     1   13740 use strict;
  1         2  
  1         248  
4              
5             our $VERSION = '0.02';
6              
7             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             sub get_baseurl {
45 0     0 1   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             sub redirect {
72 0     0 1   my $c = shift;
73              
74 0 0         if (@_) {
75 0           my $location = shift;
76 0   0       my $status = shift || 302;
77 0 0         if ($location =~ m!^https?://!) {
    0          
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;