Skip to main content

Crate psl2

Crate psl2 

Source
Expand description

psl2 — a modern alternative to the psl crate for working with Mozilla’s Public Suffix List.

It answers the practical question “is this hostname a registrable domain (one that can own cookies), or is it a public suffix?” — e.g. example.jp is registrable, while co.jp is a public suffix.

// The public suffix ("effective TLD"):
assert_eq!(psl2::suffix("www.example.co.uk").as_deref(), Some("co.uk"));

// The registrable domain (eTLD + 1) — the cookie domain:
assert_eq!(
    psl2::registrable_domain("www.example.co.uk").as_deref(),
    Some("example.co.uk")
);

// A bare public suffix has no registrable domain:
assert_eq!(psl2::registrable_domain("co.uk"), None);
assert!(psl2::is_public_suffix("co.uk"));

Unicode / internationalized domains are handled transparently (the idna feature, enabled by default); inputs and outputs are normalized to ASCII/punycode:

assert_eq!(psl2::registrable_domain("食狮.公司.cn").as_deref(), Some("xn--85x722f.xn--55qx5d.cn"));

§How it differs from psl

  • Fast builds. The list is pre-normalized to ASCII at publish time and embedded as plain data (include_str!). There is no build.rs and no procedural-macro codegen, so depending on psl2 adds almost nothing to your compile time.
  • Built-in IDNA. Queries are normalized for you; you do not have to punycode-encode input first.
  • Clean &str API with explicit ICANN / private / unknown classification.
  • Always current. CI republishes the crate whenever the upstream list changes (see psl_version).

§A note on “surprising” suffixes

The list’s PRIVATE section contains organizationally-delegated suffixes such as blogspot.com, github.io, and s3.amazonaws.com. These are public suffixes, so registrable_domain("blogspot.com") is None and registrable_domain("foo.blogspot.com") is Some("foo.blogspot.com"). This matches browser cookie behavior, but can be surprising. Use Info::is_private / Info::is_icann if you need to tell the two sections apart:

let info = psl2::analyze("foo.blogspot.com").unwrap();
assert_eq!(info.suffix(), "blogspot.com");
assert!(info.is_private()); // delegated, not an ICANN registry suffix

Structs§

Info
The result of analyzing a hostname against the Public Suffix List.

Enums§

Type
Which section of the Public Suffix List a rule comes from.

Functions§

analyze
Analyze a hostname against the Public Suffix List.
is_public_suffix
true if domain is itself a public suffix (and so cannot own cookies).
psl_version
The upstream Public Suffix List version (a UTC timestamp string) that this build of psl2 was generated from.
registrable_domain
The registrable domain (eTLD + 1) of a hostname, e.g. example.co.uk.
subdomain
The subdomain (the labels to the left of the registrable domain) of a hostname, e.g. www for www.example.co.uk.
suffix
The public suffix (“effective TLD”) of a hostname, e.g. co.uk for www.example.co.uk.