Skip to content

Implements "trailing_headers" for HTTP/2 #1012

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

tsubakiky
Copy link

@tsubakiky tsubakiky commented May 1, 2025

Summary

HTTP/2 Trailing Headers Support

This PR adds support for HTTP/2 trailing headers in httpcore. Trailing headers are now properly received, processed, and made available in the response extensions.

Background:
I’m currently developing a Connect-protocol–compliant library that doesn’t depend directly on grpcio. I’ve implemented the client with httpcore, but because I needed to receive trailer headers in HTTP/2 streaming, I introduced the following modifications.

https://connectrpc.com/docs/protocol

Changes

  • Added support for processing h2.events.TrailersReceived events
  • Store trailing headers per stream ID, filtering out pseudo-headers
  • Expose trailing headers in response extensions as trailing_headers
  • Improved response body streaming to handle trailing headers that arrive after headers but before end of stream
  • Added comprehensive test suite for trailing headers functionality

Implementation Details

  • Modified HTTP2ConnectionByteStream to update response extensions when trailing headers are received
  • Added reference tracking between response objects and their byte streams to ensure trailing headers can be properly attached
  • Ensured proper cleanup of trailing headers when responses are closed
  • Maintained consistent implementation between sync and async versions

This implementation safely handles HTTP/2 trailing headers according to the HTTP/2 specification, ensuring all header data is correctly accessible to clients.

Checklist

  • I understand that this PR may be closed in case there was no previous discussion. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.

@tsubakiky tsubakiky force-pushed the support-trailer-headers-for-http2 branch 4 times, most recently from 30a4ae9 to e70c821 Compare May 1, 2025 21:23
@tsubakiky tsubakiky force-pushed the support-trailer-headers-for-http2 branch from e70c821 to 3dce1d3 Compare May 1, 2025 22:50
@zchee
Copy link

zchee commented May 7, 2025

@tomchristie @graingert Sorry for the noisy, but gentle ping.

@tomchristie
Copy link
Member

Thanks. Given the overhead this functionality looks like an unwin for most users.

@tsubakiky
Copy link
Author

@tomchristie
Thank you for your feedback.

How about I modify the implementation so that the client only processes response trailers when the request includes the header {"TE": "trailers"}? That way, there should be no additional overhead in the typical case.

I noticed you tried a similar approach before in this PR:
#582

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants