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