http_client/
lib.rs

1//! Types and traits for http clients.
2//!
3//! This crate has been extracted from `surf`'s internals, but can be used by any http client impl.
4//! The purpose of this crate is to provide a unified interface for multiple HTTP client backends,
5//! so that they can be abstracted over without doing extra work.
6
7#![forbid(future_incompatible, rust_2018_idioms)]
8#![deny(missing_debug_implementations, nonstandard_style)]
9#![warn(missing_docs, missing_doc_code_examples, unreachable_pub)]
10#![cfg_attr(feature = "docs", feature(doc_cfg))]
11// Forbid `unsafe` for the native & curl features, but allow it (for now) under the WASM backend
12#![cfg_attr(
13    not(all(feature = "wasm_client", target_arch = "wasm32")),
14    forbid(unsafe_code)
15)]
16
17mod config;
18pub use config::Config;
19
20#[cfg_attr(feature = "docs", doc(cfg(feature = "curl_client")))]
21#[cfg(all(feature = "curl_client", not(target_arch = "wasm32")))]
22pub mod isahc;
23
24#[cfg_attr(feature = "docs", doc(cfg(feature = "wasm_client")))]
25#[cfg(all(feature = "wasm_client", target_arch = "wasm32"))]
26pub mod wasm;
27
28#[cfg_attr(feature = "docs", doc(cfg(feature = "native_client")))]
29#[cfg(any(feature = "curl_client", feature = "wasm_client"))]
30pub mod native;
31
32#[cfg_attr(feature = "docs", doc(cfg(feature = "h1_client")))]
33#[cfg_attr(feature = "docs", doc(cfg(feature = "default")))]
34#[cfg(any(feature = "h1_client", feature = "h1_client_rustls"))]
35pub mod h1;
36
37#[cfg_attr(feature = "docs", doc(cfg(feature = "hyper_client")))]
38#[cfg(feature = "hyper_client")]
39pub mod hyper;
40
41/// An HTTP Request type with a streaming body.
42pub type Request = http_types::Request;
43
44/// An HTTP Response type with a streaming body.
45pub type Response = http_types::Response;
46
47pub use async_trait::async_trait;
48pub use http_types;
49
50/// An abstract HTTP client.
51///
52/// __note that this is only exposed for use in middleware. Building new backing clients is not
53/// recommended yet. Once it is we'll likely publish a new `http_client` crate, and re-export this
54/// trait from there together with all existing HTTP client implementations.__
55///
56/// ## Spawning new request from middleware
57///
58/// When threading the trait through a layer of middleware, the middleware must be able to perform
59/// new requests. In order to enable this efficiently an `HttpClient` instance may want to be passed
60/// though middleware for one of its own requests, and in order to do so should be wrapped in an
61/// `Rc`/`Arc` to enable reference cloning.
62#[async_trait]
63pub trait HttpClient: std::fmt::Debug + Unpin + Send + Sync + 'static {
64    /// Perform a request.
65    async fn send(&self, req: Request) -> Result<Response, Error>;
66
67    /// Override the existing configuration with new configuration.
68    ///
69    /// Config options may not impact existing connections.
70    fn set_config(&mut self, _config: Config) -> http_types::Result<()> {
71        unimplemented!(
72            "{} has not implemented `HttpClient::set_config()`",
73            type_name_of(self)
74        )
75    }
76
77    /// Get the current configuration.
78    fn config(&self) -> &Config {
79        unimplemented!(
80            "{} has not implemented `HttpClient::config()`",
81            type_name_of(self)
82        )
83    }
84}
85
86fn type_name_of<T: ?Sized>(_val: &T) -> &'static str {
87    std::any::type_name::<T>()
88}
89
90/// The raw body of an http request or response.
91pub type Body = http_types::Body;
92
93/// Error type.
94pub type Error = http_types::Error;
95
96#[async_trait]
97impl HttpClient for Box<dyn HttpClient> {
98    async fn send(&self, req: Request) -> http_types::Result<Response> {
99        self.as_ref().send(req).await
100    }
101
102    fn set_config(&mut self, config: Config) -> http_types::Result<()> {
103        self.as_mut().set_config(config)
104    }
105
106    fn config(&self) -> &Config {
107        self.as_ref().config()
108    }
109}