Skip to content

Commit

Permalink
Connector: enable_all_versions with matching alpn vs features (rust…
Browse files Browse the repository at this point in the history
  • Loading branch information
TimLuq authored and wquark committed Dec 12, 2024
1 parent 1c8811a commit 73dbde1
Showing 1 changed file with 44 additions and 7 deletions.
51 changes: 44 additions & 7 deletions src/connector/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,16 +174,22 @@ impl ConnectorBuilder<WantsProtocols1> {
})
}

/// Enable all HTTP versions
/// Enable all HTTP versions built into this library (enabled with Cargo features)
///
/// For now, this enables both HTTP 1 and 2. In the future, other supported versions
/// will be enabled as well.
#[cfg(all(feature = "http1", feature = "http2"))]
/// For now, this could enable both HTTP 1 and 2, depending on active features.
/// In the future, other supported versions will be enabled as well.
#[cfg(feature = "http2")]
#[cfg_attr(docsrs, doc(cfg(feature = "http2")))]
pub fn enable_all_versions(mut self) -> ConnectorBuilder<WantsProtocols3> {
self.0.tls_config.alpn_protocols = vec![b"h2".to_vec()];
#[cfg(feature = "http1")]
let alpn_protocols = vec![b"h2".to_vec(), b"http/1.1".to_vec()];
#[cfg(not(feature = "http1"))]
let alpn_protocols = vec![b"h2".to_vec()];

self.0.tls_config.alpn_protocols = alpn_protocols;
ConnectorBuilder(WantsProtocols3 {
inner: self.0,
enable_http1: true,
enable_http1: cfg!(feature = "http1"),
})
}

Expand Down Expand Up @@ -326,7 +332,7 @@ mod tests {
.build();
assert_eq!(&connector.tls_config.alpn_protocols, &[b"h2".to_vec()]);
let connector = super::ConnectorBuilder::new()
.with_tls_config(tls_config)
.with_tls_config(tls_config.clone())
.https_only()
.enable_http1()
.enable_http2()
Expand All @@ -335,5 +341,36 @@ mod tests {
&connector.tls_config.alpn_protocols,
&[b"h2".to_vec(), b"http/1.1".to_vec()]
);
let connector = super::ConnectorBuilder::new()
.with_tls_config(tls_config)
.https_only()
.enable_all_versions()
.build();
assert_eq!(
&connector.tls_config.alpn_protocols,
&[b"h2".to_vec(), b"http/1.1".to_vec()]
);
}

#[test]
#[cfg(all(not(feature = "http1"), feature = "http2"))]
fn test_alpn_http2() {
let roots = rustls::RootCertStore::empty();
let tls_config = rustls::ClientConfig::builder()
.with_safe_defaults()
.with_root_certificates(roots)
.with_no_client_auth();
let connector = super::ConnectorBuilder::new()
.with_tls_config(tls_config.clone())
.https_only()
.enable_http2()
.build();
assert_eq!(&connector.tls_config.alpn_protocols, &[b"h2".to_vec()]);
let connector = super::ConnectorBuilder::new()
.with_tls_config(tls_config)
.https_only()
.enable_all_versions()
.build();
assert_eq!(&connector.tls_config.alpn_protocols, &[b"h2".to_vec()]);
}
}

0 comments on commit 73dbde1

Please sign in to comment.