cpython: cb09fdef19f5 (original) (raw)

Mercurial > cpython

changeset 101371:cb09fdef19f5 3.5

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. [#17214]

Martin Panter vadmium+py@gmail.com
date Mon, 16 May 2016 01:14:20 +0000
parents 52a7f580580c
children 841a9a3f3cf6 d921932fe02f
files Lib/test/test_urllib2.py Lib/urllib/request.py Misc/NEWS
diffstat 3 files changed, 52 insertions(+), 1 deletions(-)[+] [-] Lib/test/test_urllib2.py 35 Lib/urllib/request.py 12 Misc/NEWS 6

line wrap: on

line diff

--- a/Lib/test/test_urllib2.py +++ b/Lib/test/test_urllib2.py @@ -1224,6 +1224,41 @@ class HandlerTests(unittest.TestCase): fp = urllib.request.urlopen("http://python.org/path")[](#l1.4) self.assertEqual(fp.geturl(), "http://python.org/path?query")[](#l1.5)

+ def test_proxy(self): o = OpenerDirector() ph = urllib.request.ProxyHandler(dict(http="proxy.example.com:3128"))

--- a/Lib/urllib/request.py +++ b/Lib/urllib/request.py @@ -91,6 +91,7 @@ import os import posixpath import re import socket +import string import sys import time import collections @@ -616,8 +617,12 @@ class HTTPRedirectHandler(BaseHandler): # from the user (of urllib.request, in this case). In practice, # essentially all clients do redirect in this case, so we do # the same.

+

+ CONTENT_HEADERS = ("content-length", "content-type") newheaders = dict((k, v) for k, v in req.headers.items() if k.lower() not in CONTENT_HEADERS) @@ -657,6 +662,11 @@ class HTTPRedirectHandler(BaseHandler): urlparts[2] = "/" newurl = urlunparse(urlparts)

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

--- a/Misc/NEWS +++ b/Misc/NEWS @@ -121,6 +121,12 @@ Library