• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

benches/H03-May-2022-1815

src/H03-May-2022-1,4551,117

.cargo-checksum.jsonH A D03-May-202289 11

.cargo_vcs_info.jsonH A D01-Jan-197074 65

.gitignoreH A D01-Jan-197018 32

.travis.ymlH A D01-Jan-197092 118

Cargo.tomlH A D01-Jan-19701.3 KiB4941

Cargo.toml.orig-cargoH A D01-Jan-1970703 3327

LICENSEH A D01-Jan-19701 KiB2217

LICENSE-APACHEH A D01-Jan-197010.6 KiB202169

README.mdH A D01-Jan-19705 KiB8963

default.nixH A D01-Jan-1970152 86

README.md

1# Robust domain name parsing and RFC compliant email address validation
2
3[![Build Status](https://travis-ci.org/rushmorem/publicsuffix.svg?branch=master)](https://travis-ci.org/rushmorem/publicsuffix) [![Latest Version](https://img.shields.io/crates/v/publicsuffix.svg)](https://crates.io/crates/publicsuffix) [![Docs](https://docs.rs/publicsuffix/badge.svg)](https://docs.rs/publicsuffix)
4
5This library uses Mozilla's [Public Suffix List](https://publicsuffix.org) to reliably parse domain names and email addresses in [Rust](https://www.rust-lang.org). Though parsing domain names is it's primary goal, it also fully exposes the list allowing you to use convenient methods like `list.all()` to get all known domain extensions or `list.icann()` to get only ICANN extensions.
6
7If all you need is to check whether a domain is syntactically correct and do not need to utilise the list you can just use `Domain::has_valid_syntax` method. This method will reliably tell you if a domain has valid syntax whether or not it is an internationalised domain name (IDN). It also checks the length restrictions for each label, total number of labels and full length of domain name.
8
9This crate doesn't cache the public suffix list for you. If you want to use this crate in a long running application and want to make use of the public suffix list, I highly recommend you use the [psl](https://github.com/rushmorem/psl) crate which does this for you.
10
11## Setting Up
12
13Add this crate to your `Cargo.toml`:
14
15```toml
16[dependencies.publicsuffix]
17version = "1.5"
18
19# This crate exposes the methods `List::fetch` and `List::from_url` as a
20# feature named "remote_list". This feature is on by default. If you have
21# the public suffix list on your local filesystem or you would like
22# to fetch this list on your own you can disable this feature and build
23# the list using `List::from_path` or `List::from_reader` respectively.
24#
25# To disable, uncomment the line below:
26# default-features = false
27```
28
29## Examples
30
31```rust
32extern crate publicsuffix;
33
34use publicsuffix::List;
35
36// Fetch the list from the official URL,
37let list = List::fetch()?;
38
39// from your own URL
40let list = List::from_url("https://example.com/path/to/public_suffix_list.dat")?;
41
42// or from a local file. You can download the list from
43// "https://publicsuffix.org/list/public_suffix_list.dat".
44let list = List::from_path("/path/to/public_suffix_list.dat")?;
45
46// Using the list you can find out the root domain
47// or extension of any given domain name
48let domain = list.parse_domain("www.example.com")?;
49assert_eq!(domain.root(), Some("example.com"));
50assert_eq!(domain.suffix(), Some("com"));
51
52let domain = list.parse_domain("www.食狮.中国")?;
53assert_eq!(domain.root(), Some("食狮.中国"));
54assert_eq!(domain.suffix(), Some("中国"));
55
56let domain = list.parse_domain("www.xn--85x722f.xn--55qx5d.cn")?;
57assert_eq!(domain.root(), Some("xn--85x722f.xn--55qx5d.cn"));
58assert_eq!(domain.suffix(), Some("xn--55qx5d.cn"));
59
60let domain = list.parse_domain("a.b.example.uk.com")?;
61assert_eq!(domain.root(), Some("example.uk.com"));
62assert_eq!(domain.suffix(), Some("uk.com"));
63
64let name = list.parse_dns_name("_tcp.example.com.")?;
65assert_eq!(name.domain().and_then(|domain| domain.root()), Some("example.com"));
66assert_eq!(name.domain().and_then(|domain| domain.suffix()), Some("com"));
67
68// You can also find out if this is an ICANN domain
69assert!(!domain.is_icann());
70
71// or a private one
72assert!(domain.is_private());
73
74// In any case if the domain's suffix is in the list
75// then this is definately a registrable domain name
76assert!(domain.has_known_suffix());
77```
78
79## Use Cases
80
81For those who work with domain names the use cases of this library are plenty. [publicsuffix.org/learn](https://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:-
82
83* Validating domain names. This one is probably obvious. If a [Domain::has_known_suffix](https://docs.rs/publicsuffix/*/publicsuffix/struct.Domain.html#method.has_known_suffix) you can be absolutely sure this is a valid domain name. A regular expression is simply not robust enough.
84* Validating email addresses. You can utilise this library to validate email addresses in a robust and reliable manner before resorting to more expensive (DNS checks) or less convenient (sending confirmation emails) ways.
85* Blacklisting or whitelisting domain names and email addresses. 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...
86* Extracting the registrable part of a domain name so you can check whether the domain is registered or not.
87* Storing details about a domain name in a DBMS using the registrable part of a domain name as the primary key.
88* Like my company, a registrar or similar organisation can draft their own list of domain extensions they support, following the same specs as the original list, and then use this library to check whether a requested domain name is actually supported.
89