URI/_idna.pm

filter: Statementstmt 98.0% 51 / 52 Branchbran 75.0% 15 / 20 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 12 / 12 Podpod 0.0% 0 / 6 totaltotal 87.3% 83 / 95 CC 13 SCAR 10.5
CC
13
Cov
92%
ToASCII
4.1
ToUnicode
4.0
encode
4.0
CRAP 2.9
« URI/http.pm ↑ Summary URI/smb.pm »
1 package URI::_idna;
2  
3 # This module implements the RFCs 3490 (IDNA) and 3491 (Nameprep)
4 # based on Python-2.6.4/Lib/encodings/idna.py
5  
6 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 ▶use URI::_punycode qw(decode_punycode encode_punycode);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Unicode::Normalize qw(NFC);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 our $VERSION = '5.37';
14  
15 3 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.6
16 *URI::_idna::_ENV_::JOIN_LEAKS_UTF8_FLAGS
17 3 ▶ = "$]" < 5.008_003 ? sub () {1} : sub () {0};
Branch: 1 branch
BranchTrueFalseTotal
"$]" < 5.008003 ? : 0 3 3
18 }
19  
20 3 my $ASCII = qr/^[\x00-\x7F]*\z/;
21  
22 11 ▶sub encode {
Subroutine encode: called CC=4 SCAR=13.9
Pod undocumented
23 11 my $idomain = shift;
24 11 my @labels = split(/\./, $idomain, -1);
25 11 my @last_empty;
26 11 ▶ push(@last_empty, pop @labels) if @labels > 1 && $labels[-1] eq "";
Branch: 1 branch
BranchTrueFalseTotal
if @labels > 1 and $labels[-1] eq "" 1 10 11
Condition100%3/3
@labels > 1 and $labels[-1] eq ""
!ll&&!rl&&r
461
MC/DC100%2/2
@labels > 1 and $labels[-1] eq ""
@labels > 1 $labels[-1] eq ""
Truth table100%3/3
A and B
A= @labels > 1B= $labels[-1] eq ""
ABresult
0X0
100
111
27 11 for (@labels) {
28 21 $_ = ToASCII($_);
29 }
30  
31 10 return eval 'join(".", @labels, @last_empty)'
32 if URI::_idna::_ENV_::JOIN_LEAKS_UTF8_FLAGS;
33 10 return join(".", @labels, @last_empty);
34 }
35  
36 14 ▶sub decode {
Subroutine decode: called CC=1 SCAR=0.0
Pod undocumented
37 14 my $domain = shift;
38 14 return join(".", map ToUnicode($_), split(/\./, $domain, -1));
39 }
40  
41 21 ▶sub nameprep {
Subroutine nameprep: called CC=1 SCAR=0.0
Pod undocumented
42 21 my $label = shift;
43  
44 # Lowercase, then normalize. Without normalization a non-NFC label encodes
45 # to a non-standard A-label that other IDNA implementations reject, so a
46 # host used for a security check can differ from the host actually fetched.
47 # We normalize with NFC (canonical composition): RFC 3491 (IDNA2003)
48 # nominally called for NFKC, but RFC 5891 (IDNA2008) replaced that with NFC,
49 # which is also what UTS #46 applies in browsers and other clients. The
50 # prohibited-character and bidi tables that a full nameprep would enforce
51 # remain unimplemented. A host passed as bytes rather than decoded
52 # characters is treated as Latin-1 per URI's documented contract (see the
53 # host/ihost examples in URI.pm), so pass decoded characters for UTF-8.
54 21 return NFC(lc $label);
55 }
56  
57 33 ▶sub check_size {
Subroutine check_size: called CC=3 SCAR=11.0
Pod undocumented
58 33 my $label = shift;
59 33 ▶ croak "Label empty" if $label eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $label eq "" 0 33 33
60 33 ▶ croak "Label too long" if length($label) > 63;
Branch: 1 branch
BranchTrueFalseTotal
if length $label > 63 1 32 33
61 32 return $label;
62 }
63  
64 33 ▶sub ToASCII {
Subroutine ToASCII: called CC=4 SCAR=14.2
Pod undocumented
65 33 my $label = shift;
66 33 ▶ return check_size($label) if $label =~ $ASCII;
Branch: 1 branch
BranchTrueFalseTotal
if $label =~ /$ASCII/ 12 21 33
67  
68 # Step 2: nameprep
69 21 $label = nameprep($label);
70  
71 # Step 3: UseSTD3ASCIIRules is false
72 # Step 4: try ASCII again
73 21 ▶ return check_size($label) if $label =~ $ASCII;
Branch: 1 branch
BranchTrueFalseTotal
if $label =~ /$ASCII/ 0 21 21
74  
75 # Step 5: Check ACE prefix
76 21 ▶ if ($label =~ /^xn--/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($label =~ /^xn--/) 0 21 21
77 0 croak "Label starts with ACE prefix";
78 }
79  
80 # Step 6: Encode with PUNYCODE
81 21 $label = encode_punycode($label);
82  
83 # Step 7: Prepend ACE prefix
84 21 $label = "xn--$label";
85  
86 # Step 8: Check size
87 21 return check_size($label);
88 }
89  
90 31 ▶sub ToUnicode {
Subroutine ToUnicode: called CC=4 SCAR=13.9
Pod undocumented
91 31 my $label = shift;
92 31 ▶ $label = nameprep($label) unless $label =~ $ASCII;
Branch: 1 branch
BranchTrueFalseTotal
unless $label =~ /$ASCII/ 0 31 31
93 31 ▶ return $label unless $label =~ /^xn--/;
Branch: 1 branch
BranchTrueFalseTotal
unless $label =~ /^xn--/ 19 12 31
94 12 my $result = decode_punycode(substr($label, 4));
95 12 my $label2 = ToASCII($result);
96 12 ▶ if (lc($label) ne $label2) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $label ne $label2) 1 11 12
97 1 croak "IDNA does not round-trip: '\L$label\E' vs '$label2'";
98 }
99 11 return $result;
100 }
101  
102 3 1;
« URI/http.pm ↑ Summary URI/smb.pm »