identifiers: Add port method to ServerName

This commit is contained in:
Jonas Platte 2022-01-17 12:05:35 +01:00
parent 4c2654c605
commit 3c194ee43b
No known key found for this signature in database
GPG Key ID: 7D261D771D915378

View File

@ -26,6 +26,22 @@ impl ServerName {
} }
} }
/// Returns the port of the server name, if any.
pub fn port(&self) -> Option<u16> {
#[allow(clippy::unnecessary_lazy_evaluations)]
let end_of_host = self
.0
.find(']')
.map(|i| i + 1)
.or_else(|| self.0.find(':'))
.unwrap_or_else(|| self.0.len());
(self.0.len() != end_of_host).then(|| {
assert!(self.as_bytes()[end_of_host] == b':');
self.0[end_of_host + 1..].parse().unwrap()
})
}
/// Returns true if and only if the server name is an IPv4 or IPv6 address. /// Returns true if and only if the server name is an IPv4 or IPv6 address.
pub fn is_ip_literal(&self) -> bool { pub fn is_ip_literal(&self) -> bool {
self.host().parse::<Ipv4Addr>().is_ok() || self.0.starts_with('[') self.host().parse::<Ipv4Addr>().is_ok() || self.0.starts_with('[')