httpx2 2.10.0__tar.gz → 2.12.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. {httpx2-2.10.0 → httpx2-2.12.0}/CHANGELOG.md +32 -0
  2. {httpx2-2.10.0 → httpx2-2.12.0}/PKG-INFO +12 -21
  3. {httpx2-2.10.0 → httpx2-2.12.0}/README.md +1 -1
  4. {httpx2-2.10.0 → httpx2-2.12.0}/httpx2/__init__.py +1 -0
  5. {httpx2-2.10.0 → httpx2-2.12.0}/httpx2/_client.py +15 -22
  6. {httpx2-2.10.0 → httpx2-2.12.0}/httpx2/_content.py +10 -4
  7. {httpx2-2.10.0 → httpx2-2.12.0}/httpx2/_decoders.py +103 -62
  8. {httpx2-2.10.0 → httpx2-2.12.0}/httpx2/_models.py +45 -34
  9. {httpx2-2.10.0 → httpx2-2.12.0}/httpx2/_multipart.py +6 -0
  10. {httpx2-2.10.0 → httpx2-2.12.0}/httpx2/_status_codes.py +10 -5
  11. {httpx2-2.10.0 → httpx2-2.12.0}/httpx2/_transports/wsgi.py +2 -0
  12. {httpx2-2.10.0 → httpx2-2.12.0}/httpx2/_urls.py +58 -1
  13. {httpx2-2.10.0 → httpx2-2.12.0}/pyproject.toml +4 -4
  14. {httpx2-2.10.0 → httpx2-2.12.0}/.gitignore +0 -0
  15. {httpx2-2.10.0 → httpx2-2.12.0}/LICENSE.md +0 -0
  16. {httpx2-2.10.0 → httpx2-2.12.0}/httpx2/__version__.py +0 -0
  17. {httpx2-2.10.0 → httpx2-2.12.0}/httpx2/_alias.py +0 -0
  18. {httpx2-2.10.0 → httpx2-2.12.0}/httpx2/_api.py +0 -0
  19. {httpx2-2.10.0 → httpx2-2.12.0}/httpx2/_auth.py +0 -0
  20. {httpx2-2.10.0 → httpx2-2.12.0}/httpx2/_config.py +0 -0
  21. {httpx2-2.10.0 → httpx2-2.12.0}/httpx2/_exceptions.py +0 -0
  22. {httpx2-2.10.0 → httpx2-2.12.0}/httpx2/_main.py +0 -0
  23. {httpx2-2.10.0 → httpx2-2.12.0}/httpx2/_sse.py +0 -0
  24. {httpx2-2.10.0 → httpx2-2.12.0}/httpx2/_transports/__init__.py +0 -0
  25. {httpx2-2.10.0 → httpx2-2.12.0}/httpx2/_transports/asgi.py +0 -0
  26. {httpx2-2.10.0 → httpx2-2.12.0}/httpx2/_transports/base.py +0 -0
  27. {httpx2-2.10.0 → httpx2-2.12.0}/httpx2/_transports/default.py +0 -0
  28. {httpx2-2.10.0 → httpx2-2.12.0}/httpx2/_transports/mock.py +0 -0
  29. {httpx2-2.10.0 → httpx2-2.12.0}/httpx2/_types.py +0 -0
  30. {httpx2-2.10.0 → httpx2-2.12.0}/httpx2/_urlparse.py +0 -0
  31. {httpx2-2.10.0 → httpx2-2.12.0}/httpx2/_utils.py +0 -0
  32. {httpx2-2.10.0 → httpx2-2.12.0}/httpx2/py.typed +0 -0
  33. {httpx2-2.10.0 → httpx2-2.12.0}/httpx2/websockets/__init__.py +0 -0
  34. {httpx2-2.10.0 → httpx2-2.12.0}/httpx2/websockets/_api.py +0 -0
  35. {httpx2-2.10.0 → httpx2-2.12.0}/httpx2/websockets/_exceptions.py +0 -0
  36. {httpx2-2.10.0 → httpx2-2.12.0}/httpx2/websockets/_ping.py +0 -0
  37. {httpx2-2.10.0 → httpx2-2.12.0}/httpx2/websockets/_transport.py +0 -0
@@ -4,6 +4,38 @@ All notable changes to this project will be documented in this file.
4
4
 
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
6
6
 
7
+ ## 2.12.0 (August 18th, 2026)
8
+
9
+ ### Changed
10
+
11
+ * Use `backports.zstd` for Zstandard decoding on Python 3.13 and earlier.
12
+ ([#1146](https://github.com/pydantic/httpx2/pull/1146))
13
+
14
+ ### Fixed
15
+
16
+ * Bound peak memory while streaming compressed responses and close response streams when decoding fails.
17
+ ([#1126](https://github.com/pydantic/httpx2/pull/1126))
18
+
19
+ ## 2.11.0 (August 18th, 2026)
20
+
21
+ ### Added
22
+
23
+ * Add the public `Origin` value object and `URL.origin` property for normalized,
24
+ hashable origin comparisons. ([#1134](https://github.com/pydantic/httpx2/pull/1134))
25
+
26
+ ### Changed
27
+
28
+ * Require Brotli 1.2.0 or later for the `brotli` extra. ([#1141](https://github.com/pydantic/httpx2/pull/1141))
29
+
30
+ ### Fixed
31
+
32
+ * Restore deprecated status code aliases. ([#1135](https://github.com/pydantic/httpx2/pull/1135))
33
+ * Extract HTTP/2 release notes from changelog headings correctly. ([#1136](https://github.com/pydantic/httpx2/pull/1136))
34
+ * Respect explicit `Transfer-Encoding` headers and expose buffered request body lengths to WSGI applications.
35
+ ([#1137](https://github.com/pydantic/httpx2/pull/1137))
36
+ * Validate multipart part header names and values before serialization.
37
+ ([#1142](https://github.com/pydantic/httpx2/pull/1142))
38
+
7
39
  ## 2.10.0 (August 9th, 2026)
8
40
 
9
41
  ### Added
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.4
1
+ Metadata-Version: 2.5
2
2
  Name: httpx2
3
- Version: 2.10.0
3
+ Version: 2.12.0
4
4
  Summary: The next generation HTTP client.
5
5
  Project-URL: Changelog, https://github.com/pydantic/httpx2/blob/main/src/httpx2/CHANGELOG.md
6
6
  Project-URL: Homepage, https://github.com/pydantic/httpx2
@@ -27,14 +27,14 @@ Classifier: Programming Language :: Python :: 3.15
27
27
  Classifier: Topic :: Internet :: WWW/HTTP
28
28
  Requires-Python: >=3.10
29
29
  Requires-Dist: anyio>=4.10; sys_platform != 'emscripten'
30
- Requires-Dist: httpcore2==2.10.0; sys_platform != 'emscripten'
30
+ Requires-Dist: httpcore2==2.12.0; sys_platform != 'emscripten'
31
31
  Requires-Dist: httpx2-jsfetch; sys_platform == 'emscripten' and python_version >= '3.12'
32
32
  Requires-Dist: idna>=3.18
33
33
  Requires-Dist: truststore>=0.10; sys_platform != 'emscripten'
34
34
  Requires-Dist: typing-extensions>=4.5.0; python_version < '3.13'
35
35
  Provides-Extra: brotli
36
- Requires-Dist: brotli; (platform_python_implementation == 'CPython') and extra == 'brotli'
37
- Requires-Dist: brotlicffi; (platform_python_implementation != 'CPython') and extra == 'brotli'
36
+ Requires-Dist: brotli>=1.2.0; (platform_python_implementation == 'CPython') and extra == 'brotli'
37
+ Requires-Dist: brotlicffi>=1.2.0.0; (platform_python_implementation != 'CPython') and extra == 'brotli'
38
38
  Provides-Extra: cli
39
39
  Requires-Dist: click>=8.4.2; extra == 'cli'
40
40
  Requires-Dist: pygments==2.*; extra == 'cli'
@@ -46,7 +46,7 @@ Requires-Dist: socksio==1.*; extra == 'socks'
46
46
  Provides-Extra: ws
47
47
  Requires-Dist: wsproto>=1.2; extra == 'ws'
48
48
  Provides-Extra: zstd
49
- Requires-Dist: zstandard>=0.18.0; (python_version <= '3.13') and extra == 'zstd'
49
+ Requires-Dist: backports-zstd>=1.0.0; (python_version <= '3.13') and extra == 'zstd'
50
50
  Description-Content-Type: text/markdown
51
51
 
52
52
  <h1 align="center">HTTPX2</h1>
@@ -200,7 +200,7 @@ As well as these optional installs:
200
200
  * `rich` - Rich terminal support. *(Optional, with `httpx2[cli]`)*
201
201
  * `click` - Command line client support. *(Optional, with `httpx2[cli]`)*
202
202
  * `brotli` or `brotlicffi` - Decoding for "brotli" compressed responses. *(Optional, with `httpx2[brotli]`)*
203
- * `zstandard` - Decoding for "zstd" compressed responses on Python 3.13 and below. *(Optional, with `httpx2[zstd]`. On Python 3.14+, `zstd` is supported via the stdlib [`compression.zstd`](https://docs.python.org/3/library/compression.zstd.html) module when it is available; the `httpx2[zstd]` extra installs nothing there, so decoding falls back to `zstandard` only on 3.13 and below.)*
203
+ * `backports.zstd` - Decoding for "zstd" compressed responses on Python 3.13 and below. *(Optional, with `httpx2[zstd]`. On Python 3.14+, `zstd` is supported via the stdlib [`compression.zstd`](https://docs.python.org/3/library/compression.zstd.html) module when it is available, and the `httpx2[zstd]` extra installs nothing.)*
204
204
 
205
205
  A huge amount of credit is due to `requests` for the API layout that
206
206
  much of this work follows, as well as to `urllib3` for plenty of design
@@ -212,26 +212,17 @@ inspiration around the lower-level networking details.
212
212
 
213
213
  ## Release Information
214
214
 
215
- ### Added
216
-
217
- * Add support for running on WebAssembly / Emscripten via Pyodide, using a JavaScript
218
- `fetch`-based transport defined in `httpx2-jsfetch`.
219
- ([#1119](https://github.com/pydantic/httpx2/pull/1119))
220
- * Add `max_event_size` to cap SSE event buffering. ([#1071](https://github.com/pydantic/httpx2/pull/1071))
221
- * Add RFC 9110 status code constants. ([#1069](https://github.com/pydantic/httpx2/pull/1069))
222
- * Add support for Python 3.15. ([#1090](https://github.com/pydantic/httpx2/pull/1090))
215
+ ## 2.12.0 (August 18th, 2026)
223
216
 
224
217
  ### Changed
225
218
 
226
- * Improve SSE chunk buffering performance. ([#1117](https://github.com/pydantic/httpx2/pull/1117))
227
- * Skip cookie extraction for responses without `Set-Cookie` headers. ([#1107](https://github.com/pydantic/httpx2/pull/1107))
228
- * Return `str | None` instead of `Any` from `Headers.get`. ([#1121](https://github.com/pydantic/httpx2/pull/1121))
219
+ * Use `backports.zstd` for Zstandard decoding on Python 3.13 and earlier.
220
+ ([#1146](https://github.com/pydantic/httpx2/pull/1146))
229
221
 
230
222
  ### Fixed
231
223
 
232
- * Enforce the WebSocket max message size across fragmented messages. ([#1085](https://github.com/pydantic/httpx2/pull/1085))
233
- * Ignore unsolicited and duplicate WebSocket Pong frames. ([#1122](https://github.com/pydantic/httpx2/pull/1122))
234
-
224
+ * Bound peak memory while streaming compressed responses and close response streams when decoding fails.
225
+ ([#1126](https://github.com/pydantic/httpx2/pull/1126))
235
226
 
236
227
  ---
237
228
 
@@ -149,7 +149,7 @@ As well as these optional installs:
149
149
  * `rich` - Rich terminal support. *(Optional, with `httpx2[cli]`)*
150
150
  * `click` - Command line client support. *(Optional, with `httpx2[cli]`)*
151
151
  * `brotli` or `brotlicffi` - Decoding for "brotli" compressed responses. *(Optional, with `httpx2[brotli]`)*
152
- * `zstandard` - Decoding for "zstd" compressed responses on Python 3.13 and below. *(Optional, with `httpx2[zstd]`. On Python 3.14+, `zstd` is supported via the stdlib [`compression.zstd`](https://docs.python.org/3/library/compression.zstd.html) module when it is available; the `httpx2[zstd]` extra installs nothing there, so decoding falls back to `zstandard` only on 3.13 and below.)*
152
+ * `backports.zstd` - Decoding for "zstd" compressed responses on Python 3.13 and below. *(Optional, with `httpx2[zstd]`. On Python 3.14+, `zstd` is supported via the stdlib [`compression.zstd`](https://docs.python.org/3/library/compression.zstd.html) module when it is available, and the `httpx2[zstd]` extra installs nothing.)*
153
153
 
154
154
  A huge amount of credit is due to `requests` for the API layout that
155
155
  much of this work follows, as well as to `urllib3` for plenty of design
@@ -53,6 +53,7 @@ __all__ = [
53
53
  "NetRCAuth",
54
54
  "NetworkError",
55
55
  "options",
56
+ "Origin",
56
57
  "patch",
57
58
  "PoolTimeout",
58
59
  "post",
@@ -72,30 +72,18 @@ def _is_https_redirect(url: URL, location: URL) -> bool:
72
72
  """
73
73
  Return 'True' if 'location' is a HTTPS upgrade of 'url'
74
74
  """
75
- if url.host != location.host:
76
- return False
75
+ origin = url.origin
76
+ location_origin = location.origin
77
77
 
78
78
  return (
79
- url.scheme == "http"
80
- and _port_or_default(url) == 80
81
- and location.scheme == "https"
82
- and _port_or_default(location) == 443
79
+ origin.host == location_origin.host
80
+ and origin.scheme == "http"
81
+ and origin.port == 80
82
+ and location_origin.scheme == "https"
83
+ and location_origin.port == 443
83
84
  )
84
85
 
85
86
 
86
- def _port_or_default(url: URL) -> int | None:
87
- if url.port is not None:
88
- return url.port
89
- return {"http": 80, "https": 443}.get(url.scheme)
90
-
91
-
92
- def _same_origin(url: URL, other: URL) -> bool:
93
- """
94
- Return 'True' if the given URLs share the same origin.
95
- """
96
- return url.scheme == other.scheme and url.host == other.host and _port_or_default(url) == _port_or_default(other)
97
-
98
-
99
87
  class UseClientDefault:
100
88
  """
101
89
  For some parameters such as `auth=...` and `timeout=...` we need to be able
@@ -172,8 +160,13 @@ class BoundAsyncStream(AsyncByteStream):
172
160
  self.elapsed: datetime.timedelta | None = None
173
161
 
174
162
  async def __aiter__(self) -> typing.AsyncIterator[bytes]:
175
- async for chunk in self._stream:
176
- yield chunk
163
+ stream = self._stream.__aiter__()
164
+ try:
165
+ async for chunk in stream:
166
+ yield chunk
167
+ finally:
168
+ if isinstance(stream, AsyncGenerator):
169
+ await stream.aclose()
177
170
 
178
171
  async def aclose(self) -> None:
179
172
  self.elapsed = datetime.timedelta(seconds=time.perf_counter() - self._start)
@@ -534,7 +527,7 @@ class BaseClient:
534
527
  """
535
528
  headers = Headers(request.headers)
536
529
 
537
- if not _same_origin(url, request.url):
530
+ if url.origin != request.url.origin:
538
531
  if not _is_https_redirect(request.url, url):
539
532
  # Strip Authorization headers when responses are redirected
540
533
  # away from the origin. (Except for direct HTTP to HTTPS redirects.)
@@ -2,7 +2,7 @@ from __future__ import annotations
2
2
 
3
3
  import inspect
4
4
  import warnings
5
- from collections.abc import AsyncIterable, AsyncIterator, Iterable, Iterator, Mapping
5
+ from collections.abc import AsyncGenerator, AsyncIterable, AsyncIterator, Iterable, Iterator, Mapping
6
6
  from json import dumps as json_dumps
7
7
  from typing import (
8
8
  Any,
@@ -79,9 +79,15 @@ class AsyncIteratorByteStream(AsyncByteStream):
79
79
  yield chunk
80
80
  chunk = await self._stream.aread(self.CHUNK_SIZE)
81
81
  else:
82
- # Otherwise iterate.
83
- async for part in self._stream:
84
- yield part
82
+ # Otherwise iterate, making sure the wrapped stream is closed even if the
83
+ # consumer stops early (e.g. an exception is raised part-way through decoding).
84
+ stream = self._stream.__aiter__()
85
+ try:
86
+ async for part in stream:
87
+ yield part
88
+ finally:
89
+ if isinstance(stream, AsyncGenerator):
90
+ await stream.aclose()
85
91
 
86
92
 
87
93
  class UnattachedStream(AsyncByteStream, SyncByteStream):
@@ -7,8 +7,8 @@ See: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Encoding
7
7
  from __future__ import annotations
8
8
 
9
9
  import codecs
10
- import functools
11
10
  import io
11
+ import itertools
12
12
  import sys
13
13
  import typing
14
14
  import zlib
@@ -31,37 +31,60 @@ except ImportError: # pragma: no cover
31
31
  # Zstandard support is optional on Python <= 3.13.
32
32
  # On Python 3.14+, the stdlib includes an optional built-in zstd implementation.
33
33
  if typing.TYPE_CHECKING:
34
- # We keep checking Python version in the type checker path because try..except doesn't help type checkers.
35
34
  if sys.version_info >= (3, 14):
36
35
  from compression.zstd import ZstdDecompressor, ZstdError
37
36
  else:
38
- from zstandard import ZstdDecompressor as _ZstdDecompressor, ZstdError
37
+ from backports.zstd import ZstdDecompressor, ZstdError
39
38
 
40
- ZstdDecompressor = functools.partial(_ZstdDecompressor().decompressobj)
41
-
42
- _zstandard_installed: bool = False
39
+ _zstandard_installed: bool
43
40
  else: # pragma: no cover
44
- _zstandard_installed = False
45
41
  try:
46
- from compression.zstd import ZstdDecompressor, ZstdError
42
+ if sys.version_info >= (3, 14):
43
+ from compression.zstd import ZstdDecompressor, ZstdError
44
+ else:
45
+ from backports.zstd import ZstdDecompressor, ZstdError
47
46
 
48
47
  _zstandard_installed = True
49
- # Either Python <3.14 or the distro doesn't have `compression.zstd`.
50
48
  except ImportError:
51
- try:
52
- from zstandard import ZstdDecompressor as _ZstdDecompressor, ZstdError
49
+ _zstandard_installed = False
50
+
51
+
52
+ MAX_DECODE_CHUNK_SIZE = 2**20 # 1 MiB
53
+
54
+
55
+ class Decompressor(typing.Protocol):
56
+ @property
57
+ def unconsumed_tail(self) -> bytes: ...
58
+
59
+ def decompress(self, data: bytes, max_length: int) -> bytes: ...
60
+
61
+ def flush(self) -> bytes: ...
62
+
63
+
64
+ class ZlibDecompressor:
65
+ """
66
+ Drain a `zlib`/`gzip` decompressor in bounded pieces so a small compressed
67
+ input cannot inflate to an unbounded buffer in a single call.
68
+ """
69
+
70
+ def __init__(self, decompressor: Decompressor) -> None:
71
+ self.decompressor = decompressor
53
72
 
54
- ZstdDecompressor = functools.partial(_ZstdDecompressor().decompressobj)
55
- _zstandard_installed = True
56
- except ImportError:
57
- pass
73
+ def decompress(self, data: bytes) -> typing.Iterator[bytes]:
74
+ decompressed = self.decompressor.decompress(data, MAX_DECODE_CHUNK_SIZE)
75
+ while decompressed:
76
+ yield decompressed
77
+ decompressed = self.decompressor.decompress(self.decompressor.unconsumed_tail, MAX_DECODE_CHUNK_SIZE)
78
+
79
+ def flush(self) -> bytes:
80
+ return self.decompressor.flush()
58
81
 
59
82
 
60
83
  class ContentDecoder:
61
- def decode(self, data: bytes) -> bytes:
84
+ def decode(self, data: bytes) -> typing.Iterator[bytes]:
62
85
  raise NotImplementedError() # pragma: no cover
63
86
 
64
- def flush(self) -> bytes:
87
+ def flush(self) -> typing.Iterator[bytes]:
65
88
  raise NotImplementedError() # pragma: no cover
66
89
 
67
90
 
@@ -70,11 +93,11 @@ class IdentityDecoder(ContentDecoder):
70
93
  Handle unencoded data.
71
94
  """
72
95
 
73
- def decode(self, data: bytes) -> bytes:
74
- return data
96
+ def decode(self, data: bytes) -> typing.Iterator[bytes]:
97
+ yield data
75
98
 
76
- def flush(self) -> bytes:
77
- return b""
99
+ def flush(self) -> typing.Iterator[bytes]:
100
+ yield from ()
78
101
 
79
102
 
80
103
  class DeflateDecoder(ContentDecoder):
@@ -86,22 +109,23 @@ class DeflateDecoder(ContentDecoder):
86
109
 
87
110
  def __init__(self) -> None:
88
111
  self.first_attempt = True
89
- self.decompressor = zlib.decompressobj()
112
+ self.decompressor = ZlibDecompressor(zlib.decompressobj())
90
113
 
91
- def decode(self, data: bytes) -> bytes:
114
+ def decode(self, data: bytes) -> typing.Iterator[bytes]:
92
115
  was_first_attempt = self.first_attempt
93
116
  self.first_attempt = False
94
117
  try:
95
- return self.decompressor.decompress(data)
118
+ yield from self.decompressor.decompress(data)
96
119
  except zlib.error as exc:
97
120
  if was_first_attempt:
98
- self.decompressor = zlib.decompressobj(-zlib.MAX_WBITS)
99
- return self.decode(data)
100
- raise DecodingError(str(exc)) from exc
121
+ self.decompressor = ZlibDecompressor(zlib.decompressobj(-zlib.MAX_WBITS))
122
+ yield from self.decode(data)
123
+ else:
124
+ raise DecodingError(str(exc)) from exc
101
125
 
102
- def flush(self) -> bytes:
126
+ def flush(self) -> typing.Iterator[bytes]:
103
127
  try:
104
- return self.decompressor.flush()
128
+ yield self.decompressor.flush()
105
129
  except zlib.error as exc: # pragma: no cover
106
130
  raise DecodingError(str(exc)) from exc
107
131
 
@@ -114,17 +138,17 @@ class GZipDecoder(ContentDecoder):
114
138
  """
115
139
 
116
140
  def __init__(self) -> None:
117
- self.decompressor = zlib.decompressobj(zlib.MAX_WBITS | 16)
141
+ self.decompressor = ZlibDecompressor(zlib.decompressobj(zlib.MAX_WBITS | 16))
118
142
 
119
- def decode(self, data: bytes) -> bytes:
143
+ def decode(self, data: bytes) -> typing.Iterator[bytes]:
120
144
  try:
121
- return self.decompressor.decompress(data)
145
+ yield from self.decompressor.decompress(data)
122
146
  except zlib.error as exc:
123
147
  raise DecodingError(str(exc)) from exc
124
148
 
125
- def flush(self) -> bytes:
149
+ def flush(self) -> typing.Iterator[bytes]:
126
150
  try:
127
- return self.decompressor.flush()
151
+ yield self.decompressor.flush()
128
152
  except zlib.error as exc: # pragma: no cover
129
153
  raise DecodingError(str(exc)) from exc
130
154
 
@@ -149,26 +173,31 @@ class BrotliDecoder(ContentDecoder):
149
173
 
150
174
  self.decompressor = brotli.Decompressor()
151
175
  self.seen_data = False
152
- self._decompress: typing.Callable[[bytes], bytes]
176
+ self._decompress: typing.Callable[..., bytes]
153
177
  if hasattr(self.decompressor, "decompress"):
154
178
  # The 'brotlicffi' package.
155
179
  self._decompress = self.decompressor.decompress # pragma: no cover
156
180
  else:
157
181
  # The 'brotli' package.
158
- self._decompress = self.decompressor.process # pragma: no cover
182
+ self._decompress = self.decompressor.process
159
183
 
160
- def decode(self, data: bytes) -> bytes:
184
+ def decode(self, data: bytes) -> typing.Iterator[bytes]:
161
185
  if not data:
162
- return b""
186
+ return
163
187
  self.seen_data = True
164
188
  try:
165
- return self._decompress(data)
189
+ # The C backend may allocate nearly twice the requested threshold.
190
+ output_buffer_limit = MAX_DECODE_CHUNK_SIZE // 2
191
+ decompressed = self._decompress(data, output_buffer_limit=output_buffer_limit)
192
+ while decompressed:
193
+ yield decompressed
194
+ decompressed = self._decompress(b"", output_buffer_limit=output_buffer_limit)
166
195
  except brotli.error as exc:
167
196
  raise DecodingError(str(exc)) from exc
168
197
 
169
- def flush(self) -> bytes:
198
+ def flush(self) -> typing.Iterator[bytes]:
170
199
  if not self.seen_data:
171
- return b""
200
+ return
172
201
  try:
173
202
  if hasattr(self.decompressor, "finish"):
174
203
  # Only available in the 'brotlicffi' package.
@@ -177,16 +206,15 @@ class BrotliDecoder(ContentDecoder):
177
206
  # will never actually emit any data. However, it will potentially throw
178
207
  # errors if a truncated or damaged data stream has been used.
179
208
  self.decompressor.finish() # pragma: no cover
180
- return b""
181
209
  except brotli.error as exc: # pragma: no cover
182
210
  raise DecodingError(str(exc)) from exc
211
+ yield from ()
183
212
 
184
213
 
185
214
  class ZStandardDecoder(ContentDecoder):
186
215
  """Handle 'zstd' RFC 8878 decoding.
187
216
 
188
- If running on Python 3.14+ or a distro that doesn't have the `compression.zstd` stdlib module, requires either:
189
- `pip install zstandard` or `pip install httpx2[zstd]`.
217
+ On Python 3.13 and below, this requires `pip install httpx2[zstd]`.
190
218
  """
191
219
 
192
220
  # inspired by the ZstdDecoder implementation in urllib3
@@ -199,30 +227,34 @@ class ZStandardDecoder(ContentDecoder):
199
227
  self.decompressor = ZstdDecompressor()
200
228
  self.seen_data = False
201
229
 
202
- def decode(self, data: bytes) -> bytes:
230
+ def decode(self, data: bytes) -> typing.Iterator[bytes]:
203
231
  if not data:
204
- return b""
232
+ return
205
233
  self.seen_data = True
206
- output = io.BytesIO()
207
234
  try:
208
235
  if self.decompressor.eof:
209
236
  data = self.decompressor.unused_data + data
210
237
  self.decompressor = ZstdDecompressor()
211
- output.write(self.decompressor.decompress(data))
212
- while self.decompressor.eof and self.decompressor.unused_data:
213
- unused_data = self.decompressor.unused_data
238
+ while True:
239
+ decompressed = self.decompressor.decompress(data, MAX_DECODE_CHUNK_SIZE)
240
+ while decompressed:
241
+ yield decompressed
242
+ if self.decompressor.needs_input or self.decompressor.eof:
243
+ break
244
+ decompressed = self.decompressor.decompress(b"", MAX_DECODE_CHUNK_SIZE)
245
+ if not (self.decompressor.eof and self.decompressor.unused_data):
246
+ break
247
+ data = self.decompressor.unused_data
214
248
  self.decompressor = ZstdDecompressor()
215
- output.write(self.decompressor.decompress(unused_data))
216
249
  except ZstdError as exc:
217
250
  raise DecodingError(str(exc)) from exc
218
- return output.getvalue()
219
251
 
220
- def flush(self) -> bytes:
252
+ def flush(self) -> typing.Iterator[bytes]:
221
253
  if not self.seen_data:
222
- return b""
254
+ return
223
255
  if not self.decompressor.eof:
224
256
  raise DecodingError("Zstandard data is incomplete") # pragma: no cover
225
- return b""
257
+ yield from ()
226
258
 
227
259
 
228
260
  class MultiDecoder(ContentDecoder):
@@ -243,16 +275,25 @@ class MultiDecoder(ContentDecoder):
243
275
  # Note that we reverse the order for decoding.
244
276
  self.children: list[ContentDecoder] = [SUPPORTED_DECODERS[coding]() for coding in reversed(codings)]
245
277
 
246
- def decode(self, data: bytes) -> bytes:
278
+ def decode(self, data: bytes) -> typing.Iterator[bytes]:
279
+ streams: typing.Iterator[bytes] = iter((data,))
247
280
  for child in self.children:
248
- data = child.decode(data)
249
- return data
281
+ streams = self._pipe(child.decode, streams)
282
+ yield from streams
250
283
 
251
- def flush(self) -> bytes:
252
- data = b""
284
+ def flush(self) -> typing.Iterator[bytes]:
285
+ streams: typing.Iterator[bytes] = iter(())
253
286
  for child in self.children:
254
- data = child.decode(data) + child.flush()
255
- return data
287
+ streams = itertools.chain(self._pipe(child.decode, streams), child.flush())
288
+ yield from streams
289
+
290
+ @staticmethod
291
+ def _pipe(
292
+ decode: typing.Callable[[bytes], typing.Iterator[bytes]],
293
+ upstream: typing.Iterator[bytes],
294
+ ) -> typing.Iterator[bytes]:
295
+ for chunk in upstream:
296
+ yield from decode(chunk)
256
297
 
257
298
 
258
299
  class ByteChunker:
@@ -1,13 +1,14 @@
1
1
  from __future__ import annotations
2
2
 
3
3
  import codecs
4
+ import contextlib
4
5
  import datetime
5
6
  import email.message
6
7
  import json as jsonlib
7
8
  import re
8
9
  import typing
9
10
  import urllib.request
10
- from collections.abc import Mapping
11
+ from collections.abc import AsyncGenerator, Mapping
11
12
  from http.cookiejar import Cookie, CookieJar
12
13
 
13
14
  from ._content import ByteStream, UnattachedStream, encode_request, encode_response
@@ -452,7 +453,10 @@ class Request:
452
453
 
453
454
  def _prepare(self, default_headers: dict[str, str]) -> None:
454
455
  for key, value in default_headers.items():
455
- # Ignore Transfer-Encoding if the Content-Length has been set explicitly.
456
+ # Ignore Content-Length if Transfer-Encoding has been set explicitly.
457
+ if key.lower() == "content-length" and "Transfer-Encoding" in self.headers:
458
+ continue
459
+ # Ignore Transfer-Encoding if Content-Length has been set explicitly.
456
460
  if key.lower() == "transfer-encoding" and "Content-Length" in self.headers:
457
461
  continue
458
462
  self.headers.setdefault(key, value)
@@ -886,12 +890,12 @@ class Response:
886
890
  chunker = ByteChunker(chunk_size=chunk_size)
887
891
  with request_context(request=self._request):
888
892
  for raw_bytes in self.iter_raw():
889
- decoded = decoder.decode(raw_bytes)
893
+ for decoded in decoder.decode(raw_bytes):
894
+ for chunk in chunker.decode(decoded):
895
+ yield chunk
896
+ for decoded in decoder.flush():
890
897
  for chunk in chunker.decode(decoded):
891
- yield chunk
892
- decoded = decoder.flush()
893
- for chunk in chunker.decode(decoded):
894
- yield chunk # pragma: no cover
898
+ yield chunk # pragma: no cover
895
899
  for chunk in chunker.flush():
896
900
  yield chunk
897
901
 
@@ -938,16 +942,17 @@ class Response:
938
942
  self._num_bytes_downloaded = 0
939
943
  chunker = ByteChunker(chunk_size=chunk_size)
940
944
 
941
- with request_context(request=self._request):
942
- for raw_stream_bytes in self.stream:
943
- self._num_bytes_downloaded += len(raw_stream_bytes)
944
- for chunk in chunker.decode(raw_stream_bytes):
945
- yield chunk
946
-
947
- for chunk in chunker.flush():
948
- yield chunk
945
+ try:
946
+ with request_context(request=self._request):
947
+ for raw_stream_bytes in self.stream:
948
+ self._num_bytes_downloaded += len(raw_stream_bytes)
949
+ for chunk in chunker.decode(raw_stream_bytes):
950
+ yield chunk
949
951
 
950
- self.close()
952
+ for chunk in chunker.flush():
953
+ yield chunk
954
+ finally:
955
+ self.close()
951
956
 
952
957
  def close(self) -> None:
953
958
  """
@@ -967,10 +972,11 @@ class Response:
967
972
  Read and return the response content.
968
973
  """
969
974
  if not hasattr(self, "_content"):
970
- self._content = b"".join([part async for part in self.aiter_bytes()])
975
+ async with contextlib.aclosing(self.aiter_bytes()) as parts:
976
+ self._content = b"".join([part async for part in parts])
971
977
  return self._content
972
978
 
973
- async def aiter_bytes(self, chunk_size: int | None = None) -> typing.AsyncIterator[bytes]:
979
+ async def aiter_bytes(self, chunk_size: int | None = None) -> typing.AsyncGenerator[bytes, None]:
974
980
  """
975
981
  A byte-iterator over the decoded response content.
976
982
  This allows us to handle gzip, deflate, brotli, and zstd encoded responses.
@@ -983,13 +989,14 @@ class Response:
983
989
  decoder = self._get_content_decoder()
984
990
  chunker = ByteChunker(chunk_size=chunk_size)
985
991
  with request_context(request=self._request):
986
- async for raw_bytes in self.aiter_raw():
987
- decoded = decoder.decode(raw_bytes)
992
+ async with contextlib.aclosing(self.aiter_raw()) as raw_stream:
993
+ async for raw_bytes in raw_stream:
994
+ for decoded in decoder.decode(raw_bytes):
995
+ for chunk in chunker.decode(decoded):
996
+ yield chunk
997
+ for decoded in decoder.flush():
988
998
  for chunk in chunker.decode(decoded):
989
- yield chunk
990
- decoded = decoder.flush()
991
- for chunk in chunker.decode(decoded):
992
- yield chunk # pragma: no cover
999
+ yield chunk # pragma: no cover
993
1000
  for chunk in chunker.flush():
994
1001
  yield chunk
995
1002
 
@@ -1021,7 +1028,7 @@ class Response:
1021
1028
  for line in decoder.flush():
1022
1029
  yield line
1023
1030
 
1024
- async def aiter_raw(self, chunk_size: int | None = None) -> typing.AsyncIterator[bytes]:
1031
+ async def aiter_raw(self, chunk_size: int | None = None) -> typing.AsyncGenerator[bytes, None]:
1025
1032
  """
1026
1033
  A byte-iterator over the raw response content.
1027
1034
  """
@@ -1036,16 +1043,20 @@ class Response:
1036
1043
  self._num_bytes_downloaded = 0
1037
1044
  chunker = ByteChunker(chunk_size=chunk_size)
1038
1045
 
1039
- with request_context(request=self._request):
1040
- async for raw_stream_bytes in self.stream:
1041
- self._num_bytes_downloaded += len(raw_stream_bytes)
1042
- for chunk in chunker.decode(raw_stream_bytes):
1043
- yield chunk
1044
-
1045
- for chunk in chunker.flush():
1046
- yield chunk
1046
+ stream = self.stream.__aiter__()
1047
+ try:
1048
+ with request_context(request=self._request):
1049
+ async for raw_stream_bytes in stream:
1050
+ self._num_bytes_downloaded += len(raw_stream_bytes)
1051
+ for chunk in chunker.decode(raw_stream_bytes):
1052
+ yield chunk
1047
1053
 
1048
- await self.aclose()
1054
+ for chunk in chunker.flush():
1055
+ yield chunk
1056
+ finally:
1057
+ if isinstance(stream, AsyncGenerator):
1058
+ await stream.aclose()
1059
+ await self.aclose()
1049
1060
 
1050
1061
  async def aclose(self) -> None:
1051
1062
  """
@@ -24,6 +24,8 @@ from ._utils import (
24
24
  _HTML5_FORM_ENCODING_REPLACEMENTS = {'"': "%22", "\\": "\\\\"}
25
25
  _HTML5_FORM_ENCODING_REPLACEMENTS.update({chr(c): f"%{c:02X}" for c in range(0x1F + 1) if c != 0x1B})
26
26
  _HTML5_FORM_ENCODING_RE = re.compile(r"|".join([re.escape(c) for c in _HTML5_FORM_ENCODING_REPLACEMENTS.keys()]))
27
+ _HEADER_NAME_RE = re.compile(r"[!#$%&'*+\-.^_`|~0-9A-Za-z]+")
28
+ _FORBIDDEN_HEADER_VALUE_CHARS_RE = re.compile(r"[\x00-\x08\x0a-\x1f\x7f]")
27
29
 
28
30
 
29
31
  def _format_form_param(name: str, value: str) -> bytes:
@@ -176,6 +178,10 @@ class FileField:
176
178
  filename = _format_form_param("filename", self.filename)
177
179
  parts.extend([b"; ", filename])
178
180
  for header_name, header_value in self.headers.items():
181
+ if _HEADER_NAME_RE.fullmatch(header_name) is None:
182
+ raise ValueError("Invalid multipart header name.")
183
+ if _FORBIDDEN_HEADER_VALUE_CHARS_RE.search(header_value) is not None:
184
+ raise ValueError("Invalid control character in multipart header value.")
179
185
  key, val = f"\r\n{header_name}: ".encode(), header_value.encode()
180
186
  parts.extend([key, val])
181
187
  parts.append(b"\r\n\r\n")
@@ -16,7 +16,7 @@ _DEPRECATED_CODES = {
16
16
 
17
17
 
18
18
  class _CodesMeta(EnumMeta):
19
- def __getattr__(cls, name: str) -> codes:
19
+ def __getattribute__(cls, name: str) -> object:
20
20
  if name in _DEPRECATED_CODES:
21
21
  new_name = _DEPRECATED_CODES[name]
22
22
  warnings.warn(
@@ -24,8 +24,7 @@ class _CodesMeta(EnumMeta):
24
24
  category=DeprecationWarning,
25
25
  stacklevel=2,
26
26
  )
27
- return cls[new_name]
28
- raise AttributeError(name)
27
+ return super().__getattribute__(name)
29
28
 
30
29
 
31
30
  class codes(IntEnum, metaclass=_CodesMeta):
@@ -178,7 +177,13 @@ class codes(IntEnum, metaclass=_CodesMeta):
178
177
  NOT_EXTENDED = 510, "Not Extended"
179
178
  NETWORK_AUTHENTICATION_REQUIRED = 511, "Network Authentication Required"
180
179
 
180
+ # Deprecated aliases for names replaced by RFC 9110.
181
+ REQUEST_ENTITY_TOO_LARGE = CONTENT_TOO_LARGE
182
+ REQUEST_URI_TOO_LONG = URI_TOO_LONG
183
+ REQUESTED_RANGE_NOT_SATISFIABLE = RANGE_NOT_SATISFIABLE
184
+ UNPROCESSABLE_ENTITY = UNPROCESSABLE_CONTENT
185
+
181
186
 
182
187
  # Include lower-case styles for `requests` compatibility.
183
- for code in codes:
184
- setattr(codes, code._name_.lower(), int(code))
188
+ for name, code in codes.__members__.items():
189
+ setattr(codes, name.lower(), int(code))
@@ -107,6 +107,8 @@ class WSGITransport(BaseTransport):
107
107
  "SERVER_PROTOCOL": "HTTP/1.1",
108
108
  "REMOTE_ADDR": self.remote_addr,
109
109
  }
110
+ if request.content and "Content-Length" not in request.headers:
111
+ environ["CONTENT_LENGTH"] = str(len(request.content))
110
112
  for header_key, header_value in request.headers.raw:
111
113
  key = header_key.decode("ascii").upper().replace("-", "_")
112
114
  if key not in ("CONTENT_TYPE", "CONTENT_LENGTH"):
@@ -1,7 +1,9 @@
1
1
  from __future__ import annotations
2
2
 
3
+ import ipaddress
3
4
  import sys
4
5
  import typing
6
+ from dataclasses import dataclass
5
7
  from urllib.parse import parse_qs, unquote, urlencode
6
8
 
7
9
  import idna
@@ -16,7 +18,7 @@ from ._types import QueryParamTypes
16
18
  from ._urlparse import urlparse
17
19
  from ._utils import primitive_value_to_str
18
20
 
19
- __all__ = ["URL", "QueryParams"]
21
+ __all__ = ["URL", "Origin", "QueryParams"]
20
22
 
21
23
 
22
24
  class URL:
@@ -328,6 +330,16 @@ class URL:
328
330
  """
329
331
  return not self.is_absolute_url
330
332
 
333
+ @property
334
+ def origin(self) -> Origin:
335
+ """
336
+ The URL origin as an immutable, hashable `Origin` value.
337
+
338
+ The origin consists of the URL scheme, host, and effective port.
339
+ User information, path, query, and fragment components are excluded.
340
+ """
341
+ return Origin(self)
342
+
331
343
  def copy_with(self, **kwargs: typing.Any) -> URL:
332
344
  """
333
345
  Copy this URL, returning a new URL with some components altered.
@@ -418,6 +430,51 @@ class URL:
418
430
  )
419
431
 
420
432
 
433
+ _ORIGIN_DEFAULT_PORTS = {
434
+ "ftp": 21,
435
+ "http": 80,
436
+ "https": 443,
437
+ "ws": 80,
438
+ "wss": 443,
439
+ }
440
+
441
+
442
+ @dataclass(frozen=True, slots=True, init=False)
443
+ class Origin:
444
+ """
445
+ The scheme, host, and effective port of a URL.
446
+
447
+ Origins are normalized, immutable, comparable, and hashable.
448
+
449
+ See RFC 9110, Section 4.3.1: https://www.rfc-editor.org/rfc/rfc9110.html#name-uri-origin
450
+ """
451
+
452
+ scheme: str
453
+ host: str
454
+ port: int | None
455
+
456
+ def __init__(self, url: URL | str) -> None:
457
+ if not isinstance(url, URL):
458
+ url = URL(url)
459
+
460
+ if url.is_relative_url:
461
+ raise ValueError("URL must be absolute to have an origin")
462
+
463
+ host = url.host
464
+ if b":" in url.raw_host:
465
+ # IPv6 addresses may have multiple equivalent string forms. Store
466
+ # their canonical compressed form so origin equality is numeric.
467
+ host = str(ipaddress.IPv6Address(url.raw_host.decode("ascii")))
468
+
469
+ port = url.port
470
+ if port is None:
471
+ port = _ORIGIN_DEFAULT_PORTS.get(url.scheme)
472
+
473
+ object.__setattr__(self, "scheme", url.scheme)
474
+ object.__setattr__(self, "host", host)
475
+ object.__setattr__(self, "port", port)
476
+
477
+
421
478
  class QueryParams(typing.Mapping[str, str]):
422
479
  """
423
480
  URL query parameters, as a multi-dict.
@@ -55,8 +55,8 @@ dependencies = [
55
55
 
56
56
  [project.optional-dependencies]
57
57
  brotli = [
58
- "brotli; platform_python_implementation == 'CPython'",
59
- "brotlicffi; platform_python_implementation != 'CPython'",
58
+ "brotli>=1.2.0; platform_python_implementation == 'CPython'",
59
+ "brotlicffi>=1.2.0.0; platform_python_implementation != 'CPython'",
60
60
  ]
61
61
  cli = [
62
62
  "click>=8.4.2",
@@ -74,7 +74,7 @@ ws = [
74
74
  ]
75
75
  # TODO(Marcelo): Remove when Python 3.13 reaches EOL.
76
76
  zstd = [
77
- "zstandard>=0.18.0; python_version <= '3.13'",
77
+ "backports.zstd>=1.0.0; python_version <= '3.13'",
78
78
  ]
79
79
 
80
80
  [project.scripts]
@@ -106,7 +106,7 @@ text = "\n## Release Information\n\n"
106
106
 
107
107
  [[tool.hatch.metadata.hooks.fancy-pypi-readme.fragments]]
108
108
  path = "CHANGELOG.md"
109
- pattern = "\n(###.+?\n)## "
109
+ pattern = "\n(## [0-9].+?)(?=\n## |$)"
110
110
 
111
111
  [[tool.hatch.metadata.hooks.fancy-pypi-readme.fragments]]
112
112
  text = "\n---\n\n[Full changelog](https://github.com/pydantic/httpx2/blob/main/src/httpx2/CHANGELOG.md)\n"
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes