lib/Dancer/Route.pm

filter: Statementstmt 98.4% 191 / 194 Branchbran 90.2% 83 / 92 Conditioncond 68.1% 15 / 22 MC/DCmcdc 50.0% 7 / 14 Subroutinesub 96.6% 29 / 30 Podpod 9.0% 1 / 11 totaltotal 89.8% 326 / 363 CC 56 SCAR 19.7
CC
56
Cov
94%
run
15.2
match
14.0
init
6.0
CRAP 7.2
« bin/dancer ↑ Summary lib/Dancer/Handler.pm »
1 package Dancer::Route;
2 194 our $AUTHORITY = 'cpan:SUKRIA';
3 # ABSTRACT: Class to represent a route in Dancer
4 194 $Dancer::Route::VERSION = '1.3522';
5 194 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 194 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 194 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 194 ▶use base 'Dancer::Object';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 194 ▶use Dancer::App;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 194 ▶use Dancer::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 194 ▶use Dancer::Config 'setting';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 194 ▶use Dancer::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 194 ▶use Dancer::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 194 ▶use Dancer::Exception qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 194 ▶use Dancer::Factory::Hook;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 194 Dancer::Route->attributes(
19 qw(
20 app
21 method
22 pattern
23 prefix
24 code
25 prev
26 regexp
27 next
28 options
29 match_data
30 )
31 );
32  
33 194 Dancer::Factory::Hook->instance->install_hooks(
34 qw/on_route_exception/
35 );
36  
37 # supported options and aliases
38 194 my @_supported_options = Dancer::Request->get_attributes();
39 194 my %_options_aliases = (agent => 'user_agent');
40  
41 1448 ▶sub init {
Subroutine init: called CC=6 SCAR=17.9
Pod documented
42 1448 my ($self) = @_;
43 1448 $self->{'_compiled_regexp'} = undef;
44  
45 1448 ▶ raise core_route => "cannot create Dancer::Route without a pattern"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->pattern 0 1448 1448
46 unless defined $self->pattern;
47  
48 # If the route is a Regexp, store it directly
49 1448 ▶ $self->regexp($self->pattern)
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->pattern eq "Regexp" 12 1436 1448
50 if ref($self->pattern) eq 'Regexp';
51  
52 1448 $self->check_options();
53 1447 $self->app(Dancer::App->current);
54 1447 ▶ $self->prefix(Dancer::App->current->prefix) if not $self->prefix;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->prefix 1371 76 1447
55 1447 ▶ $self->_init_prefix() if $self->prefix;
Branch: 1 branch
BranchTrueFalseTotal
if $self->prefix 83 1364 1447
56 1447 $self->_build_regexp();
57 1447 ▶ $self->set_previous($self->prev) if $self->prev;
Branch: 1 branch
BranchTrueFalseTotal
if $self->prev 3 1444 1447
58  
59 1447 return $self;
60 }
61  
62 1180 ▶sub set_previous {
Subroutine set_previous: called CC=1 SCAR=0.0
Pod undocumented
63 1180 my ($self, $prev) = @_;
64 1180 $self->prev($prev);
65 1180 $self->prev->{'next'} = $self;
66 1180 return $prev;
67 }
68  
69 651 ▶sub save_match_data {
Subroutine save_match_data: called CC=1 SCAR=0.0
Pod undocumented
70 651 my ($self, $request, $match_data) = @_;
71 651 $self->match_data($match_data);
72 651 $request->_set_route_params($match_data);
73  
74 651 return $match_data;
75 }
76  
77 # Does the route match the request
78 1729 ▶sub match {
Subroutine match: called CC=14 SCAR=26.4
Pod undocumented
79 1729 my ($self, $request) = @_;
80  
81 1729 my $method = lc($request->method);
82 1729 my $path = $request->path_info;
83  
84 Dancer::Logger::core(
85 sprintf "Trying to match '%s %s' against /%s/ (generated from '%s')",
86 1729 $request->method, $path, $self->{_compiled_regexp}, $self->pattern
87 );
88  
89 1729 my @values = $path =~ $self->{_compiled_regexp};
90  
91 1729 ▶ return unless @values;
Branch: 1 branch
BranchTrueFalseTotal
unless @values 1078 651 1729
92  
93 Dancer::Logger::core(
94 " --> got " .
95 651 ▶ map { defined $_ ? $_ : 'undef' } @values
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 700 9 709
96 );
97  
98  
99 # if some named captures found, return captures
100 # no warnings is for perl < 5.10
101 651 ▶ if (my %captures =
Branch: 1 branch
BranchTrueFalseTotal
if (my(%captures) = do { no warnings; %+ }) 2 649 651
102 194 ▶ do { no warnings; %+ }
Subroutine BEGIN: called CC=1 SCAR=0.0
103 )
104 {
105 2 ▶ Dancer::Logger::core(
Branch: 1 branch
BranchTrueFalseTotal
if keys %captures 2 0 2
106 " --> captures are: " . join(", ", keys(%captures)))
107 if keys %captures;
108 2 return $self->save_match_data($request, {captures => \%captures});
109 }
110  
111  
112 # save the route pattern that matched
113 # TODO : as soon as we have proper Dancer::Internal, we should remove
114 # that, it's just a quick hack for plugins to access the matching
115 # pattern.
116 # NOTE: YOU SHOULD NOT USE THAT, OR IF YOU DO, YOU MUST KNOW
117 # IT WILL MOVE VERY SOON
118 649 $request->{_route_pattern} = $self->pattern;
119  
120 # regex comments are how we know if we captured a token,
121 # splat or a megasplat
122 my @token_or_splat
123 649 = $self->{_compiled_regexp} =~ /\(\?#([token|(?:mega)?splat]+)\)/g;
124 649 ▶ if (@token_or_splat) {
Branch: 1 branch
BranchTrueFalseTotal
if (@token_or_splat) 249 400 649
125 # named tokens
126 249 ▶ my @tokens = @{$self->{_params} || []};
Condition50%1/2
$self->{'_params'} || []
l!l
2490
MC/DC0%0/1
$self->{'_params'} || []
$self->{'_params'}
Truth table50%1/2
A
A= $self->{'_params'}
Aresult
11
00
127 249 ▶ Dancer::Logger::core(" --> named tokens are: @tokens") if @tokens;
Branch: 1 branch
BranchTrueFalseTotal
if @tokens 232 17 249
128  
129 249 my %params;
130 my @splat;
131 249 ▶ for ( my $i = 0; $i < @values; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @values) 301 249 550
132 # Is this value from a token?
133 301 ▶ if ( $token_or_splat[$i] eq 'token' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_or_splat[$i] eq "token") 274 27 301
134 274 $params{ shift @tokens } = $values[$i];
135 274 next;
136 }
137  
138 # megasplat values are split on '/'
139 27 ▶ if ($token_or_splat[$i] eq 'megasplat') {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_or_splat[$i] eq "megasplat") 9 18 27
140 9 ▶ $values[$i] = [ split '/', $values[$i] || '' ];
Condition50%1/2
$values[$i] || ''
l!l
90
MC/DC0%0/1
$values[$i] || ''
$values[$i]
Truth table50%1/2
A
A= $values[$i]
Aresult
11
00
141 }
142 27 push @splat, $values[$i];
143 }
144 249 ▶ return $self->save_match_data( $request, {
Branch: 1 branch
BranchTrueFalseTotal
@splat ? : 19 230 249
145 %params,
146 ( @splat ? ( splat => \@splat ) : () ),
147 });
148 }
149  
150 400 ▶ if ($self->{_should_capture}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_should_capture"}) 17 383 400
151 17 return $self->save_match_data($request, {splat => \@values});
152 }
153  
154 383 return $self->save_match_data($request, {});
155 }
156  
157 536 ▶sub has_options {
Subroutine has_options: called CC=2 SCAR=6.9
Pod undocumented
158 536 my ($self) = @_;
159 536 ▶ return keys(%{$self->options}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
keys %{$self->options;} ? : 26 510 536
160 }
161  
162 1448 ▶sub check_options {
Subroutine check_options: called CC=5 SCAR=16.1
Pod undocumented
163 1448 my ($self) = @_;
164 1448 ▶ return 1 unless defined $self->options;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->options 5 1443 1448
165  
166 1443 for my $opt (keys %{$self->options}) {
167 raise core_route => "Not a valid option for route matching: `$opt'"
168 240 if not( (grep {/^$opt$/} @{$_supported_options[0]})
169 12 ▶ || (grep {/^$opt$/} keys(%_options_aliases)));
Branch: 1 branch
BranchTrueFalseTotal
unless grep {/^$opt$/;} @{$_supported_options[0];} or grep {/^$opt$/;} keys %_options_aliases 1 11 12
Condition100%3/3
grep {/^$opt$/;} @{$_supported_options[0];} or grep {/^$opt$/;} keys %_options_aliases
l!l&&r!l&&!r
381
MC/DC100%2/2
grep {/^$opt$/;} @{$_supported_options[0];} or grep {/^$opt$/;} keys %_options_aliases
grep {/^$opt$/;} @{$_supported_options[0];} grep {/^$opt$/;} keys %_options_aliases
Truth table100%3/3
A or B
A= grep {/^$opt$/;} @{$_supported_options[0];}B= grep {/^$opt$/;} keys %_options_aliases
ABresult
1X1
011
000
170 }
171 1442 return 1;
172 }
173  
174 26 ▶sub validate_options {
Subroutine validate_options: called CC=5 SCAR=16.1
Pod undocumented
175 26 my ($self, $request) = @_;
176  
177 26 ▶ while (my ($option, $value) = each %{$self->options}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$self->options;}) 26 12 38
178 $option = $_options_aliases{$option}
179 26 ▶ if exists $_options_aliases{$option};
Branch: 1 branch
BranchTrueFalseTotal
if exists $_options_aliases{$option} 26 0 26
180 26 ▶ return 0 if (not $request->$option) || ($request->$option !~ $value);
Branch: 1 branch
BranchTrueFalseTotal
unless $request->$option and $request->$option =~ /$value/ 14 12 26
Condition100%3/3
$request->$option and $request->$option =~ /$value/
!ll&&!rl&&r
9512
MC/DC100%2/2
$request->$option and $request->$option =~ /$value/
$request->$option $request->$option =~ /$value/
Truth table100%3/3
A and B
A= $request->$optionB= $request->$option =~ /$value/
ABresult
0X0
100
111
181 }
182 12 return 1;
183 }
184  
185 561 ▶sub run {
Subroutine run: called CC=15 SCAR=27.2
Pod undocumented
186 561 my ($self, $request) = @_;
187  
188 561 ▶ my $content = try {
Subroutine __ANON__: called CC=1 SCAR=0.0
189 561 $self->execute();
190 120 ▶ } continuation {
Subroutine __ANON__: called CC=3 SCAR=11.0
191 120 my ($continuation) = @_;
192 # route related continuation
193 120 ▶ $continuation->isa('Dancer::Continuation::Route')
Branch: 1 branch
BranchTrueFalseTotal
$continuation->isa("Dancer::Continuation::Route") or $continuation->rethrow 111 9 120
194 or $continuation->rethrow();
195 # If the continuation carries some content, get it
196 111 my $content = $continuation->return_value();
197 111 ▶ defined $content or return; # to avoid returning undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $content or return 18 93 111
198 18 return $content;
199 28 ▶ } catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
200 28 my ($exception) = @_;
201 28 Dancer::Factory::Hook->execute_hooks('on_route_exception', $exception);
202 28 die $exception;
203 561 };
204 524 my $response = Dancer::SharedData->response;
205  
206 524 ▶ if ( $response && $response->is_forwarded ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response and $response->is_forwarded) 15 509 524
Condition66%2/3
$response and $response->is_forwarded
!ll&&!rl&&r
050915
MC/DC50%1/2
$response and $response->is_forwarded
$response $response->is_forwarded
Truth table66%2/3
A and B
A= $responseB= $response->is_forwarded
ABresult
0X0
100
111
207 my $new_req =
208 15 Dancer::Request->forward($request, $response->{forward});
209 15 my $marshalled = Dancer::Handler->handle_request($new_req);
210  
211 return Dancer::Response->new(
212 encoded => 1,
213 status => $marshalled->[0],
214 headers => $marshalled->[1],
215 # if the forward failed with 404, marshalled->[2] is not an array, but a GLOB
216 15 ▶ content => ref($marshalled->[2]) eq "ARRAY" ? @{ $marshalled->[2] } : $marshalled->[2]
Branch: 1 branch
BranchTrueFalseTotal
ref $marshalled->[2] eq 'ARRAY' ? : 15 0 15
217 );
218 }
219  
220 509 ▶ if ($response && $response->has_passed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response and $response->has_passed) 78 431 509
Condition66%2/3
$response and $response->has_passed
!ll&&!rl&&r
043178
MC/DC50%1/2
$response and $response->has_passed
$response $response->has_passed
Truth table66%2/3
A and B
A= $responseB= $response->has_passed
ABresult
0X0
100
111
221 78 $response->pass(0);
222  
223 # find the next matching route and run it
224 78 ▶ while ($self = $self->next) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self = $self->next) 139 1 140
225 139 ▶ return $self->run($request) if $self->match($request);
Branch: 1 branch
BranchTrueFalseTotal
if $self->match($request) 77 62 139
226 }
227  
228 1 Dancer::Logger::core('Last matching route passed!');
229 1 return Dancer::Renderer->render_error(404);
230 }
231  
232 # coerce undef content to empty string to
233 # prevent warnings
234 431 ▶ $content = (defined $content) ? $content : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $content ? : 415 16 431
235  
236 431 ▶ my $ct =
Branch: 1 branch
BranchTrueFalseTotal
defined $response && defined $response->content_type ? : 36 395 431
Condition66%2/3
defined $response and defined $response->content_type
!ll&&!rl&&r
039536
MC/DC50%1/2
defined $response and defined $response->content_type
defined $response defined $response->content_type
Truth table66%2/3
A and B
A= defined $responseB= defined $response->content_type
ABresult
0X0
100
111
237 ( defined $response && defined $response->content_type )
238 ? $response->content_type()
239 : setting('content_type');
240  
241 431 ▶ my $st = defined $response ? $response->status : 200;
Branch: 1 branch
BranchTrueFalseTotal
defined $response ? : 431 0 431
242  
243 431 my $headers = [];
244 431 ▶ push @$headers, @{ $response->headers_to_array } if defined $response;
Branch: 1 branch
BranchTrueFalseTotal
if defined $response 431 0 431
245  
246 # content type may have already be set earlier
247 # (eg: with send_error)
248 push(@$headers, 'Content-Type' => $ct)
249 431 ▶ unless grep {/Content-Type/} @$headers;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {/Content-Type/;} @$headers 395 36 431
250  
251 431 ▶ return $content if ref($content) eq 'Dancer::Response';
Branch: 1 branch
BranchTrueFalseTotal
if ref $content eq "Dancer::Response" 19 412 431
252 412 return Dancer::Response->new(
253 status => $st,
254 headers => $headers,
255 content => $content,
256 );
257 }
258  
259 561 ▶sub execute {
Subroutine execute: called CC=3 SCAR=11.3
Pod undocumented
260 561 my ($self) = @_;
261  
262 561 ▶ if (Dancer::Config::setting('warnings')) {
Branch: 1 branch
BranchTrueFalseTotal
if (Dancer::Config::setting('warnings')) { } 9 552 561
263 9 my $warning;
264 9 my $content = do {
265 9 ▶ local $SIG{__WARN__} = sub { $warning ||= $_[0] };
Subroutine __ANON__: called CC=2 SCAR=8.1
Condition33%1/3
$warning ||= $_[0]
l!l&&r!l&&!r
030
MC/DC0%0/2
$warning ||= $_[0]
$warning $_[0]
Truth table33%1/3
A ||= B
A= $warningB= $_[0]
ABresult
1X1
011
000
266 9 $self->code->();
267 };
268 3 ▶ if ($warning) {
Branch: 1 branch
BranchTrueFalseTotal
if ($warning) 3 0 3
269 3 die "Warning caught during route execution: $warning";
270 }
271 0 return $content;
272 }
273 else {
274 552 return $self->code->();
275 }
276 }
277  
278 83 ▶sub _init_prefix {
Subroutine _init_prefix: called CC=4 SCAR=13.9
279 83 my ($self) = @_;
280 83 my $prefix = $self->prefix;
281  
282 83 ▶ if ($self->is_regexp) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->is_regexp) { } 2 81 83
elsif ($self->pattern eq '/') { } 8 73 81
283 2 my $regexp = $self->regexp;
284 2 ▶ if ($regexp !~ /^$prefix/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($regexp =~ /^$prefix/) 2 0 2
285 2 $self->regexp(qr{${prefix}${regexp}});
286 }
287 }
288 elsif ($self->pattern eq '/') {
289  
290 # if pattern is '/', we should match:
291 # - /prefix/
292 # - /prefix
293 # this is done by creating a regex for this case
294 8 my $qpattern = quotemeta( $self->pattern );
295 8 my $qprefix = quotemeta( $self->prefix );
296 8 my $regex = qr/^$qprefix(?:$qpattern)?$/;
297 8 $self->{regexp} = $regex;
298 8 $self->{pattern} = $regex;
299 }
300 else {
301 73 $self->{pattern} = $prefix . $self->pattern;
302 }
303  
304 83 return $prefix;
305 }
306  
307 0 ▶sub equals {
Subroutine equals: not called CC=1 SCAR=6.9
Pod undocumented
308 0 my ($self, $route) = @_;
309 0 return $self->regexp eq $route->regexp;
310 }
311  
312 1530 ▶sub is_regexp {
Subroutine is_regexp: called CC=1 SCAR=0.0
Pod undocumented
313 1530 my ($self) = @_;
314 1530 return defined $self->regexp;
315 }
316  
317 1447 ▶sub _build_regexp {
Subroutine _build_regexp: called CC=2 SCAR=6.9
318 1447 my ($self) = @_;
319  
320 1447 ▶ if ($self->is_regexp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_regexp) { } 20 1427 1447
321 20 $self->{_compiled_regexp} = $self->regexp;
322 20 $self->{_compiled_regexp} = qr/^$self->{_compiled_regexp}$/;
323 20 $self->{_should_capture} = 1;
324 }
325 else {
326 1427 $self->_build_regexp_from_string($self->pattern);
327 }
328  
329 1447 return $self->{_compiled_regexp};
330 }
331  
332 1427 ▶sub _build_regexp_from_string {
Subroutine _build_regexp_from_string: called CC=6 SCAR=17.9
333 1427 my ($self, $pattern) = @_;
334 1427 my $capture = 0;
335 1427 my @params;
336  
337 # look for route with params (/hello/:foo)
338 1427 ▶ if ($pattern =~ /:/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pattern =~ /:/) 185 1242 1427
339 185 @params = $pattern =~ /:([^\/\.\?]+)/g;
340 185 ▶ if (@params) {
Branch: 1 branch
BranchTrueFalseTotal
if (@params) 185 0 185
341 185 $pattern =~ s!(:[^\/\.\?]+)!(?#token)([^/]+)!g;
342 185 $capture = 1;
343 }
344 }
345  
346 # parse megasplat
347 # we use {0,} instead of '*' not to fall in the splat rule
348 # same logic for [^\n] instead of '.'
349 1427 ▶ $capture = 1 if $pattern =~ s!\Q**\E!(?#megasplat)([^\n]+)!g;
Branch: 1 branch
BranchTrueFalseTotal
if $pattern =~ s/\*\*/(?#megasplat)([^\n]+)/g 12 1415 1427
350  
351 # parse wildcards
352 1427 ▶ $capture = 1 if $pattern =~ s!\*!(?#splat)([^/]+)!g;
Branch: 1 branch
BranchTrueFalseTotal
if $pattern =~ s[\*][(?#splat)([^/]+)]g 88 1339 1427
353  
354 # escape dots
355 1427 ▶ $pattern =~ s/\./\\\./g if $pattern =~ /\./;
Branch: 1 branch
BranchTrueFalseTotal
if $pattern =~ /\./ 6 1421 1427
356  
357 # escape slashes
358 1427 $pattern =~ s/\//\\\//g;
359  
360 1427 $self->{_compiled_regexp} = "^${pattern}\$";
361 1427 $self->{_params} = \@params;
362 1427 $self->{_should_capture} = $capture;
363  
364 1427 return $self->{_compiled_regexp};
365 }
366  
367 194 1;
368  
369 __END__
« bin/dancer ↑ Summary lib/Dancer/Handler.pm »