fix handle of non-ascii location on redirects
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by shmuelamar · committed by shmuelamar · parent fd7dcd2 · Browse files · 2 files·+14 added−11 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+14 added−11 removed
- tests:+3 added−9 removed
- other:+11 added−2 removed
Provenance
- Commit
722b1da- Tree
d438bcb- Author
shmuelamar <shmulikamar@gmail.com>- Committer
shmuelamar <shmulikamar@gmail.com>- Parent
fd7dcd2
2 files changed+14 added−11 removed
modified:requests/sessions.py+11 added-2 removed
10 unmodified lines11 from collections import Mapping12 from datetime import datetime13 14 from .auth import _basic_auth_str15-from .compat import cookielib, OrderedDict, urljoin, urlparse15+from .compat import cookielib, is_py3, OrderedDict, urljoin, urlparse16 from .cookies import (17 cookiejar_from_dict, extract_cookies_to_jar, RequestsCookieJar, merge_cookies)18 from .models import Request, PreparedRequest, DEFAULT_REDIRECT_LIMIT19 from .hooks import default_hooks, dispatch_hook69 unmodified lines89 90 def get_redirect_target(self, resp):91 """Receives a Response. Returns a redirect URI or ``None``"""92 if resp.is_redirect:93- return resp.headers['location']93+ location = resp.headers['location']94+ # Currently the underlying http module on py3 decode headers95+ # in latin1, but empirical evidence suggests that latin1 is very96+ # rarely used with non-ASCII characters in HTTP headers.97+ # It is more likely to get UTF8 header rather than latin1.98+ # This causes incorrect handling of UTF8 encoded location headers.99+ # To solve this, we re-encode the location in latin1.100+ if is_py3:101+ location = location.encode('latin1')102+ return to_native_string(location, 'utf8')103 return None104 105 def resolve_redirects(self, resp, req, stream=False, timeout=None,106 verify=True, cert=None, proxies=None, **adapter_kwargs):639 unmodified linesmodified:tests/test_lowlevel.py+3 added-9 removed
1 unmodified line2 3 import pytest4 import threading5 import requests6-from requests.compat import quote, is_py36 7 from tests.testserver.server import Server, consume_socket_content8 9 from .utils import override_environ196 unmodified lines206 207 208 def test_redirect_rfc1808_to_non_ascii_location():209 path = u'š'211- expected_path = quote(path.encode('utf8')).encode('ascii')212- expected_path_py3 = b'%C3%85%C2%A1'210+ expected_path = b'%C5%A1'211 redirect_request = [] # stores the second request to the server212 213 def redirect_resp_handler(sock):214 consume_socket_content(sock, timeout=0.5)15 unmodified lines230 r = requests.get(url=url, allow_redirects=True)231 assert r.status_code == 200232 assert len(r.history) == 1233 assert r.history[0].status_code == 301234+ assert redirect_request[0].startswith(b'GET /' + expected_path + b' HTTP/1.1')235+ assert r.url == u'{0}/{1}'.format(url, expected_path.decode('ascii'))236 237- # currently Python3 not handling non-ASCII redirects (issue #3888)238- if is_py3:239- assert redirect_request[0].startswith(b'GET /' + expected_path_py3 + b' HTTP/1.1')240- else:241- assert redirect_request[0].startswith(b'GET /' + expected_path + b' HTTP/1.1')242-237 close_server.set()