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

How to force enable HTTP/2.0 #3261

Closed
ArkadyRudenko opened this issue Aug 8, 2024 · 5 comments
Closed

How to force enable HTTP/2.0 #3261

ArkadyRudenko opened this issue Aug 8, 2024 · 5 comments

Comments

@ArkadyRudenko
Copy link

I have an HTTP/2.0 server and I want to ping/test it using an httpx client.
I use this docs: https://www.python-httpx.org/http2/

And the client send this request

GET /hello HTTP/1.1\r\nHost: localhost:8080\r\nAccept: */*\r\nAccept-Encoding: gzip, deflate\r\nConnection: keep-alive\r\nUser-Agent: python-httpx/0.27.0\r\n\r\n

The server needs a HTTP2-Settings header to send a upgrade response ("RFC: A request that upgrades from HTTP/1.1 to HTTP/2 MUST include exactly one "HTTP2-Settings" header field")

See RFC: https://datatracker.ietf.org/doc/html/rfc7540#section-3.2 and https://datatracker.ietf.org/doc/html/rfc7540#section-3.2.1

If the server responds with 101 Switching Protocols the client is not ready for this.
Question: How can my server upgrade httpx client to HTTP/2.0?

My client:

import httpx
import asyncio


async def main():
    async with httpx.AsyncClient(http2=True) as client:
        r = await client.get("http://localhost:8080/hello")
        print(r.http_version)


asyncio.run(main())

@tomchristie
Copy link
Member

http1=False, http2=True will use force HTTP/2 (prior knowledge), rather than using negotiation.

It looks like this is missing from our docs at the moment... https://www.python-httpx.org/http2/

(Pull requests welcome)

@ArkadyRudenko
Copy link
Author

Thaanks! It's working now

pr: #3262

@rafalkrupinski
Copy link

Does it mean passing http2=True (with http1 being True by default) doesn't do anything?

@tomchristie
Copy link
Member

tomchristie commented Aug 14, 2024

@rafalkrupinski - No... I've added a clarifying comment against the pull request.

  • The default is HTTP/1.1.
  • Enabling http2=True switches to either HTTP/1.1 or HTTP/2. Matching standard web-browser behaviours.
  • Using http1=False, http2=True forces HTTP/2 to be used. ("prior knowledge")

On reflection I think that we ought to be noting in the documentation that HTTP/2 is typically only supported for https:, not for http:.

@rafalkrupinski
Copy link

So OP was expecting httpx would initiate the upgrade with HTTP headers, while it actually negotiates it over alpn. thanks!

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

No branches or pull requests

3 participants