Add ClientBuilder::h2_prior_knowlege() (#443)

resolves https://github.com/seanmonstar/reqwest/issues/413
This commit is contained in:
Evan Schwartz
2019-01-18 17:15:57 -05:00
committed by Sean McArthur
parent 021851afd4
commit 6e8e781f8f
2 changed files with 34 additions and 6 deletions

View File

@@ -75,6 +75,7 @@ struct Config {
identity: Option<Identity>, identity: Option<Identity>,
#[cfg(feature = "tls")] #[cfg(feature = "tls")]
tls: TlsBackend, tls: TlsBackend,
http2_only: bool,
} }
impl ClientBuilder { impl ClientBuilder {
@@ -104,6 +105,7 @@ impl ClientBuilder {
identity: None, identity: None,
#[cfg(feature = "tls")] #[cfg(feature = "tls")]
tls: TlsBackend::default(), tls: TlsBackend::default(),
http2_only: false,
}, },
} }
} }
@@ -142,10 +144,14 @@ impl ClientBuilder {
use ::tls::NoVerifier; use ::tls::NoVerifier;
let mut tls = ::rustls::ClientConfig::new(); let mut tls = ::rustls::ClientConfig::new();
tls.set_protocols(&[ if config.http2_only {
"h2".into(), tls.set_protocols(&["h2".into()]);
"http/1.1".into(), } else {
]); tls.set_protocols(&[
"h2".into(),
"http/1.1".into(),
]);
}
tls.root_store.add_server_trust_anchors(&webpki_roots::TLS_SERVER_ROOTS); tls.root_store.add_server_trust_anchors(&webpki_roots::TLS_SERVER_ROOTS);
if !config.certs_verification { if !config.certs_verification {
@@ -168,8 +174,11 @@ impl ClientBuilder {
Connector::new(proxies.clone())? Connector::new(proxies.clone())?
}; };
let hyper_client = ::hyper::Client::builder() let mut builder = ::hyper::Client::builder();
.build(connector); if config.http2_only {
builder.http2_only(true);
}
let hyper_client = builder.build(connector);
let proxies_maybe_http_auth = proxies let proxies_maybe_http_auth = proxies
.iter() .iter()
@@ -305,6 +314,12 @@ impl ClientBuilder {
self self
} }
/// Only use HTTP/2.
pub fn h2_prior_knowledge(mut self) -> ClientBuilder {
self.config.http2_only = true;
self
}
#[doc(hidden)] #[doc(hidden)]
#[deprecated(note = "DNS no longer uses blocking threads")] #[deprecated(note = "DNS no longer uses blocking threads")]
pub fn dns_threads(self, _threads: usize) -> ClientBuilder { pub fn dns_threads(self, _threads: usize) -> ClientBuilder {

View File

@@ -293,6 +293,19 @@ impl ClientBuilder {
self.inner = func(self.inner); self.inner = func(self.inner);
self 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())
}
} }