diff options
Diffstat (limited to 'third_party/rust/headers/src/common')
52 files changed, 6105 insertions, 0 deletions
diff --git a/third_party/rust/headers/src/common/accept_ranges.rs b/third_party/rust/headers/src/common/accept_ranges.rs new file mode 100644 index 0000000000..cfea449982 --- /dev/null +++ b/third_party/rust/headers/src/common/accept_ranges.rs @@ -0,0 +1,42 @@ +use util::FlatCsv; + +/// `Accept-Ranges` header, defined in [RFC7233](http://tools.ietf.org/html/rfc7233#section-2.3) +/// +/// The `Accept-Ranges` header field allows a server to indicate that it +/// supports range requests for the target resource. +/// +/// # ABNF +/// +/// ```text +/// Accept-Ranges = acceptable-ranges +/// acceptable-ranges = 1#range-unit / \"none\" +/// +/// # Example values +/// * `bytes` +/// * `none` +/// * `unknown-unit` +/// ``` +/// +/// # Examples +/// +/// ``` +/// use headers::{AcceptRanges, HeaderMap, HeaderMapExt}; +/// +/// let mut headers = HeaderMap::new(); +/// +/// headers.typed_insert(AcceptRanges::bytes()); +/// ``` +#[derive(Clone, Debug, PartialEq)] +pub struct AcceptRanges(FlatCsv); + +derive_header! { + AcceptRanges(_), + name: ACCEPT_RANGES +} + +impl AcceptRanges { + /// A constructor to easily create the common `Accept-Ranges: bytes` header. + pub fn bytes() -> Self { + AcceptRanges(::HeaderValue::from_static("bytes").into()) + } +} diff --git a/third_party/rust/headers/src/common/access_control_allow_credentials.rs b/third_party/rust/headers/src/common/access_control_allow_credentials.rs new file mode 100644 index 0000000000..583f310aff --- /dev/null +++ b/third_party/rust/headers/src/common/access_control_allow_credentials.rs @@ -0,0 +1,72 @@ +use {Header, HeaderName, HeaderValue}; + +/// `Access-Control-Allow-Credentials` header, part of +/// [CORS](http://www.w3.org/TR/cors/#access-control-allow-headers-response-header) +/// +/// > The Access-Control-Allow-Credentials HTTP response header indicates whether the +/// > response to request can be exposed when the credentials flag is true. When part +/// > of the response to an preflight request it indicates that the actual request can +/// > be made with credentials. The Access-Control-Allow-Credentials HTTP header must +/// > match the following ABNF: +/// +/// # ABNF +/// +/// ```text +/// Access-Control-Allow-Credentials: "Access-Control-Allow-Credentials" ":" "true" +/// ``` +/// +/// Since there is only one acceptable field value, the header struct does not accept +/// any values at all. Setting an empty `AccessControlAllowCredentials` header is +/// sufficient. See the examples below. +/// +/// # Example values +/// * "true" +/// +/// # Examples +/// +/// ``` +/// # extern crate headers; +/// use headers::AccessControlAllowCredentials; +/// +/// let allow_creds = AccessControlAllowCredentials; +/// ``` +#[derive(Clone, PartialEq, Eq, Debug)] +pub struct AccessControlAllowCredentials; + +impl Header for AccessControlAllowCredentials { + fn name() -> &'static HeaderName { + &::http::header::ACCESS_CONTROL_ALLOW_CREDENTIALS + } + + fn decode<'i, I: Iterator<Item = &'i HeaderValue>>(values: &mut I) -> Result<Self, ::Error> { + values + .next() + .and_then(|value| { + if value == "true" { + Some(AccessControlAllowCredentials) + } else { + None + } + }) + .ok_or_else(::Error::invalid) + } + + fn encode<E: Extend<::HeaderValue>>(&self, values: &mut E) { + values.extend(::std::iter::once(HeaderValue::from_static("true"))); + } +} + +#[cfg(test)] +mod tests { + use super::super::test_decode; + use super::*; + + #[test] + fn allow_credentials_is_case_sensitive() { + let allow_header = test_decode::<AccessControlAllowCredentials>(&["true"]); + assert!(allow_header.is_some()); + + let allow_header = test_decode::<AccessControlAllowCredentials>(&["True"]); + assert!(allow_header.is_none()); + } +} diff --git a/third_party/rust/headers/src/common/access_control_allow_headers.rs b/third_party/rust/headers/src/common/access_control_allow_headers.rs new file mode 100644 index 0000000000..ca3d100d3b --- /dev/null +++ b/third_party/rust/headers/src/common/access_control_allow_headers.rs @@ -0,0 +1,98 @@ +use std::iter::FromIterator; + +use util::FlatCsv; +use {HeaderName, HeaderValue}; + +/// `Access-Control-Allow-Headers` header, part of +/// [CORS](http://www.w3.org/TR/cors/#access-control-allow-headers-response-header) +/// +/// The `Access-Control-Allow-Headers` header indicates, as part of the +/// response to a preflight request, which header field names can be used +/// during the actual request. +/// +/// # ABNF +/// +/// ```text +/// Access-Control-Allow-Headers: "Access-Control-Allow-Headers" ":" #field-name +/// ``` +/// +/// # Example values +/// * `accept-language, date` +/// +/// # Examples +/// +/// ``` +/// # extern crate headers; +/// extern crate http; +/// use http::header::{CACHE_CONTROL, CONTENT_TYPE}; +/// use headers::AccessControlAllowHeaders; +/// +/// let allow_headers = vec![CACHE_CONTROL, CONTENT_TYPE] +/// .into_iter() +/// .collect::<AccessControlAllowHeaders>(); +/// ``` +#[derive(Clone, Debug, PartialEq)] +pub struct AccessControlAllowHeaders(FlatCsv); + +derive_header! { + AccessControlAllowHeaders(_), + name: ACCESS_CONTROL_ALLOW_HEADERS +} + +impl AccessControlAllowHeaders { + /// Returns an iterator over `HeaderName`s contained within. + pub fn iter<'a>(&'a self) -> impl Iterator<Item = HeaderName> + 'a { + self.0 + .iter() + .map(|s| s.parse().ok()) + .take_while(|val| val.is_some()) + .filter_map(|val| val) + } +} + +impl FromIterator<HeaderName> for AccessControlAllowHeaders { + fn from_iter<I>(iter: I) -> Self + where + I: IntoIterator<Item = HeaderName>, + { + let flat = iter.into_iter().map(HeaderValue::from).collect(); + AccessControlAllowHeaders(flat) + } +} + +#[cfg(test)] +mod tests { + use super::super::{test_decode, test_encode}; + use super::*; + + #[test] + fn iter() { + let allow_headers = test_decode::<AccessControlAllowHeaders>(&["foo, bar"]).unwrap(); + + let as_vec = allow_headers.iter().collect::<Vec<_>>(); + assert_eq!(as_vec.len(), 2); + assert_eq!(as_vec[0], "foo"); + assert_eq!(as_vec[1], "bar"); + } + + #[test] + fn from_iter() { + let allow: AccessControlAllowHeaders = + vec![::http::header::CACHE_CONTROL, ::http::header::IF_RANGE] + .into_iter() + .collect(); + + let headers = test_encode(allow); + assert_eq!( + headers["access-control-allow-headers"], + "cache-control, if-range" + ); + } + + #[test] + fn test_with_invalid() { + let allow_headers = test_decode::<AccessControlAllowHeaders>(&["foo foo, bar"]).unwrap(); + + assert!(allow_headers.iter().collect::<Vec<_>>().is_empty()); + } +} diff --git a/third_party/rust/headers/src/common/access_control_allow_methods.rs b/third_party/rust/headers/src/common/access_control_allow_methods.rs new file mode 100644 index 0000000000..0fd1438c38 --- /dev/null +++ b/third_party/rust/headers/src/common/access_control_allow_methods.rs @@ -0,0 +1,91 @@ +use std::iter::FromIterator; + +use http::Method; + +use util::FlatCsv; + +/// `Access-Control-Allow-Methods` header, part of +/// [CORS](http://www.w3.org/TR/cors/#access-control-allow-methods-response-header) +/// +/// The `Access-Control-Allow-Methods` header indicates, as part of the +/// response to a preflight request, which methods can be used during the +/// actual request. +/// +/// # ABNF +/// +/// ```text +/// Access-Control-Allow-Methods: "Access-Control-Allow-Methods" ":" #Method +/// ``` +/// +/// # Example values +/// * `PUT, DELETE, XMODIFY` +/// +/// # Examples +/// +/// ``` +/// # extern crate headers; +/// extern crate http; +/// use http::Method; +/// use headers::AccessControlAllowMethods; +/// +/// let allow_methods = vec![Method::GET, Method::PUT] +/// .into_iter() +/// .collect::<AccessControlAllowMethods>(); +/// ``` +#[derive(Clone, Debug, PartialEq)] +pub struct AccessControlAllowMethods(FlatCsv); + +derive_header! { + AccessControlAllowMethods(_), + name: ACCESS_CONTROL_ALLOW_METHODS +} + +impl AccessControlAllowMethods { + /// Returns an iterator over `Method`s contained within. + pub fn iter<'a>(&'a self) -> impl Iterator<Item = Method> + 'a { + self.0.iter().filter_map(|s| s.parse().ok()) + } +} + +impl FromIterator<Method> for AccessControlAllowMethods { + fn from_iter<I>(iter: I) -> Self + where + I: IntoIterator<Item = Method>, + { + let methods = iter + .into_iter() + .map(|method| { + method + .as_str() + .parse::<::HeaderValue>() + .expect("Method is a valid HeaderValue") + }) + .collect(); + + AccessControlAllowMethods(methods) + } +} + +#[cfg(test)] +mod tests { + use super::super::{test_decode, test_encode}; + use super::*; + + #[test] + fn iter() { + let allowed = test_decode::<AccessControlAllowMethods>(&["GET, PUT"]).unwrap(); + + let as_vec = allowed.iter().collect::<Vec<_>>(); + assert_eq!(as_vec.len(), 2); + assert_eq!(as_vec[0], Method::GET); + assert_eq!(as_vec[1], Method::PUT); + } + + #[test] + fn from_iter() { + let allow: AccessControlAllowMethods = vec![Method::GET, Method::PUT].into_iter().collect(); + + let headers = test_encode(allow); + assert_eq!(headers["access-control-allow-methods"], "GET, PUT"); + } +} diff --git a/third_party/rust/headers/src/common/access_control_allow_origin.rs b/third_party/rust/headers/src/common/access_control_allow_origin.rs new file mode 100644 index 0000000000..8d38bf17b8 --- /dev/null +++ b/third_party/rust/headers/src/common/access_control_allow_origin.rs @@ -0,0 +1,169 @@ +use std::convert::TryFrom; + +use super::origin::Origin; +use util::{IterExt, TryFromValues}; +use HeaderValue; + +/// The `Access-Control-Allow-Origin` response header, +/// part of [CORS](http://www.w3.org/TR/cors/#access-control-allow-origin-response-header) +/// +/// The `Access-Control-Allow-Origin` header indicates whether a resource +/// can be shared based by returning the value of the Origin request header, +/// `*`, or `null` in the response. +/// +/// ## ABNF +/// +/// ```text +/// Access-Control-Allow-Origin = "Access-Control-Allow-Origin" ":" origin-list-or-null | "*" +/// ``` +/// +/// ## Example values +/// * `null` +/// * `*` +/// * `http://google.com/` +/// +/// # Examples +/// +/// ``` +/// # extern crate headers; +/// use headers::AccessControlAllowOrigin; +/// use std::convert::TryFrom; +/// +/// let any_origin = AccessControlAllowOrigin::ANY; +/// let null_origin = AccessControlAllowOrigin::NULL; +/// let origin = AccessControlAllowOrigin::try_from("http://web-platform.test:8000"); +/// ``` +#[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), + /// Allow all origins + Any, +} + +impl AccessControlAllowOrigin { + /// `Access-Control-Allow-Origin: *` + pub const ANY: AccessControlAllowOrigin = AccessControlAllowOrigin(OriginOrAny::Any); + /// `Access-Control-Allow-Origin: null` + pub const NULL: AccessControlAllowOrigin = + AccessControlAllowOrigin(OriginOrAny::Origin(Origin::NULL)); + + /// Returns the origin if there's one specified. + 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"); + } +} diff --git a/third_party/rust/headers/src/common/access_control_expose_headers.rs b/third_party/rust/headers/src/common/access_control_expose_headers.rs new file mode 100644 index 0000000000..a9b7a50f98 --- /dev/null +++ b/third_party/rust/headers/src/common/access_control_expose_headers.rs @@ -0,0 +1,88 @@ +use std::iter::FromIterator; + +use util::FlatCsv; +use {HeaderName, HeaderValue}; + +/// `Access-Control-Expose-Headers` header, part of +/// [CORS](http://www.w3.org/TR/cors/#access-control-expose-headers-response-header) +/// +/// The Access-Control-Expose-Headers header indicates which headers are safe to expose to the +/// API of a CORS API specification. +/// +/// # ABNF +/// +/// ```text +/// Access-Control-Expose-Headers = "Access-Control-Expose-Headers" ":" #field-name +/// ``` +/// +/// # Example values +/// * `ETag, Content-Length` +/// +/// # Examples +/// +/// ``` +/// # extern crate headers; +/// extern crate http; +/// # fn main() { +/// use http::header::{CONTENT_LENGTH, ETAG}; +/// use headers::AccessControlExposeHeaders; +/// +/// let expose = vec![CONTENT_LENGTH, ETAG] +/// .into_iter() +/// .collect::<AccessControlExposeHeaders>(); +/// # } +/// ``` +#[derive(Clone, Debug)] +pub struct AccessControlExposeHeaders(FlatCsv); + +derive_header! { + AccessControlExposeHeaders(_), + name: ACCESS_CONTROL_EXPOSE_HEADERS +} + +impl AccessControlExposeHeaders { + /// Returns an iterator over `HeaderName`s contained within. + pub fn iter<'a>(&'a self) -> impl Iterator<Item = HeaderName> + 'a { + self.0.iter().filter_map(|s| s.parse().ok()) + } +} + +impl FromIterator<HeaderName> for AccessControlExposeHeaders { + fn from_iter<I>(iter: I) -> Self + where + I: IntoIterator<Item = HeaderName>, + { + let flat = iter.into_iter().map(HeaderValue::from).collect(); + AccessControlExposeHeaders(flat) + } +} + +#[cfg(test)] +mod tests { + use super::super::{test_decode, test_encode}; + use super::*; + + #[test] + fn iter() { + let expose_headers = test_decode::<AccessControlExposeHeaders>(&["foo, bar"]).unwrap(); + + let as_vec = expose_headers.iter().collect::<Vec<_>>(); + assert_eq!(as_vec.len(), 2); + assert_eq!(as_vec[0], "foo"); + assert_eq!(as_vec[1], "bar"); + } + + #[test] + fn from_iter() { + let expose: AccessControlExposeHeaders = + vec![::http::header::CACHE_CONTROL, ::http::header::IF_RANGE] + .into_iter() + .collect(); + + let headers = test_encode(expose); + assert_eq!( + headers["access-control-expose-headers"], + "cache-control, if-range" + ); + } +} diff --git a/third_party/rust/headers/src/common/access_control_max_age.rs b/third_party/rust/headers/src/common/access_control_max_age.rs new file mode 100644 index 0000000000..b1bd0c35d6 --- /dev/null +++ b/third_party/rust/headers/src/common/access_control_max_age.rs @@ -0,0 +1,48 @@ +use std::time::Duration; + +use util::Seconds; + +/// `Access-Control-Max-Age` header, part of +/// [CORS](http://www.w3.org/TR/cors/#access-control-max-age-response-header) +/// +/// The `Access-Control-Max-Age` header indicates how long the results of a +/// preflight request can be cached in a preflight result cache. +/// +/// # ABNF +/// +/// ```text +/// Access-Control-Max-Age = \"Access-Control-Max-Age\" \":\" delta-seconds +/// ``` +/// +/// # Example values +/// +/// * `531` +/// +/// # Examples +/// +/// ``` +/// # extern crate headers; +/// use std::time::Duration; +/// use headers::AccessControlMaxAge; +/// +/// let max_age = AccessControlMaxAge::from(Duration::from_secs(531)); +/// ``` +#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct AccessControlMaxAge(Seconds); + +derive_header! { + AccessControlMaxAge(_), + name: ACCESS_CONTROL_MAX_AGE +} + +impl From<Duration> for AccessControlMaxAge { + fn from(dur: Duration) -> AccessControlMaxAge { + AccessControlMaxAge(dur.into()) + } +} + +impl From<AccessControlMaxAge> for Duration { + fn from(acma: AccessControlMaxAge) -> Duration { + acma.0.into() + } +} diff --git a/third_party/rust/headers/src/common/access_control_request_headers.rs b/third_party/rust/headers/src/common/access_control_request_headers.rs new file mode 100644 index 0000000000..5628d9d596 --- /dev/null +++ b/third_party/rust/headers/src/common/access_control_request_headers.rs @@ -0,0 +1,89 @@ +use std::iter::FromIterator; + +use util::FlatCsv; +use {HeaderName, HeaderValue}; + +/// `Access-Control-Request-Headers` header, part of +/// [CORS](http://www.w3.org/TR/cors/#access-control-request-headers-request-header) +/// +/// The `Access-Control-Request-Headers` header indicates which headers will +/// be used in the actual request as part of the preflight request. +/// during the actual request. +/// +/// # ABNF +/// +/// ```text +/// Access-Control-Allow-Headers: "Access-Control-Allow-Headers" ":" #field-name +/// ``` +/// +/// # Example values +/// * `accept-language, date` +/// +/// # Examples +/// +/// ``` +/// # extern crate headers; +/// extern crate http; +/// # fn main() { +/// use http::header::{ACCEPT_LANGUAGE, DATE}; +/// use headers::AccessControlRequestHeaders; +/// +/// let req_headers = vec![ACCEPT_LANGUAGE, DATE] +/// .into_iter() +/// .collect::<AccessControlRequestHeaders>(); +/// # } +/// ``` +#[derive(Clone, Debug)] +pub struct AccessControlRequestHeaders(FlatCsv); + +derive_header! { + AccessControlRequestHeaders(_), + name: ACCESS_CONTROL_REQUEST_HEADERS +} + +impl AccessControlRequestHeaders { + /// Returns an iterator over `HeaderName`s contained within. + pub fn iter<'a>(&'a self) -> impl Iterator<Item = HeaderName> + 'a { + self.0.iter().filter_map(|s| s.parse().ok()) + } +} + +impl FromIterator<HeaderName> for AccessControlRequestHeaders { + fn from_iter<I>(iter: I) -> Self + where + I: IntoIterator<Item = HeaderName>, + { + let flat = iter.into_iter().map(HeaderValue::from).collect(); + AccessControlRequestHeaders(flat) + } +} + +#[cfg(test)] +mod tests { + use super::super::{test_decode, test_encode}; + use super::*; + + #[test] + fn iter() { + let req_headers = test_decode::<AccessControlRequestHeaders>(&["foo, bar"]).unwrap(); + + let as_vec = req_headers.iter().collect::<Vec<_>>(); + assert_eq!(as_vec.len(), 2); + assert_eq!(as_vec[0], "foo"); + assert_eq!(as_vec[1], "bar"); + } + + #[test] + fn from_iter() { + let req_headers: AccessControlRequestHeaders = + vec![::http::header::CACHE_CONTROL, ::http::header::IF_RANGE] + .into_iter() + .collect(); + + let headers = test_encode(req_headers); + assert_eq!( + headers["access-control-request-headers"], + "cache-control, if-range" + ); + } +} diff --git a/third_party/rust/headers/src/common/access_control_request_method.rs b/third_party/rust/headers/src/common/access_control_request_method.rs new file mode 100644 index 0000000000..f922f0b5e0 --- /dev/null +++ b/third_party/rust/headers/src/common/access_control_request_method.rs @@ -0,0 +1,73 @@ +use http::Method; +use {Header, HeaderName, HeaderValue}; + +/// `Access-Control-Request-Method` header, part of +/// [CORS](http://www.w3.org/TR/cors/#access-control-request-method-request-header) +/// +/// The `Access-Control-Request-Method` header indicates which method will be +/// used in the actual request as part of the preflight request. +/// # ABNF +/// +/// ```text +/// Access-Control-Request-Method: \"Access-Control-Request-Method\" \":\" Method +/// ``` +/// +/// # Example values +/// * `GET` +/// +/// # Examples +/// +/// ``` +/// # extern crate headers; +/// extern crate http; +/// use headers::AccessControlRequestMethod; +/// use http::Method; +/// +/// let req_method = AccessControlRequestMethod::from(Method::GET); +/// ``` +#[derive(Clone, Debug, PartialEq, Eq, Hash)] +pub struct AccessControlRequestMethod(Method); + +impl Header for AccessControlRequestMethod { + fn name() -> &'static HeaderName { + &::http::header::ACCESS_CONTROL_REQUEST_METHOD + } + + fn decode<'i, I: Iterator<Item = &'i HeaderValue>>(values: &mut I) -> Result<Self, ::Error> { + values + .next() + .and_then(|value| Method::from_bytes(value.as_bytes()).ok()) + .map(AccessControlRequestMethod) + .ok_or_else(::Error::invalid) + } + + fn encode<E: Extend<::HeaderValue>>(&self, values: &mut E) { + // For the more common methods, try to use a static string. + let s = match self.0 { + Method::GET => "GET", + Method::POST => "POST", + Method::PUT => "PUT", + Method::DELETE => "DELETE", + _ => { + let val = HeaderValue::from_str(self.0.as_ref()) + .expect("Methods are also valid HeaderValues"); + values.extend(::std::iter::once(val)); + return; + } + }; + + values.extend(::std::iter::once(HeaderValue::from_static(s))); + } +} + +impl From<Method> for AccessControlRequestMethod { + fn from(method: Method) -> AccessControlRequestMethod { + AccessControlRequestMethod(method) + } +} + +impl From<AccessControlRequestMethod> for Method { + fn from(method: AccessControlRequestMethod) -> Method { + method.0 + } +} diff --git a/third_party/rust/headers/src/common/age.rs b/third_party/rust/headers/src/common/age.rs new file mode 100644 index 0000000000..11f1f6468c --- /dev/null +++ b/third_party/rust/headers/src/common/age.rs @@ -0,0 +1,69 @@ +use std::time::Duration; + +use util::Seconds; + +/// `Age` header, defined in [RFC7234](https://tools.ietf.org/html/rfc7234#section-5.1) +/// +/// The "Age" header field conveys the sender's estimate of the amount of +/// time since the response was generated or successfully validated at +/// the origin server. Age values are calculated as specified in +/// [Section 4.2.3](https://tools.ietf.org/html/rfc7234#section-4.2.3). +/// +/// ## ABNF +/// +/// ```text +/// Age = delta-seconds +/// ``` +/// +/// The Age field-value is a non-negative integer, representing time in +/// seconds (see [Section 1.2.1](https://tools.ietf.org/html/rfc7234#section-1.2.1)). +/// +/// The presence of an Age header field implies that the response was not +/// generated or validated by the origin server for this request. +/// However, lack of an Age header field does not imply the origin was +/// contacted, since the response might have been received from an +/// HTTP/1.0 cache that does not implement Age. +/// +/// ## Example values +/// +/// * `3600` +/// +/// # Example +/// +/// ``` +/// # extern crate headers; +/// use headers::Age; +/// +/// let len = Age::from_secs(60); +/// ``` +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct Age(Seconds); + +derive_header! { + Age(_), + name: AGE +} + +impl Age { + /// Creates a new `Age` header from the specified number of whole seconds. + pub fn from_secs(secs: u64) -> Self { + Self(Seconds::from_secs(secs)) + } + + /// Returns the number of seconds for this `Age` header. + pub fn as_secs(&self) -> u64 { + self.0.as_u64() + } +} + +impl From<Duration> for Age { + fn from(dur: Duration) -> Self { + Age(Seconds::from(dur)) + } +} + +impl From<Age> for Duration { + fn from(age: Age) -> Self { + age.0.into() + } +} diff --git a/third_party/rust/headers/src/common/allow.rs b/third_party/rust/headers/src/common/allow.rs new file mode 100644 index 0000000000..0285d3c780 --- /dev/null +++ b/third_party/rust/headers/src/common/allow.rs @@ -0,0 +1,68 @@ +use std::iter::FromIterator; + +use http::Method; + +use util::FlatCsv; + +/// `Allow` header, defined in [RFC7231](http://tools.ietf.org/html/rfc7231#section-7.4.1) +/// +/// The `Allow` header field lists the set of methods advertised as +/// supported by the target resource. The purpose of this field is +/// strictly to inform the recipient of valid request methods associated +/// with the resource. +/// +/// # ABNF +/// +/// ```text +/// Allow = #method +/// ``` +/// +/// # Example values +/// * `GET, HEAD, PUT` +/// * `OPTIONS, GET, PUT, POST, DELETE, HEAD, TRACE, CONNECT, PATCH, fOObAr` +/// * `` +/// +/// # Examples +/// +/// ``` +/// # extern crate headers; +/// extern crate http; +/// use headers::Allow; +/// use http::Method; +/// +/// let allow = vec![Method::GET, Method::POST] +/// .into_iter() +/// .collect::<Allow>(); +/// ``` +#[derive(Clone, Debug, PartialEq)] +pub struct Allow(FlatCsv); + +derive_header! { + Allow(_), + name: ALLOW +} + +impl Allow { + /// Returns an iterator over `Method`s contained within. + pub fn iter<'a>(&'a self) -> impl Iterator<Item = Method> + 'a { + self.0.iter().filter_map(|s| s.parse().ok()) + } +} + +impl FromIterator<Method> for Allow { + fn from_iter<I>(iter: I) -> Self + where + I: IntoIterator<Item = Method>, + { + let flat = iter + .into_iter() + .map(|method| { + method + .as_str() + .parse::<::HeaderValue>() + .expect("Method is a valid HeaderValue") + }) + .collect(); + Allow(flat) + } +} diff --git a/third_party/rust/headers/src/common/authorization.rs b/third_party/rust/headers/src/common/authorization.rs new file mode 100644 index 0000000000..dbef15301a --- /dev/null +++ b/third_party/rust/headers/src/common/authorization.rs @@ -0,0 +1,276 @@ +//! Authorization header and types. + +use base64; +use bytes::Bytes; + +use util::HeaderValueString; +use HeaderValue; + +/// `Authorization` header, defined in [RFC7235](https://tools.ietf.org/html/rfc7235#section-4.2) +/// +/// The `Authorization` header field allows a user agent to authenticate +/// itself with an origin server -- usually, but not necessarily, after +/// receiving a 401 (Unauthorized) response. Its value consists of +/// credentials containing the authentication information of the user +/// agent for the realm of the resource being requested. +/// +/// # ABNF +/// +/// ```text +/// Authorization = credentials +/// ``` +/// +/// # Example values +/// * `Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==` +/// * `Bearer fpKL54jvWmEGVoRdCNjG` +/// +/// # Examples +/// +/// ``` +/// # extern crate headers; +/// use headers::Authorization; +/// +/// let basic = Authorization::basic("Aladdin", "open sesame"); +/// let bearer = Authorization::bearer("some-opaque-token").unwrap(); +/// ``` +/// +#[derive(Clone, PartialEq, Debug)] +pub struct Authorization<C: Credentials>(pub C); + +impl Authorization<Basic> { + /// Create a `Basic` authorization header. + pub fn basic(username: &str, password: &str) -> Self { + let colon_pos = username.len(); + let decoded = format!("{}:{}", username, password); + + Authorization(Basic { decoded, colon_pos }) + } + + /// View the decoded username. + pub fn username(&self) -> &str { + self.0.username() + } + + /// View the decoded password. + pub fn password(&self) -> &str { + self.0.password() + } +} + +impl Authorization<Bearer> { + /// Try to create a `Bearer` authorization header. + pub fn bearer(token: &str) -> Result<Self, InvalidBearerToken> { + HeaderValueString::from_string(format!("Bearer {}", token)) + .map(|val| Authorization(Bearer(val))) + .ok_or_else(|| InvalidBearerToken { _inner: () }) + } + + /// View the token part as a `&str`. + pub fn token(&self) -> &str { + self.0.token() + } +} + +impl<C: Credentials> ::Header for Authorization<C> { + fn name() -> &'static ::HeaderName { + &::http::header::AUTHORIZATION + } + + fn decode<'i, I: Iterator<Item = &'i HeaderValue>>(values: &mut I) -> Result<Self, ::Error> { + values + .next() + .and_then(|val| { + let slice = val.as_bytes(); + if slice.starts_with(C::SCHEME.as_bytes()) + && slice.len() > C::SCHEME.len() + && slice[C::SCHEME.len()] == b' ' + { + C::decode(val).map(Authorization) + } else { + None + } + }) + .ok_or_else(::Error::invalid) + } + + fn encode<E: Extend<::HeaderValue>>(&self, values: &mut E) { + let value = self.0.encode(); + debug_assert!( + value.as_bytes().starts_with(C::SCHEME.as_bytes()), + "Credentials::encode should include its scheme: scheme = {:?}, encoded = {:?}", + C::SCHEME, + value, + ); + + values.extend(::std::iter::once(value)); + } +} + +/// Credentials to be used in the `Authorization` header. +pub trait Credentials: Sized { + /// The scheme identify the format of these credentials. + /// + /// This is the static string that always prefixes the actual credentials, + /// like `"Basic"` in basic authorization. + const SCHEME: &'static str; + + /// Try to decode the credentials from the `HeaderValue`. + /// + /// The `SCHEME` will be the first part of the `value`. + fn decode(value: &HeaderValue) -> Option<Self>; + + /// Encode the credentials to a `HeaderValue`. + /// + /// The `SCHEME` must be the first part of the `value`. + fn encode(&self) -> HeaderValue; +} + +/// Credential holder for Basic Authentication +#[derive(Clone, PartialEq, Debug)] +pub struct Basic { + decoded: String, + colon_pos: usize, +} + +impl Basic { + /// View the decoded username. + pub fn username(&self) -> &str { + &self.decoded[..self.colon_pos] + } + + /// View the decoded password. + pub fn password(&self) -> &str { + &self.decoded[self.colon_pos + 1..] + } +} + +impl Credentials for Basic { + const SCHEME: &'static str = "Basic"; + + fn decode(value: &HeaderValue) -> Option<Self> { + debug_assert!( + value.as_bytes().starts_with(b"Basic "), + "HeaderValue to decode should start with \"Basic ..\", received = {:?}", + value, + ); + + let bytes = &value.as_bytes()["Basic ".len()..]; + let non_space_pos = bytes.iter().position(|b| *b != b' ')?; + let bytes = &bytes[non_space_pos..]; + let bytes = base64::decode(bytes).ok()?; + + let decoded = String::from_utf8(bytes).ok()?; + + let colon_pos = decoded.find(':')?; + + Some(Basic { decoded, colon_pos }) + } + + fn encode(&self) -> HeaderValue { + let mut encoded = String::from("Basic "); + base64::encode_config_buf(&self.decoded, base64::STANDARD, &mut encoded); + + let bytes = Bytes::from(encoded); + HeaderValue::from_maybe_shared(bytes).expect("base64 encoding is always a valid HeaderValue") + } +} + +#[derive(Clone, PartialEq, Debug)] +/// Token holder for Bearer Authentication, most often seen with oauth +pub struct Bearer(HeaderValueString); + +impl Bearer { + /// View the token part as a `&str`. + pub fn token(&self) -> &str { + &self.0.as_str()["Bearer ".len()..] + } +} + +impl Credentials for Bearer { + const SCHEME: &'static str = "Bearer"; + + fn decode(value: &HeaderValue) -> Option<Self> { + debug_assert!( + value.as_bytes().starts_with(b"Bearer "), + "HeaderValue to decode should start with \"Bearer ..\", received = {:?}", + value, + ); + + HeaderValueString::from_val(value).ok().map(Bearer) + } + + fn encode(&self) -> HeaderValue { + (&self.0).into() + } +} + +error_type!(InvalidBearerToken); + +#[cfg(test)] +mod tests { + use super::super::{test_decode, test_encode}; + use super::{Authorization, Basic, Bearer}; + use http::header::HeaderMap; + use HeaderMapExt; + + #[test] + fn basic_encode() { + let auth = Authorization::basic("Aladdin", "open sesame"); + let headers = test_encode(auth); + + assert_eq!( + headers["authorization"], + "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==", + ); + } + + #[test] + fn basic_roundtrip() { + let auth = Authorization::basic("Aladdin", "open sesame"); + let mut h = HeaderMap::new(); + h.typed_insert(auth.clone()); + assert_eq!(h.typed_get(), Some(auth)); + } + + #[test] + fn basic_encode_no_password() { + let auth = Authorization::basic("Aladdin", ""); + let headers = test_encode(auth); + + assert_eq!(headers["authorization"], "Basic QWxhZGRpbjo=",); + } + + #[test] + fn basic_decode() { + let auth: Authorization<Basic> = + test_decode(&["Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="]).unwrap(); + assert_eq!(auth.0.username(), "Aladdin"); + assert_eq!(auth.0.password(), "open sesame"); + } + + #[test] + fn basic_decode_no_password() { + let auth: Authorization<Basic> = test_decode(&["Basic QWxhZGRpbjo="]).unwrap(); + assert_eq!(auth.0.username(), "Aladdin"); + assert_eq!(auth.0.password(), ""); + } + + #[test] + fn bearer_encode() { + let auth = Authorization::bearer("fpKL54jvWmEGVoRdCNjG").unwrap(); + + let headers = test_encode(auth); + + assert_eq!(headers["authorization"], "Bearer fpKL54jvWmEGVoRdCNjG",); + } + + #[test] + fn bearer_decode() { + let auth: Authorization<Bearer> = test_decode(&["Bearer fpKL54jvWmEGVoRdCNjG"]).unwrap(); + assert_eq!(auth.0.token().as_bytes(), b"fpKL54jvWmEGVoRdCNjG"); + } +} + +//bench_header!(raw, Authorization<String>, { vec![b"foo bar baz".to_vec()] }); +//bench_header!(basic, Authorization<Basic>, { vec![b"Basic QWxhZGRpbjpuIHNlc2FtZQ==".to_vec()] }); +//bench_header!(bearer, Authorization<Bearer>, { vec![b"Bearer fpKL54jvWmEGVoRdCNjG".to_vec()] }); diff --git a/third_party/rust/headers/src/common/cache_control.rs b/third_party/rust/headers/src/common/cache_control.rs new file mode 100644 index 0000000000..305361d35b --- /dev/null +++ b/third_party/rust/headers/src/common/cache_control.rs @@ -0,0 +1,464 @@ +use std::fmt; +use std::iter::FromIterator; +use std::str::FromStr; +use std::time::Duration; + +use util::{self, csv, Seconds}; +use HeaderValue; + +/// `Cache-Control` header, defined in [RFC7234](https://tools.ietf.org/html/rfc7234#section-5.2) +/// +/// The `Cache-Control` header field is used to specify directives for +/// caches along the request/response chain. Such cache directives are +/// unidirectional in that the presence of a directive in a request does +/// not imply that the same directive is to be given in the response. +/// +/// ## ABNF +/// +/// ```text +/// Cache-Control = 1#cache-directive +/// cache-directive = token [ "=" ( token / quoted-string ) ] +/// ``` +/// +/// ## Example values +/// +/// * `no-cache` +/// * `private, community="UCI"` +/// * `max-age=30` +/// +/// # Example +/// +/// ``` +/// # extern crate headers; +/// use headers::CacheControl; +/// +/// let cc = CacheControl::new(); +/// ``` +#[derive(PartialEq, Clone, Debug)] +pub struct CacheControl { + flags: Flags, + max_age: Option<Seconds>, + max_stale: Option<Seconds>, + min_fresh: Option<Seconds>, + s_max_age: Option<Seconds>, +} + +bitflags! { + struct Flags: u32 { + const NO_CACHE = 0b00000001; + const NO_STORE = 0b00000010; + const NO_TRANSFORM = 0b00000100; + const ONLY_IF_CACHED = 0b00001000; + const MUST_REVALIDATE = 0b00010000; + const PUBLIC = 0b00100000; + const PRIVATE = 0b01000000; + const PROXY_REVALIDATE = 0b10000000; + } +} + +impl CacheControl { + /// Construct a new empty `CacheControl` header. + pub fn new() -> Self { + CacheControl { + flags: Flags::empty(), + max_age: None, + max_stale: None, + min_fresh: None, + s_max_age: None, + } + } + + // getters + + /// Check if the `no-cache` directive is set. + pub fn no_cache(&self) -> bool { + self.flags.contains(Flags::NO_CACHE) + } + + /// Check if the `no-store` directive is set. + pub fn no_store(&self) -> bool { + self.flags.contains(Flags::NO_STORE) + } + + /// Check if the `no-transform` directive is set. + pub fn no_transform(&self) -> bool { + self.flags.contains(Flags::NO_TRANSFORM) + } + + /// Check if the `only-if-cached` directive is set. + pub fn only_if_cached(&self) -> bool { + self.flags.contains(Flags::ONLY_IF_CACHED) + } + + /// Check if the `public` directive is set. + pub fn public(&self) -> bool { + self.flags.contains(Flags::PUBLIC) + } + + /// Check if the `private` directive is set. + pub fn private(&self) -> bool { + self.flags.contains(Flags::PRIVATE) + } + + /// Get the value of the `max-age` directive if set. + pub fn max_age(&self) -> Option<Duration> { + self.max_age.map(Into::into) + } + + /// Get the value of the `max-stale` directive if set. + pub fn max_stale(&self) -> Option<Duration> { + self.max_stale.map(Into::into) + } + + /// Get the value of the `min-fresh` directive if set. + pub fn min_fresh(&self) -> Option<Duration> { + self.min_fresh.map(Into::into) + } + + /// Get the value of the `s-maxage` directive if set. + pub fn s_max_age(&self) -> Option<Duration> { + self.s_max_age.map(Into::into) + } + + // setters + + /// Set the `no-cache` directive. + pub fn with_no_cache(mut self) -> Self { + self.flags.insert(Flags::NO_CACHE); + self + } + + /// Set the `no-store` directive. + pub fn with_no_store(mut self) -> Self { + self.flags.insert(Flags::NO_STORE); + self + } + + /// Set the `no-transform` directive. + pub fn with_no_transform(mut self) -> Self { + self.flags.insert(Flags::NO_TRANSFORM); + self + } + + /// Set the `only-if-cached` directive. + pub fn with_only_if_cached(mut self) -> Self { + self.flags.insert(Flags::ONLY_IF_CACHED); + self + } + + /// Set the `private` directive. + pub fn with_private(mut self) -> Self { + self.flags.insert(Flags::PRIVATE); + self + } + + /// Set the `public` directive. + pub fn with_public(mut self) -> Self { + self.flags.insert(Flags::PUBLIC); + self + } + + /// Set the `max-age` directive. + pub fn with_max_age(mut self, seconds: Duration) -> Self { + self.max_age = Some(seconds.into()); + self + } + + /// Set the `max-stale` directive. + pub fn with_max_stale(mut self, seconds: Duration) -> Self { + self.max_stale = Some(seconds.into()); + self + } + + /// Set the `min-fresh` directive. + pub fn with_min_fresh(mut self, seconds: Duration) -> Self { + self.min_fresh = Some(seconds.into()); + self + } + + /// Set the `s-maxage` directive. + pub fn with_s_max_age(mut self, seconds: Duration) -> Self { + self.s_max_age = Some(seconds.into()); + self + } +} + +impl ::Header for CacheControl { + fn name() -> &'static ::HeaderName { + &::http::header::CACHE_CONTROL + } + + fn decode<'i, I: Iterator<Item = &'i HeaderValue>>(values: &mut I) -> Result<Self, ::Error> { + csv::from_comma_delimited(values).map(|FromIter(cc)| cc) + } + + fn encode<E: Extend<::HeaderValue>>(&self, values: &mut E) { + values.extend(::std::iter::once(util::fmt(Fmt(self)))); + } +} + +// Adapter to be used in Header::decode +struct FromIter(CacheControl); + +impl FromIterator<KnownDirective> for FromIter { + fn from_iter<I>(iter: I) -> Self + where + I: IntoIterator<Item = KnownDirective>, + { + let mut cc = CacheControl::new(); + + // ignore all unknown directives + let iter = iter.into_iter().filter_map(|dir| match dir { + KnownDirective::Known(dir) => Some(dir), + KnownDirective::Unknown => None, + }); + + for directive in iter { + match directive { + Directive::NoCache => { + cc.flags.insert(Flags::NO_CACHE); + } + Directive::NoStore => { + cc.flags.insert(Flags::NO_STORE); + } + Directive::NoTransform => { + cc.flags.insert(Flags::NO_TRANSFORM); + } + Directive::OnlyIfCached => { + cc.flags.insert(Flags::ONLY_IF_CACHED); + } + Directive::MustRevalidate => { + cc.flags.insert(Flags::MUST_REVALIDATE); + } + Directive::Public => { + cc.flags.insert(Flags::PUBLIC); + } + Directive::Private => { + cc.flags.insert(Flags::PRIVATE); + } + Directive::ProxyRevalidate => { + cc.flags.insert(Flags::PROXY_REVALIDATE); + } + Directive::MaxAge(secs) => { + cc.max_age = Some(Duration::from_secs(secs.into()).into()); + } + Directive::MaxStale(secs) => { + cc.max_stale = Some(Duration::from_secs(secs.into()).into()); + } + Directive::MinFresh(secs) => { + cc.min_fresh = Some(Duration::from_secs(secs.into()).into()); + } + Directive::SMaxAge(secs) => { + cc.s_max_age = Some(Duration::from_secs(secs.into()).into()); + } + } + } + + FromIter(cc) + } +} + +struct Fmt<'a>(&'a CacheControl); + +impl<'a> fmt::Display for Fmt<'a> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let if_flag = |f: Flags, dir: Directive| { + if self.0.flags.contains(f) { + Some(dir) + } else { + None + } + }; + + let slice = &[ + if_flag(Flags::NO_CACHE, Directive::NoCache), + if_flag(Flags::NO_STORE, Directive::NoStore), + if_flag(Flags::NO_TRANSFORM, Directive::NoTransform), + if_flag(Flags::ONLY_IF_CACHED, Directive::OnlyIfCached), + if_flag(Flags::MUST_REVALIDATE, Directive::MustRevalidate), + if_flag(Flags::PUBLIC, Directive::Public), + if_flag(Flags::PRIVATE, Directive::Private), + if_flag(Flags::PROXY_REVALIDATE, Directive::ProxyRevalidate), + self.0 + .max_age + .as_ref() + .map(|s| Directive::MaxAge(s.as_u64())), + self.0 + .max_stale + .as_ref() + .map(|s| Directive::MaxStale(s.as_u64())), + self.0 + .min_fresh + .as_ref() + .map(|s| Directive::MinFresh(s.as_u64())), + self.0 + .s_max_age + .as_ref() + .map(|s| Directive::SMaxAge(s.as_u64())), + ]; + + let iter = slice.iter().filter_map(|o| *o); + + csv::fmt_comma_delimited(f, iter) + } +} + +#[derive(Clone, Copy)] +enum KnownDirective { + Known(Directive), + Unknown, +} + +#[derive(Clone, Copy)] +enum Directive { + NoCache, + NoStore, + NoTransform, + OnlyIfCached, + + // request directives + MaxAge(u64), + MaxStale(u64), + MinFresh(u64), + + // response directives + MustRevalidate, + Public, + Private, + ProxyRevalidate, + SMaxAge(u64), +} + +impl fmt::Display for Directive { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fmt::Display::fmt( + match *self { + Directive::NoCache => "no-cache", + Directive::NoStore => "no-store", + Directive::NoTransform => "no-transform", + Directive::OnlyIfCached => "only-if-cached", + + Directive::MaxAge(secs) => return write!(f, "max-age={}", secs), + Directive::MaxStale(secs) => return write!(f, "max-stale={}", secs), + Directive::MinFresh(secs) => return write!(f, "min-fresh={}", secs), + + Directive::MustRevalidate => "must-revalidate", + Directive::Public => "public", + Directive::Private => "private", + Directive::ProxyRevalidate => "proxy-revalidate", + Directive::SMaxAge(secs) => return write!(f, "s-maxage={}", secs), + }, + f, + ) + } +} + +impl FromStr for KnownDirective { + type Err = (); + fn from_str(s: &str) -> Result<Self, Self::Err> { + Ok(KnownDirective::Known(match s { + "no-cache" => Directive::NoCache, + "no-store" => Directive::NoStore, + "no-transform" => Directive::NoTransform, + "only-if-cached" => Directive::OnlyIfCached, + "must-revalidate" => Directive::MustRevalidate, + "public" => Directive::Public, + "private" => Directive::Private, + "proxy-revalidate" => Directive::ProxyRevalidate, + "" => return Err(()), + _ => match s.find('=') { + Some(idx) if idx + 1 < s.len() => { + match (&s[..idx], (&s[idx + 1..]).trim_matches('"')) { + ("max-age", secs) => secs.parse().map(Directive::MaxAge).map_err(|_| ())?, + ("max-stale", secs) => { + secs.parse().map(Directive::MaxStale).map_err(|_| ())? + } + ("min-fresh", secs) => { + secs.parse().map(Directive::MinFresh).map_err(|_| ())? + } + ("s-maxage", secs) => { + secs.parse().map(Directive::SMaxAge).map_err(|_| ())? + } + _unknown => return Ok(KnownDirective::Unknown), + } + } + Some(_) | None => return Ok(KnownDirective::Unknown), + }, + })) + } +} + +#[cfg(test)] +mod tests { + use super::super::{test_decode, test_encode}; + use super::*; + + #[test] + fn test_parse_multiple_headers() { + assert_eq!( + test_decode::<CacheControl>(&["no-cache", "private"]).unwrap(), + CacheControl::new().with_no_cache().with_private(), + ); + } + + #[test] + fn test_parse_argument() { + assert_eq!( + test_decode::<CacheControl>(&["max-age=100, private"]).unwrap(), + CacheControl::new() + .with_max_age(Duration::from_secs(100)) + .with_private(), + ); + } + + #[test] + fn test_parse_quote_form() { + assert_eq!( + test_decode::<CacheControl>(&["max-age=\"200\""]).unwrap(), + CacheControl::new().with_max_age(Duration::from_secs(200)), + ); + } + + #[test] + fn test_parse_extension() { + assert_eq!( + test_decode::<CacheControl>(&["foo, no-cache, bar=baz"]).unwrap(), + CacheControl::new().with_no_cache(), + "unknown extensions are ignored but shouldn't fail parsing", + ); + } + + #[test] + fn test_parse_bad_syntax() { + assert_eq!(test_decode::<CacheControl>(&["max-age=lolz"]), None,); + } + + #[test] + fn encode_one_flag_directive() { + let cc = CacheControl::new().with_no_cache(); + + let headers = test_encode(cc); + assert_eq!(headers["cache-control"], "no-cache"); + } + + #[test] + fn encode_one_param_directive() { + let cc = CacheControl::new().with_max_age(Duration::from_secs(300)); + + let headers = test_encode(cc); + assert_eq!(headers["cache-control"], "max-age=300"); + } + + #[test] + fn encode_two_directive() { + let headers = test_encode(CacheControl::new().with_no_cache().with_private()); + assert_eq!(headers["cache-control"], "no-cache, private"); + + let headers = test_encode( + CacheControl::new() + .with_no_cache() + .with_max_age(Duration::from_secs(100)), + ); + assert_eq!(headers["cache-control"], "no-cache, max-age=100"); + } +} diff --git a/third_party/rust/headers/src/common/connection.rs b/third_party/rust/headers/src/common/connection.rs new file mode 100644 index 0000000000..00299e4702 --- /dev/null +++ b/third_party/rust/headers/src/common/connection.rs @@ -0,0 +1,136 @@ +use std::iter::FromIterator; + +use self::sealed::AsConnectionOption; +use util::FlatCsv; +use {HeaderName, HeaderValue}; + +/// `Connection` header, defined in +/// [RFC7230](http://tools.ietf.org/html/rfc7230#section-6.1) +/// +/// The `Connection` header field allows the sender to indicate desired +/// control options for the current connection. In order to avoid +/// confusing downstream recipients, a proxy or gateway MUST remove or +/// replace any received connection options before forwarding the +/// message. +/// +/// # ABNF +/// +/// ```text +/// Connection = 1#connection-option +/// connection-option = token +/// +/// # Example values +/// * `close` +/// * `keep-alive` +/// * `upgrade` +/// ``` +/// +/// # Examples +/// +/// ``` +/// # extern crate headers; +/// use headers::Connection; +/// +/// let keep_alive = Connection::keep_alive(); +/// ``` +// This is frequently just 1 or 2 values, so optimize for that case. +#[derive(Clone, Debug)] +pub struct Connection(FlatCsv); + +derive_header! { + Connection(_), + name: CONNECTION +} + +impl Connection { + /// A constructor to easily create a `Connection: close` header. + #[inline] + pub fn close() -> Connection { + Connection(HeaderValue::from_static("close").into()) + } + + /// A constructor to easily create a `Connection: keep-alive` header. + #[inline] + pub fn keep_alive() -> Connection { + Connection(HeaderValue::from_static("keep-alive").into()) + } + + /// A constructor to easily create a `Connection: Upgrade` header. + #[inline] + pub fn upgrade() -> Connection { + Connection(HeaderValue::from_static("upgrade").into()) + } + + /// Check if this header contains a given "connection option". + /// + /// This can be used with various argument types: + /// + /// - `&str` + /// - `&HeaderName` + /// - `HeaderName` + /// + /// # Example + /// + /// ``` + /// # extern crate headers; + /// extern crate http; + /// + /// use http::header::UPGRADE; + /// use headers::Connection; + /// + /// let conn = Connection::keep_alive(); + /// + /// assert!(!conn.contains("close")); + /// assert!(!conn.contains(UPGRADE)); + /// assert!(conn.contains("keep-alive")); + /// assert!(conn.contains("Keep-Alive")); + /// ``` + pub fn contains(&self, name: impl AsConnectionOption) -> bool { + let s = name.as_connection_option(); + self.0 + .iter() + .find(|&opt| opt.eq_ignore_ascii_case(s)) + .is_some() + } +} + +impl FromIterator<HeaderName> for Connection { + fn from_iter<I>(iter: I) -> Self + where + I: IntoIterator<Item = HeaderName>, + { + let flat = iter.into_iter().map(HeaderValue::from).collect(); + Connection(flat) + } +} + +mod sealed { + pub trait AsConnectionOption: Sealed { + fn as_connection_option(&self) -> &str; + } + pub trait Sealed {} + + impl<'a> AsConnectionOption for &'a str { + fn as_connection_option(&self) -> &str { + *self + } + } + + impl<'a> Sealed for &'a str {} + + impl<'a> AsConnectionOption for &'a ::HeaderName { + fn as_connection_option(&self) -> &str { + self.as_ref() + } + } + + impl<'a> Sealed for &'a ::HeaderName {} + + impl AsConnectionOption for ::HeaderName { + fn as_connection_option(&self) -> &str { + self.as_ref() + } + } + + impl Sealed for ::HeaderName {} +} diff --git a/third_party/rust/headers/src/common/content_disposition.rs b/third_party/rust/headers/src/common/content_disposition.rs new file mode 100644 index 0000000000..5c1ea0f745 --- /dev/null +++ b/third_party/rust/headers/src/common/content_disposition.rs @@ -0,0 +1,323 @@ +// # References +// +// "The Content-Disposition Header Field" https://www.ietf.org/rfc/rfc2183.txt +// "The Content-Disposition Header Field in the Hypertext Transfer Protocol (HTTP)" https://www.ietf.org/rfc/rfc6266.txt +// "Returning Values from Forms: multipart/form-data" https://www.ietf.org/rfc/rfc2388.txt +// Browser conformance tests at: http://greenbytes.de/tech/tc2231/ +// IANA assignment: http://www.iana.org/assignments/cont-disp/cont-disp.xhtml + +/// A `Content-Disposition` header, (re)defined in [RFC6266](https://tools.ietf.org/html/rfc6266). +/// +/// The Content-Disposition response header field is used to convey +/// additional information about how to process the response payload, and +/// also can be used to attach additional metadata, such as the filename +/// to use when saving the response payload locally. +/// +/// # ABNF + +/// ```text +/// content-disposition = "Content-Disposition" ":" +/// disposition-type *( ";" disposition-parm ) +/// +/// disposition-type = "inline" | "attachment" | disp-ext-type +/// ; case-insensitive +/// +/// disp-ext-type = token +/// +/// disposition-parm = filename-parm | disp-ext-parm +/// +/// filename-parm = "filename" "=" value +/// | "filename*" "=" ext-value +/// +/// disp-ext-parm = token "=" value +/// | ext-token "=" ext-value +/// +/// ext-token = <the characters in token, followed by "*"> +/// ``` +/// +/// # Example +/// +/// ``` +/// # extern crate headers; +/// use headers::ContentDisposition; +/// +/// let cd = ContentDisposition::inline(); +/// ``` +#[derive(Clone, Debug)] +pub struct ContentDisposition(::HeaderValue); + +impl ContentDisposition { + /// Construct a `Content-Disposition: inline` header. + pub fn inline() -> ContentDisposition { + ContentDisposition(::HeaderValue::from_static("inline")) + } + + /* + pub fn attachment(filename: &str) -> ContentDisposition { + let full = Bytes::from(format!("attachment; filename={}", filename)); + match ::HeaderValue::from_maybe_shared(full) { + Ok(val) => ContentDisposition(val), + Err(_) => { + unimplemented!("filename that isn't ASCII"); + } + } + } + */ + + /// Check if the disposition-type is `inline`. + pub fn is_inline(&self) -> bool { + self.get_type() == "inline" + } + + /// Check if the disposition-type is `attachment`. + pub fn is_attachment(&self) -> bool { + self.get_type() == "attachment" + } + + /// Check if the disposition-type is `form-data`. + pub fn is_form_data(&self) -> bool { + self.get_type() == "form-data" + } + + fn get_type(&self) -> &str { + self.0 + .to_str() + .unwrap_or("") + .split(';') + .next() + .expect("split always has at least 1 item") + } +} + +impl ::Header for ContentDisposition { + fn name() -> &'static ::HeaderName { + &::http::header::CONTENT_DISPOSITION + } + + fn decode<'i, I: Iterator<Item = &'i ::HeaderValue>>(values: &mut I) -> Result<Self, ::Error> { + //TODO: parse harder + values + .next() + .cloned() + .map(ContentDisposition) + .ok_or_else(::Error::invalid) + } + + fn encode<E: Extend<::HeaderValue>>(&self, values: &mut E) { + values.extend(::std::iter::once(self.0.clone())); + } +} +/* +use language_tags::LanguageTag; +use std::fmt; +use unicase; + +use {Header, Raw, parsing}; +use parsing::{parse_extended_value, http_percent_encode}; +use shared::Charset; + +/// The implied disposition of the content of the HTTP body. +#[derive(Clone, Debug, PartialEq)] +pub enum DispositionType { + /// Inline implies default processing + Inline, + /// Attachment implies that the recipient should prompt the user to save the response locally, + /// rather than process it normally (as per its media type). + Attachment, + /// Extension type. Should be handled by recipients the same way as Attachment + Ext(String) +} + +/// A parameter to the disposition type. +#[derive(Clone, Debug, PartialEq)] +pub enum DispositionParam { + /// A Filename consisting of a Charset, an optional LanguageTag, and finally a sequence of + /// bytes representing the filename + Filename(Charset, Option<LanguageTag>, Vec<u8>), + /// Extension type consisting of token and value. Recipients should ignore unrecognized + /// parameters. + Ext(String, String) +} + +#[derive(Clone, Debug, PartialEq)] +pub struct ContentDisposition { + /// The disposition + pub disposition: DispositionType, + /// Disposition parameters + pub parameters: Vec<DispositionParam>, +} + +impl Header for ContentDisposition { + fn header_name() -> &'static str { + static NAME: &'static str = "Content-Disposition"; + NAME + } + + fn parse_header(raw: &Raw) -> ::Result<ContentDisposition> { + parsing::from_one_raw_str(raw).and_then(|s: String| { + let mut sections = s.split(';'); + let disposition = match sections.next() { + Some(s) => s.trim(), + None => return Err(::Error::Header), + }; + + let mut cd = ContentDisposition { + disposition: if unicase::eq_ascii(&*disposition, "inline") { + DispositionType::Inline + } else if unicase::eq_ascii(&*disposition, "attachment") { + DispositionType::Attachment + } else { + DispositionType::Ext(disposition.to_owned()) + }, + parameters: Vec::new(), + }; + + for section in sections { + let mut parts = section.splitn(2, '='); + + let key = if let Some(key) = parts.next() { + key.trim() + } else { + return Err(::Error::Header); + }; + + let val = if let Some(val) = parts.next() { + val.trim() + } else { + return Err(::Error::Header); + }; + + cd.parameters.push( + if unicase::eq_ascii(&*key, "filename") { + DispositionParam::Filename( + Charset::Ext("UTF-8".to_owned()), None, + val.trim_matches('"').as_bytes().to_owned()) + } else if unicase::eq_ascii(&*key, "filename*") { + let extended_value = try!(parse_extended_value(val)); + DispositionParam::Filename(extended_value.charset, extended_value.language_tag, extended_value.value) + } else { + DispositionParam::Ext(key.to_owned(), val.trim_matches('"').to_owned()) + } + ); + } + + Ok(cd) + }) + } + + #[inline] + fn fmt_header(&self, f: &mut ::Formatter) -> fmt::Result { + f.fmt_line(self) + } +} + +impl fmt::Display for ContentDisposition { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self.disposition { + DispositionType::Inline => try!(write!(f, "inline")), + DispositionType::Attachment => try!(write!(f, "attachment")), + DispositionType::Ext(ref s) => try!(write!(f, "{}", s)), + } + for param in &self.parameters { + match *param { + DispositionParam::Filename(ref charset, ref opt_lang, ref bytes) => { + let mut use_simple_format: bool = false; + if opt_lang.is_none() { + if let Charset::Ext(ref ext) = *charset { + if unicase::eq_ascii(&**ext, "utf-8") { + use_simple_format = true; + } + } + } + if use_simple_format { + try!(write!(f, "; filename=\"{}\"", + match String::from_utf8(bytes.clone()) { + Ok(s) => s, + Err(_) => return Err(fmt::Error), + })); + } else { + try!(write!(f, "; filename*={}'", charset)); + if let Some(ref lang) = *opt_lang { + try!(write!(f, "{}", lang)); + }; + try!(write!(f, "'")); + try!(http_percent_encode(f, bytes)) + } + }, + DispositionParam::Ext(ref k, ref v) => try!(write!(f, "; {}=\"{}\"", k, v)), + } + } + Ok(()) + } +} + +#[cfg(test)] +mod tests { + use super::{ContentDisposition,DispositionType,DispositionParam}; + use ::Header; + use ::shared::Charset; + + #[test] + fn test_parse_header() { + assert!(ContentDisposition::parse_header(&"".into()).is_err()); + + let a = "form-data; dummy=3; name=upload;\r\n filename=\"sample.png\"".into(); + let a: ContentDisposition = ContentDisposition::parse_header(&a).unwrap(); + let b = ContentDisposition { + disposition: DispositionType::Ext("form-data".to_owned()), + parameters: vec![ + DispositionParam::Ext("dummy".to_owned(), "3".to_owned()), + DispositionParam::Ext("name".to_owned(), "upload".to_owned()), + DispositionParam::Filename( + Charset::Ext("UTF-8".to_owned()), + None, + "sample.png".bytes().collect()) ] + }; + assert_eq!(a, b); + + let a = "attachment; filename=\"image.jpg\"".into(); + let a: ContentDisposition = ContentDisposition::parse_header(&a).unwrap(); + let b = ContentDisposition { + disposition: DispositionType::Attachment, + parameters: vec![ + DispositionParam::Filename( + Charset::Ext("UTF-8".to_owned()), + None, + "image.jpg".bytes().collect()) ] + }; + assert_eq!(a, b); + + let a = "attachment; filename*=UTF-8''%c2%a3%20and%20%e2%82%ac%20rates".into(); + let a: ContentDisposition = ContentDisposition::parse_header(&a).unwrap(); + let b = ContentDisposition { + disposition: DispositionType::Attachment, + parameters: vec![ + DispositionParam::Filename( + Charset::Ext("UTF-8".to_owned()), + None, + vec![0xc2, 0xa3, 0x20, b'a', b'n', b'd', 0x20, + 0xe2, 0x82, 0xac, 0x20, b'r', b'a', b't', b'e', b's']) ] + }; + assert_eq!(a, b); + } + + #[test] + fn test_display() { + let as_string = "attachment; filename*=UTF-8'en'%C2%A3%20and%20%E2%82%AC%20rates"; + let a = as_string.into(); + let a: ContentDisposition = ContentDisposition::parse_header(&a).unwrap(); + let display_rendered = format!("{}",a); + assert_eq!(as_string, display_rendered); + + let a = "attachment; filename*=UTF-8''black%20and%20white.csv".into(); + let a: ContentDisposition = ContentDisposition::parse_header(&a).unwrap(); + let display_rendered = format!("{}",a); + assert_eq!("attachment; filename=\"black and white.csv\"".to_owned(), display_rendered); + + let a = "attachment; filename=colourful.csv".into(); + let a: ContentDisposition = ContentDisposition::parse_header(&a).unwrap(); + let display_rendered = format!("{}",a); + assert_eq!("attachment; filename=\"colourful.csv\"".to_owned(), display_rendered); + } +} +*/ diff --git a/third_party/rust/headers/src/common/content_encoding.rs b/third_party/rust/headers/src/common/content_encoding.rs new file mode 100644 index 0000000000..444eb41c0b --- /dev/null +++ b/third_party/rust/headers/src/common/content_encoding.rs @@ -0,0 +1,86 @@ +use self::sealed::AsCoding; +use util::FlatCsv; +use HeaderValue; + +/// `Content-Encoding` header, defined in +/// [RFC7231](http://tools.ietf.org/html/rfc7231#section-3.1.2.2) +/// +/// The `Content-Encoding` header field indicates what content codings +/// have been applied to the representation, beyond those inherent in the +/// media type, and thus what decoding mechanisms have to be applied in +/// order to obtain data in the media type referenced by the Content-Type +/// header field. Content-Encoding is primarily used to allow a +/// representation's data to be compressed without losing the identity of +/// its underlying media type. +/// +/// # ABNF +/// +/// ```text +/// Content-Encoding = 1#content-coding +/// ``` +/// +/// # Example values +/// +/// * `gzip` +/// +/// # Examples +/// +/// ``` +/// # extern crate headers; +/// use headers::ContentEncoding; +/// +/// let content_enc = ContentEncoding::gzip(); +/// ``` +#[derive(Clone, Debug)] +pub struct ContentEncoding(FlatCsv); + +derive_header! { + ContentEncoding(_), + name: CONTENT_ENCODING +} + +impl ContentEncoding { + /// A constructor to easily create a `Content-Encoding: gzip` header. + #[inline] + pub fn gzip() -> ContentEncoding { + ContentEncoding(HeaderValue::from_static("gzip").into()) + } + + /// Check if this header contains a given "coding". + /// + /// This can be used with these argument types: + /// + /// - `&str` + /// + /// # Example + /// + /// ``` + /// # extern crate headers; + /// use headers::ContentEncoding; + /// + /// let content_enc = ContentEncoding::gzip(); + /// + /// assert!(content_enc.contains("gzip")); + /// assert!(!content_enc.contains("br")); + /// ``` + pub fn contains(&self, coding: impl AsCoding) -> bool { + let s = coding.as_coding(); + self.0.iter().find(|&opt| opt == s).is_some() + } +} + +mod sealed { + pub trait AsCoding: Sealed {} + + pub trait Sealed { + fn as_coding(&self) -> &str; + } + + impl<'a> AsCoding for &'a str {} + + impl<'a> Sealed for &'a str { + fn as_coding(&self) -> &str { + *self + } + } +} diff --git a/third_party/rust/headers/src/common/content_length.rs b/third_party/rust/headers/src/common/content_length.rs new file mode 100644 index 0000000000..ff8c54234d --- /dev/null +++ b/third_party/rust/headers/src/common/content_length.rs @@ -0,0 +1,95 @@ +use {Header, HeaderValue}; + +/// `Content-Length` header, defined in +/// [RFC7230](http://tools.ietf.org/html/rfc7230#section-3.3.2) +/// +/// When a message does not have a `Transfer-Encoding` header field, a +/// Content-Length header field can provide the anticipated size, as a +/// decimal number of octets, for a potential payload body. For messages +/// that do include a payload body, the Content-Length field-value +/// provides the framing information necessary for determining where the +/// body (and message) ends. For messages that do not include a payload +/// body, the Content-Length indicates the size of the selected +/// representation. +/// +/// Note that setting this header will *remove* any previously set +/// `Transfer-Encoding` header, in accordance with +/// [RFC7230](http://tools.ietf.org/html/rfc7230#section-3.3.2): +/// +/// > A sender MUST NOT send a Content-Length header field in any message +/// > that contains a Transfer-Encoding header field. +/// +/// ## ABNF +/// +/// ```text +/// Content-Length = 1*DIGIT +/// ``` +/// +/// ## Example values +/// +/// * `3495` +/// +/// # Example +/// +/// ``` +/// # extern crate headers; +/// use headers::ContentLength; +/// +/// let len = ContentLength(1_000); +/// ``` +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct ContentLength(pub u64); + +impl Header for ContentLength { + fn name() -> &'static ::http::header::HeaderName { + &::http::header::CONTENT_LENGTH + } + + fn decode<'i, I: Iterator<Item = &'i HeaderValue>>(values: &mut I) -> Result<Self, ::Error> { + // If multiple Content-Length headers were sent, everything can still + // be alright if they all contain the same value, and all parse + // correctly. If not, then it's an error. + let mut len = None; + for value in values { + let parsed = value + .to_str() + .map_err(|_| ::Error::invalid())? + .parse::<u64>() + .map_err(|_| ::Error::invalid())?; + + if let Some(prev) = len { + if prev != parsed { + return Err(::Error::invalid()); + } + } else { + len = Some(parsed); + } + } + + len.map(ContentLength).ok_or_else(::Error::invalid) + } + + fn encode<E: Extend<::HeaderValue>>(&self, values: &mut E) { + values.extend(::std::iter::once(self.0.into())); + } +} + +/* +__hyper__tm!(ContentLength, tests { + // Testcase from RFC + test_header!(test1, vec![b"3495"], Some(HeaderField(3495))); + + test_header!(test_invalid, vec![b"34v95"], None); + + // Can't use the test_header macro because "5, 5" gets cleaned to "5". + #[test] + fn test_duplicates() { + let parsed = HeaderField::parse_header(&vec![b"5".to_vec(), + b"5".to_vec()].into()).unwrap(); + assert_eq!(parsed, HeaderField(5)); + assert_eq!(format!("{}", parsed), "5"); + } + + test_header!(test_duplicates_vary, vec![b"5", b"6", b"5"], None); +}); +*/ diff --git a/third_party/rust/headers/src/common/content_location.rs b/third_party/rust/headers/src/common/content_location.rs new file mode 100644 index 0000000000..29817d1b32 --- /dev/null +++ b/third_party/rust/headers/src/common/content_location.rs @@ -0,0 +1,55 @@ +use HeaderValue; + +/// `Content-Location` header, defined in +/// [RFC7231](https://tools.ietf.org/html/rfc7231#section-3.1.4.2) +/// +/// The header can be used by both the client in requests and the server +/// in responses with different semantics. Client sets `Content-Location` +/// to refer to the URI where original representation of the body was +/// obtained. +/// +/// In responses `Content-Location` represents URI for the representation +/// that was content negotiated, created or for the response payload. +/// +/// # ABNF +/// +/// ```text +/// Content-Location = absolute-URI / partial-URI +/// ``` +/// +/// # Example values +/// +/// * `/hypertext/Overview.html` +/// * `http://www.example.org/hypertext/Overview.html` +/// +/// # Examples +/// +#[derive(Clone, Debug, PartialEq)] +pub struct ContentLocation(HeaderValue); + +derive_header! { + ContentLocation(_), + name: CONTENT_LOCATION +} + +#[cfg(test)] +mod tests { + use super::super::test_decode; + use super::*; + + #[test] + fn absolute_uri() { + let s = "http://www.example.net/index.html"; + let loc = test_decode::<ContentLocation>(&[s]).unwrap(); + + assert_eq!(loc, ContentLocation(HeaderValue::from_static(s))); + } + + #[test] + fn relative_uri_with_fragment() { + let s = "/People.html#tim"; + let loc = test_decode::<ContentLocation>(&[s]).unwrap(); + + assert_eq!(loc, ContentLocation(HeaderValue::from_static(s))); + } +} diff --git a/third_party/rust/headers/src/common/content_range.rs b/third_party/rust/headers/src/common/content_range.rs new file mode 100644 index 0000000000..7ed2b200b5 --- /dev/null +++ b/third_party/rust/headers/src/common/content_range.rs @@ -0,0 +1,236 @@ +use std::fmt; +use std::ops::{Bound, RangeBounds}; + +use {util, HeaderValue}; + +/// Content-Range, described in [RFC7233](https://tools.ietf.org/html/rfc7233#section-4.2) +/// +/// # ABNF +/// +/// ```text +/// Content-Range = byte-content-range +/// / other-content-range +/// +/// byte-content-range = bytes-unit SP +/// ( byte-range-resp / unsatisfied-range ) +/// +/// byte-range-resp = byte-range "/" ( complete-length / "*" ) +/// byte-range = first-byte-pos "-" last-byte-pos +/// unsatisfied-range = "*/" complete-length +/// +/// complete-length = 1*DIGIT +/// +/// other-content-range = other-range-unit SP other-range-resp +/// other-range-resp = *CHAR +/// ``` +/// +/// # Example +/// +/// ``` +/// # extern crate headers; +/// use headers::ContentRange; +/// +/// // 100 bytes (included byte 199), with a full length of 3,400 +/// let cr = ContentRange::bytes(100..200, 3400).unwrap(); +/// ``` +//NOTE: only supporting bytes-content-range, YAGNI the extension +#[derive(Clone, Debug, PartialEq)] +pub struct ContentRange { + /// First and last bytes of the range, omitted if request could not be + /// satisfied + range: Option<(u64, u64)>, + + /// Total length of the instance, can be omitted if unknown + complete_length: Option<u64>, +} + +error_type!(InvalidContentRange); + +impl ContentRange { + /// Construct a new `Content-Range: bytes ..` header. + pub fn bytes( + range: impl RangeBounds<u64>, + complete_length: impl Into<Option<u64>>, + ) -> Result<ContentRange, InvalidContentRange> { + let complete_length = complete_length.into(); + + let start = match range.start_bound() { + Bound::Included(&s) => s, + Bound::Excluded(&s) => s + 1, + Bound::Unbounded => 0, + }; + + let end = match range.end_bound() { + Bound::Included(&e) => e, + Bound::Excluded(&e) => e - 1, + Bound::Unbounded => match complete_length { + Some(max) => max - 1, + None => return Err(InvalidContentRange { _inner: () }), + }, + }; + + Ok(ContentRange { + range: Some((start, end)), + complete_length, + }) + } + + /// Create a new `ContentRange` stating the range could not be satisfied. + /// + /// The passed argument is the complete length of the entity. + pub fn unsatisfied_bytes(complete_length: u64) -> Self { + ContentRange { + range: None, + complete_length: Some(complete_length), + } + } + + /// Get the byte range if satisified. + /// + /// Note that these byte ranges are inclusive on both ends. + pub fn bytes_range(&self) -> Option<(u64, u64)> { + self.range + } + + /// Get the bytes complete length if available. + pub fn bytes_len(&self) -> Option<u64> { + self.complete_length + } +} + +impl ::Header for ContentRange { + fn name() -> &'static ::HeaderName { + &::http::header::CONTENT_RANGE + } + + fn decode<'i, I: Iterator<Item = &'i HeaderValue>>(values: &mut I) -> Result<Self, ::Error> { + values + .next() + .and_then(|v| v.to_str().ok()) + .and_then(|s| split_in_two(s, ' ')) + .and_then(|(unit, spec)| { + if unit != "bytes" { + // For now, this only supports bytes-content-range. nani? + return None; + } + + let (range, complete_length) = split_in_two(spec, '/')?; + + let complete_length = if complete_length == "*" { + None + } else { + Some(complete_length.parse().ok()?) + }; + + let range = if range == "*" { + None + } else { + let (first_byte, last_byte) = split_in_two(range, '-')?; + let first_byte = first_byte.parse().ok()?; + let last_byte = last_byte.parse().ok()?; + if last_byte < first_byte { + return None; + } + Some((first_byte, last_byte)) + }; + + Some(ContentRange { + range, + complete_length, + }) + }) + .ok_or_else(::Error::invalid) + } + + fn encode<E: Extend<::HeaderValue>>(&self, values: &mut E) { + struct Adapter<'a>(&'a ContentRange); + + impl<'a> fmt::Display for Adapter<'a> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.write_str("bytes ")?; + + if let Some((first_byte, last_byte)) = self.0.range { + write!(f, "{}-{}", first_byte, last_byte)?; + } else { + f.write_str("*")?; + } + + f.write_str("/")?; + + if let Some(v) = self.0.complete_length { + write!(f, "{}", v) + } else { + f.write_str("*") + } + } + } + + values.extend(::std::iter::once(util::fmt(Adapter(self)))); + } +} + +fn split_in_two(s: &str, separator: char) -> Option<(&str, &str)> { + let mut iter = s.splitn(2, separator); + match (iter.next(), iter.next()) { + (Some(a), Some(b)) => Some((a, b)), + _ => None, + } +} + +/* + test_header!(test_bytes, + vec![b"bytes 0-499/500"], + Some(ContentRange(ContentRangeSpec::Bytes { + range: Some((0, 499)), + complete_length: Some(500) + }))); + + test_header!(test_bytes_unknown_len, + vec![b"bytes 0-499/*"], + Some(ContentRange(ContentRangeSpec::Bytes { + range: Some((0, 499)), + complete_length: None + }))); + + test_header!(test_bytes_unknown_range, + vec![b"bytes */500"], + Some(ContentRange(ContentRangeSpec::Bytes { + range: None, + complete_length: Some(500) + }))); + + test_header!(test_unregistered, + vec![b"seconds 1-2"], + Some(ContentRange(ContentRangeSpec::Unregistered { + unit: "seconds".to_owned(), + resp: "1-2".to_owned() + }))); + + test_header!(test_no_len, + vec![b"bytes 0-499"], + None::<ContentRange>); + + test_header!(test_only_unit, + vec![b"bytes"], + None::<ContentRange>); + + test_header!(test_end_less_than_start, + vec![b"bytes 499-0/500"], + None::<ContentRange>); + + test_header!(test_blank, + vec![b""], + None::<ContentRange>); + + test_header!(test_bytes_many_spaces, + vec![b"bytes 1-2/500 3"], + None::<ContentRange>); + + test_header!(test_bytes_many_slashes, + vec![b"bytes 1-2/500/600"], + None::<ContentRange>); + + test_header!(test_bytes_many_dashes, + vec![b"bytes 1-2-3/500"], + None::<ContentRange>); +*/ diff --git a/third_party/rust/headers/src/common/content_type.rs b/third_party/rust/headers/src/common/content_type.rs new file mode 100644 index 0000000000..bfe565272e --- /dev/null +++ b/third_party/rust/headers/src/common/content_type.rs @@ -0,0 +1,158 @@ +use std::fmt; + +use mime::{self, Mime}; + +/// `Content-Type` header, defined in +/// [RFC7231](http://tools.ietf.org/html/rfc7231#section-3.1.1.5) +/// +/// The `Content-Type` header field indicates the media type of the +/// associated representation: either the representation enclosed in the +/// message payload or the selected representation, as determined by the +/// message semantics. The indicated media type defines both the data +/// format and how that data is intended to be processed by a recipient, +/// within the scope of the received message semantics, after any content +/// codings indicated by Content-Encoding are decoded. +/// +/// Although the `mime` crate allows the mime options to be any slice, this crate +/// forces the use of Vec. This is to make sure the same header can't have more than 1 type. If +/// this is an issue, it's possible to implement `Header` on a custom struct. +/// +/// # ABNF +/// +/// ```text +/// Content-Type = media-type +/// ``` +/// +/// # Example values +/// +/// * `text/html; charset=utf-8` +/// * `application/json` +/// +/// # Examples +/// +/// ``` +/// # extern crate headers; +/// use headers::ContentType; +/// +/// let ct = ContentType::json(); +/// ``` +#[derive(Clone, Debug, PartialEq)] +pub struct ContentType(Mime); + +impl ContentType { + /// A constructor to easily create a `Content-Type: application/json` header. + #[inline] + pub fn json() -> ContentType { + ContentType(mime::APPLICATION_JSON) + } + + /// A constructor to easily create a `Content-Type: text/plain` header. + #[inline] + pub fn text() -> ContentType { + ContentType(mime::TEXT_PLAIN) + } + + /// A constructor to easily create a `Content-Type: text/plain; charset=utf-8` header. + #[inline] + pub fn text_utf8() -> ContentType { + ContentType(mime::TEXT_PLAIN_UTF_8) + } + + /// A constructor to easily create a `Content-Type: text/html` header. + #[inline] + pub fn html() -> ContentType { + ContentType(mime::TEXT_HTML) + } + + /// A constructor to easily create a `Content-Type: text/xml` header. + #[inline] + pub fn xml() -> ContentType { + ContentType(mime::TEXT_XML) + } + + /// A constructor to easily create a `Content-Type: application/www-form-url-encoded` header. + #[inline] + pub fn form_url_encoded() -> ContentType { + ContentType(mime::APPLICATION_WWW_FORM_URLENCODED) + } + /// A constructor to easily create a `Content-Type: image/jpeg` header. + #[inline] + pub fn jpeg() -> ContentType { + ContentType(mime::IMAGE_JPEG) + } + + /// A constructor to easily create a `Content-Type: image/png` header. + #[inline] + pub fn png() -> ContentType { + ContentType(mime::IMAGE_PNG) + } + + /// A constructor to easily create a `Content-Type: application/octet-stream` header. + #[inline] + pub fn octet_stream() -> ContentType { + ContentType(mime::APPLICATION_OCTET_STREAM) + } +} + +impl ::Header for ContentType { + fn name() -> &'static ::HeaderName { + &::http::header::CONTENT_TYPE + } + + fn decode<'i, I: Iterator<Item = &'i ::HeaderValue>>(values: &mut I) -> Result<Self, ::Error> { + values + .next() + .and_then(|v| v.to_str().ok()?.parse().ok()) + .map(ContentType) + .ok_or_else(::Error::invalid) + } + + fn encode<E: Extend<::HeaderValue>>(&self, values: &mut E) { + let value = self + .0 + .as_ref() + .parse() + .expect("Mime is always a valid HeaderValue"); + values.extend(::std::iter::once(value)); + } +} + +impl From<mime::Mime> for ContentType { + fn from(m: mime::Mime) -> ContentType { + ContentType(m) + } +} + +impl From<ContentType> for mime::Mime { + fn from(ct: ContentType) -> mime::Mime { + ct.0 + } +} + +impl fmt::Display for ContentType { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fmt::Display::fmt(&self.0, f) + } +} + +#[cfg(test)] +mod tests { + use super::super::test_decode; + use super::ContentType; + + #[test] + fn json() { + assert_eq!( + test_decode::<ContentType>(&["application/json"]), + Some(ContentType::json()), + ); + } + + bench_header!(bench_plain, ContentType, "text/plain"); + bench_header!(bench_json, ContentType, "application/json"); + bench_header!( + bench_formdata, + ContentType, + "multipart/form-data; boundary=---------------abcd" + ); +} diff --git a/third_party/rust/headers/src/common/cookie.rs b/third_party/rust/headers/src/common/cookie.rs new file mode 100644 index 0000000000..211ad58043 --- /dev/null +++ b/third_party/rust/headers/src/common/cookie.rs @@ -0,0 +1,205 @@ +use util::{FlatCsv, SemiColon}; + +/// `Cookie` header, defined in [RFC6265](http://tools.ietf.org/html/rfc6265#section-5.4) +/// +/// If the user agent does attach a Cookie header field to an HTTP +/// request, the user agent must send the cookie-string +/// as the value of the header field. +/// +/// When the user agent generates an HTTP request, the user agent MUST NOT +/// attach more than one Cookie header field. +/// +/// # Example values +/// * `SID=31d4d96e407aad42` +/// * `SID=31d4d96e407aad42; lang=en-US` +/// +#[derive(Clone, Debug)] +pub struct Cookie(FlatCsv<SemiColon>); + +derive_header! { + Cookie(_), + name: COOKIE +} + +impl Cookie { + /// Lookup a value for a cookie name. + /// + /// # Example + /// + /// ``` + /// # extern crate headers; + /// use headers::{Cookie, HeaderMap, HeaderMapExt, HeaderValue}; + /// + /// // Setup the header map with strings... + /// let mut headers = HeaderMap::new(); + /// headers.insert("cookie", HeaderValue::from_static("lang=en-US")); + /// + /// // Parse a `Cookie` so we can play with it... + /// let cookie = headers + /// .typed_get::<Cookie>() + /// .expect("we just inserted a valid Cookie"); + /// + /// assert_eq!(cookie.get("lang"), Some("en-US")); + /// assert_eq!(cookie.get("SID"), None); + /// ``` + pub fn get(&self, name: &str) -> Option<&str> { + self.iter() + .find(|&(key, _)| key == name) + .map(|(_, val)| val) + } + + /// Get the number of key-value pairs this `Cookie` contains. + pub fn len(&self) -> usize { + self.iter().count() + } + + /// Iterator the key-value pairs of this `Cookie` header. + pub fn iter(&self) -> impl Iterator<Item = (&str, &str)> { + self.0.iter().filter_map(|kv| { + let mut iter = kv.splitn(2, '='); + let key = iter.next()?.trim(); + let val = iter.next()?.trim(); + Some((key, val)) + }) + } +} + +/* +impl PartialEq for Cookie { + fn eq(&self, other: &Cookie) -> bool { + if self.0.len() == other.0.len() { + for &(ref k, ref v) in self.0.iter() { + if other.get(k) != Some(v) { + return false; + } + } + true + } else { + false + } + } +} +*/ + +#[cfg(test)] +mod tests { + use super::super::test_decode; + use super::Cookie; + + #[test] + fn test_parse() { + let cookie = test_decode::<Cookie>(&["foo=bar"]).unwrap(); + + assert_eq!(cookie.get("foo"), Some("bar")); + assert_eq!(cookie.get("bar"), None); + } + + #[test] + fn test_multipe_same_name() { + let cookie = test_decode::<Cookie>(&["foo=bar; foo=baz"]).unwrap(); + + assert_eq!(cookie.get("foo"), Some("bar")); + } + + #[test] + fn test_multipe_lines() { + let cookie = test_decode::<Cookie>(&["foo=bar", "lol = cat"]).unwrap(); + + assert_eq!(cookie.get("foo"), Some("bar")); + assert_eq!(cookie.get("lol"), Some("cat")); + } + + /* + #[test] + fn test_set_and_get() { + let mut cookie = Cookie::new(); + cookie.append("foo", "bar"); + cookie.append(String::from("dyn"), String::from("amic")); + + assert_eq!(cookie.get("foo"), Some("bar")); + assert_eq!(cookie.get("dyn"), Some("amic")); + assert!(cookie.get("nope").is_none()); + + cookie.append("foo", "notbar"); + assert_eq!(cookie.get("foo"), Some("bar")); + + cookie.set("foo", "hi"); + assert_eq!(cookie.get("foo"), Some("hi")); + assert_eq!(cookie.get("dyn"), Some("amic")); + } + + #[test] + fn test_eq() { + let mut cookie = Cookie::new(); + let mut cookie2 = Cookie::new(); + + // empty is equal + assert_eq!(cookie, cookie2); + + // left has more params + cookie.append("foo", "bar"); + assert_ne!(cookie, cookie2); + + // same len, different params + cookie2.append("bar", "foo"); + assert_ne!(cookie, cookie2); + + + // right has more params, and matching KV + cookie2.append("foo", "bar"); + assert_ne!(cookie, cookie2); + + // same params, different order + cookie.append("bar", "foo"); + assert_eq!(cookie, cookie2); + } + + #[test] + fn test_parse() { + let mut cookie = Cookie::new(); + + let parsed = Cookie::parse_header(&b"foo=bar".to_vec().into()).unwrap(); + cookie.append("foo", "bar"); + assert_eq!(cookie, parsed); + + let parsed = Cookie::parse_header(&b"foo=bar;".to_vec().into()).unwrap(); + assert_eq!(cookie, parsed); + + let parsed = Cookie::parse_header(&b"foo=bar; baz=quux".to_vec().into()).unwrap(); + cookie.append("baz", "quux"); + assert_eq!(cookie, parsed); + + let parsed = Cookie::parse_header(&b"foo=bar;; baz=quux".to_vec().into()).unwrap(); + assert_eq!(cookie, parsed); + + let parsed = Cookie::parse_header(&b"foo=bar; invalid ; bad; ;; baz=quux".to_vec().into()) + .unwrap(); + assert_eq!(cookie, parsed); + + let parsed = Cookie::parse_header(&b" foo = bar;baz= quux ".to_vec().into()).unwrap(); + assert_eq!(cookie, parsed); + + let parsed = + Cookie::parse_header(&vec![b"foo = bar".to_vec(), b"baz= quux ".to_vec()].into()) + .unwrap(); + assert_eq!(cookie, parsed); + + let parsed = Cookie::parse_header(&b"foo=bar; baz=quux ; empty=".to_vec().into()).unwrap(); + cookie.append("empty", ""); + assert_eq!(cookie, parsed); + + + let mut cookie = Cookie::new(); + + let parsed = Cookie::parse_header(&b"middle=equals=in=the=middle".to_vec().into()).unwrap(); + cookie.append("middle", "equals=in=the=middle"); + assert_eq!(cookie, parsed); + + let parsed = + Cookie::parse_header(&b"middle=equals=in=the=middle; double==2".to_vec().into()) + .unwrap(); + cookie.append("double", "=2"); + assert_eq!(cookie, parsed); + } + */ +} diff --git a/third_party/rust/headers/src/common/date.rs b/third_party/rust/headers/src/common/date.rs new file mode 100644 index 0000000000..96dd40f5b8 --- /dev/null +++ b/third_party/rust/headers/src/common/date.rs @@ -0,0 +1,46 @@ +use std::time::SystemTime; +use util::HttpDate; + +/// `Date` header, defined in [RFC7231](http://tools.ietf.org/html/rfc7231#section-7.1.1.2) +/// +/// The `Date` header field represents the date and time at which the +/// message was originated. +/// +/// ## ABNF +/// +/// ```text +/// Date = HTTP-date +/// ``` +/// +/// ## Example values +/// +/// * `Tue, 15 Nov 1994 08:12:31 GMT` +/// +/// # Example +/// +/// ``` +/// # extern crate headers; +/// use headers::Date; +/// use std::time::SystemTime; +/// +/// let date = Date::from(SystemTime::now()); +/// ``` +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct Date(HttpDate); + +derive_header! { + Date(_), + name: DATE +} + +impl From<SystemTime> for Date { + fn from(time: SystemTime) -> Date { + Date(time.into()) + } +} + +impl From<Date> for SystemTime { + fn from(date: Date) -> SystemTime { + date.0.into() + } +} diff --git a/third_party/rust/headers/src/common/etag.rs b/third_party/rust/headers/src/common/etag.rs new file mode 100644 index 0000000000..1bbd074070 --- /dev/null +++ b/third_party/rust/headers/src/common/etag.rs @@ -0,0 +1,113 @@ +use std::str::FromStr; +use util::EntityTag; + +/// `ETag` header, defined in [RFC7232](http://tools.ietf.org/html/rfc7232#section-2.3) +/// +/// The `ETag` header field in a response provides the current entity-tag +/// for the selected representation, as determined at the conclusion of +/// handling the request. An entity-tag is an opaque validator for +/// differentiating between multiple representations of the same +/// resource, regardless of whether those multiple representations are +/// due to resource state changes over time, content negotiation +/// resulting in multiple representations being valid at the same time, +/// or both. An entity-tag consists of an opaque quoted string, possibly +/// prefixed by a weakness indicator. +/// +/// # ABNF +/// +/// ```text +/// ETag = entity-tag +/// ``` +/// +/// # Example values +/// +/// * `"xyzzy"` +/// * `W/"xyzzy"` +/// * `""` +/// +/// # Examples +/// +/// ``` +/// let etag = "\"xyzzy\"".parse::<headers::ETag>().unwrap(); +/// ``` +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct ETag(pub(super) EntityTag); + +derive_header! { + ETag(_), + name: ETAG +} + +impl ETag { + #[cfg(test)] + pub(crate) fn from_static(src: &'static str) -> ETag { + ETag(EntityTag::from_static(src)) + } +} + +error_type!(InvalidETag); + +impl FromStr for ETag { + type Err = InvalidETag; + fn from_str(src: &str) -> Result<Self, Self::Err> { + let val = src + .parse() + .map_err(|_| InvalidETag { _inner: () })?; + + EntityTag::from_owned(val) + .map(ETag) + .ok_or_else(|| InvalidETag { _inner: () }) + } +} + +/* +test_etag { + // From the RFC + test_header!(test1, + vec![b"\"xyzzy\""], + Some(ETag(EntityTag::new(false, "xyzzy".to_owned())))); + test_header!(test2, + vec![b"W/\"xyzzy\""], + Some(ETag(EntityTag::new(true, "xyzzy".to_owned())))); + test_header!(test3, + vec![b"\"\""], + Some(ETag(EntityTag::new(false, "".to_owned())))); + // Own tests + test_header!(test4, + vec![b"\"foobar\""], + Some(ETag(EntityTag::new(false, "foobar".to_owned())))); + test_header!(test5, + vec![b"\"\""], + Some(ETag(EntityTag::new(false, "".to_owned())))); + test_header!(test6, + vec![b"W/\"weak-etag\""], + Some(ETag(EntityTag::new(true, "weak-etag".to_owned())))); + test_header!(test7, + vec![b"W/\"\x65\x62\""], + Some(ETag(EntityTag::new(true, "\u{0065}\u{0062}".to_owned())))); + test_header!(test8, + vec![b"W/\"\""], + Some(ETag(EntityTag::new(true, "".to_owned())))); + test_header!(test9, + vec![b"no-dquotes"], + None::<ETag>); + test_header!(test10, + vec![b"w/\"the-first-w-is-case-sensitive\""], + None::<ETag>); + test_header!(test11, + vec![b""], + None::<ETag>); + test_header!(test12, + vec![b"\"unmatched-dquotes1"], + None::<ETag>); + test_header!(test13, + vec![b"unmatched-dquotes2\""], + None::<ETag>); + test_header!(test14, + vec![b"matched-\"dquotes\""], + None::<ETag>); + test_header!(test15, + vec![b"\""], + None::<ETag>); +} +*/ diff --git a/third_party/rust/headers/src/common/expect.rs b/third_party/rust/headers/src/common/expect.rs new file mode 100644 index 0000000000..a1caf2530d --- /dev/null +++ b/third_party/rust/headers/src/common/expect.rs @@ -0,0 +1,86 @@ +use std::fmt; + +use util::IterExt; + +/// The `Expect` header. +/// +/// > The "Expect" header field in a request indicates a certain set of +/// > behaviors (expectations) that need to be supported by the server in +/// > order to properly handle this request. The only such expectation +/// > defined by this specification is 100-continue. +/// > +/// > Expect = "100-continue" +/// +/// # Example +/// +/// ``` +/// # extern crate headers; +/// use headers::Expect; +/// +/// let expect = Expect::CONTINUE; +/// ``` +#[derive(Clone, PartialEq)] +pub struct Expect(()); + +impl Expect { + /// "100-continue" + pub const CONTINUE: Expect = Expect(()); +} + +impl ::Header for Expect { + fn name() -> &'static ::HeaderName { + &::http::header::EXPECT + } + + fn decode<'i, I: Iterator<Item = &'i ::HeaderValue>>(values: &mut I) -> Result<Self, ::Error> { + values + .just_one() + .and_then(|value| { + if value == "100-continue" { + Some(Expect::CONTINUE) + } else { + None + } + }) + .ok_or_else(::Error::invalid) + } + + fn encode<E: Extend<::HeaderValue>>(&self, values: &mut E) { + values.extend(::std::iter::once(::HeaderValue::from_static( + "100-continue", + ))); + } +} + +impl fmt::Debug for Expect { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.debug_tuple("Expect").field(&"100-continue").finish() + } +} + +#[cfg(test)] +mod tests { + use super::super::test_decode; + use super::Expect; + + #[test] + fn expect_continue() { + assert_eq!( + test_decode::<Expect>(&["100-continue"]), + Some(Expect::CONTINUE), + ); + } + + #[test] + fn expectation_failed() { + assert_eq!(test_decode::<Expect>(&["sandwich"]), None,); + } + + #[test] + fn too_many_values() { + assert_eq!( + test_decode::<Expect>(&["100-continue", "100-continue"]), + None, + ); + } +} diff --git a/third_party/rust/headers/src/common/expires.rs b/third_party/rust/headers/src/common/expires.rs new file mode 100644 index 0000000000..26a44eeba9 --- /dev/null +++ b/third_party/rust/headers/src/common/expires.rs @@ -0,0 +1,50 @@ +use std::time::SystemTime; +use util::HttpDate; + +/// `Expires` header, defined in [RFC7234](http://tools.ietf.org/html/rfc7234#section-5.3) +/// +/// The `Expires` header field gives the date/time after which the +/// response is considered stale. +/// +/// The presence of an Expires field does not imply that the original +/// resource will change or cease to exist at, before, or after that +/// time. +/// +/// # ABNF +/// +/// ```text +/// Expires = HTTP-date +/// ``` +/// +/// # Example values +/// * `Thu, 01 Dec 1994 16:00:00 GMT` +/// +/// # Example +/// +/// ``` +/// # extern crate headers; +/// use headers::Expires; +/// use std::time::{SystemTime, Duration}; +/// +/// let time = SystemTime::now() + Duration::from_secs(60 * 60 * 24); +/// let expires = Expires::from(time); +/// ``` +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct Expires(HttpDate); + +derive_header! { + Expires(_), + name: EXPIRES +} + +impl From<SystemTime> for Expires { + fn from(time: SystemTime) -> Expires { + Expires(time.into()) + } +} + +impl From<Expires> for SystemTime { + fn from(date: Expires) -> SystemTime { + date.0.into() + } +} diff --git a/third_party/rust/headers/src/common/host.rs b/third_party/rust/headers/src/common/host.rs new file mode 100644 index 0000000000..a5c41b1d1a --- /dev/null +++ b/third_party/rust/headers/src/common/host.rs @@ -0,0 +1,54 @@ +use std::fmt; +use std::convert::TryFrom; + +use http::uri::Authority; + +/// The `Host` header. +#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd)] +pub struct Host(Authority); + +impl Host { + /// Get the hostname, such as example.domain. + pub fn hostname(&self) -> &str { + self.0.host() + } + + /// Get the optional port number. + pub fn port(&self) -> Option<u16> { + self.0.port_u16() + } +} + +impl ::Header for Host { + fn name() -> &'static ::HeaderName { + &::http::header::HOST + } + + fn decode<'i, I: Iterator<Item = &'i ::HeaderValue>>(values: &mut I) -> Result<Self, ::Error> { + values + .next() + .cloned() + .and_then(|val| Authority::try_from(val.as_bytes()).ok()) + .map(Host) + .ok_or_else(::Error::invalid) + } + + fn encode<E: Extend<::HeaderValue>>(&self, values: &mut E) { + let bytes = self.0.as_str().as_bytes(); + let val = ::HeaderValue::from_bytes(bytes).expect("Authority is a valid HeaderValue"); + + values.extend(::std::iter::once(val)); + } +} + +impl From<Authority> for Host { + fn from(auth: Authority) -> Host { + Host(auth) + } +} + +impl fmt::Display for Host { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fmt::Display::fmt(&self.0, f) + } +} diff --git a/third_party/rust/headers/src/common/if_match.rs b/third_party/rust/headers/src/common/if_match.rs new file mode 100644 index 0000000000..5b9bdd95e3 --- /dev/null +++ b/third_party/rust/headers/src/common/if_match.rs @@ -0,0 +1,111 @@ +use super::ETag; +use util::EntityTagRange; +use HeaderValue; + +/// `If-Match` header, defined in +/// [RFC7232](https://tools.ietf.org/html/rfc7232#section-3.1) +/// +/// The `If-Match` header field makes the request method conditional on +/// the recipient origin server either having at least one current +/// representation of the target resource, when the field-value is "*", +/// or having a current representation of the target resource that has an +/// entity-tag matching a member of the list of entity-tags provided in +/// the field-value. +/// +/// An origin server MUST use the strong comparison function when +/// comparing entity-tags for `If-Match`, since the client +/// intends this precondition to prevent the method from being applied if +/// there have been any changes to the representation data. +/// +/// # ABNF +/// +/// ```text +/// If-Match = "*" / 1#entity-tag +/// ``` +/// +/// # Example values +/// +/// * `"xyzzy"` +/// * "xyzzy", "r2d2xxxx", "c3piozzzz" +/// +/// # Examples +/// +/// ``` +/// # extern crate headers; +/// use headers::IfMatch; +/// +/// let if_match = IfMatch::any(); +/// ``` +#[derive(Clone, Debug, PartialEq)] +pub struct IfMatch(EntityTagRange); + +derive_header! { + IfMatch(_), + name: IF_MATCH +} + +impl IfMatch { + /// Create a new `If-Match: *` header. + pub fn any() -> IfMatch { + IfMatch(EntityTagRange::Any) + } + + /// Returns whether this is `If-Match: *`, matching any entity tag. + pub fn is_any(&self) -> bool { + match self.0 { + EntityTagRange::Any => true, + EntityTagRange::Tags(..) => false, + } + } + + /// Checks whether the `ETag` strongly matches. + pub fn precondition_passes(&self, etag: &ETag) -> bool { + self.0.matches_strong(&etag.0) + } +} + +impl From<ETag> for IfMatch { + fn from(etag: ETag) -> IfMatch { + IfMatch(EntityTagRange::Tags(HeaderValue::from(etag.0).into())) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn is_any() { + assert!(IfMatch::any().is_any()); + assert!(!IfMatch::from(ETag::from_static("\"yolo\"")).is_any()); + } + + #[test] + fn precondition_fails() { + let if_match = IfMatch::from(ETag::from_static("\"foo\"")); + + let bar = ETag::from_static("\"bar\""); + let weak_foo = ETag::from_static("W/\"foo\""); + + assert!(!if_match.precondition_passes(&bar)); + assert!(!if_match.precondition_passes(&weak_foo)); + } + + #[test] + fn precondition_passes() { + let foo = ETag::from_static("\"foo\""); + + let if_match = IfMatch::from(foo.clone()); + + assert!(if_match.precondition_passes(&foo)); + } + + #[test] + fn precondition_any() { + let foo = ETag::from_static("\"foo\""); + + let if_match = IfMatch::any(); + + assert!(if_match.precondition_passes(&foo)); + } +} diff --git a/third_party/rust/headers/src/common/if_modified_since.rs b/third_party/rust/headers/src/common/if_modified_since.rs new file mode 100644 index 0000000000..db64ea75e0 --- /dev/null +++ b/third_party/rust/headers/src/common/if_modified_since.rs @@ -0,0 +1,75 @@ +use std::time::SystemTime; +use util::HttpDate; + +/// `If-Modified-Since` header, defined in +/// [RFC7232](http://tools.ietf.org/html/rfc7232#section-3.3) +/// +/// The `If-Modified-Since` header field makes a GET or HEAD request +/// method conditional on the selected representation's modification date +/// being more recent than the date provided in the field-value. +/// Transfer of the selected representation's data is avoided if that +/// data has not changed. +/// +/// # ABNF +/// +/// ```text +/// If-Modified-Since = HTTP-date +/// ``` +/// +/// # Example values +/// * `Sat, 29 Oct 1994 19:43:31 GMT` +/// +/// # Example +/// +/// ``` +/// # extern crate headers; +/// use headers::IfModifiedSince; +/// use std::time::{Duration, SystemTime}; +/// +/// let time = SystemTime::now() - Duration::from_secs(60 * 60 * 24); +/// let if_mod = IfModifiedSince::from(time); +/// ``` +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct IfModifiedSince(HttpDate); + +derive_header! { + IfModifiedSince(_), + name: IF_MODIFIED_SINCE +} + +impl IfModifiedSince { + /// Check if the supplied time means the resource has been modified. + pub fn is_modified(&self, last_modified: SystemTime) -> bool { + self.0 < last_modified.into() + } +} + +impl From<SystemTime> for IfModifiedSince { + fn from(time: SystemTime) -> IfModifiedSince { + IfModifiedSince(time.into()) + } +} + +impl From<IfModifiedSince> for SystemTime { + fn from(date: IfModifiedSince) -> SystemTime { + date.0.into() + } +} + +#[cfg(test)] +mod tests { + use super::*; + use std::time::Duration; + + #[test] + fn is_modified() { + let newer = SystemTime::now(); + let exact = newer - Duration::from_secs(2); + let older = newer - Duration::from_secs(4); + + let if_mod = IfModifiedSince::from(exact); + assert!(if_mod.is_modified(newer)); + assert!(!if_mod.is_modified(exact)); + assert!(!if_mod.is_modified(older)); + } +} diff --git a/third_party/rust/headers/src/common/if_none_match.rs b/third_party/rust/headers/src/common/if_none_match.rs new file mode 100644 index 0000000000..590609cb1d --- /dev/null +++ b/third_party/rust/headers/src/common/if_none_match.rs @@ -0,0 +1,111 @@ +use super::ETag; +use util::EntityTagRange; +use HeaderValue; + +/// `If-None-Match` header, defined in +/// [RFC7232](https://tools.ietf.org/html/rfc7232#section-3.2) +/// +/// The `If-None-Match` header field makes the request method conditional +/// on a recipient cache or origin server either not having any current +/// representation of the target resource, when the field-value is "*", +/// or having a selected representation with an entity-tag that does not +/// match any of those listed in the field-value. +/// +/// A recipient MUST use the weak comparison function when comparing +/// entity-tags for If-None-Match (Section 2.3.2), since weak entity-tags +/// can be used for cache validation even if there have been changes to +/// the representation data. +/// +/// # ABNF +/// +/// ```text +/// If-None-Match = "*" / 1#entity-tag +/// ``` +/// +/// # Example values +/// +/// * `"xyzzy"` +/// * `W/"xyzzy"` +/// * `"xyzzy", "r2d2xxxx", "c3piozzzz"` +/// * `W/"xyzzy", W/"r2d2xxxx", W/"c3piozzzz"` +/// * `*` +/// +/// # Examples +/// +/// ``` +/// # extern crate headers; +/// use headers::IfNoneMatch; +/// +/// let if_none_match = IfNoneMatch::any(); +/// ``` +#[derive(Clone, Debug, PartialEq)] +pub struct IfNoneMatch(EntityTagRange); + +derive_header! { + IfNoneMatch(_), + name: IF_NONE_MATCH +} + +impl IfNoneMatch { + /// Create a new `If-None-Match: *` header. + pub fn any() -> IfNoneMatch { + IfNoneMatch(EntityTagRange::Any) + } + + /// Checks whether the ETag passes this precondition. + pub fn precondition_passes(&self, etag: &ETag) -> bool { + !self.0.matches_weak(&etag.0) + } +} + +impl From<ETag> for IfNoneMatch { + fn from(etag: ETag) -> IfNoneMatch { + IfNoneMatch(EntityTagRange::Tags(HeaderValue::from(etag.0).into())) + } +} + +/* +test_if_none_match { + test_header!(test1, vec![b"\"xyzzy\""]); + test_header!(test2, vec![b"W/\"xyzzy\""]); + test_header!(test3, vec![b"\"xyzzy\", \"r2d2xxxx\", \"c3piozzzz\""]); + test_header!(test4, vec![b"W/\"xyzzy\", W/\"r2d2xxxx\", W/\"c3piozzzz\""]); + test_header!(test5, vec![b"*"]); +} +*/ + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn precondition_fails() { + let foo = ETag::from_static("\"foo\""); + let weak_foo = ETag::from_static("W/\"foo\""); + + let if_none = IfNoneMatch::from(foo.clone()); + + assert!(!if_none.precondition_passes(&foo)); + assert!(!if_none.precondition_passes(&weak_foo)); + } + + #[test] + fn precondition_passes() { + let if_none = IfNoneMatch::from(ETag::from_static("\"foo\"")); + + let bar = ETag::from_static("\"bar\""); + let weak_bar = ETag::from_static("W/\"bar\""); + + assert!(if_none.precondition_passes(&bar)); + assert!(if_none.precondition_passes(&weak_bar)); + } + + #[test] + fn precondition_any() { + let foo = ETag::from_static("\"foo\""); + + let if_none = IfNoneMatch::any(); + + assert!(!if_none.precondition_passes(&foo)); + } +} diff --git a/third_party/rust/headers/src/common/if_range.rs b/third_party/rust/headers/src/common/if_range.rs new file mode 100644 index 0000000000..38480bbea5 --- /dev/null +++ b/third_party/rust/headers/src/common/if_range.rs @@ -0,0 +1,134 @@ +use std::time::SystemTime; + +use super::{ETag, LastModified}; +use util::{EntityTag, HttpDate}; +use HeaderValue; + +/// `If-Range` header, defined in [RFC7233](http://tools.ietf.org/html/rfc7233#section-3.2) +/// +/// If a client has a partial copy of a representation and wishes to have +/// an up-to-date copy of the entire representation, it could use the +/// Range header field with a conditional GET (using either or both of +/// If-Unmodified-Since and If-Match.) However, if the precondition +/// fails because the representation has been modified, the client would +/// then have to make a second request to obtain the entire current +/// representation. +/// +/// The `If-Range` header field allows a client to \"short-circuit\" the +/// second request. Informally, its meaning is as follows: if the +/// representation is unchanged, send me the part(s) that I am requesting +/// in Range; otherwise, send me the entire representation. +/// +/// # ABNF +/// +/// ```text +/// If-Range = entity-tag / HTTP-date +/// ``` +/// +/// # Example values +/// +/// * `Sat, 29 Oct 1994 19:43:31 GMT` +/// * `\"xyzzy\"` +/// +/// # Examples +/// +/// ``` +/// # extern crate headers; +/// use headers::IfRange; +/// use std::time::{SystemTime, Duration}; +/// +/// let fetched = SystemTime::now() - Duration::from_secs(60 * 60 * 24); +/// let if_range = IfRange::date(fetched); +/// ``` +#[derive(Clone, Debug, PartialEq)] +pub struct IfRange(IfRange_); + +derive_header! { + IfRange(_), + name: IF_RANGE +} + +impl IfRange { + /// Create an `IfRange` header with an entity tag. + pub fn etag(tag: ETag) -> IfRange { + IfRange(IfRange_::EntityTag(tag.0)) + } + + /// Create an `IfRange` header with a date value. + pub fn date(time: SystemTime) -> IfRange { + IfRange(IfRange_::Date(time.into())) + } + + /// Checks if the resource has been modified, or if the range request + /// can be served. + pub fn is_modified(&self, etag: Option<&ETag>, last_modified: Option<&LastModified>) -> bool { + match self.0 { + IfRange_::Date(since) => last_modified.map(|time| since < time.0).unwrap_or(true), + IfRange_::EntityTag(ref entity) => etag.map(|etag| !etag.0.strong_eq(entity)).unwrap_or(true), + } + } +} + +#[derive(Clone, Debug, PartialEq)] +enum IfRange_ { + /// The entity-tag the client has of the resource + EntityTag(EntityTag), + /// The date when the client retrieved the resource + Date(HttpDate), +} + +impl ::util::TryFromValues for IfRange_ { + fn try_from_values<'i, I>(values: &mut I) -> Result<Self, ::Error> + where + I: Iterator<Item = &'i HeaderValue>, + { + values + .next() + .and_then(|val| { + if let Some(tag) = EntityTag::from_val(val) { + return Some(IfRange_::EntityTag(tag)); + } + + let date = HttpDate::from_val(val)?; + Some(IfRange_::Date(date)) + }) + .ok_or_else(::Error::invalid) + } +} + +impl<'a> From<&'a IfRange_> for HeaderValue { + fn from(if_range: &'a IfRange_) -> HeaderValue { + match *if_range { + IfRange_::EntityTag(ref tag) => tag.into(), + IfRange_::Date(ref date) => date.into(), + } + } +} + +/* +#[cfg(test)] +mod tests { + use std::str; + use *; + use super::IfRange as HeaderField; + test_header!(test1, vec![b"Sat, 29 Oct 1994 19:43:31 GMT"]); + test_header!(test2, vec![b"\"xyzzy\""]); + test_header!(test3, vec![b"this-is-invalid"], None::<IfRange>); +} +*/ + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_is_modified_etag() { + let etag = ETag::from_static("\"xyzzy\""); + let if_range = IfRange::etag(etag.clone()); + + assert!(!if_range.is_modified(Some(&etag), None)); + + let etag = ETag::from_static("W/\"xyzzy\""); + assert!(if_range.is_modified(Some(&etag), None)); + } +} diff --git a/third_party/rust/headers/src/common/if_unmodified_since.rs b/third_party/rust/headers/src/common/if_unmodified_since.rs new file mode 100644 index 0000000000..d9b32107a1 --- /dev/null +++ b/third_party/rust/headers/src/common/if_unmodified_since.rs @@ -0,0 +1,76 @@ +use std::time::SystemTime; +use util::HttpDate; + +/// `If-Unmodified-Since` header, defined in +/// [RFC7232](http://tools.ietf.org/html/rfc7232#section-3.4) +/// +/// The `If-Unmodified-Since` header field makes the request method +/// conditional on the selected representation's last modification date +/// being earlier than or equal to the date provided in the field-value. +/// This field accomplishes the same purpose as If-Match for cases where +/// the user agent does not have an entity-tag for the representation. +/// +/// # ABNF +/// +/// ```text +/// If-Unmodified-Since = HTTP-date +/// ``` +/// +/// # Example values +/// +/// * `Sat, 29 Oct 1994 19:43:31 GMT` +/// +/// # Example +/// +/// ``` +/// # extern crate headers; +/// use headers::IfUnmodifiedSince; +/// use std::time::{SystemTime, Duration}; +/// +/// let time = SystemTime::now() - Duration::from_secs(60 * 60 * 24); +/// let if_unmod = IfUnmodifiedSince::from(time); +/// ``` +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct IfUnmodifiedSince(HttpDate); + +derive_header! { + IfUnmodifiedSince(_), + name: IF_UNMODIFIED_SINCE +} + +impl IfUnmodifiedSince { + /// Check if the supplied time passes the precondtion. + pub fn precondition_passes(&self, last_modified: SystemTime) -> bool { + self.0 >= last_modified.into() + } +} + +impl From<SystemTime> for IfUnmodifiedSince { + fn from(time: SystemTime) -> IfUnmodifiedSince { + IfUnmodifiedSince(time.into()) + } +} + +impl From<IfUnmodifiedSince> for SystemTime { + fn from(date: IfUnmodifiedSince) -> SystemTime { + date.0.into() + } +} + +#[cfg(test)] +mod tests { + use super::*; + use std::time::Duration; + + #[test] + fn precondition_passes() { + let newer = SystemTime::now(); + let exact = newer - Duration::from_secs(2); + let older = newer - Duration::from_secs(4); + + let if_unmod = IfUnmodifiedSince::from(exact); + assert!(!if_unmod.precondition_passes(newer)); + assert!(if_unmod.precondition_passes(exact)); + assert!(if_unmod.precondition_passes(older)); + } +} diff --git a/third_party/rust/headers/src/common/last_modified.rs b/third_party/rust/headers/src/common/last_modified.rs new file mode 100644 index 0000000000..c8f47dabdf --- /dev/null +++ b/third_party/rust/headers/src/common/last_modified.rs @@ -0,0 +1,51 @@ +use std::time::SystemTime; +use util::HttpDate; + +/// `Last-Modified` header, defined in +/// [RFC7232](http://tools.ietf.org/html/rfc7232#section-2.2) +/// +/// The `Last-Modified` header field in a response provides a timestamp +/// indicating the date and time at which the origin server believes the +/// selected representation was last modified, as determined at the +/// conclusion of handling the request. +/// +/// # ABNF +/// +/// ```text +/// Expires = HTTP-date +/// ``` +/// +/// # Example values +/// +/// * `Sat, 29 Oct 1994 19:43:31 GMT` +/// +/// # Example +/// +/// ``` +/// # extern crate headers; +/// use headers::LastModified; +/// use std::time::{Duration, SystemTime}; +/// +/// let modified = LastModified::from( +/// SystemTime::now() - Duration::from_secs(60 * 60 * 24) +/// ); +/// ``` +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct LastModified(pub(super) HttpDate); + +derive_header! { + LastModified(_), + name: LAST_MODIFIED +} + +impl From<SystemTime> for LastModified { + fn from(time: SystemTime) -> LastModified { + LastModified(time.into()) + } +} + +impl From<LastModified> for SystemTime { + fn from(date: LastModified) -> SystemTime { + date.0.into() + } +} diff --git a/third_party/rust/headers/src/common/location.rs b/third_party/rust/headers/src/common/location.rs new file mode 100644 index 0000000000..54a1cc513d --- /dev/null +++ b/third_party/rust/headers/src/common/location.rs @@ -0,0 +1,51 @@ +use HeaderValue; + +/// `Location` header, defined in +/// [RFC7231](http://tools.ietf.org/html/rfc7231#section-7.1.2) +/// +/// The `Location` header field is used in some responses to refer to a +/// specific resource in relation to the response. The type of +/// relationship is defined by the combination of request method and +/// status code semantics. +/// +/// # ABNF +/// +/// ```text +/// Location = URI-reference +/// ``` +/// +/// # Example values +/// * `/People.html#tim` +/// * `http://www.example.net/index.html` +/// +/// # Examples +/// +#[derive(Clone, Debug, PartialEq)] +pub struct Location(HeaderValue); + +derive_header! { + Location(_), + name: LOCATION +} + +#[cfg(test)] +mod tests { + use super::super::test_decode; + use super::*; + + #[test] + fn absolute_uri() { + let s = "http://www.example.net/index.html"; + let loc = test_decode::<Location>(&[s]).unwrap(); + + assert_eq!(loc, Location(HeaderValue::from_static(s))); + } + + #[test] + fn relative_uri_with_fragment() { + let s = "/People.html#tim"; + let loc = test_decode::<Location>(&[s]).unwrap(); + + assert_eq!(loc, Location(HeaderValue::from_static(s))); + } +} diff --git a/third_party/rust/headers/src/common/mod.rs b/third_party/rust/headers/src/common/mod.rs new file mode 100644 index 0000000000..2237ae8e64 --- /dev/null +++ b/third_party/rust/headers/src/common/mod.rs @@ -0,0 +1,190 @@ +//! A Collection of Header implementations for common HTTP Headers. +//! +//! ## Mime +//! +//! Several header fields use MIME values for their contents. Keeping with the +//! strongly-typed theme, the [mime](https://docs.rs/mime) crate +//! is used, such as `ContentType(pub Mime)`. + +//pub use self::accept_charset::AcceptCharset; +//pub use self::accept_encoding::AcceptEncoding; +//pub use self::accept_language::AcceptLanguage; +pub use self::accept_ranges::AcceptRanges; +//pub use self::accept::Accept; +pub use self::access_control_allow_credentials::AccessControlAllowCredentials; +pub use self::access_control_allow_headers::AccessControlAllowHeaders; +pub use self::access_control_allow_methods::AccessControlAllowMethods; +pub use self::access_control_allow_origin::AccessControlAllowOrigin; +pub use self::access_control_expose_headers::AccessControlExposeHeaders; +pub use self::access_control_max_age::AccessControlMaxAge; +pub use self::access_control_request_headers::AccessControlRequestHeaders; +pub use self::access_control_request_method::AccessControlRequestMethod; +pub use self::age::Age; +pub use self::allow::Allow; +pub use self::authorization::Authorization; +pub use self::cache_control::CacheControl; +pub use self::connection::Connection; +pub use self::content_disposition::ContentDisposition; +pub use self::content_encoding::ContentEncoding; +//pub use self::content_language::ContentLanguage; +pub use self::content_length::ContentLength; +pub use self::content_location::ContentLocation; +pub use self::content_range::ContentRange; +pub use self::content_type::ContentType; +pub use self::cookie::Cookie; +pub use self::date::Date; +pub use self::etag::ETag; +pub use self::expect::Expect; +pub use self::expires::Expires; +//pub use self::from::From; +pub use self::host::Host; +pub use self::if_match::IfMatch; +pub use self::if_modified_since::IfModifiedSince; +pub use self::if_none_match::IfNoneMatch; +pub use self::if_range::IfRange; +pub use self::if_unmodified_since::IfUnmodifiedSince; +//pub use self::last_event_id::LastEventId; +pub use self::last_modified::LastModified; +//pub use self::link::{Link, LinkValue, RelationType, MediaDesc}; +pub use self::location::Location; +pub use self::origin::Origin; +pub use self::pragma::Pragma; +//pub use self::prefer::{Prefer, Preference}; +//pub use self::preference_applied::PreferenceApplied; +pub use self::proxy_authorization::ProxyAuthorization; +pub use self::range::Range; +pub use self::referer::Referer; +pub use self::referrer_policy::ReferrerPolicy; +pub use self::retry_after::RetryAfter; +pub use self::sec_websocket_accept::SecWebsocketAccept; +pub use self::sec_websocket_key::SecWebsocketKey; +pub use self::sec_websocket_version::SecWebsocketVersion; +pub use self::server::Server; +pub use self::set_cookie::SetCookie; +pub use self::strict_transport_security::StrictTransportSecurity; +pub use self::te::Te; +pub use self::transfer_encoding::TransferEncoding; +pub use self::upgrade::Upgrade; +pub use self::user_agent::UserAgent; +pub use self::vary::Vary; +//pub use self::warning::Warning; + +#[cfg(test)] +fn test_decode<T: ::Header>(values: &[&str]) -> Option<T> { + use HeaderMapExt; + let mut map = ::http::HeaderMap::new(); + for val in values { + map.append(T::name(), val.parse().unwrap()); + } + map.typed_get() +} + +#[cfg(test)] +fn test_encode<T: ::Header>(header: T) -> ::http::HeaderMap { + use HeaderMapExt; + let mut map = ::http::HeaderMap::new(); + map.typed_insert(header); + map +} + +#[cfg(test)] +macro_rules! bench_header { + ($mod:ident, $ty:ident, $value:expr) => { + #[cfg(feature = "nightly")] + mod $mod { + use super::$ty; + use HeaderMapExt; + + #[bench] + fn bench_decode(b: &mut ::test::Bencher) { + let mut map = ::http::HeaderMap::new(); + map.append( + <$ty as ::Header>::name(), + $value.parse().expect("HeaderValue::from_str($value)"), + ); + b.bytes = $value.len() as u64; + b.iter(|| { + map.typed_get::<$ty>().unwrap(); + }); + } + + #[bench] + fn bench_encode(b: &mut ::test::Bencher) { + let mut map = ::http::HeaderMap::new(); + map.append( + <$ty as ::Header>::name(), + $value.parse().expect("HeaderValue::from_str($value)"), + ); + let typed = map.typed_get::<$ty>().unwrap(); + b.bytes = $value.len() as u64; + b.iter(|| { + map.typed_insert(typed.clone()); + map.clear(); + }); + } + } + }; +} + +//mod accept; +//mod accept_charset; +//mod accept_encoding; +//mod accept_language; +mod accept_ranges; +mod access_control_allow_credentials; +mod access_control_allow_headers; +mod access_control_allow_methods; +mod access_control_allow_origin; +mod access_control_expose_headers; +mod access_control_max_age; +mod access_control_request_headers; +mod access_control_request_method; +mod age; +mod allow; +pub mod authorization; +mod cache_control; +mod connection; +mod content_disposition; +mod content_encoding; +//mod content_language; +mod content_length; +mod content_location; +mod content_range; +mod content_type; +mod cookie; +mod date; +mod etag; +mod expect; +mod expires; +//mod from; +mod host; +mod if_match; +mod if_modified_since; +mod if_none_match; +mod if_range; +mod if_unmodified_since; +//mod last_event_id; +mod last_modified; +//mod link; +mod location; +mod origin; +mod pragma; +//mod prefer; +//mod preference_applied; +mod proxy_authorization; +mod range; +mod referer; +mod referrer_policy; +mod retry_after; +mod sec_websocket_accept; +mod sec_websocket_key; +mod sec_websocket_version; +mod server; +mod set_cookie; +mod strict_transport_security; +mod te; +mod transfer_encoding; +mod upgrade; +mod user_agent; +mod vary; +//mod warning; diff --git a/third_party/rust/headers/src/common/origin.rs b/third_party/rust/headers/src/common/origin.rs new file mode 100644 index 0000000000..09349f5ea7 --- /dev/null +++ b/third_party/rust/headers/src/common/origin.rs @@ -0,0 +1,210 @@ +use std::fmt; +use std::convert::TryFrom; + +use bytes::Bytes; +use http::uri::{self, Authority, Scheme, Uri}; + +use util::{IterExt, TryFromValues}; +use HeaderValue; + +/// The `Origin` header. +/// +/// The `Origin` header is a version of the `Referer` header that is used for all HTTP fetches and `POST`s whose CORS flag is set. +/// This header is often used to inform recipients of the security context of where the request was initiated. +/// +/// Following the spec, [https://fetch.spec.whatwg.org/#origin-header][url], the value of this header is composed of +/// a String (scheme), Host (host/port) +/// +/// [url]: https://fetch.spec.whatwg.org/#origin-header +/// +/// # Examples +/// +/// ``` +/// # extern crate headers; +/// use headers::Origin; +/// +/// let origin = Origin::NULL; +/// ``` +#[derive(Clone, Debug, PartialEq, Eq, Hash)] +pub struct Origin(OriginOrNull); + +derive_header! { + Origin(_), + name: ORIGIN +} + +#[derive(Clone, Debug, PartialEq, Eq, Hash)] +enum OriginOrNull { + Origin(Scheme, Authority), + Null, +} + +impl Origin { + /// The literal `null` Origin header. + pub const NULL: Origin = Origin(OriginOrNull::Null); + + /// Checks if `Origin` is `null`. + #[inline] + pub fn is_null(&self) -> bool { + match self.0 { + OriginOrNull::Null => true, + _ => false, + } + } + + /// Get the "scheme" part of this origin. + #[inline] + pub fn scheme(&self) -> &str { + match self.0 { + OriginOrNull::Origin(ref scheme, _) => scheme.as_str(), + OriginOrNull::Null => "", + } + } + + /// Get the "hostname" part of this origin. + #[inline] + pub fn hostname(&self) -> &str { + match self.0 { + OriginOrNull::Origin(_, ref auth) => auth.host(), + OriginOrNull::Null => "", + } + } + + /// Get the "port" part of this origin. + #[inline] + pub fn port(&self) -> Option<u16> { + match self.0 { + OriginOrNull::Origin(_, ref auth) => auth.port_u16(), + OriginOrNull::Null => None, + } + } + + /// Tries to build a `Origin` from three parts, the scheme, the host and an optional port. + pub fn try_from_parts( + scheme: &str, + host: &str, + port: impl Into<Option<u16>>, + ) -> Result<Self, InvalidOrigin> { + struct MaybePort(Option<u16>); + + impl fmt::Display for MaybePort { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + if let Some(port) = self.0 { + write!(f, ":{}", port) + } else { + Ok(()) + } + } + } + + let bytes = Bytes::from(format!("{}://{}{}", scheme, host, MaybePort(port.into()))); + HeaderValue::from_maybe_shared(bytes) + .ok() + .and_then(|val| Self::try_from_value(&val)) + .ok_or_else(|| InvalidOrigin { _inner: () }) + } + + // Used in AccessControlAllowOrigin + pub(super) fn try_from_value(value: &HeaderValue) -> Option<Self> { + OriginOrNull::try_from_value(value).map(Origin) + } + + pub(super) fn into_value(&self) -> HeaderValue { + (&self.0).into() + } +} + +impl fmt::Display for Origin { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self.0 { + OriginOrNull::Origin(ref scheme, ref auth) => write!(f, "{}://{}", scheme, auth), + OriginOrNull::Null => f.write_str("null"), + } + } +} + +error_type!(InvalidOrigin); + +impl OriginOrNull { + fn try_from_value(value: &HeaderValue) -> Option<Self> { + if value == "null" { + return Some(OriginOrNull::Null); + } + + let uri = Uri::try_from(value.as_bytes()).ok()?; + + let (scheme, auth) = match uri.into_parts() { + uri::Parts { + scheme: Some(scheme), + authority: Some(auth), + path_and_query: None, + .. + } => (scheme, auth), + uri::Parts { + scheme: Some(ref scheme), + authority: Some(ref auth), + path_and_query: Some(ref p), + .. + } if p == "/" => (scheme.clone(), auth.clone()), + _ => { + return None; + } + }; + + Some(OriginOrNull::Origin(scheme, auth)) + } +} + +impl TryFromValues for OriginOrNull { + fn try_from_values<'i, I>(values: &mut I) -> Result<Self, ::Error> + where + I: Iterator<Item = &'i HeaderValue>, + { + values + .just_one() + .and_then(OriginOrNull::try_from_value) + .ok_or_else(::Error::invalid) + } +} + +impl<'a> From<&'a OriginOrNull> for HeaderValue { + fn from(origin: &'a OriginOrNull) -> HeaderValue { + match origin { + OriginOrNull::Origin(ref scheme, ref auth) => { + let s = format!("{}://{}", scheme, auth); + let bytes = Bytes::from(s); + HeaderValue::from_maybe_shared(bytes) + .expect("Scheme and Authority are valid header values") + } + // Serialized as "null" per ASCII serialization of an origin + // https://html.spec.whatwg.org/multipage/browsers.html#ascii-serialisation-of-an-origin + OriginOrNull::Null => HeaderValue::from_static("null"), + } + } +} + +#[cfg(test)] +mod tests { + use super::super::{test_decode, test_encode}; + use super::*; + + #[test] + fn origin() { + let s = "http://web-platform.test:8000"; + let origin = test_decode::<Origin>(&[s]).unwrap(); + assert_eq!(origin.scheme(), "http"); + assert_eq!(origin.hostname(), "web-platform.test"); + assert_eq!(origin.port(), Some(8000)); + + let headers = test_encode(origin); + assert_eq!(headers["origin"], s); + } + + #[test] + fn null() { + assert_eq!(test_decode::<Origin>(&["null"]), Some(Origin::NULL),); + + let headers = test_encode(Origin::NULL); + assert_eq!(headers["origin"], "null"); + } +} diff --git a/third_party/rust/headers/src/common/pragma.rs b/third_party/rust/headers/src/common/pragma.rs new file mode 100644 index 0000000000..9b953978ff --- /dev/null +++ b/third_party/rust/headers/src/common/pragma.rs @@ -0,0 +1,61 @@ +use HeaderValue; + +/// The `Pragma` header defined by HTTP/1.0. +/// +/// > The "Pragma" header field allows backwards compatibility with +/// > HTTP/1.0 caches, so that clients can specify a "no-cache" request +/// > that they will understand (as Cache-Control was not defined until +/// > HTTP/1.1). When the Cache-Control header field is also present and +/// > understood in a request, Pragma is ignored. +/// > In HTTP/1.0, Pragma was defined as an extensible field for +/// > implementation-specified directives for recipients. This +/// > specification deprecates such extensions to improve interoperability. +/// +/// Spec: [https://tools.ietf.org/html/rfc7234#section-5.4][url] +/// +/// [url]: https://tools.ietf.org/html/rfc7234#section-5.4 +/// +/// # Examples +/// +/// ``` +/// # extern crate headers; +/// use headers::Pragma; +/// +/// let pragma = Pragma::no_cache(); +/// ``` +#[derive(Clone, Debug, PartialEq)] +pub struct Pragma(HeaderValue); + +derive_header! { + Pragma(_), + name: PRAGMA +} + +impl Pragma { + /// Construct the literal `no-cache` Pragma header. + pub fn no_cache() -> Pragma { + Pragma(HeaderValue::from_static("no-cache")) + } + + /// Return whether this pragma is `no-cache`. + pub fn is_no_cache(&self) -> bool { + self.0 == "no-cache" + } +} + +#[cfg(test)] +mod tests { + use super::super::test_decode; + use super::Pragma; + + #[test] + fn no_cache_is_no_cache() { + assert!(Pragma::no_cache().is_no_cache()); + } + + #[test] + fn etc_is_not_no_cache() { + let ext = test_decode::<Pragma>(&["dexter"]).unwrap(); + assert!(!ext.is_no_cache()); + } +} diff --git a/third_party/rust/headers/src/common/proxy_authorization.rs b/third_party/rust/headers/src/common/proxy_authorization.rs new file mode 100644 index 0000000000..50a6b0a4a8 --- /dev/null +++ b/third_party/rust/headers/src/common/proxy_authorization.rs @@ -0,0 +1,47 @@ +use super::authorization::{Authorization, Credentials}; + +/// `Proxy-Authorization` header, defined in [RFC7235](https://tools.ietf.org/html/rfc7235#section-4.4) +/// +/// The `Proxy-Authorization` header field allows a user agent to authenticate +/// itself with an HTTP proxy -- usually, but not necessarily, after +/// receiving a 407 (Proxy Authentication Required) response and the +/// `Proxy-Authenticate` header. Its value consists of credentials containing +/// the authentication information of the user agent for the realm of the +/// resource being requested. +/// +/// # ABNF +/// +/// ```text +/// Proxy-Authorization = credentials +/// ``` +/// +/// # Example values +/// * `Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==` +/// * `Bearer fpKL54jvWmEGVoRdCNjG` +/// +/// # Examples +/// +#[derive(Clone, PartialEq, Debug)] +pub struct ProxyAuthorization<C: Credentials>(pub C); + +impl<C: Credentials> ::Header for ProxyAuthorization<C> { + fn name() -> &'static ::HeaderName { + &::http::header::PROXY_AUTHORIZATION + } + + fn decode<'i, I: Iterator<Item = &'i ::HeaderValue>>(values: &mut I) -> Result<Self, ::Error> { + Authorization::decode(values).map(|auth| ProxyAuthorization(auth.0)) + } + + fn encode<E: Extend<::HeaderValue>>(&self, values: &mut E) { + let value = self.0.encode(); + debug_assert!( + value.as_bytes().starts_with(C::SCHEME.as_bytes()), + "Credentials::encode should include its scheme: scheme = {:?}, encoded = {:?}", + C::SCHEME, + value, + ); + + values.extend(::std::iter::once(value)); + } +} diff --git a/third_party/rust/headers/src/common/range.rs b/third_party/rust/headers/src/common/range.rs new file mode 100644 index 0000000000..29cc79d3c5 --- /dev/null +++ b/third_party/rust/headers/src/common/range.rs @@ -0,0 +1,418 @@ +use std::ops::{Bound, RangeBounds}; + +/// `Range` header, defined in [RFC7233](https://tools.ietf.org/html/rfc7233#section-3.1) +/// +/// The "Range" header field on a GET request modifies the method +/// semantics to request transfer of only one or more subranges of the +/// selected representation data, rather than the entire selected +/// representation data. +/// +/// # ABNF +/// +/// ```text +/// Range = byte-ranges-specifier / other-ranges-specifier +/// other-ranges-specifier = other-range-unit "=" other-range-set +/// other-range-set = 1*VCHAR +/// +/// bytes-unit = "bytes" +/// +/// byte-ranges-specifier = bytes-unit "=" byte-range-set +/// byte-range-set = 1#(byte-range-spec / suffix-byte-range-spec) +/// byte-range-spec = first-byte-pos "-" [last-byte-pos] +/// first-byte-pos = 1*DIGIT +/// last-byte-pos = 1*DIGIT +/// ``` +/// +/// # Example values +/// +/// * `bytes=1000-` +/// * `bytes=-2000` +/// * `bytes=0-1,30-40` +/// * `bytes=0-10,20-90,-100` +/// +/// # Examples +/// +/// ``` +/// # extern crate headers; +/// use headers::Range; +/// +/// +/// let range = Range::bytes(0..1234).unwrap(); +/// ``` +#[derive(Clone, Debug, PartialEq)] +pub struct Range(::HeaderValue); + +error_type!(InvalidRange); + +impl Range { + /// Creates a `Range` header from bounds. + pub fn bytes(bounds: impl RangeBounds<u64>) -> Result<Self, InvalidRange> { + let v = match (bounds.start_bound(), bounds.end_bound()) { + (Bound::Unbounded, Bound::Included(end)) => format!("bytes=-{}", end), + (Bound::Unbounded, Bound::Excluded(&end)) => format!("bytes=-{}", end - 1), + (Bound::Included(start), Bound::Included(end)) => format!("bytes={}-{}", start, end), + (Bound::Included(start), Bound::Excluded(&end)) => { + format!("bytes={}-{}", start, end - 1) + } + (Bound::Included(start), Bound::Unbounded) => format!("bytes={}-", start), + _ => return Err(InvalidRange { _inner: () }), + }; + + Ok(Range(::HeaderValue::from_str(&v).unwrap())) + } + + /// Iterate the range sets as a tuple of bounds. + pub fn iter<'a>(&'a self) -> impl Iterator<Item = (Bound<u64>, Bound<u64>)> + 'a { + let s = self + .0 + .to_str() + .expect("valid string checked in Header::decode()"); + + s["bytes=".len()..].split(',').filter_map(|spec| { + let mut iter = spec.trim().splitn(2, '-'); + Some((parse_bound(iter.next()?)?, parse_bound(iter.next()?)?)) + }) + } +} + +fn parse_bound(s: &str) -> Option<Bound<u64>> { + if s.is_empty() { + return Some(Bound::Unbounded); + } + + s.parse().ok().map(Bound::Included) +} + +impl ::Header for Range { + fn name() -> &'static ::HeaderName { + &::http::header::RANGE + } + + fn decode<'i, I: Iterator<Item = &'i ::HeaderValue>>(values: &mut I) -> Result<Self, ::Error> { + values + .next() + .and_then(|val| { + if val.to_str().ok()?.starts_with("bytes=") { + Some(Range(val.clone())) + } else { + None + } + }) + .ok_or_else(::Error::invalid) + } + + fn encode<E: Extend<::HeaderValue>>(&self, values: &mut E) { + values.extend(::std::iter::once(self.0.clone())); + } +} + +/* + +impl ByteRangeSpec { + /// Given the full length of the entity, attempt to normalize the byte range + /// into an satisfiable end-inclusive (from, to) range. + /// + /// The resulting range is guaranteed to be a satisfiable range within the bounds + /// of `0 <= from <= to < full_length`. + /// + /// If the byte range is deemed unsatisfiable, `None` is returned. + /// An unsatisfiable range is generally cause for a server to either reject + /// the client request with a `416 Range Not Satisfiable` status code, or to + /// simply ignore the range header and serve the full entity using a `200 OK` + /// status code. + /// + /// This function closely follows [RFC 7233][1] section 2.1. + /// As such, it considers ranges to be satisfiable if they meet the following + /// conditions: + /// + /// > If a valid byte-range-set includes at least one byte-range-spec with + /// a first-byte-pos that is less than the current length of the + /// representation, or at least one suffix-byte-range-spec with a + /// non-zero suffix-length, then the byte-range-set is satisfiable. + /// Otherwise, the byte-range-set is unsatisfiable. + /// + /// The function also computes remainder ranges based on the RFC: + /// + /// > If the last-byte-pos value is + /// absent, or if the value is greater than or equal to the current + /// length of the representation data, the byte range is interpreted as + /// the remainder of the representation (i.e., the server replaces the + /// value of last-byte-pos with a value that is one less than the current + /// length of the selected representation). + /// + /// [1]: https://tools.ietf.org/html/rfc7233 + pub fn to_satisfiable_range(&self, full_length: u64) -> Option<(u64, u64)> { + // If the full length is zero, there is no satisfiable end-inclusive range. + if full_length == 0 { + return None; + } + match self { + &ByteRangeSpec::FromTo(from, to) => { + if from < full_length && from <= to { + Some((from, ::std::cmp::min(to, full_length - 1))) + } else { + None + } + }, + &ByteRangeSpec::AllFrom(from) => { + if from < full_length { + Some((from, full_length - 1)) + } else { + None + } + }, + &ByteRangeSpec::Last(last) => { + if last > 0 { + // From the RFC: If the selected representation is shorter + // than the specified suffix-length, + // the entire representation is used. + if last > full_length { + Some((0, full_length - 1)) + } else { + Some((full_length - last, full_length - 1)) + } + } else { + None + } + } + } + } +} + +impl Range { + /// Get the most common byte range header ("bytes=from-to") + pub fn bytes(from: u64, to: u64) -> Range { + Range::Bytes(vec![ByteRangeSpec::FromTo(from, to)]) + } + + /// Get byte range header with multiple subranges + /// ("bytes=from1-to1,from2-to2,fromX-toX") + pub fn bytes_multi(ranges: Vec<(u64, u64)>) -> Range { + Range::Bytes(ranges.iter().map(|r| ByteRangeSpec::FromTo(r.0, r.1)).collect()) + } +} + + +impl fmt::Display for ByteRangeSpec { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match *self { + ByteRangeSpec::FromTo(from, to) => write!(f, "{}-{}", from, to), + ByteRangeSpec::Last(pos) => write!(f, "-{}", pos), + ByteRangeSpec::AllFrom(pos) => write!(f, "{}-", pos), + } + } +} + + +impl fmt::Display for Range { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match *self { + Range::Bytes(ref ranges) => { + try!(write!(f, "bytes=")); + + for (i, range) in ranges.iter().enumerate() { + if i != 0 { + try!(f.write_str(",")); + } + try!(Display::fmt(range, f)); + } + Ok(()) + }, + Range::Unregistered(ref unit, ref range_str) => { + write!(f, "{}={}", unit, range_str) + }, + } + } +} + +impl FromStr for Range { + type Err = ::Error; + + fn from_str(s: &str) -> ::Result<Range> { + let mut iter = s.splitn(2, '='); + + match (iter.next(), iter.next()) { + (Some("bytes"), Some(ranges)) => { + let ranges = from_comma_delimited(ranges); + if ranges.is_empty() { + return Err(::Error::Header); + } + Ok(Range::Bytes(ranges)) + } + (Some(unit), Some(range_str)) if unit != "" && range_str != "" => { + Ok(Range::Unregistered(unit.to_owned(), range_str.to_owned())) + + }, + _ => Err(::Error::Header) + } + } +} + +impl FromStr for ByteRangeSpec { + type Err = ::Error; + + fn from_str(s: &str) -> ::Result<ByteRangeSpec> { + let mut parts = s.splitn(2, '-'); + + match (parts.next(), parts.next()) { + (Some(""), Some(end)) => { + end.parse().or(Err(::Error::Header)).map(ByteRangeSpec::Last) + }, + (Some(start), Some("")) => { + start.parse().or(Err(::Error::Header)).map(ByteRangeSpec::AllFrom) + }, + (Some(start), Some(end)) => { + match (start.parse(), end.parse()) { + (Ok(start), Ok(end)) if start <= end => Ok(ByteRangeSpec::FromTo(start, end)), + _ => Err(::Error::Header) + } + }, + _ => Err(::Error::Header) + } + } +} + +fn from_comma_delimited<T: FromStr>(s: &str) -> Vec<T> { + s.split(',') + .filter_map(|x| match x.trim() { + "" => None, + y => Some(y) + }) + .filter_map(|x| x.parse().ok()) + .collect() +} + +impl Header for Range { + + fn header_name() -> &'static str { + static NAME: &'static str = "Range"; + NAME + } + + fn parse_header(raw: &Raw) -> ::Result<Range> { + from_one_raw_str(raw) + } + + fn fmt_header(&self, f: &mut ::Formatter) -> fmt::Result { + f.fmt_line(self) + } + +} + +#[test] +fn test_parse_bytes_range_valid() { + let r: Range = Header::parse_header(&"bytes=1-100".into()).unwrap(); + let r2: Range = Header::parse_header(&"bytes=1-100,-".into()).unwrap(); + let r3 = Range::bytes(1, 100); + assert_eq!(r, r2); + assert_eq!(r2, r3); + + let r: Range = Header::parse_header(&"bytes=1-100,200-".into()).unwrap(); + let r2: Range = Header::parse_header(&"bytes= 1-100 , 101-xxx, 200- ".into()).unwrap(); + let r3 = Range::Bytes( + vec![ByteRangeSpec::FromTo(1, 100), ByteRangeSpec::AllFrom(200)] + ); + assert_eq!(r, r2); + assert_eq!(r2, r3); + + let r: Range = Header::parse_header(&"bytes=1-100,-100".into()).unwrap(); + let r2: Range = Header::parse_header(&"bytes=1-100, ,,-100".into()).unwrap(); + let r3 = Range::Bytes( + vec![ByteRangeSpec::FromTo(1, 100), ByteRangeSpec::Last(100)] + ); + assert_eq!(r, r2); + assert_eq!(r2, r3); + + let r: Range = Header::parse_header(&"custom=1-100,-100".into()).unwrap(); + let r2 = Range::Unregistered("custom".to_owned(), "1-100,-100".to_owned()); + assert_eq!(r, r2); + +} + +#[test] +fn test_parse_unregistered_range_valid() { + let r: Range = Header::parse_header(&"custom=1-100,-100".into()).unwrap(); + let r2 = Range::Unregistered("custom".to_owned(), "1-100,-100".to_owned()); + assert_eq!(r, r2); + + let r: Range = Header::parse_header(&"custom=abcd".into()).unwrap(); + let r2 = Range::Unregistered("custom".to_owned(), "abcd".to_owned()); + assert_eq!(r, r2); + + let r: Range = Header::parse_header(&"custom=xxx-yyy".into()).unwrap(); + let r2 = Range::Unregistered("custom".to_owned(), "xxx-yyy".to_owned()); + assert_eq!(r, r2); +} + +#[test] +fn test_parse_invalid() { + let r: ::Result<Range> = Header::parse_header(&"bytes=1-a,-".into()); + assert_eq!(r.ok(), None); + + let r: ::Result<Range> = Header::parse_header(&"bytes=1-2-3".into()); + assert_eq!(r.ok(), None); + + let r: ::Result<Range> = Header::parse_header(&"abc".into()); + assert_eq!(r.ok(), None); + + let r: ::Result<Range> = Header::parse_header(&"bytes=1-100=".into()); + assert_eq!(r.ok(), None); + + let r: ::Result<Range> = Header::parse_header(&"bytes=".into()); + assert_eq!(r.ok(), None); + + let r: ::Result<Range> = Header::parse_header(&"custom=".into()); + assert_eq!(r.ok(), None); + + let r: ::Result<Range> = Header::parse_header(&"=1-100".into()); + assert_eq!(r.ok(), None); +} + +#[test] +fn test_fmt() { + use Headers; + + let mut headers = Headers::new(); + + headers.set( + Range::Bytes( + vec![ByteRangeSpec::FromTo(0, 1000), ByteRangeSpec::AllFrom(2000)] + )); + assert_eq!(&headers.to_string(), "Range: bytes=0-1000,2000-\r\n"); + + headers.clear(); + headers.set(Range::Bytes(vec![])); + + assert_eq!(&headers.to_string(), "Range: bytes=\r\n"); + + headers.clear(); + headers.set(Range::Unregistered("custom".to_owned(), "1-xxx".to_owned())); + + assert_eq!(&headers.to_string(), "Range: custom=1-xxx\r\n"); +} + +#[test] +fn test_byte_range_spec_to_satisfiable_range() { + assert_eq!(Some((0, 0)), ByteRangeSpec::FromTo(0, 0).to_satisfiable_range(3)); + assert_eq!(Some((1, 2)), ByteRangeSpec::FromTo(1, 2).to_satisfiable_range(3)); + assert_eq!(Some((1, 2)), ByteRangeSpec::FromTo(1, 5).to_satisfiable_range(3)); + assert_eq!(None, ByteRangeSpec::FromTo(3, 3).to_satisfiable_range(3)); + assert_eq!(None, ByteRangeSpec::FromTo(2, 1).to_satisfiable_range(3)); + assert_eq!(None, ByteRangeSpec::FromTo(0, 0).to_satisfiable_range(0)); + + assert_eq!(Some((0, 2)), ByteRangeSpec::AllFrom(0).to_satisfiable_range(3)); + assert_eq!(Some((2, 2)), ByteRangeSpec::AllFrom(2).to_satisfiable_range(3)); + assert_eq!(None, ByteRangeSpec::AllFrom(3).to_satisfiable_range(3)); + assert_eq!(None, ByteRangeSpec::AllFrom(5).to_satisfiable_range(3)); + assert_eq!(None, ByteRangeSpec::AllFrom(0).to_satisfiable_range(0)); + + assert_eq!(Some((1, 2)), ByteRangeSpec::Last(2).to_satisfiable_range(3)); + assert_eq!(Some((2, 2)), ByteRangeSpec::Last(1).to_satisfiable_range(3)); + assert_eq!(Some((0, 2)), ByteRangeSpec::Last(5).to_satisfiable_range(3)); + assert_eq!(None, ByteRangeSpec::Last(0).to_satisfiable_range(3)); + assert_eq!(None, ByteRangeSpec::Last(2).to_satisfiable_range(0)); +} + +bench_header!(bytes_multi, Range, { vec![b"bytes=1-1001,2001-3001,10001-".to_vec()]}); +bench_header!(custom_unit, Range, { vec![b"other=0-100000".to_vec()]}); +*/ diff --git a/third_party/rust/headers/src/common/referer.rs b/third_party/rust/headers/src/common/referer.rs new file mode 100644 index 0000000000..c85973b9cb --- /dev/null +++ b/third_party/rust/headers/src/common/referer.rs @@ -0,0 +1,60 @@ +use std::str::FromStr; + +use http::header::HeaderValue; + +/// `Referer` header, defined in +/// [RFC7231](http://tools.ietf.org/html/rfc7231#section-5.5.2) +/// +/// The `Referer` \[sic\] header field allows the user agent to specify a +/// URI reference for the resource from which the target URI was obtained +/// (i.e., the "referrer", though the field name is misspelled). A user +/// agent MUST NOT include the fragment and userinfo components of the +/// URI reference, if any, when generating the Referer field value. +/// +/// ## ABNF +/// +/// ```text +/// Referer = absolute-URI / partial-URI +/// ``` +/// +/// ## Example values +/// +/// * `http://www.example.org/hypertext/Overview.html` +/// +/// # Examples +/// +/// ``` +/// # extern crate headers; +/// use headers::Referer; +/// +/// let r = Referer::from_static("/People.html#tim"); +/// ``` +#[derive(Debug, Clone, PartialEq)] +pub struct Referer(HeaderValue); + +derive_header! { + Referer(_), + name: REFERER +} + +impl Referer { + /// Create a `Referer` with a static string. + /// + /// # Panic + /// + /// Panics if the string is not a legal header value. + pub fn from_static(s: &'static str) -> Referer { + Referer(HeaderValue::from_static(s)) + } +} + +error_type!(InvalidReferer); + +impl FromStr for Referer { + type Err = InvalidReferer; + fn from_str(src: &str) -> Result<Self, Self::Err> { + HeaderValue::from_str(src) + .map(Referer) + .map_err(|_| InvalidReferer { _inner: () }) + } +} diff --git a/third_party/rust/headers/src/common/referrer_policy.rs b/third_party/rust/headers/src/common/referrer_policy.rs new file mode 100644 index 0000000000..95bc36c056 --- /dev/null +++ b/third_party/rust/headers/src/common/referrer_policy.rs @@ -0,0 +1,190 @@ +use HeaderValue; + +/// `Referrer-Policy` header, part of +/// [Referrer Policy](https://www.w3.org/TR/referrer-policy/#referrer-policy-header) +/// +/// The `Referrer-Policy` HTTP header specifies the referrer +/// policy that the user agent applies when determining what +/// referrer information should be included with requests made, +/// and with browsing contexts created from the context of the +/// protected resource. +/// +/// # ABNF +/// +/// ```text +/// Referrer-Policy: 1#policy-token +/// policy-token = "no-referrer" / "no-referrer-when-downgrade" +/// / "same-origin" / "origin" +/// / "origin-when-cross-origin" / "unsafe-url" +/// ``` +/// +/// # Example values +/// +/// * `no-referrer` +/// +/// # Example +/// +/// ``` +/// # extern crate headers; +/// use headers::ReferrerPolicy; +/// +/// let rp = ReferrerPolicy::NO_REFERRER; +/// ``` +#[derive(Clone, Debug, PartialEq, Eq, Hash)] +pub struct ReferrerPolicy(Policy); + +derive_header! { + ReferrerPolicy(_), + name: REFERRER_POLICY +} + +#[derive(Clone, Debug, PartialEq, Eq, Hash)] +enum Policy { + NoReferrer, + NoReferrerWhenDowngrade, + SameOrigin, + Origin, + OriginWhenCrossOrigin, + UnsafeUrl, + StrictOrigin, + StrictOriginWhenCrossOrigin, +} + +impl ReferrerPolicy { + /// `no-referrer` + pub const NO_REFERRER: Self = ReferrerPolicy(Policy::NoReferrer); + + /// `no-referrer-when-downgrade` + pub const NO_REFERRER_WHEN_DOWNGRADE: Self = ReferrerPolicy(Policy::NoReferrerWhenDowngrade); + + /// `same-origin` + pub const SAME_ORIGIN: Self = ReferrerPolicy(Policy::SameOrigin); + + /// `origin` + pub const ORIGIN: Self = ReferrerPolicy(Policy::Origin); + + /// `origin-when-cross-origin` + pub const ORIGIN_WHEN_CROSS_ORIGIN: Self = ReferrerPolicy(Policy::OriginWhenCrossOrigin); + + /// `unsafe-url` + pub const UNSAFE_URL: Self = ReferrerPolicy(Policy::UnsafeUrl); + + /// `strict-origin` + pub const STRICT_ORIGIN: Self = ReferrerPolicy(Policy::StrictOrigin); + + ///`strict-origin-when-cross-origin` + pub const STRICT_ORIGIN_WHEN_CROSS_ORIGIN: Self = + ReferrerPolicy(Policy::StrictOriginWhenCrossOrigin); +} + +impl ::util::TryFromValues for Policy { + fn try_from_values<'i, I>(values: &mut I) -> Result<Self, ::Error> + where + I: Iterator<Item = &'i HeaderValue>, + { + // See https://www.w3.org/TR/referrer-policy/#determine-policy-for-token + // tl;dr - Pick *last* known policy in the list + let mut known = None; + for s in csv(values) { + known = Some(match s { + "no-referrer" | "never" => Policy::NoReferrer, + "no-referrer-when-downgrade" | "default" => Policy::NoReferrerWhenDowngrade, + "same-origin" => Policy::SameOrigin, + "origin" => Policy::Origin, + "origin-when-cross-origin" => Policy::OriginWhenCrossOrigin, + "strict-origin" => Policy::StrictOrigin, + "strict-origin-when-cross-origin" => Policy::StrictOriginWhenCrossOrigin, + "unsafe-url" | "always" => Policy::UnsafeUrl, + _ => continue, + }); + } + + known.ok_or_else(::Error::invalid) + } +} + +impl<'a> From<&'a Policy> for HeaderValue { + fn from(policy: &'a Policy) -> HeaderValue { + HeaderValue::from_static(match *policy { + Policy::NoReferrer => "no-referrer", + Policy::NoReferrerWhenDowngrade => "no-referrer-when-downgrade", + Policy::SameOrigin => "same-origin", + Policy::Origin => "origin", + Policy::OriginWhenCrossOrigin => "origin-when-cross-origin", + Policy::StrictOrigin => "strict-origin", + Policy::StrictOriginWhenCrossOrigin => "strict-origin-when-cross-origin", + Policy::UnsafeUrl => "unsafe-url", + }) + } +} + +fn csv<'i, I>(values: I) -> impl Iterator<Item = &'i str> +where + I: Iterator<Item = &'i HeaderValue>, +{ + values.flat_map(|value| { + value.to_str().into_iter().flat_map(|string| { + string.split(',').filter_map(|x| match x.trim() { + "" => None, + y => Some(y), + }) + }) + }) +} + +#[cfg(test)] +mod tests { + use super::super::test_decode; + use super::ReferrerPolicy; + + #[test] + fn decode_as_last_policy() { + assert_eq!( + test_decode::<ReferrerPolicy>(&["same-origin, origin"]), + Some(ReferrerPolicy::ORIGIN), + ); + + assert_eq!( + test_decode::<ReferrerPolicy>(&["origin", "same-origin"]), + Some(ReferrerPolicy::SAME_ORIGIN), + ); + } + + #[test] + fn decode_as_last_known() { + assert_eq!( + test_decode::<ReferrerPolicy>(&["origin, nope, nope, nope"]), + Some(ReferrerPolicy::ORIGIN), + ); + + assert_eq!( + test_decode::<ReferrerPolicy>(&["nope, origin, nope, nope"]), + Some(ReferrerPolicy::ORIGIN), + ); + + assert_eq!( + test_decode::<ReferrerPolicy>(&["nope, origin", "nope, nope"]), + Some(ReferrerPolicy::ORIGIN), + ); + + assert_eq!( + test_decode::<ReferrerPolicy>(&["nope", "origin", "nope, nope"]), + Some(ReferrerPolicy::ORIGIN), + ); + } + + #[test] + fn decode_unknown() { + assert_eq!(test_decode::<ReferrerPolicy>(&["nope"]), None,); + } + + #[test] + fn matching() { + let rp = ReferrerPolicy::ORIGIN; + + match rp { + ReferrerPolicy::ORIGIN => (), + _ => panic!("matched wrong"), + } + } +} diff --git a/third_party/rust/headers/src/common/retry_after.rs b/third_party/rust/headers/src/common/retry_after.rs new file mode 100644 index 0000000000..ec67aaf7da --- /dev/null +++ b/third_party/rust/headers/src/common/retry_after.rs @@ -0,0 +1,111 @@ +use std::time::{Duration, SystemTime}; + +use util::{HttpDate, Seconds, TryFromValues}; +use HeaderValue; + +/// The `Retry-After` header. +/// +/// The `Retry-After` response-header field can be used with a 503 (Service +/// Unavailable) response to indicate how long the service is expected to be +/// unavailable to the requesting client. This field MAY also be used with any +/// 3xx (Redirection) response to indicate the minimum time the user-agent is +/// asked wait before issuing the redirected request. The value of this field +/// can be either an HTTP-date or an integer number of seconds (in decimal) +/// after the time of the response. +/// +/// # Examples +/// ``` +/// # extern crate headers; +/// use std::time::{Duration, SystemTime}; +/// use headers::RetryAfter; +/// +/// let delay = RetryAfter::delay(Duration::from_secs(300)); +/// let date = RetryAfter::date(SystemTime::now()); +/// ``` + +/// Retry-After header, defined in [RFC7231](http://tools.ietf.org/html/rfc7231#section-7.1.3) +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct RetryAfter(After); + +derive_header! { + RetryAfter(_), + name: RETRY_AFTER +} + +#[derive(Debug, Clone, PartialEq, Eq)] +enum After { + /// Retry after the given DateTime + DateTime(HttpDate), + /// Retry after this duration has elapsed + Delay(Seconds), +} + +impl RetryAfter { + /// Create an `RetryAfter` header with a date value. + pub fn date(time: SystemTime) -> RetryAfter { + RetryAfter(After::DateTime(time.into())) + } + + /// Create an `RetryAfter` header with a date value. + pub fn delay(dur: Duration) -> RetryAfter { + RetryAfter(After::Delay(dur.into())) + } +} + +impl TryFromValues for After { + fn try_from_values<'i, I>(values: &mut I) -> Result<Self, ::Error> + where + I: Iterator<Item = &'i HeaderValue>, + { + values + .next() + .and_then(|val| { + if let Some(delay) = Seconds::from_val(val) { + return Some(After::Delay(delay)); + } + + let date = HttpDate::from_val(val)?; + Some(After::DateTime(date)) + }) + .ok_or_else(::Error::invalid) + } +} + +impl<'a> From<&'a After> for HeaderValue { + fn from(after: &'a After) -> HeaderValue { + match *after { + After::Delay(ref delay) => delay.into(), + After::DateTime(ref date) => date.into(), + } + } +} + +#[cfg(test)] +mod tests { + use super::super::test_decode; + use super::RetryAfter; + use std::time::Duration; + use util::HttpDate; + + #[test] + fn delay_decode() { + let r: RetryAfter = test_decode(&["1234"]).unwrap(); + assert_eq!(r, RetryAfter::delay(Duration::from_secs(1234)),); + } + + macro_rules! test_retry_after_datetime { + ($name:ident, $s:expr) => { + #[test] + fn $name() { + let r: RetryAfter = test_decode(&[$s]).unwrap(); + let dt = "Sun, 06 Nov 1994 08:49:37 GMT".parse::<HttpDate>().unwrap(); + + assert_eq!(r, RetryAfter(super::After::DateTime(dt))); + } + }; + } + + test_retry_after_datetime!(date_decode_rfc1123, "Sun, 06 Nov 1994 08:49:37 GMT"); + test_retry_after_datetime!(date_decode_rfc850, "Sunday, 06-Nov-94 08:49:37 GMT"); + test_retry_after_datetime!(date_decode_asctime, "Sun Nov 6 08:49:37 1994"); +} diff --git a/third_party/rust/headers/src/common/sec_websocket_accept.rs b/third_party/rust/headers/src/common/sec_websocket_accept.rs new file mode 100644 index 0000000000..9e9176f057 --- /dev/null +++ b/third_party/rust/headers/src/common/sec_websocket_accept.rs @@ -0,0 +1,66 @@ +use base64; +use bytes::Bytes; +use sha1::{Digest, Sha1}; + +use super::SecWebsocketKey; + +/// The `Sec-Websocket-Accept` header. +/// +/// This header is used in the Websocket handshake, sent back by the +/// server indicating a successful handshake. It is a signature +/// of the `Sec-Websocket-Key` header. +/// +/// # Example +/// +/// ```no_run +/// # extern crate headers; +/// use headers::{SecWebsocketAccept, SecWebsocketKey}; +/// +/// let sec_key: SecWebsocketKey = /* from request headers */ +/// # unimplemented!(); +/// +/// let sec_accept = SecWebsocketAccept::from(sec_key); +/// ``` +#[derive(Clone, Debug, PartialEq, Eq, Hash)] +pub struct SecWebsocketAccept(::HeaderValue); + +derive_header! { + SecWebsocketAccept(_), + name: SEC_WEBSOCKET_ACCEPT +} + +impl From<SecWebsocketKey> for SecWebsocketAccept { + fn from(key: SecWebsocketKey) -> SecWebsocketAccept { + sign(key.0.as_bytes()) + } +} + +fn sign(key: &[u8]) -> SecWebsocketAccept { + let mut sha1 = Sha1::default(); + sha1.update(key); + sha1.update(&b"258EAFA5-E914-47DA-95CA-C5AB0DC85B11"[..]); + let b64 = Bytes::from(base64::encode(&sha1.finalize())); + + let val = ::HeaderValue::from_maybe_shared(b64).expect("base64 is a valid value"); + + SecWebsocketAccept(val) +} + +#[cfg(test)] +mod tests { + use super::super::{test_decode, test_encode}; + use super::*; + + #[test] + fn key_to_accept() { + // From https://tools.ietf.org/html/rfc6455#section-1.2 + let key = test_decode::<SecWebsocketKey>(&["dGhlIHNhbXBsZSBub25jZQ=="]).expect("key"); + let accept = SecWebsocketAccept::from(key); + let headers = test_encode(accept); + + assert_eq!( + headers["sec-websocket-accept"], + "s3pPLMBiTxaQ9kYGzzhZRbK+xOo=" + ); + } +} diff --git a/third_party/rust/headers/src/common/sec_websocket_key.rs b/third_party/rust/headers/src/common/sec_websocket_key.rs new file mode 100644 index 0000000000..2c12dda4b2 --- /dev/null +++ b/third_party/rust/headers/src/common/sec_websocket_key.rs @@ -0,0 +1,8 @@ +/// The `Sec-Websocket-Key` header. +#[derive(Clone, Debug, PartialEq, Eq, Hash)] +pub struct SecWebsocketKey(pub(super) ::HeaderValue); + +derive_header! { + SecWebsocketKey(_), + name: SEC_WEBSOCKET_KEY +} diff --git a/third_party/rust/headers/src/common/sec_websocket_version.rs b/third_party/rust/headers/src/common/sec_websocket_version.rs new file mode 100644 index 0000000000..d20c49c709 --- /dev/null +++ b/third_party/rust/headers/src/common/sec_websocket_version.rs @@ -0,0 +1,58 @@ +/// The `Sec-Websocket-Version` header. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +pub struct SecWebsocketVersion(u8); + +impl SecWebsocketVersion { + /// `Sec-Websocket-Version: 13` + pub const V13: SecWebsocketVersion = SecWebsocketVersion(13); +} + +impl ::Header for SecWebsocketVersion { + fn name() -> &'static ::HeaderName { + &::http::header::SEC_WEBSOCKET_VERSION + } + + fn decode<'i, I: Iterator<Item = &'i ::HeaderValue>>(values: &mut I) -> Result<Self, ::Error> { + values + .next() + .and_then(|value| { + if value == "13" { + Some(SecWebsocketVersion::V13) + } else { + None + } + }) + .ok_or_else(::Error::invalid) + } + + fn encode<E: Extend<::HeaderValue>>(&self, values: &mut E) { + debug_assert_eq!(self.0, 13); + + values.extend(::std::iter::once(::HeaderValue::from_static("13"))); + } +} + +#[cfg(test)] +mod tests { + use super::super::{test_decode, test_encode}; + use super::SecWebsocketVersion; + + #[test] + fn decode_v13() { + assert_eq!( + test_decode::<SecWebsocketVersion>(&["13"]), + Some(SecWebsocketVersion::V13), + ); + } + + #[test] + fn decode_fail() { + assert_eq!(test_decode::<SecWebsocketVersion>(&["1"]), None,); + } + + #[test] + fn encode_v13() { + let headers = test_encode(SecWebsocketVersion::V13); + assert_eq!(headers["sec-websocket-version"], "13"); + } +} diff --git a/third_party/rust/headers/src/common/server.rs b/third_party/rust/headers/src/common/server.rs new file mode 100644 index 0000000000..b0e8f88099 --- /dev/null +++ b/third_party/rust/headers/src/common/server.rs @@ -0,0 +1,72 @@ +use std::fmt; +use std::str::FromStr; + +use util::HeaderValueString; + +/// `Server` header, defined in [RFC7231](http://tools.ietf.org/html/rfc7231#section-7.4.2) +/// +/// The `Server` header field contains information about the software +/// used by the origin server to handle the request, which is often used +/// by clients to help identify the scope of reported interoperability +/// problems, to work around or tailor requests to avoid particular +/// server limitations, and for analytics regarding server or operating +/// system use. An origin server MAY generate a Server field in its +/// responses. +/// +/// # ABNF +/// +/// ```text +/// Server = product *( RWS ( product / comment ) ) +/// ``` +/// +/// # Example values +/// * `CERN/3.0 libwww/2.17` +/// +/// # Example +/// +/// ``` +/// # extern crate headers; +/// use headers::Server; +/// +/// let server = Server::from_static("hyper/0.12.2"); +/// ``` +#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct Server(HeaderValueString); + +derive_header! { + Server(_), + name: SERVER +} + +impl Server { + /// Construct a `Server` from a static string. + /// + /// # Panic + /// + /// Panics if the static string is not a legal header value. + pub fn from_static(s: &'static str) -> Server { + Server(HeaderValueString::from_static(s)) + } + + /// View this `Server` as a `&str`. + pub fn as_str(&self) -> &str { + self.0.as_str() + } +} + +error_type!(InvalidServer); + +impl FromStr for Server { + type Err = InvalidServer; + fn from_str(src: &str) -> Result<Self, Self::Err> { + HeaderValueString::from_str(src) + .map(Server) + .map_err(|_| InvalidServer { _inner: () }) + } +} + +impl fmt::Display for Server { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fmt::Display::fmt(&self.0, f) + } +} diff --git a/third_party/rust/headers/src/common/set_cookie.rs b/third_party/rust/headers/src/common/set_cookie.rs new file mode 100644 index 0000000000..32876ac7d8 --- /dev/null +++ b/third_party/rust/headers/src/common/set_cookie.rs @@ -0,0 +1,103 @@ +/// `Set-Cookie` header, defined [RFC6265](http://tools.ietf.org/html/rfc6265#section-4.1) +/// +/// The Set-Cookie HTTP response header is used to send cookies from the +/// server to the user agent. +/// +/// Informally, the Set-Cookie response header contains the header name +/// "Set-Cookie" followed by a ":" and a cookie. Each cookie begins with +/// a name-value-pair, followed by zero or more attribute-value pairs. +/// +/// # ABNF +/// +/// ```text +/// set-cookie-header = "Set-Cookie:" SP set-cookie-string +/// set-cookie-string = cookie-pair *( ";" SP cookie-av ) +/// cookie-pair = cookie-name "=" cookie-value +/// cookie-name = token +/// cookie-value = *cookie-octet / ( DQUOTE *cookie-octet DQUOTE ) +/// cookie-octet = %x21 / %x23-2B / %x2D-3A / %x3C-5B / %x5D-7E +/// ; US-ASCII characters excluding CTLs, +/// ; whitespace DQUOTE, comma, semicolon, +/// ; and backslash +/// token = <token, defined in [RFC2616], Section 2.2> +/// +/// cookie-av = expires-av / max-age-av / domain-av / +/// path-av / secure-av / httponly-av / +/// extension-av +/// expires-av = "Expires=" sane-cookie-date +/// sane-cookie-date = <rfc1123-date, defined in [RFC2616], Section 3.3.1> +/// max-age-av = "Max-Age=" non-zero-digit *DIGIT +/// ; In practice, both expires-av and max-age-av +/// ; are limited to dates representable by the +/// ; user agent. +/// non-zero-digit = %x31-39 +/// ; digits 1 through 9 +/// domain-av = "Domain=" domain-value +/// domain-value = <subdomain> +/// ; defined in [RFC1034], Section 3.5, as +/// ; enhanced by [RFC1123], Section 2.1 +/// path-av = "Path=" path-value +/// path-value = <any CHAR except CTLs or ";"> +/// secure-av = "Secure" +/// httponly-av = "HttpOnly" +/// extension-av = <any CHAR except CTLs or ";"> +/// ``` +/// +/// # Example values +/// +/// * `SID=31d4d96e407aad42` +/// * `lang=en-US; Expires=Wed, 09 Jun 2021 10:18:14 GMT` +/// * `lang=; Expires=Sun, 06 Nov 1994 08:49:37 GMT` +/// * `lang=en-US; Path=/; Domain=example.com` +/// +/// # Example +#[derive(Clone, Debug)] +pub struct SetCookie(Vec<::HeaderValue>); + +impl ::Header for SetCookie { + fn name() -> &'static ::HeaderName { + &::http::header::SET_COOKIE + } + + fn decode<'i, I: Iterator<Item = &'i ::HeaderValue>>(values: &mut I) -> Result<Self, ::Error> { + let vec = values.cloned().collect::<Vec<_>>(); + + if !vec.is_empty() { + Ok(SetCookie(vec)) + } else { + Err(::Error::invalid()) + } + } + + fn encode<E: Extend<::HeaderValue>>(&self, values: &mut E) { + values.extend(self.0.iter().cloned()); + } +} + +#[cfg(test)] +mod tests { + use super::super::{test_decode, test_encode}; + use super::*; + + #[test] + fn decode() { + let set_cookie = test_decode::<SetCookie>(&["foo=bar", "baz=quux"]).unwrap(); + assert_eq!(set_cookie.0.len(), 2); + assert_eq!(set_cookie.0[0], "foo=bar"); + assert_eq!(set_cookie.0[1], "baz=quux"); + } + + #[test] + fn encode() { + let set_cookie = SetCookie(vec![ + ::HeaderValue::from_static("foo=bar"), + ::HeaderValue::from_static("baz=quux"), + ]); + + let headers = test_encode(set_cookie); + let mut vals = headers.get_all("set-cookie").into_iter(); + assert_eq!(vals.next().unwrap(), "foo=bar"); + assert_eq!(vals.next().unwrap(), "baz=quux"); + assert_eq!(vals.next(), None); + } +} diff --git a/third_party/rust/headers/src/common/strict_transport_security.rs b/third_party/rust/headers/src/common/strict_transport_security.rs new file mode 100644 index 0000000000..628b800438 --- /dev/null +++ b/third_party/rust/headers/src/common/strict_transport_security.rs @@ -0,0 +1,247 @@ +use std::fmt; +use std::time::Duration; + +use util::{self, IterExt, Seconds}; + +/// `StrictTransportSecurity` header, defined in [RFC6797](https://tools.ietf.org/html/rfc6797) +/// +/// This specification defines a mechanism enabling web sites to declare +/// themselves accessible only via secure connections and/or for users to be +/// able to direct their user agent(s) to interact with given sites only over +/// secure connections. This overall policy is referred to as HTTP Strict +/// Transport Security (HSTS). The policy is declared by web sites via the +/// Strict-Transport-Security HTTP response header field and/or by other means, +/// such as user agent configuration, for example. +/// +/// # ABNF +/// +/// ```text +/// [ directive ] *( ";" [ directive ] ) +/// +/// directive = directive-name [ "=" directive-value ] +/// directive-name = token +/// directive-value = token | quoted-string +/// +/// ``` +/// +/// # Example values +/// +/// * `max-age=31536000` +/// * `max-age=15768000 ; includeSubdomains` +/// +/// # Example +/// +/// ``` +/// # extern crate headers; +/// use std::time::Duration; +/// use headers::StrictTransportSecurity; +/// +/// let sts = StrictTransportSecurity::including_subdomains(Duration::from_secs(31_536_000)); +/// ``` +#[derive(Clone, Debug, PartialEq)] +pub struct StrictTransportSecurity { + /// Signals the UA that the HSTS Policy applies to this HSTS Host as well as + /// any subdomains of the host's domain name. + include_subdomains: bool, + + /// Specifies the number of seconds, after the reception of the STS header + /// field, during which the UA regards the host (from whom the message was + /// received) as a Known HSTS Host. + max_age: Seconds, +} + +impl StrictTransportSecurity { + // NOTE: The two constructors exist to make a user *have* to decide if + // subdomains can be included or not, instead of forgetting due to an + // incorrect assumption about a default. + + /// Create an STS header that includes subdomains + pub fn including_subdomains(max_age: Duration) -> StrictTransportSecurity { + StrictTransportSecurity { + max_age: max_age.into(), + include_subdomains: true, + } + } + + /// Create an STS header that excludes subdomains + pub fn excluding_subdomains(max_age: Duration) -> StrictTransportSecurity { + StrictTransportSecurity { + max_age: max_age.into(), + include_subdomains: false, + } + } + + // getters + + /// Get whether this should include subdomains. + pub fn include_subdomains(&self) -> bool { + self.include_subdomains + } + + /// Get the max-age. + pub fn max_age(&self) -> Duration { + self.max_age.into() + } +} + +enum Directive { + MaxAge(u64), + IncludeSubdomains, + Unknown, +} + +fn from_str(s: &str) -> Result<StrictTransportSecurity, ::Error> { + s.split(';') + .map(str::trim) + .map(|sub| { + if sub.eq_ignore_ascii_case("includeSubdomains") { + Some(Directive::IncludeSubdomains) + } else { + let mut sub = sub.splitn(2, '='); + match (sub.next(), sub.next()) { + (Some(left), Some(right)) if left.trim().eq_ignore_ascii_case("max-age") => { + right + .trim() + .trim_matches('"') + .parse() + .ok() + .map(Directive::MaxAge) + } + _ => Some(Directive::Unknown), + } + } + }) + .fold(Some((None, None)), |res, dir| match (res, dir) { + (Some((None, sub)), Some(Directive::MaxAge(age))) => Some((Some(age), sub)), + (Some((age, None)), Some(Directive::IncludeSubdomains)) => Some((age, Some(()))), + (Some((Some(_), _)), Some(Directive::MaxAge(_))) + | (Some((_, Some(_))), Some(Directive::IncludeSubdomains)) + | (_, None) => None, + (res, _) => res, + }) + .and_then(|res| match res { + (Some(age), sub) => Some(StrictTransportSecurity { + max_age: Duration::from_secs(age).into(), + include_subdomains: sub.is_some(), + }), + _ => None, + }) + .ok_or_else(::Error::invalid) +} + +impl ::Header for StrictTransportSecurity { + fn name() -> &'static ::HeaderName { + &::http::header::STRICT_TRANSPORT_SECURITY + } + + fn decode<'i, I: Iterator<Item = &'i ::HeaderValue>>(values: &mut I) -> Result<Self, ::Error> { + values + .just_one() + .and_then(|v| v.to_str().ok()) + .map(from_str) + .unwrap_or_else(|| Err(::Error::invalid())) + } + + fn encode<E: Extend<::HeaderValue>>(&self, values: &mut E) { + struct Adapter<'a>(&'a StrictTransportSecurity); + + impl<'a> fmt::Display for Adapter<'a> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + if self.0.include_subdomains { + write!(f, "max-age={}; includeSubdomains", self.0.max_age) + } else { + write!(f, "max-age={}", self.0.max_age) + } + } + } + + values.extend(::std::iter::once(util::fmt(Adapter(self)))); + } +} + +#[cfg(test)] +mod tests { + use super::super::test_decode; + use super::StrictTransportSecurity; + use std::time::Duration; + + #[test] + fn test_parse_max_age() { + let h = test_decode::<StrictTransportSecurity>(&["max-age=31536000"]).unwrap(); + assert_eq!( + h, + StrictTransportSecurity { + include_subdomains: false, + max_age: Duration::from_secs(31536000).into(), + } + ); + } + + #[test] + fn test_parse_max_age_no_value() { + assert_eq!(test_decode::<StrictTransportSecurity>(&["max-age"]), None,); + } + + #[test] + fn test_parse_quoted_max_age() { + let h = test_decode::<StrictTransportSecurity>(&["max-age=\"31536000\""]).unwrap(); + assert_eq!( + h, + StrictTransportSecurity { + include_subdomains: false, + max_age: Duration::from_secs(31536000).into(), + } + ); + } + + #[test] + fn test_parse_spaces_max_age() { + let h = test_decode::<StrictTransportSecurity>(&["max-age = 31536000"]).unwrap(); + assert_eq!( + h, + StrictTransportSecurity { + include_subdomains: false, + max_age: Duration::from_secs(31536000).into(), + } + ); + } + + #[test] + fn test_parse_include_subdomains() { + let h = test_decode::<StrictTransportSecurity>(&["max-age=15768000 ; includeSubDomains"]) + .unwrap(); + assert_eq!( + h, + StrictTransportSecurity { + include_subdomains: true, + max_age: Duration::from_secs(15768000).into(), + } + ); + } + + #[test] + fn test_parse_no_max_age() { + assert_eq!( + test_decode::<StrictTransportSecurity>(&["includeSubdomains"]), + None, + ); + } + + #[test] + fn test_parse_max_age_nan() { + assert_eq!( + test_decode::<StrictTransportSecurity>(&["max-age = izzy"]), + None, + ); + } + + #[test] + fn test_parse_duplicate_directives() { + assert_eq!( + test_decode::<StrictTransportSecurity>(&["max-age=1; max-age=2"]), + None, + ); + } +} + +//bench_header!(bench, StrictTransportSecurity, { vec![b"max-age=15768000 ; includeSubDomains".to_vec()] }); diff --git a/third_party/rust/headers/src/common/te.rs b/third_party/rust/headers/src/common/te.rs new file mode 100644 index 0000000000..9a471b8a6f --- /dev/null +++ b/third_party/rust/headers/src/common/te.rs @@ -0,0 +1,41 @@ +use util::FlatCsv; + +/// `TE` header, defined in +/// [RFC7230](http://tools.ietf.org/html/rfc7230#section-4.3) +/// +/// As RFC7230 states, "The "TE" header field in a request indicates what transfer codings, +/// besides chunked, the client is willing to accept in response, and +/// whether or not the client is willing to accept trailer fields in a +/// chunked transfer coding." +/// +/// For HTTP/1.1 compliant clients `chunked` transfer codings are assumed to be acceptable and +/// so should never appear in this header. +/// +/// # ABNF +/// +/// ```text +/// TE = "TE" ":" #( t-codings ) +/// t-codings = "trailers" | ( transfer-extension [ accept-params ] ) +/// ``` +/// +/// # Example values +/// * `trailers` +/// * `trailers, deflate;q=0.5` +/// * `` +/// +/// # Examples +/// +#[derive(Clone, Debug, PartialEq)] +pub struct Te(FlatCsv); + +derive_header! { + Te(_), + name: TE +} + +impl Te { + /// Create a `TE: trailers` header. + pub fn trailers() -> Self { + Te(::HeaderValue::from_static("trailers").into()) + } +} diff --git a/third_party/rust/headers/src/common/transfer_encoding.rs b/third_party/rust/headers/src/common/transfer_encoding.rs new file mode 100644 index 0000000000..c3c49b15f1 --- /dev/null +++ b/third_party/rust/headers/src/common/transfer_encoding.rs @@ -0,0 +1,104 @@ +use util::FlatCsv; +use HeaderValue; + +/// `Transfer-Encoding` header, defined in +/// [RFC7230](http://tools.ietf.org/html/rfc7230#section-3.3.1) +/// +/// The `Transfer-Encoding` header field lists the transfer coding names +/// corresponding to the sequence of transfer codings that have been (or +/// will be) applied to the payload body in order to form the message +/// body. +/// +/// Note that setting this header will *remove* any previously set +/// `Content-Length` header, in accordance with +/// [RFC7230](http://tools.ietf.org/html/rfc7230#section-3.3.2): +/// +/// > A sender MUST NOT send a Content-Length header field in any message +/// > that contains a Transfer-Encoding header field. +/// +/// # ABNF +/// +/// ```text +/// Transfer-Encoding = 1#transfer-coding +/// ``` +/// +/// # Example values +/// +/// * `chunked` +/// * `gzip, chunked` +/// +/// # Example +/// +/// ``` +/// # extern crate headers; +/// use headers::TransferEncoding; +/// +/// let transfer = TransferEncoding::chunked(); +/// ``` +// This currently is just a `HeaderValue`, instead of a `Vec<Encoding>`, since +// the most common by far instance is simply the string `chunked`. It'd be a +// waste to need to allocate just for that. +#[derive(Clone, Debug)] +pub struct TransferEncoding(FlatCsv); + +derive_header! { + TransferEncoding(_), + name: TRANSFER_ENCODING +} + +impl TransferEncoding { + /// Constructor for the most common Transfer-Encoding, `chunked`. + pub fn chunked() -> TransferEncoding { + TransferEncoding(HeaderValue::from_static("chunked").into()) + } + + /// Returns whether this ends with the `chunked` encoding. + pub fn is_chunked(&self) -> bool { + self.0 + .value + //TODO(perf): use split and trim (not an actual method) on &[u8] + .to_str() + .map(|s| { + s.split(',') + .next_back() + .map(|encoding| encoding.trim() == "chunked") + .expect("split always has at least 1 item") + }) + .unwrap_or(false) + } +} + +#[cfg(test)] +mod tests { + use super::super::test_decode; + use super::TransferEncoding; + + #[test] + fn chunked_is_chunked() { + assert!(TransferEncoding::chunked().is_chunked()); + } + + #[test] + fn decode_gzip_chunked_is_chunked() { + let te = test_decode::<TransferEncoding>(&["gzip, chunked"]).unwrap(); + assert!(te.is_chunked()); + } + + #[test] + fn decode_chunked_gzip_is_not_chunked() { + let te = test_decode::<TransferEncoding>(&["chunked, gzip"]).unwrap(); + assert!(!te.is_chunked()); + } + + #[test] + fn decode_notchunked_is_not_chunked() { + let te = test_decode::<TransferEncoding>(&["notchunked"]).unwrap(); + assert!(!te.is_chunked()); + } + + #[test] + fn decode_multiple_is_chunked() { + let te = test_decode::<TransferEncoding>(&["gzip", "chunked"]).unwrap(); + assert!(te.is_chunked()); + } +} diff --git a/third_party/rust/headers/src/common/upgrade.rs b/third_party/rust/headers/src/common/upgrade.rs new file mode 100644 index 0000000000..8002b68b23 --- /dev/null +++ b/third_party/rust/headers/src/common/upgrade.rs @@ -0,0 +1,55 @@ +use HeaderValue; + +/// `Upgrade` header, defined in [RFC7230](http://tools.ietf.org/html/rfc7230#section-6.7) +/// +/// The `Upgrade` header field is intended to provide a simple mechanism +/// for transitioning from HTTP/1.1 to some other protocol on the same +/// connection. A client MAY send a list of protocols in the Upgrade +/// header field of a request to invite the server to switch to one or +/// more of those protocols, in order of descending preference, before +/// sending the final response. A server MAY ignore a received Upgrade +/// header field if it wishes to continue using the current protocol on +/// that connection. Upgrade cannot be used to insist on a protocol +/// change. +/// +/// ## ABNF +/// +/// ```text +/// Upgrade = 1#protocol +/// +/// protocol = protocol-name ["/" protocol-version] +/// protocol-name = token +/// protocol-version = token +/// ``` +/// +/// ## Example values +/// +/// * `HTTP/2.0, SHTTP/1.3, IRC/6.9, RTA/x11` +/// +/// # Note +/// +/// In practice, the `Upgrade` header is never that complicated. In most cases, +/// it is only ever a single value, such as `"websocket"`. +/// +/// # Examples +/// +/// ``` +/// # extern crate headers; +/// use headers::Upgrade; +/// +/// let ws = Upgrade::websocket(); +/// ``` +#[derive(Clone, Debug, PartialEq)] +pub struct Upgrade(HeaderValue); + +derive_header! { + Upgrade(_), + name: UPGRADE +} + +impl Upgrade { + /// Constructs an `Upgrade: websocket` header. + pub fn websocket() -> Upgrade { + Upgrade(HeaderValue::from_static("websocket")) + } +} diff --git a/third_party/rust/headers/src/common/user_agent.rs b/third_party/rust/headers/src/common/user_agent.rs new file mode 100644 index 0000000000..fc1d8ba458 --- /dev/null +++ b/third_party/rust/headers/src/common/user_agent.rs @@ -0,0 +1,81 @@ +use std::fmt; +use std::str::FromStr; + +use util::HeaderValueString; + +/// `User-Agent` header, defined in +/// [RFC7231](http://tools.ietf.org/html/rfc7231#section-5.5.3) +/// +/// The `User-Agent` header field contains information about the user +/// agent originating the request, which is often used by servers to help +/// identify the scope of reported interoperability problems, to work +/// around or tailor responses to avoid particular user agent +/// limitations, and for analytics regarding browser or operating system +/// use. A user agent SHOULD send a User-Agent field in each request +/// unless specifically configured not to do so. +/// +/// # ABNF +/// +/// ```text +/// User-Agent = product *( RWS ( product / comment ) ) +/// product = token ["/" product-version] +/// product-version = token +/// ``` +/// +/// # Example values +/// +/// * `CERN-LineMode/2.15 libwww/2.17b3` +/// * `Bunnies` +/// +/// # Notes +/// +/// * The parser does not split the value +/// +/// # Example +/// +/// ``` +/// # extern crate headers; +/// use headers::UserAgent; +/// +/// let ua = UserAgent::from_static("hyper/0.12.2"); +/// ``` +#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct UserAgent(HeaderValueString); + +derive_header! { + UserAgent(_), + name: USER_AGENT +} + +impl UserAgent { + /// Create a `UserAgent` from a static string. + /// + /// # Panic + /// + /// Panics if the static string is not a legal header value. + pub fn from_static(src: &'static str) -> UserAgent { + UserAgent(HeaderValueString::from_static(src)) + } + + /// View this `UserAgent` as a `&str`. + pub fn as_str(&self) -> &str { + self.0.as_str() + } +} + +error_type!(InvalidUserAgent); + +impl FromStr for UserAgent { + type Err = InvalidUserAgent; + fn from_str(src: &str) -> Result<Self, Self::Err> { + HeaderValueString::from_str(src) + .map(UserAgent) + .map_err(|_| InvalidUserAgent { _inner: () }) + } +} + +impl fmt::Display for UserAgent { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fmt::Display::fmt(&self.0, f) + } +} diff --git a/third_party/rust/headers/src/common/vary.rs b/third_party/rust/headers/src/common/vary.rs new file mode 100644 index 0000000000..32b21fe7db --- /dev/null +++ b/third_party/rust/headers/src/common/vary.rs @@ -0,0 +1,84 @@ +use util::FlatCsv; + +use HeaderValue; + +/// `Vary` header, defined in [RFC7231](https://tools.ietf.org/html/rfc7231#section-7.1.4) +/// +/// The "Vary" header field in a response describes what parts of a +/// request message, aside from the method, Host header field, and +/// request target, might influence the origin server's process for +/// selecting and representing this response. The value consists of +/// either a single asterisk ("*") or a list of header field names +/// (case-insensitive). +/// +/// # ABNF +/// +/// ```text +/// Vary = "*" / 1#field-name +/// ``` +/// +/// # Example values +/// +/// * `accept-encoding, accept-language` +/// +/// # Example +/// +/// ``` +/// # extern crate headers; +/// use headers::Vary; +/// +/// let vary = Vary::any(); +/// ``` +#[derive(Debug, Clone, PartialEq)] +pub struct Vary(FlatCsv); + +derive_header! { + Vary(_), + name: VARY +} + +impl Vary { + /// Create a new `Very: *` header. + pub fn any() -> Vary { + Vary(HeaderValue::from_static("*").into()) + } + + /// Check if this includes `*`. + pub fn is_any(&self) -> bool { + self.0.iter().any(|val| val == "*") + } + + /// Iterate the header names of this `Vary`. + pub fn iter_strs(&self) -> impl Iterator<Item = &str> { + self.0.iter() + } +} + +/* +test_vary { + test_header!(test1, vec![b"accept-encoding, accept-language"]); + + #[test] + fn test2() { + let mut vary: ::Result<Vary>; + + vary = Header::parse_header(&"*".into()); + assert_eq!(vary.ok(), Some(Vary::Any)); + + vary = Header::parse_header(&"etag,cookie,allow".into()); + assert_eq!(vary.ok(), Some(Vary::Items(vec!["eTag".parse().unwrap(), + "cookIE".parse().unwrap(), + "AlLOw".parse().unwrap(),]))); + } +} +*/ + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn any_is_any() { + assert!(Vary::any().is_any()); + } +} |