| 1 | package PAGI::App::Router; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 13 | $PAGI::App::Router::VERSION = '0.002002'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 13 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 13 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 13 | ▶ | use Future::AsyncAwait; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 13 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 13 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 13 | ▶ | use PAGI::Utils (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | =encoding UTF-8 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | PAGI::App::Router - Unified routing for HTTP, WebSocket, and SSE | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | use PAGI::App::Router; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | my $router = PAGI::App::Router->new; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | # HTTP routes (method + path) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | $router->get('/users/:id' => $get_user); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | $router->post('/users' => $create_user); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | $router->delete('/users/:id' => $delete_user); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | # Routes with middleware | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | $router->get('/admin' => [$auth_mw, $log_mw] => $admin_handler); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | $router->post('/api/data' => [$rate_limit] => $data_handler); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | # WebSocket routes (path only) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | $router->websocket('/ws/chat/:room' => $chat_handler); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | # SSE routes (path only) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | $router->sse('/events/:channel' => $events_handler); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | # Mount with middleware (applies to all sub-routes) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | $router->mount('/api' => [$auth_mw] => $api_router); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | # Mount from a package (auto-require + to_app) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | $router->mount('/admin' => 'MyApp::Admin'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | # Static files as fallback | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | $router->mount('/' => $static_files); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | # Named routes for URL generation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | $router->get('/users/:id' => $get_user)->name('users.get'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | $router->post('/users' => $create_user)->name('users.create'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | my $url = $router->uri_for('users.get', { id => 42 }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | # Returns: "/users/42" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | # Namespace mounted routers | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | $router->mount('/api/v1' => $api_router)->as('api'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | $router->uri_for('api.users.get', { id => 42 }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | # Returns: "/api/v1/users/42" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | # Match any HTTP method | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | $router->any('/health' => $health_handler); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | $router->any('/resource' => $handler, method => ['GET', 'POST']); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | # Path constraints (inline) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | $router->get('/users/{id:\d+}' => $get_user); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | # Path constraints (chained) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | $router->get('/posts/:slug' => $get_post) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | ->constraints(slug => qr/^[a-z0-9-]+$/); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | # Route grouping (flattened into parent) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | $router->group('/api' => [$auth_mw] => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | my ($r) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | $r->get('/users' => $list_users); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | $r->post('/users' => $create_user); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | # Include routes from another router | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | $router->group('/api/v2' => $v2_router); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | # Include routes from a package | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | $router->group('/api/users' => 'MyApp::Routes::Users'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | my $app = $router->to_app; # Handles all scope types | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 143 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 143 | my ($class, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | return bless { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | routes => [], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | websocket_routes => [], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | sse_routes => [], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | mounts => [], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | not_found => $args{not_found}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 143 | _group_stack => [], # for group() prefix/middleware accumulation | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | _named_routes => {}, # name => route info | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | _last_route => undef, # for ->name() chaining | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | _last_mount => undef, # for ->as() chaining | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | }, $class; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 20 | ▶ | sub mount { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
mount: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 20 | my ($self, $prefix, @rest) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 20 | $prefix =~ s{/$}{}; # strip trailing slash | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 20 | my ($middleware, $app_or_router) = $self->_parse_route_args(@rest); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 20 | my $sub_router; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 20 | ▶ | if (blessed($app_or_router) && $app_or_router->isa('PAGI::App::Router')) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
builtin::blessed($app_or_router) and $app_or_router->isa("PAGI::App::Router")
MC/DC100%2/2
builtin::blessed($app_or_router) and $app_or_router->isa("PAGI::App::Router")
builtin::blessed($app_or_router)
$app_or_router->isa("PAGI::App::Router")
Truth table100%3/3
A and B
A= builtin::blessed($app_or_router)B= $app_or_router->isa("PAGI::App::Router")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 3 | $sub_router = $app_or_router; # Keep reference for ->as() | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 20 | my $app = PAGI::Utils::to_app($app_or_router); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 18 | my $mount = { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | prefix => $prefix, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | app => $app, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | middleware => $middleware, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | sub_router => $sub_router, # Keep reference for ->as() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 18 | push @{$self->{mounts}}, $mount; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 18 | $self->{_last_mount} = $mount; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 18 | $self->{_last_route} = undef; # Clear route tracking | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 18 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 143 | ▶ | sub get { my ($self, $path, @rest) = @_; $self->route('GET', $path, @rest) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 8 | ▶ | sub post { my ($self, $path, @rest) = @_; $self->route('POST', $path, @rest) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
post: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | ▶ | sub put { my ($self, $path, @rest) = @_; $self->route('PUT', $path, @rest) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
put: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | ▶ | sub patch { my ($self, $path, @rest) = @_; $self->route('PATCH', $path, @rest) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
patch: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 2 | ▶ | sub delete { my ($self, $path, @rest) = @_; $self->route('DELETE', $path, @rest) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
delete: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 0 | ▶ | sub head { my ($self, $path, @rest) = @_; $self->route('HEAD', $path, @rest) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
head: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | ▶ | sub options { my ($self, $path, @rest) = @_; $self->route('OPTIONS', $path, @rest) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
options: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 10 | ▶ | sub any { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
any: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 10 | my ($self, $path, @rest) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | # Parse optional trailing key-value args (method => [...]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 10 | my %opts; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 10 | ▶ | if (@rest >= 2 && !ref($rest[-2]) && $rest[-2] eq 'method') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
@rest >= 2 and not ref $rest[-2] and $rest[-2] eq "method"
@rest >= 2 and not ref $rest[-2]
MC/DC66%2/3
@rest >= 2 and not ref $rest[-2] and $rest[-2] eq "method"
@rest >= 2
not ref $rest[-2]
$rest[-2] eq "method"
Truth table75%3/4
A and not(B) and C
A= @rest >= 2B= not ref $rest[-2]C= $rest[-2] eq "method"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 4 | %opts = splice(@rest, -2); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 10 | ▶ | my $method = $opts{method} // '*'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts{"method"} // "*"
MC/DC100%1/1
$opts{"method"} // "*"
$opts{"method"}
Truth table100%2/2
A
A= $opts{"method"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 10 | ▶ | if (ref($method) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 4 | $method = [map { uc($_) } @$method]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 10 | $self->route($method, $path, @rest); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 24 | ▶ | sub group { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
group: called CC=12 SCAR=25.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 24 | my ($self, $prefix, @rest) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 24 | $prefix =~ s{/$}{}; # strip trailing slash | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 24 | my ($middleware, $target) = $self->_parse_route_args(@rest); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 24 | my %names_before = map { $_ => 1 } keys %{$self->{_named_routes}}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 24 | ▶ | if (ref($target) eq 'CODE') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 14 | push @{$self->{_group_stack}}, { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | prefix => $prefix, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | middleware => [@$middleware], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 14 | $target->($self); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | 14 | pop @{$self->{_group_stack}}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | ▶ | elsif (blessed($target) && $target->isa('PAGI::App::Router')) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
builtin::blessed($target) and $target->isa("PAGI::App::Router")
MC/DC50%1/2
builtin::blessed($target) and $target->isa("PAGI::App::Router")
builtin::blessed($target)
$target->isa("PAGI::App::Router")
Truth table66%2/3
A and B
A= builtin::blessed($target)B= $target->isa("PAGI::App::Router")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 6 | push @{$self->{_group_stack}}, { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | prefix => $prefix, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | middleware => [@$middleware], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 6 | $self->_include_router($target); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 6 | pop @{$self->{_group_stack}}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | elsif (!ref($target)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | # String form: auto-require and call ->router | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | 3 | my $pkg = $target; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 3 | local $@; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | 3 | ▶ | eval "require $pkg; 1" or croak "Failed to load '$pkg': $@"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 2 | ▶ | croak "'$pkg' does not have a router() method" unless $pkg->can('router'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 1 | my $router_obj = $pkg->router; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | 1 | ▶ | croak "'${pkg}->router()' must return a PAGI::App::Router, got " | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition20%1/5
builtin::blessed($router_obj) and $router_obj->isa("PAGI::App::Router")
ref $router_obj || 'scalar'
MC/DC0%0/2
builtin::blessed($router_obj) and $router_obj->isa("PAGI::App::Router")
builtin::blessed($router_obj)
$router_obj->isa("PAGI::App::Router")
MC/DC0%0/1
ref $router_obj || 'scalar'
ref $router_obj
Truth table33%1/3
A and B
A= builtin::blessed($router_obj)B= $router_obj->isa("PAGI::App::Router")
Truth table0%0/2
A
A= ref $router_obj
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | . (ref($router_obj) || 'scalar') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | unless blessed($router_obj) && $router_obj->isa('PAGI::App::Router'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | 1 | push @{$self->{_group_stack}}, { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | prefix => $prefix, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | middleware => [@$middleware], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | 1 | $self->_include_router($router_obj); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | 1 | pop @{$self->{_group_stack}}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | 1 | ▶ | croak "group() target must be a coderef, PAGI::App::Router, or package name, got " | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
ref $target || 'scalar'
MC/DC0%0/1
ref $target || 'scalar'
ref $target
Truth table50%1/2
A
A= ref $target
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | . (ref($target) || 'scalar'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 21 | my @new_names = grep { !$names_before{$_} } keys %{$self->{_named_routes}}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 21 | ▶ | $self->{_last_group_names} = @new_names ? \@new_names : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | 21 | $self->{_last_route} = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | 21 | $self->{_last_mount} = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | 21 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 10 | ▶ | sub websocket { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
websocket: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | 10 | my ($self, $path, @rest) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | 10 | my ($middleware, $app) = $self->_parse_route_args(@rest); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | 10 | $app = PAGI::Utils::to_app($app); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | # Apply accumulated group context (reverse: innermost prefix first) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | 10 | for my $ctx (reverse @{$self->{_group_stack}}) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | 1 | $path = $ctx->{prefix} . $path; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 1 | unshift @$middleware, @{$ctx->{middleware}}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | 10 | my ($regex, $names, $constraints) = $self->_compile_path($path); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | 10 | my $route = { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | path => $path, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | regex => $regex, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | names => $names, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | constraints => $constraints, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | app => $app, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | middleware => $middleware, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | 10 | push @{$self->{websocket_routes}}, $route; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | 10 | $self->{_last_route} = $route; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | 10 | $self->{_last_mount} = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | 10 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 10 | ▶ | sub sse { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sse: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | 10 | my ($self, $path, @rest) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | 10 | my ($middleware, $app) = $self->_parse_route_args(@rest); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | 10 | $app = PAGI::Utils::to_app($app); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | # Apply accumulated group context (reverse: innermost prefix first) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | 10 | for my $ctx (reverse @{$self->{_group_stack}}) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | 1 | $path = $ctx->{prefix} . $path; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | 1 | unshift @$middleware, @{$ctx->{middleware}}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | 10 | my ($regex, $names, $constraints) = $self->_compile_path($path); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | 10 | my $route = { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | path => $path, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | regex => $regex, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | names => $names, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | constraints => $constraints, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | app => $app, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | middleware => $middleware, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | 10 | push @{$self->{sse_routes}}, $route; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | 10 | $self->{_last_route} = $route; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | 10 | $self->{_last_mount} = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | 10 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | 174 | ▶ | sub route { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
route: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | 174 | my ($self, $method, $path, @rest) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | 174 | my ($middleware, $app) = $self->_parse_route_args(@rest); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | 171 | $app = PAGI::Utils::to_app($app); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | # Apply accumulated group context (reverse: innermost prefix first) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | 171 | for my $ctx (reverse @{$self->{_group_stack}}) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | 32 | $path = $ctx->{prefix} . $path; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | 32 | unshift @$middleware, @{$ctx->{middleware}}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | 171 | my ($regex, $names, $constraints) = $self->_compile_path($path); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | 171 | ▶ | my $route = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | method => ref($method) eq 'ARRAY' ? $method : ($method eq '*' ? '*' : uc($method)), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | path => $path, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | regex => $regex, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | names => $names, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | constraints => $constraints, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | app => $app, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | middleware => $middleware, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | 171 | push @{$self->{routes}}, $route; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | 171 | $self->{_last_route} = $route; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | 171 | $self->{_last_mount} = undef; # Clear mount tracking | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | 171 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | 191 | ▶ | sub _compile_path { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_compile_path: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | 191 | my ($self, $path) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | 191 | my @names; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | my @constraints; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | 191 | my $regex = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | # Tokenize the path | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | 191 | my $remaining = $path; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | 191 | ▶ | while (length $remaining) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | # {name:pattern} — constrained parameter | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | 260 | ▶ | if ($remaining =~ s/^\{(\w+):([^}]+)\}//) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | 11 | push @names, $1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | 11 | push @constraints, [$1, $2]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | 11 | $regex .= "([^/]+)"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | # {name} — unconstrained parameter (same as :name) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | elsif ($remaining =~ s/^\{(\w+)\}//) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | 2 | push @names, $1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | 2 | $regex .= "([^/]+)"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | # *name — wildcard/splat | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | elsif ($remaining =~ s/^\*(\w+)//) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | 3 | push @names, $1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | 3 | $regex .= "(.+)"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | # :name — named parameter (legacy syntax) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | elsif ($remaining =~ s/^:(\w+)//) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | 48 | push @names, $1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | 48 | $regex .= "([^/]+)"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | # Literal text up to next special token | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | elsif ($remaining =~ s/^([^{:*]+)//) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | 196 | $regex .= quotemeta($1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | # Safety: consume one character to avoid infinite loop | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | 0 | $regex .= quotemeta(substr($remaining, 0, 1, '')); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | 191 | return (qr{^$regex$}, \@names, \@constraints); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | 148 | ▶ | sub _check_constraints { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_check_constraints: called CC=7 SCAR=19.6
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | 148 | my ($self, $route, $params) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | 148 | ▶ | for my $constraints_list ($route->{constraints} // [], $route->{_user_constraints} // []) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition75%3/4
$route->{"constraints"} // []
$route->{"_user_constraints"} // []
MC/DC0%0/1
$route->{"constraints"} // []
$route->{"constraints"}
MC/DC100%1/1
$route->{"_user_constraints"} // []
$route->{"_user_constraints"}
Truth table50%1/2
A
A= $route->{"constraints"}
Truth table100%2/2
A
A= $route->{"_user_constraints"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | 288 | for my $c (@$constraints_list) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | 28 | my ($name, $pattern) = @$c; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | 28 | ▶ | my $value = $params->{$name} // return 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$params->{$name} // (return 0)
MC/DC0%0/1
$params->{$name} // (return 0)
$params->{$name}
Truth table50%1/2
A
A= $params->{$name}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | 28 | ▶ | return 0 unless $value =~ m/^(?:$pattern)$/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | 135 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | # ============================================================ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | # Named Routes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | # ============================================================ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | 42 | ▶ | sub name { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
name: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | 42 | my ($self, $name) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | 42 | ▶ | croak "name() called without a preceding route" unless $self->{_last_route}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | 40 | ▶ | croak "Route name required" unless defined $name && length $name; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $name and length $name
MC/DC50%1/2
defined $name and length $name
defined $name
length $name
Truth table66%2/3
A and B
A= defined $nameB= length $name
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | 39 | ▶ | croak "Named route '$name' already exists" if exists $self->{_named_routes}{$name}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | 38 | my $route = $self->{_last_route}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | 38 | $route->{name} = $name; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | $self->{_named_routes}{$name} = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | path => $route->{path}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | names => $route->{names}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | 38 | prefix => '', | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | 38 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | 9 | ▶ | sub constraints { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
constraints: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | 9 | my ($self, %new_constraints) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | 9 | ▶ | croak "constraints() called without a preceding route" unless $self->{_last_route}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | 8 | my $route = $self->{_last_route}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | 8 | ▶ | my $user_constraints = $route->{_user_constraints} //= []; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$route->{"_user_constraints"} //= []
MC/DC0%0/1
$route->{"_user_constraints"} //= []
$route->{"_user_constraints"}
Truth table50%1/2
A
A= $route->{"_user_constraints"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | 8 | for my $name (keys %new_constraints) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | 8 | my $pattern = $new_constraints{$name}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | 8 | ▶ | croak "Constraint for '$name' must be a Regexp (qr//), got " . ref($pattern) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | unless ref($pattern) eq 'Regexp'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | 7 | push @$user_constraints, [$name, $pattern]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | 7 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | 9 | ▶ | sub as { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
as: called CC=11 SCAR=24.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | 9 | my ($self, $namespace) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | 9 | ▶ | croak "Namespace required" unless defined $namespace && length $namespace; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $namespace and length $namespace
MC/DC0%0/2
defined $namespace and length $namespace
defined $namespace
length $namespace
Truth table33%1/3
A and B
A= defined $namespaceB= length $namespace
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | # Handle group namespacing | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | 9 | ▶ | if ($self->{_last_group_names} && @{$self->{_last_group_names}}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"_last_group_names"} and @{$self->{"_last_group_names"};}
MC/DC50%1/2
$self->{"_last_group_names"} and @{$self->{"_last_group_names"};}
$self->{"_last_group_names"}
@{$self->{"_last_group_names"};}
Truth table66%2/3
A and B
A= $self->{"_last_group_names"}B= @{$self->{"_last_group_names"};}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | 3 | for my $name (@{$self->{_last_group_names}}) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | 5 | my $info = delete $self->{_named_routes}{$name}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | 5 | my $full_name = "$namespace.$name"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | croak "Named route '$full_name' already exists" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | 5 | ▶ | if exists $self->{_named_routes}{$full_name}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | 5 | $self->{_named_routes}{$full_name} = $info; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | 3 | $self->{_last_group_names} = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | 3 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | # Handle mount namespacing | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | croak "as() called without a preceding mount or group" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | 6 | ▶ | unless $self->{_last_mount}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | 4 | my $mount = $self->{_last_mount}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | 4 | my $sub_router = $mount->{sub_router}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | 4 | ▶ | croak "as() requires mounting a router object, not an app coderef" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | unless $sub_router; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 416 | # Import all named routes from sub-router with namespace prefix | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 417 | 3 | my $prefix = $mount->{prefix}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 418 | 3 | for my $name (keys %{$sub_router->{_named_routes}}) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 419 | 6 | my $info = $sub_router->{_named_routes}{$name}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 420 | 6 | my $full_name = "$namespace.$name"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 421 | $self->{_named_routes}{$full_name} = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 422 | path => $info->{path}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 423 | names => $info->{names}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 424 | 6 | ▶ | prefix => $prefix . ($info->{prefix} // ''), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$info->{"prefix"} // ""
MC/DC0%0/1
$info->{"prefix"} // ""
$info->{"prefix"}
Truth table50%1/2
A
A= $info->{"prefix"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 425 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 426 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 428 | 3 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 429 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 431 | 3 | ▶ | sub named_routes { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
named_routes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 432 | 3 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 433 | 3 | return { %{$self->{_named_routes}} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 434 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 436 | 32 | ▶ | sub uri_for { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
uri_for: called CC=12 SCAR=24.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 437 | 32 | my ($self, $name, $path_params, $query_params) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 439 | 32 | ▶ | $path_params //= {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$path_params //= {}
MC/DC100%1/1
$path_params //= {}
$path_params
Truth table100%2/2
A
A= $path_params
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 440 | 32 | ▶ | $query_params //= {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$query_params //= {}
MC/DC100%1/1
$query_params //= {}
$query_params
Truth table100%2/2
A
A= $query_params
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 442 | 32 | ▶ | my $info = $self->{_named_routes}{$name} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 443 | or croak "Unknown route name: '$name'"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 445 | 31 | my $path = $info->{path}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 446 | 31 | ▶ | my $prefix = $info->{prefix} // ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$info->{"prefix"} // ""
MC/DC0%0/1
$info->{"prefix"} // ""
$info->{"prefix"}
Truth table50%1/2
A
A= $info->{"prefix"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 448 | # Substitute path parameters | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 449 | 31 | for my $param_name (@{$info->{names}}) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 450 | 16 | ▶ | unless (exists $path_params->{$param_name}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 451 | 1 | croak "Missing required path parameter '$param_name' for route '$name'"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 452 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 453 | 15 | my $value = $path_params->{$param_name}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 454 | 15 | ▶ | $path =~ s/:$param_name\b/$value/ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$path =~ s/:$param_name\b/$value/ or $path =~ s/\{$param_name(?::[^}]*)?\}/$value/
MC/DC100%2/2
$path =~ s/:$param_name\b/$value/ or $path =~ s/\{$param_name(?::[^}]*)?\}/$value/
$path =~ s/:$param_name\b/$value/
$path =~ s/\{$param_name(?::[^}]*)?\}/$value/
Truth table100%3/3
A or B
A= $path =~ s/:$param_name\b/$value/B= $path =~ s/\{$param_name(?::[^}]*)?\}/$value/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 455 | || $path =~ s/\{$param_name(?::[^}]*)?\}/$value/ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 456 | || $path =~ s/\*$param_name\b/$value/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 457 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 459 | # Prepend mount prefix | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 460 | 30 | ▶ | $path = $prefix . $path if $prefix; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 462 | # Add query string if any | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 463 | 30 | ▶ | if (%$query_params) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 464 | 4 | my @pairs; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 465 | 4 | for my $key (sort keys %$query_params) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 466 | 5 | my $value = $query_params->{$key}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 467 | # Simple URL encoding | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 468 | 0 | $key =~ s/([^A-Za-z0-9\-_.~])/sprintf("%%%02X", ord($1))/ge; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 469 | 5 | $value =~ s/([^A-Za-z0-9\-_.~])/sprintf("%%%02X", ord($1))/ge; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 470 | 5 | push @pairs, "$key=$value"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 471 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 472 | 4 | $path .= '?' . join('&', @pairs); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 473 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 475 | 30 | return $path; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 476 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 478 | 7 | ▶ | sub _include_router { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_include_router: called CC=13 SCAR=39.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 479 | 7 | my ($self, $source) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 481 | # Re-register HTTP routes through route() (stack applies prefix/middleware) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 482 | # Re-registered apps are already compiled coderefs; to_app is identity on coderefs | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 483 | 7 | for my $route (@{$source->{routes}}) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 484 | $self->route( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 485 | $route->{method}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 486 | $route->{path}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 487 | 11 | [@{$route->{middleware}}], | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 488 | $route->{app}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 489 | 11 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 490 | 11 | ▶ | if ($route->{name}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 491 | 6 | $self->name($route->{name}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 492 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 493 | 11 | ▶ | if ($route->{_user_constraints} && @{$route->{_user_constraints}}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$route->{"_user_constraints"} and @{$route->{"_user_constraints"};}
MC/DC50%1/2
$route->{"_user_constraints"} and @{$route->{"_user_constraints"};}
$route->{"_user_constraints"}
@{$route->{"_user_constraints"};}
Truth table66%2/3
A and B
A= $route->{"_user_constraints"}B= @{$route->{"_user_constraints"};}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 494 | 1 | my %uc = map { $_->[0] => $_->[1] } @{$route->{_user_constraints}}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 495 | 1 | $self->constraints(%uc); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 496 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 497 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 499 | # Re-register WebSocket routes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 500 | 7 | for my $route (@{$source->{websocket_routes}}) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 501 | $self->websocket( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 502 | $route->{path}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 503 | 0 | [@{$route->{middleware}}], | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 504 | $route->{app}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 505 | 0 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 506 | 0 | ▶ | if ($route->{name}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 507 | 0 | $self->name($route->{name}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 508 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 509 | 0 | ▶ | if ($route->{_user_constraints} && @{$route->{_user_constraints}}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$route->{"_user_constraints"} and @{$route->{"_user_constraints"};}
MC/DC0%0/2
$route->{"_user_constraints"} and @{$route->{"_user_constraints"};}
$route->{"_user_constraints"}
@{$route->{"_user_constraints"};}
Truth table0%0/3
A and B
A= $route->{"_user_constraints"}B= @{$route->{"_user_constraints"};}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 510 | 0 | my %uc = map { $_->[0] => $_->[1] } @{$route->{_user_constraints}}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 511 | 0 | $self->constraints(%uc); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 512 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 513 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 515 | # Re-register SSE routes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 516 | 7 | for my $route (@{$source->{sse_routes}}) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 517 | $self->sse( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 518 | $route->{path}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 519 | 0 | [@{$route->{middleware}}], | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 520 | $route->{app}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 521 | 0 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 522 | 0 | ▶ | if ($route->{name}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 523 | 0 | $self->name($route->{name}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 524 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 525 | 0 | ▶ | if ($route->{_user_constraints} && @{$route->{_user_constraints}}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$route->{"_user_constraints"} and @{$route->{"_user_constraints"};}
MC/DC0%0/2
$route->{"_user_constraints"} and @{$route->{"_user_constraints"};}
$route->{"_user_constraints"}
@{$route->{"_user_constraints"};}
Truth table0%0/3
A and B
A= $route->{"_user_constraints"}B= @{$route->{"_user_constraints"};}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 526 | 0 | my %uc = map { $_->[0] => $_->[1] } @{$route->{_user_constraints}}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 527 | 0 | $self->constraints(%uc); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 528 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 529 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 530 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 532 | 238 | ▶ | sub _parse_route_args { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_route_args: called CC=4 SCAR=14.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 533 | 238 | my ($self, @args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 534 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 535 | 238 | ▶ | if (@args == 2 && ref($args[0]) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
@args == 2 and ref $args[0] eq "ARRAY"
MC/DC50%1/2
@args == 2 and ref $args[0] eq "ARRAY"
@args == 2
ref $args[0] eq "ARRAY"
Truth table66%2/3
A and B
A= @args == 2B= ref $args[0] eq "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 536 | # (\@middleware, $app) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 537 | 37 | my ($middleware, $app) = @args; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 538 | 37 | $self->_validate_middleware($middleware); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 539 | 34 | return ($middleware, $app); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 540 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 541 | elsif (@args == 1) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 542 | # ($app) - no middleware | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 543 | 201 | return ([], $args[0]); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 544 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 545 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 546 | 0 | croak 'Invalid route arguments: expected ($app) or (\@middleware => $app)'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 547 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 548 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 550 | 37 | ▶ | sub _validate_middleware { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_validate_middleware: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 551 | 37 | my ($self, $middleware) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 552 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 553 | 37 | for my $mw (@$middleware) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 554 | 28 | ▶ | if (ref($mw) eq 'CODE') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 555 | 23 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 556 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 557 | ▶ | elsif (blessed($mw) && $mw->can('wrap')) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
builtin::blessed($mw) and $mw->can("wrap")
MC/DC100%2/2
builtin::blessed($mw) and $mw->can("wrap")
builtin::blessed($mw)
$mw->can("wrap")
Truth table100%3/3
A and B
A= builtin::blessed($mw)B= $mw->can("wrap")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 558 | 2 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 559 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 560 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 561 | 3 | ▶ | my $type = ref($mw) || 'scalar'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
ref $mw || 'scalar'
MC/DC100%1/1
ref $mw || 'scalar'
ref $mw
Truth table100%2/2
A
A= ref $mw
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 562 | 3 | croak "Invalid middleware: expected coderef factory or object with ->wrap method, got $type"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 563 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 564 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 565 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 566 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 567 | 165 | ▶ | sub _build_middleware_chain { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_middleware_chain: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 568 | 165 | my ($self, $middlewares, $app) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 569 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 570 | 165 | ▶ | return $app unless $middlewares && @$middlewares; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$middlewares and @$middlewares
MC/DC50%1/2
$middlewares and @$middlewares
$middlewares
@$middlewares
Truth table66%2/3
A and B
A= $middlewaresB= @$middlewares
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 571 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 572 | 22 | my $chain = $app; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 573 | 22 | for my $mw (reverse @$middlewares) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 574 | 27 | ▶ | $chain = ref($mw) eq 'CODE' ? $mw->($chain) : $mw->wrap($chain); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 575 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 576 | 22 | return $chain; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 577 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 578 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 579 | 109 | ▶ | sub to_app { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
to_app: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 580 | 109 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 581 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 582 | 109 | my @routes = @{$self->{routes}}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 583 | 109 | my @websocket_routes = @{$self->{websocket_routes}}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 584 | 109 | my @sse_routes = @{$self->{sse_routes}}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 585 | 109 | my @mounts = @{$self->{mounts}}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 586 | 109 | my $not_found = $self->{not_found}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 587 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 588 | # Pre-build middleware chains for efficiency | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 589 | 109 | for my $route (@routes, @websocket_routes, @sse_routes) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 590 | 150 | $route->{_handler} = $self->_build_middleware_chain($route->{middleware}, $route->{app}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 591 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 592 | 109 | for my $m (@mounts) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 593 | 15 | $m->{_handler} = $self->_build_middleware_chain($m->{middleware}, $m->{app}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 594 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 595 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 596 | # Helper to check mounts | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 597 | 40 | ▶ | my $check_mounts = async sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 598 | 40 | my ($scope, $receive, $send, $path) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 599 | 40 | for my $m (sort { length($b->{prefix}) <=> length($a->{prefix}) } @mounts) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 600 | 17 | my $prefix = $m->{prefix}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 601 | 17 | ▶ | if ($path eq $prefix || $path =~ m{^\Q$prefix\E(/.*)$}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$path eq $prefix or $path =~ m[^\Q$prefix\E(/.*)$]
MC/DC100%2/2
$path eq $prefix or $path =~ m[^\Q$prefix\E(/.*)$]
$path eq $prefix
$path =~ m[^\Q$prefix\E(/.*)$]
Truth table100%3/3
A or B
A= $path eq $prefixB= $path =~ m[^\Q$prefix\E(/.*)$]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 602 | 15 | ▶ | my $sub_path = $1 // '/'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$1 // "/"
MC/DC100%1/1
$1 // "/"
$1
Truth table100%2/2
A
A= $1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 603 | my $new_scope = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 604 | %$scope, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 605 | path => $sub_path, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 606 | 15 | ▶ | root_path => ($scope->{root_path} // '') . $prefix, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$scope->{"root_path"} // ""
MC/DC100%1/1
$scope->{"root_path"} // ""
$scope->{"root_path"}
Truth table100%2/2
A
A= $scope->{"root_path"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 607 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 608 | 15 | await $m->{_handler}->($new_scope, $receive, $send); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 609 | 15 | return 1; # Matched | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 610 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 611 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 612 | 25 | return 0; # No match | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 613 | 109 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 614 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 615 | # Emit an unmatched-route response using the event family that matches the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 616 | # scope. HTTP uses plain http.response.*; SSE and WebSocket scopes only | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 617 | # accept their namespaced decline events (sse.http.response.* / | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 618 | # websocket.http.response.*) before a stream/handshake starts — a plain | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 619 | # http.response.* on those scopes raises on a conforming server. A custom | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 620 | # not_found app, if configured, takes over for every scope type. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 621 | 25 | ▶ | my $send_not_found = async sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 622 | 25 | my ($scope, $receive, $send) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 623 | 25 | ▶ | if ($not_found) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 624 | 3 | await $not_found->($scope, $receive, $send); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 625 | 3 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 626 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 627 | 22 | ▶ | my $type = $scope->{type} // 'http'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$scope->{"type"} // "http"
MC/DC100%1/1
$scope->{"type"} // "http"
$scope->{"type"}
Truth table100%2/2
A
A= $scope->{"type"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 628 | 22 | ▶ | my $prefix = $type eq 'http' ? 'http.response' : "$type.http.response"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 629 | 22 | await $send->({ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 630 | type => "$prefix.start", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 631 | status => 404, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 632 | headers => [['content-type', 'text/plain']], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 633 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 634 | 22 | await $send->({ type => "$prefix.body", body => 'Not Found', more => 0 }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 635 | 109 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 636 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 637 | 172 | ▶ | return async sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=31 SCAR=34.4
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 638 | 172 | my ($scope, $receive, $send) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 639 | 172 | ▶ | my $type = $scope->{type} // 'http'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$scope->{"type"} // "http"
MC/DC100%1/1
$scope->{"type"} // "http"
$scope->{"type"}
Truth table100%2/2
A
A= $scope->{"type"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 640 | 172 | ▶ | my $method = uc($scope->{method} // ''); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$scope->{"method"} // ""
MC/DC100%1/1
$scope->{"method"} // ""
$scope->{"method"}
Truth table100%2/2
A
A= $scope->{"method"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 641 | 172 | ▶ | my $path = $scope->{path} // '/'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$scope->{"path"} // "/"
MC/DC100%1/1
$scope->{"path"} // "/"
$scope->{"path"}
Truth table100%2/2
A
A= $scope->{"path"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 642 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 643 | # Ignore lifespan events | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 644 | 172 | ▶ | return if $type eq 'lifespan'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 645 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 646 | # WebSocket routes (path-only matching) - check before mounts | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 647 | 170 | ▶ | if ($type eq 'websocket') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 648 | 14 | for my $route (@websocket_routes) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 649 | 12 | ▶ | if (my @captures = ($path =~ $route->{regex})) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 650 | 10 | my %params; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 651 | 10 | for my $i (0 .. $#{$route->{names}}) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 652 | 6 | $params{$route->{names}[$i]} = $captures[$i]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 653 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 654 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 655 | # Check constraints — skip route if any fail | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 656 | 10 | ▶ | next unless $self->_check_constraints($route, \%params); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 657 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 658 | my $new_scope = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 659 | %$scope, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 660 | path_params => \%params, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 661 | 'pagi.router' => { route => $route->{path} }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 662 | 9 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 663 | 9 | await $route->{_handler}->($new_scope, $receive, $send); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 664 | 9 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 665 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 666 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 667 | # No websocket route matched - try mounts as fallback | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 668 | 5 | ▶ | if (await $check_mounts->($scope, $receive, $send, $path)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 669 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 670 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 671 | # No mount matched either - 404 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 672 | 5 | await $send_not_found->($scope, $receive, $send); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 673 | 5 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 674 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 675 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 676 | # SSE routes (path-only matching) - check before mounts | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 677 | 156 | ▶ | if ($type eq 'sse') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 678 | 12 | for my $route (@sse_routes) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 679 | 10 | ▶ | if (my @captures = ($path =~ $route->{regex})) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 680 | 10 | my %params; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 681 | 10 | for my $i (0 .. $#{$route->{names}}) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 682 | 6 | $params{$route->{names}[$i]} = $captures[$i]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 683 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 684 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 685 | # Check constraints — skip route if any fail | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 686 | 10 | ▶ | next unless $self->_check_constraints($route, \%params); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 687 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 688 | my $new_scope = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 689 | %$scope, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 690 | path_params => \%params, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 691 | 'pagi.router' => { route => $route->{path} }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 692 | 9 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 693 | 9 | await $route->{_handler}->($new_scope, $receive, $send); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 694 | 8 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 695 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 696 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 697 | # No SSE route matched - try mounts as fallback | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 698 | 3 | ▶ | if (await $check_mounts->($scope, $receive, $send, $path)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 699 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 700 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 701 | # No mount matched either - 404 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 702 | 3 | await $send_not_found->($scope, $receive, $send); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 703 | 3 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 704 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 705 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 706 | # HTTP routes (method + path matching) - check routes first | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 707 | # HEAD should match GET routes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 708 | 144 | ▶ | my $match_method = $method eq 'HEAD' ? 'GET' : $method; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 709 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 710 | 144 | my @method_matches; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 711 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 712 | 144 | for my $route (@routes) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 713 | 189 | ▶ | if (my @captures = ($path =~ $route->{regex})) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 714 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 715 | # Build params FIRST (needed for constraint checking) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 716 | 128 | my %params; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 717 | 128 | for my $i (0 .. $#{$route->{names}}) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 718 | 43 | $params{$route->{names}[$i]} = $captures[$i]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 719 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 720 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 721 | # Check constraints — skip route if any fail | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 722 | 128 | ▶ | next unless $self->_check_constraints($route, \%params); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 723 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 724 | # Check method | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 725 | 117 | my $route_method = $route->{method}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 726 | my $method_match = ref($route_method) eq 'ARRAY' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 727 | 117 | ▶ | ? (grep { $_ eq $match_method || $_ eq $method } @$route_method) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition77%7/9
$_ eq $match_method || $_ eq $method
$route_method eq "*" or $route_method eq $match_method or $route_method eq $method
$route_method eq "*" or $route_method eq $match_method
MC/DC50%1/2
$_ eq $match_method || $_ eq $method
$_ eq $match_method
$_ eq $method
MC/DC66%2/3
$route_method eq "*" or $route_method eq $match_method or $route_method eq $method
$route_method eq "*"
$route_method eq $match_method
$route_method eq $method
Truth table66%2/3
A || B
A= $_ eq $match_methodB= $_ eq $method
Truth table75%3/4
A or B or C
A= $route_method eq "*"B= $route_method eq $match_methodC= $route_method eq $method
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 728 | : ($route_method eq '*' || $route_method eq $match_method || $route_method eq $method); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 729 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 730 | 117 | ▶ | if ($method_match) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 731 | my $new_scope = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 732 | %$scope, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 733 | path_params => \%params, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 734 | 'pagi.router' => { route => $route->{path} }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 735 | 104 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 736 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 737 | 104 | return await $route->{_handler}->($new_scope, $receive, $send); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 738 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 739 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 740 | 13 | ▶ | if (ref($route->{method}) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 741 | 3 | push @method_matches, @{$route->{method}}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 742 | } elsif ($route->{method} ne '*') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 743 | 10 | push @method_matches, $route->{method}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 744 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 745 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 746 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 747 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 748 | # Path matched but method didn't - 405 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 749 | 40 | ▶ | if (@method_matches) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 750 | 8 | my $allowed = join ', ', sort keys %{{ map { $_ => 1 } @method_matches }}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 751 | 8 | await $send->({ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 752 | type => 'http.response.start', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 753 | status => 405, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 754 | headers => [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 755 | ['content-type', 'text/plain'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 756 | ['allow', $allowed], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 757 | ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 758 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 759 | 8 | await $send->({ type => 'http.response.body', body => 'Method Not Allowed', more => 0 }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 760 | 8 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 761 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 762 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 763 | # No HTTP route matched - try mounts as fallback | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 764 | 32 | ▶ | if (await $check_mounts->($scope, $receive, $send, $path)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 765 | 15 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 766 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 767 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 768 | # No mount matched either - 404 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 769 | 17 | await $send_not_found->($scope, $receive, $send); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 770 | 109 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 771 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 772 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 773 | 13 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 774 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 775 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||