Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
86 commits
Select commit Hold shift + click to select a range
4badcd8
Add support for HTTP/2
Moist-Cat Jul 2, 2026
8d6ffe2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 2, 2026
1b21c5b
Merge branch 'master' into master
Moist-Cat Jul 4, 2026
982ad3a
Lint code and add test
Moist-Cat Jul 4, 2026
2ecae5a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 4, 2026
73bfa16
Fix lint errors
Moist-Cat Jul 4, 2026
9bd6ff9
Add more tests.
Moist-Cat Jul 5, 2026
91b46d8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 5, 2026
0b361d3
Fix type issues
Moist-Cat Jul 5, 2026
9ab172f
Fix protocol violations and perform functional tests
Moist-Cat Jul 5, 2026
38b9d3d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 5, 2026
538164e
More tests
Moist-Cat Jul 5, 2026
2ce9b41
Misc changes
Moist-Cat Jul 6, 2026
fdb10b7
Merge branch 'aio-libs:master' into master
Moist-Cat Jul 8, 2026
e312859
Merge branch 'aio-libs:master' into master
Moist-Cat Jul 14, 2026
93aef89
Release connection in the HTTP/2 path
Moist-Cat Jul 8, 2026
23997c4
Merge branch 'master' into master
Moist-Cat Jul 27, 2026
d78d47a
Merge branch 'aio-libs:master' into master
Moist-Cat Jul 28, 2026
35eb119
Merge branch 'aio-libs:master' into master
Moist-Cat Aug 3, 2026
742899f
Merge branch 'aio-libs:master' into master
Moist-Cat Aug 4, 2026
44de092
Merge branch 'master' into master
Moist-Cat Aug 6, 2026
0f9f4d8
Merge branch 'master' into master
Moist-Cat Aug 10, 2026
979bb4a
Merge branch 'aio-libs:master' into master
Moist-Cat Aug 11, 2026
51a5ee2
Update response with base class
Moist-Cat Jul 15, 2026
1d10b19
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 11, 2026
519c830
Merge branch 'aio-libs:master' into master
Moist-Cat Aug 12, 2026
44811d3
Merge branch 'aio-libs:master' into master
Moist-Cat Aug 13, 2026
d242ad7
Merge branch 'master' into master
Moist-Cat Aug 15, 2026
5f57683
Merge branch 'aio-libs:master' into master
Moist-Cat Aug 20, 2026
d14a1c7
First working version after changes
Moist-Cat Aug 20, 2026
314a6e5
Remove unneeded response file.
Moist-Cat Aug 20, 2026
38c11ff
Fix type errors
Moist-Cat Aug 20, 2026
899742e
Merge branch 'aio-libs:master' into master
Moist-Cat Aug 21, 2026
2c0493a
Fix type errors and compatibility issues with py < 3.14
Moist-Cat Aug 21, 2026
088eb38
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 21, 2026
d40dc83
Merge branch 'master' into master
Moist-Cat Aug 25, 2026
1028739
Merge branch 'master' into master
Moist-Cat Aug 27, 2026
bc9eb29
Add tests for new code paths
Moist-Cat Aug 27, 2026
d2941d2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 27, 2026
d8dc63a
Merge branch 'master' into master
Moist-Cat Aug 28, 2026
8fdf2e4
Merge branch 'master' into master
Moist-Cat Aug 29, 2026
af86d82
Merge branch 'master' into master
Moist-Cat Aug 31, 2026
e70d672
Merge branch 'master' into master
Moist-Cat Aug 31, 2026
5f1dc50
Merge branch 'master' into master
Moist-Cat Sep 1, 2026
1f1b8db
Add new synchronization mechanism and extra tests
Moist-Cat Sep 1, 2026
9f9d127
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 1, 2026
a5124c0
flake8
Moist-Cat Sep 2, 2026
7e8daa3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 2, 2026
bce855d
Bypass proxy class
Moist-Cat Sep 2, 2026
b76be6c
Merge branch 'aio-libs:master' into master
Moist-Cat Sep 2, 2026
263ea5a
Merge branch 'master' into master
Moist-Cat Sep 3, 2026
aa3d6f7
Set exception on protocol error/GOAWAY to payload
Moist-Cat Sep 4, 2026
d8cb468
Merge branch 'master' into master
Moist-Cat Sep 4, 2026
1b7f7c1
Merge branch 'master' into master
Moist-Cat Sep 4, 2026
3ae99c9
Cover additional edge cases
Moist-Cat Sep 5, 2026
4eace7e
Fix tests
Moist-Cat Sep 5, 2026
e7a205a
Merge branch 'aio-libs:master' into master
Moist-Cat Sep 5, 2026
ff6f1f5
Misc fixes
Moist-Cat Sep 6, 2026
00e8aaa
Add opt-in parameter
Moist-Cat Sep 6, 2026
c014585
Add news entry.
Moist-Cat Sep 6, 2026
8b52301
Add docs; Misc fixes
Moist-Cat Sep 6, 2026
8c1316e
Add hapack to the imports used in CI/CD; Improve error handling
Moist-Cat Sep 19, 2026
d92b6d9
Complete comment
Moist-Cat Sep 19, 2026
8bc087f
Merge branch 'master' into master
Moist-Cat Sep 19, 2026
e6c0104
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 19, 2026
6a237a3
Merge branch 'master' into master
Moist-Cat Oct 11, 2026
86ae832
Add fuzzers; misc fixes
Moist-Cat Oct 11, 2026
111eb99
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 11, 2026
5d05621
Merge branch 'aio-libs:master' into master
Moist-Cat Oct 11, 2026
a63237d
Merge branch 'master' into master
Moist-Cat Nov 8, 2026
8e849f0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 8, 2026
285b1d3
Add new fuzzers
Moist-Cat Nov 8, 2026
83e975b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 8, 2026
312ba64
Fix formatting issues
Moist-Cat Nov 8, 2026
2f47164
Merge branch 'aio-libs:master' into master
Moist-Cat Nov 14, 2026
7e19d7c
Isolate stream logic inside connector.Connection
Moist-Cat Nov 14, 2026
366c442
Merge branch 'aio-libs:master' into master
Moist-Cat Nov 27, 2026
4d1182e
Extend connector
Moist-Cat Nov 28, 2026
8f82fe7
Merge branch 'aio-libs:master' into master
Moist-Cat Dec 1, 2026
79f86f0
Merge branch 'aio-libs:master' into master
Moist-Cat Dec 2, 2026
2c3a5ec
Fix type errors
Moist-Cat Dec 2, 2026
16e1ada
Flake8 fixes
Moist-Cat Dec 2, 2026
07d933b
Harden tests
Moist-Cat Dec 2, 2026
0d12d1e
Merge branch 'aio-libs:master' into master
Moist-Cat Dec 2, 2026
9e737c3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 2, 2026
8418f19
Merge branch 'master' into master
Moist-Cat Dec 2, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,6 @@ ignore_missing_imports = True

[mypy-gunicorn.*]
ignore_missing_imports = True

[mypy-hpack]
ignore_missing_imports = True
1 change: 1 addition & 0 deletions CHANGES/13039.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added experimental support for ``HTTP/2``. It can be enabled via the ``http2_enabled`` parameter of the :class:`~aiohttp.ClientSession` -- by :user:`Moist-Cat`.
4 changes: 4 additions & 0 deletions aiohttp/base_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class BaseProtocol(asyncio.Protocol):
"_buffer_paused",
"_upgraded",
"transport",
"version",
)

def __init__(
Expand All @@ -42,6 +43,9 @@ def __init__(
self._upgraded = False

self.transport: asyncio.Transport | None = None
# proto version
# we can't set the default due to a circular import
self.version: Any = None

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it for HttpVersion? Move the class to some helper file if needed, but please avoid Any.


@property
def connected(self) -> bool:
Expand Down
46 changes: 34 additions & 12 deletions aiohttp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,25 +282,42 @@ class _SessionOptions(TypedDict, total=False):
async def _connect_and_send_request(req: ClientRequest) -> ClientResponse:
connector = req._session._connector
assert connector is not None
key = req.connection_key

if (tracker := req._upload_tracker) is not None:
req._upload_gen = tracker._attempt_started()

try:
conn = await connector.connect(req, traces=req._traces, timeout=req._timeout)
if connector.http2_enabled:
await connector.semaphore.acquire(key)
# only the first connection to a host blocks
# the rest connect concurrently
try:
conn = await connector.connect(
req, traces=req._traces, timeout=req._timeout
)
finally:
if connector.http2_enabled:
connector.semaphore.release(key)
except TimeoutError as exc:
raise ConnectionTimeoutError(f"Connection timeout to host {req.url}") from exc

assert conn.protocol is not None
conn.protocol.set_response_params(**req._response_params)
assert conn.protocol.transport is not None

resp = None
started = False

try:
resp = await req._send(conn)
try:
await resp.start(conn)
except BaseException:
resp = await conn.send_request(req)
await resp.start(conn)
started = True
finally:
if resp is not None and not started:
resp.close()
raise
except BaseException:
conn.close()
raise
conn.close()
if resp is None:
conn.close()
return resp


Expand Down Expand Up @@ -372,6 +389,7 @@ def __init__(
fallback_charset_resolver: _CharsetResolver = lambda r, b: "utf-8",
middlewares: Sequence[ClientMiddlewareType] = (),
ssl_shutdown_timeout: _SENTINEL | None | float = sentinel,
http2_enabled: bool = False,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Update the threat model

Adding the public ClientSession(http2_enabled=...) API introduces HTTP/2 framing, multiplexing, flow-control, decompression, and resource-exhaustion risks, but THREAT_MODEL.md contains no HTTP/2 client coverage. The repository requires the threat model to be revised when a public API is added in client.py; this repository requirement must be satisfied before merging.

Context Used: AGENTS.md (source)

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

) -> None:
# We initialise _connector to None immediately, as it's referenced in __del__()
# and could cause issues if an exception occurs during initialisation.
Expand Down Expand Up @@ -411,7 +429,9 @@ def __init__(
)

if connector is None:
connector = TCPConnector(ssl_shutdown_timeout=ssl_shutdown_timeout)
connector = TCPConnector(
ssl_shutdown_timeout=ssl_shutdown_timeout, http2_enabled=http2_enabled
)
# Initialize these three attrs before raising any exception,
# they are used in __del__
self._connector = connector
Expand Down Expand Up @@ -1673,7 +1693,9 @@ def request(
connector_owner = False
if connector is None:
connector_owner = True
connector = TCPConnector(force_close=True)
# _request doesn't accept http2_enabled
http2_enabled = kwargs.pop("http2_enabled", False)
connector = TCPConnector(force_close=True, http2_enabled=http2_enabled)

session = ClientSession(
cookies=kwargs.pop("cookies", None),
Expand Down
3 changes: 3 additions & 0 deletions aiohttp/client_proto.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
)
from .http import HttpResponseParser, RawResponseMessage, WebSocketReader
from .http_exceptions import HttpProcessingError
from .http_writer import HttpVersion11
from .streams import EMPTY_PAYLOAD, DataQueue, StreamReader


Expand Down Expand Up @@ -57,6 +58,8 @@ def __init__(self, loop: asyncio.AbstractEventLoop) -> None:
self._closed: None | asyncio.Future[None] = None
self._connection_lost_called = False

self.version = HttpVersion11

@property
def closed(self) -> None | asyncio.Future[None]:
"""Future that is set when the connection is closed.
Expand Down
39 changes: 29 additions & 10 deletions aiohttp/client_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
HttpVersion11,
StreamWriter,
)
from .http2.adapter import Http2StreamWriter
from .streams import EMPTY_PAYLOAD, StreamReader
from .typedefs import DEFAULT_JSON_DECODER, JSONDecoder, RawHeaders

Expand Down Expand Up @@ -711,7 +712,12 @@ async def start(self, connection: "Connection") -> "ClientResponse":
# read response
try:
protocol = self._protocol
message, payload = await protocol.read() # type: ignore[union-attr]
assert protocol is not None
if hasattr(protocol, "version") and protocol.version.major == 2:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better ensure that all protocols have version attribute.

message, payload = await connection.read_response()
else:
# backwards compatibility
message, payload = await protocol.read()
except HttpProcessingError as exc:
raise ClientResponseError(
self.request_info,
Expand Down Expand Up @@ -1113,8 +1119,10 @@ def _create_response(
stream_writer=stream_writer,
)

def _create_writer(self, protocol: BaseProtocol) -> StreamWriter:
return StreamWriter(protocol, self.loop)
def _create_writer(
self, connection: "Connection"
) -> StreamWriter | Http2StreamWriter:
return connection.create_writer()

def _should_write(self, protocol: BaseProtocol) -> bool:
return protocol.writing_paused
Expand All @@ -1133,9 +1141,7 @@ async def _send(self, conn: "Connection") -> ClientResponse:
else:
path = self.url.raw_path_qs

protocol = conn.protocol
assert protocol is not None
writer = self._create_writer(protocol)
writer = self._create_writer(conn)

# set default content-type
if (
Expand Down Expand Up @@ -1163,6 +1169,8 @@ async def _send(self, conn: "Connection") -> ClientResponse:
await writer.write_headers(status_line, self.headers)

task: asyncio.Task[None] | None
assert conn.protocol is not None
protocol = conn.protocol
if self._should_write(protocol):
coro = self._write_bytes(writer, conn, self._get_content_length())
if sys.version_info >= (3, 14):
Expand Down Expand Up @@ -1613,10 +1621,10 @@ def _create_response(
stream_writer=stream_writer,
)

def _create_writer(self, protocol: BaseProtocol) -> StreamWriter:
writer = StreamWriter(
protocol,
self.loop,
def _create_writer(
self, connection: "Connection"
) -> StreamWriter | Http2StreamWriter:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Return type looks cumbersome. Could you declare that an abstract writer is returned without using a union type?

writer = connection.create_writer(
on_chunk_sent=(
functools.partial(self._on_chunk_request_sent, self.method, self.url)
if self._traces
Expand All @@ -1634,6 +1642,17 @@ def _create_writer(self, protocol: BaseProtocol) -> StreamWriter:

if self.chunked is not None:
writer.enable_chunking()

# patch for http/2
if (
hasattr(connection.protocol, "version")
and connection.protocol is not None
and connection.protocol.version.major == 2
):
# we can get this from the status line
# but it's less convenient
writer.url = self.url # type: ignore[union-attr]
writer.method = self.method # type: ignore[union-attr]
return writer

def _should_write(self, protocol: BaseProtocol) -> bool:
Expand Down
Loading
Loading