alloy_transport_http/
lib.rs

1#![doc = include_str!("../README.md")]
2#![doc(
3    html_logo_url = "https://raw.githubusercontent.com/alloy-rs/core/main/assets/alloy.jpg",
4    html_favicon_url = "https://raw.githubusercontent.com/alloy-rs/core/main/assets/favicon.ico"
5)]
6#![cfg_attr(not(test), warn(unused_crate_dependencies))]
7#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
8
9#[cfg(feature = "reqwest")]
10pub use reqwest;
11#[cfg(feature = "reqwest")]
12mod reqwest_transport;
13
14#[cfg(feature = "reqwest")]
15#[doc(inline)]
16pub use reqwest_transport::*;
17
18#[cfg(all(not(target_family = "wasm"), feature = "hyper"))]
19pub use hyper;
20#[cfg(all(not(target_family = "wasm"), feature = "hyper"))]
21pub use hyper_util;
22
23#[cfg(all(not(target_family = "wasm"), feature = "hyper", feature = "jwt-auth"))]
24mod layers;
25#[cfg(all(not(target_family = "wasm"), feature = "hyper", feature = "jwt-auth"))]
26pub use layers::{AuthLayer, AuthService};
27
28#[cfg(all(not(target_family = "wasm"), feature = "hyper"))]
29mod hyper_transport;
30#[cfg(all(not(target_family = "wasm"), feature = "hyper"))]
31#[doc(inline)]
32pub use hyper_transport::{HyperClient, HyperResponse, HyperResponseFut, HyperTransport};
33
34use alloy_transport::utils::guess_local_url;
35use core::str::FromStr;
36use std::marker::PhantomData;
37use url::Url;
38
39/// Connection details for an HTTP transport.
40#[derive(Clone, Debug, PartialEq, Eq, Hash)]
41#[doc(hidden)]
42pub struct HttpConnect<T> {
43    /// The URL to connect to.
44    url: Url,
45
46    _pd: PhantomData<T>,
47}
48
49impl<T> HttpConnect<T> {
50    /// Create a new [`HttpConnect`] with the given URL.
51    pub const fn new(url: Url) -> Self {
52        Self { url, _pd: PhantomData }
53    }
54
55    /// Get a reference to the URL.
56    pub const fn url(&self) -> &Url {
57        &self.url
58    }
59}
60
61impl<T> FromStr for HttpConnect<T> {
62    type Err = url::ParseError;
63
64    fn from_str(s: &str) -> Result<Self, Self::Err> {
65        Ok(Self::new(s.parse()?))
66    }
67}
68
69/// An Http transport.
70///
71/// The user must provide an internal http client and a URL to which to
72/// connect. It implements `Service<Box<RawValue>>`, and therefore
73/// [`Transport`].
74///
75/// [`Transport`]: alloy_transport::Transport
76///
77/// Currently supported clients are:
78#[cfg_attr(feature = "reqwest", doc = " - [`reqwest`](::reqwest::Client)")]
79#[cfg_attr(feature = "hyper", doc = " - [`hyper`](hyper_util::client::legacy::Client)")]
80#[derive(Clone, Debug)]
81pub struct Http<T> {
82    client: T,
83    url: Url,
84}
85
86impl<T> Http<T> {
87    /// Create a new [`Http`] transport with a custom client.
88    pub const fn with_client(client: T, url: Url) -> Self {
89        Self { client, url }
90    }
91
92    /// Set the URL.
93    pub fn set_url(&mut self, url: Url) {
94        self.url = url;
95    }
96
97    /// Set the client.
98    pub fn set_client(&mut self, client: T) {
99        self.client = client;
100    }
101
102    /// Guess whether the URL is local, based on the hostname.
103    ///
104    /// The output of this function is best-efforts, and should be checked if
105    /// possible. It simply returns `true` if the connection has no hostname,
106    /// or the hostname is `localhost` or `127.0.0.1`.
107    pub fn guess_local(&self) -> bool {
108        guess_local_url(&self.url)
109    }
110
111    /// Get a reference to the client.
112    pub const fn client(&self) -> &T {
113        &self.client
114    }
115
116    /// Get a reference to the URL.
117    pub fn url(&self) -> &str {
118        self.url.as_ref()
119    }
120}