Skip to content

Commit 3312f87

Browse files
author
pp-qq
committed
fix(url): canonicalize_url('http://您好.中国:80/') failed
expect: 'http://xn--5usr0o.xn--fiqs8s:80/' actual: 'http://xn--5usr0o.xn--:80-u68dy61b/'
1 parent 34435d0 commit 3312f87

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

w3lib/url.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,15 @@ def _safe_ParseResult(parts, encoding='utf8', path_encoding='utf8'):
374374
# IDNA encoding can fail for too long labels (>63 characters)
375375
# or missing labels (e.g. http://.example.com)
376376
try:
377-
netloc = parts.netloc.encode('idna')
377+
idx = parts.netloc.rfind(u':')
378+
if idx != -1:
379+
hostname = parts.netloc[:idx]
380+
portpart = parts.netloc[idx:]
381+
else:
382+
hostname = parts.netloc
383+
portpart = u''
384+
hostname = to_unicode(hostname.encode('idna'))
385+
netloc = hostname + portpart
378386
except UnicodeError:
379387
netloc = parts.netloc
380388

0 commit comments

Comments
 (0)