line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Plack::Middleware::Redirect; |
2
|
1
|
|
|
1
|
|
1167
|
use 5.008001; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
52
|
|
3
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
47
|
|
4
|
1
|
|
|
1
|
|
16
|
use warnings; |
|
1
|
|
|
|
|
7
|
|
|
1
|
|
|
|
|
50
|
|
5
|
1
|
|
|
1
|
|
6
|
use Carp qw/croak/; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
75
|
|
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
495
|
use parent 'Plack::Middleware'; |
|
1
|
|
|
|
|
291
|
|
|
1
|
|
|
|
|
5
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our $VERSION = "0.01"; |
10
|
|
|
|
|
|
|
|
11
|
1
|
|
|
1
|
|
11908
|
use Plack::Util::Accessor qw/url_patterns/; |
|
1
|
|
|
|
|
443
|
|
|
1
|
|
|
|
|
7
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
my $BODY = { |
14
|
|
|
|
|
|
|
301 => "Moved Permanently", |
15
|
|
|
|
|
|
|
302 => "Found" |
16
|
|
|
|
|
|
|
}; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub prepare_app { |
19
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
20
|
0
|
|
|
|
|
|
my $url_patterns = $self->url_patterns; |
21
|
|
|
|
|
|
|
|
22
|
0
|
|
|
|
|
|
for (my $i = 0; $i < scalar(@$url_patterns); $i += 2) { |
23
|
0
|
|
|
|
|
|
my ($from, $to) = _fetch_url_pattern($url_patterns, $i); |
24
|
|
|
|
|
|
|
|
25
|
0
|
|
|
|
|
|
my $type = ref $from; |
26
|
0
|
0
|
0
|
|
|
|
if ($type ne 'Regexp' && $type ne "") { |
27
|
0
|
|
|
|
|
|
croak "$from is invalid parameter"; |
28
|
|
|
|
|
|
|
} |
29
|
0
|
0
|
|
|
|
|
if ($type ne 'Regexp') { |
30
|
0
|
|
|
|
|
|
$url_patterns->[$i] = qr/$from/; |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub call { |
37
|
0
|
|
|
0
|
1
|
|
my ($self, $env) = @_; |
38
|
|
|
|
|
|
|
|
39
|
0
|
|
|
|
|
|
my $url_patterns = $self->url_patterns; |
40
|
|
|
|
|
|
|
|
41
|
0
|
|
|
|
|
|
my $path = $env->{PATH_INFO}; |
42
|
0
|
|
|
|
|
|
my $body = ""; |
43
|
|
|
|
|
|
|
|
44
|
0
|
|
|
|
|
|
for ( my $i = 0; $i < scalar(@$url_patterns); $i += 2 ) { |
45
|
0
|
|
|
|
|
|
my ($from, $to) = _fetch_url_pattern($url_patterns, $i); |
46
|
|
|
|
|
|
|
|
47
|
0
|
0
|
|
|
|
|
next unless $path =~ m#$from#; |
48
|
|
|
|
|
|
|
|
49
|
0
|
|
|
|
|
|
my $type = ref $to; |
50
|
|
|
|
|
|
|
|
51
|
0
|
0
|
|
|
|
|
if ($type ne 'ARRAY') { |
52
|
0
|
|
|
|
|
|
my $to = [$to, 301]; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
0
|
|
|
|
|
|
my ($to_path, $status_code) = @$to; |
56
|
0
|
|
|
|
|
|
$type = ref $to_path; |
57
|
0
|
0
|
|
|
|
|
if ($type eq 'CODE') { |
58
|
0
|
|
|
|
|
|
$path = &$to_path($env, $from); |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
else { |
61
|
0
|
|
|
|
|
|
$path =~ s#$from#"\"$to_path\""#iee; |
|
0
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
} |
63
|
0
|
|
|
|
|
|
my $query = $env->{QUERY_STRING}; |
64
|
0
|
0
|
|
|
|
|
if ($query) { |
65
|
0
|
0
|
|
|
|
|
$path .= $path =~ /\?/ |
66
|
|
|
|
|
|
|
? '&' . $query |
67
|
|
|
|
|
|
|
: '?' . $query; |
68
|
|
|
|
|
|
|
} |
69
|
0
|
|
0
|
|
|
|
my $body = $BODY->{$status_code} || ""; |
70
|
0
|
|
|
|
|
|
return [$status_code, ['Location' => $path], [$body] ]; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
0
|
|
|
|
|
|
return $self->app->($env); |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub _fetch_url_pattern { |
78
|
0
|
|
|
0
|
|
|
my ($url_patterns, $i) = @_; |
79
|
0
|
|
|
|
|
|
return ($url_patterns->[$i], $url_patterns->[$i+1]); |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
1; |
83
|
|
|
|
|
|
|
__END__ |