Skip to content

Commit

Permalink
build(http): Disable http1 usage
Browse files Browse the repository at this point in the history
When HTTP/1.1 and H2 are both enabled, like previously,
Cloudflare/Discord would always prefer H2. These changes
ensure we always negotiate H2 in the ALPN, which allows us
to completely disable HTTP/1.1 support at build time where
possible.

Signed-off-by: Jens Reidel <[email protected]>
  • Loading branch information
Gelbpunkt committed Dec 29, 2023
1 parent a336f93 commit aaefcb9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
9 changes: 5 additions & 4 deletions twilight-http/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ fastrand = { default-features = false, features = ["std"], version = "2" }
http = { default-features = false, version = "1" }
http-body-util = { default-features = false, version = "0.1" }
hyper = { default-features = false, version = "1" }
hyper-util = { default-features = false, features = ["client-legacy", "http1", "http2", "tokio"], version = "0.1.2" }
hyper-rustls = { default-features = false, optional = true, features = ["http1", "http2", "ring"], version = "0.25" }
hyper-tls = { default-features = false, optional = true, version = "0.6" }
hyper-util = { default-features = false, features = ["client-legacy", "http2", "tokio"], version = "0.1.2" }
hyper-rustls = { default-features = false, optional = true, features = ["http2", "ring"], version = "0.25" }
hyper-tls = { default-features = false, optional = true, features = ["alpn"], version = "0.6" }
hyper-hickory = { default-features = false, optional = true, features = ["tokio"], version = "0.6" }
native-tls = { default-features = false, features = ["alpn"], optional = true, version = "0.2.7" }
percent-encoding = { default-features = false, version = "2" }
serde = { default-features = false, features = ["derive"], version = "1" }
serde_json = { default-features = false, features = ["std"], version = "1" }
Expand All @@ -38,7 +39,7 @@ simd-json = { default-features = false, features = ["serde_impl", "swar-number-p
[features]
default = ["decompression", "rustls-native-roots"]
decompression = ["dep:brotli"]
native = ["dep:hyper-tls"]
native = ["dep:hyper-tls", "dep:native-tls"]
rustls-native-roots = ["dep:hyper-rustls", "hyper-rustls?/native-tokio"]
rustls-webpki-roots = ["dep:hyper-rustls", "hyper-rustls?/webpki-tokio"]
hickory = ["dep:hyper-hickory"]
Expand Down
12 changes: 9 additions & 3 deletions twilight-http/src/client/connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,28 @@ pub fn create() -> Connector {
.with_native_roots()
.expect("no native root certificates found")
.https_or_http()
.enable_http1()
.enable_http2()
.wrap_connector(connector);
#[cfg(all(feature = "rustls-webpki-roots", not(feature = "rustls-native-roots")))]
let connector = hyper_rustls::HttpsConnectorBuilder::new()
.with_webpki_roots()
.https_or_http()
.enable_http1()
.enable_http2()
.wrap_connector(connector);
#[cfg(all(
feature = "native",
not(feature = "rustls-native-roots"),
not(feature = "rustls-webpki-roots")
))]
let connector = hyper_tls::HttpsConnector::new_with_connector(connector);
let connector = {
// While hyper-tls has support for handling negotiated ALPN,
// it does not send the ALPNs in the client hello out of the box
let tls = native_tls::TlsConnector::builder()
.request_alpns(&["h2"])
.build()
.expect("TlsConnector::new() failure");
hyper_tls::HttpsConnector::from((connector, tls.into()))
};

connector
}

0 comments on commit aaefcb9

Please sign in to comment.