line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Router::Simple; |
2
|
12
|
|
|
12
|
|
36390
|
use strict; |
|
12
|
|
|
|
|
18
|
|
|
12
|
|
|
|
|
389
|
|
3
|
12
|
|
|
12
|
|
50
|
use warnings; |
|
12
|
|
|
|
|
17
|
|
|
12
|
|
|
|
|
289
|
|
4
|
12
|
|
|
12
|
|
287
|
use 5.00800; |
|
12
|
|
|
|
|
36
|
|
|
12
|
|
|
|
|
656
|
|
5
|
|
|
|
|
|
|
our $VERSION = '0.17'; |
6
|
12
|
|
|
12
|
|
4323
|
use Router::Simple::SubMapper; |
|
12
|
|
|
|
|
17
|
|
|
12
|
|
|
|
|
291
|
|
7
|
12
|
|
|
12
|
|
4671
|
use Router::Simple::Route; |
|
12
|
|
|
|
|
28
|
|
|
12
|
|
|
|
|
440
|
|
8
|
12
|
|
|
12
|
|
66
|
use List::Util qw/max/; |
|
12
|
|
|
|
|
19
|
|
|
12
|
|
|
|
|
1109
|
|
9
|
12
|
|
|
12
|
|
55
|
use Carp (); |
|
12
|
|
|
|
|
15
|
|
|
12
|
|
|
|
|
314
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
use Class::Accessor::Lite 0.05 ( |
12
|
12
|
|
|
|
|
70
|
new => 1, |
13
|
|
|
|
|
|
|
ro => [qw(routes directory_slash)], |
14
|
12
|
|
|
12
|
|
50
|
); |
|
12
|
|
|
|
|
185
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
our $_METHOD_NOT_ALLOWED; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub connect { |
19
|
41
|
|
|
41
|
1
|
368
|
my $self = shift; |
20
|
|
|
|
|
|
|
|
21
|
41
|
100
|
|
|
|
145
|
if ($self->{directory_slash}) { |
22
|
|
|
|
|
|
|
# connect([$name, ]$pattern[, \%dest[, \%opt]]) |
23
|
4
|
100
|
100
|
|
|
26
|
if (@_ == 1 || ref $_[1]) { |
24
|
3
|
|
|
|
|
8
|
unshift(@_, undef); |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
# \%opt |
28
|
4
|
|
100
|
|
|
12
|
$_[3] ||= {}; |
29
|
4
|
|
|
|
|
11
|
$_[3]->{directory_slash} = 1; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
41
|
|
|
|
|
174
|
my $route = Router::Simple::Route->new(@_); |
33
|
41
|
|
|
|
|
40
|
push @{ $self->{routes} }, $route; |
|
41
|
|
|
|
|
83
|
|
34
|
41
|
|
|
|
|
80
|
return $self; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub submapper { |
38
|
4
|
|
|
4
|
1
|
42
|
my ($self, $pattern, $dest, $opt) = @_; |
39
|
4
|
|
100
|
|
|
82
|
return Router::Simple::SubMapper->new( |
|
|
|
50
|
|
|
|
|
40
|
|
|
|
|
|
|
parent => $self, |
41
|
|
|
|
|
|
|
pattern => $pattern, |
42
|
|
|
|
|
|
|
dest => $dest || +{}, |
43
|
|
|
|
|
|
|
opt => $opt || +{}, |
44
|
|
|
|
|
|
|
); |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub _match { |
48
|
44
|
|
|
44
|
|
56
|
my ($self, $env) = @_; |
49
|
|
|
|
|
|
|
|
50
|
44
|
100
|
|
|
|
107
|
if (ref $env) { |
51
|
|
|
|
|
|
|
# "I think there was a discussion about that a while ago and it is up to apps to deal with empty PATH_INFO as root / iirc" |
52
|
|
|
|
|
|
|
# -- by @miyagawa |
53
|
|
|
|
|
|
|
# |
54
|
|
|
|
|
|
|
# see http://blog.64p.org/entry/2012/10/05/132354 |
55
|
37
|
100
|
|
|
|
112
|
if ($env->{PATH_INFO} eq '') { |
56
|
1
|
|
|
|
|
4
|
$env->{PATH_INFO} = '/'; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
} else { |
59
|
7
|
|
|
|
|
16
|
$env = +{ PATH_INFO => $env } |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
44
|
|
|
|
|
46
|
local $_METHOD_NOT_ALLOWED; |
63
|
44
|
|
|
|
|
68
|
$self->{method_not_allowed} = 0; |
64
|
44
|
|
|
|
|
45
|
for my $route (@{$self->{routes}}) { |
|
44
|
|
|
|
|
97
|
|
65
|
123
|
|
|
|
|
264
|
my $match = $route->match($env); |
66
|
123
|
100
|
|
|
|
299
|
return ($match, $route) if $match; |
67
|
|
|
|
|
|
|
} |
68
|
9
|
|
|
|
|
20
|
$self->{method_not_allowed} = $_METHOD_NOT_ALLOWED; |
69
|
9
|
|
|
|
|
18
|
return undef; # not matched. |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub method_not_allowed { |
73
|
5
|
|
|
5
|
1
|
1311
|
my $self = shift; |
74
|
5
|
|
|
|
|
20
|
$self->{method_not_allowed}; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub match { |
78
|
43
|
|
|
43
|
1
|
13096
|
my ($self, $req) = @_; |
79
|
43
|
|
|
|
|
102
|
my ($match) = $self->_match($req); |
80
|
43
|
|
|
|
|
127
|
return $match; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub routematch { |
84
|
1
|
|
|
1
|
1
|
13
|
my ($self, $req) = @_; |
85
|
1
|
|
|
|
|
4
|
return $self->_match($req); |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub as_string { |
89
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
90
|
|
|
|
|
|
|
|
91
|
0
|
0
|
|
|
|
|
my $mn = max(map { $_->{name} ? length($_->{name}) : 0 } @{$self->{routes}}); |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
92
|
0
|
0
|
|
|
|
|
my $nn = max(map { $_->{method} ? length(join(",",@{$_->{method}})) : 0 } @{$self->{routes}}); |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
return join('', map { |
95
|
0
|
|
0
|
|
|
|
sprintf "%-${mn}s %-${nn}s %s\n", $_->{name}||'', join(',', @{$_->{method} || []}) || '', $_->{pattern} |
|
0
|
|
0
|
|
|
|
|
96
|
0
|
|
|
|
|
|
} @{$self->{routes}}) . "\n"; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
1; |
100
|
|
|
|
|
|
|
__END__ |