1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
use lazy_static::lazy_static; use regex::Regex; use std::convert::TryFrom; use std::fmt::{Display, Formatter, Result as FmtResult}; use thiserror::Error; pub(crate) const DOMAIN_NAME_MAX_RECURSION: usize = 16; #[derive(Debug, PartialEq, Eq, Error)] pub enum DomainNameError { #[error("Label is too big: {0}]")] LabelLength(usize), #[error("Domain name is too big: {0}")] DomainNameLength(usize), #[error("Domain name contains illegal character: {0}")] Regex(String), } #[derive(Debug, PartialEq, Clone, Eq, Hash)] pub struct DomainName(pub(super) String); impl DomainName { pub fn append_label(&mut self, label: &str) -> Result<(), DomainNameError> { lazy_static! { static ref LABEL_REGEX: Regex = Regex::new(r"^[_0-9a-zA-Z]([_0-9a-zA-Z-]*[_0-9a-zA-Z])?$").unwrap(); } let label_length = label.len(); if label_length >= 64 { return Err(DomainNameError::LabelLength(label_length)); } let domain_name_length = self.0.len() + label_length; if domain_name_length >= 256 { return Err(DomainNameError::DomainNameLength(domain_name_length)); } if LABEL_REGEX.is_match(label) { let label = label.to_lowercase(); if &self.0 == "." { self.0.insert_str(0, &label); } else { self.0.push_str(&label); self.0.push('.'); } Ok(()) } else { Err(DomainNameError::Regex(label.to_string())) } } } impl TryFrom<&str> for DomainName { type Error = DomainNameError; fn try_from(string: &str) -> Result<Self, DomainNameError> { let string_relativ = if let Some(string_relativ) = string.strip_suffix('.') { string_relativ } else { string }; let mut domain_name = DomainName::default(); for label in string_relativ.split('.') { domain_name.append_label(label)?; } Ok(domain_name) } } impl Default for DomainName { fn default() -> Self { DomainName(".".to_string()) } } impl From<DomainName> for String { fn from(domain_name: DomainName) -> Self { domain_name.0 } } impl PartialEq<&str> for DomainName { fn eq(&self, other: &&str) -> bool { self.0 == other.to_lowercase() } } impl Display for DomainName { fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { write!(f, "{}", self.0) } }