Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to disable http2 upgrade #1292

Merged
merged 2 commits into from
Jun 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 41 additions & 13 deletions src/async_impl/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ pub struct ClientBuilder {
config: Config,
}

enum HttpVersionPref {
Http1,
Http2,
All,
}

struct Config {
// NOTE: When adding a new field, update `fmt::Debug for ClientBuilder`
accepts: Accepts,
Expand All @@ -94,7 +100,7 @@ struct Config {
tls_built_in_root_certs: bool,
#[cfg(feature = "__tls")]
tls: TlsBackend,
http2_only: bool,
http_version_pref: HttpVersionPref,
http1_title_case_headers: bool,
http2_initial_stream_window_size: Option<u32>,
http2_initial_connection_window_size: Option<u32>,
Expand Down Expand Up @@ -153,7 +159,7 @@ impl ClientBuilder {
identity: None,
#[cfg(feature = "__tls")]
tls: TlsBackend::default(),
http2_only: false,
http_version_pref: HttpVersionPref::All,
http1_title_case_headers: false,
http2_initial_stream_window_size: None,
http2_initial_connection_window_size: None,
Expand Down Expand Up @@ -223,10 +229,16 @@ impl ClientBuilder {

#[cfg(feature = "native-tls-alpn")]
{
if config.http2_only {
tls.request_alpns(&["h2"]);
} else {
tls.request_alpns(&["h2", "http/1.1"]);
match config.http_version_pref {
HttpVersionPref::Http1 => {
tls.request_alpns(&["http/1.1"]);
}
HttpVersionPref::Http2 => {
tls.request_alpns(&["h2"]);
}
HttpVersionPref::All => {
tls.request_alpns(&["h2", "http/1.1"]);
}
}
}

Expand Down Expand Up @@ -282,10 +294,16 @@ impl ClientBuilder {
use crate::tls::NoVerifier;

let mut tls = rustls::ClientConfig::new();
if config.http2_only {
tls.set_protocols(&["h2".into()]);
} else {
tls.set_protocols(&["h2".into(), "http/1.1".into()]);
match config.http_version_pref {
HttpVersionPref::Http1 => {
tls.set_protocols(&["http/1.1".into()]);
}
HttpVersionPref::Http2 => {
tls.set_protocols(&["h2".into()]);
}
HttpVersionPref::All => {
tls.set_protocols(&["h2".into(), "http/1.1".into()]);
}
}
#[cfg(feature = "rustls-tls-webpki-roots")]
if config.tls_built_in_root_certs {
Expand Down Expand Up @@ -336,7 +354,7 @@ impl ClientBuilder {
connector.set_verbose(config.connection_verbose);

let mut builder = hyper::Client::builder();
if config.http2_only {
if matches!(config.http_version_pref, HttpVersionPref::Http2) {
builder.http2_only(true);
}

Expand Down Expand Up @@ -737,9 +755,15 @@ impl ClientBuilder {
self
}

/// Only use HTTP/1.
pub fn http1_only(mut self) -> ClientBuilder {
self.config.http_version_pref = HttpVersionPref::Http1;
self
}

/// Only use HTTP/2.
pub fn http2_prior_knowledge(mut self) -> ClientBuilder {
self.config.http2_only = true;
self.config.http_version_pref = HttpVersionPref::Http2;
self
}

Expand Down Expand Up @@ -1339,7 +1363,11 @@ impl Config {
f.field("http1_title_case_headers", &true);
}

if self.http2_only {
if matches!(self.http_version_pref, HttpVersionPref::Http1) {
f.field("http1_only", &true);
}

if matches!(self.http_version_pref, HttpVersionPref::Http2) {
f.field("http2_prior_knowledge", &true);
}

Expand Down
5 changes: 5 additions & 0 deletions src/blocking/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,11 @@ impl ClientBuilder {
self.with_inner(|inner| inner.http1_title_case_headers())
}

/// Only use HTTP/1.
pub fn http1_only(self) -> ClientBuilder {
self.with_inner(|inner| inner.http1_only())
}

/// Only use HTTP/2.
pub fn http2_prior_knowledge(self) -> ClientBuilder {
self.with_inner(|inner| inner.http2_prior_knowledge())
Expand Down