rocket/listener/
endpoint.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
use std::fmt;
use std::any::Any;
use std::net::{self, AddrParseError, IpAddr, Ipv4Addr};
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::sync::Arc;

use figment::Figment;
use serde::de;

use crate::http::uncased::AsUncased;

#[cfg(feature = "tls")]      type TlsInfo = Option<Box<crate::tls::TlsConfig>>;
#[cfg(not(feature = "tls"))] type TlsInfo = Option<()>;

pub trait CustomEndpoint: fmt::Display + fmt::Debug + Sync + Send + Any { }

impl<T: fmt::Display + fmt::Debug + Sync + Send + Any> CustomEndpoint for T {}

/// # Conversions
///
/// * [`&str`] - parse with [`FromStr`]
/// * [`tokio::net::unix::SocketAddr`] - must be path: [`Endpoint::Unix`]
/// * [`PathBuf`] - infallibly as [`Endpoint::Unix`]
///
/// # Syntax
///
/// The string syntax is:
///
/// ```text
/// endpoint = 'tcp' ':' socket | 'quic' ':' socket | 'unix' ':' path | socket
/// socket := IP_ADDR | SOCKET_ADDR
/// path := PATH
///
/// IP_ADDR := `std::net::IpAddr` string as defined by Rust
/// SOCKET_ADDR := `std::net::SocketAddr` string as defined by Rust
/// PATH := `PathBuf` (any UTF-8) string as defined by Rust
/// ```
///
/// If `IP_ADDR` is specified in socket, port defaults to `8000`.
#[derive(Clone)]
#[non_exhaustive]
pub enum Endpoint {
    Tcp(net::SocketAddr),
    Quic(net::SocketAddr),
    Unix(PathBuf),
    Tls(Arc<Endpoint>, TlsInfo),
    Custom(Arc<dyn CustomEndpoint>),
}

impl Endpoint {
    pub fn new<T: CustomEndpoint>(value: T) -> Endpoint {
        Endpoint::Custom(Arc::new(value))
    }

    pub fn tcp(&self) -> Option<net::SocketAddr> {
        match self {
            Endpoint::Tcp(addr) => Some(*addr),
            Endpoint::Tls(addr, _) => addr.tcp(),
            _ => None,
        }
    }

    pub fn quic(&self) -> Option<net::SocketAddr> {
        match self {
            Endpoint::Quic(addr) => Some(*addr),
            Endpoint::Tls(addr, _) => addr.tcp(),
            _ => None,
        }
    }

    pub fn socket_addr(&self) -> Option<net::SocketAddr> {
        match self {
            Endpoint::Quic(addr) => Some(*addr),
            Endpoint::Tcp(addr) => Some(*addr),
            Endpoint::Tls(inner, _) => inner.socket_addr(),
            _ => None,
        }
    }

    pub fn ip(&self) -> Option<IpAddr> {
        match self {
            Endpoint::Quic(addr) => Some(addr.ip()),
            Endpoint::Tcp(addr) => Some(addr.ip()),
            Endpoint::Tls(inner, _) => inner.ip(),
            _ => None,
        }
    }

    pub fn port(&self) -> Option<u16> {
        match self {
            Endpoint::Quic(addr) => Some(addr.port()),
            Endpoint::Tcp(addr) => Some(addr.port()),
            Endpoint::Tls(inner, _) => inner.port(),
            _ => None,
        }
    }

    pub fn unix(&self) -> Option<&Path> {
        match self {
            Endpoint::Unix(addr) => Some(addr),
            Endpoint::Tls(addr, _) => addr.unix(),
            _ => None,
        }
    }

    pub fn tls(&self) -> Option<&Endpoint> {
        match self {
            Endpoint::Tls(addr, _) => Some(addr),
            _ => None,
        }
    }

    #[cfg(feature = "tls")]
    pub fn tls_config(&self) -> Option<&crate::tls::TlsConfig> {
        match self {
            Endpoint::Tls(_, Some(ref config)) => Some(config),
            _ => None,
        }
    }

    #[cfg(feature = "mtls")]
    pub fn mtls_config(&self) -> Option<&crate::mtls::MtlsConfig> {
        match self {
            Endpoint::Tls(_, Some(config)) => config.mutual(),
            _ => None,
        }
    }

    pub fn downcast<T: 'static>(&self) -> Option<&T> {
        match self {
            Endpoint::Tcp(addr) => (addr as &dyn Any).downcast_ref(),
            Endpoint::Quic(addr) => (addr as &dyn Any).downcast_ref(),
            Endpoint::Unix(addr) => (addr as &dyn Any).downcast_ref(),
            Endpoint::Custom(addr) => (addr as &dyn Any).downcast_ref(),
            Endpoint::Tls(inner, ..) => inner.downcast(),
        }
    }

    pub fn is_tcp(&self) -> bool {
        self.tcp().is_some()
    }

    pub fn is_quic(&self) -> bool {
        self.quic().is_some()
    }

    pub fn is_unix(&self) -> bool {
        self.unix().is_some()
    }

    pub fn is_tls(&self) -> bool {
        self.tls().is_some()
    }

    #[cfg(feature = "tls")]
    pub fn with_tls(self, tls: &crate::tls::TlsConfig) -> Endpoint {
        if self.is_tls() {
            return self;
        }

        Self::Tls(Arc::new(self), Some(Box::new(tls.clone())))
    }

    pub fn assume_tls(self) -> Endpoint {
        if self.is_tls() {
            return self;
        }

        Self::Tls(Arc::new(self), None)
    }

    /// Fetch the endpoint at `path` in `figment` of kind `kind` (e.g, "tcp")
    /// then map the value using `f(Some(value))` if present and `f(None)` if
    /// missing into a different value of typr `T`.
    ///
    /// If the conversion succeeds, returns `Ok(value)`. If the conversion fails
    /// and `Some` value was passed in, returns an error indicating the endpoint
    /// was an invalid `kind` and otherwise returns a "missing field" error.
    pub(crate) fn fetch<T, F>(figment: &Figment, kind: &str, path: &str, f: F) -> figment::Result<T>
        where F: FnOnce(Option<&Endpoint>) -> Option<T>
    {
        match figment.extract_inner::<Endpoint>(path) {
            Ok(endpoint) => f(Some(&endpoint)).ok_or_else(|| {
                let msg = format!("invalid {kind} endpoint: {endpoint:?}");
                let mut error = figment::Error::from(msg).with_path(path);
                error.profile = Some(figment.profile().clone());
                error.metadata = figment.find_metadata(path).cloned();
                error
            }),
            Err(e) if e.missing() => f(None).ok_or(e),
            Err(e) => Err(e)
        }
    }
}

impl fmt::Display for Endpoint {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        use Endpoint::*;

        match self {
            Tcp(addr) | Quic(addr) => write!(f, "http://{addr}"),
            Unix(addr) => write!(f, "unix:{}", addr.display()),
            Custom(inner) => inner.fmt(f),
            Tls(inner, _c) => {
                match (inner.tcp(), inner.quic()) {
                    (Some(addr), _) => write!(f, "https://{addr} (TCP")?,
                    (_, Some(addr)) => write!(f, "https://{addr} (QUIC")?,
                    (None, None) => write!(f, "{inner} (TLS")?,
                }

                #[cfg(feature = "mtls")]
                if _c.as_ref().and_then(|c| c.mutual()).is_some() {
                    write!(f, " + mTLS")?;
                }

                write!(f, ")")
            }
        }
    }
}

impl fmt::Debug for Endpoint {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Tcp(a) => write!(f, "tcp:{a}"),
            Self::Quic(a) => write!(f, "quic:{a}]"),
            Self::Unix(a) => write!(f, "unix:{}", a.display()),
            Self::Tls(e, _) => write!(f, "unix:{:?}", &**e),
            Self::Custom(e) => e.fmt(f),
        }
    }
}

impl Default for Endpoint {
    fn default() -> Self {
        Endpoint::Tcp(net::SocketAddr::new(Ipv4Addr::LOCALHOST.into(), 8000))
    }
}

impl FromStr for Endpoint {
    type Err = AddrParseError;

    fn from_str(string: &str) -> Result<Self, Self::Err> {
        fn parse_tcp(str: &str, def_port: u16) -> Result<net::SocketAddr, AddrParseError> {
            str.parse().or_else(|_| str.parse().map(|ip| net::SocketAddr::new(ip, def_port)))
        }

        if let Some((proto, string)) = string.split_once(':') {
            if proto.trim().as_uncased() == "tcp" {
                return parse_tcp(string.trim(), 8000).map(Self::Tcp);
            } else if proto.trim().as_uncased() == "unix" {
                return Ok(Self::Unix(PathBuf::from(string.trim())));
            }
        }

        parse_tcp(string.trim(), 8000).map(Self::Tcp)
    }
}

impl<'de> de::Deserialize<'de> for Endpoint {
    fn deserialize<D: de::Deserializer<'de>>(de: D) -> Result<Self, D::Error> {
        struct Visitor;

        impl<'de> de::Visitor<'de> for Visitor {
            type Value = Endpoint;

            fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
                formatter.write_str("valid TCP (ip) or unix (path) endpoint")
            }

            fn visit_str<E: de::Error>(self, v: &str) -> Result<Self::Value, E> {
                v.parse::<Endpoint>().map_err(|e| E::custom(e.to_string()))
            }
        }

        de.deserialize_any(Visitor)
    }
}

impl Eq for Endpoint { }

impl PartialEq for Endpoint {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::Tcp(l0), Self::Tcp(r0)) => l0 == r0,
            (Self::Quic(l0), Self::Quic(r0)) => l0 == r0,
            (Self::Unix(l0), Self::Unix(r0)) => l0 == r0,
            (Self::Tls(l0, _), Self::Tls(r0, _)) => l0 == r0,
            (Self::Custom(l0), Self::Custom(r0)) => l0.to_string() == r0.to_string(),
            _ => false,
        }
    }
}

impl PartialEq<PathBuf> for Endpoint {
    fn eq(&self, other: &PathBuf) -> bool {
        self.unix() == Some(other.as_path())
    }
}

impl PartialEq<Path> for Endpoint {
    fn eq(&self, other: &Path) -> bool {
        self.unix() == Some(other)
    }
}

#[cfg(unix)]
impl TryFrom<tokio::net::unix::SocketAddr> for Endpoint {
    type Error = std::io::Error;

    fn try_from(v: tokio::net::unix::SocketAddr) -> Result<Self, Self::Error> {
        v.as_pathname()
            .ok_or_else(|| std::io::Error::other("unix socket is not path"))
            .map(|path| Endpoint::Unix(path.to_path_buf()))
    }
}

impl TryFrom<&str> for Endpoint {
    type Error = AddrParseError;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        value.parse()
    }
}

macro_rules! impl_from {
    ($T:ty => $V:ident) => {
        impl From<$T> for Endpoint {
            fn from(value: $T) -> Self {
                Self::$V(value.into())
            }
        }
    }
}

impl_from!(std::net::SocketAddr => Tcp);
impl_from!(std::net::SocketAddrV4 => Tcp);
impl_from!(std::net::SocketAddrV6 => Tcp);
impl_from!(PathBuf => Unix);