« blib/lib/Dancer/Template/TemplateToolkit.pm
↑ Summary
/home/cpancover/.cpan/build/Dancer-1.3520-0/bin/dancer »
| 1 | package Dancer::Route; | ||||||||||||||||||||||||||||
| 2 | 185 | our $AUTHORITY = 'cpan:SUKRIA'; | |||||||||||||||||||||||||||
| 3 | # ABSTRACT: Class to represent a route in Dancer | ||||||||||||||||||||||||||||
| 4 | 185 | $Dancer::Route::VERSION = '1.3520'; | |||||||||||||||||||||||||||
| 5 | 185 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 185 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 185 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 185 | ▶ | use base 'Dancer::Object'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 185 | ▶ | use Dancer::App; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 185 | ▶ | use Dancer::Logger; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 185 | ▶ | use Dancer::Config 'setting'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 185 | ▶ | use Dancer::Request; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 185 | ▶ | use Dancer::Response; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 185 | ▶ | use Dancer::Exception qw(:all); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 185 | ▶ | use Dancer::Factory::Hook; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 185 | 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 | 185 | Dancer::Factory::Hook->instance->install_hooks( | |||||||||||||||||||||||||||
| 34 | qw/on_route_exception/ | ||||||||||||||||||||||||||||
| 35 | ); | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | # supported options and aliases | ||||||||||||||||||||||||||||
| 38 | 185 | my @_supported_options = Dancer::Request->get_attributes(); | |||||||||||||||||||||||||||
| 39 | 185 | my %_options_aliases = (agent => 'user_agent'); | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | sub init { | ||||||||||||||||||||||||||||
| 42 | 1448 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 43 | 1448 | $self->{'_compiled_regexp'} = undef; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 1448 | ▶ | raise core_route => "cannot create Dancer::Route without a pattern" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 55 | 1447 | ▶ | $self->_init_prefix() if $self->prefix; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 1447 | $self->_build_regexp(); | |||||||||||||||||||||||||||
| 57 | 1447 | ▶ | $self->set_previous($self->prev) if $self->prev; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 1447 | return $self; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | sub set_previous { | ||||||||||||||||||||||||||||
| 63 | 1180 | ▶ | my ($self, $prev) = @_; | ||||||||||||||||||||||||||
|
Subroutine
set_previous: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 64 | 1180 | $self->prev($prev); | |||||||||||||||||||||||||||
| 65 | 1180 | $self->prev->{'next'} = $self; | |||||||||||||||||||||||||||
| 66 | 1180 | return $prev; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | sub save_match_data { | ||||||||||||||||||||||||||||
| 70 | 651 | ▶ | my ($self, $request, $match_data) = @_; | ||||||||||||||||||||||||||
|
Subroutine
save_match_data: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 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 | sub match { | ||||||||||||||||||||||||||||
| 79 | 1729 | ▶ | my ($self, $request) = @_; | ||||||||||||||||||||||||||
|
Subroutine
match: called CC=14 SCAR=26.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | Dancer::Logger::core( | ||||||||||||||||||||||||||||
| 94 | " --> got " . | ||||||||||||||||||||||||||||
| 95 | 651 | ▶ | map { defined $_ ? $_ : 'undef' } @values | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 102 | 185 | ▶ | do { no warnings; %+ } | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 103 | ) | ||||||||||||||||||||||||||||
| 104 | { | ||||||||||||||||||||||||||||
| 105 | 2 | ▶ | Dancer::Logger::core( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 125 | # named tokens | ||||||||||||||||||||||||||||
| 126 | 249 | ▶ | my @tokens = @{$self->{_params} || []}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 249 | ▶ | Dancer::Logger::core(" --> named tokens are: @tokens") if @tokens; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 249 | my %params; | |||||||||||||||||||||||||||
| 130 | my @splat; | ||||||||||||||||||||||||||||
| 131 | 249 | ▶ | for ( my $i = 0; $i < @values; $i++ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | # Is this value from a token? | ||||||||||||||||||||||||||||
| 133 | 301 | ▶ | if ( $token_or_splat[$i] eq 'token' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 140 | 9 | ▶ | $values[$i] = [ split '/', $values[$i] || '' ]; | ||||||||||||||||||||||||||
|
Condition50%1/2
$values[$i] || ''
MC/DC0%0/1
$values[$i] || ''
$values[$i]
Truth table50%1/2
A
A= $values[$i]
| |||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | 27 | push @splat, $values[$i]; | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | 249 | ▶ | return $self->save_match_data( $request, { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | %params, | ||||||||||||||||||||||||||||
| 146 | ( @splat ? ( splat => \@splat ) : () ), | ||||||||||||||||||||||||||||
| 147 | }); | ||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 400 | ▶ | if ($self->{_should_capture}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | 17 | return $self->save_match_data($request, {splat => \@values}); | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 383 | return $self->save_match_data($request, {}); | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | sub has_options { | ||||||||||||||||||||||||||||
| 158 | 536 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
has_options: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 159 | 536 | ▶ | return keys(%{$self->options}) ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | sub check_options { | ||||||||||||||||||||||||||||
| 163 | 1448 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
check_options: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 164 | 1448 | ▶ | return 1 unless defined $self->options; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
grep {/^$opt$/;} @{$_supported_options[0];} or grep {/^$opt$/;} keys %_options_aliases
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
| |||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | 1442 | return 1; | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | sub validate_options { | ||||||||||||||||||||||||||||
| 175 | 26 | ▶ | my ($self, $request) = @_; | ||||||||||||||||||||||||||
|
Subroutine
validate_options: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 26 | ▶ | while (my ($option, $value) = each %{$self->options}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 178 | $option = $_options_aliases{$option} | ||||||||||||||||||||||||||||
| 179 | 26 | ▶ | if exists $_options_aliases{$option}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 180 | 26 | ▶ | return 0 if (not $request->$option) || ($request->$option !~ $value); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$request->$option and $request->$option =~ /$value/
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/
| |||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | 12 | return 1; | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | sub run { | ||||||||||||||||||||||||||||
| 186 | 561 | ▶ | my ($self, $request) = @_; | ||||||||||||||||||||||||||
|
Subroutine
run: called CC=15 SCAR=27.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | my $content = try { | ||||||||||||||||||||||||||||
| 189 | 561 | ▶ | $self->execute(); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 190 | } continuation { | ||||||||||||||||||||||||||||
| 191 | 120 | ▶ | my ($continuation) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 192 | # route related continuation | ||||||||||||||||||||||||||||
| 193 | 120 | ▶ | $continuation->isa('Dancer::Continuation::Route') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 198 | 18 | return $content; | |||||||||||||||||||||||||||
| 199 | } catch { | ||||||||||||||||||||||||||||
| 200 | 28 | ▶ | my ($exception) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
$response and $response->is_forwarded
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 217 | ); | ||||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | 509 | ▶ | if ($response && $response->has_passed) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$response and $response->has_passed
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
| |||||||||||||||||||||||||||||
| 221 | 78 | $response->pass(0); | |||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | # find the next matching route and run it | ||||||||||||||||||||||||||||
| 224 | 78 | ▶ | while ($self = $self->next) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 225 | 139 | ▶ | return $self->run($request) if $self->match($request); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | 431 | ▶ | my $ct = | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $response and defined $response->content_type
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | 431 | my $headers = []; | |||||||||||||||||||||||||||
| 244 | 431 | ▶ | push @$headers, @{ $response->headers_to_array } if defined $response; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | 431 | ▶ | return $content if ref($content) eq 'Dancer::Response'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 252 | 412 | return Dancer::Response->new( | |||||||||||||||||||||||||||
| 253 | status => $st, | ||||||||||||||||||||||||||||
| 254 | headers => $headers, | ||||||||||||||||||||||||||||
| 255 | content => $content, | ||||||||||||||||||||||||||||
| 256 | ); | ||||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | sub execute { | ||||||||||||||||||||||||||||
| 260 | 561 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
execute: called CC=3 SCAR=11.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | 561 | ▶ | if (Dancer::Config::setting('warnings')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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]
MC/DC0%0/2
$warning ||= $_[0]
$warning
$_[0]
Truth table33%1/3
A ||= B
A= $warningB= $_[0]
| |||||||||||||||||||||||||||||
| 266 | 9 | $self->code->(); | |||||||||||||||||||||||||||
| 267 | }; | ||||||||||||||||||||||||||||
| 268 | 3 | ▶ | if ($warning) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | sub _init_prefix { | ||||||||||||||||||||||||||||
| 279 | 83 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_init_prefix: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 280 | 83 | my $prefix = $self->prefix; | |||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||
| 282 | 83 | ▶ | if ($self->is_regexp) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 283 | 2 | my $regexp = $self->regexp; | |||||||||||||||||||||||||||
| 284 | 2 | ▶ | if ($regexp !~ /^$prefix/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | sub equals { | ||||||||||||||||||||||||||||
| 308 | 0 | ▶ | my ($self, $route) = @_; | ||||||||||||||||||||||||||
|
Subroutine
equals: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 309 | 0 | return $self->regexp eq $route->regexp; | |||||||||||||||||||||||||||
| 310 | } | ||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | sub is_regexp { | ||||||||||||||||||||||||||||
| 313 | 1530 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
is_regexp: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 314 | 1530 | return defined $self->regexp; | |||||||||||||||||||||||||||
| 315 | } | ||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | sub _build_regexp { | ||||||||||||||||||||||||||||
| 318 | 1447 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_build_regexp: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | 1447 | ▶ | if ($self->is_regexp) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | sub _build_regexp_from_string { | ||||||||||||||||||||||||||||
| 333 | 1427 | ▶ | my ($self, $pattern) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_build_regexp_from_string: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 339 | 185 | @params = $pattern =~ /:([^\/\.\?]+)/g; | |||||||||||||||||||||||||||
| 340 | 185 | ▶ | if (@params) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | # parse wildcards | ||||||||||||||||||||||||||||
| 352 | 1427 | ▶ | $capture = 1 if $pattern =~ s!\*!(?#splat)([^/]+)!g; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | # escape dots | ||||||||||||||||||||||||||||
| 355 | 1427 | ▶ | $pattern =~ s/\./\\\./g if $pattern =~ /\./; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | 185 | 1; | |||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||
| 369 | __END__ | ||||||||||||||||||||||||||||