Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e6f0609

Browse files
committed
Issue #17214: Percent-encode non-ASCII bytes in redirect targets
Some servers send Location header fields with non-ASCII bytes, but "http. client" requires the request target to be ASCII-encodable, otherwise a UnicodeEncodeError is raised. Based on patch by Christian Heimes. Python 2 does not suffer any problem because it allows non-ASCII bytes in the HTTP request target.
1 parent ce6e068 commit e6f0609

3 files changed

Lines changed: 52 additions & 1 deletion

File tree

Lib/test/test_urllib2.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,6 +1224,41 @@ def request(conn, method, url, *pos, **kw):
12241224
fp = urllib.request.urlopen("http://python.org/path")
12251225
self.assertEqual(fp.geturl(), "http://python.org/path?query")
12261226

1227+
def test_redirect_encoding(self):
1228+
# Some characters in the redirect target may need special handling,
1229+
# but most ASCII characters should be treated as already encoded
1230+
class Handler(urllib.request.HTTPHandler):
1231+
def http_open(self, req):
1232+
result = self.do_open(self.connection, req)
1233+
self.last_buf = self.connection.buf
1234+
# Set up a normal response for the next request
1235+
self.connection = test_urllib.fakehttp(
1236+
b'HTTP/1.1 200 OK\r\n'
1237+
b'Content-Length: 3\r\n'
1238+
b'\r\n'
1239+
b'123'
1240+
)
1241+
return result
1242+
handler = Handler()
1243+
opener = urllib.request.build_opener(handler)
1244+
tests = (
1245+
(b'/p\xC3\xA5-dansk/', b'/p%C3%A5-dansk/'),
1246+
(b'/spaced%20path/', b'/spaced%20path/'),
1247+
(b'/spaced path/', b'/spaced%20path/'),
1248+
(b'/?p\xC3\xA5-dansk', b'/?p%C3%A5-dansk'),
1249+
)
1250+
for [location, result] in tests:
1251+
with self.subTest(repr(location)):
1252+
handler.connection = test_urllib.fakehttp(
1253+
b'HTTP/1.1 302 Redirect\r\n'
1254+
b'Location: ' + location + b'\r\n'
1255+
b'\r\n'
1256+
)
1257+
response = opener.open('http://example.com/')
1258+
expected = b'GET ' + result + b' '
1259+
request = handler.last_buf
1260+
self.assertTrue(request.startswith(expected), repr(request))
1261+
12271262
def test_proxy(self):
12281263
o = OpenerDirector()
12291264
ph = urllib.request.ProxyHandler(dict(http="proxy.example.com:3128"))

Lib/urllib/request.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
import posixpath
9292
import re
9393
import socket
94+
import string
9495
import sys
9596
import time
9697
import collections
@@ -616,8 +617,12 @@ def redirect_request(self, req, fp, code, msg, headers, newurl):
616617
# from the user (of urllib.request, in this case). In practice,
617618
# essentially all clients do redirect in this case, so we do
618619
# the same.
619-
# be conciliant with URIs containing a space
620+
621+
# Be conciliant with URIs containing a space. This is mainly
622+
# redundant with the more complete encoding done in http_error_302(),
623+
# but it is kept for compatibility with other callers.
620624
newurl = newurl.replace(' ', '%20')
625+
621626
CONTENT_HEADERS = ("content-length", "content-type")
622627
newheaders = dict((k, v) for k, v in req.headers.items()
623628
if k.lower() not in CONTENT_HEADERS)
@@ -657,6 +662,11 @@ def http_error_302(self, req, fp, code, msg, headers):
657662
urlparts[2] = "/"
658663
newurl = urlunparse(urlparts)
659664

665+
# http.client.parse_headers() decodes as ISO-8859-1. Recover the
666+
# original bytes and percent-encode non-ASCII bytes, and any special
667+
# characters such as the space.
668+
newurl = quote(
669+
newurl, encoding="iso-8859-1", safe=string.punctuation)
660670
newurl = urljoin(req.full_url, newurl)
661671

662672
# XXX Probably want to forget about the state of the current

Misc/NEWS

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,12 @@ Library
121121
- Issue #14132: Fix urllib.request redirect handling when the target only has
122122
a query string. Original fix by Ján Janech.
123123

124+
- Issue #17214: The "urllib.request" module now percent-encodes non-ASCII
125+
bytes found in redirect target URLs. Some servers send Location header
126+
fields with non-ASCII bytes, but "http.client" requires the request target
127+
to be ASCII-encodable, otherwise a UnicodeEncodeError is raised. Based on
128+
patch by Christian Heimes.
129+
124130
- Issue #26892: Honor debuglevel flag in urllib.request.HTTPHandler. Patch
125131
contributed by Chi Hsuan Yen.
126132

0 commit comments

Comments
 (0)