axum-listener 0.2.2

A simple listener for axum with UDS and TCP support
Documentation
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
use axum::serve::Listener;
use std::net::ToSocketAddrs;
#[cfg(unix)]
use tokio::net::UnixListener;

/// A unified listener that can bind to either TCP or Unix Domain Socket addresses.
///
/// This enum allows you to create a single listener type that can handle both TCP and UDS
/// connections transparently. The specific variant is determined at runtime based on the
/// address format provided to [`DualListener::bind`].
///
/// # Examples
///
/// ```rust,no_run
/// # tokio_test::block_on(async {
/// use axum_listener::listener::DualListener;
///
/// // Bind to TCP
/// let tcp_listener = DualListener::bind("localhost:8080").await.unwrap();
///
/// // Bind to Unix Domain Socket (on Unix systems)
/// # #[cfg(unix)] {
/// let uds_listener = DualListener::bind("unix:/tmp/app.sock").await.unwrap();
/// # }
/// # });
/// ```
///
/// # Platform Support
///
/// - `Tcp` variant is available on all platforms
/// - `Uds` variant is only available on Unix-like systems
#[derive(Debug)]
pub enum DualListener {
    /// A TCP listener for network connections
    Tcp(tokio::net::TcpListener),
    /// A Unix Domain Socket listener for local inter-process communication
    #[cfg(unix)]
    Uds(tokio::net::UnixListener),
}

/// An address that can represent either a TCP socket address or a Unix Domain Socket address.
///
/// This enum is used to represent the local and remote addresses for connections
/// accepted by [`DualListener`]. It automatically implements cleanup for UDS
/// socket files when the `remove-on-drop` feature is enabled.
///
/// # Examples
///
/// ```rust
/// use axum_listener::listener::DualAddr;
/// use std::str::FromStr;
///
/// // Parse a TCP address
/// let tcp_addr = DualAddr::from_str("127.0.0.1:8080").unwrap();
///
/// // Parse a UDS address (on Unix systems)
/// # #[cfg(unix)] {
/// let uds_addr = DualAddr::from_str("unix:/tmp/app.sock").unwrap();
/// # }
/// ```
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub enum DualAddr {
    /// A TCP socket address (IPv4 or IPv6)
    Tcp(core::net::SocketAddr),
    /// A Unix Domain Socket address
    #[cfg(unix)]
    Uds(tokio::net::unix::SocketAddr),
}

impl From<core::net::SocketAddr> for DualAddr {
    fn from(addr: core::net::SocketAddr) -> Self {
        DualAddr::Tcp(addr)
    }
}

#[cfg(unix)]
impl From<tokio::net::unix::SocketAddr> for DualAddr {
    fn from(addr: tokio::net::unix::SocketAddr) -> Self {
        DualAddr::Uds(addr)
    }
}

impl core::str::FromStr for DualAddr {
    type Err = std::io::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let unix_like = s.starts_with("/") || s.starts_with("unix:");
        let has_uds = cfg!(unix);
        let tcp_like = s.to_socket_addrs().is_ok();

        if unix_like && has_uds && !tcp_like {
            #[cfg(unix)]
            {
                let path = s.trim_start_matches("unix:");
                let addr = From::from(std::os::unix::net::SocketAddr::from_pathname(path)?);
                Ok(DualAddr::Uds(addr))
            }
            #[cfg(not(unix))]
            {
                Err(std::io::Error::new(
                    std::io::ErrorKind::Other,
                    "Unix domain sockets are not supported on this platform",
                ))
            }
        } else if tcp_like {
            let addr = s.to_socket_addrs()?.next().ok_or_else(|| {
                std::io::Error::new(std::io::ErrorKind::InvalidInput, "Invalid TCP address")
            })?;
            Ok(DualAddr::Tcp(addr))
        } else if unix_like && !has_uds {
            Err(std::io::Error::other(
                "Unix domain sockets are not supported on this platform",
            ))
        } else {
            Err(std::io::Error::new(
                std::io::ErrorKind::InvalidInput,
                "Invalid address format",
            ))
        }
    }
}

/// A trait for types that can be converted to a [`DualAddr`].
///
/// This trait enables convenient address binding by allowing various types
/// to be converted to the unified [`DualAddr`] type. It's implemented for
/// common address types including strings, socket addresses, and paths.
///
/// # Examples
///
/// ```rust
/// use axum_listener::listener::{ToDualAddr, DualAddr};
/// use std::net::SocketAddr;
///
/// // String addresses
/// let addr1 = "127.0.0.1:8080".to_dual_addr().unwrap();
/// let addr2 = "unix:/tmp/app.sock".to_dual_addr().unwrap();
///
/// // Socket address
/// let socket_addr: SocketAddr = "127.0.0.1:8080".parse().unwrap();
/// let addr3 = socket_addr.to_dual_addr().unwrap();
/// ```
pub trait ToDualAddr {
    /// Convert this type to a [`DualAddr`].
    ///
    /// # Errors
    ///
    /// Returns an [`std::io::Error`] if the address format is invalid or
    /// if Unix Domain Sockets are not supported on the current platform.
    fn to_dual_addr(&self) -> Result<DualAddr, std::io::Error>;
}

impl ToDualAddr for &str {
    fn to_dual_addr(&self) -> Result<DualAddr, std::io::Error> {
        self.parse()
    }
}

impl ToDualAddr for String {
    fn to_dual_addr(&self) -> Result<DualAddr, std::io::Error> {
        self.as_str().to_dual_addr()
    }
}

impl ToDualAddr for core::net::SocketAddr {
    fn to_dual_addr(&self) -> Result<DualAddr, std::io::Error> {
        Ok(DualAddr::Tcp(*self))
    }
}

#[cfg(unix)]
impl ToDualAddr for tokio::net::unix::SocketAddr {
    fn to_dual_addr(&self) -> Result<DualAddr, std::io::Error> {
        Ok(DualAddr::Uds(self.clone()))
    }
}

impl ToDualAddr for DualAddr {
    fn to_dual_addr(&self) -> Result<DualAddr, std::io::Error> {
        Ok(self.clone())
    }
}

impl ToDualAddr for &DualAddr {
    fn to_dual_addr(&self) -> Result<DualAddr, std::io::Error> {
        Ok((*self).clone())
    }
}

#[cfg(unix)]
impl ToDualAddr for &std::path::Path {
    fn to_dual_addr(&self) -> Result<DualAddr, std::io::Error> {
        Ok(DualAddr::Uds(From::from(
            std::os::unix::net::SocketAddr::from_pathname(self)?,
        )))
    }
}

#[cfg(unix)]
impl ToDualAddr for std::path::PathBuf {
    fn to_dual_addr(&self) -> Result<DualAddr, std::io::Error> {
        self.as_path().to_dual_addr()
    }
}

impl DualListener {
    /// Creates a new [`DualListener`] bound to the specified address.
    ///
    /// This method accepts any type that implements [`ToDualAddr`], allowing
    /// for flexible address specification. The listener type (TCP or UDS) is
    /// automatically determined based on the address format.
    ///
    /// # Arguments
    ///
    /// * `address` - An address that can be converted to [`DualAddr`]
    ///
    /// # Returns
    ///
    /// Returns a [`DualListener`] bound to the specified address, or an error
    /// if binding fails.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # tokio_test::block_on(async {
    /// use axum_listener::listener::DualListener;
    ///
    /// // Bind to TCP address
    /// let listener = DualListener::bind("localhost:8080").await.unwrap();
    ///
    /// // Bind to UDS address (Unix only)
    /// # #[cfg(unix)] {
    /// let listener = DualListener::bind("unix:/tmp/app.sock").await.unwrap();
    /// # }
    /// # });
    /// ```
    ///
    /// # Errors
    ///
    /// This method can fail if:
    /// - The address format is invalid
    /// - The address is already in use
    /// - Permission is denied for the requested address
    /// - Unix Domain Sockets are not supported on the current platform
    pub async fn bind<A: ToDualAddr>(address: A) -> Result<Self, std::io::Error> {
        let address = address.to_dual_addr()?;
        match address {
            DualAddr::Tcp(addr) => {
                let listener = tokio::net::TcpListener::bind(addr).await?;
                Ok(DualListener::Tcp(listener))
            }
            #[cfg(unix)]
            DualAddr::Uds(ref addr) => {
                let path = addr.as_pathname().ok_or_else(|| {
                    std::io::Error::new(
                        std::io::ErrorKind::InvalidInput,
                        "UDS address does not have a valid pathname",
                    )
                })?;
                let listener = UnixListener::bind(path)?;
                Ok(DualListener::Uds(listener))
            }
        }
    }

    /// Accepts a new incoming connection from this listener.
    ///
    /// This method will wait for a connection to be established and return
    /// a stream and address representing the connection.
    ///
    /// # Returns
    ///
    /// Returns a tuple containing:
    /// - [`DualStream`]: The stream for communicating with the client
    /// - [`DualAddr`]: The address of the connected client
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # tokio_test::block_on(async {
    /// use axum_listener::listener::DualListener;
    ///
    /// let listener = DualListener::bind("localhost:8080").await.unwrap();
    ///
    /// // Accept a connection
    /// let (stream, addr) = listener.accept().await.unwrap();
    /// println!("Accepted connection from: {:?}", addr);
    /// # });
    /// ```
    ///
    /// # Errors
    ///
    /// This method can fail if there's an I/O error while accepting the connection.
    pub async fn accept(&self) -> Result<(DualStream, DualAddr), std::io::Error> {
        match self {
            DualListener::Tcp(listener) => {
                let (stream, addr) = listener.accept().await?;
                Ok((DualStream::Tcp(stream), DualAddr::Tcp(addr)))
            }
            #[cfg(unix)]
            DualListener::Uds(listener) => {
                let (stream, addr) = listener.accept().await?;
                Ok((DualStream::Uds(stream), DualAddr::Uds(addr)))
            }
        }
    }

    pub(crate) fn _accept_unpin(
        &self,
    ) -> impl core::future::Future<Output = Result<(DualStream, DualAddr), std::io::Error>>
    + Unpin
    + use<'_> {
        Box::pin(async move {
            match self {
                DualListener::Tcp(listener) => {
                    let (stream, addr) = listener.accept().await?;
                    Ok((DualStream::Tcp(stream), DualAddr::Tcp(addr)))
                }
                #[cfg(unix)]
                DualListener::Uds(listener) => {
                    let (stream, addr) = listener.accept().await?;
                    Ok((DualStream::Uds(stream), DualAddr::Uds(addr)))
                }
            }
        })
    }
    pub(crate) async fn _accept_axum(&mut self) -> (DualStream, DualAddr) {
        match self {
            DualListener::Tcp(listener) => {
                let (stream, addr) = Listener::accept(listener).await;
                (DualStream::Tcp(stream), DualAddr::Tcp(addr))
            }
            #[cfg(unix)]
            DualListener::Uds(listener) => {
                let (stream, addr) = Listener::accept(listener).await;
                (DualStream::Uds(stream), DualAddr::Uds(addr))
            }
        }
    }

    pub(crate) fn _accept_axum_unpin(
        &mut self,
    ) -> impl core::future::Future<Output = (DualStream, DualAddr)> + Unpin + use<'_> {
        Box::pin(async move {
            match self {
                DualListener::Tcp(listener) => {
                    let (stream, addr) = Listener::accept(listener).await;
                    (DualStream::Tcp(stream), DualAddr::Tcp(addr))
                }
                #[cfg(unix)]
                DualListener::Uds(listener) => {
                    let (stream, addr) = Listener::accept(listener).await;
                    (DualStream::Uds(stream), DualAddr::Uds(addr))
                }
            }
        })
    }
}

/// A stream that can be either a TCP stream or a Unix Domain Socket stream.
///
/// This enum provides a unified interface for both TCP and UDS connections,
/// implementing the necessary async I/O traits to work seamlessly with Axum
/// and other async frameworks.
///
/// # Examples
///
/// ```rust,no_run
/// # tokio_test::block_on(async {
/// use axum_listener::listener::DualListener;
///
/// let listener = DualListener::bind("localhost:8080").await.unwrap();
/// let (stream, _addr) = listener.accept().await.unwrap();
///
/// // The stream can be used with Axum or any other async framework
/// // that works with tokio's AsyncRead + AsyncWrite traits
/// println!("Accepted connection from: {:?}", _addr);
/// # });
/// ```
pub enum DualStream {
    /// A TCP stream for network connections
    Tcp(tokio::net::TcpStream),
    /// A Unix Domain Socket stream for local inter-process communication
    #[cfg(unix)]
    Uds(tokio::net::UnixStream),
}

impl From<tokio::net::TcpStream> for DualStream {
    fn from(stream: tokio::net::TcpStream) -> Self {
        DualStream::Tcp(stream)
    }
}

#[cfg(unix)]
impl From<tokio::net::UnixStream> for DualStream {
    fn from(stream: tokio::net::UnixStream) -> Self {
        DualStream::Uds(stream)
    }
}

impl tokio::io::AsyncRead for DualStream {
    fn poll_read(
        self: std::pin::Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
        buf: &mut tokio::io::ReadBuf<'_>,
    ) -> std::task::Poll<std::io::Result<()>> {
        match self.get_mut() {
            DualStream::Tcp(stream) => std::pin::Pin::new(stream).poll_read(cx, buf),
            #[cfg(unix)]
            DualStream::Uds(stream) => std::pin::Pin::new(stream).poll_read(cx, buf),
        }
    }
}

impl tokio::io::AsyncWrite for DualStream {
    fn poll_write(
        self: std::pin::Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
        buf: &[u8],
    ) -> std::task::Poll<std::io::Result<usize>> {
        match self.get_mut() {
            DualStream::Tcp(stream) => std::pin::Pin::new(stream).poll_write(cx, buf),
            #[cfg(unix)]
            DualStream::Uds(stream) => std::pin::Pin::new(stream).poll_write(cx, buf),
        }
    }

    fn poll_flush(
        self: std::pin::Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<std::io::Result<()>> {
        match self.get_mut() {
            DualStream::Tcp(stream) => std::pin::Pin::new(stream).poll_flush(cx),
            #[cfg(unix)]
            DualStream::Uds(stream) => std::pin::Pin::new(stream).poll_flush(cx),
        }
    }

    fn poll_shutdown(
        self: std::pin::Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<std::io::Result<()>> {
        match self.get_mut() {
            DualStream::Tcp(stream) => std::pin::Pin::new(stream).poll_shutdown(cx),
            #[cfg(unix)]
            DualStream::Uds(stream) => std::pin::Pin::new(stream).poll_shutdown(cx),
        }
    }
}

impl axum::serve::Listener for DualListener {
    type Io = DualStream;
    type Addr = DualAddr;
    async fn accept(&mut self) -> (Self::Io, Self::Addr) {
        self._accept_axum().await
    }

    fn local_addr(&self) -> Result<Self::Addr, std::io::Error> {
        match self {
            DualListener::Tcp(listener) => Listener::local_addr(listener).map(DualAddr::Tcp),
            #[cfg(unix)]
            DualListener::Uds(listener) => Listener::local_addr(listener).map(DualAddr::Uds),
        }
    }
}

const _: () = {
    use super::DualAddr;
    use axum::extract::connect_info::Connected;
    impl Connected<DualAddr> for DualAddr {
        fn connect_info(remote_addr: DualAddr) -> Self {
            remote_addr
        }
    }
    use axum::serve;

    impl Connected<serve::IncomingStream<'_, DualListener>> for DualAddr {
        fn connect_info(stream: serve::IncomingStream<'_, DualListener>) -> Self {
            stream.remote_addr().clone()
        }
    }
};

#[cfg(test)]
mod tests {
    use super::*;
    #[tokio::test]
    async fn test_tcp_bind() {
        let listener = DualListener::bind("localhost:8080").await;
        assert!(listener.is_ok());
        if let DualListener::Tcp(tcp_listener) = listener.unwrap() {
            let addr = tcp_listener.local_addr().unwrap();
            assert_eq!(addr.port(), 8080);
        } else {
            panic!("Expected TCP listener");
        }
    }

    #[tokio::test]
    async fn test_uds_bind() {
        #[cfg(unix)]
        {
            let listener = DualListener::bind("/tmp/test.sock").await;
            assert!(listener.is_ok());
            if let DualListener::Uds(uds_listener) = listener.unwrap() {
                let addr = uds_listener.local_addr().unwrap();
                assert_eq!(
                    addr.as_pathname().unwrap(),
                    std::path::Path::new("/tmp/test.sock")
                );
            } else {
                panic!("Expected UDS listener");
            }
        }
    }
}