Skip to content

Implement Support for ALPN #85

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

Closed
wants to merge 1 commit into from
Closed

Implement Support for ALPN #85

wants to merge 1 commit into from

Conversation

CryZe
Copy link

@CryZe CryZe commented Dec 30, 2020

This adds support for ALPN via the newly released native-tls 0.2.7. With this hyper-tls can now automatically establish an HTTP2 connection with the server without having to force it from the client side.

This adds support for ALPN via the newly released native-tls 0.2.7. With
this hyper-tls can now automatically establish an HTTP2 connection with
the server without having to force it from the client side.
native-tls = "0.2.1"
hyper = { version = "0.14.2", default-features = false, features = ["tcp", "client"] }
native-tls = { version = "0.2.7", features = ["alpn"] }
hyper = { version = "0.14.2", default-features = false, features = ["tcp", "client", "http1", "http2"] }
Copy link
Author

Choose a reason for hiding this comment

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

This forces http1 and http2 back in. It's unclear to me how to proceed here. I believe we either have to always activate these or have to add new public features of hyper-tls, which if you forget to activate them, you are not getting any ALPN. Though we could make those features active by default.

Copy link
Member

Choose a reason for hiding this comment

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

Do we need to enable them in this crate? As long as we don't send ALPN by default, the user can opt-in after enabling the features.

Copy link
Author

Choose a reason for hiding this comment

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

Yeah we can definitely just not send ALPN by default. It could then either be a feature of this crate (which then probably should delegate it further) or be some kind of constructor / builder / configure method of the HttpsConnector (and hoping that the appropriate features are active for hyper). I'll leave that decision up to you before I proceed further.

Choose a reason for hiding this comment

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

I was thinking about this issue and the easiest implementation that comes to mind would be to have something like this in hyper:

/// Returns `true` if the `http2` feature is enabled
pub const fn http2_feature_enabled() -> bool {
    cfg!(feature = "http2")
}

The advantage is that enabling http2 in hyper would also enable it automatically in the TLS libraries. The downside is that it wouldn't be possible to disable the ALPN feature in hyper-tls. Also I've never seen something like this done in other crates so maybe it's a bad pattern to do in Rust?

let tls = s.get_ref();
if tls
.negotiated_alpn()
.ok()
Copy link
Author

Choose a reason for hiding this comment

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

The error is somewhat silently ignored here, probably not great.

Copy link
Member

Choose a reason for hiding this comment

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

Ignoring it here seems fine.

@@ -15,8 +15,8 @@ vendored = ["native-tls/vendored"]

[dependencies]
bytes = "1"
native-tls = "0.2.1"
hyper = { version = "0.14.2", default-features = false, features = ["tcp", "client"] }
native-tls = { version = "0.2.7", features = ["alpn"] }

Choose a reason for hiding this comment

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

Even if its enabled by default, ALPN support should be behind a feature, since old versions of the TLS backends don't support it.

Copy link
Member

Choose a reason for hiding this comment

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

Say more. You mean if built on a system with an old openssl/schannel/security-framework, it will probably fail with link errors of missing symbols?

Copy link

Choose a reason for hiding this comment

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

Yes

Copy link
Member

Choose a reason for hiding this comment

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

I'm curious how likely it is that installations would be too old. I'd really wish to make it default enabled in reqwest, so people can use h2 more frequently...

Copy link

Choose a reason for hiding this comment

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

It is specifically OpenSSL 1.0.1 (CentOS 6 for example), and macOS 10.12 and older.

Gelbpunkt added a commit to twilight-rs/twilight that referenced this pull request Feb 22, 2021
…round (#720)

This cleans up some TLS stuff to make it work in every scenario possible.

It effectively reverts #683 and replaces it with the http1 feature flag because hyper-tls is not currently in a state where we can use ALPN at all. Enabling http1 would cause it to always handle everything as http1 even when http2 ALPN is negotiated. For that we should keep an eye on hyperium/hyper-tls#85.

It also fixes #703 by adding features for webpki-roots called `rustls-webpki-roots` and renaming `rustls` to `rustls-native-roots` while keeping `rustls` as an alias to preserve backwards compatibility.

Approved-by: Cassandra McCarthy <[email protected]>
Approved-by: Valdemar Erk <[email protected]>
Merged-by: Jens Reidel <[email protected]>
Signed-off-by: Jens Reidel <[email protected]>
spring4175 pushed a commit to twilight-rs/embed-builder that referenced this pull request Jun 5, 2022
…round (#720)

This cleans up some TLS stuff to make it work in every scenario possible.

It effectively reverts #683 and replaces it with the http1 feature flag because hyper-tls is not currently in a state where we can use ALPN at all. Enabling http1 would cause it to always handle everything as http1 even when http2 ALPN is negotiated. For that we should keep an eye on hyperium/hyper-tls#85.

It also fixes #703 by adding features for webpki-roots called `rustls-webpki-roots` and renaming `rustls` to `rustls-native-roots` while keeping `rustls` as an alias to preserve backwards compatibility.

Approved-by: Cassandra McCarthy <[email protected]>
Approved-by: Valdemar Erk <[email protected]>
Merged-by: Jens Reidel <[email protected]>
Signed-off-by: Jens Reidel <[email protected]>
@Enter-tainer
Copy link

Hi! Is there any update on this?

I'm trying to use HTTP/2 in reqwest and I found the issue seanmonstar/reqwest#857. It says that the ALPN support only works in rustls, so we can only make HTTP/2 requests with rustls. Doing this requires manually enabling the rustls-tls feature flag and adding use_rustls_tls to ClientBuilder.

IMHO this PR adds ALPN support for the native tls, which is the default tls backend of reqwest. So if this is merged, I guess we can make reqwestsupport HTTP/2 without extra configuration.

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.

5 participants