Handle complex redirect URIs on Python 3

This commit is contained in:
Cory Benfield
2015-06-28 16:56:37 +01:00
parent 0acbf2b91d
commit aedc0e515d
+8 -1
View File
@@ -13,7 +13,7 @@ from collections import Mapping
from datetime import datetime
from .auth import _basic_auth_str
from .compat import cookielib, OrderedDict, urljoin, urlparse
from .compat import cookielib, OrderedDict, urljoin, urlparse, is_py3, str
from .cookies import (
cookiejar_from_dict, extract_cookies_to_jar, RequestsCookieJar, merge_cookies)
from .models import Request, PreparedRequest, DEFAULT_REDIRECT_LIMIT
@@ -132,6 +132,13 @@ class SessionRedirectMixin(object):
parsed = urlparse(location_url)
location_url = parsed.geturl()
# On Python 3, the location header was decoded using Latin 1, but
# urlparse in requote_uri will encode it with UTF-8 before quoting.
# Because of this insanity, we need to fix it up ourselves by
# sending the URL back to bytes ourselves.
if is_py3 and isinstance(url, str):
url = url.encode('latin1')
# Facilitate relative 'location' headers, as allowed by RFC 7231.
# (e.g. '/path/to/resource' instead of 'http://domain.tld/path/to/resource')
# Compliant with RFC3986, we percent encode the url.