verify_domain

Function verify_domain 

Source
pub fn verify_domain(input: &[u8]) -> Result<(), ParseDomainError>
Available on crate features alloc or std only.
Expand description

Verifies that the input is a valid domain name.

This function validates domain names including international domains (with IDNA/punycode) and percent-encoded domains. It requires the alloc or std feature to be enabled.

See also verify_ascii_domain and verify_ascii_domain_allow_percent_encoding.

§Valid Inputs

  • ASCII domain names: example.com
  • International domains: 测试.中国 (automatically validated after punycode conversion)
  • Punycode domains: xn--e1afmkfd.xn--80akhbyknj4f
  • Percent-encoded domains: example%2Ecom
  • FQDNs: example.com.

§Example

use hostaddr::verify_domain;

// Valid ASCII domain
let domain = b"example.com";
assert!(verify_domain(domain).is_ok());

// Valid punycode domain
let domain = b"xn--e1afmkfd.xn--80akhbyknj4f";
assert!(verify_domain(domain).is_ok());

// Valid international domain
let domain = "测试.中国";
assert!(verify_domain(domain.as_bytes()).is_ok());

// Valid percent-encoded domain
let domain = "测试%2E中国";
assert!(verify_domain(domain.as_bytes()).is_ok());

// Invalid: empty string
assert!(verify_domain(b"").is_err());

// Invalid: labels starting with hyphen
assert!(verify_domain(b"-example.com").is_err());

// Invalid: invalid characters
assert!(verify_domain(b"exam ple.com").is_err());