Skip to content
Myr

Merge pull request #1944 from schlamar/redirect-decoding

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

Catch errors while handling redirects

authored by Kenneth Reitz · committed by Kenneth Reitz · parents 5893cfc, 1645991 · Browse files · 3 files+50 added−9 removed
This commit

State

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

Diff stats

3 files+50 added−9 removed

  • other:+50 added−9 removed

3 files+50 added−9 removed

modified:requests/adapters.py+1 added-6 removed
383 unmodified lines
384 raise Timeout(e, request=request)385 else:386 raise387388 r = self.build_response(request, resp)389390 if not stream:391 r.content392393 return r388 return self.build_response(request, resp)
modified:requests/sessions.py+9 added-2 removed
17 unmodified lines
18 cookiejar_from_dict, extract_cookies_to_jar, RequestsCookieJar, merge_cookies)19from .models import Request, PreparedRequest, DEFAULT_REDIRECT_LIMIT20from .hooks import default_hooks, dispatch_hook21from .utils import to_key_val_list, default_headers, to_native_string22from .exceptions import TooManyRedirects, InvalidSchema22from .exceptions import (23 TooManyRedirects, InvalidSchema, ChunkedEncodingError, ContentDecodingError)24from .structures import CaseInsensitiveDict2526from .adapters import HTTPAdapter27
66 unmodified lines
9495 while resp.is_redirect:96 prepared_request = req.copy()9798 try:97 resp.content # Consume socket so it can be released99 resp.content # Consume socket so it can be released100 except (ChunkedEncodingError, ContentDecodingError, RuntimeError):101 resp.raw.read(decode_content=False)102103 if i >= self.max_redirects:104 raise TooManyRedirects('Exceeded %s redirects.' % self.max_redirects)105
485 unmodified lines
591 # Get the last request made592 r = history.pop()593 r.history = history594595 if not stream:596 r.content597598 return r599600 def get_adapter(self, url):601 """Returns the appropriate connnection adapter for the given URL."""
36 unmodified lines
modified:test_requests.py+40 added-1 removed
915 unmodified lines
916 h2 = r.request.headers['Authorization']917918 assert h1 == h2919920 def test_manual_redirect_with_partial_body_read(self):921 s = requests.Session()922 r1 = s.get(httpbin('redirect/2'), allow_redirects=False, stream=True)923 assert r1.is_redirect924 rg = s.resolve_redirects(r1, r1.request, stream=True)925926 # read only the first eight bytes of the response body,927 # then follow the redirect928 r1.iter_content(8)929 r2 = next(rg)930 assert r2.is_redirect931932 # read all of the response via iter_content,933 # then follow the redirect934 for _ in r2.iter_content():935 pass936 r3 = next(rg)937 assert not r3.is_redirect938939 def _patch_adapter_gzipped_redirect(self, session, url):940 adapter = session.get_adapter(url=url)941 org_build_response = adapter.build_response942 self._patched_response = False943944 def build_response(*args, **kwargs):945 resp = org_build_response(*args, **kwargs)946 if not self._patched_response:947 resp.raw.headers['content-encoding'] = 'gzip'948 self._patched_response = True949 return resp950951 adapter.build_response = build_response952953 def test_redirect_with_wrong_gzipped_header(self):954 s = requests.Session()955 url = httpbin('redirect/1')956 self._patch_adapter_gzipped_redirect(s, url)957 s.get(url)958959960class TestContentEncodingDetection(unittest.TestCase):961962 def test_none(self):963 encodings = requests.utils.get_encodings_from_content('')
395 unmodified lines
1359 data=data,1360 hooks=default_hooks()1361 )1362 assert p.body == urlencode(data)1324 136313641365if __name__ == '__main__':1366 unittest.main()