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
Provenance
- Commit
739d153- Tree
022dfa7- Author
Kenneth Reitz <me@kennethreitz.org>- Committer
Kenneth Reitz <me@kennethreitz.org>- Parent 1
5893cfc- Parent 2
1645991
3 files changed+50 added−9 removed
modified:requests/adapters.py+1 added-6 removed
383 unmodified lines384 raise Timeout(e, request=request)385 else:386 raise387 388- r = self.build_response(request, resp)389-390- if not stream:391- r.content392-393- return r388+ return self.build_response(request, resp)modified:requests/sessions.py+9 added-2 removed
17 unmodified lines18 cookiejar_from_dict, extract_cookies_to_jar, RequestsCookieJar, merge_cookies)19 from .models import Request, PreparedRequest, DEFAULT_REDIRECT_LIMIT20 from .hooks import default_hooks, dispatch_hook21 from .utils import to_key_val_list, default_headers, to_native_string22-from .exceptions import TooManyRedirects, InvalidSchema22+from .exceptions import (23+ TooManyRedirects, InvalidSchema, ChunkedEncodingError, ContentDecodingError)24 from .structures import CaseInsensitiveDict25 26 from .adapters import HTTPAdapter27 66 unmodified lines94 95 while resp.is_redirect:96 prepared_request = req.copy()97 98+ 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)102 103 if i >= self.max_redirects:104 raise TooManyRedirects('Exceeded %s redirects.' % self.max_redirects)105 485 unmodified lines591 # Get the last request made592 r = history.pop()593 r.history = history594 595+ if not stream:596+ r.content597+598 return r599 600 def get_adapter(self, url):601 """Returns the appropriate connnection adapter for the given URL."""36 unmodified linesmodified:test_requests.py+40 added-1 removed
915 unmodified lines916 h2 = r.request.headers['Authorization']917 918 assert h1 == h2919 920+ 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)925 926+ # 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_redirect931+932+ # 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_redirect938+939+ 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 = False943+944+ 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 resp950+951+ adapter.build_response = build_response952+953+ 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)958+959+960 class TestContentEncodingDetection(unittest.TestCase):961 962 def test_none(self):963 encodings = requests.utils.get_encodings_from_content('')395 unmodified lines1359 data=data,1360 hooks=default_hooks()1361 )1362 assert p.body == urlencode(data)1324- 1363 1364+1365 if __name__ == '__main__':1366 unittest.main()