smtp client: remove [] from ip literal host names

This commit is contained in:
Wez Furlong
2023-08-17 09:45:10 -07:00
parent 7223bd1be1
commit 629b65533a
+26 -9
View File
@@ -202,6 +202,22 @@ pub struct SmtpClient {
timeouts: SmtpClientTimeouts,
}
fn extract_hostname(hostname: &str) -> &str {
// Just the hostname, without any :port
let fields: Vec<&str> = hostname.rsplitn(2, ':').collect();
let hostname = if fields.len() == 2 {
fields[1]
} else {
hostname
};
if hostname.starts_with('[') && hostname.ends_with(']') {
&hostname[1..hostname.len() - 1]
} else {
hostname
}
}
impl SmtpClient {
pub async fn new<A: ToSocketAddrs + ToString + Clone>(
addr: A,
@@ -216,15 +232,7 @@ impl SmtpClient {
peer_hostname: H,
timeouts: SmtpClientTimeouts,
) -> Self {
let hostname = peer_hostname.as_ref().to_string();
let fields: Vec<&str> = hostname.rsplitn(2, ':').collect();
// Just the hostname, without any :port
let hostname = if fields.len() == 2 {
fields[1].to_string()
} else {
hostname
};
let hostname = extract_hostname(peer_hostname.as_ref()).to_string();
Self {
socket: Some(Box::new(stream)),
@@ -869,4 +877,13 @@ mod test {
Err(ClientError::MalformedResponseLine(_))
));
}
#[test]
fn test_extract_hostname() {
assert_eq!(extract_hostname("foo"), "foo");
assert_eq!(extract_hostname("foo:25"), "foo");
assert_eq!(extract_hostname("[foo]:25"), "foo");
assert_eq!(extract_hostname("[::1]:25"), "::1");
assert_eq!(extract_hostname("::1:25"), "::1");
}
}