#![forbid(unsafe_code)]
#![doc = include_str!("../README.md")]
use std::net::IpAddr;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SocketEndpoint {
pub host: String,
pub port: u16,
}
fn parse_port(input: &str) -> Option<u16> {
if input.is_empty() {
None
} else {
input.parse::<u16>().ok()
}
}
fn is_valid_label(label: &str) -> bool {
!label.is_empty()
&& label.len() <= 63
&& !label.starts_with('-')
&& !label.ends_with('-')
&& label
.chars()
.all(|character| character.is_ascii_alphanumeric() || character == '-')
}
fn normalize_host(input: &str) -> Option<String> {
if input.eq_ignore_ascii_case("localhost") {
return Some(String::from("localhost"));
}
if let Ok(address) = input.parse::<IpAddr>() {
return Some(address.to_string());
}
let normalized = input.trim_end_matches('.').to_ascii_lowercase();
if normalized.is_empty()
|| normalized.len() > 253
|| normalized.contains(':')
|| normalized.contains('/')
|| normalized.chars().any(char::is_whitespace)
{
return None;
}
if normalized.split('.').all(is_valid_label) {
Some(normalized)
} else {
None
}
}
fn split_raw_endpoint(input: &str) -> Option<(String, u16)> {
let trimmed = input.trim();
if trimmed.is_empty() {
return None;
}
if trimmed.starts_with('[') {
let closing_index = trimmed.find(']')?;
let host = &trimmed[1..closing_index];
let port = trimmed.get(closing_index + 2..)?;
if !trimmed[closing_index + 1..].starts_with(':') {
return None;
}
match host.parse::<IpAddr>() {
Ok(IpAddr::V6(address)) => Some((address.to_string(), parse_port(port)?)),
_ => None,
}
} else {
let (host, port) = trimmed.rsplit_once(':')?;
if host.is_empty() || host.contains(':') {
return None;
}
Some((normalize_host(host)?, parse_port(port)?))
}
}
pub fn parse_socket_endpoint(input: &str) -> Option<SocketEndpoint> {
let (host, port) = split_raw_endpoint(input)?;
Some(SocketEndpoint { host, port })
}
pub fn format_socket_endpoint(endpoint: &SocketEndpoint) -> String {
match endpoint.host.parse::<IpAddr>() {
Ok(IpAddr::V6(address)) => format!("[{address}]:{}", endpoint.port),
_ => format!("{}:{}", endpoint.host, endpoint.port),
}
}
pub fn is_ipv4_socket_endpoint(input: &str) -> bool {
parse_socket_endpoint(input)
.is_some_and(|endpoint| matches!(endpoint.host.parse::<IpAddr>(), Ok(IpAddr::V4(_))))
}
pub fn is_ipv6_socket_endpoint(input: &str) -> bool {
parse_socket_endpoint(input)
.is_some_and(|endpoint| matches!(endpoint.host.parse::<IpAddr>(), Ok(IpAddr::V6(_))))
}
pub fn is_host_port(input: &str) -> bool {
parse_socket_endpoint(input).is_some()
}
pub fn split_host_port(input: &str) -> Option<(String, u16)> {
split_raw_endpoint(input)
}