| 1 | 5 | package At::Protocol::URI v1.8.0 { # https://github.com/bluesky-social/atproto/blob/main/packages/syntax/src/aturi.ts | |||||||||||||||||||||||||||||||||||||||||
| 2 | 5 | ▶ | use v5.42; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 5 | ▶ | no warnings qw[experimental::builtin experimental::try]; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use At::Error qw[register throw]; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use At::Protocol::DID qw[ensureValidDid ensureValidDidRegex]; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use At::Protocol::Handle qw[ensureValidHandle ensureValidHandleRegex]; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use At::Protocol::NSID qw[ensureValidNsid ensureValidNsidRegex]; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use feature 'try'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use parent -norequire => 'Exporter'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | use overload | ||||||||||||||||||||||||||||||||||||||||||
| 11 | 5 | ▶ | '""' => sub ( $s, $u, $q ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 14 | $s->as_string; | |||||||||||||||||||||||||||||||||||||||||
| 13 | 5 | }; | |||||||||||||||||||||||||||||||||||||||||
| 14 | 5 | our %EXPORT_TAGS = ( all => [ our @EXPORT_OK = qw[ensureValidAtUri ensureValidAtUriRegex] ] ); | |||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 149 | ▶ | sub ATP_URI_REGEX () { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ATP_URI_REGEX: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | # proto- --did-------------- --name---------------- --path---- --query-- --hash-- | ||||||||||||||||||||||||||||||||||||||||||
| 19 | 149 | qr/^(at:\/\/)?((?:did:[a-z0-9:%-]+)|(?:[a-z0-9][a-z0-9.:-]*))(\/[^?#\s]*)?(\?[^#\s]+)?(#[^\s]+)?$/i; | |||||||||||||||||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | 90 | ▶ | sub RELATIVE_REGEX () { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
RELATIVE_REGEX: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | # --path----- --query-- --hash-- | ||||||||||||||||||||||||||||||||||||||||||
| 25 | 90 | qr/^(\/[^?#\s]*)?(\?[^#\s]+)?(#[^\s]+)?$/i; | |||||||||||||||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 149 | ▶ | sub new( $class, $uri, $base //= () ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=6 SCAR=19.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 149 | my $parsed; | |||||||||||||||||||||||||||||||||||||||||
| 30 | 149 | ▶ | if ( defined $base ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | 90 | $parsed = _parse($base); | |||||||||||||||||||||||||||||||||||||||||
| 32 | 90 | ▶ | $parsed // throw InvalidAtUriError( 'Invalid AT URI: ' . $base ); | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$parsed // throw(InvalidAtUriError('Invalid AT URI: ' . $base))
MC/DC0%0/2
$parsed // throw(InvalidAtUriError('Invalid AT URI: ' . $base))
$parsed
throw(InvalidAtUriError('Invalid AT URI: ' . $base))
Truth table33%1/3
A // B
A= $parsedB= throw(InvalidAtUriError('Invalid AT URI: ' . $base))
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | 90 | my $relativep = _parseRelative($uri); | |||||||||||||||||||||||||||||||||||||||||
| 34 | 90 | ▶ | $relativep // throw InvalidAtUriError( 'Invalid path: ' . $uri ); | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$relativep // throw(InvalidAtUriError('Invalid path: ' . $uri))
MC/DC0%0/2
$relativep // throw(InvalidAtUriError('Invalid path: ' . $uri))
$relativep
throw(InvalidAtUriError('Invalid path: ' . $uri))
Truth table33%1/3
A // B
A= $relativepB= throw(InvalidAtUriError('Invalid path: ' . $uri))
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | 90 | %$parsed = ( %$parsed, %$relativep ); | |||||||||||||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||||||||||||
| 37 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 38 | 59 | $parsed = _parse($uri); | |||||||||||||||||||||||||||||||||||||||||
| 39 | 59 | ▶ | $parsed // throw InvalidAtUriError( 'Invalid AT URI: ' . $uri ); | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$parsed // throw(InvalidAtUriError('Invalid AT URI: ' . $uri))
MC/DC0%0/2
$parsed // throw(InvalidAtUriError('Invalid AT URI: ' . $uri))
$parsed
throw(InvalidAtUriError('Invalid AT URI: ' . $uri))
Truth table33%1/3
A // B
A= $parsedB= throw(InvalidAtUriError('Invalid AT URI: ' . $uri))
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||||||||
| 41 | 149 | bless $parsed, $class; | |||||||||||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | 149 | ▶ | sub _parse($uri) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse: called CC=6 SCAR=18.1
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | 149 | my @res = $uri =~ ATP_URI_REGEX(); | |||||||||||||||||||||||||||||||||||||||||
| 46 | 149 | ▶ | @res or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 149 | ▶ | { hash => $res[4] // '', host => $res[1] // '', pathname => $res[2] // '', searchParams => At::Protocol::URI::_query->new( $res[3] // '' ) }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition87%7/8
$res[4] // ""
$res[1] // ""
$res[2] // ""
$res[3] // ""
MC/DC100%1/1
$res[4] // ""
$res[4]
MC/DC0%0/1
$res[1] // ""
$res[1]
MC/DC100%1/1
$res[2] // ""
$res[2]
MC/DC100%1/1
$res[3] // ""
$res[3]
Truth table100%2/2
A
A= $res[4]
Truth table50%1/2
A
A= $res[1]
Truth table100%2/2
A
A= $res[2]
Truth table100%2/2
A
A= $res[3]
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | 90 | ▶ | sub _parseRelative($uri) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parseRelative: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | 90 | my @res = $uri =~ RELATIVE_REGEX(); | |||||||||||||||||||||||||||||||||||||||||
| 52 | 90 | ▶ | @res or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | 90 | ▶ | { hash => $res[2] // '', pathname => $res[0] // '', searchParams => At::Protocol::URI::_query->new( $res[1] // '' ) }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%6/6
$res[2] // ""
$res[0] // ""
$res[1] // ""
MC/DC100%1/1
$res[2] // ""
$res[2]
MC/DC100%1/1
$res[0] // ""
$res[0]
MC/DC100%1/1
$res[1] // ""
$res[1]
Truth table100%2/2
A
A= $res[2]
Truth table100%2/2
A
A= $res[0]
Truth table100%2/2
A
A= $res[1]
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | 14 | ▶ | sub as_string($s) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
as_string: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | 14 | ▶ | my $path = $s->pathname // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$s->pathname // ""
MC/DC0%0/1
$s->pathname // ""
$s->pathname
Truth table50%1/2
A
A= $s->pathname
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 14 | ▶ | $path = '/' . $path if $path !~ m[^/]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 14 | my $qs = $s->search; | |||||||||||||||||||||||||||||||||||||||||
| 60 | 14 | ▶ | $qs = '?' . $qs if length $qs && $qs !~ m[^\?]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
length $qs and not $qs =~ /^\?/u
MC/DC50%1/2
length $qs and not $qs =~ /^\?/u
length $qs
not $qs =~ /^\?/u
Truth table66%2/3
A and not(B)
A= length $qsB= not $qs =~ /^\?/u
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 14 | my $hash = $s->hash; | |||||||||||||||||||||||||||||||||||||||||
| 62 | 14 | ▶ | $hash = '#' . $hash if length $hash && $hash !~ m[^#]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
length $hash and not $hash =~ /^#/u
MC/DC100%2/2
length $hash and not $hash =~ /^#/u
length $hash
not $hash =~ /^#/u
Truth table100%3/3
A and not(B)
A= length $hashB= not $hash =~ /^#/u
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | 14 | join '', grep {defined} $s->origin, $path, $qs, $hash; | |||||||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | ▶ | sub create ( $handle_r_did, $collection //= (), $rkey //= () ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
create: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | At::Protocol::URI->new( join '/', grep {defined} $handle_r_did, $collection, $rkey ); | |||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||
| 69 | 382 | ▶ | sub protocol ($s) {'at:'} | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
protocol: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 243 | ▶ | sub origin($s) { $s->protocol . '//' . $s->host } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
origin: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 475 | ▶ | sub host ( $s, $v //= () ) { $v // return $s->{host}; $s->{host} = $v } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
host: called CC=3 SCAR=11.0
Pod
documented
Condition100%2/2
$v // (return $s->{"host"})
MC/DC100%1/1
$v // (return $s->{"host"})
$v
Truth table100%2/2
A
A= $v
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 174 | ▶ | sub pathname( $s, $v //= () ) { $v // return $s->{pathname}; $s->{pathname} = $v } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
pathname: called CC=3 SCAR=11.0
Pod
documented
Condition100%2/2
$v // (return $s->{"pathname"})
MC/DC100%1/1
$v // (return $s->{"pathname"})
$v
Truth table100%2/2
A
A= $v
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | 155 | ▶ | sub search ( $s, $v //= () ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
search: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | 155 | ▶ | $v // return $s->{searchParams}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$v // (return $s->{"searchParams"})
MC/DC100%1/1
$v // (return $s->{"searchParams"})
$v
Truth table100%2/2
A
A= $v
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | 1 | $s->{searchParams}->parse_params($v); | |||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||
| 78 | 155 | ▶ | sub hash ( $s, $v //= () ) { $v // return $s->{hash}; $s->{hash} = $v; } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
hash: called CC=3 SCAR=11.0
Pod
documented
Condition100%2/2
$v // (return $s->{"hash"})
MC/DC100%1/1
$v // (return $s->{"hash"})
$v
Truth table100%2/2
A
A= $v
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | 5 | ▶ | sub collection ( $s, $v //= () ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
collection: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | 5 | ▶ | return [ grep {length} split '/', $s->pathname ]->[0] || '' unless defined $v; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
[grep({length $_;} split(m[/]u, $s->pathname, 0))]->[0] || ''
MC/DC100%1/1
[grep({length $_;} split(m[/]u, $s->pathname, 0))]->[0] || ''
[grep({length $_;} split(m[/]u, $s->pathname, 0))]->[0]
Truth table100%2/2
A
A= [grep({length $_;} split(m[/]u, $s->pathname, 0))]->[0]
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | 2 | my @parts = split '/', $s->pathname; | |||||||||||||||||||||||||||||||||||||||||
| 83 | 2 | $parts[0] = $v; | |||||||||||||||||||||||||||||||||||||||||
| 84 | 2 | $s->pathname( join '/', @parts ); | |||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | 6 | ▶ | sub rkey ( $s, $v //= () ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
rkey: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | 6 | ▶ | return [ grep {length} split '/', $s->pathname ]->[1] || '' unless defined $v; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
[grep({length $_;} split(m[/]u, $s->pathname, 0))]->[1] || ''
MC/DC100%1/1
[grep({length $_;} split(m[/]u, $s->pathname, 0))]->[1] || ''
[grep({length $_;} split(m[/]u, $s->pathname, 0))]->[1]
Truth table100%2/2
A
A= [grep({length $_;} split(m[/]u, $s->pathname, 0))]->[1]
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 3 | my @parts = split '/', $s->pathname; | |||||||||||||||||||||||||||||||||||||||||
| 90 | 3 | ▶ | $parts[0] //= 'undefined'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$parts[0] //= "undefined"
MC/DC100%1/1
$parts[0] //= "undefined"
$parts[0]
Truth table100%2/2
A
A= $parts[0]
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | 3 | $parts[1] = $v; | |||||||||||||||||||||||||||||||||||||||||
| 92 | 3 | $s->pathname( join '/', @parts ); | |||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | #~ Validation utils from https://github.com/bluesky-social/atproto/blob/main/packages/syntax/src/aturi_validation.ts | ||||||||||||||||||||||||||||||||||||||||||
| 96 | #~ Human-readable constraints on ATURI: | ||||||||||||||||||||||||||||||||||||||||||
| 97 | #~ - following regular URLs, a 8KByte hard total length limit | ||||||||||||||||||||||||||||||||||||||||||
| 98 | #~ - follows ATURI docs on website | ||||||||||||||||||||||||||||||||||||||||||
| 99 | #~ - all ASCII characters, no whitespace. non-ASCII could be URL-encoded | ||||||||||||||||||||||||||||||||||||||||||
| 100 | #~ - starts "at://" | ||||||||||||||||||||||||||||||||||||||||||
| 101 | #~ - "authority" is a valid DID or a valid handle | ||||||||||||||||||||||||||||||||||||||||||
| 102 | #~ - optionally, follow "authority" with "/" and valid NSID as start of path | ||||||||||||||||||||||||||||||||||||||||||
| 103 | #~ - optionally, if NSID given, follow that with "/" and rkey | ||||||||||||||||||||||||||||||||||||||||||
| 104 | #~ - rkey path component can include URL-encoded ("percent encoded"), or: | ||||||||||||||||||||||||||||||||||||||||||
| 105 | #~ ALPHA / DIGIT / "-" / "." / "_" / "~" / ":" / "@" / "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "=" | ||||||||||||||||||||||||||||||||||||||||||
| 106 | #~ [a-zA-Z0-9._~:@!$&'\(\)*+,;=-] | ||||||||||||||||||||||||||||||||||||||||||
| 107 | #~ - rkey must have at least one char | ||||||||||||||||||||||||||||||||||||||||||
| 108 | #~ - regardless of path component, a fragment can follow as "#" and then a JSON pointer (RFC-6901) | ||||||||||||||||||||||||||||||||||||||||||
| 109 | 192 | ▶ | sub ensureValidAtUri($uri) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ensureValidAtUri: called CC=20 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | 192 | my $fragmentPart; | |||||||||||||||||||||||||||||||||||||||||
| 111 | 192 | my @uriParts = split '#', $uri, -1; # negative limit, ftw | |||||||||||||||||||||||||||||||||||||||||
| 112 | 192 | ▶ | throw InvalidAtUriError('ATURI can have at most one "#", separating fragment out') if scalar @uriParts > 2; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | 188 | $fragmentPart = $uriParts[1]; | |||||||||||||||||||||||||||||||||||||||||
| 114 | 188 | $uri = $uriParts[0]; | |||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | # Check that all chars are boring ASCII | ||||||||||||||||||||||||||||||||||||||||||
| 117 | 188 | ▶ | throw InvalidAtUriError('Disallowed characters in ATURI (ASCII)') unless $uri =~ /^[a-zA-Z0-9._~:@!\$&')(*+,;=%\/-]*$/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | # | ||||||||||||||||||||||||||||||||||||||||||
| 119 | 177 | my @parts = split /\//, $uri, -1; # negative limit, ftw | |||||||||||||||||||||||||||||||||||||||||
| 120 | 177 | ▶ | throw InvalidAtUriError('ATURI must start with "at://"') if scalar @parts >= 3 && ( $parts[0] ne 'at:' || length $parts[1] ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
scalar @parts >= 3 and $parts[0] ne "at:" || length $parts[1]
$parts[0] ne "at:" or length $parts[1]
MC/DC100%3/3
scalar @parts >= 3 and $parts[0] ne "at:" || length $parts[1]
scalar @parts >= 3
$parts[0] ne "at:"
length $parts[1]
Truth table100%4/4
A and B or C
A= scalar @parts >= 3B= $parts[0] ne "at:"C= length $parts[1]
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | 163 | ▶ | throw InvalidAtUriError('ATURI requires at least method and authority sections') if scalar @parts < 3; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | 156 | try { | |||||||||||||||||||||||||||||||||||||||||
| 123 | 156 | ▶ | if ( $parts[2] =~ m/^did:/ ) { ensureValidDid( $parts[2] ); } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | 31 | else { ensureValidHandle( $parts[2] ) } | |||||||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||||||||
| 126 | catch ($err) { | ||||||||||||||||||||||||||||||||||||||||||
| 127 | 24 | throw InvalidAtUriError('ATURI authority must be a valid handle or DID'); | |||||||||||||||||||||||||||||||||||||||||
| 128 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 129 | 132 | ▶ | if ( scalar @parts >= 4 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 130 | 110 | ▶ | if ( !length $parts[3] ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 131 | 8 | throw InvalidAtUriError('ATURI can not have a slash after authority without a path segment'); | |||||||||||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||||||||||||
| 133 | 102 | try { | |||||||||||||||||||||||||||||||||||||||||
| 134 | 102 | ensureValidNsid( $parts[3] ); | |||||||||||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||||||||||
| 136 | catch ($err) { | ||||||||||||||||||||||||||||||||||||||||||
| 137 | 18 | throw InvalidAtUriError('ATURI requires first path segment (if supplied) to be valid NSID') | |||||||||||||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||||||
| 140 | 106 | ▶ | if ( scalar @parts >= 5 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 141 | 69 | ▶ | throw InvalidAtUriError('ATURI can not have a slash after collection, unless record key is provided') if !length $parts[4] | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | # would validate rkey here, but there are basically no constraints! | ||||||||||||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||||||||||||
| 145 | 102 | ▶ | throw InvalidAtUriError('ATURI path can have at most two parts, and no trailing slash') if scalar @parts >= 6; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 146 | 94 | ▶ | throw InvalidAtUriError('ATURI fragment must be non-empty and start with slash') if scalar @uriParts >= 2 && !defined $fragmentPart; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
scalar @uriParts >= 2 and not defined $fragmentPart
MC/DC0%0/2
scalar @uriParts >= 2 and not defined $fragmentPart
scalar @uriParts >= 2
not defined $fragmentPart
Truth table66%2/3
A and not(B)
A= scalar @uriParts >= 2B= not defined $fragmentPart
| |||||||||||||||||||||||||||||||||||||||||||
| 147 | 94 | ▶ | if ( defined $fragmentPart ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 148 | 24 | ▶ | throw InvalidAtUriError('ATURI fragment must be non-empty and start with slash') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
length $fragmentPart == 0 or substr($fragmentPart, 0, 1) ne "/"
MC/DC100%2/2
length $fragmentPart == 0 or substr($fragmentPart, 0, 1) ne "/"
length $fragmentPart == 0
substr($fragmentPart, 0, 1) ne "/"
Truth table100%3/3
A or B
A= length $fragmentPart == 0B= substr($fragmentPart, 0, 1) ne "/"
| |||||||||||||||||||||||||||||||||||||||||||
| 149 | if length $fragmentPart == 0 || substr( $fragmentPart, 0, 1 ) ne '/'; | ||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||
| 151 | # NOTE: enforcing *some* checks here for sanity. Eg, at least no whitespace | ||||||||||||||||||||||||||||||||||||||||||
| 152 | 18 | ▶ | throw InvalidAtUriError( 'Disallowed characters in ATURI fragment (ASCII)' . $fragmentPart ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 153 | if $fragmentPart !~ /^\/[a-zA-Z0-9._~:@!\$&')(*+,;=%[\]\/-]*$/; | ||||||||||||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||||||||||||
| 155 | 84 | ▶ | throw InvalidAtUriError('ATURI is far too long') if length $uri > 8 * 1024; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 156 | 82 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||
| 159 | 192 | ▶ | sub ensureValidAtUriRegex($uri) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ensureValidAtUriRegex: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||
| 161 | #~ simple regex to enforce most constraints via just regex and length. | ||||||||||||||||||||||||||||||||||||||||||
| 162 | 192 | my $aturiRegex | |||||||||||||||||||||||||||||||||||||||||
| 163 | = qr/^at:\/\/(?<authority>[a-zA-Z0-9._:%-]+)(\/(?<collection>[a-zA-Z0-9-.]+)(\/(?<rkey>[a-zA-Z0-9._~:@!\$&%')(*+,;=-]+))?)?(#(?<fragment>\/[a-zA-Z0-9._~:@!\$&%')(*+,;=\-[\]\/\\]*))?$/; | ||||||||||||||||||||||||||||||||||||||||||
| 164 | 192 | my ($rm) = $uri =~ $aturiRegex; | |||||||||||||||||||||||||||||||||||||||||
| 165 | 192 | ▶ | throw InvalidAtUriError(q[ATURI didn't validate via regex]) if !$rm || !keys %+; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$rm and keys %+
MC/DC50%1/2
$rm and keys %+
$rm
keys %+
Truth table66%2/3
A and B
A= $rmB= keys %+
| |||||||||||||||||||||||||||||||||||||||||||
| 166 | 108 | my %groups = %+; | |||||||||||||||||||||||||||||||||||||||||
| 167 | 108 | try { | |||||||||||||||||||||||||||||||||||||||||
| 168 | ensureValidHandleRegex( $groups{authority} ) | ||||||||||||||||||||||||||||||||||||||||||
| 169 | 108 | } | |||||||||||||||||||||||||||||||||||||||||
| 170 | catch ($err) { | ||||||||||||||||||||||||||||||||||||||||||
| 171 | 103 | try { | |||||||||||||||||||||||||||||||||||||||||
| 172 | ensureValidDidRegex( $groups{authority} ) | ||||||||||||||||||||||||||||||||||||||||||
| 173 | 103 | } | |||||||||||||||||||||||||||||||||||||||||
| 174 | catch ($err) { | ||||||||||||||||||||||||||||||||||||||||||
| 175 | 18 | throw InvalidAtUriError('ATURI authority must be a valid handle or DID') | |||||||||||||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||||||||||||||
| 178 | 90 | ▶ | if ( defined $groups{collection} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 179 | 70 | try { | |||||||||||||||||||||||||||||||||||||||||
| 180 | ensureValidNsidRegex( $groups{collection} ) | ||||||||||||||||||||||||||||||||||||||||||
| 181 | 70 | } | |||||||||||||||||||||||||||||||||||||||||
| 182 | catch ($err) { | ||||||||||||||||||||||||||||||||||||||||||
| 183 | 6 | throw InvalidAtUriError('ATURI collection path segment must be a valid NSID'); | |||||||||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||||||||||||||
| 186 | 84 | ▶ | throw InvalidAtUriError('ATURI is far too long') if length $uri > 8 * 1024; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 187 | 82 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||
| 190 | # fatal error | ||||||||||||||||||||||||||||||||||||||||||
| 191 | 5 | register 'InvalidAtUriError', 1; | |||||||||||||||||||||||||||||||||||||||||
| 192 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 193 | package # | ||||||||||||||||||||||||||||||||||||||||||
| 194 | 5 | At::Protocol::URI::_query 1.0 { | |||||||||||||||||||||||||||||||||||||||||
| 195 | 5 | ▶ | use v5.42; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 196 | 5 | ▶ | use URI::Escape qw[uri_escape_utf8 uri_unescape]; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 197 | use overload | ||||||||||||||||||||||||||||||||||||||||||
| 198 | 5 | ▶ | '""' => sub ( $s, $u, $q ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 199 | 171 | $s->as_string; | |||||||||||||||||||||||||||||||||||||||||
| 200 | 5 | }; | |||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||
| 202 | 240 | ▶ | sub new( $class, $qs ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 203 | 240 | my $s = bless [], $class; | |||||||||||||||||||||||||||||||||||||||||
| 204 | 240 | $s->parse_params($qs); | |||||||||||||||||||||||||||||||||||||||||
| 205 | 240 | $s; | |||||||||||||||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||
| 208 | 241 | ▶ | sub parse_params( $s, $qs ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse_params: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 209 | 241 | $qs =~ s[^\?+][]; # Just in case | |||||||||||||||||||||||||||||||||||||||||
| 210 | @$s = map { | ||||||||||||||||||||||||||||||||||||||||||
| 211 | 241 | [ map { uri_unescape($_) } split /=/, $_, 2 ] | |||||||||||||||||||||||||||||||||||||||||
| 212 | } split /[&;]/, $qs; | ||||||||||||||||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||
| 215 | 2 | ▶ | sub get_param( $s, $name ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_param: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 216 | 2 | map { $_->[1] } grep { $_->[0] eq $name } @$s; | |||||||||||||||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||
| 219 | 2 | ▶ | sub add_param( $s, $name, @v ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_param: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 220 | 2 | $name = uri_unescape $name; | |||||||||||||||||||||||||||||||||||||||||
| 221 | 2 | ▶ | push @$s, [ $name, uri_unescape shift @v ] while @v; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 222 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||||||||||||
| 225 | 5 | ▶ | sub set_param( $s, $name, @v ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
set_param: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 226 | 5 | $name = uri_unescape $name; | |||||||||||||||||||||||||||||||||||||||||
| 227 | 5 | for my $slot ( grep { $_->[0] eq $name } @$s ) { | |||||||||||||||||||||||||||||||||||||||||
| 228 | 4 | $slot->[1] = uri_unescape shift @v; | |||||||||||||||||||||||||||||||||||||||||
| 229 | 4 | ▶ | @v || last; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||||||||||||||||
| 231 | 5 | ▶ | push @$s, [ $name, uri_unescape shift @v ] while @v; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 232 | 5 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||
| 235 | 2 | ▶ | sub delete_param( $s, $name ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
delete_param: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 236 | 2 | $name = uri_unescape $name; | |||||||||||||||||||||||||||||||||||||||||
| 237 | 2 | @$s = grep { $_->[0] ne $name } @$s; | |||||||||||||||||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||||||
| 240 | 1 | ▶ | sub replace_param( $s, $name, @v ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
replace_param: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 241 | 1 | $s->delete_param($name); | |||||||||||||||||||||||||||||||||||||||||
| 242 | 1 | $name = uri_unescape $name; | |||||||||||||||||||||||||||||||||||||||||
| 243 | 1 | ▶ | push @$s, [ $name, uri_unescape shift @v ] while @v; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 244 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||
| 247 | 1 | ▶ | sub reset($s) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
reset: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 248 | 1 | !( @$s = () ); | |||||||||||||||||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||
| 251 | 179 | ▶ | sub as_string( $s, $sep //= '&' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
as_string: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 252 | 179 | join $sep, map { join '=', uri_escape_utf8( $_->[0] ), uri_escape_utf8( $_->[1] ) } @$s; | |||||||||||||||||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||||||||||||||||
| 254 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 255 | 5 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 256 | __END__ | ||||||||||||||||||||||||||||||||||||||||||