Origin

Enum Origin 

Source
pub enum Origin {
    Null,
    Triple {
        scheme: String,
        host: String,
        port: u16,
    },
}
Expand description

A struct which implements the concept ‘Web Origin’ as defined in https://tools.ietf.org/html/rfc6454.

This implementation only considers hierarchical URLs and null.

The rationale behind skipping random id:s is that any such random origin should never be equal to another random origin. This has the implication that it’s unneccesary to compare them to each other and we might as well return parse error and handle that case separately.

Variants§

§

Null

The Null origin, indicating that a resource lacks a proper origin. This value is commonly used in the Origin header to indicate that an origin couldn’t be deduced or was deliberitely left out. This value is set instead of omitting the Origin

§

Triple

The common origin, formed from a (schem, host, port) triple.

Fields

§scheme: String

Lower-case scheme

§host: String

Host with all ascii chars lowercased and punycoded

§port: u16

The explicit port or scheme default port if not explicity set

Implementations§

Source§

impl Origin

Source

pub fn parse(s: &str) -> Result<Origin, String>

Parses the given string as an origin. #Errors Errors are returned if

  • The argument cannot be parsed as an URL
  • There’s no host in the URL
  • The URL scheme is not supported by the URL parser (rust-url)
  • If there is no known default port for the scheme

#Examples

use corsware::Origin;
let o1 = Origin::parse("http://exämple.com");
let o2 = Origin::parse("hTtP://user:password@eXämpLe.cOm:80/a/path.html");
assert_eq!(o1, o2);
Source

pub fn parse_allow_null(s: &str) -> Result<Origin, String>

Parses the given string as an origin. Allows for “null” to be parsed as Origin::Null and should only be used in cases where getting Null origin is not a security problem. Remember that Null origin should be treated as “Origin could not be deduced”

#Examples

use corsware::Origin;
let o1 = Origin::parse_allow_null("null");
assert_eq!(o1, Ok(Origin::Null));
let o2 = Origin::parse_allow_null("http://www.a.com");
assert_eq!(o2, Ok(Origin::Triple {
        scheme: "http".to_owned(),
        host: "www.a.com".to_owned(),
        port: 80u16
        }));
Source

pub fn scheme(&self) -> &String

Returns the scheme of the origin in lower case. #Example

use corsware::Origin;
assert_eq!(Origin::parse("hTtP://a.com").unwrap().scheme(), &"http".to_owned());
Source

pub fn host(&self) -> &String

Returns the host of the origin in ascii lower case. #Example

use corsware::Origin;
assert_eq!(Origin::parse("ftp://Aö.coM").unwrap().host(), &"xn--a-1ga.com".to_owned());
Source

pub fn port(&self) -> u16

Returns the port of the origin. Will return the default port if not set explicitly #Example

use corsware::Origin;
assert_eq!(Origin::parse("ftp://a.com").unwrap().port(), 21);

Trait Implementations§

Source§

impl Clone for Origin

Source§

fn clone(&self) -> Origin

Returns a duplicate 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 Origin

Source§

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

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

impl Hash for Origin

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 PartialEq for Origin

Source§

fn eq(&self, other: &Origin) -> 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 Eq for Origin

Source§

impl StructuralPartialEq for Origin

Auto Trait Implementations§

§

impl Freeze for Origin

§

impl RefUnwindSafe for Origin

§

impl Send for Origin

§

impl Sync for Origin

§

impl Unpin for Origin

§

impl UnwindSafe for Origin

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> CloneAny for T
where T: Any + Clone,

Source§

fn clone_any(&self) -> Box<dyn CloneAny>

Source§

fn clone_any_send(&self) -> Box<dyn CloneAny + Send>
where T: Send,

Source§

fn clone_any_sync(&self) -> Box<dyn CloneAny + Sync>
where T: Sync,

Source§

fn clone_any_send_sync(&self) -> Box<dyn CloneAny + Sync + Send>
where T: Send + Sync,

Source§

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

Source§

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

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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, 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> Typeable for T
where T: Any,

Source§

fn get_type(&self) -> TypeId

Get the TypeId of this object.
Source§

impl<T> DebugAny for T
where T: Any + Debug,

Source§

impl<T> UnsafeAny for T
where T: Any,