diff options
author | Yuchen Wu <[email protected]> | 2024-06-21 16:04:22 -0700 |
---|---|---|
committer | Edward Wang <[email protected]> | 2024-06-28 12:34:25 -0700 |
commit | 92bbeb11390a6e6205fb577571f4307f0627eb83 (patch) | |
tree | 873719ea0781eb60f6831854684412a07895cfd8 | |
parent | 62ddb7ebaef5aff39f4bf8c0d60906e8151b3cae (diff) | |
download | pingora-92bbeb11390a6e6205fb577571f4307f0627eb83.tar.gz pingora-92bbeb11390a6e6205fb577571f4307f0627eb83.zip |
Parse UDS with prefix
Now UDS can be parsed from string with prefix "unix:". The raw path
support will be deprecated.
From https://github.com/cloudflare/pingora/pull/141
Co-authored-by: blackanger <[email protected]>
-rw-r--r-- | .bleep | 2 | ||||
-rw-r--r-- | pingora-core/src/protocols/l4/socket.rs | 30 |
2 files changed, 24 insertions, 8 deletions
@@ -1 +1 @@ -cab68d4c24c82270ceffef312efc99a3bbc8aaa9
\ No newline at end of file +e8c45299beef05e26f59b620d998b98d31139aac
\ No newline at end of file diff --git a/pingora-core/src/protocols/l4/socket.rs b/pingora-core/src/protocols/l4/socket.rs index 186fbec..93d334e 100644 --- a/pingora-core/src/protocols/l4/socket.rs +++ b/pingora-core/src/protocols/l4/socket.rs @@ -15,6 +15,7 @@ //! Generic socket type use crate::{Error, OrErr}; +use log::warn; use nix::sys::socket::{getpeername, getsockname, SockaddrStorage}; use std::cmp::Ordering; use std::hash::{Hash, Hasher}; @@ -174,14 +175,23 @@ impl std::str::FromStr for SocketAddr { type Err = Box<Error>; // This is very basic parsing logic, it might treat invalid IP:PORT str as UDS path - // TODO: require UDS to have some prefix fn from_str(s: &str) -> Result<Self, Self::Err> { - match StdSockAddr::from_str(s) { - Ok(addr) => Ok(SocketAddr::Inet(addr)), - Err(_) => { - let uds_socket = StdUnixSockAddr::from_pathname(s) - .or_err(crate::BindError, "invalid UDS path")?; - Ok(SocketAddr::Unix(uds_socket)) + if s.starts_with("unix:") { + // format unix:/tmp/server.socket + let path = s.trim_start_matches("unix:"); + let uds_socket = StdUnixSockAddr::from_pathname(path) + .or_err(crate::BindError, "invalid UDS path")?; + Ok(SocketAddr::Unix(uds_socket)) + } else { + match StdSockAddr::from_str(s) { + Ok(addr) => Ok(SocketAddr::Inet(addr)), + Err(_) => { + // Try to parse as UDS for backward compatibility + let uds_socket = StdUnixSockAddr::from_pathname(s) + .or_err(crate::BindError, "invalid UDS path")?; + warn!("Raw Unix domain socket path support will be deprecated, add 'unix:' prefix instead"); + Ok(SocketAddr::Unix(uds_socket)) + } } } } @@ -246,4 +256,10 @@ mod test { let uds: SocketAddr = "/tmp/my.sock".parse().unwrap(); assert!(uds.as_unix().is_some()); } + + #[test] + fn parse_uds_with_prefix() { + let uds: SocketAddr = "unix:/tmp/my.sock".parse().unwrap(); + assert!(uds.as_unix().is_some()); + } } |