blib/lib/PAGI/App/Router.pm

filter: Statementstmt 91.0% 366 / 402 Branchbran 85.7% 120 / 140 Conditioncond 69.3% 61 / 88 MC/DCmcdc 52.9% 27 / 51 Subroutinesub 89.1% 33 / 37 Podpod 55.0% 11 / 20 totaltotal 83.7% 618 / 738 CC 130 SCAR 27.6
CC
130
Cov
87%
_include_router
50.1
__ANON__
35.2
group
12.2
CRAP 15.8
« lib/PAGI/Middleware/Static.pm ↑ Summary blib/lib/PAGI/Middleware/CORS.pm »
1 package PAGI::App::Router;
2 11 $PAGI::App::Router::VERSION = '0.002000';
3 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶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 sub new {
87 136 ▶ my ($class, %args) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
88  
89 return bless {
90 routes => [],
91 websocket_routes => [],
92 sse_routes => [],
93 mounts => [],
94 not_found => $args{not_found},
95 136 _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 sub mount {
103 20 ▶ my ($self, $prefix, @rest) = @_;
Subroutine mount: called CC=3 SCAR=11.0
Pod documented
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
BranchTrueFalseTotal
if (builtin::blessed($app_or_router) and $app_or_router->isa("PAGI::App::Router")) 3 17 20
Condition100%3/3
builtin::blessed($app_or_router) and $app_or_router->isa("PAGI::App::Router")
!ll&&!rl&&r
1523
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")
ABresult
0X0
100
111
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 139 ▶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 sub any {
135 10 ▶ my ($self, $path, @rest) = @_;
Subroutine any: called CC=6 SCAR=17.9
Pod documented
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
BranchTrueFalseTotal
if (@rest >= 2 and not ref $rest[-2] and $rest[-2] eq "method") 4 6 10
Condition83%5/6
@rest >= 2 and not ref $rest[-2] and $rest[-2] eq "method"
!ll&&!rl&&r
604
@rest >= 2 and not ref $rest[-2]
!ll&&!rl&&r
514
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"
ABCresult
0XX0
10X0
1100
1111
140 4 %opts = splice(@rest, -2);
141 }
142  
143 10 ▶ my $method = $opts{method} // '*';
Condition100%2/2
$opts{"method"} // "*"
l!l
46
MC/DC100%1/1
$opts{"method"} // "*"
$opts{"method"}
Truth table100%2/2
A
A= $opts{"method"}
Aresult
11
00
144 10 ▶ if (ref($method) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $method eq "ARRAY") 4 6 10
145 4 $method = [map { uc($_) } @$method];
146 }
147  
148 10 $self->route($method, $path, @rest);
149 }
150  
151 sub group {
152 24 ▶ my ($self, $prefix, @rest) = @_;
Subroutine group: called CC=12 SCAR=25.0
Pod documented
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
BranchTrueFalseTotal
if (ref $target eq 'CODE') { } 14 10 24
elsif (builtin::blessed($target) and $target->isa('PAGI::App::Router')) { } 6 4 10
elsif (not ref $target) { } 3 1 4
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")
!ll&&!rl&&r
406
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")
ABresult
0X0
100
111
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
BranchTrueFalseTotal
eval "require $pkg; 1" or croak("Failed to load '${pkg}': $@") 1 2 3
181 }
182 2 ▶ croak "'$pkg' does not have a router() method" unless $pkg->can('router');
Branch: 1 branch
BranchTrueFalseTotal
unless $pkg->can("router") 1 1 2
183 1 my $router_obj = $pkg->router;
184 1 ▶ croak "'${pkg}->router()' must return a PAGI::App::Router, got "
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($router_obj) and $router_obj->isa("PAGI::App::Router") 0 1 1
Condition20%1/5
builtin::blessed($router_obj) and $router_obj->isa("PAGI::App::Router")
!ll&&!rl&&r
001
ref $router_obj || 'scalar'
l!l
00
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")
ABresult
0X0
100
111
Truth table0%0/2
A
A= ref $router_obj
Aresult
11
00
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'
l!l
10
MC/DC0%0/1
ref $target || 'scalar'
ref $target
Truth table50%1/2
A
A= ref $target
Aresult
11
00
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
BranchTrueFalseTotal
@new_names ? : 6 15 21
202  
203 21 $self->{_last_route} = undef;
204 21 $self->{_last_mount} = undef;
205  
206 21 return $self;
207 }
208  
209 sub websocket {
210 10 ▶ my ($self, $path, @rest) = @_;
Subroutine websocket: called CC=2 SCAR=6.9
Pod documented
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 sub sse {
237 10 ▶ my ($self, $path, @rest) = @_;
Subroutine sse: called CC=2 SCAR=6.9
Pod documented
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 sub route {
264 170 ▶ my ($self, $method, $path, @rest) = @_;
Subroutine route: called CC=4 SCAR=13.9
Pod undocumented
265  
266 170 my ($middleware, $app) = $self->_parse_route_args(@rest);
267 167 $app = PAGI::Utils::to_app($app);
268  
269 # Apply accumulated group context (reverse: innermost prefix first)
270 167 for my $ctx (reverse @{$self->{_group_stack}}) {
271 32 $path = $ctx->{prefix} . $path;
272 32 unshift @$middleware, @{$ctx->{middleware}};
273 }
274  
275 167 my ($regex, $names, $constraints) = $self->_compile_path($path);
276 167 ▶ my $route = {
Branch: 2 branches
BranchTrueFalseTotal
ref $method eq 'ARRAY' ? : 4 163 167
$method eq '*' ? : 6 157 163
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 167 push @{$self->{routes}}, $route;
286 167 $self->{_last_route} = $route;
287 167 $self->{_last_mount} = undef; # Clear mount tracking
288  
289 167 return $self;
290 }
291  
292 sub _compile_path {
293 187 ▶ my ($self, $path) = @_;
Subroutine _compile_path: called CC=7 SCAR=19.5
294  
295 187 my @names;
296 my @constraints;
297 187 my $regex = '';
298  
299 # Tokenize the path
300 187 my $remaining = $path;
301 187 ▶ while (length $remaining) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $remaining) 256 187 443
302 # {name:pattern} — constrained parameter
303 256 ▶ if ($remaining =~ s/^\{(\w+):([^}]+)\}//) {
Branch: 5 branches
BranchTrueFalseTotal
if ($remaining =~ s/^\{(\w+):([^}]+)\}//) { } 11 245 256
elsif ($remaining =~ s/^\{(\w+)\}//) { } 2 243 245
elsif ($remaining =~ s/^\*(\w+)//) { } 3 240 243
elsif ($remaining =~ s/^:(\w+)//) { } 48 192 240
elsif ($remaining =~ s/^([^{:*]+)//) { } 192 0 192
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 192 $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 187 return (qr{^$regex$}, \@names, \@constraints);
334 }
335  
336 sub _check_constraints {
337 146 ▶ my ($self, $route, $params) = @_;
Subroutine _check_constraints: called CC=7 SCAR=19.6
338 146 ▶ for my $constraints_list ($route->{constraints} // [], $route->{_user_constraints} // []) {
Condition75%3/4
$route->{"constraints"} // []
l!l
1460
$route->{"_user_constraints"} // []
l!l
12134
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"}
Aresult
11
00
Truth table100%2/2
A
A= $route->{"_user_constraints"}
Aresult
11
00
339 284 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)
l!l
280
MC/DC0%0/1
$params->{$name} // (return 0)
$params->{$name}
Truth table50%1/2
A
A= $params->{$name}
Aresult
11
00
342 28 ▶ return 0 unless $value =~ m/^(?:$pattern)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /^(?:$pattern)$/ 13 15 28
343 }
344 }
345 133 return 1;
346 }
347  
348 # ============================================================
349 # Named Routes
350 # ============================================================
351  
352 sub name {
353 42 ▶ my ($self, $name) = @_;
Subroutine name: called CC=5 SCAR=16.1
Pod documented
354  
355 42 ▶ croak "name() called without a preceding route" unless $self->{_last_route};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_last_route"} 2 40 42
356 40 ▶ croak "Route name required" unless defined $name && length $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and length $name 1 39 40
Condition66%2/3
defined $name and length $name
!ll&&!rl&&r
0139
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
ABresult
0X0
100
111
357 39 ▶ croak "Named route '$name' already exists" if exists $self->{_named_routes}{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"_named_routes"}{$name} 1 38 39
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 sub constraints {
371 9 ▶ my ($self, %new_constraints) = @_;
Subroutine constraints: called CC=5 SCAR=16.1
Pod documented
372  
373 9 ▶ croak "constraints() called without a preceding route" unless $self->{_last_route};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_last_route"} 1 8 9
374  
375 8 my $route = $self->{_last_route};
376 8 ▶ my $user_constraints = $route->{_user_constraints} //= [];
Condition50%1/2
$route->{"_user_constraints"} //= []
l!l
08
MC/DC0%0/1
$route->{"_user_constraints"} //= []
$route->{"_user_constraints"}
Truth table50%1/2
A
A= $route->{"_user_constraints"}
Aresult
11
00
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
BranchTrueFalseTotal
unless ref $pattern eq "Regexp" 1 7 8
381 unless ref($pattern) eq 'Regexp';
382 7 push @$user_constraints, [$name, $pattern];
383 }
384  
385 7 return $self;
386 }
387  
388 sub as {
389 9 ▶ my ($self, $namespace) = @_;
Subroutine as: called CC=11 SCAR=24.3
Pod documented
390  
391 9 ▶ croak "Namespace required" unless defined $namespace && length $namespace;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $namespace and length $namespace 0 9 9
Condition33%1/3
defined $namespace and length $namespace
!ll&&!rl&&r
009
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
ABresult
0X0
100
111
392  
393 # Handle group namespacing
394 9 ▶ if ($self->{_last_group_names} && @{$self->{_last_group_names}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_last_group_names"} and @{$self->{"_last_group_names"};}) 3 6 9
Condition66%2/3
$self->{"_last_group_names"} and @{$self->{"_last_group_names"};}
!ll&&!rl&&r
603
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"};}
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if exists $self->{"_named_routes"}{$full_name} 0 5 5
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
BranchTrueFalseTotal
unless $self->{"_last_mount"} 2 4 6
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
BranchTrueFalseTotal
unless $sub_router 1 3 4
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"} // ""
l!l
60
MC/DC0%0/1
$info->{"prefix"} // ""
$info->{"prefix"}
Truth table50%1/2
A
A= $info->{"prefix"}
Aresult
11
00
425 };
426 }
427  
428 3 return $self;
429 }
430  
431 sub named_routes {
432 3 ▶ my ($self) = @_;
Subroutine named_routes: called CC=1 SCAR=0.0
Pod documented
433 3 return { %{$self->{_named_routes}} };
434 }
435  
436 sub uri_for {
437 32 ▶ my ($self, $name, $path_params, $query_params) = @_;
Subroutine uri_for: called CC=12 SCAR=24.9
Pod documented
438  
439 32 ▶ $path_params //= {};
Condition100%2/2
$path_params //= {}
l!l
1913
MC/DC100%1/1
$path_params //= {}
$path_params
Truth table100%2/2
A
A= $path_params
Aresult
11
00
440 32 ▶ $query_params //= {};
Condition100%2/2
$query_params //= {}
l!l
428
MC/DC100%1/1
$query_params //= {}
$query_params
Truth table100%2/2
A
A= $query_params
Aresult
11
00
441  
442 32 ▶ my $info = $self->{_named_routes}{$name}
Branch: 1 branch
BranchTrueFalseTotal
my $info = $self->{"_named_routes"}{$name} or croak("Unknown route name: '${name}'") 1 31 32
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"} // ""
l!l
310
MC/DC0%0/1
$info->{"prefix"} // ""
$info->{"prefix"}
Truth table50%1/2
A
A= $info->{"prefix"}
Aresult
11
00
447  
448 # Substitute path parameters
449 31 for my $param_name (@{$info->{names}}) {
450 16 ▶ unless (exists $path_params->{$param_name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $path_params->{$param_name}) 1 15 16
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
BranchTrueFalseTotal
$path =~ s/:$param_name\b/$value/ or $path =~ s/\{$param_name(?::[^}]*)?\}/$value/ or $path =~ s/\*$param_name\b/$value/ 1 14 15
Condition100%3/3
$path =~ s/:$param_name\b/$value/ or $path =~ s/\{$param_name(?::[^}]*)?\}/$value/
l!l&&r!l&&!r
1131
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/
ABresult
1X1
011
000
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
BranchTrueFalseTotal
if $prefix 4 26 30
461  
462 # Add query string if any
463 30 ▶ if (%$query_params) {
Branch: 1 branch
BranchTrueFalseTotal
if (%$query_params) 4 26 30
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 5 $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 sub _include_router {
479 7 ▶ my ($self, $source) = @_;
Subroutine _include_router: called CC=13 SCAR=39.1
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
BranchTrueFalseTotal
if ($route->{"name"}) 6 5 11
491 6 $self->name($route->{name});
492 }
493 11 ▶ if ($route->{_user_constraints} && @{$route->{_user_constraints}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($route->{"_user_constraints"} and @{$route->{"_user_constraints"};}) 1 10 11
Condition66%2/3
$route->{"_user_constraints"} and @{$route->{"_user_constraints"};}
!ll&&!rl&&r
1001
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"};}
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if ($route->{"name"}) 0 0 0
507 0 $self->name($route->{name});
508 }
509 0 ▶ if ($route->{_user_constraints} && @{$route->{_user_constraints}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($route->{"_user_constraints"} and @{$route->{"_user_constraints"};}) 0 0 0
Condition0%0/3
$route->{"_user_constraints"} and @{$route->{"_user_constraints"};}
!ll&&!rl&&r
000
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"};}
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if ($route->{"name"}) 0 0 0
523 0 $self->name($route->{name});
524 }
525 0 ▶ if ($route->{_user_constraints} && @{$route->{_user_constraints}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($route->{"_user_constraints"} and @{$route->{"_user_constraints"};}) 0 0 0
Condition0%0/3
$route->{"_user_constraints"} and @{$route->{"_user_constraints"};}
!ll&&!rl&&r
000
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"};}
ABresult
0X0
100
111
526 0 my %uc = map { $_->[0] => $_->[1] } @{$route->{_user_constraints}};
527 0 $self->constraints(%uc);
528 }
529 }
530 }
531  
532 sub _parse_route_args {
533 234 ▶ my ($self, @args) = @_;
Subroutine _parse_route_args: called CC=4 SCAR=14.2
534  
535 234 ▶ if (@args == 2 && ref($args[0]) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (@args == 2 and ref $args[0] eq 'ARRAY') { } 57 177 234
elsif (@args == 1) { } 177 0 177
Condition66%2/3
@args == 2 and ref $args[0] eq "ARRAY"
!ll&&!rl&&r
177057
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"
ABresult
0X0
100
111
536 # (\@middleware, $app)
537 57 my ($middleware, $app) = @args;
538 57 $self->_validate_middleware($middleware);
539 54 return ($middleware, $app);
540 }
541 elsif (@args == 1) {
542 # ($app) - no middleware
543 177 return ([], $args[0]);
544 }
545 else {
546 0 croak 'Invalid route arguments: expected ($app) or (\@middleware => $app)';
547 }
548 }
549  
550 sub _validate_middleware {
551 57 ▶ my ($self, $middleware) = @_;
Subroutine _validate_middleware: called CC=6 SCAR=17.9
552  
553 57 for my $mw (@$middleware) {
554 49 ▶ if (ref($mw) eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $mw eq 'CODE') { } 44 5 49
elsif (builtin::blessed($mw) and $mw->can('call')) { } 2 3 5
555 # Coderef is valid
556 44 next;
557 }
558 ▶ elsif (blessed($mw) && $mw->can('call')) {
Condition100%3/3
builtin::blessed($mw) and $mw->can("call")
!ll&&!rl&&r
212
MC/DC100%2/2
builtin::blessed($mw) and $mw->can("call")
builtin::blessed($mw) $mw->can("call")
Truth table100%3/3
A and B
A= builtin::blessed($mw)B= $mw->can("call")
ABresult
0X0
100
111
559 # PAGI::Middleware instance with call() method
560 2 next;
561 }
562 else {
563 3 ▶ my $type = ref($mw) || 'scalar';
Condition100%2/2
ref $mw || 'scalar'
l!l
21
MC/DC100%1/1
ref $mw || 'scalar'
ref $mw
Truth table100%2/2
A
A= ref $mw
Aresult
11
00
564 3 croak "Invalid middleware: expected coderef or object with ->call method, got $type";
565 }
566 }
567 }
568  
569 sub _build_middleware_chain {
570 161 ▶ my ($self, $middlewares, $app) = @_;
Subroutine _build_middleware_chain: called CC=5 SCAR=16.1
571  
572 161 ▶ return $app unless $middlewares && @$middlewares;
Branch: 1 branch
BranchTrueFalseTotal
unless $middlewares and @$middlewares 119 42 161
Condition66%2/3
$middlewares and @$middlewares
!ll&&!rl&&r
011942
MC/DC50%1/2
$middlewares and @$middlewares
$middlewares @$middlewares
Truth table66%2/3
A and B
A= $middlewaresB= @$middlewares
ABresult
0X0
100
111
573  
574 42 my $chain = $app;
575  
576 42 for my $mw (reverse @$middlewares) {
577 48 my $next = $chain;
578  
579 48 ▶ if (ref($mw) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $mw eq 'CODE') { } 46 2 48
580 # Coderef with $next signature. Forward a transformed channel when the
581 # middleware passes one to $next (so a coderef can wrap $receive/$send
582 # or replace $scope, like an object middleware); otherwise continue
583 # with the inherited triple, preserving the arg-less $next->() form.
584 35 ▶ $chain = async sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
585 35 my ($scope, $receive, $send) = @_;
586 31 ▶ await $mw->($scope, $receive, $send, async sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
587 31 ▶ my ($s, $r, $sd) = @_ ? @_ : ($scope, $receive, $send);
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 2 29 31
588 31 await $next->($s, $r, $sd);
589 35 });
590 46 };
591 }
592 else {
593 # PAGI::Middleware instance - use existing call()
594 2 ▶ $chain = async sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
595 2 my ($scope, $receive, $send) = @_;
596 2 await $mw->call($scope, $receive, $send, $next);
597 2 };
598 }
599 }
600  
601 42 return $chain;
602 }
603  
604 sub to_app {
605 104 ▶ my ($self) = @_;
Subroutine to_app: called CC=3 SCAR=11.0
Pod documented
606  
607 104 my @routes = @{$self->{routes}};
608 104 my @websocket_routes = @{$self->{websocket_routes}};
609 104 my @sse_routes = @{$self->{sse_routes}};
610 104 my @mounts = @{$self->{mounts}};
611 104 my $not_found = $self->{not_found};
612  
613 # Pre-build middleware chains for efficiency
614 104 for my $route (@routes, @websocket_routes, @sse_routes) {
615 146 $route->{_handler} = $self->_build_middleware_chain($route->{middleware}, $route->{app});
616 }
617 104 for my $m (@mounts) {
618 15 $m->{_handler} = $self->_build_middleware_chain($m->{middleware}, $m->{app});
619 }
620  
621 # Helper to check mounts
622 34 ▶ my $check_mounts = async sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
623 34 my ($scope, $receive, $send, $path) = @_;
624 34 for my $m (sort { length($b->{prefix}) <=> length($a->{prefix}) } @mounts) {
625 17 my $prefix = $m->{prefix};
626 17 ▶ if ($path eq $prefix || $path =~ m{^\Q$prefix\E(/.*)$}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path eq $prefix or $path =~ m[^\Q$prefix\E(/.*)$]) 15 2 17
Condition100%3/3
$path eq $prefix or $path =~ m[^\Q$prefix\E(/.*)$]
l!l&&r!l&&!r
1142
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(/.*)$]
ABresult
1X1
011
000
627 15 ▶ my $sub_path = $1 // '/';
Condition50%1/2
$1 // "/"
l!l
150
MC/DC0%0/1
$1 // "/"
$1
Truth table50%1/2
A
A= $1
Aresult
11
00
628 my $new_scope = {
629 %$scope,
630 path => $sub_path,
631 15 ▶ root_path => ($scope->{root_path} // '') . $prefix,
Condition100%2/2
$scope->{"root_path"} // ""
l!l
312
MC/DC100%1/1
$scope->{"root_path"} // ""
$scope->{"root_path"}
Truth table100%2/2
A
A= $scope->{"root_path"}
Aresult
11
00
632 };
633 15 await $m->{_handler}->($new_scope, $receive, $send);
634 15 return 1; # Matched
635 }
636 }
637 19 return 0; # No match
638 104 };
639  
640 164 ▶ return async sub {
Subroutine __ANON__: called CC=34 SCAR=35.6
641 164 my ($scope, $receive, $send) = @_;
642 164 ▶ my $type = $scope->{type} // 'http';
Condition100%2/2
$scope->{"type"} // "http"
l!l
8084
MC/DC100%1/1
$scope->{"type"} // "http"
$scope->{"type"}
Truth table100%2/2
A
A= $scope->{"type"}
Aresult
11
00
643 164 ▶ my $method = uc($scope->{method} // '');
Condition100%2/2
$scope->{"method"} // ""
l!l
14321
MC/DC100%1/1
$scope->{"method"} // ""
$scope->{"method"}
Truth table100%2/2
A
A= $scope->{"method"}
Aresult
11
00
644 164 ▶ my $path = $scope->{path} // '/';
Condition100%2/2
$scope->{"path"} // "/"
l!l
1631
MC/DC100%1/1
$scope->{"path"} // "/"
$scope->{"path"}
Truth table100%2/2
A
A= $scope->{"path"}
Aresult
11
00
645  
646 # Ignore lifespan events
647 164 ▶ return if $type eq 'lifespan';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "lifespan" 2 162 164
648  
649 # WebSocket routes (path-only matching) - check before mounts
650 162 ▶ if ($type eq 'websocket') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "websocket") 12 150 162
651 12 for my $route (@websocket_routes) {
652 12 ▶ if (my @captures = ($path =~ $route->{regex})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@captures) = $path =~ /$route->{"regex"}/) 10 2 12
653 10 my %params;
654 10 for my $i (0 .. $#{$route->{names}}) {
655 6 $params{$route->{names}[$i]} = $captures[$i];
656 }
657  
658 # Check constraints — skip route if any fail
659 10 ▶ next unless $self->_check_constraints($route, \%params);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_check_constraints($route, \%params) 1 9 10
660  
661 my $new_scope = {
662 %$scope,
663 path_params => \%params,
664 'pagi.router' => { route => $route->{path} },
665 9 };
666 9 await $route->{_handler}->($new_scope, $receive, $send);
667 9 return;
668 }
669 }
670 # No websocket route matched - try mounts as fallback
671 3 ▶ if (await $check_mounts->($scope, $receive, $send, $path)) {
Branch: 1 branch
BranchTrueFalseTotal
if (XXX) 0 3 3
672 0 return;
673 }
674 # No mount matched either - 404
675 3 ▶ if ($not_found) {
Branch: 1 branch
BranchTrueFalseTotal
if ($not_found) { } 0 3 3
676 0 await $not_found->($scope, $receive, $send);
677 } else {
678 3 await $send->({
679 type => 'http.response.start',
680 status => 404,
681 headers => [['content-type', 'text/plain']],
682 });
683 3 await $send->({ type => 'http.response.body', body => 'Not Found', more => 0 });
684 }
685 3 return;
686 }
687  
688 # SSE routes (path-only matching) - check before mounts
689 150 ▶ if ($type eq 'sse') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "sse") 10 140 150
690 10 for my $route (@sse_routes) {
691 10 ▶ if (my @captures = ($path =~ $route->{regex})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@captures) = $path =~ /$route->{"regex"}/) 10 0 10
692 10 my %params;
693 10 for my $i (0 .. $#{$route->{names}}) {
694 6 $params{$route->{names}[$i]} = $captures[$i];
695 }
696  
697 # Check constraints — skip route if any fail
698 10 ▶ next unless $self->_check_constraints($route, \%params);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_check_constraints($route, \%params) 1 9 10
699  
700 my $new_scope = {
701 %$scope,
702 path_params => \%params,
703 'pagi.router' => { route => $route->{path} },
704 9 };
705 9 await $route->{_handler}->($new_scope, $receive, $send);
706 8 return;
707 }
708 }
709 # No SSE route matched - try mounts as fallback
710 1 ▶ if (await $check_mounts->($scope, $receive, $send, $path)) {
Branch: 1 branch
BranchTrueFalseTotal
if (XXX) 0 1 1
711 0 return;
712 }
713 # No mount matched either - 404
714 1 ▶ if ($not_found) {
Branch: 1 branch
BranchTrueFalseTotal
if ($not_found) { } 0 1 1
715 0 await $not_found->($scope, $receive, $send);
716 } else {
717 1 await $send->({
718 type => 'http.response.start',
719 status => 404,
720 headers => [['content-type', 'text/plain']],
721 });
722 1 await $send->({ type => 'http.response.body', body => 'Not Found', more => 0 });
723 }
724 1 return;
725 }
726  
727 # HTTP routes (method + path matching) - check routes first
728 # HEAD should match GET routes
729 140 ▶ my $match_method = $method eq 'HEAD' ? 'GET' : $method;
Branch: 1 branch
BranchTrueFalseTotal
$method eq 'HEAD' ? : 2 138 140
730  
731 140 my @method_matches;
732  
733 140 for my $route (@routes) {
734 187 ▶ if (my @captures = ($path =~ $route->{regex})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@captures) = $path =~ /$route->{"regex"}/) 126 61 187
735  
736 # Build params FIRST (needed for constraint checking)
737 126 my %params;
738 126 for my $i (0 .. $#{$route->{names}}) {
739 43 $params{$route->{names}[$i]} = $captures[$i];
740 }
741  
742 # Check constraints — skip route if any fail
743 126 ▶ next unless $self->_check_constraints($route, \%params);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_check_constraints($route, \%params) 11 115 126
744  
745 # Check method
746 115 my $route_method = $route->{method};
747 my $method_match = ref($route_method) eq 'ARRAY'
748 115 ▶ ? (grep { $_ eq $match_method || $_ eq $method } @$route_method)
Branch: 2 branches
BranchTrueFalseTotal
ref $route_method eq 'ARRAY' ? : 7 108 115
$_ eq $match_method or $_ eq $method 10 4 14
Condition83%5/6
$route_method eq "*" or $route_method eq $match_method or $route_method eq $method
l!l&&r!l&&!r
98010
$route_method eq "*" or $route_method eq $match_method
l!l&&r!l&&!r
118710
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 table75%3/4
A or B or C
A= $route_method eq "*"B= $route_method eq $match_methodC= $route_method eq $method
ABCresult
1XX1
01X1
0011
0000
749 : ($route_method eq '*' || $route_method eq $match_method || $route_method eq $method);
750  
751 115 ▶ if ($method_match) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method_match) 102 13 115
752 my $new_scope = {
753 %$scope,
754 path_params => \%params,
755 'pagi.router' => { route => $route->{path} },
756 102 };
757  
758 102 return await $route->{_handler}->($new_scope, $receive, $send);
759 }
760  
761 13 ▶ if (ref($route->{method}) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $route->{'method'} eq 'ARRAY') { } 3 10 13
elsif ($route->{'method'} ne '*') { } 10 0 10
762 3 push @method_matches, @{$route->{method}};
763 } elsif ($route->{method} ne '*') {
764 10 push @method_matches, $route->{method};
765 }
766 }
767 }
768  
769 # Path matched but method didn't - 405
770 38 ▶ if (@method_matches) {
Branch: 1 branch
BranchTrueFalseTotal
if (@method_matches) 8 30 38
771 8 my $allowed = join ', ', sort keys %{{ map { $_ => 1 } @method_matches }};
772 8 await $send->({
773 type => 'http.response.start',
774 status => 405,
775 headers => [
776 ['content-type', 'text/plain'],
777 ['allow', $allowed],
778 ],
779 });
780 8 await $send->({ type => 'http.response.body', body => 'Method Not Allowed', more => 0 });
781 8 return;
782 }
783  
784 # No HTTP route matched - try mounts as fallback
785 30 ▶ if (await $check_mounts->($scope, $receive, $send, $path)) {
Branch: 1 branch
BranchTrueFalseTotal
if (XXX) 15 15 30
786 15 return;
787 }
788  
789 # No mount matched either - 404
790 15 ▶ if ($not_found) {
Branch: 1 branch
BranchTrueFalseTotal
if ($not_found) { } 0 15 15
791 0 await $not_found->($scope, $receive, $send);
792 } else {
793 15 await $send->({
794 type => 'http.response.start',
795 status => 404,
796 headers => [['content-type', 'text/plain']],
797 });
798 15 await $send->({ type => 'http.response.body', body => 'Not Found', more => 0 });
799 }
800 104 };
801 }
802  
803 11 1;
804  
805 __END__
« lib/PAGI/Middleware/Static.pm ↑ Summary blib/lib/PAGI/Middleware/CORS.pm »