| 1 |
|
| 2 |
|
| 3 |
import stringprep, re, codecs |
| 4 |
from unicodedata import ucd_3_2_0 as unicodedata |
| 5 |
|
| 6 |
|
| 7 |
dots = re.compile("[\u002E\u3002\uFF0E\uFF61]") |
| 8 |
|
| 9 |
|
| 10 |
ace_prefix = b"xn--" |
| 11 |
sace_prefix = "xn--" |
| 12 |
|
| 13 |
|
| 14 |
def nameprep(label): |
| 15 |
|
| 16 |
newlabel = [] |
| 17 |
for c in label: |
| 18 |
if stringprep.in_table_b1(c): |
| 19 |
|
| 20 |
continue |
| 21 |
newlabel.append(stringprep.map_table_b2(c)) |
| 22 |
label = "".join(newlabel) |
| 23 |
|
| 24 |
|
| 25 |
label = unicodedata.normalize("NFKC", label) |
| 26 |
|
| 27 |
|
| 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): |
| 38 |
raise UnicodeError("Invalid character %r" % c) |
| 39 |
|
| 40 |
|
| 41 |
RandAL = [stringprep.in_table_d1(x) for x in label] |
| 42 |
if any(RandAL): |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
if any(stringprep.in_table_d2(x) for x in label): |
| 50 |
raise UnicodeError("Violation of BIDI requirement 2") |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
if not RandAL[0] or not RandAL[-1]: |
| 56 |
raise UnicodeError("Violation of BIDI requirement 3") |
| 57 |
|
| 58 |
return label |
| 59 |
|
| 60 |
def ToASCII(label): |
| 61 |
try: |
| 62 |
|
| 63 |
label = label.encode("ascii") |
| 64 |
except UnicodeError: |
| 65 |
pass |
| 66 |
else: |
| 67 |
|
| 68 |
|
| 69 |
if 0 < len(label) < 64: |
| 70 |
return label |
| 71 |
raise UnicodeError("label empty or too long") |
| 72 |
|
| 73 |
|
| 74 |
label = nameprep(label) |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
try: |
| 79 |
label = label.encode("ascii") |
| 80 |
except UnicodeError: |
| 81 |
pass |
| 82 |
else: |
| 83 |
|
| 84 |
if 0 < len(label) < 64: |
| 85 |
return label |
| 86 |
raise UnicodeError("label empty or too long") |
| 87 |
|
| 88 |
|
| 89 |
if label.startswith(sace_prefix): |
| 90 |
raise UnicodeError("Label starts with ACE prefix") |
| 91 |
|
| 92 |
|
| 93 |
label = label.encode("punycode") |
| 94 |
|
| 95 |
|
| 96 |
label = ace_prefix + label |
| 97 |
|
| 98 |
|
| 99 |
if 0 < len(label) < 64: |
| 100 |
return label |
| 101 |
raise UnicodeError("label empty or too long") |
| 102 |
|
| 103 |
def ToUnicode(label): |
| 104 |
|
| 105 |
if isinstance(label, bytes): |
| 106 |
pure_ascii = True |
| 107 |
else: |
| 108 |
try: |
| 109 |
label = label.encode("ascii") |
| 110 |
pure_ascii = True |
| 111 |
except UnicodeError: |
| 112 |
pure_ascii = False |
| 113 |
if not pure_ascii: |
| 114 |
|
| 115 |
label = nameprep(label) |
| 116 |
|
| 117 |
try: |
| 118 |
label = label.encode("ascii") |
| 119 |
except UnicodeError: |
| 120 |
raise UnicodeError("Invalid character in IDN label") |
| 121 |
|
| 122 |
if not label.startswith(ace_prefix): |
| 123 |
return str(label, "ascii") |
| 124 |
|
| 125 |
|
| 126 |
label1 = label[len(ace_prefix):] |
| 127 |
|
| 128 |
|
| 129 |
result = label1.decode("punycode") |
| 130 |
|
| 131 |
|
| 132 |
label2 = ToASCII(result) |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
if str(label, "ascii").lower() != str(label2, "ascii"): |
| 137 |
raise UnicodeError("IDNA does not round-trip", label, label2) |
| 138 |
|
| 139 |
|
| 140 |
return result |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
class Codec(codecs.Codec): |
| 145 |
def encode(self, input, errors='strict'): |
| 146 |
|
| 147 |
if errors != 'strict': |
| 148 |
|
| 149 |
raise UnicodeError("unsupported error handling "+errors) |
| 150 |
|
| 151 |
if not input: |
| 152 |
return b'', 0 |
| 153 |
|
| 154 |
try: |
| 155 |
result = input.encode('ascii') |
| 156 |
except UnicodeEncodeError: |
| 157 |
pass |
| 158 |
else: |
| 159 |
|
| 160 |
labels = result.split(b'.') |
| 161 |
for label in labels[:-1]: |
| 162 |
if not (0 < len(label) < 64): |
| 163 |
raise UnicodeError("label empty or too long") |
| 164 |
if len(labels[-1]) >= 64: |
| 165 |
raise UnicodeError("label too long") |
| 166 |
return result, len(input) |
| 167 |
|
| 168 |
result = bytearray() |
| 169 |
labels = dots.split(input) |
| 170 |
if labels and not labels[-1]: |
| 171 |
trailing_dot = b'.' |
| 172 |
del labels[-1] |
| 173 |
else: |
| 174 |
trailing_dot = b'' |
| 175 |
for label in labels: |
| 176 |
if result: |
| 177 |
|
| 178 |
result.extend(b'.') |
| 179 |
result.extend(ToASCII(label)) |
| 180 |
return bytes(result+trailing_dot), len(input) |
| 181 |
|
| 182 |
def decode(self, input, errors='strict'): |
| 183 |
|
| 184 |
if errors != 'strict': |
| 185 |
raise UnicodeError("Unsupported error handling "+errors) |
| 186 |
|
| 187 |
if not input: |
| 188 |
return "", 0 |
| 189 |
|
| 190 |
|
| 191 |
if not isinstance(input, bytes): |
| 192 |
|
| 193 |
input = bytes(input) |
| 194 |
|
| 195 |
if ace_prefix not in input: |
| 196 |
|
| 197 |
try: |
| 198 |
return input.decode('ascii'), len(input) |
| 199 |
except UnicodeDecodeError: |
| 200 |
pass |
| 201 |
|
| 202 |
labels = input.split(b".") |
| 203 |
|
| 204 |
if labels and len(labels[-1]) == 0: |
| 205 |
trailing_dot = '.' |
| 206 |
del labels[-1] |
| 207 |
else: |
| 208 |
trailing_dot = '' |
| 209 |
|
| 210 |
result = [] |
| 211 |
for label in labels: |
| 212 |
result.append(ToUnicode(label)) |
| 213 |
|
| 214 |
return ".".join(result)+trailing_dot, len(input) |
| 215 |
|
| 216 |
class IncrementalEncoder(codecs.BufferedIncrementalEncoder): |
| 217 |
def _buffer_encode(self, input, errors, final): |
| 218 |
if errors != 'strict': |
| 219 |
|
| 220 |
raise UnicodeError("unsupported error handling "+errors) |
| 221 |
|
| 222 |
if not input: |
| 223 |
return (b'', 0) |
| 224 |
|
| 225 |
labels = dots.split(input) |
| 226 |
trailing_dot = b'' |
| 227 |
if labels: |
| 228 |
if not labels[-1]: |
| 229 |
trailing_dot = b'.' |
| 230 |
del labels[-1] |
| 231 |
elif not final: |
| 232 |
|
| 233 |
del labels[-1] |
| 234 |
if labels: |
| 235 |
trailing_dot = b'.' |
| 236 |
|
| 237 |
result = bytearray() |
| 238 |
size = 0 |
| 239 |
for label in labels: |
| 240 |
if size: |
| 241 |
|
| 242 |
result.extend(b'.') |
| 243 |
size += 1 |
| 244 |
result.extend(ToASCII(label)) |
| 245 |
size += len(label) |
| 246 |
|
| 247 |
result += trailing_dot |
| 248 |
size += len(trailing_dot) |
| 249 |
return (bytes(result), size) |
| 250 |
|
| 251 |
class IncrementalDecoder(codecs.BufferedIncrementalDecoder): |
| 252 |
def _buffer_decode(self, input, errors, final): |
| 253 |
if errors != 'strict': |
| 254 |
raise UnicodeError("Unsupported error handling "+errors) |
| 255 |
|
| 256 |
if not input: |
| 257 |
return ("", 0) |
| 258 |
|
| 259 |
|
| 260 |
if isinstance(input, str): |
| 261 |
labels = dots.split(input) |
| 262 |
else: |
| 263 |
|
| 264 |
input = str(input, "ascii") |
| 265 |
labels = input.split(".") |
| 266 |
|
| 267 |
trailing_dot = '' |
| 268 |
if labels: |
| 269 |
if not labels[-1]: |
| 270 |
trailing_dot = '.' |
| 271 |
del labels[-1] |
| 272 |
elif not final: |
| 273 |
|
| 274 |
del labels[-1] |
| 275 |
if labels: |
| 276 |
trailing_dot = '.' |
| 277 |
|
| 278 |
result = [] |
| 279 |
size = 0 |
| 280 |
for label in labels: |
| 281 |
result.append(ToUnicode(label)) |
| 282 |
if size: |
| 283 |
size += 1 |
| 284 |
size += len(label) |
| 285 |
|
| 286 |
result = ".".join(result) + trailing_dot |
| 287 |
size += len(trailing_dot) |
| 288 |
return (result, size) |
| 289 |
|
| 290 |
class StreamWriter(Codec,codecs.StreamWriter): |
| 291 |
pass |
| 292 |
|
| 293 |
class StreamReader(Codec,codecs.StreamReader): |
| 294 |
pass |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
def getregentry(): |
| 299 |
return codecs.CodecInfo( |
| 300 |
name='idna', |
| 301 |
encode=Codec().encode, |
| 302 |
decode=Codec().decode, |
| 303 |
incrementalencoder=IncrementalEncoder, |
| 304 |
incrementaldecoder=IncrementalDecoder, |
| 305 |
streamwriter=StreamWriter, |
| 306 |
streamreader=StreamReader, |
| 307 |
) |