From 4dcdf3457d9e5c51ea6fd7ed75103b5799a98239 Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Sun, 9 Aug 2026 10:03:57 +0200 Subject: [PATCH] Avoid quadratic copying when sending large HTTP/2 request bodies --- src/httpcore2/httpcore2/_async/http2.py | 7 ++++--- src/httpcore2/httpcore2/_sync/http2.py | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/httpcore2/httpcore2/_async/http2.py b/src/httpcore2/httpcore2/_async/http2.py index 0022a2b6..6f009c13 100644 --- a/src/httpcore2/httpcore2/_async/http2.py +++ b/src/httpcore2/httpcore2/_async/http2.py @@ -246,10 +246,11 @@ async def _send_stream_data(self, request: Request, stream_id: int, data: bytes) """ Send a single chunk of data in one or more data frames. """ - while data: + position = 0 + while position < len(data): max_flow = await self._wait_for_outgoing_flow(request, stream_id) - chunk_size = min(len(data), max_flow) - chunk, data = data[:chunk_size], data[chunk_size:] + chunk = data[position : position + max_flow] + position += len(chunk) self._h2_state.send_data(stream_id, chunk) await self._write_outgoing_data(request) diff --git a/src/httpcore2/httpcore2/_sync/http2.py b/src/httpcore2/httpcore2/_sync/http2.py index 3b79af8c..ee314c8a 100644 --- a/src/httpcore2/httpcore2/_sync/http2.py +++ b/src/httpcore2/httpcore2/_sync/http2.py @@ -246,10 +246,11 @@ def _send_stream_data(self, request: Request, stream_id: int, data: bytes) -> No """ Send a single chunk of data in one or more data frames. """ - while data: + position = 0 + while position < len(data): max_flow = self._wait_for_outgoing_flow(request, stream_id) - chunk_size = min(len(data), max_flow) - chunk, data = data[:chunk_size], data[chunk_size:] + chunk = data[position : position + max_flow] + position += len(chunk) self._h2_state.send_data(stream_id, chunk) self._write_outgoing_data(request)