Skip to content
Myr

Handle empty zstd responses (#3412)

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

authored by Tom Christie · committed by GitHub · parent 189fc4b · Browse files · 5 files+26 added−5 removed
This commit

State

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

Diff stats

5 files+26 added−5 removed

  • tests:+19 added−0 removed
  • other:+7 added−5 removed

5 files+26 added−5 removed

modified:httpx/__version__.py+1 added-1 removed
1__title__ = "httpx"2__description__ = "A next generation HTTP client, for Python 3."3__version__ = "0.27.2"3__version__ = "0.28.0"
modified:httpx/_decoders.py+4 added
173 unmodified lines
174 "Make sure to install httpx using `pip install httpx[zstd]`."175 ) from None176177 self.decompressor = zstandard.ZstdDecompressor().decompressobj()178 self.seen_data = False179180 def decode(self, data: bytes) -> bytes:181 assert zstandard is not None182 self.seen_data = True183 output = io.BytesIO()184 try:185 output.write(self.decompressor.decompress(data))186 while self.decompressor.eof and self.decompressor.unused_data:
4 unmodified lines
191 raise DecodingError(str(exc)) from exc192 return output.getvalue()193194 def flush(self) -> bytes:195 if not self.seen_data:196 return b""197 ret = self.decompressor.flush() # note: this is a no-op198 if not self.decompressor.eof:199 raise DecodingError("Zstandard data is incomplete") # pragma: no cover200 return bytes(ret)
193 unmodified lines
modified:tests/test_decoders.py+19 added
98 unmodified lines
99 content=compressed_body,100 )101102103def test_zstd_empty():104 headers = [(b"Content-Encoding", b"zstd")]105 response = httpx.Response(200, headers=headers, content=b"")106 assert response.content == b""107108109def test_zstd_truncated():110 body = b"test 123"111 compressed_body = zstd.compress(body)112113 headers = [(b"Content-Encoding", b"zstd")]114 with pytest.raises(httpx.DecodingError):115 httpx.Response(116 200,117 headers=headers,118 content=compressed_body[1:3],119 )120121122def test_zstd_multiframe():123 # test inspired by urllib3 test suite124 data = (125 # Zstandard frame
230 unmodified lines
modified:CHANGELOG.md+1 added-1 removed
2 unmodified lines
3All notable changes to this project will be documented in this file.45The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).67## [Unreleased]7## 0.28.0 (...)89The 0.28 release includes a limited set of backwards incompatible changes.1011**Backwards incompatible changes**:
1116 unmodified lines
modified:README.md+1 added-3 removed