From 6e8e781f8fa1980701f1c721e58bb3941a76fa93 Mon Sep 17 00:00:00 2001 From: Evan Schwartz Date: Fri, 18 Jan 2019 17:15:57 -0500 Subject: [PATCH] Add ClientBuilder::h2_prior_knowlege() (#443) resolves https://github.com/seanmonstar/reqwest/issues/413 --- src/async_impl/client.rs | 27 +++++++++++++++++++++------ src/client.rs | 13 +++++++++++++ 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/src/async_impl/client.rs b/src/async_impl/client.rs index da6fa78..41e85b4 100644 --- a/src/async_impl/client.rs +++ b/src/async_impl/client.rs @@ -75,6 +75,7 @@ struct Config { identity: Option, #[cfg(feature = "tls")] tls: TlsBackend, + http2_only: bool, } impl ClientBuilder { @@ -104,6 +105,7 @@ impl ClientBuilder { identity: None, #[cfg(feature = "tls")] tls: TlsBackend::default(), + http2_only: false, }, } } @@ -142,10 +144,14 @@ impl ClientBuilder { use ::tls::NoVerifier; let mut tls = ::rustls::ClientConfig::new(); - tls.set_protocols(&[ - "h2".into(), - "http/1.1".into(), - ]); + if config.http2_only { + tls.set_protocols(&["h2".into()]); + } else { + tls.set_protocols(&[ + "h2".into(), + "http/1.1".into(), + ]); + } tls.root_store.add_server_trust_anchors(&webpki_roots::TLS_SERVER_ROOTS); if !config.certs_verification { @@ -168,8 +174,11 @@ impl ClientBuilder { Connector::new(proxies.clone())? }; - let hyper_client = ::hyper::Client::builder() - .build(connector); + let mut builder = ::hyper::Client::builder(); + if config.http2_only { + builder.http2_only(true); + } + let hyper_client = builder.build(connector); let proxies_maybe_http_auth = proxies .iter() @@ -305,6 +314,12 @@ impl ClientBuilder { self } + /// Only use HTTP/2. + pub fn h2_prior_knowledge(mut self) -> ClientBuilder { + self.config.http2_only = true; + self + } + #[doc(hidden)] #[deprecated(note = "DNS no longer uses blocking threads")] pub fn dns_threads(self, _threads: usize) -> ClientBuilder { diff --git a/src/client.rs b/src/client.rs index fee1d03..b93deee 100644 --- a/src/client.rs +++ b/src/client.rs @@ -293,6 +293,19 @@ impl ClientBuilder { self.inner = func(self.inner); self } + + /// Only use HTTP/2. + /// + /// # Example + /// + /// ``` + /// let client = reqwest::Client::builder() + /// .h2_prior_knowledge() + /// .build().unwrap(); + /// ``` + pub fn h2_prior_knowledge(self) -> ClientBuilder { + self.with_inner(|inner| inner.h2_prior_knowledge()) + } }