URI/_idna.pm

filter: Statementstmt 98.1% 53 / 54 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.6% 85 / 97 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.40';
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 12 ▶sub encode {
Subroutine encode: called CC=4 SCAR=13.9
Pod undocumented
23 12 my $idomain = shift;
24 12 my @labels = split(/\./, $idomain, -1);
25 12 my @last_empty;
26 12 ▶ push(@last_empty, pop @labels) if @labels > 1 && $labels[-1] eq "";
Branch: 1 branch
BranchTrueFalseTotal
if @labels > 1 and $labels[-1] eq "" 1 11 12
Condition100%3/3
@labels > 1 and $labels[-1] eq ""
!ll&&!rl&&r
561
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 12 for (@labels) {
28 22 $_ = 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 15 ▶sub decode {
Subroutine decode: called CC=1 SCAR=0.0
Pod undocumented
37 15 my $domain = shift;
38 15 return join(".", map ToUnicode($_), split(/\./, $domain, -1));
39 }
40  
41 22 ▶sub nameprep {
Subroutine nameprep: called CC=1 SCAR=0.0
Pod undocumented
42 22 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 22 return NFC(lc $label);
55 }
56  
57 67 ▶sub check_size {
Subroutine check_size: called CC=3 SCAR=11.0
Pod undocumented
58 67 my $label = shift;
59 67 ▶ croak "Label empty" if $label eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $label eq "" 0 67 67
60 67 ▶ croak "Label too long" if length($label) > 63;
Branch: 1 branch
BranchTrueFalseTotal
if length $label > 63 3 64 67
61 64 return $label;
62 }
63  
64 34 ▶sub ToASCII {
Subroutine ToASCII: called CC=4 SCAR=14.1
Pod undocumented
65 34 my $label = shift;
66 34 ▶ return check_size($label) if $label =~ $ASCII;
Branch: 1 branch
BranchTrueFalseTotal
if $label =~ /$ASCII/ 12 22 34
67  
68 # Step 2: nameprep
69 22 $label = nameprep($label);
70  
71 # Step 3: UseSTD3ASCIIRules is false
72 # Step 4: try ASCII again
73 22 ▶ return check_size($label) if $label =~ $ASCII;
Branch: 1 branch
BranchTrueFalseTotal
if $label =~ /$ASCII/ 0 22 22
74  
75 # Step 5: Check ACE prefix
76 22 ▶ if ($label =~ /^xn--/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($label =~ /^xn--/) 0 22 22
77 0 croak "Label starts with ACE prefix";
78 }
79  
80 # Punycode output is never shorter than its input, so reject over-long
81 # labels now rather than after a quadratic encode (GH#207).
82 22 check_size($label);
83  
84 # Step 6: Encode with PUNYCODE
85 20 $label = encode_punycode($label);
86  
87 # Step 7: Prepend ACE prefix
88 20 $label = "xn--$label";
89  
90 # Step 8: Check size
91 20 return check_size($label);
92 }
93  
94 32 ▶sub ToUnicode {
Subroutine ToUnicode: called CC=4 SCAR=13.9
Pod undocumented
95 32 my $label = shift;
96 32 ▶ $label = nameprep($label) unless $label =~ $ASCII;
Branch: 1 branch
BranchTrueFalseTotal
unless $label =~ /$ASCII/ 0 32 32
97 32 ▶ return $label unless $label =~ /^xn--/;
Branch: 1 branch
BranchTrueFalseTotal
unless $label =~ /^xn--/ 19 13 32
98  
99 # Too long to round-trip, so don't spend time decoding it (GH#207).
100 13 check_size($label);
101 12 my $result = decode_punycode(substr($label, 4));
102 12 my $label2 = ToASCII($result);
103 12 ▶ if (lc($label) ne $label2) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $label ne $label2) 1 11 12
104 1 croak "IDNA does not round-trip: '\L$label\E' vs '$label2'";
105 }
106 11 return $result;
107 }
108  
109 3 1;
« URI/http.pm ↑ Summary URI/smb.pm »