_URI.pm

filter: Statementstmt 68.9% 51 / 74 Branchbran 47.9% 23 / 48 Conditioncond 28.3% 15 / 53 MC/DCmcdc 16.1% 5 / 31 Subroutinesub 75.0% 9 / 12 Podpod 0.0% 0 / 5 totaltotal 46.1% 103 / 223 CC 44 SCAR 46.3
CC
44
Cov
51%
parse_uri_query
306.0
append_query_params
20.0
_validate_hierarchical_url
17.4
CRAP 102.4
« Git.pm ↑ Summary Comment.pm »
1 package Net::Nostr::_URI;
2  
3 12 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 12 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 ▶use URI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 12 our @EXPORT_OK = qw(
10 append_query_params
11 build_nip05_url
12 parse_uri_query
13 validate_http_base_url
14 validate_relay_url
15 );
16  
17 41 ▶sub validate_http_base_url {
Subroutine validate_http_base_url: called CC=2 SCAR=7.2
Pod undocumented
18 41 my ($url, %opts) = @_;
19 return _validate_hierarchical_url(
20 $url,
21 41 ▶ label => $opts{label} // 'url',
Condition50%1/2
$opts{"label"} // "url"
l!l
410
MC/DC0%0/1
$opts{"label"} // "url"
$opts{"label"}
Truth table50%1/2
A
A= $opts{"label"}
Aresult
11
00
22 schemes => { http => 1, https => 1 },
23 );
24 }
25  
26 0 ▶sub validate_relay_url {
Subroutine validate_relay_url: not called CC=2 SCAR=17.9
Pod undocumented
27 0 my ($url, %opts) = @_;
28 return _validate_hierarchical_url(
29 $url,
30 0 ▶ label => $opts{label} // 'relay URL',
Condition0%0/2
$opts{"label"} // "relay URL"
l!l
00
MC/DC0%0/1
$opts{"label"} // "relay URL"
$opts{"label"}
Truth table0%0/2
A
A= $opts{"label"}
Aresult
11
00
31 schemes => { ws => 1, wss => 1 },
32 );
33 }
34  
35 18 ▶sub build_nip05_url {
Subroutine build_nip05_url: called CC=2 SCAR=7.0
Pod undocumented
36 18 my ($base_url, $local) = @_;
37 18 my $uri = URI->new(validate_http_base_url($base_url, label => 'base_url'));
38 18 my $path = $uri->path;
39 18 ▶ $path = '' unless defined $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 0 18 18
40 18 $path =~ s{/+\z}{};
41 18 $uri->path("$path/.well-known/nostr.json");
42 18 $uri->query_form(name => $local);
43 18 return $uri->as_string;
44 }
45  
46 0 ▶sub append_query_params {
Subroutine append_query_params: not called CC=4 SCAR=30.0
Pod undocumented
47 0 my ($base_url, @params) = @_;
48 0 ▶ croak "url is required" unless defined $base_url && !ref($base_url);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $base_url and not ref $base_url 0 0 0
Condition0%0/3
defined $base_url and not ref $base_url
!ll&&!rl&&r
000
MC/DC0%0/2
defined $base_url and not ref $base_url
defined $base_url not ref $base_url
Truth table0%0/3
A and not(B)
A= defined $base_urlB= not ref $base_url
ABresult
0X0
100
111
49 0 my $uri = URI->new($base_url);
50 0 my @query = $uri->query_form;
51 0 push @query, @params;
52 0 ▶ $uri->query_form(@query) if @query;
Branch: 1 branch
BranchTrueFalseTotal
if @query 0 0 0
53 0 return $uri->as_string;
54 }
55  
56 0 ▶sub parse_uri_query {
Subroutine parse_uri_query: not called CC=17 SCAR=57.2
Pod undocumented
57 0 my ($input, %opts) = @_;
58 0 ▶ my $label = $opts{label} // 'URI';
Condition0%0/2
$opts{"label"} // "URI"
l!l
00
MC/DC0%0/1
$opts{"label"} // "URI"
$opts{"label"}
Truth table0%0/2
A
A= $opts{"label"}
Aresult
11
00
59 0 ▶ my $scheme = $opts{scheme} // croak "scheme is required";
Condition0%0/2
$opts{"scheme"} // croak("scheme is required")
l!l
00
MC/DC0%0/1
$opts{"scheme"} // croak("scheme is required")
$opts{"scheme"}
Truth table0%0/2
A
A= $opts{"scheme"}
Aresult
11
00
60  
61 0 ▶ croak "$label is required" unless defined $input && !ref($input) && length $input;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $input and not ref $input and length $input 0 0 0
Condition0%0/6
defined $input and not ref $input and length $input
!ll&&!rl&&r
000
defined $input and not ref $input
!ll&&!rl&&r
000
MC/DC0%0/3
defined $input and not ref $input and length $input
defined $input not ref $input length $input
Truth table0%0/4
A and not(B) and C
A= defined $inputB= not ref $inputC= length $input
ABCresult
0XX0
10X0
1100
1111
62 0 ▶ croak "$label must not contain control or space characters"
Branch: 1 branch
BranchTrueFalseTotal
if $input =~ /[[:cntrl:]\s]/ 0 0 0
63 if $input =~ /[[:cntrl:]\s]/;
64  
65 0 my $uri = URI->new($input);
66 0 ▶ croak "$label must use $scheme:// protocol"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $uri->scheme and lc $uri->scheme eq lc $scheme 0 0 0
Condition0%0/3
defined $uri->scheme and lc $uri->scheme eq lc $scheme
!ll&&!rl&&r
000
MC/DC0%0/2
defined $uri->scheme and lc $uri->scheme eq lc $scheme
defined $uri->scheme lc $uri->scheme eq lc $scheme
Truth table0%0/3
A and B
A= defined $uri->schemeB= lc $uri->scheme eq lc $scheme
ABresult
0X0
100
111
67 unless defined($uri->scheme) && lc($uri->scheme) eq lc($scheme);
68 0 ▶ croak "$label must include an authority"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $uri->authority and length $uri->authority 0 0 0
Condition0%0/3
defined $uri->authority and length $uri->authority
!ll&&!rl&&r
000
MC/DC0%0/2
defined $uri->authority and length $uri->authority
defined $uri->authority length $uri->authority
Truth table0%0/3
A and B
A= defined $uri->authorityB= length $uri->authority
ABresult
0X0
100
111
69 unless defined($uri->authority) && length($uri->authority);
70 0 ▶ croak "$label must not include userinfo"
Branch: 1 branch
BranchTrueFalseTotal
if $uri->authority =~ /@/ 0 0 0
71 if $uri->authority =~ /@/;
72 0 ▶ croak "$label must not include a path"
Branch: 1 branch
BranchTrueFalseTotal
if defined $uri->path and length $uri->path 0 0 0
Condition0%0/3
defined $uri->path and length $uri->path
!ll&&!rl&&r
000
MC/DC0%0/2
defined $uri->path and length $uri->path
defined $uri->path length $uri->path
Truth table0%0/3
A and B
A= defined $uri->pathB= length $uri->path
ABresult
0X0
100
111
73 if defined($uri->path) && length($uri->path);
74 0 ▶ croak "$label must not include a fragment"
Branch: 1 branch
BranchTrueFalseTotal
if defined $uri->fragment 0 0 0
75 if defined $uri->fragment;
76 0 ▶ croak "$label must include query parameters"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $uri->query and length $uri->query 0 0 0
Condition0%0/3
defined $uri->query and length $uri->query
!ll&&!rl&&r
000
MC/DC0%0/2
defined $uri->query and length $uri->query
defined $uri->query length $uri->query
Truth table0%0/3
A and B
A= defined $uri->queryB= length $uri->query
ABresult
0X0
100
111
77 unless defined($uri->query) && length($uri->query);
78  
79 0 return ($uri->authority, $uri->query_form);
80 }
81  
82 41 ▶sub _validate_hierarchical_url {
Subroutine _validate_hierarchical_url: called CC=15 SCAR=28.6
83 41 my ($url, %opts) = @_;
84 41 my $label = $opts{label};
85  
86 41 ▶ croak "$label is required"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $url and not ref $url and length $url 0 41 41
Condition33%2/6
defined $url and not ref $url and length $url
!ll&&!rl&&r
0041
defined $url and not ref $url
!ll&&!rl&&r
0041
MC/DC0%0/3
defined $url and not ref $url and length $url
defined $url not ref $url length $url
Truth table25%1/4
A and not(B) and C
A= defined $urlB= not ref $urlC= length $url
ABCresult
0XX0
10X0
1100
1111
87 unless defined $url && !ref($url) && length $url;
88 41 ▶ croak "$label must not contain control or space characters"
Branch: 1 branch
BranchTrueFalseTotal
if $url =~ /[[:cntrl:]\s]/ 2 39 41
89 if $url =~ /[[:cntrl:]\s]/;
90  
91 39 my $uri = URI->new($url);
92 39 my $scheme = $uri->scheme;
93 croak "$label must use " . _scheme_list($opts{schemes})
94 39 ▶ unless defined $scheme && $opts{schemes}{lc $scheme};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $scheme and $opts{"schemes"}{lc $scheme} 1 38 39
Condition66%2/3
defined $scheme and $opts{"schemes"}{lc $scheme}
!ll&&!rl&&r
0138
MC/DC50%1/2
defined $scheme and $opts{"schemes"}{lc $scheme}
defined $scheme $opts{"schemes"}{lc $scheme}
Truth table66%2/3
A and B
A= defined $schemeB= $opts{"schemes"}{lc $scheme}
ABresult
0X0
100
111
95  
96 38 my $authority = $uri->authority;
97 38 ▶ croak "$label must include an authority/host"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $authority and length $authority 1 37 38
Condition66%2/3
defined $authority and length $authority
!ll&&!rl&&r
0137
MC/DC50%1/2
defined $authority and length $authority
defined $authority length $authority
Truth table66%2/3
A and B
A= defined $authorityB= length $authority
ABresult
0X0
100
111
98 unless defined $authority && length $authority;
99 37 ▶ croak "$label must not include userinfo"
Branch: 1 branch
BranchTrueFalseTotal
if defined $uri->userinfo or $authority =~ /@/ 1 36 37
Condition66%2/3
defined $uri->userinfo or $authority =~ /@/
l!l&&r!l&&!r
1036
MC/DC50%1/2
defined $uri->userinfo or $authority =~ /@/
defined $uri->userinfo $authority =~ /@/
Truth table66%2/3
A or B
A= defined $uri->userinfoB= $authority =~ /@/
ABresult
1X1
011
000
100 if defined($uri->userinfo) || $authority =~ /@/;
101 36 ▶ croak "$label must not include a query"
Branch: 1 branch
BranchTrueFalseTotal
if defined $uri->query 1 35 36
102 if defined $uri->query;
103 35 ▶ croak "$label must not include a fragment"
Branch: 1 branch
BranchTrueFalseTotal
if defined $uri->fragment 1 34 35
104 if defined $uri->fragment;
105  
106 34 my $host = $uri->host;
107 34 ▶ croak "$label must include an authority/host"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $host and length $host 0 34 34
Condition33%1/3
defined $host and length $host
!ll&&!rl&&r
0034
MC/DC0%0/2
defined $host and length $host
defined $host length $host
Truth table33%1/3
A and B
A= defined $hostB= length $host
ABresult
0X0
100
111
108 unless defined $host && length $host;
109  
110 34 _validate_authority_port($label, $authority, $host);
111  
112 32 return $url;
113 }
114  
115 34 ▶sub _validate_authority_port {
Subroutine _validate_authority_port: called CC=8 SCAR=21.1
116 34 my ($label, $authority, $host) = @_;
117  
118 34 my $port;
119 34 ▶ if ($authority =~ /\A\[[^\]]+\](?::([^:]*))?\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($authority =~ /\A\[[^\]]+\](?::([^:]*))?\z/) { } 0 34 34
120 0 $port = $1;
121 } else {
122 34 ▶ croak "$label must bracket IPv6 host"
Branch: 1 branch
BranchTrueFalseTotal
if $host =~ /:/ 0 34 34
123 if $host =~ /:/;
124 34 ▶ if ($authority =~ /:(.*)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($authority =~ /:(.*)\z/) 26 8 34
125 26 $port = $1;
126 }
127 }
128  
129 34 ▶ return unless defined $port;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $port 8 26 34
130  
131 26 ▶ croak "$label port must be between 1 and 65535"
Branch: 1 branch
BranchTrueFalseTotal
unless $port =~ /\A[0-9]+\z/ and $port >= 1 and $port <= 65535 2 24 26
Condition83%5/6
$port =~ /\A[0-9]+\z/ and $port >= 1 and $port <= 65535
!ll&&!rl&&r
1124
$port =~ /\A[0-9]+\z/ and $port >= 1
!ll&&!rl&&r
0125
MC/DC66%2/3
$port =~ /\A[0-9]+\z/ and $port >= 1 and $port <= 65535
$port =~ /\A[0-9]+\z/ $port >= 1 $port <= 65535
Truth table75%3/4
A and B and C
A= $port =~ /\A[0-9]+\z/B= $port >= 1C= $port <= 65535
ABCresult
0XX0
10X0
1100
1111
132 unless $port =~ /\A[0-9]+\z/ && $port >= 1 && $port <= 65535;
133 }
134  
135 1 ▶sub _scheme_list {
Subroutine _scheme_list: called CC=1 SCAR=0.0
136 1 my ($schemes) = @_;
137 1 my @schemes = sort keys %$schemes;
138 1 return join ' or ', @schemes;
139 }
140  
141 12 1;
« Git.pm ↑ Summary Comment.pm »