Corvus/Uri.pm

filter: Statementstmt 75.8% 91 / 120 Branchbran 50.0% 35 / 70 Conditioncond 24.2% 8 / 33 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 100% 17 / 17 Podpod 0.0% 0 / 7 totaltotal 57.4% 151 / 263 CC 50 SCAR 25.1
CC
50
Cov
60%
_remove_dot_segments
27.5
_normalize_parts
17.4
decode_fragment
13.6
CRAP 12.3
« Corvus/Node.pm ↑ Summary Corvus/Regex/Unicode.pm »
1 package JSON::Schema::Corvus::Uri;
2  
3 # URI handling for schema identification and reference resolution (RFC 3986 section 5). A port of the Python
4 # package's uri.py, which mirrors Corvus.Text.Json.RuntimeEvaluator.Compilation.UriUtilities but resolves references
5 # directly, so that opaque bases (urn:, tag:) resolve the same way everywhere.
6  
7 4 ▶use 5.026;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use JSON::Schema::Corvus::Json ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 4 my $URI_RE = qr/\A(?:([A-Za-z][A-Za-z0-9+.-]*):)?(?:\/\/([^\/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?\z/s;
14  
15 38 ▶sub _parse {
Subroutine _parse: called CC=2 SCAR=7.1
16 38 my ($uri) = @_;
17 38 $uri =~ $URI_RE;
18 38 ▶ return { scheme => $1, authority => $2, path => $3 // '', query => $4, fragment => $5 };
Condition50%1/2
$3 // ""
l!l
380
MC/DC0%0/1
$3 // ""
$3
Truth table50%1/2
A
A= $3
Aresult
11
00
19 }
20  
21 27 ▶sub _format {
Subroutine _format: called CC=5 SCAR=16.8
22 27 my ($p) = @_;
23 27 my $s = '';
24 27 ▶ $s .= "$p->{scheme}:" if defined $p->{scheme};
Branch: 1 branch
BranchTrueFalseTotal
if defined $p->{"scheme"} 27 0 27
25 27 ▶ $s .= "//$p->{authority}" if defined $p->{authority};
Branch: 1 branch
BranchTrueFalseTotal
if defined $p->{"authority"} 27 0 27
26 27 $s .= $p->{path};
27 27 ▶ $s .= "?$p->{query}" if defined $p->{query};
Branch: 1 branch
BranchTrueFalseTotal
if defined $p->{"query"} 0 27 27
28 27 ▶ $s .= "#$p->{fragment}" if defined $p->{fragment};
Branch: 1 branch
BranchTrueFalseTotal
if defined $p->{"fragment"} 0 27 27
29 27 return $s;
30 }
31  
32 38 ▶sub _remove_dot_segments {
Subroutine _remove_dot_segments: called CC=10 SCAR=33.1
33 38 my ($path) = @_;
34 38 ▶ return $path if index($path, '.') < 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($path, ".") == -1 29 9 38
35 9 my @segments = split m{/}, $path, -1;
36 9 my @out;
37 9 my $last = $#segments;
38 9 for my $i (0 .. $last) {
39 18 my $seg = $segments[$i];
40 18 ▶ if ($seg eq '.') {
Branch: 1 branch
BranchTrueFalseTotal
if ($seg eq ".") 0 18 18
41 0 ▶ push @out, '' if $i == $last;
Branch: 1 branch
BranchTrueFalseTotal
if $i == $last 0 0 0
42 0 next;
43 }
44 18 ▶ if ($seg eq '..') {
Branch: 1 branch
BranchTrueFalseTotal
if ($seg eq "..") 0 18 18
45 0 ▶ pop @out if @out > 1 || (@out == 1 && $out[0] ne '');
Branch: 1 branch
BranchTrueFalseTotal
if @out > 1 or @out == 1 and $out[0] ne "" 0 0 0
Condition0%0/6
@out > 1 or @out == 1 and $out[0] ne ""
l!l&&r!l&&!r
000
@out == 1 and $out[0] ne ""
!ll&&!rl&&r
000
MC/DC0%0/3
@out > 1 or @out == 1 and $out[0] ne ""
@out > 1 @out == 1 $out[0] ne ""
Truth table0%0/4
A or B and C
A= @out > 1B= @out == 1C= $out[0] ne ""
ABCresult
1XX1
0111
00X0
0100
46 0 ▶ push @out, '' if $i == $last;
Branch: 1 branch
BranchTrueFalseTotal
if $i == $last 0 0 0
47 0 next;
48 }
49 18 push @out, $seg;
50 }
51 9 return join '/', @out;
52 }
53  
54 11 ▶sub _merge {
Subroutine _merge: called CC=4 SCAR=15.6
55 11 my ($base, $ref_path) = @_;
56 11 ▶ return "/$ref_path" if defined $base->{authority} && $base->{path} eq '';
Branch: 1 branch
BranchTrueFalseTotal
if defined $base->{"authority"} and $base->{"path"} eq "" 0 11 11
Condition33%1/3
defined $base->{"authority"} and $base->{"path"} eq ""
!ll&&!rl&&r
0110
MC/DC0%0/2
defined $base->{"authority"} and $base->{"path"} eq ""
defined $base->{"authority"} $base->{"path"} eq ""
Truth table33%1/3
A and B
A= defined $base->{"authority"}B= $base->{"path"} eq ""
ABresult
0X0
100
111
57 11 my $i = rindex($base->{path}, '/');
58 11 ▶ return $i >= 0 ? substr($base->{path}, 0, $i + 1) . $ref_path : $ref_path;
Branch: 1 branch
BranchTrueFalseTotal
$i >= 0 ? : 11 0 11
59 }
60  
61 # Whether a reference starts with a URI scheme.
62 3 ▶sub has_scheme { $_[0] =~ /\A[A-Za-z][A-Za-z0-9+.-]*:/ }
Subroutine has_scheme: called CC=1 SCAR=0.0
Pod undocumented
63  
64 # A reference split at its first '#': (the URI, the fragment).
65 62 ▶sub split_fragment {
Subroutine split_fragment: called CC=2 SCAR=6.9
Pod undocumented
66 62 my ($reference) = @_;
67 62 my $i = index($reference, '#');
68 62 ▶ return $i < 0 ? ($reference, '') : (substr($reference, 0, $i), substr($reference, $i + 1));
Branch: 1 branch
BranchTrueFalseTotal
$i < 0 ? : 24 38 62
69 }
70  
71 27 ▶sub _normalize_parts {
Subroutine _normalize_parts: called CC=10 SCAR=28.6
72 27 my ($p) = @_;
73 27 my %n = (%$p, fragment => undef);
74 27 ▶ $n{scheme} = lc $n{scheme} if defined $n{scheme};
Branch: 1 branch
BranchTrueFalseTotal
if defined $n{"scheme"} 27 0 27
75 27 ▶ if (defined $n{authority}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $n{"authority"}) 27 0 27
76 27 $n{authority} = lc $n{authority};
77 27 ▶ if ((($n{scheme} // '') eq 'http' && $n{authority} =~ /:80\z/)
Branch: 1 branch
BranchTrueFalseTotal
if (($n{"scheme"} // "") eq "http" and $n{"authority"} =~ /:80\z/u or ($n{"scheme"} // "") eq "https" and $n{"authority"} =~ /:443\z/u) 0 27 27
Condition38%5/13
($n{"scheme"} // "") eq "http" and $n{"authority"} =~ /:80\z/u or ($n{"scheme"} // "") eq "https" and $n{"authority"} =~ /:443\z/u
l!l&&r!l&&!r
0027
($n{"scheme"} // "") eq "http" and $n{"authority"} =~ /:80\z/u
!ll&&!rl&&r
2700
$n{"scheme"} // ""
l!l
270
($n{"scheme"} // "") eq "https" and $n{"authority"} =~ /:443\z/u
!ll&&!rl&&r
0270
$n{"scheme"} // ""
l!l
270
MC/DC0%0/4
($n{"scheme"} // "") eq "http" and $n{"authority"} =~ /:80\z/u or ($n{"scheme"} // "") eq "https" and $n{"authority"} =~ /:443\z/u
$n{"scheme"} $n{"authority"} =~ /:80\z/u $n{"scheme"} $n{"authority"} =~ /:443\z/u
Truth table0%0/7
A and B or C and D
A= $n{"scheme"}B= $n{"authority"} =~ /:80\z/uC= $n{"scheme"}D= $n{"authority"} =~ /:443\z/u
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
78 || (($n{scheme} // '') eq 'https' && $n{authority} =~ /:443\z/)) {
79 0 $n{authority} = substr($n{authority}, 0, rindex($n{authority}, ':'));
80 }
81 27 ▶ $n{path} = '/' if $n{path} eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $n{"path"} eq "" 0 27 27
82 }
83 27 $n{path} = _remove_dot_segments($n{path});
84 27 return _format(\%n);
85 }
86  
87 # An absolute URI (without its fragment), normalised so that equivalent spellings compare equal.
88 3 ▶sub normalize {
Subroutine normalize: called CC=2 SCAR=7.0
Pod undocumented
89 3 my ($uri) = @_;
90 3 my ($part) = split_fragment($uri);
91 3 ▶ return $part unless has_scheme($part);
Branch: 1 branch
BranchTrueFalseTotal
unless has_scheme($part) 0 3 3
92 3 return _normalize_parts(_parse($part));
93 }
94  
95 # A reference (without fragment) resolved against a base URI: the normalised absolute URI.
96 41 ▶sub resolve {
Subroutine resolve: called CC=9 SCAR=25.5
Pod undocumented
97 41 my ($base_uri, $reference) = @_;
98 41 ▶ return $base_uri if $reference eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $reference eq "" 17 24 41
99 24 my $r = _parse($reference);
100 24 ▶ return _normalize_parts($r) if defined $r->{scheme};
Branch: 1 branch
BranchTrueFalseTotal
if defined $r->{"scheme"} 13 11 24
101 11 ▶ return $reference if $base_uri eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $base_uri eq "" 0 11 11
102 11 my $b = _parse($base_uri);
103 11 my %t = (path => '');
104 11 ▶ if (defined $r->{authority}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $r->{'authority'}) { } 0 11 11
105 0 $t{authority} = $r->{authority};
106 0 $t{path} = _remove_dot_segments($r->{path});
107 0 $t{query} = $r->{query};
108 }
109 else {
110 11 ▶ if ($r->{path} eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{'path'} eq '') { } 0 11 11
111 0 $t{path} = $b->{path};
112 0 ▶ $t{query} = $r->{query} // $b->{query};
Condition0%0/3
$r->{"query"} // $b->{"query"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$r->{"query"} // $b->{"query"}
$r->{"query"} $b->{"query"}
Truth table0%0/3
A // B
A= $r->{"query"}B= $b->{"query"}
ABresult
1X1
011
000
113 }
114 else {
115 11 ▶ $t{path} = _remove_dot_segments(substr($r->{path}, 0, 1) eq '/' ? $r->{path} : _merge($b, $r->{path}));
Branch: 1 branch
BranchTrueFalseTotal
substr($r->{'path'}, 0, 1) eq '/' ? : 0 11 11
116 11 $t{query} = $r->{query};
117 }
118 11 $t{authority} = $b->{authority};
119 }
120 11 $t{scheme} = $b->{scheme};
121 11 ▶ return defined $t{scheme} ? _normalize_parts(\%t) : _format(\%t);
Branch: 1 branch
BranchTrueFalseTotal
defined $t{'scheme'} ? : 11 0 11
122 }
123  
124 # A fragment, percent-decoded (as UTF-8); unchanged when that is not valid.
125 48 ▶sub decode_fragment {
Subroutine decode_fragment: called CC=4 SCAR=26.1
Pod undocumented
126 48 my ($fragment) = @_;
127 48 ▶ return $fragment if index($fragment, '%') < 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($fragment, "%") == -1 48 0 48
128 0 (my $bytes = $fragment) =~ s/%([0-9A-Fa-f]{2})/chr hex $1/ge;
129 0 utf8::encode(my $raw = $fragment);
130 # Only the escapes are bytes: the rest of the fragment is characters, encoded as UTF-8 around them.
131 0 ▶ my $encoded = join '', map { /\A%([0-9A-Fa-f]{2})\z/ ? chr hex $1 : do { my $c = $_; utf8::encode($c); $c } }
Branch: 1 branch
BranchTrueFalseTotal
/\A%([0-9A-Fa-f]{2})\z/u ? : 0 0 0
132 split /(%[0-9A-Fa-f]{2})/, $fragment;
133 0 my $decoded = eval { Encode::decode('UTF-8', $encoded, Encode::FB_CROAK) };
134 0 ▶ return defined $decoded ? $decoded : $fragment;
Branch: 1 branch
BranchTrueFalseTotal
defined $decoded ? : 0 0 0
135 }
136  
137 # An RFC 6901 JSON pointer resolved against a JSON value: (found, value, path), the path's steps names and indexes.
138 191 ▶sub resolve_pointer {
Subroutine resolve_pointer: called CC=9 SCAR=25.6
Pod undocumented
139 191 my ($root, $pointer) = @_;
140 191 ▶ return (1, $root, []) if $pointer eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $pointer eq "" 176 15 191
141 15 ▶ return (0, undef, []) if substr($pointer, 0, 1) ne '/';
Branch: 1 branch
BranchTrueFalseTotal
if substr($pointer, 0, 1) ne "/" 0 15 15
142 15 my $current = $root;
143 15 my @path;
144 15 for my $raw (split m{/}, substr($pointer, 1), -1) {
145 29 (my $token = $raw) =~ s/~1/\//g;
146 29 $token =~ s/~0/~/g;
147 29 my $r = ref $current;
148 29 ▶ if ($r eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if ($r eq 'ARRAY') { } 0 29 29
elsif ($r eq 'HASH') { } 29 0 29
149 0 ▶ return (0, undef, \@path) unless $token =~ /\A(?:0|[1-9][0-9]*)\z/ && $token < @$current;
Branch: 1 branch
BranchTrueFalseTotal
unless $token =~ /\A(?:0|[1-9][0-9]*)\z/u and $token < @$current 0 0 0
Condition0%0/3
$token =~ /\A(?:0|[1-9][0-9]*)\z/u and $token < @$current
!ll&&!rl&&r
000
MC/DC0%0/2
$token =~ /\A(?:0|[1-9][0-9]*)\z/u and $token < @$current
$token =~ /\A(?:0|[1-9][0-9]*)\z/u $token < @$current
Truth table0%0/3
A and B
A= $token =~ /\A(?:0|[1-9][0-9]*)\z/uB= $token < @$current
ABresult
0X0
100
111
150 0 $current = $current->[$token];
151 0 push @path, 0 + $token;
152 }
153 elsif ($r eq 'HASH') {
154 29 ▶ return (0, undef, \@path) unless exists $current->{$token};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $current->{$token} 1 28 29
155 28 $current = $current->{$token};
156 28 push @path, $token;
157 }
158 else {
159 0 return (0, undef, \@path);
160 }
161 }
162 14 return (1, $current, \@path);
163 }
164  
165 458 ▶sub escape_pointer_token {
Subroutine escape_pointer_token: called CC=3 SCAR=16.0
Pod undocumented
166 458 my ($token) = @_;
167 458 ▶ return $token if index($token, '~') < 0 && index($token, '/') < 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($token, "~") == -1 and index($token, "/") == -1 458 0 458
Condition33%1/3
index($token, "~") == -1 and index($token, "/") == -1
!ll&&!rl&&r
00458
MC/DC0%0/2
index($token, "~") == -1 and index($token, "/") == -1
index($token, "~") == -1 index($token, "/") == -1
Truth table33%1/3
A and B
A= index($token, "~") == -1B= index($token, "/") == -1
ABresult
0X0
100
111
168 0 $token =~ s/~/~0/g;
169 0 $token =~ s/\//~1/g;
170 0 return $token;
171 }
172  
173 4 1;
« Corvus/Node.pm ↑ Summary Corvus/Regex/Unicode.pm »