| 1 | package Skilletor::URL; | ||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: The parts of a URL as the WHATWG URL parser (Node's new URL) gives them | ||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||
| 4 | 39 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 5 | 39 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||
| 7 | 39 | ▶ | use Encode (); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 8 | 39 | ▶ | use Exporter qw( import ); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 9 | 39 | ▶ | use Skilletor::FSUtil qw( decode_utf8_lossy ); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 10 | 39 | ▶ | use Unicode::Normalize (); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||
| 12 | 39 | our $VERSION = '0.001'; | |||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||
| 14 | 39 | our @EXPORT_OK = qw( parse_url punycode_encode ); | |||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||
| 16 | 39 | my %SPECIAL = map { $_ => 1 } qw( ftp file http https ws wss ); | |||||||||||||||||||||||||||||||||||
| 17 | 39 | my %DEFAULT_PORT = ( ftp => 21, http => 80, https => 443, ws => 80, wss => 443 ); | |||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||
| 19 | # Forbidden host code points; a domain also forbids C0 controls, "%" and DEL. | ||||||||||||||||||||||||||||||||||||
| 20 | 39 | my $FORBIDDEN_HOST = qr/[\x00\t\n\r #\/:<>?\@\[\\\]^|]/; | |||||||||||||||||||||||||||||||||||
| 21 | 39 | my $FORBIDDEN_DOMAIN = qr/[\x00-\x1F #%\/:<>?\@\[\\\]^|\x7F]/; | |||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||
| 23 | 384 | ▶ | sub _pct_encode { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_pct_encode: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 24 | 384 | my ( $s, $set ) = @_; | |||||||||||||||||||||||||||||||||||
| 25 | 384 | $s =~ s{($set)}{ join '', map { sprintf '%%%02X', $_ } unpack 'C*', Encode::encode( 'UTF-8', $1 ) }ge; | |||||||||||||||||||||||||||||||||||
| 26 | 384 | return $s; | |||||||||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||
| 29 | # The path percent-encode set: C0 controls, space, " # < > ? ` { } and everything above "~". | ||||||||||||||||||||||||||||||||||||
| 30 | 39 | my $PATH_SET = qr/[\x00-\x20"#<>?`{}\x7F-\x{10FFFF}]/; | |||||||||||||||||||||||||||||||||||
| 31 | 39 | my $C0_SET = qr/[\x00-\x1F\x7F-\x{10FFFF}]/; | |||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||
| 33 | 185 | ▶ | sub _pct_decode_bytes { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_pct_decode_bytes: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 34 | 185 | my ( $s ) = @_; | |||||||||||||||||||||||||||||||||||
| 35 | 185 | my $bytes = Encode::encode( 'UTF-8', $s ); | |||||||||||||||||||||||||||||||||||
| 36 | 185 | $bytes =~ s/%([0-9A-Fa-f]{2})/chr hex $1/ge; | |||||||||||||||||||||||||||||||||||
| 37 | 185 | return $bytes; | |||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||
| 40 | 241 | ▶ | sub parse_url { | ||||||||||||||||||||||||||||||||||
|
Subroutine
parse_url: called CC=27 SCAR=33.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 41 | 241 | my ( $input ) = @_; | |||||||||||||||||||||||||||||||||||
| 42 | 241 | my $s = $input; | |||||||||||||||||||||||||||||||||||
| 43 | 241 | $s =~ s/\A[\x00-\x20]+//; | |||||||||||||||||||||||||||||||||||
| 44 | 241 | $s =~ s/[\x00-\x20]+\z//; | |||||||||||||||||||||||||||||||||||
| 45 | 241 | $s =~ s/[\t\n\r]//g; | |||||||||||||||||||||||||||||||||||
| 46 | 241 | ▶ | return undef unless $s =~ /\A([A-Za-z][A-Za-z0-9+.\-]*):(.*)\z/s; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 47 | 241 | my ( $scheme, $rest ) = ( lc $1, $2 ); | |||||||||||||||||||||||||||||||||||
| 48 | 241 | ▶ | my $special = $SPECIAL{$scheme} ? 1 : 0; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 49 | 241 | my %url = ( protocol => $scheme.':', username => '', password => '', hostname => '', port => '', search => '', hash => '' ); | |||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||
| 51 | # fragment and query | ||||||||||||||||||||||||||||||||||||
| 52 | 241 | ▶ | if ( $rest =~ s/#(.*)\z//s ) { $url{hash} = length $1 ? '#'.$1 : '' } | ||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||
| 53 | 241 | ▶ | if ( $rest =~ s/\?(.*)\z//s ) { $url{search} = length $1 ? '?'.$1 : '' } | ||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||
| 55 | 241 | ▶ | my $sep = $special ? qr{[/\\]} : qr{/}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 56 | 241 | my $path; | |||||||||||||||||||||||||||||||||||
| 57 | 241 | ▶ | if ( $special && $scheme ne 'file' ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition100%3/3
$special and $scheme ne "file"
MC/DC100%2/2
$special and $scheme ne "file"
$special
$scheme ne "file"
Truth table100%3/3
A and B
A= $specialB= $scheme ne "file"
| |||||||||||||||||||||||||||||||||||||
| 58 | 191 | $rest =~ s{\A[/\\]+}{}; | |||||||||||||||||||||||||||||||||||
| 59 | 191 | my ( $authority, $p ) = $rest =~ m{\A([^/\\]*)(.*)\z}s; | |||||||||||||||||||||||||||||||||||
| 60 | 191 | ▶ | return undef unless _authority( \%url, $authority, $scheme, 1 ); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 61 | 180 | ▶ | return undef if $url{hostname} eq ''; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 62 | 180 | $path = $p; | |||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||
| 64 | elsif ( $scheme eq 'file' ) { | ||||||||||||||||||||||||||||||||||||
| 65 | 10 | ▶ | if ( $rest =~ s{\A[/\\]{2}}{} ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 66 | 10 | my ( $host, $p ) = $rest =~ m{\A([^/\\]*)(.*)\z}s; | |||||||||||||||||||||||||||||||||||
| 67 | 10 | ▶ | if ( length $host ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 68 | 3 | my $h = _host( $host, 1 ); | |||||||||||||||||||||||||||||||||||
| 69 | 3 | ▶ | return undef unless defined $h; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 70 | 3 | ▶ | $url{hostname} = $h eq 'localhost' ? '' : $h; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||
| 72 | 10 | $path = $p; | |||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||
| 74 | else { | ||||||||||||||||||||||||||||||||||||
| 75 | 0 | $path = $rest; | |||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||
| 78 | elsif ( $rest =~ s{\A//}{} ) { | ||||||||||||||||||||||||||||||||||||
| 79 | 40 | my ( $authority, $p ) = $rest =~ m{\A([^/]*)(.*)\z}s; | |||||||||||||||||||||||||||||||||||
| 80 | 40 | ▶ | return undef unless _authority( \%url, $authority, $scheme, 0 ); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 81 | 39 | $path = $p; | |||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||
| 83 | else { | ||||||||||||||||||||||||||||||||||||
| 84 | # an opaque path: kept, C0 controls encoded | ||||||||||||||||||||||||||||||||||||
| 85 | 0 | $url{pathname} = _pct_encode( $rest, $C0_SET ); | |||||||||||||||||||||||||||||||||||
| 86 | 0 | return \%url; | |||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||
| 89 | 229 | my @segments; | |||||||||||||||||||||||||||||||||||
| 90 | 229 | ▶ | if ( $special || length $path ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$special or length $path
MC/DC100%2/2
$special or length $path
$special
length $path
Truth table100%3/3
A or B
A= $specialB= length $path
| |||||||||||||||||||||||||||||||||||||
| 91 | 216 | $path =~ s{\A$sep}{}; | |||||||||||||||||||||||||||||||||||
| 92 | 216 | my @raw = split /$sep/, $path, -1; | |||||||||||||||||||||||||||||||||||
| 93 | 216 | ▶ | @raw = ('') unless @raw; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 94 | 216 | for my $i ( 0 .. $#raw ) { | |||||||||||||||||||||||||||||||||||
| 95 | 361 | my $seg = $raw[$i]; | |||||||||||||||||||||||||||||||||||
| 96 | 361 | my $last = $i == $#raw; | |||||||||||||||||||||||||||||||||||
| 97 | 361 | ▶ | if ( $seg =~ /\A(?:\.|%2e)(?:\.|%2e)\z/i ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||
| 98 | 8 | pop @segments; | |||||||||||||||||||||||||||||||||||
| 99 | 8 | ▶ | push @segments, '' if $last; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||
| 101 | elsif ( $seg =~ /\A(?:\.|%2e)\z/i ) { | ||||||||||||||||||||||||||||||||||||
| 102 | 6 | ▶ | push @segments, '' if $last; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||
| 104 | else { | ||||||||||||||||||||||||||||||||||||
| 105 | 347 | push @segments, _pct_encode( $seg, $PATH_SET ); | |||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||
| 109 | 229 | $url{pathname} = join '', map { '/'.$_ } @segments; | |||||||||||||||||||||||||||||||||||
| 110 | 229 | return \%url; | |||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||
| 113 | 231 | ▶ | sub _authority { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_authority: called CC=16 SCAR=27.7
| |||||||||||||||||||||||||||||||||||||
| 114 | 231 | my ( $url, $authority, $scheme, $special ) = @_; | |||||||||||||||||||||||||||||||||||
| 115 | 231 | my $hostport = $authority; | |||||||||||||||||||||||||||||||||||
| 116 | 231 | my $at = rindex $authority, '@'; | |||||||||||||||||||||||||||||||||||
| 117 | 231 | ▶ | if ( $at >= 0 ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 118 | 19 | my $userinfo = substr $authority, 0, $at; | |||||||||||||||||||||||||||||||||||
| 119 | 19 | $hostport = substr $authority, $at + 1; | |||||||||||||||||||||||||||||||||||
| 120 | 19 | my ( $user, $pass ) = split /:/, $userinfo, 2; | |||||||||||||||||||||||||||||||||||
| 121 | 19 | ▶ | $url->{username} = $user // ''; | ||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$user // ""
MC/DC0%0/1
$user // ""
$user
Truth table50%1/2
A
A= $user
| |||||||||||||||||||||||||||||||||||||
| 122 | 19 | ▶ | $url->{password} = $pass // ''; | ||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$pass // ""
MC/DC100%1/1
$pass // ""
$pass
Truth table100%2/2
A
A= $pass
| |||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||
| 124 | 231 | my ( $host, $port ) = ( $hostport, '' ); | |||||||||||||||||||||||||||||||||||
| 125 | 231 | ▶ | if ( $hostport =~ /\A(\[[^\]]*\])(?::(.*))?\z/s ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||
| 126 | 4 | ▶ | ( $host, $port ) = ( $1, $2 // '' ); | ||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$2 // ""
MC/DC100%1/1
$2 // ""
$2
Truth table100%2/2
A
A= $2
| |||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||
| 128 | elsif ( ( my $c = index $hostport, ':' ) >= 0 ) { | ||||||||||||||||||||||||||||||||||||
| 129 | 38 | ( $host, $port ) = ( substr( $hostport, 0, $c ), substr( $hostport, $c + 1 ) ); | |||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||
| 131 | 231 | ▶ | if ( length $port ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 132 | 39 | ▶ | return 0 unless $port =~ /\A[0-9]+\z/; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 133 | 37 | ▶ | return 0 if $port > 65535; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 134 | 35 | $port = 0 + $port; | |||||||||||||||||||||||||||||||||||
| 135 | 35 | ▶ | $port = '' if defined $DEFAULT_PORT{$scheme} && $port == $DEFAULT_PORT{$scheme}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $DEFAULT_PORT{$scheme} and $port == $DEFAULT_PORT{$scheme}
MC/DC100%2/2
defined $DEFAULT_PORT{$scheme} and $port == $DEFAULT_PORT{$scheme}
defined $DEFAULT_PORT{$scheme}
$port == $DEFAULT_PORT{$scheme}
Truth table100%3/3
A and B
A= defined $DEFAULT_PORT{$scheme}B= $port == $DEFAULT_PORT{$scheme}
| |||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||
| 137 | 227 | $url->{port} = "$port"; | |||||||||||||||||||||||||||||||||||
| 138 | 227 | ▶ | return 0 if $special && $host eq ''; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$special and $host eq ""
MC/DC100%2/2
$special and $host eq ""
$special
$host eq ""
Truth table100%3/3
A and B
A= $specialB= $host eq ""
| |||||||||||||||||||||||||||||||||||||
| 139 | 225 | ▶ | return 1 if $host eq ''; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 140 | 224 | my $h = _host( $host, $special ); | |||||||||||||||||||||||||||||||||||
| 141 | 224 | ▶ | return 0 unless defined $h; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 142 | 218 | $url->{hostname} = $h; | |||||||||||||||||||||||||||||||||||
| 143 | 218 | return 1; | |||||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||
| 146 | 227 | ▶ | sub _host { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_host: called CC=9 SCAR=22.2
| |||||||||||||||||||||||||||||||||||||
| 147 | 227 | my ( $input, $special ) = @_; | |||||||||||||||||||||||||||||||||||
| 148 | 227 | ▶ | if ( $input =~ /\A\[/ ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 149 | 4 | ▶ | return undef unless $input =~ /\A\[([0-9A-Fa-f:.]+)\]\z/; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 150 | 4 | return '['.lc($1).']'; | |||||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||||
| 152 | 223 | ▶ | unless ($special) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 153 | 38 | ▶ | return undef if $input =~ $FORBIDDEN_HOST; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 154 | 37 | return _pct_encode( $input, $C0_SET ); | |||||||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||||||
| 156 | 185 | my $domain = decode_utf8_lossy( _pct_decode_bytes($input) ); | |||||||||||||||||||||||||||||||||||
| 157 | 185 | my $ascii = _domain_to_ascii($domain); | |||||||||||||||||||||||||||||||||||
| 158 | 185 | ▶ | return undef unless defined $ascii && length $ascii; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $ascii and length $ascii
MC/DC0%0/2
defined $ascii and length $ascii
defined $ascii
length $ascii
Truth table33%1/3
A and B
A= defined $asciiB= length $ascii
| |||||||||||||||||||||||||||||||||||||
| 159 | 185 | ▶ | return undef if $ascii =~ $FORBIDDEN_DOMAIN; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 160 | 183 | ▶ | if ( _ends_in_number($ascii) ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 161 | 35 | return _ipv4($ascii); | |||||||||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||||||||||
| 163 | 148 | return $ascii; | |||||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||
| 166 | # UTS #46 lite: NFC and lower case, then each label holding non-ASCII as punycode. | ||||||||||||||||||||||||||||||||||||
| 167 | 185 | ▶ | sub _domain_to_ascii { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_domain_to_ascii: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||
| 168 | 185 | my ( $domain ) = @_; | |||||||||||||||||||||||||||||||||||
| 169 | 185 | my $mapped = lc Unicode::Normalize::NFC($domain); | |||||||||||||||||||||||||||||||||||
| 170 | 185 | $mapped =~ s/[\x{3002}\x{FF0E}\x{FF61}]/./g; | |||||||||||||||||||||||||||||||||||
| 171 | 185 | my @labels = split /\./, $mapped, -1; | |||||||||||||||||||||||||||||||||||
| 172 | 185 | for my $label (@labels) { | |||||||||||||||||||||||||||||||||||
| 173 | 411 | ▶ | next unless $label =~ /[^\x00-\x7F]/; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 174 | 6 | my $encoded = punycode_encode($label); | |||||||||||||||||||||||||||||||||||
| 175 | 6 | ▶ | return undef unless defined $encoded; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 176 | 6 | $label = 'xn--'.$encoded; | |||||||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||||||||
| 178 | 185 | return join '.', @labels; | |||||||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||
| 181 | 274 | ▶ | sub _ipv4_number { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_ipv4_number: called CC=11 SCAR=24.3
| |||||||||||||||||||||||||||||||||||||
| 182 | 274 | my ( $s ) = @_; | |||||||||||||||||||||||||||||||||||
| 183 | 274 | ▶ | return undef if $s eq ''; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 184 | 274 | my $radix = 10; | |||||||||||||||||||||||||||||||||||
| 185 | 274 | ▶ | if ( $s =~ s/\A0[xX]// ) { $radix = 16 } | ||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||
| 186 | 1 | ▶ | elsif ( length $s > 1 && $s =~ s/\A0// ) { $radix = 8 } | ||||||||||||||||||||||||||||||||||
|
Condition100%3/3
length $s > 1 and $s =~ s/\A0//
MC/DC100%2/2
length $s > 1 and $s =~ s/\A0//
length $s > 1
$s =~ s/\A0//
Truth table100%3/3
A and B
A= length $s > 1B= $s =~ s/\A0//
| |||||||||||||||||||||||||||||||||||||
| 187 | 274 | ▶ | return 0 if $s eq ''; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 188 | 274 | ▶ | return $radix == 16 ? ( $s =~ /\A[0-9A-Fa-f]+\z/ ? hex $s : undef ) | ||||||||||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||||||||||
| 189 | : $radix == 8 ? ( $s =~ /\A[0-7]+\z/ ? oct $s : undef ) | ||||||||||||||||||||||||||||||||||||
| 190 | : ( $s =~ /\A[0-9]+\z/ ? 0 + $s : undef ); | ||||||||||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||
| 193 | 183 | ▶ | sub _ends_in_number { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_ends_in_number: called CC=8 SCAR=21.5
| |||||||||||||||||||||||||||||||||||||
| 194 | 183 | my ( $host ) = @_; | |||||||||||||||||||||||||||||||||||
| 195 | 183 | my @parts = split /\./, $host, -1; | |||||||||||||||||||||||||||||||||||
| 196 | 183 | ▶ | if ( @parts && $parts[-1] eq '' ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@parts and $parts[-1] eq ""
MC/DC50%1/2
@parts and $parts[-1] eq ""
@parts
$parts[-1] eq ""
Truth table66%2/3
A and B
A= @partsB= $parts[-1] eq ""
| |||||||||||||||||||||||||||||||||||||
| 197 | 1 | ▶ | return 0 if @parts == 1; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 198 | 1 | pop @parts; | |||||||||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||||||||||
| 200 | 183 | ▶ | my $last = $parts[-1] // ''; | ||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$parts[-1] // ""
MC/DC0%0/1
$parts[-1] // ""
$parts[-1]
Truth table50%1/2
A
A= $parts[-1]
| |||||||||||||||||||||||||||||||||||||
| 201 | 183 | ▶ | return 1 if $last ne '' && $last =~ /\A[0-9]+\z/; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$last ne "" and $last =~ /\A[0-9]+\z/
MC/DC50%1/2
$last ne "" and $last =~ /\A[0-9]+\z/
$last ne ""
$last =~ /\A[0-9]+\z/
Truth table66%2/3
A and B
A= $last ne ""B= $last =~ /\A[0-9]+\z/
| |||||||||||||||||||||||||||||||||||||
| 202 | 148 | ▶ | return defined _ipv4_number($last) ? 1 : 0; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||
| 205 | 35 | ▶ | sub _ipv4 { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_ipv4: called CC=10 SCAR=23.1
| |||||||||||||||||||||||||||||||||||||
| 206 | 35 | my ( $host ) = @_; | |||||||||||||||||||||||||||||||||||
| 207 | 35 | my @parts = split /\./, $host, -1; | |||||||||||||||||||||||||||||||||||
| 208 | 35 | ▶ | pop @parts if @parts > 1 && $parts[-1] eq ''; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@parts > 1 and $parts[-1] eq ""
MC/DC0%0/2
@parts > 1 and $parts[-1] eq ""
@parts > 1
$parts[-1] eq ""
Truth table66%2/3
A and B
A= @parts > 1B= $parts[-1] eq ""
| |||||||||||||||||||||||||||||||||||||
| 209 | 35 | ▶ | return undef if @parts > 4; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 210 | 35 | my @n; | |||||||||||||||||||||||||||||||||||
| 211 | 35 | for my $p (@parts) { | |||||||||||||||||||||||||||||||||||
| 212 | 126 | my $v = _ipv4_number($p); | |||||||||||||||||||||||||||||||||||
| 213 | 126 | ▶ | return undef unless defined $v; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 214 | 125 | push @n, $v; | |||||||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||||||
| 216 | 34 | ▶ | for my $i ( 0 .. $#n - 1 ) { return undef if $n[$i] > 255 } | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 217 | 33 | ▶ | return undef if $n[-1] >= 256**( 5 - @n ); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 218 | 32 | my $ipv4 = $n[-1]; | |||||||||||||||||||||||||||||||||||
| 219 | 32 | $ipv4 += $n[$_] * 256**( 3 - $_ ) for 0 .. $#n - 1; | |||||||||||||||||||||||||||||||||||
| 220 | 32 | return join '.', map { ( $ipv4 >> ( 8 * ( 3 - $_ ) ) ) & 255 } 0 .. 3; | |||||||||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||
| 223 | # RFC 3492 punycode of one label (without the xn-- prefix). | ||||||||||||||||||||||||||||||||||||
| 224 | 8 | ▶ | sub punycode_encode { | ||||||||||||||||||||||||||||||||||
|
Subroutine
punycode_encode: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 225 | 8 | my ( $input ) = @_; | |||||||||||||||||||||||||||||||||||
| 226 | 8 | my ( $base, $tmin, $tmax, $skew, $damp ) = ( 36, 1, 26, 38, 700 ); | |||||||||||||||||||||||||||||||||||
| 227 | 8 | my ( $n, $delta, $bias ) = ( 128, 0, 72 ); | |||||||||||||||||||||||||||||||||||
| 228 | 8 | my @cp = map { ord } split //, $input; | |||||||||||||||||||||||||||||||||||
| 229 | 8 | my $output = join '', map { chr } grep { $_ < 128 } @cp; | |||||||||||||||||||||||||||||||||||
| 230 | 8 | my $basic = length $output; | |||||||||||||||||||||||||||||||||||
| 231 | 8 | my $h = $basic; | |||||||||||||||||||||||||||||||||||
| 232 | 8 | ▶ | $output .= '-' if $basic > 0; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 233 | 8 | ▶ | my $digit = sub { my $d = shift; chr( $d < 26 ? 97 + $d : 22 + $d ) }; | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 234 | 10 | ▶ | my $adapt = sub { | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||
| 235 | 10 | my ( $d, $num, $first ) = @_; | |||||||||||||||||||||||||||||||||||
| 236 | 10 | ▶ | $d = $first ? int( $d / $damp ) : int( $d / 2 ); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 237 | 10 | $d += int( $d / $num ); | |||||||||||||||||||||||||||||||||||
| 238 | 10 | my $k = 0; | |||||||||||||||||||||||||||||||||||
| 239 | 10 | ▶ | while ( $d > ( ( $base - $tmin ) * $tmax ) / 2 ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 240 | 2 | $d = int( $d / ( $base - $tmin ) ); | |||||||||||||||||||||||||||||||||||
| 241 | 2 | $k += $base; | |||||||||||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||||||||||
| 243 | 10 | return $k + int( ( ( $base - $tmin + 1 ) * $d ) / ( $d + $skew ) ); | |||||||||||||||||||||||||||||||||||
| 244 | 8 | }; | |||||||||||||||||||||||||||||||||||
| 245 | 8 | ▶ | while ( $h < @cp ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 246 | 10 | my ($m) = sort { $a <=> $b } grep { $_ >= $n } @cp; | |||||||||||||||||||||||||||||||||||
| 247 | 10 | $delta += ( $m - $n ) * ( $h + 1 ); | |||||||||||||||||||||||||||||||||||
| 248 | 10 | $n = $m; | |||||||||||||||||||||||||||||||||||
| 249 | 10 | for my $c (@cp) { | |||||||||||||||||||||||||||||||||||
| 250 | 42 | ▶ | $delta++ if $c < $n; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 251 | 42 | ▶ | next unless $c == $n; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 252 | 10 | my $q = $delta; | |||||||||||||||||||||||||||||||||||
| 253 | 10 | for ( my $k = $base ; ; $k += $base ) { | |||||||||||||||||||||||||||||||||||
| 254 | 30 | ▶ | my $t = $k <= $bias ? $tmin : $k >= $bias + $tmax ? $tmax : $k - $bias; | ||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||
| 255 | 30 | ▶ | last if $q < $t; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 256 | 20 | $output .= $digit->( $t + ( $q - $t ) % ( $base - $t ) ); | |||||||||||||||||||||||||||||||||||
| 257 | 20 | $q = int( ( $q - $t ) / ( $base - $t ) ); | |||||||||||||||||||||||||||||||||||
| 258 | } | ||||||||||||||||||||||||||||||||||||
| 259 | 10 | $output .= $digit->($q); | |||||||||||||||||||||||||||||||||||
| 260 | 10 | $bias = $adapt->( $delta, $h + 1, $h == $basic ); | |||||||||||||||||||||||||||||||||||
| 261 | 10 | $delta = 0; | |||||||||||||||||||||||||||||||||||
| 262 | 10 | $h++; | |||||||||||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||||||||||
| 264 | 10 | $delta++; | |||||||||||||||||||||||||||||||||||
| 265 | 10 | $n++; | |||||||||||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||||||||||
| 267 | 8 | return $output; | |||||||||||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||
| 270 | 39 | 1; | |||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||
| 272 | __END__ | ||||||||||||||||||||||||||||||||||||