|
| 1 | +use crate::headers::{HeaderName, HeaderValue, Headers, ToHeaderValues, TIMING_ALLOW_ORIGIN}; |
| 2 | +use crate::Url; |
| 3 | +use std::option; |
| 4 | + |
| 5 | +/// `Timing-Allow-Origin` header. |
| 6 | +/// |
| 7 | +/// # Specifications |
| 8 | +/// |
| 9 | +/// - [W3C Timing-Allow-Origin header](https://w3c.github.io/resource-timing/#sec-timing-allow-origin) |
| 10 | +/// - [WhatWG Fetch Origin header](https://fetch.spec.whatwg.org/#origin-header) |
| 11 | +#[derive(Debug, Clone, Eq, PartialEq)] |
| 12 | +pub struct AllowOrigin { |
| 13 | + origins: Vec<Origin>, |
| 14 | +} |
| 15 | + |
| 16 | +impl AllowOrigin { |
| 17 | + /// Create a new instance of `AllowOrigin`. |
| 18 | + pub fn new() -> Self { |
| 19 | + Self { origins: vec![] } |
| 20 | + } |
| 21 | + |
| 22 | + /// Create an instance of `AllowOrigin` from a `Headers` instance. |
| 23 | + /// |
| 24 | + /// # Implementation note |
| 25 | + /// |
| 26 | + /// If a `"null"` value is found |
| 27 | + pub fn from_headers(headers: impl AsRef<Headers>) -> crate::Result<Option<Self>> { |
| 28 | + let allow_origin = match headers.as_ref().get(TIMING_ALLOW_ORIGIN) { |
| 29 | + Some(header) => header, |
| 30 | + None => return Ok(None), |
| 31 | + }; |
| 32 | + |
| 33 | + allow_origin.as_str().split(","); |
| 34 | + todo!(); |
| 35 | + } |
| 36 | + |
| 37 | + /// Append an origin to the list of origins. |
| 38 | + pub fn push(&mut self, origin: impl Into<Origin>) { |
| 39 | + self.origins.push(origin.into()); |
| 40 | + } |
| 41 | + |
| 42 | + /// Insert a `HeaderName` + `HeaderValue` pair into a `Headers` instance. |
| 43 | + pub fn apply(&self, headers: impl AsMut<Headers>) { |
| 44 | + todo!(); |
| 45 | + } |
| 46 | + |
| 47 | + /// Get the `HeaderName`. |
| 48 | + pub fn name(&self) -> HeaderName { |
| 49 | + todo!(); |
| 50 | + } |
| 51 | + |
| 52 | + /// Get the `HeaderValue`. |
| 53 | + pub fn value(&self) -> HeaderValue { |
| 54 | + todo!(); |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +// Conversion from `AllowOrigin` -> `HeaderValue`. |
| 59 | +impl ToHeaderValues for AllowOrigin { |
| 60 | + type Iter = option::IntoIter<HeaderValue>; |
| 61 | + fn to_header_values(&self) -> crate::Result<Self::Iter> { |
| 62 | + todo!() |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +/// An origin passed into `AllowOrigin`. |
| 67 | +/// |
| 68 | +/// Values can either be `Url` or `Wildcard`. `"null"` values are skipped during parsing. |
| 69 | +// |
| 70 | +// NOTE: this origin is different than the origin in the fetch spec. It needs to |
| 71 | +// be its own type. |
| 72 | +#[derive(Debug, Clone, Eq, PartialEq)] |
| 73 | +pub enum Origin { |
| 74 | + /// An origin URL. |
| 75 | + Url(Url), |
| 76 | + /// Allow all origins. |
| 77 | + Wildcard, |
| 78 | +} |
| 79 | + |
| 80 | +impl From<Url> for Origin { |
| 81 | + fn from(url: Url) -> Self { |
| 82 | + Origin::Url(url) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +// Conversion from `AllowOrigin` -> `HeaderValue`. |
| 87 | +impl ToHeaderValues for Origin { |
| 88 | + type Iter = option::IntoIter<HeaderValue>; |
| 89 | + fn to_header_values(&self) -> crate::Result<Self::Iter> { |
| 90 | + let res = unsafe { |
| 91 | + match self { |
| 92 | + Self::Url(url) => { |
| 93 | + HeaderValue::from_bytes_unchecked(format!("{}", url).into_bytes()) |
| 94 | + } |
| 95 | + Self::Wildcard => HeaderValue::from_bytes_unchecked(String::from("*").into_bytes()), |
| 96 | + } |
| 97 | + }; |
| 98 | + Ok(Some(res).into_iter()) |
| 99 | + } |
| 100 | +} |
0 commit comments