Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "trailing_headers" extension for HTTP/2 #912

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
16 changes: 11 additions & 5 deletions httpcore/_async/http2.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,16 @@ async def handle_async_request(self, request: Request) -> Response:
)
trace.return_value = (status, headers)

trailing_headers: typing.List[typing.Tuple[bytes, bytes]] = []
return Response(
status=status,
headers=headers,
content=HTTP2ConnectionByteStream(self, request, stream_id=stream_id),
content=HTTP2ConnectionByteStream(self, request, stream_id=stream_id, trailing_headers=trailing_headers),
extensions={
"http_version": b"HTTP/2",
"network_stream": self._network_stream,
"stream_id": stream_id,
"trailing_headers": trailing_headers,
},
)
except BaseException as exc: # noqa: PIE786
Expand Down Expand Up @@ -304,7 +306,7 @@ async def _receive_response(
return (status_code, headers)

async def _receive_response_body(
self, request: Request, stream_id: int
self, request: Request, stream_id: int, trailing_headers: typing.List[typing.Tuple[bytes, bytes]]
) -> typing.AsyncIterator[bytes]:
"""
Iterator that returns the bytes of the response body for a given stream ID.
Expand All @@ -316,6 +318,8 @@ async def _receive_response_body(
self._h2_state.acknowledge_received_data(amount, stream_id)
await self._write_outgoing_data(request)
yield event.data
elif isinstance(event, h2.events.TrailersReceived):
trailing_headers.extend(event.headers)
elif isinstance(event, h2.events.StreamEnded):
break

Expand Down Expand Up @@ -372,6 +376,7 @@ async def _receive_events(
(
h2.events.ResponseReceived,
h2.events.DataReceived,
h2.events.TrailersReceived,
h2.events.StreamEnded,
h2.events.StreamReset,
),
Expand Down Expand Up @@ -558,19 +563,20 @@ async def __aexit__(

class HTTP2ConnectionByteStream:
def __init__(
self, connection: AsyncHTTP2Connection, request: Request, stream_id: int
self, connection: AsyncHTTP2Connection, request: Request, stream_id: int, trailing_headers: typing.List[typing.Tuple[bytes, bytes]]
) -> None:
self._connection = connection
self._request = request
self._stream_id = stream_id
self._trailing_headers = trailing_headers
self._closed = False

async def __aiter__(self) -> typing.AsyncIterator[bytes]:
kwargs = {"request": self._request, "stream_id": self._stream_id}
kwargs = {"request": self._request, "stream_id": self._stream_id, "trailing_headers": self._trailing_headers}
try:
async with Trace("receive_response_body", logger, self._request, kwargs):
async for chunk in self._connection._receive_response_body(
request=self._request, stream_id=self._stream_id
request=self._request, stream_id=self._stream_id, trailing_headers=self._trailing_headers
):
yield chunk
except BaseException as exc:
Expand Down
16 changes: 11 additions & 5 deletions httpcore/_sync/http2.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,16 @@ def handle_request(self, request: Request) -> Response:
)
trace.return_value = (status, headers)

trailing_headers: typing.List[typing.Tuple[bytes, bytes]] = []
return Response(
status=status,
headers=headers,
content=HTTP2ConnectionByteStream(self, request, stream_id=stream_id),
content=HTTP2ConnectionByteStream(self, request, stream_id=stream_id, trailing_headers=trailing_headers),
extensions={
"http_version": b"HTTP/2",
"network_stream": self._network_stream,
"stream_id": stream_id,
"trailing_headers": trailing_headers,
},
)
except BaseException as exc: # noqa: PIE786
Expand Down Expand Up @@ -304,7 +306,7 @@ def _receive_response(
return (status_code, headers)

def _receive_response_body(
self, request: Request, stream_id: int
self, request: Request, stream_id: int, trailing_headers: typing.List[typing.Tuple[bytes, bytes]]
) -> typing.Iterator[bytes]:
"""
Iterator that returns the bytes of the response body for a given stream ID.
Expand All @@ -316,6 +318,8 @@ def _receive_response_body(
self._h2_state.acknowledge_received_data(amount, stream_id)
self._write_outgoing_data(request)
yield event.data
elif isinstance(event, h2.events.TrailersReceived):
trailing_headers.extend(event.headers)
elif isinstance(event, h2.events.StreamEnded):
break

Expand Down Expand Up @@ -372,6 +376,7 @@ def _receive_events(
(
h2.events.ResponseReceived,
h2.events.DataReceived,
h2.events.TrailersReceived,
h2.events.StreamEnded,
h2.events.StreamReset,
),
Expand Down Expand Up @@ -558,19 +563,20 @@ def __exit__(

class HTTP2ConnectionByteStream:
def __init__(
self, connection: HTTP2Connection, request: Request, stream_id: int
self, connection: HTTP2Connection, request: Request, stream_id: int, trailing_headers: typing.List[typing.Tuple[bytes, bytes]]
) -> None:
self._connection = connection
self._request = request
self._stream_id = stream_id
self._trailing_headers = trailing_headers
self._closed = False

def __iter__(self) -> typing.Iterator[bytes]:
kwargs = {"request": self._request, "stream_id": self._stream_id}
kwargs = {"request": self._request, "stream_id": self._stream_id, "trailing_headers": self._trailing_headers}
try:
with Trace("receive_response_body", logger, self._request, kwargs):
for chunk in self._connection._receive_response_body(
request=self._request, stream_id=self._stream_id
request=self._request, stream_id=self._stream_id, trailing_headers=self._trailing_headers
):
yield chunk
except BaseException as exc:
Expand Down
Loading