Skip to content

Commit 73dbde1

Browse files
TimLuqwquark
authored andcommitted
Connector: enable_all_versions with matching alpn vs features (rustls#224)
Fixes rustls#208
1 parent 1c8811a commit 73dbde1

File tree

1 file changed

+44
-7
lines changed

1 file changed

+44
-7
lines changed

src/connector/builder.rs

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -174,16 +174,22 @@ impl ConnectorBuilder<WantsProtocols1> {
174174
})
175175
}
176176

177-
/// Enable all HTTP versions
177+
/// Enable all HTTP versions built into this library (enabled with Cargo features)
178178
///
179-
/// For now, this enables both HTTP 1 and 2. In the future, other supported versions
180-
/// will be enabled as well.
181-
#[cfg(all(feature = "http1", feature = "http2"))]
179+
/// For now, this could enable both HTTP 1 and 2, depending on active features.
180+
/// In the future, other supported versions will be enabled as well.
181+
#[cfg(feature = "http2")]
182+
#[cfg_attr(docsrs, doc(cfg(feature = "http2")))]
182183
pub fn enable_all_versions(mut self) -> ConnectorBuilder<WantsProtocols3> {
183-
self.0.tls_config.alpn_protocols = vec![b"h2".to_vec()];
184+
#[cfg(feature = "http1")]
185+
let alpn_protocols = vec![b"h2".to_vec(), b"http/1.1".to_vec()];
186+
#[cfg(not(feature = "http1"))]
187+
let alpn_protocols = vec![b"h2".to_vec()];
188+
189+
self.0.tls_config.alpn_protocols = alpn_protocols;
184190
ConnectorBuilder(WantsProtocols3 {
185191
inner: self.0,
186-
enable_http1: true,
192+
enable_http1: cfg!(feature = "http1"),
187193
})
188194
}
189195

@@ -326,7 +332,7 @@ mod tests {
326332
.build();
327333
assert_eq!(&connector.tls_config.alpn_protocols, &[b"h2".to_vec()]);
328334
let connector = super::ConnectorBuilder::new()
329-
.with_tls_config(tls_config)
335+
.with_tls_config(tls_config.clone())
330336
.https_only()
331337
.enable_http1()
332338
.enable_http2()
@@ -335,5 +341,36 @@ mod tests {
335341
&connector.tls_config.alpn_protocols,
336342
&[b"h2".to_vec(), b"http/1.1".to_vec()]
337343
);
344+
let connector = super::ConnectorBuilder::new()
345+
.with_tls_config(tls_config)
346+
.https_only()
347+
.enable_all_versions()
348+
.build();
349+
assert_eq!(
350+
&connector.tls_config.alpn_protocols,
351+
&[b"h2".to_vec(), b"http/1.1".to_vec()]
352+
);
353+
}
354+
355+
#[test]
356+
#[cfg(all(not(feature = "http1"), feature = "http2"))]
357+
fn test_alpn_http2() {
358+
let roots = rustls::RootCertStore::empty();
359+
let tls_config = rustls::ClientConfig::builder()
360+
.with_safe_defaults()
361+
.with_root_certificates(roots)
362+
.with_no_client_auth();
363+
let connector = super::ConnectorBuilder::new()
364+
.with_tls_config(tls_config.clone())
365+
.https_only()
366+
.enable_http2()
367+
.build();
368+
assert_eq!(&connector.tls_config.alpn_protocols, &[b"h2".to_vec()]);
369+
let connector = super::ConnectorBuilder::new()
370+
.with_tls_config(tls_config)
371+
.https_only()
372+
.enable_all_versions()
373+
.build();
374+
assert_eq!(&connector.tls_config.alpn_protocols, &[b"h2".to_vec()]);
338375
}
339376
}

0 commit comments

Comments
 (0)