From aedc0e515d83685e8047001b05980a005d8fc057 Mon Sep 17 00:00:00 2001 From: Cory Benfield Date: Sun, 28 Jun 2015 16:56:37 +0100 Subject: [PATCH] Handle complex redirect URIs on Python 3 --- requests/sessions.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/requests/sessions.py b/requests/sessions.py index b11bdb6d..ca4fee8b 100644 --- a/requests/sessions.py +++ b/requests/sessions.py @@ -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.