| 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"
MC/DC0%0/1
$opts{"label"} // "url"
$opts{"label"}
Truth table50%1/2
A
A= $opts{"label"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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"
MC/DC0%0/1
$opts{"label"} // "relay URL"
$opts{"label"}
Truth table0%0/2
A
A= $opts{"label"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
defined $base_url and not ref $base_url
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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"
MC/DC0%0/1
$opts{"label"} // "URI"
$opts{"label"}
Truth table0%0/2
A
A= $opts{"label"}
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | ▶ | my $scheme = $opts{scheme} // croak "scheme is required"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$opts{"scheme"} // croak("scheme is required")
MC/DC0%0/1
$opts{"scheme"} // croak("scheme is required")
$opts{"scheme"}
Truth table0%0/2
A
A= $opts{"scheme"}
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | ▶ | croak "$label is required" unless defined $input && !ref($input) && length $input; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
defined $input and not ref $input and length $input
defined $input and not ref $input
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
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | ▶ | croak "$label must not contain control or space characters" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | if $input =~ /[[:cntrl:]\s]/; | ||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | my $uri = URI->new($input); | |||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | ▶ | croak "$label must use $scheme:// protocol" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $uri->scheme and lc $uri->scheme eq lc $scheme
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
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | unless defined($uri->scheme) && lc($uri->scheme) eq lc($scheme); | ||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | ▶ | croak "$label must include an authority" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $uri->authority and length $uri->authority
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
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | unless defined($uri->authority) && length($uri->authority); | ||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | ▶ | croak "$label must not include userinfo" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | if $uri->authority =~ /@/; | ||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | ▶ | croak "$label must not include a path" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $uri->path and length $uri->path
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
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | if defined($uri->path) && length($uri->path); | ||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | ▶ | croak "$label must not include a fragment" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | if defined $uri->fragment; | ||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | ▶ | croak "$label must include query parameters" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $uri->query and length $uri->query
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%2/6
defined $url and not ref $url and length $url
defined $url and not ref $url
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
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | unless defined $url && !ref($url) && length $url; | ||||||||||||||||||||||||||||||||||||||||||
| 88 | 41 | ▶ | croak "$label must not contain control or space characters" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
defined $scheme and $opts{"schemes"}{lc $scheme}
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}
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | 38 | my $authority = $uri->authority; | |||||||||||||||||||||||||||||||||||||||||
| 97 | 38 | ▶ | croak "$label must include an authority/host" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $authority and length $authority
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
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | unless defined $authority && length $authority; | ||||||||||||||||||||||||||||||||||||||||||
| 99 | 37 | ▶ | croak "$label must not include userinfo" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $uri->userinfo or $authority =~ /@/
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 =~ /@/
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | if defined($uri->userinfo) || $authority =~ /@/; | ||||||||||||||||||||||||||||||||||||||||||
| 101 | 36 | ▶ | croak "$label must not include a query" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | if defined $uri->query; | ||||||||||||||||||||||||||||||||||||||||||
| 103 | 35 | ▶ | croak "$label must not include a fragment" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | if defined $uri->fragment; | ||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | 34 | my $host = $uri->host; | |||||||||||||||||||||||||||||||||||||||||
| 107 | 34 | ▶ | croak "$label must include an authority/host" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $host and length $host
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | $port = $1; | |||||||||||||||||||||||||||||||||||||||||
| 121 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 122 | 34 | ▶ | croak "$label must bracket IPv6 host" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 123 | if $host =~ /:/; | ||||||||||||||||||||||||||||||||||||||||||
| 124 | 34 | ▶ | if ($authority =~ /:(.*)\z/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | 26 | $port = $1; | |||||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | 34 | ▶ | return unless defined $port; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | 26 | ▶ | croak "$label port must be between 1 and 65535" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$port =~ /\A[0-9]+\z/ and $port >= 1 and $port <= 65535
$port =~ /\A[0-9]+\z/ and $port >= 1
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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; | |||||||||||||||||||||||||||||||||||||||||