Skip to content

Commit 79f863b

Browse files
committed
init Timing-Allow-Origin headers
1 parent a50b9b9 commit 79f863b

File tree

4 files changed

+110
-3
lines changed

4 files changed

+110
-3
lines changed

src/headers/constants.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ pub const PRAGMA: HeaderName = HeaderName::from_lowercase_str("pragma");
125125

126126
/// The `Proxy-Authenticate` Header
127127
pub const PROXY_AUTHENTICATE: HeaderName = HeaderName::from_lowercase_str("proxy-authenticate");
128+
128129
/// The `Proxy-Authorization` Header
129130
pub const PROXY_AUTHORIZATION: HeaderName = HeaderName::from_lowercase_str("proxy-authorization");
130131

@@ -143,6 +144,9 @@ pub const SERVER_TIMING: HeaderName = HeaderName::from_lowercase_str("server-tim
143144
/// The `Te` Header
144145
pub const TE: HeaderName = HeaderName::from_lowercase_str("te");
145146

147+
/// The `Timing-Allow-Origin` Header
148+
pub const TIMING_ALLOW_ORIGIN: HeaderName = HeaderName::from_lowercase_str("timing-allow-origin");
149+
146150
/// The `Traceparent` Header
147151
pub const TRACEPARENT: HeaderName = HeaderName::from_lowercase_str("traceparent");
148152

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,9 @@ mod status;
131131
mod status_code;
132132
mod version;
133133

134+
pub mod trace;
134135
cfg_unstable! {
135136
pub mod upgrade;
136-
pub mod trace;
137137

138138
mod client;
139139
mod server;

src/trace/allow_origin.rs

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
}

src/trace/mod.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66
//!
77
//! # Specifications
88
//!
9-
//! - [W3C Trace-Context headers](https://w3c.github.io/trace-context/)
10-
//! - [W3C Server-Timing headers](https://w3c.github.io/server-timing/#the-server-timing-header-field)
9+
//! - [W3C Trace-Context header](https://w3c.github.io/trace-context/)
10+
//! - [W3C Server-Timing header](https://w3c.github.io/server-timing/#the-server-timing-header-field)
11+
//! - [W3C Timing-Allow-Origin header](https://w3c.github.io/resource-timing/#sec-timing-allow-origin)
1112
13+
mod allow_origin;
1214
pub mod server_timing;
1315
mod trace_context;
1416

17+
pub use allow_origin::{AllowOrigin, Origin};
1518
#[doc(inline)]
1619
pub use server_timing::{Metric, ServerTiming};
1720
pub use trace_context::TraceContext;

0 commit comments

Comments
 (0)