3 # This module implements the RFCs 3490 (IDNA) and 3491 (Nameprep)
4 # based on Python-2.6.4/Lib/encodings/idna.py
7 use URI::_punycode qw(encode_punycode decode_punycode);
10 my $ASCII = qr/^[\x00-\x7F]*\z/;
14 my @labels = split(/\./, $idomain, -1);
16 push(@last_empty, pop @labels) if @labels > 1 && $labels[-1] eq "";
20 return join(".", @labels, @last_empty);
25 return join(".", map ToUnicode($_), split(/\./, $domain, -1))
28 sub nameprep { # XXX real implementation missing
36 croak "Label empty" if $label eq "";
37 croak "Label too long" if length($label) > 63;
43 return check_size($label) if $label =~ $ASCII;
46 $label = nameprep($label);
47 # Step 3: UseSTD3ASCIIRules is false
48 # Step 4: try ASCII again
49 return check_size($label) if $label =~ $ASCII;
51 # Step 5: Check ACE prefix
52 if ($label =~ /^xn--/) {
53 croak "Label starts with ACE prefix";
56 # Step 6: Encode with PUNYCODE
57 $label = encode_punycode($label);
59 # Step 7: Prepend ACE prefix
60 $label = "xn--$label";
63 return check_size($label);
68 $label = nameprep($label) unless $label =~ $ASCII;
69 return $label unless $label =~ /^xn--/;
70 my $label1 = decode_punycode(substr($label, 4));
71 my $label2 = ToASCII($label);
72 if (lc($label) ne $label2) {
73 croak "IDNA does not round-trip: '$label' vs '$label2'";