blob: 55e1643c25daf124f94717c1c4bb1bd909b0a377 [file] [log] [blame]
Martin v. Löwis2548c732003-04-18 10:39:54 +00001# This module implements the RFCs 3490 (IDNA) and 3491 (Nameprep)
2
Martin v. Löwis480f1bb2006-03-09 23:38:20 +00003import stringprep, re, codecs
Martin v. Löwis5bd7c022006-03-10 11:20:04 +00004from unicodedata import ucd_3_2_0 as unicodedata
Martin v. Löwis2548c732003-04-18 10:39:54 +00005
6# IDNA section 3.1
Guido van Rossumef87d6e2007-05-02 19:09:54 +00007dots = re.compile("[\u002E\u3002\uFF0E\uFF61]")
Martin v. Löwis2548c732003-04-18 10:39:54 +00008
9# IDNA section 5
Walter Dörwald0ac30f82007-05-11 10:32:57 +000010ace_prefix = b"xn--"
11sace_prefix = "xn--"
Martin v. Löwis2548c732003-04-18 10:39:54 +000012
13# This assumes query strings, so AllowUnassigned is true
14def nameprep(label):
15 # Map
16 newlabel = []
17 for c in label:
18 if stringprep.in_table_b1(c):
19 # Map to nothing
20 continue
21 newlabel.append(stringprep.map_table_b2(c))
Guido van Rossumef87d6e2007-05-02 19:09:54 +000022 label = "".join(newlabel)
Tim Peters0eadaac2003-04-24 16:02:54 +000023
Martin v. Löwis2548c732003-04-18 10:39:54 +000024 # Normalize
25 label = unicodedata.normalize("NFKC", label)
Tim Peters0eadaac2003-04-24 16:02:54 +000026
Martin v. Löwis2548c732003-04-18 10:39:54 +000027 # Prohibit
28 for c in label:
29 if stringprep.in_table_c12(c) or \
30 stringprep.in_table_c22(c) or \
31 stringprep.in_table_c3(c) or \
32 stringprep.in_table_c4(c) or \
33 stringprep.in_table_c5(c) or \
34 stringprep.in_table_c6(c) or \
35 stringprep.in_table_c7(c) or \
36 stringprep.in_table_c8(c) or \
37 stringprep.in_table_c9(c):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000038 raise UnicodeError("Invalid character %r" % c)
Martin v. Löwis2548c732003-04-18 10:39:54 +000039
40 # Check bidi
41 RandAL = map(stringprep.in_table_d1, label)
42 for c in RandAL:
43 if c:
44 # There is a RandAL char in the string. Must perform further
45 # tests:
46 # 1) The characters in section 5.8 MUST be prohibited.
47 # This is table C.8, which was already checked
48 # 2) If a string contains any RandALCat character, the string
49 # MUST NOT contain any LCat character.
50 if filter(stringprep.in_table_d2, label):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000051 raise UnicodeError("Violation of BIDI requirement 2")
Martin v. Löwis2548c732003-04-18 10:39:54 +000052
53 # 3) If a string contains any RandALCat character, a
54 # RandALCat character MUST be the first character of the
55 # string, and a RandALCat character MUST be the last
56 # character of the string.
57 if not RandAL[0] or not RandAL[-1]:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000058 raise UnicodeError("Violation of BIDI requirement 3")
Martin v. Löwis2548c732003-04-18 10:39:54 +000059
60 return label
61
62def ToASCII(label):
63 try:
64 # Step 1: try ASCII
65 label = label.encode("ascii")
66 except UnicodeError:
67 pass
68 else:
69 # Skip to step 3: UseSTD3ASCIIRules is false, so
70 # Skip to step 8.
71 if 0 < len(label) < 64:
72 return label
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000073 raise UnicodeError("label empty or too long")
Martin v. Löwis2548c732003-04-18 10:39:54 +000074
75 # Step 2: nameprep
76 label = nameprep(label)
77
78 # Step 3: UseSTD3ASCIIRules is false
79 # Step 4: try ASCII
80 try:
81 label = label.encode("ascii")
82 except UnicodeError:
83 pass
84 else:
85 # Skip to step 8.
86 if 0 < len(label) < 64:
87 return label
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000088 raise UnicodeError("label empty or too long")
Martin v. Löwis2548c732003-04-18 10:39:54 +000089
90 # Step 5: Check ACE prefix
Walter Dörwald0ac30f82007-05-11 10:32:57 +000091 if label.startswith(sace_prefix):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000092 raise UnicodeError("Label starts with ACE prefix")
Martin v. Löwis2548c732003-04-18 10:39:54 +000093
94 # Step 6: Encode with PUNYCODE
95 label = label.encode("punycode")
96
97 # Step 7: Prepend ACE prefix
98 label = ace_prefix + label
99
100 # Step 8: Check size
101 if 0 < len(label) < 64:
102 return label
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000103 raise UnicodeError("label empty or too long")
Martin v. Löwis2548c732003-04-18 10:39:54 +0000104
105def ToUnicode(label):
106 # Step 1: Check for ASCII
Guido van Rossum0e02abb2007-05-09 23:40:37 +0000107 if isinstance(label, bytes):
Martin v. Löwis2548c732003-04-18 10:39:54 +0000108 pure_ascii = True
109 else:
110 try:
111 label = label.encode("ascii")
112 pure_ascii = True
113 except UnicodeError:
114 pure_ascii = False
115 if not pure_ascii:
116 # Step 2: Perform nameprep
117 label = nameprep(label)
118 # It doesn't say this, but apparently, it should be ASCII now
119 try:
120 label = label.encode("ascii")
121 except UnicodeError:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000122 raise UnicodeError("Invalid character in IDN label")
Martin v. Löwis2548c732003-04-18 10:39:54 +0000123 # Step 3: Check for ACE prefix
124 if not label.startswith(ace_prefix):
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000125 return str(label, "ascii")
Martin v. Löwis2548c732003-04-18 10:39:54 +0000126
127 # Step 4: Remove ACE prefix
128 label1 = label[len(ace_prefix):]
129
130 # Step 5: Decode using PUNYCODE
131 result = label1.decode("punycode")
132
133 # Step 6: Apply ToASCII
134 label2 = ToASCII(result)
135
136 # Step 7: Compare the result of step 6 with the one of step 3
137 # label2 will already be in lower case.
Walter Dörwald0ac30f82007-05-11 10:32:57 +0000138 if str(label, "ascii").lower() != str(label2, "ascii"):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000139 raise UnicodeError("IDNA does not round-trip", label, label2)
Martin v. Löwis2548c732003-04-18 10:39:54 +0000140
141 # Step 8: return the result of step 5
142 return result
Tim Peters0eadaac2003-04-24 16:02:54 +0000143
Martin v. Löwis2548c732003-04-18 10:39:54 +0000144### Codec APIs
145
146class Codec(codecs.Codec):
Walter Dörwald0ac30f82007-05-11 10:32:57 +0000147 def encode(self, input, errors='strict'):
Martin v. Löwis2548c732003-04-18 10:39:54 +0000148
149 if errors != 'strict':
150 # IDNA is quite clear that implementations must be strict
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000151 raise UnicodeError("unsupported error handling "+errors)
Martin v. Löwis2548c732003-04-18 10:39:54 +0000152
Martin v. Löwis8b595142005-08-25 11:03:38 +0000153 if not input:
Guido van Rossum0e02abb2007-05-09 23:40:37 +0000154 return b"", 0
Martin v. Löwis8b595142005-08-25 11:03:38 +0000155
Walter Dörwald0ac30f82007-05-11 10:32:57 +0000156 result = b""
Martin v. Löwis0d8e16c2003-08-05 06:19:47 +0000157 labels = dots.split(input)
Walter Dörwald0ac30f82007-05-11 10:32:57 +0000158 if labels and not labels[-1]:
Guido van Rossum0e02abb2007-05-09 23:40:37 +0000159 trailing_dot = b'.'
Martin v. Löwis0d8e16c2003-08-05 06:19:47 +0000160 del labels[-1]
161 else:
Guido van Rossum0e02abb2007-05-09 23:40:37 +0000162 trailing_dot = b''
Martin v. Löwis0d8e16c2003-08-05 06:19:47 +0000163 for label in labels:
Walter Dörwald0ac30f82007-05-11 10:32:57 +0000164 if result:
165 # Join with U+002E
166 result.extend(b'.')
167 result.extend(ToASCII(label))
168 return result+trailing_dot, len(input)
Martin v. Löwis2548c732003-04-18 10:39:54 +0000169
Walter Dörwald0ac30f82007-05-11 10:32:57 +0000170 def decode(self, input, errors='strict'):
Tim Peters0eadaac2003-04-24 16:02:54 +0000171
Martin v. Löwis2548c732003-04-18 10:39:54 +0000172 if errors != 'strict':
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000173 raise UnicodeError("Unsupported error handling "+errors)
Martin v. Löwis2548c732003-04-18 10:39:54 +0000174
Martin v. Löwis8b595142005-08-25 11:03:38 +0000175 if not input:
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000176 return "", 0
Martin v. Löwis8b595142005-08-25 11:03:38 +0000177
Martin v. Löwis2548c732003-04-18 10:39:54 +0000178 # IDNA allows decoding to operate on Unicode strings, too.
Guido van Rossum0e02abb2007-05-09 23:40:37 +0000179 if isinstance(input, bytes):
Martin v. Löwis2548c732003-04-18 10:39:54 +0000180 labels = dots.split(input)
181 else:
Guido van Rossum0e02abb2007-05-09 23:40:37 +0000182 # Force to bytes
183 input = bytes(input)
184 labels = input.split(b".")
Martin v. Löwis2548c732003-04-18 10:39:54 +0000185
Martin v. Löwis0d8e16c2003-08-05 06:19:47 +0000186 if labels and len(labels[-1]) == 0:
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000187 trailing_dot = '.'
Martin v. Löwis0d8e16c2003-08-05 06:19:47 +0000188 del labels[-1]
189 else:
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000190 trailing_dot = ''
Martin v. Löwis0d8e16c2003-08-05 06:19:47 +0000191
Martin v. Löwis2548c732003-04-18 10:39:54 +0000192 result = []
193 for label in labels:
194 result.append(ToUnicode(label))
195
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000196 return ".".join(result)+trailing_dot, len(input)
Martin v. Löwis2548c732003-04-18 10:39:54 +0000197
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000198class IncrementalEncoder(codecs.BufferedIncrementalEncoder):
199 def _buffer_encode(self, input, errors, final):
200 if errors != 'strict':
201 # IDNA is quite clear that implementations must be strict
202 raise UnicodeError("unsupported error handling "+errors)
Thomas Woutersa9773292006-04-21 09:43:23 +0000203
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000204 if not input:
Walter Dörwald0ac30f82007-05-11 10:32:57 +0000205 return (b'', 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000206
207 labels = dots.split(input)
Walter Dörwald0ac30f82007-05-11 10:32:57 +0000208 trailing_dot = b''
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000209 if labels:
210 if not labels[-1]:
Walter Dörwald0ac30f82007-05-11 10:32:57 +0000211 trailing_dot = b'.'
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000212 del labels[-1]
213 elif not final:
214 # Keep potentially unfinished label until the next call
215 del labels[-1]
216 if labels:
Walter Dörwald0ac30f82007-05-11 10:32:57 +0000217 trailing_dot = b'.'
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000218
Walter Dörwald0ac30f82007-05-11 10:32:57 +0000219 result = b""
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000220 size = 0
221 for label in labels:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000222 if size:
Walter Dörwald0ac30f82007-05-11 10:32:57 +0000223 # Join with U+002E
224 result.extend(b'.')
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000225 size += 1
Walter Dörwald0ac30f82007-05-11 10:32:57 +0000226 result.extend(ToASCII(label))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000227 size += len(label)
228
Walter Dörwald0ac30f82007-05-11 10:32:57 +0000229 result += trailing_dot
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000230 size += len(trailing_dot)
231 return (result, size)
232
233class IncrementalDecoder(codecs.BufferedIncrementalDecoder):
234 def _buffer_decode(self, input, errors, final):
235 if errors != 'strict':
236 raise UnicodeError("Unsupported error handling "+errors)
237
238 if not input:
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000239 return ("", 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000240
241 # IDNA allows decoding to operate on Unicode strings, too.
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000242 if isinstance(input, str):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000243 labels = dots.split(input)
244 else:
245 # Must be ASCII string
Walter Dörwald0ac30f82007-05-11 10:32:57 +0000246 input = str(input, "ascii")
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000247 labels = input.split(".")
248
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000249 trailing_dot = ''
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000250 if labels:
251 if not labels[-1]:
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000252 trailing_dot = '.'
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000253 del labels[-1]
254 elif not final:
255 # Keep potentially unfinished label until the next call
256 del labels[-1]
257 if labels:
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000258 trailing_dot = '.'
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000259
260 result = []
261 size = 0
262 for label in labels:
263 result.append(ToUnicode(label))
264 if size:
265 size += 1
266 size += len(label)
267
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000268 result = ".".join(result) + trailing_dot
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000269 size += len(trailing_dot)
270 return (result, size)
Thomas Woutersa9773292006-04-21 09:43:23 +0000271
Martin v. Löwis2548c732003-04-18 10:39:54 +0000272class StreamWriter(Codec,codecs.StreamWriter):
273 pass
274
275class StreamReader(Codec,codecs.StreamReader):
276 pass
277
278### encodings module API
279
280def getregentry():
Thomas Woutersa9773292006-04-21 09:43:23 +0000281 return codecs.CodecInfo(
282 name='idna',
283 encode=Codec().encode,
284 decode=Codec().decode,
285 incrementalencoder=IncrementalEncoder,
286 incrementaldecoder=IncrementalDecoder,
287 streamwriter=StreamWriter,
288 streamreader=StreamReader,
289 )