line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
=head1 NAME |
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
Dancer::Plugin::BeforeRoute - A before hook for a specify route or routes |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
=head1 USAGE |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
use Dancer::Plugin::BeforeRoute; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
before_route get => "/", sub { |
11
|
|
|
|
|
|
|
var before_run => "homepage"; |
12
|
|
|
|
|
|
|
}; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
before_route ["get", "post"] => "/", sub { |
15
|
|
|
|
|
|
|
var before_run => "homepage"; |
16
|
|
|
|
|
|
|
}; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
get "/" => sub { |
19
|
|
|
|
|
|
|
## Return "homepage" |
20
|
|
|
|
|
|
|
return var "before_run"; |
21
|
|
|
|
|
|
|
}; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
before_route get => "/foo", sub { |
24
|
|
|
|
|
|
|
var before_run => "foo" |
25
|
|
|
|
|
|
|
}; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
get "/foo" => sub { |
28
|
|
|
|
|
|
|
## Return "foo" |
29
|
|
|
|
|
|
|
return var "before_run"; |
30
|
|
|
|
|
|
|
}; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
before_template_route |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=head1 DESCRIPTION |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
Dancer provides hook before to do everythings before going any route. |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
This plugin is to provide a little bit more specifically hook before route or route(s) executed. |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
=head1 AUTHOR |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
Michael Vu, C<< >> |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
=head1 BUGS |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
Please report any bugs or feature requests to C, or through |
47
|
|
|
|
|
|
|
the web interface at L. I will be notified, and then you'll |
48
|
|
|
|
|
|
|
automatically be notified of progress on your bug as I make changes. |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
=head1 SUPPORT |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
perldoc Dancer::Plugin::BeforeRoute |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
You can also look for information at: |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=over 4 |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
L |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
L |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=item * CPAN Ratings |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
L |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=item * Search CPAN |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
L |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=item * GIT Respority |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
L |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=back |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
85
|
|
|
|
|
|
|
under the same terms as Perl itself. |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=cut |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
package Dancer::Plugin::BeforeRoute; |
90
|
|
|
|
|
|
|
$Dancer::Plugin::BeforeRoute::VERSION = '1.0'; |
91
|
2
|
|
|
2
|
|
21431
|
use Carp "confess"; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
148
|
|
92
|
2
|
|
|
2
|
|
1799
|
use Dancer ":syntax"; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
use Dancer::Plugin; |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
my @ROUTES = (); |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
hook before => sub { |
98
|
|
|
|
|
|
|
ROUTE: |
99
|
|
|
|
|
|
|
foreach my $route (@ROUTES) { |
100
|
|
|
|
|
|
|
next ROUTE if !request_for( $route->{path}, @{ $route->{methods} } ); |
101
|
|
|
|
|
|
|
$route->{subref}->(); |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
}; |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
register before_route => sub { |
106
|
|
|
|
|
|
|
my ( $path, $subref, @methods ) = _args(@_); |
107
|
|
|
|
|
|
|
push @ROUTES, |
108
|
|
|
|
|
|
|
{ |
109
|
|
|
|
|
|
|
methods => \@methods, |
110
|
|
|
|
|
|
|
path => $path, |
111
|
|
|
|
|
|
|
subref => $subref, |
112
|
|
|
|
|
|
|
}; |
113
|
|
|
|
|
|
|
}; |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
register request_for => sub { |
116
|
|
|
|
|
|
|
my ( $path, @methods ) = @_; |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
my $request_method = request->method; |
119
|
|
|
|
|
|
|
my $request_path = request->path_info; |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
# Shared portion of the log message, |
122
|
|
|
|
|
|
|
# for both when the request matches and when it does not. |
123
|
|
|
|
|
|
|
my $match_message = q{}; |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
grep { |
126
|
|
|
|
|
|
|
$match_message = "'$request_method $request_path' against '$_ $path'"; |
127
|
|
|
|
|
|
|
# Mismatches only logged at debug to remove excessive log volume. |
128
|
|
|
|
|
|
|
debug "Trying to match $match_message" |
129
|
|
|
|
|
|
|
} @methods; |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
if ( !_is_the_right_method( $request_method, @methods ) ) { |
132
|
|
|
|
|
|
|
return; |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
if ( !_is_the_right_path( $request_path, $path ) ) { |
136
|
|
|
|
|
|
|
return; |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
info "Matched $match_message --> got 1"; |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
return 1; |
142
|
|
|
|
|
|
|
}; |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
sub _args { |
145
|
|
|
|
|
|
|
my $methods = shift |
146
|
|
|
|
|
|
|
or confess "dev: missing method\n"; |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
my @methods = ref $methods ? @$methods : ($methods); |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
my $path = shift |
151
|
|
|
|
|
|
|
or confess "dev: missing path\n"; |
152
|
|
|
|
|
|
|
my $subref = shift |
153
|
|
|
|
|
|
|
or confess "dev: missing a subref -> [[ @methods: $path ]]\n"; |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
return ( $path, $subref, @methods ); |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
sub _is_the_right_method { |
159
|
|
|
|
|
|
|
my $method = shift; |
160
|
|
|
|
|
|
|
if ( $method eq "HEAD" ) { |
161
|
|
|
|
|
|
|
$method = "get"; |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
my @methods = @_; |
164
|
|
|
|
|
|
|
return ( grep { ( lc($_) eq "any" ) || /^\Q$method\E$/i } @methods ) |
165
|
|
|
|
|
|
|
? 1 |
166
|
|
|
|
|
|
|
: 0; |
167
|
|
|
|
|
|
|
} |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
sub _is_the_right_path { |
170
|
|
|
|
|
|
|
my $got_path = shift; |
171
|
|
|
|
|
|
|
my $expected_path = shift; |
172
|
|
|
|
|
|
|
if ( ref $expected_path ) { |
173
|
|
|
|
|
|
|
return $got_path =~ /$expected_path/ ? 1 : 0; |
174
|
|
|
|
|
|
|
} |
175
|
|
|
|
|
|
|
if ( $expected_path =~ /\:/ ) { |
176
|
|
|
|
|
|
|
$expected_path =~ s/\:[^\/]+/[^\/]+/g; |
177
|
|
|
|
|
|
|
return $got_path =~ /$expected_path/ ? 1 : 0; |
178
|
|
|
|
|
|
|
} |
179
|
|
|
|
|
|
|
return $got_path eq $expected_path ? 1 : 0; |
180
|
|
|
|
|
|
|
} |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
register_plugin; |