| 1 |
package IDNA::Punycode; |
| 2 |
|
| 3 |
use strict; |
| 4 |
use utf8; |
| 5 |
use warnings; |
| 6 |
|
| 7 |
our $VERSION = "1.100"; |
| 8 |
$VERSION = eval $VERSION; |
| 9 |
|
| 10 |
require Exporter; |
| 11 |
our @ISA = qw(Exporter); |
| 12 |
our @EXPORT = qw(encode_punycode decode_punycode idn_prefix); |
| 13 |
|
| 14 |
use Carp; |
| 15 |
use Net::IDN::Punycode(); |
| 16 |
use Net::IDN::Nameprep(); |
| 17 |
# use Net::IDN::Encode(); |
| 18 |
|
| 19 |
our $PREFIX = 'xn--'; |
| 20 |
|
| 21 |
sub idn_prefix { |
| 22 |
$PREFIX = shift; |
| 23 |
} |
| 24 |
|
| 25 |
# These functions are copied from Net::IDN::Encode. This allows us to optimise |
| 26 |
# Net::IDN::Encode for the static prefix (e.g. by using /o), while we continue |
| 27 |
# to support changing the prefix here. |
| 28 |
|
| 29 |
sub _to_ascii { |
| 30 |
use bytes; |
| 31 |
no warnings qw(utf8); # needed for perl v5.6.x |
| 32 |
|
| 33 |
my $label = shift; |
| 34 |
|
| 35 |
if($label =~ m/[^\x00-\x7F]/) { |
| 36 |
$label = Net::IDN::Nameprep::nameprep($label); |
| 37 |
croak 'Invalid label (toASCII, step 5)' if $label =~ m/^$PREFIX/i; |
| 38 |
$label = $PREFIX.(Net::IDN::Punycode::encode_punycode($label)); |
| 39 |
} |
| 40 |
|
| 41 |
croak 'Invalid label length (toASCII, step 8)' if |
| 42 |
length($label) < 1 || |
| 43 |
length($label) > 63; |
| 44 |
|
| 45 |
return $label; |
| 46 |
} |
| 47 |
|
| 48 |
sub _to_unicode { |
| 49 |
use bytes; |
| 50 |
|
| 51 |
my $label = shift; |
| 52 |
my $result = $label; |
| 53 |
|
| 54 |
eval { |
| 55 |
if($label =~ m/[^\x00-\x7F]/) { |
| 56 |
$label = Net::IDN::Nameprep::nameprep($label); |
| 57 |
} |
| 58 |
|
| 59 |
my $save3 = $label; |
| 60 |
croak 'Missing IDNA prefix (ToUnicode, step 3)' unless $label =~ s/^$PREFIX//i; |
| 61 |
$label = Net::IDN::Punycode::decode_punycode($label); |
| 62 |
|
| 63 |
my $save6 = _to_ascii($label); |
| 64 |
croak 'Invalid label (ToUnicode, step 7)' unless uc($save6) eq uc($save3); |
| 65 |
$result = $label; |
| 66 |
}; |
| 67 |
|
| 68 |
return $result; |
| 69 |
} |
| 70 |
|
| 71 |
sub decode_punycode { |
| 72 |
if ($PREFIX) { |
| 73 |
return _to_unicode(shift); |
| 74 |
} else { |
| 75 |
return Net::IDN::Punycode::decode_punycode(shift); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
sub encode_punycode { |
| 80 |
if ($PREFIX) { |
| 81 |
return _to_ascii(shift); |
| 82 |
} else { |
| 83 |
return Net::IDN::Punycode::encode_punycode(shift); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
1; |
| 88 |
__END__ |
| 89 |
|
| 90 |
=head1 NAME |
| 91 |
|
| 92 |
IDNA::Punycode - DEPRECATED module for IDNA and Punyode |
| 93 |
|
| 94 |
=head1 DESCRIPTION |
| 95 |
|
| 96 |
This module is deprecated. |
| 97 |
|
| 98 |
Please use L<Net::IDN::Encode> to handle full domain names and |
| 99 |
L<Net::IDN::Punycode> for raw I<Punycode> encoding. |
| 100 |
|
| 101 |
This module is provided for compatibility with earlier versions of |
| 102 |
C<IDNA::Punycode>. |
| 103 |
|
| 104 |
=head1 FUNCTIONS |
| 105 |
|
| 106 |
The following functions are imported by default. If you also C<use Net::IDN::Punycode>, |
| 107 |
be sure to disable import from this module: |
| 108 |
|
| 109 |
use IDNA::Punycode(); |
| 110 |
|
| 111 |
=over |
| 112 |
|
| 113 |
=item idn_prefix($prefix) |
| 114 |
|
| 115 |
Sets C<$IDNA::Punycode::PREFIX> to C<$prefix>. |
| 116 |
|
| 117 |
B<Do not use this function> in larger applications or environments |
| 118 |
in which multiple application share global variables (such as |
| 119 |
L<mod_perl>). Instead, set the variable locally: |
| 120 |
|
| 121 |
local $IDNA::Punycode::PREFIX = 'yo--'; |
| 122 |
|
| 123 |
=item encode_punycode($input) |
| 124 |
|
| 125 |
If C<$IDNA::Punycode::PREFIX> is C<''>, encodes C<$input> with |
| 126 |
Punycode. |
| 127 |
|
| 128 |
If C<$IDNA::Punycode::PREFIX> is not C<''>, encodes C<$input> with |
| 129 |
Punycode and adds the prefix if C<$input> does contain non-base |
| 130 |
characters (S<i. e.> characters above U+007F). If C<$input> does |
| 131 |
not contain any non-base characters, it is returned as-is. |
| 132 |
|
| 133 |
This function does not do any string preparation as specified by |
| 134 |
I<nameprep> or other I<stringprep> profiles. Use |
| 135 |
L<Net::IDN::Encode> if you just want to convert a domain name. |
| 136 |
|
| 137 |
This function will croak on invalid input. |
| 138 |
|
| 139 |
=item decode_punycode($input) |
| 140 |
|
| 141 |
If C<$IDNA::Punycode::PREFIX> is C<''>, decodes C<$input> with |
| 142 |
Punycode. |
| 143 |
|
| 144 |
If C<$IDNA::Punycode::PREFIX> is not C<''>, checks whether |
| 145 |
C<$input> starts with the prefix. If C<$input> starts with the |
| 146 |
prefix, the prefix is removed and the remainder is decoded with |
| 147 |
Punycode. If C<$input> does not start with the prefix, it is |
| 148 |
returned as-is. |
| 149 |
|
| 150 |
Of course, this function does not do any string preparation as |
| 151 |
specified by I<nameprep> or other I<stringprep> profiles (or some |
| 152 |
sort of de-preparation). |
| 153 |
|
| 154 |
This function will croak on invalid input. |
| 155 |
|
| 156 |
=back |
| 157 |
|
| 158 |
=head1 AUTHORS |
| 159 |
|
| 160 |
Claus FE<auml>rber <[email protected]> |
| 161 |
|
| 162 |
Previous version written by Tatsuhiko Miyagawa E<lt>[email protected]<gt> |
| 163 |
and extended by Robert Urban E<lt>[email protected]<gt>. |
| 164 |
|
| 165 |
=head1 LICENSE |
| 166 |
|
| 167 |
Copyright 2007-2010 Claus FE<auml>rber. |
| 168 |
|
| 169 |
This library is free software; you can redistribute it and/or modify |
| 170 |
it under the same terms as Perl itself. |
| 171 |
|
| 172 |
=head1 SEE ALSO |
| 173 |
|
| 174 |
L<Encode>, L<Net::IDN::Encode>, L<Net::IDN::Punycode>, |
| 175 |
S<RFC 3492> (L<http://www.ietf.org/rfc/rfc3492.txt>) |
| 176 |
|
| 177 |
=cut |