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

Add option to disable http2 upgrade #1292

merged 2 commits into from
Jun 25, 2021

Conversation

ducaale
Copy link
Sponsor Contributor

@ducaale ducaale commented Jun 23, 2021

This can be helpful when there is a need to make an HTTP/1.0 or HTTP/1.1 connection but the server supports upgrading to HTTP/2.

let client = reqwest::Client::builder()
    .use_rustls_tls()
    .http1_only()
    .build()?;
let request = client
    .request(reqwest::Method::GET, "https://www.google.com")
    .version(reqwest::Version::HTTP_10)
    .build()?;
let res = client.execute(request).await?;
println!("Status: {:?}", res.version()); // Status: HTTP/1.0

let client = reqwest::Client::builder().use_rustls_tls().build()?;
let request = client
    .request(reqwest::Method::GET, "https://www.google.com")
    .version(reqwest::Version::HTTP_2)
    .build()?;
let res = client.execute(request).await?;
println!("Status: {:?}", res.version()); // Status: HTTP/2

Resolves #1290

Copy link
Owner

@seanmonstar seanmonstar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sounds like a great addition, thank you!

@@ -94,6 +94,7 @@ struct Config {
tls_built_in_root_certs: bool,
#[cfg(feature = "__tls")]
tls: TlsBackend,
http1_only: bool,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since http1_only and http2_only are exclusive (they can't both be true), perhaps it'd be best if we made an internal enum to track that?

enum HttpVersionPref {
    Http1,
    Http2,
    All,
}

@@ -737,8 +743,16 @@ impl ClientBuilder {
self
}

/// Only use HTTP/1.
pub fn disable_http2_upgrade(mut self) -> ClientBuilder {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming is always hard. Another option could be http1_only()...

Copy link
Owner

@seanmonstar seanmonstar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yesss, let's go! Thanks!

@seanmonstar seanmonstar merged commit bccefe7 into seanmonstar:master Jun 25, 2021
@ducaale ducaale deleted the http1-only branch June 25, 2021 07:10
pfernie added a commit to pfernie/reqwest that referenced this pull request Jun 25, 2021
* master:
  add option to disable http2 upgrade (seanmonstar#1292)
  Fix documentation of http1_title_case_headers() (seanmonstar#1294)
  CI: make a single final job that depends on all others (seanmonstar#1291)
  Fix bare url warnings in `multipart` docs (seanmonstar#1289)
  v0.11.4
  Allow overriding of DNS resolution to specified IP addresses(seanmonstar#561) (seanmonstar#1277)
  WASM: Add `try_clone` implementations to `Request` and `RequestBuilder` (seanmonstar#1286)
  Add native-tls-alpn feature (seanmonstar#1283)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: Option to disable upgrading to HTTP/2
2 participants