40 releases (14 breaking)

0.14.0 Mar 30, 2021
0.12.1 Mar 17, 2021
0.2.0 Jul 30, 2018

#72 in Network programming

Download history 745/week @ 2021-01-13 866/week @ 2021-01-20 1242/week @ 2021-01-27 927/week @ 2021-02-03 819/week @ 2021-02-10 806/week @ 2021-02-17 846/week @ 2021-02-24 1244/week @ 2021-03-03 1330/week @ 2021-03-10 1390/week @ 2021-03-17 917/week @ 2021-03-24 1129/week @ 2021-03-31 1227/week @ 2021-04-07 1081/week @ 2021-04-14 870/week @ 2021-04-21 589/week @ 2021-04-28

4,224 downloads per month
Used in 3 crates (2 directly)

MIT/Apache

91KB
741 lines

Robust and fast domain name parsing

CI Latest Version Docs

This library uses Mozilla's Public Suffix List to reliably parse domain names in Rust. It will reliably check if a domain has valid syntax. It also checks the length restrictions for each label, total number of labels and full length of domain name.

Examples

use addr::parser::{DomainName, DnsName};
use addr::psl::List;

// You can find out the root domain
// or extension of any given domain name
let domain = List.parse_domain_name("www.example.com")?;
assert_eq!(domain.root(), Some("example.com"));
assert_eq!(domain.suffix(), "com");

let domain = List.parse_domain_name("www.食狮.中国")?;
assert_eq!(domain.root(), Some("食狮.中国"));
assert_eq!(domain.suffix(), "中国");

let domain = List.parse_domain_name("www.xn--85x722f.xn--55qx5d.cn")?;
assert_eq!(domain.root(), Some("xn--85x722f.xn--55qx5d.cn"));
assert_eq!(domain.suffix(), "xn--55qx5d.cn");

let domain = List.parse_domain_name("a.b.example.uk.com")?;
assert_eq!(domain.root(), Some("example.uk.com"));
assert_eq!(domain.suffix(), "uk.com");

let name = List.parse_dns_name("_tcp.example.com.")?;
assert_eq!(name.suffix(), Some("com."));

// In any case if the domain's suffix is in the list
// then this is definately a registrable domain name
assert!(domain.has_known_suffix());

Use Cases

For those who work with domain names the use cases of this library are plenty. publicsuffix.org/learn lists quite a few. For the sake of brevity, I'm not going to repeat them here. I work for a domain registrar so we make good use of this library. Here are some of the ways this library can be used:

  • Validating domain names. This one is probably obvious. If a domain.has_known_suffix() you can be absolutely sure this is a valid domain name. A regular expression is simply not robust enough.
  • Blacklisting or whitelisting domain names. You can't just blindly do this without knowing the actual registrable domain name otherwise you risk being too restrictive or too lenient. Bad news either way...
  • Extracting the registrable part of a domain name so you can check whether the domain is registered or not.
  • Storing details about a domain name in a DBMS using the registrable part of a domain name as the primary key.

Dependencies

~2.5MB
~90K SLoC