pub struct DomainOrigin<'a, 'b> {
pub scheme: Scheme<'a>,
pub host: &'b str,
pub port: Port,
}Expand description
A tuple origin.
This can be used to make origin validation more convenient.
Fields§
§scheme: Scheme<'a>The scheme.
host: &'b strThe host.
port: PortThe TCP/UDP port.
Implementations§
Source§impl<'b> DomainOrigin<'_, 'b>
impl<'b> DomainOrigin<'_, 'b>
Sourcepub const fn new<'c: 'b>(host: &'c str) -> Self
pub const fn new<'c: 'b>(host: &'c str) -> Self
Returns a DomainOrigin with Self::scheme as Scheme::Https, Self::host as host, and
Self::port as Port::None.
§Examples
assert_eq!(
DomainOrigin::new("www.example.com"),
Origin(Cow::Borrowed("https://www.example.com"))
);
// `DomainOrigin::new` does not allow _any_ port to exist.
assert_ne!(
DomainOrigin::new("www.example.com"),
Origin(Cow::Borrowed("https://www.example.com:443"))
);Sourcepub const fn new_ignore_port<'c: 'b>(host: &'c str) -> Self
pub const fn new_ignore_port<'c: 'b>(host: &'c str) -> Self
Returns a DomainOrigin with Self::scheme as Scheme::Https, Self::host as host, and
Self::port as Port::Any.
§Examples
// Any port is allowed to exist.
assert_eq!(
DomainOrigin::new_ignore_port("www.example.com"),
Origin(Cow::Borrowed("https://www.example.com:1234"))
);
// A port doesn't have to exist at all either.
assert_eq!(
DomainOrigin::new_ignore_port("www.example.com"),
Origin(Cow::Borrowed("https://www.example.com"))
);Trait Implementations§
Source§impl<'a, 'b> Clone for DomainOrigin<'a, 'b>
impl<'a, 'b> Clone for DomainOrigin<'a, 'b>
impl<'a, 'b> Copy for DomainOrigin<'a, 'b>
Source§impl<'a, 'b> Debug for DomainOrigin<'a, 'b>
impl<'a, 'b> Debug for DomainOrigin<'a, 'b>
Source§impl PartialEq<&DomainOrigin<'_, '_>> for Origin<'_>
impl PartialEq<&DomainOrigin<'_, '_>> for Origin<'_>
Source§impl PartialEq<&Origin<'_>> for DomainOrigin<'_, '_>
impl PartialEq<&Origin<'_>> for DomainOrigin<'_, '_>
Source§impl PartialEq<DomainOrigin<'_, '_>> for Origin<'_>
impl PartialEq<DomainOrigin<'_, '_>> for Origin<'_>
Source§impl PartialEq<DomainOrigin<'_, '_>> for &Origin<'_>
impl PartialEq<DomainOrigin<'_, '_>> for &Origin<'_>
Source§impl PartialEq<Origin<'_>> for DomainOrigin<'_, '_>
impl PartialEq<Origin<'_>> for DomainOrigin<'_, '_>
Source§fn eq(&self, other: &Origin<'_>) -> bool
fn eq(&self, other: &Origin<'_>) -> bool
Returns true iff DomainOrigin::scheme, DomainOrigin::host, and DomainOrigin::port are the
same after calling DomainOrigin::try_from on other.0.as_str().
Note that Scheme and Port need not be the same variant. For example Scheme::Https and
Scheme::Other containing "https" will be treated the same.
Source§impl PartialEq<Origin<'_>> for &DomainOrigin<'_, '_>
impl PartialEq<Origin<'_>> for &DomainOrigin<'_, '_>
Source§impl<'a: 'b + 'c, 'b, 'c> TryFrom<&'a str> for DomainOrigin<'b, 'c>
impl<'a: 'b + 'c, 'b, 'c> TryFrom<&'a str> for DomainOrigin<'b, 'c>
Source§fn try_from(value: &'a str) -> Result<Self, Self::Error>
fn try_from(value: &'a str) -> Result<Self, Self::Error>
value is parsed according to the following extended regex:
^([^:]*:\/\/)?[^:]*(:.*)?$
where the [^:]* of the first capturing group is parsed according to Scheme::try_from, and
the .* of the second capturing group is parsed according to Port::from_str.
§Errors
Errors iff Scheme::try_from or Port::from_str fail when applicable.
§Examples
assert!(
DomainOrigin::try_from("https://www.example.com:443").map_or(false, |dom| matches!(
dom.scheme,
Scheme::Https
) && dom.host
== "www.example.com"
&& matches!(dom.port, Port::Val(port) if port == 443))
);
// Parsing is done in a case sensitive way.
assert!(DomainOrigin::try_from("Https://www.EXample.com").map_or(
false,
|dom| matches!(dom.scheme, Scheme::Other(scheme) if scheme == "Https")
&& dom.host == "www.EXample.com"
&& matches!(dom.port, Port::None)
));