Skip to content

httpcore2: prefer h2 for ALPN protocol if requesting HTTP/2 - #1155

Open
benjarobin wants to merge 1 commit into
pydantic:mainfrom
benjarobin:fix-alpn-protocol
Open

httpcore2: prefer h2 for ALPN protocol if requesting HTTP/2#1155
benjarobin wants to merge 1 commit into
pydantic:mainfrom
benjarobin:fix-alpn-protocol

Conversation

@benjarobin

@benjarobin benjarobin commented Aug 21, 2026

Copy link
Copy Markdown

Summary

When building the list of ALPN protocols for negotiation in the SSL context:

  • If HTTP/2 is requested, prioritize "h2" first. This is critical, as some servers (e.g., lighttpd) select the first protocol in the list.
  • If HTTP/1.1 is not disabled, include "http/1.1". If HTTP/1.1 was disabled we do not want to advertise that, otherwise the server may select HTTP/1.1 as protocol in the SSL context, but the client (httpx2) is still going to send the request using HTTP/2, which is going to confuse the server.
  • If both HTTP/2 and HTTP/1.1 are disabled, still include "http/1.1" as a fallback.

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 was not able to find the test infrastructure to test against an server with TLS and HTTP/2.
    - Maybe somebody could point me in the right direction.
  • I've updated the documentation accordingly.
    - It does not seem necessary to update the documentation.

I will try to add test and to update the documentation if nobody is against this change :)

Review in cubic

@codspeed-hq

codspeed-hq Bot commented Aug 21, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 17 untouched benchmarks
⏩ 7 skipped benchmarks1


Comparing benjarobin:fix-alpn-protocol (368ff61) with main (5465b4e)

Open in CodSpeed

Footnotes

  1. 7 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@cubic-dev-ai cubic-dev-ai Bot left a comment

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.

No issues found across 6 files

Re-trigger cubic

@benjarobin
benjarobin force-pushed the fix-alpn-protocol branch 4 times, most recently from 88e6a5c to 25a9d14 Compare August 22, 2026 07:30
When building the list of ALPN protocols for negotiation in the SSL
context:
- If HTTP/2 is requested, prioritize "h2" first. This is critical, as some
  servers (e.g., lighttpd) select the first protocol in the list.
- If HTTP/1.1 is not disabled, include "http/1.1". If HTTP/1.1 was disabled
  we do not want to advertise that, otherwise the server may select
  HTTP/1.1 as protocol in the SSL context, but the client (httpx2) is still
  going to send the request using HTTP/2, which is going to confuse the
  server.
- If both HTTP/2 and HTTP/1.1 are disabled, still include "http/1.1" as a
  fallback.

Signed-off-by: Benjamin Robin <benjamin.robin@bootlin.com>
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.

1 participant