re-organize builder methods for docs purposes
This commit is contained in:
		| @@ -239,80 +239,47 @@ impl ClientBuilder { | |||||||
|         }) |         }) | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /// Set that all sockets have `SO_NODELAY` set to `true`. |     // Higher-level options | ||||||
|     pub fn tcp_nodelay(mut self) -> ClientBuilder { |  | ||||||
|         self.config.nodelay = true; |  | ||||||
|         self |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     /// Use native TLS backend. |  | ||||||
|     #[cfg(feature = "default-tls")] |  | ||||||
|     pub fn use_default_tls(mut self) -> ClientBuilder { |  | ||||||
|         self.config.tls = TlsBackend::Default; |  | ||||||
|         self |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     /// Use rustls TLS backend. |  | ||||||
|     #[cfg(feature = "rustls-tls")] |  | ||||||
|     pub fn use_rustls_tls(mut self) -> ClientBuilder { |  | ||||||
|         self.config.tls = TlsBackend::Rustls; |  | ||||||
|         self |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     /// Add a custom root certificate. |  | ||||||
|     /// |  | ||||||
|     /// This can be used to connect to a server that has a self-signed |  | ||||||
|     /// certificate for example. |  | ||||||
|     #[cfg(feature = "tls")] |  | ||||||
|     pub fn add_root_certificate(mut self, cert: Certificate) -> ClientBuilder { |  | ||||||
|         self.config.root_certs.push(cert); |  | ||||||
|         self |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     /// Sets the identity to be used for client certificate authentication. |  | ||||||
|     #[cfg(feature = "tls")] |  | ||||||
|     pub fn identity(mut self, identity: Identity) -> ClientBuilder { |  | ||||||
|         self.config.identity = Some(identity); |  | ||||||
|         self |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     /// Controls the use of hostname verification. |  | ||||||
|     /// |  | ||||||
|     /// Defaults to `false`. |  | ||||||
|     /// |  | ||||||
|     /// # Warning |  | ||||||
|     /// |  | ||||||
|     /// You should think very carefully before you use this method. If |  | ||||||
|     /// hostname verification is not used, any valid certificate for any |  | ||||||
|     /// site will be trusted for use from any other. This introduces a |  | ||||||
|     /// significant vulnerability to man-in-the-middle attacks. |  | ||||||
|     #[cfg(feature = "default-tls")] |  | ||||||
|     pub fn danger_accept_invalid_hostnames( |  | ||||||
|         mut self, |  | ||||||
|         accept_invalid_hostname: bool, |  | ||||||
|     ) -> ClientBuilder { |  | ||||||
|         self.config.hostname_verification = !accept_invalid_hostname; |  | ||||||
|         self |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     /// Controls the use of certificate validation. |  | ||||||
|     /// |  | ||||||
|     /// Defaults to `false`. |  | ||||||
|     /// |  | ||||||
|     /// # Warning |  | ||||||
|     /// |  | ||||||
|     /// You should think very carefully before using this method. If |  | ||||||
|     /// invalid certificates are trusted, *any* certificate for *any* site |  | ||||||
|     /// will be trusted for use. This includes expired certificates. This |  | ||||||
|     /// introduces significant vulnerabilities, and should only be used |  | ||||||
|     /// as a last resort. |  | ||||||
|     #[cfg(feature = "tls")] |  | ||||||
|     pub fn danger_accept_invalid_certs(mut self, accept_invalid_certs: bool) -> ClientBuilder { |  | ||||||
|         self.config.certs_verification = !accept_invalid_certs; |  | ||||||
|         self |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     /// Sets the default headers for every request. |     /// Sets the default headers for every request. | ||||||
|  |     /// | ||||||
|  |     /// # Example | ||||||
|  |     /// | ||||||
|  |     /// ```rust | ||||||
|  |     /// use reqwest::header; | ||||||
|  |     /// # async fn doc() -> Result<(), reqwest::Error> { | ||||||
|  |     /// let mut headers = header::HeaderMap::new(); | ||||||
|  |     /// headers.insert(header::AUTHORIZATION, header::HeaderValue::from_static("secret")); | ||||||
|  |     /// | ||||||
|  |     /// // get a client builder | ||||||
|  |     /// let client = reqwest::Client::builder() | ||||||
|  |     ///     .default_headers(headers) | ||||||
|  |     ///     .build()?; | ||||||
|  |     /// let res = client.get("https://www.rust-lang.org").send().await?; | ||||||
|  |     /// # Ok(()) | ||||||
|  |     /// # } | ||||||
|  |     /// ``` | ||||||
|  |     /// | ||||||
|  |     /// Override the default headers: | ||||||
|  |     /// | ||||||
|  |     /// ```rust | ||||||
|  |     /// use reqwest::header; | ||||||
|  |     /// # async fn doc() -> Result<(), reqwest::Error> { | ||||||
|  |     /// let mut headers = header::HeaderMap::new(); | ||||||
|  |     /// headers.insert(header::AUTHORIZATION, header::HeaderValue::from_static("secret")); | ||||||
|  |     /// | ||||||
|  |     /// // get a client builder | ||||||
|  |     /// let client = reqwest::Client::builder() | ||||||
|  |     ///     .default_headers(headers) | ||||||
|  |     ///     .build()?; | ||||||
|  |     /// let res = client | ||||||
|  |     ///     .get("https://www.rust-lang.org") | ||||||
|  |     ///     .header(header::AUTHORIZATION, "token") | ||||||
|  |     ///     .send() | ||||||
|  |     ///     .await?; | ||||||
|  |     /// # Ok(()) | ||||||
|  |     /// # } | ||||||
|  |     /// ``` | ||||||
|     pub fn default_headers(mut self, headers: HeaderMap) -> ClientBuilder { |     pub fn default_headers(mut self, headers: HeaderMap) -> ClientBuilder { | ||||||
|         for (key, value) in headers.iter() { |         for (key, value) in headers.iter() { | ||||||
|             self.config.headers.insert(key, value.clone()); |             self.config.headers.insert(key, value.clone()); | ||||||
| @@ -320,6 +287,27 @@ impl ClientBuilder { | |||||||
|         self |         self | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |  | ||||||
|  |     /// Enable a persistent cookie store for the client. | ||||||
|  |     /// | ||||||
|  |     /// Cookies received in responses will be preserved and included in | ||||||
|  |     /// additional requests. | ||||||
|  |     /// | ||||||
|  |     /// By default, no cookie store is used. | ||||||
|  |     /// | ||||||
|  |     /// # Optional | ||||||
|  |     /// | ||||||
|  |     /// This requires the optional `cookies` feature to be enabled. | ||||||
|  |     #[cfg(feature = "cookies")] | ||||||
|  |     pub fn cookie_store(mut self, enable: bool) -> ClientBuilder { | ||||||
|  |         self.config.cookie_store = if enable { | ||||||
|  |             Some(cookie::CookieStore::default()) | ||||||
|  |         } else { | ||||||
|  |             None | ||||||
|  |         }; | ||||||
|  |         self | ||||||
|  |     } | ||||||
|  |  | ||||||
|     /// Enable auto gzip decompression by checking the `Content-Encoding` response header. |     /// Enable auto gzip decompression by checking the `Content-Encoding` response header. | ||||||
|     /// |     /// | ||||||
|     /// If auto gzip decompresson is turned on: |     /// If auto gzip decompresson is turned on: | ||||||
| @@ -359,6 +347,26 @@ impl ClientBuilder { | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     // Redirect options | ||||||
|  |  | ||||||
|  |     /// Set a `RedirectPolicy` for this client. | ||||||
|  |     /// | ||||||
|  |     /// Default will follow redirects up to a maximum of 10. | ||||||
|  |     pub fn redirect(mut self, policy: RedirectPolicy) -> ClientBuilder { | ||||||
|  |         self.config.redirect_policy = policy; | ||||||
|  |         self | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     /// Enable or disable automatic setting of the `Referer` header. | ||||||
|  |     /// | ||||||
|  |     /// Default is `true`. | ||||||
|  |     pub fn referer(mut self, enable: bool) -> ClientBuilder { | ||||||
|  |         self.config.referer = enable; | ||||||
|  |         self | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     // Proxy options | ||||||
|  |  | ||||||
|     /// Add a `Proxy` to the list of proxies the `Client` will use. |     /// Add a `Proxy` to the list of proxies the `Client` will use. | ||||||
|     pub fn proxy(mut self, proxy: Proxy) -> ClientBuilder { |     pub fn proxy(mut self, proxy: Proxy) -> ClientBuilder { | ||||||
|         self.config.proxies.push(proxy); |         self.config.proxies.push(proxy); | ||||||
| @@ -384,21 +392,7 @@ impl ClientBuilder { | |||||||
|         self |         self | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /// Set a `RedirectPolicy` for this client. |     // Timeout options | ||||||
|     /// |  | ||||||
|     /// Default will follow redirects up to a maximum of 10. |  | ||||||
|     pub fn redirect(mut self, policy: RedirectPolicy) -> ClientBuilder { |  | ||||||
|         self.config.redirect_policy = policy; |  | ||||||
|         self |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     /// Enable or disable automatic setting of the `Referer` header. |  | ||||||
|     /// |  | ||||||
|     /// Default is `true`. |  | ||||||
|     pub fn referer(mut self, enable: bool) -> ClientBuilder { |  | ||||||
|         self.config.referer = enable; |  | ||||||
|         self |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     /// Enables a request timeout. |     /// Enables a request timeout. | ||||||
|     /// |     /// | ||||||
| @@ -411,26 +405,6 @@ impl ClientBuilder { | |||||||
|         self |         self | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /// Sets the maximum idle connection per host allowed in the pool. |  | ||||||
|     /// |  | ||||||
|     /// Default is usize::MAX (no limit). |  | ||||||
|     pub fn max_idle_per_host(mut self, max: usize) -> ClientBuilder { |  | ||||||
|         self.config.max_idle_per_host = max; |  | ||||||
|         self |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     /// Only use HTTP/2. |  | ||||||
|     pub fn h2_prior_knowledge(mut self) -> ClientBuilder { |  | ||||||
|         self.config.http2_only = true; |  | ||||||
|         self |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     /// Enable case sensitive headers. |  | ||||||
|     pub fn http1_title_case_headers(mut self) -> ClientBuilder { |  | ||||||
|         self.config.http1_title_case_headers = true; |  | ||||||
|         self |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     /// Set a timeout for only the connect phase of a `Client`. |     /// Set a timeout for only the connect phase of a `Client`. | ||||||
|     /// |     /// | ||||||
|     /// Default is `None`. |     /// Default is `None`. | ||||||
| @@ -444,7 +418,45 @@ impl ClientBuilder { | |||||||
|         self |         self | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /// Bind to a local IP Address |     // HTTP options | ||||||
|  |  | ||||||
|  |     /// Sets the maximum idle connection per host allowed in the pool. | ||||||
|  |     pub fn max_idle_per_host(mut self, max: usize) -> ClientBuilder { | ||||||
|  |         self.config.max_idle_per_host = max; | ||||||
|  |         self | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     /// Enable case sensitive headers. | ||||||
|  |     pub fn http1_title_case_headers(mut self) -> ClientBuilder { | ||||||
|  |         self.config.http1_title_case_headers = true; | ||||||
|  |         self | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     /// Only use HTTP/2. | ||||||
|  |     pub fn http2_prior_knowledge(mut self) -> ClientBuilder { | ||||||
|  |         self.config.http2_only = true; | ||||||
|  |         self | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     // TCP options | ||||||
|  |  | ||||||
|  |     /// Set that all sockets have `SO_NODELAY` set to `true`. | ||||||
|  |     pub fn tcp_nodelay(mut self) -> ClientBuilder { | ||||||
|  |         self.config.nodelay = true; | ||||||
|  |         self | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     /// Bind to a local IP Address. | ||||||
|  |     /// | ||||||
|  |     /// # Example | ||||||
|  |     /// | ||||||
|  |     /// ``` | ||||||
|  |     /// use std::net::IpAddr; | ||||||
|  |     /// let local_addr = IpAddr::from([12, 4, 1, 8]); | ||||||
|  |     /// let client = reqwest::Client::builder() | ||||||
|  |     ///     .local_address(local_addr) | ||||||
|  |     ///     .build().unwrap(); | ||||||
|  |     /// ``` | ||||||
|     pub fn local_address<T>(mut self, addr: T) -> ClientBuilder |     pub fn local_address<T>(mut self, addr: T) -> ClientBuilder | ||||||
|     where |     where | ||||||
|         T: Into<Option<IpAddr>>, |         T: Into<Option<IpAddr>>, | ||||||
| @@ -453,25 +465,110 @@ impl ClientBuilder { | |||||||
|         self |         self | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /// Enable a persistent cookie store for the client. |     // TLS options | ||||||
|  |  | ||||||
|  |     /// Add a custom root certificate. | ||||||
|     /// |     /// | ||||||
|     /// Cookies received in responses will be preserved and included in |     /// This can be used to connect to a server that has a self-signed | ||||||
|     /// additional requests. |     /// certificate for example. | ||||||
|     /// |  | ||||||
|     /// By default, no cookie store is used. |  | ||||||
|     /// |     /// | ||||||
|     /// # Optional |     /// # Optional | ||||||
|     /// |     /// | ||||||
|     /// This requires the optional `cookies` feature to be enabled. |     /// This requires the optional `default-tls` or `rustls-tls` feature to be | ||||||
|     #[cfg(feature = "cookies")] |     /// enabled. | ||||||
|     pub fn cookie_store(mut self, enable: bool) -> ClientBuilder { |     #[cfg(feature = "tls")] | ||||||
|         self.config.cookie_store = if enable { |     pub fn add_root_certificate(mut self, cert: Certificate) -> ClientBuilder { | ||||||
|             Some(cookie::CookieStore::default()) |         self.config.root_certs.push(cert); | ||||||
|         } else { |  | ||||||
|             None |  | ||||||
|         }; |  | ||||||
|         self |         self | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     /// Sets the identity to be used for client certificate authentication. | ||||||
|  |     /// | ||||||
|  |     /// # Optional | ||||||
|  |     /// | ||||||
|  |     /// This requires the optional `default-tls` or `rustls-tls` feature to be | ||||||
|  |     /// enabled. | ||||||
|  |     #[cfg(feature = "tls")] | ||||||
|  |     pub fn identity(mut self, identity: Identity) -> ClientBuilder { | ||||||
|  |         self.config.identity = Some(identity); | ||||||
|  |         self | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     /// Controls the use of hostname verification. | ||||||
|  |     /// | ||||||
|  |     /// Defaults to `false`. | ||||||
|  |     /// | ||||||
|  |     /// # Warning | ||||||
|  |     /// | ||||||
|  |     /// You should think very carefully before you use this method. If | ||||||
|  |     /// hostname verification is not used, any valid certificate for any | ||||||
|  |     /// site will be trusted for use from any other. This introduces a | ||||||
|  |     /// significant vulnerability to man-in-the-middle attacks. | ||||||
|  |     /// | ||||||
|  |     /// # Optional | ||||||
|  |     /// | ||||||
|  |     /// This requires the optional `default-tls` feature to be enabled. | ||||||
|  |     #[cfg(feature = "default-tls")] | ||||||
|  |     pub fn danger_accept_invalid_hostnames( | ||||||
|  |         mut self, | ||||||
|  |         accept_invalid_hostname: bool, | ||||||
|  |     ) -> ClientBuilder { | ||||||
|  |         self.config.hostname_verification = !accept_invalid_hostname; | ||||||
|  |         self | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     /// Controls the use of certificate validation. | ||||||
|  |     /// | ||||||
|  |     /// Defaults to `false`. | ||||||
|  |     /// | ||||||
|  |     /// # Warning | ||||||
|  |     /// | ||||||
|  |     /// You should think very carefully before using this method. If | ||||||
|  |     /// invalid certificates are trusted, *any* certificate for *any* site | ||||||
|  |     /// will be trusted for use. This includes expired certificates. This | ||||||
|  |     /// introduces significant vulnerabilities, and should only be used | ||||||
|  |     /// as a last resort. | ||||||
|  |     /// | ||||||
|  |     /// # Optional | ||||||
|  |     /// | ||||||
|  |     /// This requires the optional `default-tls` or `rustls-tls` feature to be | ||||||
|  |     /// enabled. | ||||||
|  |     #[cfg(feature = "tls")] | ||||||
|  |     pub fn danger_accept_invalid_certs(mut self, accept_invalid_certs: bool) -> ClientBuilder { | ||||||
|  |         self.config.certs_verification = !accept_invalid_certs; | ||||||
|  |         self | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     /// Force using the native TLS backend. | ||||||
|  |     /// | ||||||
|  |     /// Since multiple TLS backends can be optionally enabled, this option will | ||||||
|  |     /// force the `native-tls` backend to be used for this `Client`. | ||||||
|  |     /// | ||||||
|  |     /// # Optional | ||||||
|  |     /// | ||||||
|  |     /// This requires the optional `default-tls` feature to be enabled. | ||||||
|  |     #[cfg(feature = "default-tls")] | ||||||
|  |     pub fn use_default_tls(mut self) -> ClientBuilder { | ||||||
|  |         self.config.tls = TlsBackend::Default; | ||||||
|  |         self | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |  | ||||||
|  |     /// Force using the Rustls TLS backend. | ||||||
|  |     /// | ||||||
|  |     /// Since multiple TLS backends can be optionally enabled, this option will | ||||||
|  |     /// force the `rustls` backend to be used for this `Client`. | ||||||
|  |     /// | ||||||
|  |     /// # Optional | ||||||
|  |     /// | ||||||
|  |     /// This requires the optional `rustls-tls` feature to be enabled. | ||||||
|  |     #[cfg(feature = "rustls-tls")] | ||||||
|  |     pub fn use_rustls_tls(mut self) -> ClientBuilder { | ||||||
|  |         self.config.tls = TlsBackend::Rustls; | ||||||
|  |         self | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |  | ||||||
| } | } | ||||||
|  |  | ||||||
| type HyperClient = hyper::Client<Connector, super::body::ImplStream>; | type HyperClient = hyper::Client<Connector, super::body::ImplStream>; | ||||||
|   | |||||||
| @@ -84,6 +84,119 @@ impl ClientBuilder { | |||||||
|         ClientHandle::new(self).map(|handle| Client { inner: handle }) |         ClientHandle::new(self).map(|handle| Client { inner: handle }) | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |  | ||||||
|  |     // Higher-level options | ||||||
|  |  | ||||||
|  |     /// Sets the default headers for every request. | ||||||
|  |     /// | ||||||
|  |     /// # Example | ||||||
|  |     /// | ||||||
|  |     /// ```rust | ||||||
|  |     /// use reqwest::header; | ||||||
|  |     /// # fn build_client() -> Result<(), reqwest::Error> { | ||||||
|  |     /// let mut headers = header::HeaderMap::new(); | ||||||
|  |     /// headers.insert(header::AUTHORIZATION, header::HeaderValue::from_static("secret")); | ||||||
|  |     /// | ||||||
|  |     /// // get a client builder | ||||||
|  |     /// let client = reqwest::blocking::Client::builder() | ||||||
|  |     ///     .default_headers(headers) | ||||||
|  |     ///     .build()?; | ||||||
|  |     /// let res = client.get("https://www.rust-lang.org").send()?; | ||||||
|  |     /// # Ok(()) | ||||||
|  |     /// # } | ||||||
|  |     /// ``` | ||||||
|  |     /// | ||||||
|  |     /// Override the default headers: | ||||||
|  |     /// | ||||||
|  |     /// ```rust | ||||||
|  |     /// use reqwest::header; | ||||||
|  |     /// # fn build_client() -> Result<(), reqwest::Error> { | ||||||
|  |     /// let mut headers = header::HeaderMap::new(); | ||||||
|  |     /// headers.insert(header::AUTHORIZATION, header::HeaderValue::from_static("secret")); | ||||||
|  |     /// | ||||||
|  |     /// // get a client builder | ||||||
|  |     /// let client = reqwest::blocking::Client::builder() | ||||||
|  |     ///     .default_headers(headers) | ||||||
|  |     ///     .build()?; | ||||||
|  |     /// let res = client | ||||||
|  |     ///     .get("https://www.rust-lang.org") | ||||||
|  |     ///     .header(header::AUTHORIZATION, "token") | ||||||
|  |     ///     .send()?; | ||||||
|  |     /// # Ok(()) | ||||||
|  |     /// # } | ||||||
|  |     /// ``` | ||||||
|  |     pub fn default_headers(self, headers: header::HeaderMap) -> ClientBuilder { | ||||||
|  |         self.with_inner(move |inner| inner.default_headers(headers)) | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     /// Enable a persistent cookie store for the client. | ||||||
|  |     /// | ||||||
|  |     /// Cookies received in responses will be preserved and included in | ||||||
|  |     /// additional requests. | ||||||
|  |     /// | ||||||
|  |     /// By default, no cookie store is used. | ||||||
|  |     /// | ||||||
|  |     /// # Optional | ||||||
|  |     /// | ||||||
|  |     /// This requires the optional `cookies` feature to be enabled. | ||||||
|  |     #[cfg(feature = "cookies")] | ||||||
|  |     pub fn cookie_store(self, enable: bool) -> ClientBuilder { | ||||||
|  |         self.with_inner(|inner| inner.cookie_store(enable)) | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     /// Enable auto gzip decompression by checking the `Content-Encoding` response header. | ||||||
|  |     /// | ||||||
|  |     /// If auto gzip decompresson is turned on: | ||||||
|  |     /// | ||||||
|  |     /// - When sending a request and if the request's headers do not already contain | ||||||
|  |     ///   an `Accept-Encoding` **and** `Range` values, the `Accept-Encoding` header is set to `gzip`. | ||||||
|  |     ///   The request body is **not** automatically compressed. | ||||||
|  |     /// - When receiving a response, if it's headers contain a `Content-Encoding` value that | ||||||
|  |     ///   equals to `gzip`, both values `Content-Encoding` and `Content-Length` are removed from the | ||||||
|  |     ///   headers' set. The response body is automatically decompressed. | ||||||
|  |     /// | ||||||
|  |     /// If the `gzip` feature is turned on, the default option is enabled. | ||||||
|  |     /// | ||||||
|  |     /// # Optional | ||||||
|  |     /// | ||||||
|  |     /// This requires the optional `gzip` feature to be enabled | ||||||
|  |     #[cfg(feature = "gzip")] | ||||||
|  |     pub fn gzip(self, enable: bool) -> ClientBuilder { | ||||||
|  |         self.with_inner(|inner| inner.gzip(enable)) | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     /// Disable auto response body gzip decompression. | ||||||
|  |     /// | ||||||
|  |     /// This method exists even if the optional `gzip` feature is not enabled. | ||||||
|  |     /// This can be used to ensure a `Client` doesn't use gzip decompression | ||||||
|  |     /// even if another dependency were to enable the optional `gzip` feature. | ||||||
|  |     pub fn no_gzip(self) -> ClientBuilder { | ||||||
|  |         self.with_inner(|inner| inner.no_gzip()) | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     // Redirect options | ||||||
|  |  | ||||||
|  |     /// Set a `RedirectPolicy` for this client. | ||||||
|  |     /// | ||||||
|  |     /// Default will follow redirects up to a maximum of 10. | ||||||
|  |     pub fn redirect(self, policy: RedirectPolicy) -> ClientBuilder { | ||||||
|  |         self.with_inner(move |inner| inner.redirect(policy)) | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     /// Enable or disable automatic setting of the `Referer` header. | ||||||
|  |     /// | ||||||
|  |     /// Default is `true`. | ||||||
|  |     pub fn referer(self, enable: bool) -> ClientBuilder { | ||||||
|  |         self.with_inner(|inner| inner.referer(enable)) | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     // Proxy options | ||||||
|  |  | ||||||
|  |     /// Add a `Proxy` to the list of proxies the `Client` will use. | ||||||
|  |     pub fn proxy(self, proxy: Proxy) -> ClientBuilder { | ||||||
|  |         self.with_inner(move |inner| inner.proxy(proxy)) | ||||||
|  |     } | ||||||
|  |  | ||||||
|     /// Disable proxy setting. |     /// Disable proxy setting. | ||||||
|     pub fn no_proxy(self) -> ClientBuilder { |     pub fn no_proxy(self) -> ClientBuilder { | ||||||
|         self.with_inner(move |inner| inner.no_proxy()) |         self.with_inner(move |inner| inner.no_proxy()) | ||||||
| @@ -94,22 +207,79 @@ impl ClientBuilder { | |||||||
|         self.with_inner(move |inner| inner.use_sys_proxy()) |         self.with_inner(move |inner| inner.use_sys_proxy()) | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     // Timeout options | ||||||
|  |  | ||||||
|  |     /// Set a timeout for connect, read and write operations of a `Client`. | ||||||
|  |     /// | ||||||
|  |     /// Default is 30 seconds. | ||||||
|  |     /// | ||||||
|  |     /// Pass `None` to disable timeout. | ||||||
|  |     pub fn timeout<T>(mut self, timeout: T) -> ClientBuilder | ||||||
|  |     where | ||||||
|  |         T: Into<Option<Duration>>, | ||||||
|  |     { | ||||||
|  |         self.timeout = Timeout(timeout.into()); | ||||||
|  |         self | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     /// Set a timeout for only the connect phase of a `Client`. | ||||||
|  |     /// | ||||||
|  |     /// Default is `None`. | ||||||
|  |     pub fn connect_timeout<T>(self, timeout: T) -> ClientBuilder | ||||||
|  |     where | ||||||
|  |         T: Into<Option<Duration>>, | ||||||
|  |     { | ||||||
|  |         let timeout = timeout.into(); | ||||||
|  |         if let Some(dur) = timeout { | ||||||
|  |             self.with_inner(|inner| inner.connect_timeout(dur)) | ||||||
|  |         } else { | ||||||
|  |             self | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     // HTTP options | ||||||
|  |  | ||||||
|  |     /// Sets the maximum idle connection per host allowed in the pool. | ||||||
|  |     pub fn max_idle_per_host(self, max: usize) -> ClientBuilder { | ||||||
|  |         self.with_inner(move |inner| inner.max_idle_per_host(max)) | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     /// Enable case sensitive headers. | ||||||
|  |     pub fn http1_title_case_headers(self) -> ClientBuilder { | ||||||
|  |         self.with_inner(|inner| inner.http1_title_case_headers()) | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     /// Only use HTTP/2. | ||||||
|  |     pub fn http2_prior_knowledge(self) -> ClientBuilder { | ||||||
|  |         self.with_inner(|inner| inner.http2_prior_knowledge()) | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     // TCP options | ||||||
|  |  | ||||||
|     /// Set that all sockets have `SO_NODELAY` set to `true`. |     /// Set that all sockets have `SO_NODELAY` set to `true`. | ||||||
|     pub fn tcp_nodelay(self) -> ClientBuilder { |     pub fn tcp_nodelay(self) -> ClientBuilder { | ||||||
|         self.with_inner(move |inner| inner.tcp_nodelay()) |         self.with_inner(move |inner| inner.tcp_nodelay()) | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /// Use native TLS backend. |     /// Bind to a local IP Address. | ||||||
|     #[cfg(feature = "default-tls")] |     /// | ||||||
|     pub fn use_default_tls(self) -> ClientBuilder { |     /// # Example | ||||||
|         self.with_inner(move |inner| inner.use_default_tls()) |     /// | ||||||
|  |     /// ``` | ||||||
|  |     /// use std::net::IpAddr; | ||||||
|  |     /// let local_addr = IpAddr::from([12, 4, 1, 8]); | ||||||
|  |     /// let client = reqwest::blocking::Client::builder() | ||||||
|  |     ///     .local_address(local_addr) | ||||||
|  |     ///     .build().unwrap(); | ||||||
|  |     /// ``` | ||||||
|  |     pub fn local_address<T>(self, addr: T) -> ClientBuilder | ||||||
|  |     where | ||||||
|  |         T: Into<Option<IpAddr>>, | ||||||
|  |     { | ||||||
|  |         self.with_inner(move |inner| inner.local_address(addr)) | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /// Use rustls TLS backend. |     // TLS options | ||||||
|     #[cfg(feature = "rustls-tls")] |  | ||||||
|     pub fn use_rustls_tls(self) -> ClientBuilder { |  | ||||||
|         self.with_inner(move |inner| inner.use_rustls_tls()) |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     /// Add a custom root certificate. |     /// Add a custom root certificate. | ||||||
|     /// |     /// | ||||||
| @@ -118,16 +288,16 @@ impl ClientBuilder { | |||||||
|     /// trusted store. |     /// trusted store. | ||||||
|     /// |     /// | ||||||
|     /// # Example |     /// # Example | ||||||
|  |     /// | ||||||
|     /// ``` |     /// ``` | ||||||
|     /// # use std::fs::File; |     /// # use std::fs::File; | ||||||
|     /// # use std::io::Read; |     /// # use std::io::Read; | ||||||
|     /// # fn build_client() -> Result<(), Box<std::error::Error>> { |     /// # fn build_client() -> Result<(), Box<dyn std::error::Error>> { | ||||||
|     /// // read a local binary DER encoded certificate |     /// // read a local binary DER encoded certificate | ||||||
|     /// let mut buf = Vec::new(); |     /// let der = std::fs::read("my-cert.der")?; | ||||||
|     /// File::open("my-cert.der")?.read_to_end(&mut buf)?; |  | ||||||
|     /// |     /// | ||||||
|     /// // create a certificate |     /// // create a certificate | ||||||
|     /// let cert = reqwest::Certificate::from_der(&buf)?; |     /// let cert = reqwest::Certificate::from_der(&der)?; | ||||||
|     /// |     /// | ||||||
|     /// // get a client builder |     /// // get a client builder | ||||||
|     /// let client = reqwest::blocking::Client::builder() |     /// let client = reqwest::blocking::Client::builder() | ||||||
| @@ -138,9 +308,10 @@ impl ClientBuilder { | |||||||
|     /// # } |     /// # } | ||||||
|     /// ``` |     /// ``` | ||||||
|     /// |     /// | ||||||
|     /// # Errors |     /// # Optional | ||||||
|     /// |     /// | ||||||
|     /// This method fails if adding root certificate was unsuccessful. |     /// This requires the optional `default-tls` or `rustls-tls` feature to be | ||||||
|  |     /// enabled. | ||||||
|     #[cfg(feature = "tls")] |     #[cfg(feature = "tls")] | ||||||
|     pub fn add_root_certificate(self, cert: Certificate) -> ClientBuilder { |     pub fn add_root_certificate(self, cert: Certificate) -> ClientBuilder { | ||||||
|         self.with_inner(move |inner| inner.add_root_certificate(cert)) |         self.with_inner(move |inner| inner.add_root_certificate(cert)) | ||||||
| @@ -213,131 +384,33 @@ impl ClientBuilder { | |||||||
|         self.with_inner(|inner| inner.danger_accept_invalid_certs(accept_invalid_certs)) |         self.with_inner(|inner| inner.danger_accept_invalid_certs(accept_invalid_certs)) | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /// Sets the default headers for every request. |     /// Force using the native TLS backend. | ||||||
|     /// |     /// | ||||||
|     /// # Example |     /// Since multiple TLS backends can be optionally enabled, this option will | ||||||
|     /// |     /// force the `native-tls` backend to be used for this `Client`. | ||||||
|     /// ```rust |  | ||||||
|     /// use reqwest::header; |  | ||||||
|     /// # fn build_client() -> Result<(), Box<std::error::Error>> { |  | ||||||
|     /// let mut headers = header::HeaderMap::new(); |  | ||||||
|     /// headers.insert(header::AUTHORIZATION, header::HeaderValue::from_static("secret")); |  | ||||||
|     /// |  | ||||||
|     /// // get a client builder |  | ||||||
|     /// let client = reqwest::blocking::Client::builder() |  | ||||||
|     ///     .default_headers(headers) |  | ||||||
|     ///     .build()?; |  | ||||||
|     /// let res = client.get("https://www.rust-lang.org").send()?; |  | ||||||
|     /// # Ok(()) |  | ||||||
|     /// # } |  | ||||||
|     /// ``` |  | ||||||
|     /// |  | ||||||
|     /// Override the default headers: |  | ||||||
|     /// |  | ||||||
|     /// ```rust |  | ||||||
|     /// use reqwest::header; |  | ||||||
|     /// # fn build_client() -> Result<(), Box<std::error::Error>> { |  | ||||||
|     /// let mut headers = header::HeaderMap::new(); |  | ||||||
|     /// headers.insert(header::AUTHORIZATION, header::HeaderValue::from_static("secret")); |  | ||||||
|     /// |  | ||||||
|     /// // get a client builder |  | ||||||
|     /// let client = reqwest::blocking::Client::builder() |  | ||||||
|     ///     .default_headers(headers) |  | ||||||
|     ///     .build()?; |  | ||||||
|     /// let res = client |  | ||||||
|     ///     .get("https://www.rust-lang.org") |  | ||||||
|     ///     .header(header::AUTHORIZATION, "token") |  | ||||||
|     ///     .send()?; |  | ||||||
|     /// # Ok(()) |  | ||||||
|     /// # } |  | ||||||
|     /// ``` |  | ||||||
|     pub fn default_headers(self, headers: header::HeaderMap) -> ClientBuilder { |  | ||||||
|         self.with_inner(move |inner| inner.default_headers(headers)) |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     /// Enable auto gzip decompression by checking the `Content-Encoding` response header. |  | ||||||
|     /// |  | ||||||
|     /// If auto gzip decompresson is turned on: |  | ||||||
|     /// |  | ||||||
|     /// - When sending a request and if the request's headers do not already contain |  | ||||||
|     ///   an `Accept-Encoding` **and** `Range` values, the `Accept-Encoding` header is set to `gzip`. |  | ||||||
|     ///   The request body is **not** automatically compressed. |  | ||||||
|     /// - When receiving a response, if it's headers contain a `Content-Encoding` value that |  | ||||||
|     ///   equals to `gzip`, both values `Content-Encoding` and `Content-Length` are removed from the |  | ||||||
|     ///   headers' set. The response body is automatically decompressed. |  | ||||||
|     /// |  | ||||||
|     /// If the `gzip` feature is turned on, the default option is enabled. |  | ||||||
|     /// |     /// | ||||||
|     /// # Optional |     /// # Optional | ||||||
|     /// |     /// | ||||||
|     /// This requires the optional `gzip` feature to be enabled |     /// This requires the optional `default-tls` feature to be enabled. | ||||||
|     #[cfg(feature = "gzip")] |     #[cfg(feature = "default-tls")] | ||||||
|     pub fn gzip(self, enable: bool) -> ClientBuilder { |     pub fn use_default_tls(self) -> ClientBuilder { | ||||||
|         self.with_inner(|inner| inner.gzip(enable)) |         self.with_inner(move |inner| inner.use_default_tls()) | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /// Disable auto response body gzip decompression. |     /// Force using the Rustls TLS backend. | ||||||
|     /// |     /// | ||||||
|     /// This method exists even if the optional `gzip` feature is not enabled. |     /// Since multiple TLS backends can be optionally enabled, this option will | ||||||
|     /// This can be used to ensure a `Client` doesn't use gzip decompression |     /// force the `rustls` backend to be used for this `Client`. | ||||||
|     /// even if another dependency were to enable the optional `gzip` feature. |     /// | ||||||
|     pub fn no_gzip(self) -> ClientBuilder { |     /// # Optional | ||||||
|         self.with_inner(|inner| inner.no_gzip()) |     /// | ||||||
|  |     /// This requires the optional `rustls-tls` feature to be enabled. | ||||||
|  |     #[cfg(feature = "rustls-tls")] | ||||||
|  |     pub fn use_rustls_tls(self) -> ClientBuilder { | ||||||
|  |         self.with_inner(move |inner| inner.use_rustls_tls()) | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /// Add a `Proxy` to the list of proxies the `Client` will use. |     // private | ||||||
|     pub fn proxy(self, proxy: Proxy) -> ClientBuilder { |  | ||||||
|         self.with_inner(move |inner| inner.proxy(proxy)) |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     /// Set a `RedirectPolicy` for this client. |  | ||||||
|     /// |  | ||||||
|     /// Default will follow redirects up to a maximum of 10. |  | ||||||
|     pub fn redirect(self, policy: RedirectPolicy) -> ClientBuilder { |  | ||||||
|         self.with_inner(move |inner| inner.redirect(policy)) |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     /// Enable or disable automatic setting of the `Referer` header. |  | ||||||
|     /// |  | ||||||
|     /// Default is `true`. |  | ||||||
|     pub fn referer(self, enable: bool) -> ClientBuilder { |  | ||||||
|         self.with_inner(|inner| inner.referer(enable)) |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     /// Set a timeout for connect, read and write operations of a `Client`. |  | ||||||
|     /// |  | ||||||
|     /// Default is 30 seconds. |  | ||||||
|     /// |  | ||||||
|     /// Pass `None` to disable timeout. |  | ||||||
|     pub fn timeout<T>(mut self, timeout: T) -> ClientBuilder |  | ||||||
|     where |  | ||||||
|         T: Into<Option<Duration>>, |  | ||||||
|     { |  | ||||||
|         self.timeout = Timeout(timeout.into()); |  | ||||||
|         self |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     /// Sets the maximum idle connection per host allowed in the pool. |  | ||||||
|     /// |  | ||||||
|     /// Default is usize::MAX (no limit). |  | ||||||
|     pub fn max_idle_per_host(self, max: usize) -> ClientBuilder { |  | ||||||
|         self.with_inner(move |inner| inner.max_idle_per_host(max)) |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     /// Set a timeout for only the connect phase of a `Client`. |  | ||||||
|     /// |  | ||||||
|     /// Default is `None`. |  | ||||||
|     pub fn connect_timeout<T>(self, timeout: T) -> ClientBuilder |  | ||||||
|     where |  | ||||||
|         T: Into<Option<Duration>>, |  | ||||||
|     { |  | ||||||
|         let timeout = timeout.into(); |  | ||||||
|         if let Some(dur) = timeout { |  | ||||||
|             self.with_inner(|inner| inner.connect_timeout(dur)) |  | ||||||
|         } else { |  | ||||||
|             self |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     fn with_inner<F>(mut self, func: F) -> ClientBuilder |     fn with_inner<F>(mut self, func: F) -> ClientBuilder | ||||||
|     where |     where | ||||||
| @@ -346,74 +419,6 @@ impl ClientBuilder { | |||||||
|         self.inner = func(self.inner); |         self.inner = func(self.inner); | ||||||
|         self |         self | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /// Only use HTTP/2. |  | ||||||
|     /// |  | ||||||
|     /// # Example |  | ||||||
|     /// |  | ||||||
|     /// ``` |  | ||||||
|     /// let client = reqwest::blocking::Client::builder() |  | ||||||
|     ///     .h2_prior_knowledge() |  | ||||||
|     ///     .build().unwrap(); |  | ||||||
|     /// ``` |  | ||||||
|     pub fn h2_prior_knowledge(self) -> ClientBuilder { |  | ||||||
|         self.with_inner(|inner| inner.h2_prior_knowledge()) |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     /// Enable case sensitive headers. |  | ||||||
|     /// |  | ||||||
|     /// # Example |  | ||||||
|     /// |  | ||||||
|     /// ``` |  | ||||||
|     /// let client = reqwest::blocking::Client::builder() |  | ||||||
|     ///     .http1_title_case_headers() |  | ||||||
|     ///     .build().unwrap(); |  | ||||||
|     /// ``` |  | ||||||
|     pub fn http1_title_case_headers(self) -> ClientBuilder { |  | ||||||
|         self.with_inner(|inner| inner.http1_title_case_headers()) |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     /// Bind to a local IP Address |  | ||||||
|     /// |  | ||||||
|     /// # Example |  | ||||||
|     /// |  | ||||||
|     /// ``` |  | ||||||
|     /// use std::net::IpAddr; |  | ||||||
|     /// let local_addr = IpAddr::from([12, 4, 1, 8]); |  | ||||||
|     /// let client = reqwest::blocking::Client::builder() |  | ||||||
|     ///     .local_address(local_addr) |  | ||||||
|     ///     .build().unwrap(); |  | ||||||
|     /// ``` |  | ||||||
|     pub fn local_address<T>(self, addr: T) -> ClientBuilder |  | ||||||
|     where |  | ||||||
|         T: Into<Option<IpAddr>>, |  | ||||||
|     { |  | ||||||
|         self.with_inner(move |inner| inner.local_address(addr)) |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     /// Enable a persistent cookie store for the client. |  | ||||||
|     /// |  | ||||||
|     /// Cookies received in responses will be preserved and included in |  | ||||||
|     /// additional requests. |  | ||||||
|     /// |  | ||||||
|     /// By default, no cookie store is used. |  | ||||||
|     /// |  | ||||||
|     /// # Example |  | ||||||
|     /// |  | ||||||
|     /// ``` |  | ||||||
|     /// let client = reqwest::blocking::Client::builder() |  | ||||||
|     ///     .cookie_store(true) |  | ||||||
|     ///     .build() |  | ||||||
|     ///     .unwrap(); |  | ||||||
|     /// ``` |  | ||||||
|     /// |  | ||||||
|     /// # Optional |  | ||||||
|     /// |  | ||||||
|     /// This requires the optional `cookies` feature to be enabled. |  | ||||||
|     #[cfg(feature = "cookies")] |  | ||||||
|     pub fn cookie_store(self, enable: bool) -> ClientBuilder { |  | ||||||
|         self.with_inner(|inner| inner.cookie_store(enable)) |  | ||||||
|     } |  | ||||||
| } | } | ||||||
|  |  | ||||||
| impl Client { | impl Client { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user