| 1 | package OpenID::Lite::Util::URI; | ||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||
| 3 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 4 | 8 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||
| 6 | 8 | ▶ | use URI; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 7 | 8 | ▶ | use List::MoreUtils qw(none any); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||
| 10 | 8 | ▶ | sub _build_url_regexp { | ||||||||||||||||||||||||||||||||
|
Subroutine
_build_url_regexp: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 11 | 8 | my $class = shift; | |||||||||||||||||||||||||||||||||
| 12 | 8 | my $digit = q{[0-9]}; | |||||||||||||||||||||||||||||||||
| 13 | 8 | my $upalpha = q{[A-Z]}; | |||||||||||||||||||||||||||||||||
| 14 | 8 | my $lowalpha = q{[a-z]}; | |||||||||||||||||||||||||||||||||
| 15 | 8 | my $alpha = qq{(?:$lowalpha|$upalpha)}; | |||||||||||||||||||||||||||||||||
| 16 | 8 | my $alphanum = qq{(?:$alpha|$digit)}; | |||||||||||||||||||||||||||||||||
| 17 | 8 | my $hex = qq{(?:$digit|[A-Fa-f])}; | |||||||||||||||||||||||||||||||||
| 18 | 8 | my $escaped = qq{%$hex$hex}; | |||||||||||||||||||||||||||||||||
| 19 | 8 | my $mark = q{[-_.!~*'()]}; | |||||||||||||||||||||||||||||||||
| 20 | 8 | my $unreserved = qq{(?:$alphanum|$mark)}; | |||||||||||||||||||||||||||||||||
| 21 | 8 | my $reserved = q{[;/?:@&=+$,]}; | |||||||||||||||||||||||||||||||||
| 22 | 8 | my $uric = qq{(?:$reserved|$unreserved|$escaped)}; | |||||||||||||||||||||||||||||||||
| 23 | 8 | my $query = qq{$uric*}; | |||||||||||||||||||||||||||||||||
| 24 | 8 | my $pchar = qq{(?:$unreserved|$escaped|} . q{[:@&=+$,])}; | |||||||||||||||||||||||||||||||||
| 25 | 8 | my $param = qq{$pchar*}; | |||||||||||||||||||||||||||||||||
| 26 | 8 | my $segment = qq{$pchar*(?:;$param)*}; | |||||||||||||||||||||||||||||||||
| 27 | 8 | my $path_segments = qq{$segment(?:/$segment)*}; | |||||||||||||||||||||||||||||||||
| 28 | 8 | my $abs_path = qq{/$path_segments}; | |||||||||||||||||||||||||||||||||
| 29 | 8 | my $port = qq{$digit*}; | |||||||||||||||||||||||||||||||||
| 30 | 8 | my $IPv4address = qq{$digit+\\.$digit+\\.$digit+\\.$digit+}; | |||||||||||||||||||||||||||||||||
| 31 | 8 | my $toplabel = qq{(?:$alpha|$alpha(?:$alphanum|-)*$alphanum)}; | |||||||||||||||||||||||||||||||||
| 32 | 8 | my $domainlabel = qq{(?:$alphanum|$alphanum(?:$alphanum|-)*$alphanum)}; | |||||||||||||||||||||||||||||||||
| 33 | 8 | my $hostname = qq{(?:$domainlabel\\.)*$toplabel\\.?}; | |||||||||||||||||||||||||||||||||
| 34 | 8 | my $host = qq{(?:$hostname|$IPv4address)}; | |||||||||||||||||||||||||||||||||
| 35 | 8 | my $fragment = qq{$uric*}; | |||||||||||||||||||||||||||||||||
| 36 | 8 | my $pattern = qq{https?://$host(?::$port)?(?:$abs_path(?:\\?$query)?)?(?:#$fragment)?}; | |||||||||||||||||||||||||||||||||
| 37 | 8 | return $pattern; | |||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||
| 40 | 8 | my $REGEX = __PACKAGE__->_build_url_regexp(); | |||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||
| 42 | 106 | ▶ | sub is_uri { | ||||||||||||||||||||||||||||||||
|
Subroutine
is_uri: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 43 | 106 | my $class = shift; | |||||||||||||||||||||||||||||||||
| 44 | 106 | my $uri = shift; | |||||||||||||||||||||||||||||||||
| 45 | 106 | return $uri =~ /^$REGEX$/o; | |||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||
| 48 | 133 | ▶ | sub normalize { | ||||||||||||||||||||||||||||||||
|
Subroutine
normalize: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 49 | 133 | my $class = shift; | |||||||||||||||||||||||||||||||||
| 50 | 133 | my $uri = shift; | |||||||||||||||||||||||||||||||||
| 51 | 133 | my $u = URI->new($uri); | |||||||||||||||||||||||||||||||||
| 52 | 133 | ▶ | return unless $u->scheme; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 53 | 127 | ▶ | return if ( none { $_ eq $u->scheme } qw(http https)); | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 54 | 124 | ▶ | return unless $u->can('host') && $u->host; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$u->can("host") and $u->host
MC/DC50%1/2
$u->can("host") and $u->host
$u->can("host")
$u->host
Truth table66%2/3
A and B
A= $u->can("host")B= $u->host
| |||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||
| 56 | 123 | my $path = $class->_remove_dot_segments($u->path); | |||||||||||||||||||||||||||||||||
| 57 | 123 | ▶ | $path = '/' if length($path) == 0; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 58 | 123 | $u->path($path); | |||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||
| 60 | 123 | my $u_str = $u->canonical->as_string; | |||||||||||||||||||||||||||||||||
| 61 | 123 | $u_str =~ s/(%[a-fA-F0-9]{2})/uc $class->_encode($1)/eg; | |||||||||||||||||||||||||||||||||
| 62 | 123 | return $u_str; | |||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||
| 65 | 6 | ▶ | sub _encode { | ||||||||||||||||||||||||||||||||
|
Subroutine
_encode: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||
| 66 | 6 | my ( $class, $u ) = @_; | |||||||||||||||||||||||||||||||||
| 67 | 6 | my $num = substr($u, 1); | |||||||||||||||||||||||||||||||||
| 68 | 6 | my $packed = pack('H*', $num); | |||||||||||||||||||||||||||||||||
| 69 | 6 | ▶ | return $packed =~ /[A-Za-z0-9._~-]/ ? $packed : $u; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||
| 72 | 123 | ▶ | sub _remove_dot_segments { | ||||||||||||||||||||||||||||||||
|
Subroutine
_remove_dot_segments: called CC=14 SCAR=27.7
| |||||||||||||||||||||||||||||||||||
| 73 | 123 | my ( $class, $path ) = @_; | |||||||||||||||||||||||||||||||||
| 74 | 123 | my @result_segments; | |||||||||||||||||||||||||||||||||
| 75 | 123 | ▶ | while ( length($path) > 0 ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 76 | 146 | ▶ | if ($path =~ m!^\.\./!) { | ||||||||||||||||||||||||||||||||
|
Branch: 7 branches
| |||||||||||||||||||||||||||||||||||
| 77 | 0 | $path = substr($path, 3); | |||||||||||||||||||||||||||||||||
| 78 | } elsif ($path =~ m!^\./!) { | ||||||||||||||||||||||||||||||||||
| 79 | 0 | $path = substr($path, 2); | |||||||||||||||||||||||||||||||||
| 80 | } elsif ($path =~ m!^/\./!) { | ||||||||||||||||||||||||||||||||||
| 81 | 4 | $path = substr($path, 2); | |||||||||||||||||||||||||||||||||
| 82 | } elsif ($path eq q{/.}) { | ||||||||||||||||||||||||||||||||||
| 83 | 1 | $path = q{/}; | |||||||||||||||||||||||||||||||||
| 84 | } elsif ($path =~ m!^/\.\./!) { | ||||||||||||||||||||||||||||||||||
| 85 | 7 | $path = substr($path, 3); | |||||||||||||||||||||||||||||||||
| 86 | 7 | ▶ | pop(@result_segments) if @result_segments > 0; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 87 | } elsif ($path eq q{/..}) { | ||||||||||||||||||||||||||||||||||
| 88 | 1 | $path = q{/}; | |||||||||||||||||||||||||||||||||
| 89 | 1 | ▶ | pop(@result_segments) if @result_segments > 0; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 90 | ▶ | } elsif ($path eq q{..} || $path eq q{.}) { | |||||||||||||||||||||||||||||||||
|
Condition33%1/3
$path eq ".." or $path eq "."
MC/DC0%0/2
$path eq ".." or $path eq "."
$path eq ".."
$path eq "."
Truth table33%1/3
A or B
A= $path eq ".."B= $path eq "."
| |||||||||||||||||||||||||||||||||||
| 91 | 0 | $path = q{}; | |||||||||||||||||||||||||||||||||
| 92 | } else { | ||||||||||||||||||||||||||||||||||
| 93 | 133 | my $i = 0; | |||||||||||||||||||||||||||||||||
| 94 | 133 | ▶ | $i = 1 if substr($path, 0, 1) eq q{/}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 95 | 133 | $i = index($path, q{/}, $i); | |||||||||||||||||||||||||||||||||
| 96 | 133 | ▶ | $i = length($path) unless $i >= 0; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 97 | 133 | push(@result_segments, substr($path, 0, $i)); | |||||||||||||||||||||||||||||||||
| 98 | 133 | $path = substr($path, $i); | |||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||
| 101 | 123 | return join('', @result_segments); | |||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||
| 104 | 8 | 1; | |||||||||||||||||||||||||||||||||