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 support for WebSockets over HTTP/2 #2894

Merged
merged 6 commits into from
Oct 6, 2024
Merged

Conversation

SabrinaJewson
Copy link
Contributor

Motivation

WebSockets over HTTP/2 are currently not supported. It is defined in RFC 8441 (websockets over HTTP/3 are defined in RFC 9220 and it is exactly the same as HTTP/2).

Solution

Implement the changes necessary to support them, which just requires looking at the :protocol pseudo-header in the websocket extractor and advertising our support by enabling SETTINGS_ENABLE_CONNECT_PROTOCOL in HTTP/2.

Additionally, I needed to fix the handling of responses to CONNECT requests: Axum was incorrectly adding the Content-Length header to them (see also hyperium/hyper#3748), which was causing things to break.

Copy link
Member

@jplatte jplatte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a very high-quality PR, but I really don't know enough about websockets or HTTP/2 to review it.

@mladedav are you able to review this?

Also @SabrinaJewson would you be willing to maintain the websocket stuff going forward (be pinged on issues and PRs to help)?

@SabrinaJewson
Copy link
Contributor Author

I’m happy to be pinged!

Copy link
Collaborator

@yanns yanns left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm also no expert in ws and http2.
The example is running great! 💯

/// For HTTP/1.1 requests, this extractor requires the request method to be `GET`;
/// in later versions, `CONNECT` is used instead. Thus it should either be used
/// with [`any`](crate::routing::any), or placed behind
/// [`on`](crate::routing::on)`(`[`MethodFilter`]`::GET.or(`[`MethodFilter`]`::POST), ...)`.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we're using POST here because we don't support CONNECT directly?
Do you think that we should add CONNECT as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, that’s just a typo on my part…

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although I do now reälize that MethodFilter::CONNECT doesn’t exist. I pushed a commit to consistently use any instead.

return RouteFuture::from_future(route.clone().oneshot_inner($req))
.strip_body($method == Method::HEAD);
return route.clone().oneshot_inner($req);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that this is the same as #2897.
Do you think we should first merge #2897 to reduce the scope of this PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both PRs benefit from this change, so it can be merged in either order!

Copy link
Member

@jplatte jplatte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a changelog entry, please?

@SabrinaJewson
Copy link
Contributor Author

Done and done.

@jplatte jplatte merged commit 64e6eda into tokio-rs:main Oct 6, 2024
18 checks passed
@jplatte
Copy link
Member

jplatte commented Oct 6, 2024

Let me know if you want this backported (though maybe a follow-up PR adding MethodFilter::CONNECT would be good to have in first).

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