lib/PAGI/App/URLMap.pm

filter: Statementstmt 92.0% 46 / 50 Branchbran 70.0% 7 / 10 Conditioncond 60.0% 6 / 10 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 88.8% 8 / 9 Podpod 50.0% 2 / 4 totaltotal 80.6% 71 / 88 CC 11 SCAR 17.2
CC
11
Cov
84%
__ANON__
9.3
map
6.0
new
2.0
CRAP 5.6
« lib/PAGI/App/Healthcheck.pm ↑ Summary blib/lib/PAGI/Middleware/ContentNegotiation.pm »
1 package PAGI::App::URLMap;
2 1 $PAGI::App::URLMap::VERSION = '0.002002';
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use PAGI::Utils ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 PAGI::App::URLMap - Mount apps at URL path prefixes
11  
12 =head1 SYNOPSIS
13  
14 use PAGI::App::URLMap;
15  
16 my $map = PAGI::App::URLMap->new;
17 $map->mount('/api' => $api_app);
18 $map->mount('/static' => PAGI::App::File->new(root => $dir));
19 my $app = $map->to_app;
20  
21 =cut
22  
23 6 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
24 6 my ($class, %args) = @_;
25  
26 return bless {
27 mounts => [],
28 6 ▶ default => defined $args{default} ? PAGI::Utils::to_app($args{default}) : undef,
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'default'} ? : 1 5 6
29 }, $class;
30 }
31  
32 9 ▶sub mount {
Subroutine mount: called CC=1 SCAR=0.0
Pod documented
33 9 my ($self, $path, $app) = @_;
34  
35 9 $path =~ s{/+$}{}; # Remove trailing slashes
36 9 push @{$self->{mounts}}, [$path, PAGI::Utils::to_app($app)];
37 # Keep sorted by length (longest first) for proper matching
38 9 @{$self->{mounts}} = sort { length($b->[0]) <=> length($a->[0]) } @{$self->{mounts}};
39 9 return $self;
40 }
41  
42 0 ▶sub map {
Subroutine map: not called CC=2 SCAR=17.9
Pod documented
43 0 my ($self, $mapping) = @_;
44  
45 0 ▶ while (my ($path, $app) = each %$mapping) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$mapping) 0 0 0
46 0 $self->mount($path, $app);
47 }
48 0 return $self;
49 }
50  
51 6 ▶sub to_app {
Subroutine to_app: called CC=1 SCAR=0.0
Pod undocumented
52 6 my ($self) = @_;
53  
54 6 my @mounts = @{$self->{mounts}};
55 6 my $default = $self->{default};
56  
57 9 ▶ return async sub {
Subroutine __ANON__: called CC=9 SCAR=22.3
58 9 my ($scope, $receive, $send) = @_;
59 9 ▶ my $path = $scope->{path} // '/';
Condition50%1/2
$scope->{"path"} // "/"
l!l
90
MC/DC0%0/1
$scope->{"path"} // "/"
$scope->{"path"}
Truth table50%1/2
A
A= $scope->{"path"}
Aresult
11
00
60  
61 9 for my $mount (@mounts) {
62 11 my ($prefix, $app) = @$mount;
63  
64 11 ▶ if ($prefix eq '' || $path eq $prefix || $path =~ /^\Q$prefix\E\//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prefix eq "" or $path eq $prefix or $path =~ m[^\Q$prefix\E/]) 7 4 11
Condition50%3/6
$prefix eq "" or $path eq $prefix or $path =~ m[^\Q$prefix\E/]
l!l&&r!l&&!r
074
$prefix eq "" or $path eq $prefix
l!l&&r!l&&!r
0011
MC/DC33%1/3
$prefix eq "" or $path eq $prefix or $path =~ m[^\Q$prefix\E/]
$prefix eq "" $path eq $prefix $path =~ m[^\Q$prefix\E/]
Truth table50%2/4
A or B or C
A= $prefix eq ""B= $path eq $prefixC= $path =~ m[^\Q$prefix\E/]
ABCresult
1XX1
01X1
0011
0000
65 # Match found - adjust path for mounted app
66 7 my $new_path = $path;
67 7 $new_path =~ s/^\Q$prefix\E//;
68 7 ▶ $new_path = '/' if $new_path eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $new_path eq "" 0 7 7
69  
70 my $new_scope = {
71 %$scope,
72 path => $new_path,
73 7 ▶ root_path => ($scope->{root_path} // '') . $prefix,
Condition100%2/2
$scope->{"root_path"} // ""
l!l
16
MC/DC100%1/1
$scope->{"root_path"} // ""
$scope->{"root_path"}
Truth table100%2/2
A
A= $scope->{"root_path"}
Aresult
11
00
74 };
75  
76 7 await $app->($new_scope, $receive, $send);
77 7 return;
78 }
79 }
80  
81 # No match - use default or 404
82 2 ▶ if ($default) {
Branch: 1 branch
BranchTrueFalseTotal
if ($default) { } 1 1 2
83 1 await $default->($scope, $receive, $send);
84 } else {
85 1 await $send->({
86 type => 'http.response.start',
87 status => 404,
88 headers => [['content-type', 'text/plain']],
89 });
90 1 await $send->({ type => 'http.response.body', body => 'Not Found', more => 0 });
91 }
92 6 };
93 }
94  
95 1 1;
96  
97 __END__
« lib/PAGI/App/Healthcheck.pm ↑ Summary blib/lib/PAGI/Middleware/ContentNegotiation.pm »