Skip to content
Myr

Merge pull request #3969 from shmuelamar/master

Before adoption: this commit came in with the history the project adopted, so no landing records it.

fix handle of non-ascii location on redirects issue #3888

authored by Cory Benfield · committed by GitHub · parents fd7dcd2, 722b1da · 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

2 files+14 added−11 removed

modified:requests/sessions.py+11 added-2 removed
10 unmodified lines
11from collections import Mapping12from datetime import datetime1314from .auth import _basic_auth_str15from .compat import cookielib, OrderedDict, urljoin, urlparse15from .compat import cookielib, is_py3, OrderedDict, urljoin, urlparse16from .cookies import (17 cookiejar_from_dict, extract_cookies_to_jar, RequestsCookieJar, merge_cookies)18from .models import Request, PreparedRequest, DEFAULT_REDIRECT_LIMIT19from .hooks import default_hooks, dispatch_hook
69 unmodified lines
8990 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 None104105 def resolve_redirects(self, resp, req, stream=False, timeout=None,106 verify=True, cert=None, proxies=None, **adapter_kwargs):
639 unmodified lines
modified:tests/test_lowlevel.py+3 added-9 removed
1 unmodified line
23import pytest4import threading5import requests6from requests.compat import quote, is_py367from tests.testserver.server import Server, consume_socket_content89from .utils import override_environ
196 unmodified lines
206207208def 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 server212213 def redirect_resp_handler(sock):214 consume_socket_content(sock, timeout=0.5)
15 unmodified lines
230 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'))236237 # 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')242237 close_server.set()