#hostname #idna #validation #punycode #publicsuffix

adbyss_psl

A minimal Public Suffix List hostname validator

50 releases

0.10.1 Mar 21, 2024
0.9.5 Feb 8, 2024
0.9.3 Dec 28, 2023
0.9.2 Nov 16, 2023
0.4.2 Jul 29, 2021

#208 in Web programming

Download history 4/week @ 2023-12-25 5/week @ 2024-01-22 6/week @ 2024-02-05 123/week @ 2024-02-12 65/week @ 2024-02-19 16/week @ 2024-02-26 1/week @ 2024-03-11 137/week @ 2024-03-18 2/week @ 2024-03-25 16/week @ 2024-04-01

156 downloads per month

WTFPL license

335KB
1.5K SLoC

Adbyss: Public Suffix

docs.rs changelog
crates.io ci deps.rs
license contributions welcome

This library contains a single public-facing struct — adbyss_psl::Domain — used for validating and normalizing Internet hostnames, like "www.domain.com".

It will:

  • Validate, normalize, and Puny-encode internationalized/Unicode labels (RFC 3492);
  • Validate and normalize the public suffix;
  • Ensure conformance with RFC 1123;
  • And locate the boundaries of the subdomain (if any), root (required), and suffix (required);

Suffix and IDNA reference data is compiled at build-time, allowing for very fast runtime parsing, but at the cost of temporality. Projects using this library will need to periodically issue new releases or risk growing stale.

Examples

New instances of Domain can be initialized using either Domain::new or TryFrom<&str>.

use adbyss_psl::Domain;

// These are equivalent and fine:
assert!(Domain::new("www.MyDomain.com").is_some());
assert!(Domain::try_from("www.MyDomain.com").is_ok());

// The following is valid DNS, but invalid as an Internet hostname:
assert!(Domain::new("_acme-challenge.mydomain.com").is_none());

Valid Internet hostnames must be no longer than 253 characters, and contain both root and (valid) suffix components.

Their labels — the bits between the dots — must additionally:

  • Be no longer than 63 characters;
  • (Ultimately) contain only ASCII letters, digits, and -;
  • Start and end with an alphanumeric character;

Unicode/internationalized labels are allowed, but must be Puny-encodable and not contain any conflicting bidirectionality constraints. Domain will encode such labels using Punycode when it finds them, ensuring the resulting hostname will always be ASCII-only.

Post-parsing, Domain gives you access to each individual component, or the whole thing:

use adbyss_psl::Domain;

let dom = Domain::new("www.MyDomain.com").unwrap();

// Pull out the pieces if you're into that sort of thing.
assert_eq!(dom.host(), "www.mydomain.com");
assert_eq!(dom.subdomain(), Some("www"));
assert_eq!(dom.root(), "mydomain");
assert_eq!(dom.suffix(), "com");
assert_eq!(dom.tld(), "mydomain.com");

// If you just want the sanitized host back as an owned value, use
// `Domain::take`:
let owned = dom.take(); // "www.mydomain.com"

Optional Crate Features

  • serde: Enables serialization/deserialization support.

Installation

Add adbyss_psl to your dependencies in Cargo.toml, like:

[dependencies]
adbyss_psl = "0.10.*"

License

Copyright © 2024 Blobfolio, LLC <hello@blobfolio.com>

This work is free. You can redistribute it and/or modify it under the terms of the Do What The Fuck You Want To Public License, Version 2.

DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004

Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>

Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.

DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

0. You just DO WHAT THE FUCK YOU WANT TO.

Dependencies

~1.5–2.5MB
~69K SLoC