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
Provenance
- Commit
47f4a96- Tree
da43ad1- Author
Tom Christie <tom@tomchristie.com>- Committer
GitHub <noreply@github.com>- Parent
189fc4b
5 files changed+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 lines174 "Make sure to install httpx using `pip install httpx[zstd]`."175 ) from None176 177 self.decompressor = zstandard.ZstdDecompressor().decompressobj()178+ self.seen_data = False179 180 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 lines191 raise DecodingError(str(exc)) from exc192 return output.getvalue()193 194 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 linesmodified:tests/test_decoders.py+19 added
98 unmodified lines99 content=compressed_body,100 )101 102 103+def test_zstd_empty():104+ headers = [(b"Content-Encoding", b"zstd")]105+ response = httpx.Response(200, headers=headers, content=b"")106+ assert response.content == b""107+108+109+def test_zstd_truncated():110+ body = b"test 123"111+ compressed_body = zstd.compress(body)112+113+ 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+ )120+121+122 def test_zstd_multiframe():123 # test inspired by urllib3 test suite124 data = (125 # Zstandard frame230 unmodified linesmodified:CHANGELOG.md+1 added-1 removed
2 unmodified lines3 All notable changes to this project will be documented in this file.4 5 The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).6 7-## [Unreleased]7+## 0.28.0 (...)8 9 The 0.28 release includes a limited set of backwards incompatible changes.10 11 **Backwards incompatible changes**:1116 unmodified linesmodified:README.md+1 added-3 removed
11 unmodified lines121314151617181617181920127 unmodified linesExpand all11 unmodified lines <img src="https://badge.fury.io/py/httpx.svg" alt="Package version"></a></p>
HTTPX is a fully featured HTTP client library for Python 3. It includes **an integratedcommand line client**, has support for both **HTTP/1.1 and HTTP/2**, and provides both **syncand async APIs**.HTTPX is a fully featured HTTP client library for Python 3. It includes **an integrated command line client**, has support for both **HTTP/1.1 and HTTP/2**, and provides both **sync and async APIs**.
---
Install HTTPX using pip:127 unmodified linesExpand all