use std::convert::TryFrom;
use super::origin::Origin;
use util::{IterExt, TryFromValues};
use HeaderValue;
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct AccessControlAllowOrigin(OriginOrAny);
derive_header! {
AccessControlAllowOrigin(_),
name: ACCESS_CONTROL_ALLOW_ORIGIN
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
enum OriginOrAny {
Origin(Origin),
Any,
}
impl AccessControlAllowOrigin {
pub const ANY: AccessControlAllowOrigin = AccessControlAllowOrigin(OriginOrAny::Any);
pub const NULL: AccessControlAllowOrigin =
AccessControlAllowOrigin(OriginOrAny::Origin(Origin::NULL));
pub fn origin(&self) -> Option<&Origin> {
match self.0 {
OriginOrAny::Origin(ref origin) => Some(origin),
_ => None,
}
}
}
impl TryFrom<&str> for AccessControlAllowOrigin {
type Error = ::Error;
fn try_from(s: &str) -> Result<Self, ::Error> {
let header_value = HeaderValue::from_str(s).map_err(|_| ::Error::invalid())?;
let origin = OriginOrAny::try_from(&header_value)?;
Ok(Self(origin))
}
}
impl TryFrom<&HeaderValue> for OriginOrAny {
type Error = ::Error;
fn try_from(header_value: &HeaderValue) -> Result<Self, ::Error> {
Origin::try_from_value(header_value)
.map(OriginOrAny::Origin)
.ok_or_else(::Error::invalid)
}
}
impl TryFromValues for OriginOrAny {
fn try_from_values<'i, I>(values: &mut I) -> Result<Self, ::Error>
where
I: Iterator<Item = &'i HeaderValue>,
{
values
.just_one()
.and_then(|value| {
if value == "*" {
return Some(OriginOrAny::Any);
}
Origin::try_from_value(value).map(OriginOrAny::Origin)
})
.ok_or_else(::Error::invalid)
}
}
impl<'a> From<&'a OriginOrAny> for HeaderValue {
fn from(origin: &'a OriginOrAny) -> HeaderValue {
match origin {
OriginOrAny::Origin(ref origin) => origin.into_value(),
OriginOrAny::Any => HeaderValue::from_static("*"),
}
}
}
#[cfg(test)]
mod tests {
use super::super::{test_decode, test_encode};
use super::*;
#[test]
fn origin() {
let s = "http://web-platform.test:8000";
let allow_origin = test_decode::<AccessControlAllowOrigin>(&[s]).unwrap();
{
let origin = allow_origin.origin().unwrap();
assert_eq!(origin.scheme(), "http");
assert_eq!(origin.hostname(), "web-platform.test");
assert_eq!(origin.port(), Some(8000));
}
let headers = test_encode(allow_origin);
assert_eq!(headers["access-control-allow-origin"], s);
}
#[test]
fn try_from_origin() {
let s = "http://web-platform.test:8000";
let allow_origin = AccessControlAllowOrigin::try_from(s).unwrap();
{
let origin = allow_origin.origin().unwrap();
assert_eq!(origin.scheme(), "http");
assert_eq!(origin.hostname(), "web-platform.test");
assert_eq!(origin.port(), Some(8000));
}
let headers = test_encode(allow_origin);
assert_eq!(headers["access-control-allow-origin"], s);
}
#[test]
fn any() {
let allow_origin = test_decode::<AccessControlAllowOrigin>(&["*"]).unwrap();
assert_eq!(allow_origin, AccessControlAllowOrigin::ANY);
let headers = test_encode(allow_origin);
assert_eq!(headers["access-control-allow-origin"], "*");
}
#[test]
fn null() {
let allow_origin = test_decode::<AccessControlAllowOrigin>(&["null"]).unwrap();
assert_eq!(allow_origin, AccessControlAllowOrigin::NULL);
let headers = test_encode(allow_origin);
assert_eq!(headers["access-control-allow-origin"], "null");
}
}