#hostname #validation #idna #punycode #publicsuffix

adbyss_psl

最小化公共后缀列表主机名验证器

54 个版本

0.12.0 2024 年 8 月 9 日
0.11.1 2024 年 7 月 25 日
0.11.0 2024 年 6 月 13 日
0.10.1 2024 年 3 月 21 日
0.4.2 2021 年 7 月 29 日

网页编程 中排名 #208

Download history 123/week @ 2024-04-29 7/week @ 2024-05-20 5/week @ 2024-06-03 118/week @ 2024-06-10 5/week @ 2024-06-17 3/week @ 2024-07-01 121/week @ 2024-07-22 430/week @ 2024-07-29 105/week @ 2024-08-05 18/week @ 2024-08-12

每月下载量 674

WTFPL 许可证

105KB
721 行代码(不包括注释)

Adbyss: 公共后缀

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

该库包含一个面向公共的 struct —— adbyss_psl::Domain —— 用于验证和规范化互联网主机名,如 "www.domain.com"。

它将

  • 验证、规范化和 Puny 编码国际化/Unicode 标签(《RFC 3492》[https://datatracker.ietf.org/doc/html/rfc3492#ref-IDNA]);
  • 验证和规范化公共后缀(《公共后缀列表》[https://publicsuffix.org/list/]);
  • 确保符合《RFC 1123》[https://datatracker.ietf.org/doc/html/rfc1123];
  • 并定位子域(如果有)、根(必需)和后缀(必需)的边界;

示例

可以使用 DomainDomain::newTryFrom<&str> 初始化 Domain 的新实例。

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());

有效的互联网主机名长度不得超过 253 个字符,并且包含根和(有效的)后缀组件。

它们的标签——点之间的部分——还必须

  • 长度不超过 63 个字符;
  • (最终)只包含 ASCII 字母、数字和 -
  • 以字母数字字符开头和结尾;

允许 Unicode/国际化标签,但必须是 Puny 编码的,并且不能包含任何冲突的 bidirectional 约束。当 Domain 发现这样的标签时,它将使用 Punycode 对其进行编码,确保生成的域名始终只包含 ASCII 字符。

解析后,Domain 允许您访问每个单独的组件或整个结构

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"

可选的 Crate 功能

  • serde:启用序列化和反序列化支持。

许可证

版权 © 2024 Blobfolio, LLC <[email protected]>

本作品是免费的。您可以在 Do What The Fuck You Want To Public License,版本 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.

依赖关系

约 4.5MB
~78K SLoC