Struct Domain

Source
pub struct Domain {
    is_wildcard: bool,
    labels: Vec<Label>,
}
Expand description

A domain name split into individual labels (not including the root label).

Labels are stored in most-significant-first order, i.e. "www.example.com." would be stored as ["com", "example", "www"]. Labels are stored in their ASCII-encoded form (A-labels for internationalized domain name labels). If the left-most label is equal to '*', is_wildcard is set to true, and the label itself is not stored in labels.

See the library documentation for details about syntax rules for domain names.

§Eq vs matches()

The PartialEq / Eq implementation for Domain is equivalent to a string comparison of the domains (e.g. "example.com" == "example.com", but "*.example.com" != "www.example.com"). Domain::matches, on the other hand checks if a reference identifier domain matches a given presented identifier domain (e.g. "example.com".matches("example.com"), and "*.example.com".matches("www.example.com")). Care should be taken to use the appropriate method in each situation.

Fields§

§is_wildcard: bool

Indicates whether the domain is a wildcard, i.e. that the left-most label is exactly equal to "*"

§labels: Vec<Label>

The labels of the domain, in right-to-left (most-significant-first) order, not including the seperators or wildcard label (if any)

Implementations§

Source§

impl Domain

Source

pub fn reference(input: &str) -> Result<Self, ParseError>

Create a new Domain from a [reference identifier], without allowing wildcards. Note that this function assumes the input is already ACE-encoded and it does not check the validity of A-labels, allowing so-called “fake A-labels” (labels starting with “xn–”, while not being valid punycode).

§Errors

Returns a ParseError if the parsing of the domain name fails. See the documentation for the error type for an explanation of possible error variants.

§Examples
let example = Domain::reference(&"www.example.com".to_string())?;
assert!(!example.is_wildcard());
assert_eq!(example.labels().len(), 3);
assert_eq!(example.labels()[0].as_ref(), "com");
assert_eq!(example.labels()[1].as_ref(), "example");
assert_eq!(example.labels()[2].as_ref(), "www");

let wildcard = Domain::reference(&"*.example.com".to_string());
assert!(wildcard.is_err());
assert!(matches!(wildcard, Err(ParseError::InvalidChar('*'))));
if let Some(server_name) = client_hello.server_name() {
	let domain = Domain::reference(&server_name).ok()?;
	let certificate = certificates.get(&domain)?;
	Some(certificate.clone())
} else {
	get_default_cert()
}
Source

pub fn presented(input: &str) -> Result<Self, ParseError>

Create a new Domain from a presented identifier, while also checking for wildcards. This function accepts and encodes ASCII labels, A-labels, or U-labels, or a mix of them. If the leftmost label is “*”, then the domain name is considered a wildcard domain, and is_wildcard is set to true. Additionally, this function also accepts absolute domain names (i.e. domain names ending with a ‘.’), which is not allowed in certificates.

§Errors

Returns a ParseError if the parsing of the domain name fails. See the documentation for the error type for an explanation of possible error variants.

§Examples
let example = Domain::presented(&"www.example.com".to_string())?;
assert!(!example.is_wildcard());
assert_eq!(example.labels().len(), 3);
assert_eq!(example.labels()[0].as_ref(), "com");
assert_eq!(example.labels()[1].as_ref(), "example");
assert_eq!(example.labels()[2].as_ref(), "www");

let idn = Domain::presented(&"παράδειγμα.例子.example.com".to_string())?;
assert!(!idn.is_wildcard());
assert_eq!(idn.labels().len(), 4);
assert_eq!(idn.labels()[0].as_ref(), "com");
assert_eq!(idn.labels()[1].as_ref(), "example");
assert_eq!(idn.labels()[2].as_ref(), "xn--fsqu00a");
assert_eq!(idn.labels()[3].as_ref(), "xn--hxajbheg2az3al");

let wildcard = Domain::presented(&"*.example.com".to_string())?;
assert!(wildcard.is_wildcard());
assert_eq!(wildcard.labels().len(), 2);
assert_eq!(wildcard.labels()[0].as_ref(), "com");
assert_eq!(wildcard.labels()[1].as_ref(), "example");

let wildcard_idn = Domain::presented(&"*.приклад.com".to_string())?;
assert!(wildcard_idn.is_wildcard());
assert_eq!(wildcard_idn.labels().len(), 2);
assert_eq!(wildcard_idn.labels()[0].as_ref(), "com");
assert_eq!(wildcard_idn.labels()[1].as_ref(), "xn--80aikifvh");
for (domain_name, certificate) in tls_config.get_config() {
	let domain = Domain::presented(&domain_name)?;
	let certificate = certificates.set(domain, certificate);
}
Source

pub const fn is_wildcard(&self) -> bool

Whether this Domain represents a wildcard, i.e. the left-most label is “*”. If this is true, this domain matches another non-wildcard domain, if this domain’s labels are a prefix of the other domain’s, and the other domain has exactly one extra label, e.g. if this domain has the labels ["com", "example"], then it would match another domain with labels ["com", "example", "foo"] or ["com", "example", "bar"], but not ["com", "example"] or ["com", "example", "bar", "foo"].

Source

pub fn labels(&self) -> &[Label]

Get the labels of this Domain. The labels are in right-to-left / most-significant-first order, i.e. "www.example.com" would have the labels ["com", "example", "www"]. If this domain is a wildcard domain, the wildcard label is not included in the returned slice. See Domain’s documentation for details.

Source

pub fn matches(&self, presented: &Self) -> Option<bool>

Check whether this Domain matches the given presented identifier. This domain is treated as a reference identifier, and therefore if its is_wildcard property is set, this function returns None.

Trait Implementations§

Source§

impl Clone for Domain

Source§

fn clone(&self) -> Domain

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Domain

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for Domain

Source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for Domain

Format a Domain with the given formatter. Use alternate formatting ("{:#}") to encode labels into Unicode; by default internationalized labels are formatted in their ASCII compatible encoding form.

Source§

fn fmt(&self, fmt: &mut Formatter<'_>) -> FmtResult

Formats the value using the given formatter. Read more
Source§

impl Hash for Domain

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for Domain

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Domain

Source§

fn eq(&self, other: &Domain) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Domain

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Serialize for Domain

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Eq for Domain

Source§

impl StructuralPartialEq for Domain

Auto Trait Implementations§

§

impl Freeze for Domain

§

impl RefUnwindSafe for Domain

§

impl Send for Domain

§

impl Sync for Domain

§

impl Unpin for Domain

§

impl UnwindSafe for Domain

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

§

impl<T> ErasedDestructor for T
where T: 'static,

§

impl<T> MaybeSendSync for T