Skip to content
Myr

#4965 fix: Accessing response.content twice removes forgets read error.

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

authored by Белавин Денис · committed by Белавин Денис · parent d91fe00 · Browse files · 2 files+57 added−7 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+57 added−7 removed

  • tests:+41 added−0 removed
  • other:+16 added−7 removed

2 files+57 added−7 removed

modified:requests/models.py+16 added-7 removed
638 unmodified lines
639 #: The :class:`PreparedRequest <PreparedRequest>` object to which this640 #: is a response.641 self.request = None642643 #: If there was an error in the processing of content,644 #: then save the error that would return the same error when you re-appeal.643 self.error = None645 self._error = None646647 def __enter__(self):648 return self649
102 unmodified lines
752 if hasattr(self.raw, 'stream'):753 try:754 for chunk in self.raw.stream(chunk_size, decode_content=True):755 yield chunk756757 except ProtocolError as e:755 self._error = e756 raise ChunkedEncodingError(e)758 self._error = ChunkedEncodingError(e)759760 except DecodeError as e:758 self._error = e759 raise ContentDecodingError(e)761 self._error = ContentDecodingError(e)762763 except ReadTimeoutError as e:761 self._error = e762 raise ConnectionError(e)764 self._error = ConnectionError(e)765766 finally:767 # if we had an error - throw the saved error768 if self._error:769 raise self._error770771 else:772 # Standard file-like object.773 while True:774 chunk = self.raw.read(chunk_size)
64 unmodified lines
839 self._content = None840 else:841 self._content = b''.join(self.iter_content(CONTENT_CHUNK_SIZE)) or b''842843 # if we had an error - throw the saved error844 if self._error is not None:845 raise self._error846847 self._content_consumed = True
123 unmodified lines
modified:tests/test_lowlevel.py+41 added
1 unmodified line
23import pytest4import threading5import requests6from requests.exceptions import ChunkedEncodingError78from tests.testserver.server import Server, consume_socket_content910from .utils import override_environ
296 unmodified lines
307 # Verify previous fragment is used and not the original.308 assert r.url == 'http://{}:{}/final-url/#relevant-section'.format(host, port)309310 close_server.set()311312313def test_response_content_retains_error():314 """Verify that accessing response.content retains an error.315316 See https://github.com/kennethreitz/requests/issues/4965317 """318319 data = "Some random stuff to read from remove server.\n"320321 def response_handler(sock):322 req = consume_socket_content(sock, timeout=0.5)323324 # Send invalid chunked data (length mismatch)325 sock.send(326 b'HTTP/1.1 200 OK\r\n'327 b'Transfer-Encoding: chunked\r\n'328 b'\r\n2\r\n42\r\n8\r\n123\r\n' # 5 bytes missing329 )330331 close_server = threading.Event()332 server = Server(response_handler, wait_to_close_event=close_server)333334 with server as (host, port):335 url = 'http://{}:{}/path'.format(host, port)336 r = requests.post(url, stream=True)337 with pytest.raises(ChunkedEncodingError):338 r.content339340 # Access the bad response data again, I would expect the same341 # error again.342343 try:344 content = r.content345 except ChunkedEncodingError:346 pass # fine, same exception347 else:348 assert False, "error response has content: {0!r}".format(content)349 close_server.set()350