Skip to main content

parse_host_port

Function parse_host_port 

Source
pub fn parse_host_port(value: &[u8]) -> Result<HostPort<'_>, ParseError>
Expand description

Parses a Host value: uri-host [ ":" port ].

A registered name (unreserved / pct-encoded / sub-delims), a dotted-decimal IPv4 address with no leading zeros, a bracketed IPv6 address in any RFC 4291 text form, or a bracketed IPvFuture literal, optionally followed by : and a port up to 65535. An empty port is accepted with empty_port set. An empty value is accepted as an empty name (RFC 9112 section 3.2: the target has no authority); the caller decides what that means.

A name made only of digits and dots, or whose last label is a number (127.1, 0x7f.1, example.123), must be a canonical IPv4 address: RFC 3986 reads such hosts as names and the WHATWG URL standard as IPv4 addresses, and refusing them removes the disagreement.

§Errors

ErrorKind::InvalidHost for userinfo (any @, checked first), a byte outside the name class, a port with no host, or unbracketed IPv6; ErrorKind::InvalidAddress for a malformed IP literal or a numeric name that is not canonical IPv4; ErrorKind::InvalidPort; ErrorKind::InvalidPercentEncoding; a control kind for a control byte.

§Examples

use parse_net::{parse_host_port, ErrorKind};

let hp = parse_host_port(b"example.com:8443")?;
assert_eq!(hp.port, Some(8443));
assert_eq!(parse_host_port(b"a:65536").unwrap_err().kind(), ErrorKind::InvalidPort);