diff options
author | Martin Panter <vadmium+py@gmail.com> | 2016-05-16 07:45:28 (GMT) |
---|---|---|
committer | Martin Panter <vadmium+py@gmail.com> | 2016-05-16 07:45:28 (GMT) |
commit | 0b39a556e8aa4d15de21a3305f74e12886d5f31b (patch) | |
tree | 6ed9623fc778c14e7ee27dfa9a497b8d09b06363 /Lib/urllib/request.py | |
parent | c944c2dab8036bab6c4d3b2391fa3fbbab8859b5 (diff) | |
parent | e6f060903cf2080b6570a87fde5021aa14d05530 (diff) | |
download | cpython-0b39a556e8aa4d15de21a3305f74e12886d5f31b.zip cpython-0b39a556e8aa4d15de21a3305f74e12886d5f31b.tar.gz cpython-0b39a556e8aa4d15de21a3305f74e12886d5f31b.tar.bz2 |
Issue #14132, Issue #17214: Merge two redirect handling fixes from 3.5
Diffstat (limited to 'Lib/urllib/request.py')
-rw-r--r-- | Lib/urllib/request.py | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py index 4373c26..4a3daec 100644 --- 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 @@ -676,8 +677,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. - # be conciliant with URIs containing a space + + # Be conciliant with URIs containing a space. This is mainly + # redundant with the more complete encoding done in http_error_302(), + # but it is kept for compatibility with other callers. newurl = newurl.replace(' ', '%20') + CONTENT_HEADERS = ("content-length", "content-type") newheaders = dict((k, v) for k, v in req.headers.items() if k.lower() not in CONTENT_HEADERS) @@ -712,11 +717,16 @@ class HTTPRedirectHandler(BaseHandler): "%s - Redirection to url '%s' is not allowed" % (msg, newurl), headers, fp) - if not urlparts.path: + if not urlparts.path and urlparts.netloc: urlparts = list(urlparts) urlparts[2] = "/" newurl = urlunparse(urlparts) + # http.client.parse_headers() decodes as ISO-8859-1. Recover the + # original bytes and percent-encode non-ASCII bytes, and any special + # characters such as the space. + newurl = quote( + newurl, encoding="iso-8859-1", safe=string.punctuation) newurl = urljoin(req.full_url, newurl) # XXX Probably want to forget about the state of the current |