reqwest/blocking/client.rs
1#[cfg(any(feature = "native-tls", feature = "__rustls",))]
2use std::any::Any;
3use std::convert::TryInto;
4use std::fmt;
5use std::future::Future;
6use std::net::IpAddr;
7use std::net::SocketAddr;
8use std::sync::Arc;
9use std::task::{ready, Poll};
10use std::thread;
11use std::time::Duration;
12
13use http::header::HeaderValue;
14use log::{error, trace};
15use tokio::sync::{mpsc, oneshot};
16use tower::Layer;
17use tower::Service;
18
19use super::request::{Request, RequestBuilder};
20use super::response::Response;
21use super::wait;
22use crate::connect::sealed::{Conn, Unnameable};
23use crate::connect::BoxedConnectorService;
24use crate::dns::Resolve;
25use crate::error::BoxError;
26#[cfg(feature = "__tls")]
27use crate::tls;
28#[cfg(feature = "__rustls")]
29use crate::tls::CertificateRevocationList;
30#[cfg(feature = "__tls")]
31use crate::Certificate;
32#[cfg(any(feature = "native-tls", feature = "__rustls"))]
33use crate::Identity;
34use crate::{async_impl, header, redirect, IntoUrl, Method, Proxy};
35
36/// A `Client` to make Requests with.
37///
38/// The Client has various configuration values to tweak, but the defaults
39/// are set to what is usually the most commonly desired value. To configure a
40/// `Client`, use `Client::builder()`.
41///
42/// The `Client` holds a connection pool internally, so it is advised that
43/// you create one and **reuse** it.
44///
45/// # Examples
46///
47/// ```rust
48/// use reqwest::blocking::Client;
49/// #
50/// # fn run() -> Result<(), reqwest::Error> {
51/// let client = Client::new();
52/// let resp = client.get("http://httpbin.org/").send()?;
53/// # drop(resp);
54/// # Ok(())
55/// # }
56///
57/// ```
58#[derive(Clone)]
59pub struct Client {
60 inner: ClientHandle,
61}
62
63/// A `ClientBuilder` can be used to create a `Client` with custom configuration.
64///
65/// # Example
66///
67/// ```
68/// # fn run() -> Result<(), reqwest::Error> {
69/// use std::time::Duration;
70///
71/// let client = reqwest::blocking::Client::builder()
72/// .timeout(Duration::from_secs(10))
73/// .build()?;
74/// # Ok(())
75/// # }
76/// ```
77#[must_use]
78pub struct ClientBuilder {
79 inner: async_impl::ClientBuilder,
80 timeout: Timeout,
81}
82
83impl Default for ClientBuilder {
84 fn default() -> Self {
85 Self::new()
86 }
87}
88
89impl ClientBuilder {
90 /// Constructs a new `ClientBuilder`.
91 ///
92 /// This is the same as `Client::builder()`.
93 pub fn new() -> Self {
94 ClientBuilder {
95 inner: async_impl::ClientBuilder::new(),
96 timeout: Timeout::default(),
97 }
98 }
99}
100
101impl ClientBuilder {
102 /// Returns a `Client` that uses this `ClientBuilder` configuration.
103 ///
104 /// # Errors
105 ///
106 /// This method fails if TLS backend cannot be initialized, or the resolver
107 /// cannot load the system configuration.
108 ///
109 /// # Panics
110 ///
111 /// This method panics if called from within an async runtime. See docs on
112 /// [`reqwest::blocking`][crate::blocking] for details.
113 pub fn build(self) -> crate::Result<Client> {
114 ClientHandle::new(self).map(|handle| Client { inner: handle })
115 }
116
117 // Higher-level options
118
119 /// Sets the `User-Agent` header to be used by this client.
120 ///
121 /// # Example
122 ///
123 /// ```rust
124 /// # fn doc() -> Result<(), reqwest::Error> {
125 /// // Name your user agent after your app?
126 /// static APP_USER_AGENT: &str = concat!(
127 /// env!("CARGO_PKG_NAME"),
128 /// "/",
129 /// env!("CARGO_PKG_VERSION"),
130 /// );
131 ///
132 /// let client = reqwest::blocking::Client::builder()
133 /// .user_agent(APP_USER_AGENT)
134 /// .build()?;
135 /// let res = client.get("https://www.rust-lang.org").send()?;
136 /// # Ok(())
137 /// # }
138 /// ```
139 pub fn user_agent<V>(self, value: V) -> ClientBuilder
140 where
141 V: TryInto<HeaderValue>,
142 V::Error: Into<http::Error>,
143 {
144 self.with_inner(move |inner| inner.user_agent(value))
145 }
146
147 /// Sets the default headers for every request.
148 ///
149 /// # Example
150 ///
151 /// ```rust
152 /// use reqwest::header;
153 /// # fn build_client() -> Result<(), reqwest::Error> {
154 /// let mut headers = header::HeaderMap::new();
155 /// headers.insert("X-MY-HEADER", header::HeaderValue::from_static("value"));
156 /// headers.insert(header::AUTHORIZATION, header::HeaderValue::from_static("secret"));
157 ///
158 /// // Consider marking security-sensitive headers with `set_sensitive`.
159 /// let mut auth_value = header::HeaderValue::from_static("secret");
160 /// auth_value.set_sensitive(true);
161 /// headers.insert(header::AUTHORIZATION, auth_value);
162 ///
163 /// // get a client builder
164 /// let client = reqwest::blocking::Client::builder()
165 /// .default_headers(headers)
166 /// .build()?;
167 /// let res = client.get("https://www.rust-lang.org").send()?;
168 /// # Ok(())
169 /// # }
170 /// ```
171 pub fn default_headers(self, headers: header::HeaderMap) -> ClientBuilder {
172 self.with_inner(move |inner| inner.default_headers(headers))
173 }
174
175 /// Enable a persistent cookie store for the client.
176 ///
177 /// Cookies received in responses will be preserved and included in
178 /// additional requests.
179 ///
180 /// By default, no cookie store is used.
181 ///
182 /// # Optional
183 ///
184 /// This requires the optional `cookies` feature to be enabled.
185 #[cfg(feature = "cookies")]
186 #[cfg_attr(docsrs, doc(cfg(feature = "cookies")))]
187 pub fn cookie_store(self, enable: bool) -> ClientBuilder {
188 self.with_inner(|inner| inner.cookie_store(enable))
189 }
190
191 /// Set the persistent cookie store for the client.
192 ///
193 /// Cookies received in responses will be passed to this store, and
194 /// additional requests will query this store for cookies.
195 ///
196 /// By default, no cookie store is used.
197 ///
198 /// # Optional
199 ///
200 /// This requires the optional `cookies` feature to be enabled.
201 #[cfg(feature = "cookies")]
202 #[cfg_attr(docsrs, doc(cfg(feature = "cookies")))]
203 pub fn cookie_provider<C: crate::cookie::CookieStore + 'static>(
204 self,
205 cookie_store: Arc<C>,
206 ) -> ClientBuilder {
207 self.with_inner(|inner| inner.cookie_provider(cookie_store))
208 }
209
210 /// Enable auto gzip decompression by checking the `Content-Encoding` response header.
211 ///
212 /// If auto gzip decompression is turned on:
213 ///
214 /// - When sending a request and if the request's headers do not already contain
215 /// an `Accept-Encoding` **and** `Range` values, the `Accept-Encoding` header is set to `gzip`.
216 /// The request body is **not** automatically compressed.
217 /// - When receiving a response, if it's headers contain a `Content-Encoding` value that
218 /// equals to `gzip`, both values `Content-Encoding` and `Content-Length` are removed from the
219 /// headers' set. The response body is automatically decompressed.
220 ///
221 /// If the `gzip` feature is turned on, the default option is enabled.
222 ///
223 /// # Optional
224 ///
225 /// This requires the optional `gzip` feature to be enabled
226 #[cfg(feature = "gzip")]
227 #[cfg_attr(docsrs, doc(cfg(feature = "gzip")))]
228 pub fn gzip(self, enable: bool) -> ClientBuilder {
229 self.with_inner(|inner| inner.gzip(enable))
230 }
231
232 /// Enable auto brotli decompression by checking the `Content-Encoding` response header.
233 ///
234 /// If auto brotli decompression is turned on:
235 ///
236 /// - When sending a request and if the request's headers do not already contain
237 /// an `Accept-Encoding` **and** `Range` values, the `Accept-Encoding` header is set to `br`.
238 /// The request body is **not** automatically compressed.
239 /// - When receiving a response, if it's headers contain a `Content-Encoding` value that
240 /// equals to `br`, both values `Content-Encoding` and `Content-Length` are removed from the
241 /// headers' set. The response body is automatically decompressed.
242 ///
243 /// If the `brotli` feature is turned on, the default option is enabled.
244 ///
245 /// # Optional
246 ///
247 /// This requires the optional `brotli` feature to be enabled
248 #[cfg(feature = "brotli")]
249 #[cfg_attr(docsrs, doc(cfg(feature = "brotli")))]
250 pub fn brotli(self, enable: bool) -> ClientBuilder {
251 self.with_inner(|inner| inner.brotli(enable))
252 }
253
254 /// Enable auto zstd decompression by checking the `Content-Encoding` response header.
255 ///
256 /// If auto zstd decompression is turned on:
257 ///
258 /// - When sending a request and if the request's headers do not already contain
259 /// an `Accept-Encoding` **and** `Range` values, the `Accept-Encoding` header is set to `zstd`.
260 /// The request body is **not** automatically compressed.
261 /// - When receiving a response, if its headers contain a `Content-Encoding` value of
262 /// `zstd`, both `Content-Encoding` and `Content-Length` are removed from the
263 /// headers' set. The response body is automatically decompressed.
264 ///
265 /// If the `zstd` feature is turned on, the default option is enabled.
266 ///
267 /// # Optional
268 ///
269 /// This requires the optional `zstd` feature to be enabled
270 #[cfg(feature = "zstd")]
271 #[cfg_attr(docsrs, doc(cfg(feature = "zstd")))]
272 pub fn zstd(self, enable: bool) -> ClientBuilder {
273 self.with_inner(|inner| inner.zstd(enable))
274 }
275
276 /// Enable auto deflate decompression by checking the `Content-Encoding` response header.
277 ///
278 /// If auto deflate decompression is turned on:
279 ///
280 /// - When sending a request and if the request's headers do not already contain
281 /// an `Accept-Encoding` **and** `Range` values, the `Accept-Encoding` header is set to `deflate`.
282 /// The request body is **not** automatically compressed.
283 /// - When receiving a response, if it's headers contain a `Content-Encoding` value that
284 /// equals to `deflate`, both values `Content-Encoding` and `Content-Length` are removed from the
285 /// headers' set. The response body is automatically decompressed.
286 ///
287 /// If the `deflate` feature is turned on, the default option is enabled.
288 ///
289 /// # Optional
290 ///
291 /// This requires the optional `deflate` feature to be enabled
292 #[cfg(feature = "deflate")]
293 #[cfg_attr(docsrs, doc(cfg(feature = "deflate")))]
294 pub fn deflate(self, enable: bool) -> ClientBuilder {
295 self.with_inner(|inner| inner.deflate(enable))
296 }
297
298 /// Disable auto response body gzip decompression.
299 ///
300 /// This method exists even if the optional `gzip` feature is not enabled.
301 /// This can be used to ensure a `Client` doesn't use gzip decompression
302 /// even if another dependency were to enable the optional `gzip` feature.
303 pub fn no_gzip(self) -> ClientBuilder {
304 self.with_inner(|inner| inner.no_gzip())
305 }
306
307 /// Disable auto response body brotli decompression.
308 ///
309 /// This method exists even if the optional `brotli` feature is not enabled.
310 /// This can be used to ensure a `Client` doesn't use brotli decompression
311 /// even if another dependency were to enable the optional `brotli` feature.
312 pub fn no_brotli(self) -> ClientBuilder {
313 self.with_inner(|inner| inner.no_brotli())
314 }
315
316 /// Disable auto response body zstd decompression.
317 ///
318 /// This method exists even if the optional `zstd` feature is not enabled.
319 /// This can be used to ensure a `Client` doesn't use zstd decompression
320 /// even if another dependency were to enable the optional `zstd` feature.
321 pub fn no_zstd(self) -> ClientBuilder {
322 self.with_inner(|inner| inner.no_zstd())
323 }
324
325 /// Disable auto response body deflate decompression.
326 ///
327 /// This method exists even if the optional `deflate` feature is not enabled.
328 /// This can be used to ensure a `Client` doesn't use deflate decompression
329 /// even if another dependency were to enable the optional `deflate` feature.
330 pub fn no_deflate(self) -> ClientBuilder {
331 self.with_inner(|inner| inner.no_deflate())
332 }
333
334 // Redirect options
335
336 /// Set a `redirect::Policy` for this client.
337 ///
338 /// Default will follow redirects up to a maximum of 10.
339 pub fn redirect(self, policy: redirect::Policy) -> ClientBuilder {
340 self.with_inner(move |inner| inner.redirect(policy))
341 }
342
343 /// Enable or disable automatic setting of the `Referer` header.
344 ///
345 /// Default is `true`.
346 pub fn referer(self, enable: bool) -> ClientBuilder {
347 self.with_inner(|inner| inner.referer(enable))
348 }
349
350 // Proxy options
351
352 /// Add a `Proxy` to the list of proxies the `Client` will use.
353 ///
354 /// # Note
355 ///
356 /// Adding a proxy will disable the automatic usage of the "system" proxy.
357 pub fn proxy(self, proxy: Proxy) -> ClientBuilder {
358 self.with_inner(move |inner| inner.proxy(proxy))
359 }
360
361 /// Clear all `Proxies`, so `Client` will use no proxy anymore.
362 ///
363 /// # Note
364 /// To add a proxy exclusion list, use [Proxy::no_proxy()]
365 /// on all desired proxies instead.
366 ///
367 /// This also disables the automatic usage of the "system" proxy.
368 pub fn no_proxy(self) -> ClientBuilder {
369 self.with_inner(move |inner| inner.no_proxy())
370 }
371
372 // Timeout options
373
374 /// Set a timeout for connect, read and write operations of a `Client`.
375 ///
376 /// Default is 30 seconds.
377 ///
378 /// Pass `None` to disable timeout.
379 pub fn timeout<T>(mut self, timeout: T) -> ClientBuilder
380 where
381 T: Into<Option<Duration>>,
382 {
383 self.timeout = Timeout(timeout.into());
384 self
385 }
386
387 /// Set a timeout for only the connect phase of a `Client`.
388 ///
389 /// Default is `None`.
390 pub fn connect_timeout<T>(self, timeout: T) -> ClientBuilder
391 where
392 T: Into<Option<Duration>>,
393 {
394 let timeout = timeout.into();
395 if let Some(dur) = timeout {
396 self.with_inner(|inner| inner.connect_timeout(dur))
397 } else {
398 self
399 }
400 }
401
402 /// Set whether connections should emit verbose logs.
403 ///
404 /// Enabling this option will emit [log][] messages at the `TRACE` level
405 /// for read and write operations on connections.
406 ///
407 /// [log]: https://crates.io/crates/log
408 pub fn connection_verbose(self, verbose: bool) -> ClientBuilder {
409 self.with_inner(move |inner| inner.connection_verbose(verbose))
410 }
411
412 // HTTP options
413
414 /// Set an optional timeout for idle sockets being kept-alive.
415 ///
416 /// Pass `None` to disable timeout.
417 ///
418 /// Default is 90 seconds.
419 pub fn pool_idle_timeout<D>(self, val: D) -> ClientBuilder
420 where
421 D: Into<Option<Duration>>,
422 {
423 self.with_inner(|inner| inner.pool_idle_timeout(val))
424 }
425
426 /// Sets the maximum idle connection per host allowed in the pool.
427 pub fn pool_max_idle_per_host(self, max: usize) -> ClientBuilder {
428 self.with_inner(move |inner| inner.pool_max_idle_per_host(max))
429 }
430
431 /// Send headers as title case instead of lowercase.
432 pub fn http1_title_case_headers(self) -> ClientBuilder {
433 self.with_inner(|inner| inner.http1_title_case_headers())
434 }
435
436 /// Set whether HTTP/1 connections will accept obsolete line folding for
437 /// header values.
438 ///
439 /// Newline codepoints (`\r` and `\n`) will be transformed to spaces when
440 /// parsing.
441 pub fn http1_allow_obsolete_multiline_headers_in_responses(self, value: bool) -> ClientBuilder {
442 self.with_inner(|inner| inner.http1_allow_obsolete_multiline_headers_in_responses(value))
443 }
444
445 /// Sets whether invalid header lines should be silently ignored in HTTP/1 responses.
446 pub fn http1_ignore_invalid_headers_in_responses(self, value: bool) -> ClientBuilder {
447 self.with_inner(|inner| inner.http1_ignore_invalid_headers_in_responses(value))
448 }
449
450 /// Set whether HTTP/1 connections will accept spaces between header
451 /// names and the colon that follow them in responses.
452 ///
453 /// Newline codepoints (\r and \n) will be transformed to spaces when
454 /// parsing.
455 pub fn http1_allow_spaces_after_header_name_in_responses(self, value: bool) -> ClientBuilder {
456 self.with_inner(|inner| inner.http1_allow_spaces_after_header_name_in_responses(value))
457 }
458
459 /// Only use HTTP/1.
460 pub fn http1_only(self) -> ClientBuilder {
461 self.with_inner(|inner| inner.http1_only())
462 }
463
464 /// Allow HTTP/0.9 responses
465 pub fn http09_responses(self) -> ClientBuilder {
466 self.with_inner(|inner| inner.http09_responses())
467 }
468
469 /// Only use HTTP/2.
470 #[cfg(feature = "http2")]
471 #[cfg_attr(docsrs, doc(cfg(feature = "http2")))]
472 pub fn http2_prior_knowledge(self) -> ClientBuilder {
473 self.with_inner(|inner| inner.http2_prior_knowledge())
474 }
475
476 /// Sets the `SETTINGS_INITIAL_WINDOW_SIZE` option for HTTP2 stream-level flow control.
477 ///
478 /// Default is currently 65,535 but may change internally to optimize for common uses.
479 #[cfg(feature = "http2")]
480 #[cfg_attr(docsrs, doc(cfg(feature = "http2")))]
481 pub fn http2_initial_stream_window_size(self, sz: impl Into<Option<u32>>) -> ClientBuilder {
482 self.with_inner(|inner| inner.http2_initial_stream_window_size(sz))
483 }
484
485 /// Sets the max connection-level flow control for HTTP2
486 ///
487 /// Default is currently 65,535 but may change internally to optimize for common uses.
488 #[cfg(feature = "http2")]
489 #[cfg_attr(docsrs, doc(cfg(feature = "http2")))]
490 pub fn http2_initial_connection_window_size(self, sz: impl Into<Option<u32>>) -> ClientBuilder {
491 self.with_inner(|inner| inner.http2_initial_connection_window_size(sz))
492 }
493
494 /// Sets whether to use an adaptive flow control.
495 ///
496 /// Enabling this will override the limits set in `http2_initial_stream_window_size` and
497 /// `http2_initial_connection_window_size`.
498 #[cfg(feature = "http2")]
499 #[cfg_attr(docsrs, doc(cfg(feature = "http2")))]
500 pub fn http2_adaptive_window(self, enabled: bool) -> ClientBuilder {
501 self.with_inner(|inner| inner.http2_adaptive_window(enabled))
502 }
503
504 /// Sets the maximum frame size to use for HTTP2.
505 ///
506 /// Default is currently 16,384 but may change internally to optimize for common uses.
507 #[cfg(feature = "http2")]
508 #[cfg_attr(docsrs, doc(cfg(feature = "http2")))]
509 pub fn http2_max_frame_size(self, sz: impl Into<Option<u32>>) -> ClientBuilder {
510 self.with_inner(|inner| inner.http2_max_frame_size(sz))
511 }
512
513 /// Sets the maximum size of received header frames for HTTP2.
514 ///
515 /// Default is currently 16KB, but can change.
516 #[cfg(feature = "http2")]
517 #[cfg_attr(docsrs, doc(cfg(feature = "http2")))]
518 pub fn http2_max_header_list_size(self, max_header_size_bytes: u32) -> ClientBuilder {
519 self.with_inner(|inner| inner.http2_max_header_list_size(max_header_size_bytes))
520 }
521
522 /// This requires the optional `http3` feature to be
523 /// enabled.
524 #[cfg(feature = "http3")]
525 #[cfg_attr(docsrs, doc(cfg(feature = "http3")))]
526 pub fn http3_prior_knowledge(self) -> ClientBuilder {
527 self.with_inner(|inner| inner.http3_prior_knowledge())
528 }
529
530 // TCP options
531
532 /// Set whether sockets have `TCP_NODELAY` enabled.
533 ///
534 /// Default is `true`.
535 pub fn tcp_nodelay(self, enabled: bool) -> ClientBuilder {
536 self.with_inner(move |inner| inner.tcp_nodelay(enabled))
537 }
538
539 /// Bind to a local IP Address.
540 ///
541 /// # Example
542 ///
543 /// ```
544 /// use std::net::IpAddr;
545 /// let local_addr = IpAddr::from([12, 4, 1, 8]);
546 /// let client = reqwest::blocking::Client::builder()
547 /// .local_address(local_addr)
548 /// .build().unwrap();
549 /// ```
550 pub fn local_address<T>(self, addr: T) -> ClientBuilder
551 where
552 T: Into<Option<IpAddr>>,
553 {
554 self.with_inner(move |inner| inner.local_address(addr))
555 }
556
557 /// Bind to an interface by `SO_BINDTODEVICE`.
558 ///
559 /// # Example
560 ///
561 /// ```
562 /// let interface = "lo";
563 /// let client = reqwest::blocking::Client::builder()
564 /// .interface(interface)
565 /// .build().unwrap();
566 /// ```
567 #[cfg(any(
568 target_os = "android",
569 target_os = "fuchsia",
570 target_os = "illumos",
571 target_os = "ios",
572 target_os = "linux",
573 target_os = "macos",
574 target_os = "solaris",
575 target_os = "tvos",
576 target_os = "visionos",
577 target_os = "watchos",
578 ))]
579 pub fn interface(self, interface: &str) -> ClientBuilder {
580 self.with_inner(move |inner| inner.interface(interface))
581 }
582
583 /// Set that all sockets have `SO_KEEPALIVE` set with the supplied duration.
584 ///
585 /// If `None`, the option will not be set.
586 pub fn tcp_keepalive<D>(self, val: D) -> ClientBuilder
587 where
588 D: Into<Option<Duration>>,
589 {
590 self.with_inner(move |inner| inner.tcp_keepalive(val))
591 }
592
593 /// Set that all sockets have `SO_KEEPALIVE` set with the supplied interval.
594 ///
595 /// If `None`, the option will not be set.
596 pub fn tcp_keepalive_interval<D>(self, val: D) -> ClientBuilder
597 where
598 D: Into<Option<Duration>>,
599 {
600 self.with_inner(move |inner| inner.tcp_keepalive_interval(val))
601 }
602
603 /// Set that all sockets have `SO_KEEPALIVE` set with the supplied retry count.
604 ///
605 /// If `None`, the option will not be set.
606 pub fn tcp_keepalive_retries<C>(self, retries: C) -> ClientBuilder
607 where
608 C: Into<Option<u32>>,
609 {
610 self.with_inner(move |inner| inner.tcp_keepalive_retries(retries))
611 }
612
613 /// Set that all sockets have `TCP_USER_TIMEOUT` set with the supplied duration.
614 ///
615 /// This option controls how long transmitted data may remain unacknowledged before
616 /// the connection is force-closed.
617 ///
618 /// The current default is `None` (option disabled).
619 #[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux"))]
620 pub fn tcp_user_timeout<D>(self, val: D) -> ClientBuilder
621 where
622 D: Into<Option<Duration>>,
623 {
624 self.with_inner(move |inner| inner.tcp_user_timeout(val))
625 }
626
627 // TLS options
628
629 /// Add a custom root certificate.
630 ///
631 /// This allows connecting to a server that has a self-signed
632 /// certificate for example. This **does not** replace the existing
633 /// trusted store.
634 ///
635 /// # Example
636 ///
637 /// ```
638 /// # use std::fs::File;
639 /// # use std::io::Read;
640 /// # fn build_client() -> Result<(), Box<dyn std::error::Error>> {
641 /// // read a local binary DER encoded certificate
642 /// let der = std::fs::read("my-cert.der")?;
643 ///
644 /// // create a certificate
645 /// let cert = reqwest::Certificate::from_der(&der)?;
646 ///
647 /// // get a client builder
648 /// let client = reqwest::blocking::Client::builder()
649 /// .add_root_certificate(cert)
650 /// .build()?;
651 /// # drop(client);
652 /// # Ok(())
653 /// # }
654 /// ```
655 ///
656 /// # Optional
657 ///
658 /// This requires the optional `default-tls`, `native-tls`, or `rustls-tls(-...)`
659 /// feature to be enabled.
660 #[cfg(feature = "__tls")]
661 #[cfg_attr(
662 docsrs,
663 doc(cfg(any(
664 feature = "default-tls",
665 feature = "native-tls",
666 feature = "rustls-tls"
667 )))
668 )]
669 pub fn add_root_certificate(self, cert: Certificate) -> ClientBuilder {
670 self.with_inner(move |inner| inner.add_root_certificate(cert))
671 }
672
673 /// Add a certificate revocation list.
674 ///
675 ///
676 /// # Optional
677 ///
678 /// This requires the `rustls-tls(-...)` Cargo feature enabled.
679 #[cfg(feature = "__rustls")]
680 #[cfg_attr(docsrs, doc(cfg(feature = "rustls-tls")))]
681 pub fn add_crl(self, crl: CertificateRevocationList) -> ClientBuilder {
682 self.with_inner(move |inner| inner.add_crl(crl))
683 }
684
685 /// Add multiple certificate revocation lists.
686 ///
687 ///
688 /// # Optional
689 ///
690 /// This requires the `rustls-tls(-...)` Cargo feature enabled.
691 #[cfg(feature = "__rustls")]
692 #[cfg_attr(docsrs, doc(cfg(feature = "rustls-tls")))]
693 pub fn add_crls(
694 self,
695 crls: impl IntoIterator<Item = CertificateRevocationList>,
696 ) -> ClientBuilder {
697 self.with_inner(move |inner| inner.add_crls(crls))
698 }
699
700 /// Controls the use of built-in system certificates during certificate validation.
701 ///
702 /// Defaults to `true` -- built-in system certs will be used.
703 ///
704 /// # Optional
705 ///
706 /// This requires the optional `default-tls`, `native-tls`, or `rustls-tls(-...)`
707 /// feature to be enabled.
708 #[cfg(feature = "__tls")]
709 #[cfg_attr(
710 docsrs,
711 doc(cfg(any(
712 feature = "default-tls",
713 feature = "native-tls",
714 feature = "rustls-tls"
715 )))
716 )]
717 pub fn tls_built_in_root_certs(self, tls_built_in_root_certs: bool) -> ClientBuilder {
718 self.with_inner(move |inner| inner.tls_built_in_root_certs(tls_built_in_root_certs))
719 }
720
721 /// Sets whether to load webpki root certs with rustls.
722 ///
723 /// If the feature is enabled, this value is `true` by default.
724 #[cfg(feature = "rustls-tls-webpki-roots-no-provider")]
725 #[cfg_attr(docsrs, doc(cfg(feature = "rustls-tls-webpki-roots-no-provider")))]
726 pub fn tls_built_in_webpki_certs(self, enabled: bool) -> ClientBuilder {
727 self.with_inner(move |inner| inner.tls_built_in_webpki_certs(enabled))
728 }
729
730 /// Sets whether to load native root certs with rustls.
731 ///
732 /// If the feature is enabled, this value is `true` by default.
733 #[cfg(feature = "rustls-tls-native-roots-no-provider")]
734 #[cfg_attr(docsrs, doc(cfg(feature = "rustls-tls-native-roots-no-provider")))]
735 pub fn tls_built_in_native_certs(self, enabled: bool) -> ClientBuilder {
736 self.with_inner(move |inner| inner.tls_built_in_native_certs(enabled))
737 }
738
739 /// Sets the identity to be used for client certificate authentication.
740 ///
741 /// # Optional
742 ///
743 /// This requires the optional `native-tls` or `rustls-tls(-...)` feature to be
744 /// enabled.
745 #[cfg(any(feature = "native-tls", feature = "__rustls"))]
746 #[cfg_attr(docsrs, doc(cfg(any(feature = "native-tls", feature = "rustls-tls"))))]
747 pub fn identity(self, identity: Identity) -> ClientBuilder {
748 self.with_inner(move |inner| inner.identity(identity))
749 }
750
751 /// Controls the use of hostname verification.
752 ///
753 /// Defaults to `false`.
754 ///
755 /// # Warning
756 ///
757 /// You should think very carefully before you use this method. If
758 /// hostname verification is not used, any valid certificate for any
759 /// site will be trusted for use from any other. This introduces a
760 /// significant vulnerability to man-in-the-middle attacks.
761 ///
762 /// # Optional
763 ///
764 /// This requires the optional `default-tls`, `native-tls`, or `rustls-tls(-...)`
765 /// feature to be enabled.
766 #[cfg(feature = "__tls")]
767 #[cfg_attr(
768 docsrs,
769 doc(cfg(any(
770 feature = "default-tls",
771 feature = "native-tls",
772 feature = "rustls-tls"
773 )))
774 )]
775 pub fn danger_accept_invalid_hostnames(self, accept_invalid_hostname: bool) -> ClientBuilder {
776 self.with_inner(|inner| inner.danger_accept_invalid_hostnames(accept_invalid_hostname))
777 }
778
779 /// Controls the use of certificate validation.
780 ///
781 /// Defaults to `false`.
782 ///
783 /// # Warning
784 ///
785 /// You should think very carefully before using this method. If
786 /// invalid certificates are trusted, *any* certificate for *any* site
787 /// will be trusted for use. This includes expired certificates. This
788 /// introduces significant vulnerabilities, and should only be used
789 /// as a last resort.
790 #[cfg(feature = "__tls")]
791 #[cfg_attr(
792 docsrs,
793 doc(cfg(any(
794 feature = "default-tls",
795 feature = "native-tls",
796 feature = "rustls-tls"
797 )))
798 )]
799 pub fn danger_accept_invalid_certs(self, accept_invalid_certs: bool) -> ClientBuilder {
800 self.with_inner(|inner| inner.danger_accept_invalid_certs(accept_invalid_certs))
801 }
802
803 /// Controls the use of TLS server name indication.
804 ///
805 /// Defaults to `true`.
806 #[cfg(feature = "__tls")]
807 #[cfg_attr(
808 docsrs,
809 doc(cfg(any(
810 feature = "default-tls",
811 feature = "native-tls",
812 feature = "rustls-tls"
813 )))
814 )]
815 pub fn tls_sni(self, tls_sni: bool) -> ClientBuilder {
816 self.with_inner(|inner| inner.tls_sni(tls_sni))
817 }
818
819 /// Set the minimum required TLS version for connections.
820 ///
821 /// By default, the TLS backend's own default is used.
822 ///
823 /// # Errors
824 ///
825 /// A value of `tls::Version::TLS_1_3` will cause an error with the
826 /// `native-tls`/`default-tls` backend. This does not mean the version
827 /// isn't supported, just that it can't be set as a minimum due to
828 /// technical limitations.
829 ///
830 /// # Optional
831 ///
832 /// This requires the optional `default-tls`, `native-tls`, or `rustls-tls(-...)`
833 /// feature to be enabled.
834 #[cfg(feature = "__tls")]
835 #[cfg_attr(
836 docsrs,
837 doc(cfg(any(
838 feature = "default-tls",
839 feature = "native-tls",
840 feature = "rustls-tls"
841 )))
842 )]
843 pub fn min_tls_version(self, version: tls::Version) -> ClientBuilder {
844 self.with_inner(|inner| inner.min_tls_version(version))
845 }
846
847 /// Set the maximum allowed TLS version for connections.
848 ///
849 /// By default, there's no maximum.
850 ///
851 /// # Errors
852 ///
853 /// A value of `tls::Version::TLS_1_3` will cause an error with the
854 /// `native-tls`/`default-tls` backend. This does not mean the version
855 /// isn't supported, just that it can't be set as a maximum due to
856 /// technical limitations.
857 ///
858 /// # Optional
859 ///
860 /// This requires the optional `default-tls`, `native-tls`, or `rustls-tls(-...)`
861 /// feature to be enabled.
862 #[cfg(feature = "__tls")]
863 #[cfg_attr(
864 docsrs,
865 doc(cfg(any(
866 feature = "default-tls",
867 feature = "native-tls",
868 feature = "rustls-tls"
869 )))
870 )]
871 pub fn max_tls_version(self, version: tls::Version) -> ClientBuilder {
872 self.with_inner(|inner| inner.max_tls_version(version))
873 }
874
875 /// Force using the native TLS backend.
876 ///
877 /// Since multiple TLS backends can be optionally enabled, this option will
878 /// force the `native-tls` backend to be used for this `Client`.
879 ///
880 /// # Optional
881 ///
882 /// This requires the optional `native-tls` feature to be enabled.
883 #[cfg(feature = "native-tls")]
884 #[cfg_attr(docsrs, doc(cfg(feature = "native-tls")))]
885 pub fn use_native_tls(self) -> ClientBuilder {
886 self.with_inner(move |inner| inner.use_native_tls())
887 }
888
889 /// Force using the Rustls TLS backend.
890 ///
891 /// Since multiple TLS backends can be optionally enabled, this option will
892 /// force the `rustls` backend to be used for this `Client`.
893 ///
894 /// # Optional
895 ///
896 /// This requires the optional `rustls-tls(-...)` feature to be enabled.
897 #[cfg(feature = "__rustls")]
898 #[cfg_attr(docsrs, doc(cfg(feature = "rustls-tls")))]
899 pub fn use_rustls_tls(self) -> ClientBuilder {
900 self.with_inner(move |inner| inner.use_rustls_tls())
901 }
902
903 /// Add TLS information as `TlsInfo` extension to responses.
904 ///
905 /// # Optional
906 ///
907 /// This requires the optional `default-tls`, `native-tls`, or `rustls-tls(-...)`
908 /// feature to be enabled.
909 #[cfg(feature = "__tls")]
910 #[cfg_attr(
911 docsrs,
912 doc(cfg(any(
913 feature = "default-tls",
914 feature = "native-tls",
915 feature = "rustls-tls"
916 )))
917 )]
918 pub fn tls_info(self, tls_info: bool) -> ClientBuilder {
919 self.with_inner(|inner| inner.tls_info(tls_info))
920 }
921
922 /// Use a preconfigured TLS backend.
923 ///
924 /// If the passed `Any` argument is not a TLS backend that reqwest
925 /// understands, the `ClientBuilder` will error when calling `build`.
926 ///
927 /// # Advanced
928 ///
929 /// This is an advanced option, and can be somewhat brittle. Usage requires
930 /// keeping the preconfigured TLS argument version in sync with reqwest,
931 /// since version mismatches will result in an "unknown" TLS backend.
932 ///
933 /// If possible, it's preferable to use the methods on `ClientBuilder`
934 /// to configure reqwest's TLS.
935 ///
936 /// # Optional
937 ///
938 /// This requires one of the optional features `native-tls` or
939 /// `rustls-tls(-...)` to be enabled.
940 #[cfg(any(feature = "native-tls", feature = "__rustls",))]
941 #[cfg_attr(docsrs, doc(cfg(any(feature = "native-tls", feature = "rustls-tls"))))]
942 pub fn use_preconfigured_tls(self, tls: impl Any) -> ClientBuilder {
943 self.with_inner(move |inner| inner.use_preconfigured_tls(tls))
944 }
945
946 /// Enables the [hickory-dns](hickory_resolver) async resolver instead of a default threadpool using `getaddrinfo`.
947 ///
948 /// If the `hickory-dns` feature is turned on, the default option is enabled.
949 ///
950 /// # Optional
951 ///
952 /// This requires the optional `hickory-dns` feature to be enabled
953 #[cfg(feature = "hickory-dns")]
954 #[cfg_attr(docsrs, doc(cfg(feature = "hickory-dns")))]
955 #[deprecated(note = "use `hickory_dns` instead", since = "0.12.0")]
956 pub fn trust_dns(self, enable: bool) -> ClientBuilder {
957 self.with_inner(|inner| inner.hickory_dns(enable))
958 }
959
960 /// Enables the [hickory-dns](hickory_resolver) async resolver instead of a default threadpool using `getaddrinfo`.
961 ///
962 /// If the `hickory-dns` feature is turned on, the default option is enabled.
963 ///
964 /// # Optional
965 ///
966 /// This requires the optional `hickory-dns` feature to be enabled
967 #[cfg(feature = "hickory-dns")]
968 #[cfg_attr(docsrs, doc(cfg(feature = "hickory-dns")))]
969 pub fn hickory_dns(self, enable: bool) -> ClientBuilder {
970 self.with_inner(|inner| inner.hickory_dns(enable))
971 }
972
973 /// Disables the hickory-dns async resolver.
974 ///
975 /// This method exists even if the optional `hickory-dns` feature is not enabled.
976 /// This can be used to ensure a `Client` doesn't use the hickory-dns async resolver
977 /// even if another dependency were to enable the optional `hickory-dns` feature.
978 #[deprecated(note = "use `no_hickory_dns` instead", since = "0.12.0")]
979 pub fn no_trust_dns(self) -> ClientBuilder {
980 self.with_inner(|inner| inner.no_hickory_dns())
981 }
982
983 /// Disables the hickory-dns async resolver.
984 ///
985 /// This method exists even if the optional `hickory-dns` feature is not enabled.
986 /// This can be used to ensure a `Client` doesn't use the hickory-dns async resolver
987 /// even if another dependency were to enable the optional `hickory-dns` feature.
988 pub fn no_hickory_dns(self) -> ClientBuilder {
989 self.with_inner(|inner| inner.no_hickory_dns())
990 }
991
992 /// Restrict the Client to be used with HTTPS only requests.
993 ///
994 /// Defaults to false.
995 pub fn https_only(self, enabled: bool) -> ClientBuilder {
996 self.with_inner(|inner| inner.https_only(enabled))
997 }
998
999 /// Override DNS resolution for specific domains to a particular IP address.
1000 ///
1001 /// Set the port to `0` to use the conventional port for the given scheme (e.g. 80 for http).
1002 /// Ports in the URL itself will always be used instead of the port in the overridden addr.
1003 pub fn resolve(self, domain: &str, addr: SocketAddr) -> ClientBuilder {
1004 self.resolve_to_addrs(domain, &[addr])
1005 }
1006
1007 /// Override DNS resolution for specific domains to particular IP addresses.
1008 ///
1009 /// Set the port to `0` to use the conventional port for the given scheme (e.g. 80 for http).
1010 /// Ports in the URL itself will always be used instead of the port in the overridden addr.
1011 pub fn resolve_to_addrs(self, domain: &str, addrs: &[SocketAddr]) -> ClientBuilder {
1012 self.with_inner(|inner| inner.resolve_to_addrs(domain, addrs))
1013 }
1014
1015 /// Override the DNS resolver implementation.
1016 ///
1017 /// Pass an `Arc` wrapping a trait object implementing `Resolve`.
1018 /// Overrides for specific names passed to `resolve` and `resolve_to_addrs` will
1019 /// still be applied on top of this resolver.
1020 pub fn dns_resolver<R: Resolve + 'static>(self, resolver: Arc<R>) -> ClientBuilder {
1021 self.with_inner(|inner| inner.dns_resolver(resolver))
1022 }
1023
1024 /// Adds a new Tower [`Layer`](https://docs.rs/tower/latest/tower/trait.Layer.html) to the
1025 /// base connector [`Service`](https://docs.rs/tower/latest/tower/trait.Service.html) which
1026 /// is responsible for connection establishment.
1027 ///
1028 /// Each subsequent invocation of this function will wrap previous layers.
1029 ///
1030 /// Example usage:
1031 /// ```
1032 /// use std::time::Duration;
1033 ///
1034 /// let client = reqwest::blocking::Client::builder()
1035 /// // resolved to outermost layer, meaning while we are waiting on concurrency limit
1036 /// .connect_timeout(Duration::from_millis(200))
1037 /// // underneath the concurrency check, so only after concurrency limit lets us through
1038 /// .connector_layer(tower::timeout::TimeoutLayer::new(Duration::from_millis(50)))
1039 /// .connector_layer(tower::limit::concurrency::ConcurrencyLimitLayer::new(2))
1040 /// .build()
1041 /// .unwrap();
1042 /// ```
1043 pub fn connector_layer<L>(self, layer: L) -> ClientBuilder
1044 where
1045 L: Layer<BoxedConnectorService> + Clone + Send + Sync + 'static,
1046 L::Service:
1047 Service<Unnameable, Response = Conn, Error = BoxError> + Clone + Send + Sync + 'static,
1048 <L::Service as Service<Unnameable>>::Future: Send + 'static,
1049 {
1050 self.with_inner(|inner| inner.connector_layer(layer))
1051 }
1052
1053 // private
1054
1055 fn with_inner<F>(mut self, func: F) -> ClientBuilder
1056 where
1057 F: FnOnce(async_impl::ClientBuilder) -> async_impl::ClientBuilder,
1058 {
1059 self.inner = func(self.inner);
1060 self
1061 }
1062}
1063
1064impl From<async_impl::ClientBuilder> for ClientBuilder {
1065 fn from(builder: async_impl::ClientBuilder) -> Self {
1066 Self {
1067 inner: builder,
1068 timeout: Timeout::default(),
1069 }
1070 }
1071}
1072
1073impl Default for Client {
1074 fn default() -> Self {
1075 Self::new()
1076 }
1077}
1078
1079impl Client {
1080 /// Constructs a new `Client`.
1081 ///
1082 /// # Panic
1083 ///
1084 /// This method panics if TLS backend cannot be initialized, or the resolver
1085 /// cannot load the system configuration.
1086 ///
1087 /// Use `Client::builder()` if you wish to handle the failure as an `Error`
1088 /// instead of panicking.
1089 ///
1090 /// This method also panics if called from within an async runtime. See docs
1091 /// on [`reqwest::blocking`][crate::blocking] for details.
1092 pub fn new() -> Client {
1093 ClientBuilder::new().build().expect("Client::new()")
1094 }
1095
1096 /// Creates a `ClientBuilder` to configure a `Client`.
1097 ///
1098 /// This is the same as `ClientBuilder::new()`.
1099 pub fn builder() -> ClientBuilder {
1100 ClientBuilder::new()
1101 }
1102
1103 /// Convenience method to make a `GET` request to a URL.
1104 ///
1105 /// # Errors
1106 ///
1107 /// This method fails whenever supplied `Url` cannot be parsed.
1108 pub fn get<U: IntoUrl>(&self, url: U) -> RequestBuilder {
1109 self.request(Method::GET, url)
1110 }
1111
1112 /// Convenience method to make a `POST` request to a URL.
1113 ///
1114 /// # Errors
1115 ///
1116 /// This method fails whenever supplied `Url` cannot be parsed.
1117 pub fn post<U: IntoUrl>(&self, url: U) -> RequestBuilder {
1118 self.request(Method::POST, url)
1119 }
1120
1121 /// Convenience method to make a `PUT` request to a URL.
1122 ///
1123 /// # Errors
1124 ///
1125 /// This method fails whenever supplied `Url` cannot be parsed.
1126 pub fn put<U: IntoUrl>(&self, url: U) -> RequestBuilder {
1127 self.request(Method::PUT, url)
1128 }
1129
1130 /// Convenience method to make a `PATCH` request to a URL.
1131 ///
1132 /// # Errors
1133 ///
1134 /// This method fails whenever supplied `Url` cannot be parsed.
1135 pub fn patch<U: IntoUrl>(&self, url: U) -> RequestBuilder {
1136 self.request(Method::PATCH, url)
1137 }
1138
1139 /// Convenience method to make a `DELETE` request to a URL.
1140 ///
1141 /// # Errors
1142 ///
1143 /// This method fails whenever supplied `Url` cannot be parsed.
1144 pub fn delete<U: IntoUrl>(&self, url: U) -> RequestBuilder {
1145 self.request(Method::DELETE, url)
1146 }
1147
1148 /// Convenience method to make a `HEAD` request to a URL.
1149 ///
1150 /// # Errors
1151 ///
1152 /// This method fails whenever supplied `Url` cannot be parsed.
1153 pub fn head<U: IntoUrl>(&self, url: U) -> RequestBuilder {
1154 self.request(Method::HEAD, url)
1155 }
1156
1157 /// Start building a `Request` with the `Method` and `Url`.
1158 ///
1159 /// Returns a `RequestBuilder`, which will allow setting headers and
1160 /// request body before sending.
1161 ///
1162 /// # Errors
1163 ///
1164 /// This method fails whenever supplied `Url` cannot be parsed.
1165 pub fn request<U: IntoUrl>(&self, method: Method, url: U) -> RequestBuilder {
1166 let req = url.into_url().map(move |url| Request::new(method, url));
1167 RequestBuilder::new(self.clone(), req)
1168 }
1169
1170 /// Executes a `Request`.
1171 ///
1172 /// A `Request` can be built manually with `Request::new()` or obtained
1173 /// from a RequestBuilder with `RequestBuilder::build()`.
1174 ///
1175 /// You should prefer to use the `RequestBuilder` and
1176 /// `RequestBuilder::send()`.
1177 ///
1178 /// # Errors
1179 ///
1180 /// This method fails if there was an error while sending request,
1181 /// or redirect limit was exhausted.
1182 pub fn execute(&self, request: Request) -> crate::Result<Response> {
1183 self.inner.execute_request(request)
1184 }
1185}
1186
1187impl fmt::Debug for Client {
1188 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1189 f.debug_struct("Client")
1190 //.field("gzip", &self.inner.gzip)
1191 //.field("redirect_policy", &self.inner.redirect_policy)
1192 //.field("referer", &self.inner.referer)
1193 .finish()
1194 }
1195}
1196
1197impl fmt::Debug for ClientBuilder {
1198 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1199 self.inner.fmt(f)
1200 }
1201}
1202
1203#[derive(Clone)]
1204struct ClientHandle {
1205 timeout: Timeout,
1206 inner: Arc<InnerClientHandle>,
1207}
1208
1209type OneshotResponse = oneshot::Sender<crate::Result<async_impl::Response>>;
1210type ThreadSender = mpsc::UnboundedSender<(async_impl::Request, OneshotResponse)>;
1211
1212struct InnerClientHandle {
1213 tx: Option<ThreadSender>,
1214 thread: Option<thread::JoinHandle<()>>,
1215}
1216
1217impl Drop for InnerClientHandle {
1218 fn drop(&mut self) {
1219 let id = self
1220 .thread
1221 .as_ref()
1222 .map(|h| h.thread().id())
1223 .expect("thread not dropped yet");
1224
1225 trace!("closing runtime thread ({id:?})");
1226 self.tx.take();
1227 trace!("signaled close for runtime thread ({id:?})");
1228 self.thread.take().map(|h| h.join());
1229 trace!("closed runtime thread ({id:?})");
1230 }
1231}
1232
1233impl ClientHandle {
1234 fn new(builder: ClientBuilder) -> crate::Result<ClientHandle> {
1235 let timeout = builder.timeout;
1236 let builder = builder.inner;
1237 let (tx, rx) = mpsc::unbounded_channel::<(async_impl::Request, OneshotResponse)>();
1238 let (spawn_tx, spawn_rx) = oneshot::channel::<crate::Result<()>>();
1239 let handle = thread::Builder::new()
1240 .name("reqwest-internal-sync-runtime".into())
1241 .spawn(move || {
1242 use tokio::runtime;
1243 let rt = match runtime::Builder::new_current_thread()
1244 .enable_all()
1245 .build()
1246 .map_err(crate::error::builder)
1247 {
1248 Err(e) => {
1249 if let Err(e) = spawn_tx.send(Err(e)) {
1250 error!("Failed to communicate runtime creation failure: {e:?}");
1251 }
1252 return;
1253 }
1254 Ok(v) => v,
1255 };
1256
1257 let f = async move {
1258 let client = match builder.build() {
1259 Err(e) => {
1260 if let Err(e) = spawn_tx.send(Err(e)) {
1261 error!("Failed to communicate client creation failure: {e:?}");
1262 }
1263 return;
1264 }
1265 Ok(v) => v,
1266 };
1267 if let Err(e) = spawn_tx.send(Ok(())) {
1268 error!("Failed to communicate successful startup: {e:?}");
1269 return;
1270 }
1271
1272 let mut rx = rx;
1273
1274 while let Some((req, req_tx)) = rx.recv().await {
1275 let req_fut = client.execute(req);
1276 tokio::spawn(forward(req_fut, req_tx));
1277 }
1278
1279 trace!("({:?}) Receiver is shutdown", thread::current().id());
1280 };
1281
1282 trace!("({:?}) start runtime::block_on", thread::current().id());
1283 rt.block_on(f);
1284 trace!("({:?}) end runtime::block_on", thread::current().id());
1285 drop(rt);
1286 trace!("({:?}) finished", thread::current().id());
1287 })
1288 .map_err(crate::error::builder)?;
1289
1290 // Wait for the runtime thread to start up...
1291 match wait::timeout(spawn_rx, None) {
1292 Ok(Ok(())) => (),
1293 Ok(Err(err)) => return Err(err),
1294 Err(_canceled) => event_loop_panicked(),
1295 }
1296
1297 let inner_handle = Arc::new(InnerClientHandle {
1298 tx: Some(tx),
1299 thread: Some(handle),
1300 });
1301
1302 Ok(ClientHandle {
1303 timeout,
1304 inner: inner_handle,
1305 })
1306 }
1307
1308 fn execute_request(&self, req: Request) -> crate::Result<Response> {
1309 let (tx, rx) = oneshot::channel();
1310 let (req, body) = req.into_async();
1311 let url = req.url().clone();
1312 let timeout = req.timeout().copied().or(self.timeout.0);
1313
1314 self.inner
1315 .tx
1316 .as_ref()
1317 .expect("core thread exited early")
1318 .send((req, tx))
1319 .expect("core thread panicked");
1320
1321 let result: Result<crate::Result<async_impl::Response>, wait::Waited<crate::Error>> =
1322 if let Some(body) = body {
1323 let f = async move {
1324 body.send().await?;
1325 rx.await.map_err(|_canceled| event_loop_panicked())
1326 };
1327 wait::timeout(f, timeout)
1328 } else {
1329 let f = async move { rx.await.map_err(|_canceled| event_loop_panicked()) };
1330 wait::timeout(f, timeout)
1331 };
1332
1333 match result {
1334 Ok(Err(err)) => Err(err.with_url(url)),
1335 Ok(Ok(res)) => Ok(Response::new(
1336 res,
1337 timeout,
1338 KeepCoreThreadAlive(Some(self.inner.clone())),
1339 )),
1340 Err(wait::Waited::TimedOut(e)) => Err(crate::error::request(e).with_url(url)),
1341 Err(wait::Waited::Inner(err)) => Err(err.with_url(url)),
1342 }
1343 }
1344}
1345
1346async fn forward<F>(fut: F, mut tx: OneshotResponse)
1347where
1348 F: Future<Output = crate::Result<async_impl::Response>>,
1349{
1350 futures_util::pin_mut!(fut);
1351
1352 // "select" on the sender being canceled, and the future completing
1353 let res = std::future::poll_fn(|cx| {
1354 match fut.as_mut().poll(cx) {
1355 Poll::Ready(val) => Poll::Ready(Some(val)),
1356 Poll::Pending => {
1357 // check if the callback is canceled
1358 ready!(tx.poll_closed(cx));
1359 Poll::Ready(None)
1360 }
1361 }
1362 })
1363 .await;
1364
1365 if let Some(res) = res {
1366 let _ = tx.send(res);
1367 }
1368 // else request is canceled
1369}
1370
1371#[derive(Clone, Copy)]
1372struct Timeout(Option<Duration>);
1373
1374impl Default for Timeout {
1375 fn default() -> Timeout {
1376 // default mentioned in ClientBuilder::timeout() doc comment
1377 Timeout(Some(Duration::from_secs(30)))
1378 }
1379}
1380
1381pub(crate) struct KeepCoreThreadAlive(#[allow(dead_code)] Option<Arc<InnerClientHandle>>);
1382
1383impl KeepCoreThreadAlive {
1384 pub(crate) fn empty() -> KeepCoreThreadAlive {
1385 KeepCoreThreadAlive(None)
1386 }
1387}
1388
1389#[cold]
1390#[inline(never)]
1391fn event_loop_panicked() -> ! {
1392 // The only possible reason there would be a Canceled error
1393 // is if the thread running the event loop panicked. We could return
1394 // an Err here, like a BrokenPipe, but the Client is not
1395 // recoverable. Additionally, the panic in the other thread
1396 // is not normal, and should likely be propagated.
1397 panic!("event loop thread panicked");
1398}