is_valid_hostname
Checks that hostname is a valid hostname (RFC 1035 and RFC 1123, ASCII only).
The rules: at most 253 octets, not counting one optional trailing dot; labels of 1 to 63
octets made of ASCII letters, digits and hyphens; no label starts or ends with a hyphen. A
label may start with a digit, but the last label may not be a number (decimal like 1, or
hexadecimal like 0x7f): RFC 1123 section 2.1 keeps the top-level label alphabetic so that a
hostname is never mistaken for an address, and URL parsers do read 127.1, 2130706433 or
0x7f000001 as 127.0.0.1. Underscores are refused (they are not hostname characters), and so
are non-ASCII characters: convert an internationalized name to its xn-- form first.
This checks syntax only. It is not an SSRF check: a name that passes can still resolve to a
private address. To guard a server that fetches user-supplied URLs, parse the URL first, then
check the address you will actually connect to with
is_public_ip.
Import
Section titled “Import”Cargo feature net (enabled by default). To compile only this module:
or in Cargo.toml:
Signature
Section titled “Signature”Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
hostname | &str | The hostname to validate. |
Returns
Section titled “Returns”Result<(), HostnameError> — Ok on success, otherwise an Err: see Errors.
Errors
Section titled “Errors”A HostnameError naming the first rule that fails.
Examples
Section titled “Examples”Error type: HostnameError
Section titled “Error type: HostnameError”Why a string is not a valid hostname (see is_valid_hostname).
