Klonk/Routes.pm

filter: Statementstmt 19.3% 29 / 150 Branchbran 0.0% 0 / 76 Conditioncond 0.0% 0 / 22 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 36.3% 4 / 11 Podpod 75.0% 3 / 4 totaltotal 13.0% 36 / 277 CC 50 SCAR 46.7
CC
50
Cov
12%
_postprocess
210.0
dispatch
132.0
_parse_route
103.2
CRAP 106.2
↑ Summary Klonk/Env.pm »
1 package Klonk::Routes 0.01;
2 1 ▶use Klonk::pragma;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use HTML::Blitz ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Klonk::Env ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our %routes;
8  
9 0 ▶fun _parse_route($pattern) {
Subroutine _parse_route: not called CC=10 SCAR=46.4
Branch: 2 branches
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for fun _parse_route (expected 1, got ' . @_ . ')') 0 0 0
@_ > 1 and Function::Parameters::_croak('Too many arguments for fun _parse_route (expected 1, got ' . @_ . ')') 0 0 0
10 0 my @segments;
11 0 while () {
12 0 ▶ $pattern =~ m{\G ( / [^`*]*+ (?: `[`*] [^`*]*+ )*+ ) }xgc
Branch: 1 branch
BranchTrueFalseTotal
$pattern =~ m[\G ( / [^`*]*+ (?: `[`*] [^`*]*+ )*+ ) ]cgux or croak("Malformed route pattern (must start with '/'): '${pattern}'") 0 0 0
13 or croak "Malformed route pattern (must start with '/'): '$pattern'";
14 0 my $raw = $1;
15 0 $raw =~ s/`([`*])/$1/g;
16 0 push @segments, $raw;
17 0 ▶ if ($pattern =~ m{\G \z}x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pattern =~ /\G \z/ux) 0 0 0
18 0 last;
19 }
20 0 ▶ if ($pattern =~ m{\G `}x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pattern =~ /\G `/ux) 0 0 0
21 0 croak "Malformed route pattern ('`' must be followed by '`' or '*'): '$pattern'";
22 }
23 0 ▶ if ($pattern =~ m{\G (?<= / ) \*\* \z}xgc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pattern =~ m[\G (?<= / ) \*\* \z]cgux) 0 0 0
24 0 push @segments, undef;
25 0 last;
26 }
27 0 ▶ if ($pattern =~ m{\G (?<= / ) \* }xgc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pattern =~ m[\G (?<= / ) \* ]cgux) { } 0 0 0
28 0 ▶ if ($pattern =~ m{\G \z}x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pattern =~ /\G \z/ux) 0 0 0
29 0 push @segments, '';
30 0 last;
31 }
32 0 ▶ $pattern =~ m{\G / }x
Branch: 1 branch
BranchTrueFalseTotal
$pattern =~ m[\G / ]ux or croak("Malformed route pattern ('*' must be followed by '/'): '${pattern}'") 0 0 0
33 or croak "Malformed route pattern ('*' must be followed by '/'): '$pattern'";
34 } else {
35 0 croak "Malformed route pattern ('*' must be preceded by '/') :'$pattern'";
36 }
37 }
38 \@segments
39 0 }
40  
41 1 my $dispatch_info;
42  
43 0 ▶fun mkroute($pattern, %handlers) {
Subroutine mkroute: not called CC=8 SCAR=42.8
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for fun mkroute (expected 1, got ' . @_ . ')') 0 0 0
@_ > 1 and (@_ - 1) % 2 and Function::Parameters::_croak("Odd number of paired arguments for fun mkroute") 0 0 0
Condition0%0/3
@_ > 1 and (@_ - 1) % 2
!ll&&!rl&&r
000
MC/DC0%0/2
@_ > 1 and (@_ - 1) % 2
@_ > 1 (@_ - 1) % 2
Truth table0%0/3
A and B
A= @_ > 1B= (@_ - 1) % 2
ABresult
0X0
100
111
44 0 my (undef, $filename, $line) = caller;
45 0 my $location = [$filename, $line];
46  
47 0 $dispatch_info = undef;
48 0 ▶ my $def = $routes{$pattern} //= do {
Condition0%0/3
$Klonk::Routes::routes{$pattern} //= do { my $chunks = _parse_route($pattern); my $weight = join("", map(chr(defined $_ ? 1 + length($_) : 0), @$chunks)) . "\x{7fffffff}"; +{"chunks", $chunks, "weight", $weight, "resource", {}} }
l!l&&r!l&&!r
000
MC/DC0%0/2
$Klonk::Routes::routes{$pattern} //= do { my $chunks = _parse_route($pattern); my $weight = join("", map(chr(defined $_ ? 1 + length($_) : 0), @$chunks)) . "\x{7fffffff}"; +{"chunks", $chunks, "weight", $weight, "resource", {}} }
$Klonk::Routes::routes{$pattern} do { my $chunks = _parse_route($pattern); my $weight = join("", map(chr(defined $_ ? 1 + length($_) : 0), @$chunks)) . "\x{7fffffff}"; +{"chunks", $chunks, "weight", $weight, "resource", {}} }
Truth table0%0/3
A //= B
A= $Klonk::Routes::routes{$pattern}B= do { my $chunks = _parse_route($pattern); my $weight = join("", map(chr(defined $_ ? 1 + length($_) : 0), @$chunks)) . "\x{7fffffff}"; +{"chunks", $chunks, "weight", $weight, "resource", {}} }
ABresult
1X1
011
000
49 0 my $chunks = _parse_route $pattern;
50 0 ▶ my $weight = join('', map chr(defined $_ ? 1 + length : 0), @$chunks) . "\x{7fff_ffff}";
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
51 #say STDERR sprintf "weight(%s): %vd", $pattern, $weight;
52 +{
53 0 chunks => $chunks,
54 weight => $weight,
55 resource => {},
56 }
57 };
58  
59 0 for my $pmethod (sort keys %handlers) {
60 0 my $method = uc $pmethod;
61 0 my $resource = $def->{resource};
62 0 ▶ exists $resource->{$method}
Branch: 1 branch
BranchTrueFalseTotal
exists $resource->{$method} and croak(qq[Redefinition of $method $pattern (previously defined at $resource->{$method}{"location"}[0] line $resource->{$method}{"location"}[1])]) 0 0 0
63 and croak "Redefinition of $method $pattern (previously defined at $resource->{$method}{location}[0] line $resource->{$method}{location}[1])";
64 $resource->{$method} = {
65 location => $location,
66 0 handler => $handlers{$pmethod},
67 };
68 }
69 1 }
70  
71 1 my $redirect_template = do {
72 1 my $blitz = HTML::Blitz->new(
73 { dummy_marker_re => qr/\bXXX\b/ },
74 [ 'a[href=URL]' =>
75 [ 'set_attribute_var', href => 'url' ],
76 [ 'replace_inner_var', 'url' ],
77 ],
78 [ 'title, h1' =>
79 [ 'replace_inner_text', "308 Permanent Redirect" ]
80 ],
81 );
82  
83 1 my $template = $blitz->apply_to_html(
84 '(inline_redirect)',
85 '<!doctype html><title>XXX</title><h1>XXX</h1><p>See <a href=URL>XXX</a></p>'
86 );
87  
88 1 $template->compile_to_sub
89 };
90  
91 0 ▶fun _routes_prepare() {
Subroutine _routes_prepare: not called CC=6 SCAR=37.4
Branch: 1 branch
BranchTrueFalseTotal
@_ > 0 and Function::Parameters::_croak('Too many arguments for fun _routes_prepare (expected 0, got ' . @_ . ')') 0 0 0
92 0 my $gen = 'a';
93 0 my %mapping;
94 my $regex = join '|',
95 map {
96 0 my @chunks = @{$_->[2]};
97 0 ▶ my $rest = defined $chunks[-1] ? 0 : (pop @chunks, 1);
Branch: 1 branch
BranchTrueFalseTotal
defined $chunks[-1] ? : 0 0 0
98 0 my $re = join '([^/]*+)', map quotemeta, @chunks;
99 0 ▶ $re .= '(.*+)' if $rest;
Branch: 1 branch
BranchTrueFalseTotal
if $rest 0 0 0
100 0 $re .= "(*:$gen)";
101 0 $mapping{$gen++} = $_->[0];
102 0 $re
103 }
104 0 ▶ sort { $b->[1] cmp $a->[1] || $a->[0] cmp $b->[0] }
Condition0%0/3
$Klonk::Routes::b->[1] cmp $Klonk::Routes::a->[1] || $Klonk::Routes::a->[0] cmp $Klonk::Routes::b->[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
$Klonk::Routes::b->[1] cmp $Klonk::Routes::a->[1] || $Klonk::Routes::a->[0] cmp $Klonk::Routes::b->[0]
$Klonk::Routes::b->[1] cmp $Klonk::Routes::a->[1] $Klonk::Routes::a->[0] cmp $Klonk::Routes::b->[0]
Truth table0%0/3
A || B
A= $Klonk::Routes::b->[1] cmp $Klonk::Routes::a->[1]B= $Klonk::Routes::a->[0] cmp $Klonk::Routes::b->[0]
ABresult
1X1
011
000
105 0 map [$_, @{$routes{$_}}{'weight', 'chunks'}],
106 keys %routes;
107 0 ▶ $regex = '(?!)' if $regex eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $regex eq "" 0 0 0
108 #say STDERR ">>> $regex";
109 #use re 'debugcolor';
110 [
111 0 qr/\A(?|$regex)\z/s,
112 \%mapping,
113 ]
114 1 }
115  
116 1 my %text_types = (
117 'css' => 'text/css',
118 'csv' => 'text/csv',
119 'html' => 'text/html',
120 'js' => 'text/javascript',
121 'json' => 'application/json',
122 'jsonld' => 'application/ld+json',
123 'text' => 'text/plain',
124 );
125  
126 1 my %bin_types = (
127 'bin' => 'application/octet-stream',
128 'jpeg' => 'image/jpeg',
129 'png' => 'image/png',
130 'webp' => 'image/webp',
131 );
132  
133 0 ▶fun _postprocess($ret) {
Subroutine _postprocess: not called CC=14 SCAR=53.5
Branch: 2 branches
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for fun _postprocess (expected 1, got ' . @_ . ')') 0 0 0
@_ > 1 and Function::Parameters::_croak('Too many arguments for fun _postprocess (expected 1, got ' . @_ . ')') 0 0 0
134 0 my $status = 200;
135 0 ▶ if ($ret->[0] =~ /\A\d{3}\z/a) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ret->[0] =~ /\A\d{3}\z/a) 0 0 0
136 0 $status = shift @$ret;
137 }
138 0 my ($itype, $body, $headers) = @$ret;
139  
140 0 for my $spec (
141 [\%text_types, 1],
142 [\%bin_types, 0],
143 ) {
144 0 my ($type_map, $encode_p) = @$spec;
145 0 ▶ if (my $type = $type_map->{$itype}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $type = $type_map->{$itype}) 0 0 0
146 0 ▶ if ($encode_p) {
Branch: 1 branch
BranchTrueFalseTotal
if ($encode_p) 0 0 0
147 0 ▶ utf8::encode $body unless ref $body;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $body 0 0 0
148 0 $type .= '; charset=utf-8';
149 }
150 0 ▶ my $length = ref $body
Branch: 1 branch
BranchTrueFalseTotal
ref $body ? : 0 0 0
Condition0%0/2
-s $body or undef
l!l
00
MC/DC0%0/1
-s $body or undef
-s $body
Truth table0%0/2
A
A= -s $body
Aresult
11
00
151 ? -s $body || undef
152 : length $body;
153 return [
154 $status,
155 [
156 'content-type' => $type,
157 defined $length
158 ? ('content-length' => $length)
159 : (),
160 map {
161 0 my $k = $_;
162 0 my $v = $headers->{$k};
163 0 ▶ map +($k => $_), ref($v) eq 'ARRAY' ? @$v : $v
Branch: 1 branch
BranchTrueFalseTotal
ref $v eq 'ARRAY' ? : 0 0 0
164 }
165 0 ▶ keys %{$headers // {}}
Branch: 2 branches
BranchTrueFalseTotal
defined $length ? : 0 0 0
ref $body ? : 0 0 0
Condition0%0/2
$headers // {}
l!l
00
MC/DC0%0/1
$headers // {}
$headers
Truth table0%0/2
A
A= $headers
Aresult
11
00
166 ],
167 ref $body ? $body : [ $body ]
168 ];
169 }
170 }
171
172 0 die "Unknown content type: $itype";
173 1 }
174  
175 1 my $booted;
176 my @init;
177  
178 0 ▶fun on_init($fun) {
Subroutine on_init: not called CC=4 SCAR=30.0
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for fun on_init (expected 1, got ' . @_ . ')') 0 0 0
@_ > 1 and Function::Parameters::_croak('Too many arguments for fun on_init (expected 1, got ' . @_ . ')') 0 0 0
179 0 ▶ croak "Can't call on_init() after boot()" if $booted;
Branch: 1 branch
BranchTrueFalseTotal
if $booted 0 0 0
180 0 push @init, $fun;
181 1 }
182  
183 0 ▶fun dispatch($env) {
Subroutine dispatch: not called CC=11 SCAR=48.8
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for fun dispatch (expected 1, got ' . @_ . ')') 0 0 0
@_ > 1 and Function::Parameters::_croak('Too many arguments for fun dispatch (expected 1, got ' . @_ . ')') 0 0 0
184 0 my $kenv = Klonk::Env->new($env);
185 0 my $req_path = $env->{PATH_INFO};
186 0 my $req_method = $env->{REQUEST_METHOD};
187  
188 0 ▶ $dispatch_info //= _routes_prepare;
Condition0%0/3
$dispatch_info //= _routes_prepare()
l!l&&r!l&&!r
000
MC/DC0%0/2
$dispatch_info //= _routes_prepare()
$dispatch_info _routes_prepare()
Truth table0%0/3
A //= B
A= $dispatch_infoB= _routes_prepare()
ABresult
1X1
011
000
189 0 local our $REGMARK;
190 0 ▶ if (my @captures = $req_path =~ /$dispatch_info->[0]/) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@captures) = $req_path =~ /$dispatch_info->[0]/u) 0 0 0
191 0 my $pattern = $dispatch_info->[1]{$REGMARK};
192 0 my $meta = $routes{$pattern};
193 0 splice @captures, $#{$meta->{chunks}};
194 0 my $resource = $meta->{resource};
195 0 ▶ if (my $info = $resource->{$req_method}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $info = $resource->{$req_method}) 0 0 0
196 0 my $handler = $info->{handler};
197 0 return _postprocess $handler->($kenv, @captures);
198 }
199  
200 0 ▶ if ($req_method eq 'HEAD' && (my $info = $resource->{GET})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req_method eq "HEAD" and my $info = $resource->{"GET"}) 0 0 0
Condition0%0/3
$req_method eq "HEAD" and my $info = $resource->{"GET"}
!ll&&!rl&&r
000
MC/DC0%0/2
$req_method eq "HEAD" and my $info = $resource->{"GET"}
$req_method eq "HEAD" my $info = $resource->{"GET"}
Truth table0%0/3
A and B
A= $req_method eq "HEAD"B= my $info = $resource->{"GET"}
ABresult
0X0
100
111
201 0 my $ret = _postprocess $info->{handler}($kenv, @captures);
202 0 return [ 204, $ret->[1], [] ];
203 }
204  
205 0 my $allowed_methods = join ', ', sort keys %$resource;
206 0 ▶ if ($req_method eq 'OPTIONS') {
Branch: 1 branch
BranchTrueFalseTotal
if ($req_method eq "OPTIONS") 0 0 0
207 0 return [ 204, [ 'allow' => $allowed_methods ], [] ];
208 }
209  
210 0 return _postprocess [
211 405,
212 'html',
213 "<!doctype html><title>405 Method Not Allowed</title><h1>405 Method Not Allowed</h1>",
214 { allow => $allowed_methods }
215 ];
216 }
217  
218 0 ▶ if ($req_path !~ m{/\z} && "$req_path/" =~ /$dispatch_info->[0]/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $req_path =~ m[/\z]u and ("$req_path/") =~ /$dispatch_info->[0]/u) 0 0 0
Condition0%0/3
not $req_path =~ m[/\z]u and ("$req_path/") =~ /$dispatch_info->[0]/u
!ll&&!rl&&r
000
MC/DC0%0/2
not $req_path =~ m[/\z]u and ("$req_path/") =~ /$dispatch_info->[0]/u
not $req_path =~ m[/\z]u ("$req_path/") =~ /$dispatch_info->[0]/u
Truth table0%0/3
not(A) and B
A= not $req_path =~ m[/\z]uB= ("$req_path/") =~ /$dispatch_info->[0]/u
ABresult
0X0
100
111
219 0 my $uri = $env->{REQUEST_URI};
220 0 $uri =~ s{(?=[?#])|\z}{/};
221 0 return _postprocess [
222 308,
223 'html',
224 $redirect_template->({ url => $uri }),
225 {
226 'location' => $uri,
227 },
228 ];
229 }
230  
231 0 return _postprocess [
232 404,
233 'html',
234 "<!doctype html><title>404 Not Found</title><h1>404 Not Found</h1>",
235 ];
236 1 }
237  
238 0 ▶fun boot() {
Subroutine boot: not called CC=3 SCAR=24.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ > 0 and Function::Parameters::_croak('Too many arguments for fun boot (expected 0, got ' . @_ . ')') 0 0 0
239 0 $booted = 1;
240 0 my @fun = splice @init;
241 0 for my $fun (@fun) {
242 0 $fun->();
243 }
244  
245 0 \&dispatch
246 1 }
247  
248 1 1
249 __END__
↑ Summary Klonk/Env.pm »