mirror of https://github.com/fafhrd91/actix-web
Merge branch 'master' into http/h2_handshake_timeout
This commit is contained in:
commit
e510677c70
14
CHANGES.md
14
CHANGES.md
|
@ -1,6 +1,20 @@
|
||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
## Unreleased - 2021-xx-xx
|
## Unreleased - 2021-xx-xx
|
||||||
|
### Added
|
||||||
|
* Methods on `AcceptLanguage`: `ranked` and `preference`. [#2480]
|
||||||
|
* `AcceptEncoding` typed header. [#2482]
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
* Rename `Accept::{mime_precedence => ranked}`. [#2480]
|
||||||
|
* Rename `Accept::{mime_preference => preference}`. [#2480]
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
* Accept wildcard `*` items in `AcceptLanguage`. [#2480]
|
||||||
|
* Typed headers containing lists that require one or more items now enforce this minimum. [#2482]
|
||||||
|
|
||||||
|
[#2480]: https://github.com/actix/actix-web/pull/2480
|
||||||
|
[#2482]: https://github.com/actix/actix-web/pull/2482
|
||||||
|
|
||||||
|
|
||||||
## 4.0.0-beta.13 - 2021-11-30
|
## 4.0.0-beta.13 - 2021-11-30
|
||||||
|
|
|
@ -55,7 +55,7 @@ pub trait Header: IntoHeaderValue {
|
||||||
fn name() -> HeaderName;
|
fn name() -> HeaderName;
|
||||||
|
|
||||||
/// Parse a header
|
/// Parse a header
|
||||||
fn parse<T: HttpMessage>(msg: &T) -> Result<Self, ParseError>;
|
fn parse<M: HttpMessage>(msg: &M) -> Result<Self, ParseError>;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Convert `http::HeaderMap` to our `HeaderMap`.
|
/// Convert `http::HeaderMap` to our `HeaderMap`.
|
||||||
|
|
|
@ -7,7 +7,7 @@ use self::Charset::*;
|
||||||
/// The string representation is normalized to upper case.
|
/// The string representation is normalized to upper case.
|
||||||
///
|
///
|
||||||
/// See <http://www.iana.org/assignments/character-sets/character-sets.xhtml>.
|
/// See <http://www.iana.org/assignments/character-sets/character-sets.xhtml>.
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
#[allow(non_camel_case_types)]
|
#[allow(non_camel_case_types)]
|
||||||
pub enum Charset {
|
pub enum Charset {
|
||||||
/// US ASCII
|
/// US ASCII
|
||||||
|
|
|
@ -31,7 +31,7 @@ pub struct ExtendedValue {
|
||||||
///
|
///
|
||||||
/// ## ABNF
|
/// ## ABNF
|
||||||
///
|
///
|
||||||
/// ```text
|
/// ```plain
|
||||||
/// ext-value = charset "'" [ language ] "'" value-chars
|
/// ext-value = charset "'" [ language ] "'" value-chars
|
||||||
/// ; like RFC 2231's <extended-initial-value>
|
/// ; like RFC 2231's <extended-initial-value>
|
||||||
/// ; (see [RFC 2231 §7])
|
/// ; (see [RFC 2231 §7])
|
||||||
|
|
|
@ -8,7 +8,7 @@ use crate::{
|
||||||
helpers::MutWriter,
|
helpers::MutWriter,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// A timestamp with HTTP formatting and parsing.
|
/// A timestamp with HTTP-style formatting and parsing.
|
||||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
|
||||||
pub struct HttpDate(SystemTime);
|
pub struct HttpDate(SystemTime);
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,7 @@
|
||||||
use std::{
|
use std::{
|
||||||
cmp,
|
cmp,
|
||||||
convert::{TryFrom, TryInto},
|
convert::{TryFrom, TryInto},
|
||||||
fmt,
|
fmt, str,
|
||||||
str::{self, FromStr},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
use derive_more::{Display, Error};
|
use derive_more::{Display, Error};
|
||||||
|
@ -28,7 +27,7 @@ const MAX_FLOAT_QUALITY: f32 = 1.0;
|
||||||
///
|
///
|
||||||
/// [RFC 7231 §5.3.1](https://datatracker.ietf.org/doc/html/rfc7231#section-5.3.1) gives more
|
/// [RFC 7231 §5.3.1](https://datatracker.ietf.org/doc/html/rfc7231#section-5.3.1) gives more
|
||||||
/// information on quality values in HTTP header fields.
|
/// information on quality values in HTTP header fields.
|
||||||
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
|
||||||
pub struct Quality(u16);
|
pub struct Quality(u16);
|
||||||
|
|
||||||
impl Quality {
|
impl Quality {
|
||||||
|
@ -81,18 +80,19 @@ impl TryFrom<f32> for Quality {
|
||||||
|
|
||||||
/// Represents an item with a quality value as defined
|
/// Represents an item with a quality value as defined
|
||||||
/// in [RFC 7231 §5.3.1](https://datatracker.ietf.org/doc/html/rfc7231#section-5.3.1).
|
/// in [RFC 7231 §5.3.1](https://datatracker.ietf.org/doc/html/rfc7231#section-5.3.1).
|
||||||
#[derive(Clone, PartialEq, Debug)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub struct QualityItem<T> {
|
pub struct QualityItem<T> {
|
||||||
/// The actual contents of the field.
|
/// The wrapped contents of the field.
|
||||||
pub item: T,
|
pub item: T,
|
||||||
|
|
||||||
/// The quality (client or server preference) for the value.
|
/// The quality (client or server preference) for the value.
|
||||||
pub quality: Quality,
|
pub quality: Quality,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> QualityItem<T> {
|
impl<T> QualityItem<T> {
|
||||||
/// Creates a new `QualityItem` from an item and a quality.
|
/// Constructs a new `QualityItem` from an item and a quality value.
|
||||||
/// The item can be of any type.
|
///
|
||||||
/// The quality should be a value in the range [0, 1].
|
/// The item can be of any type. The quality should be a value in the range [0, 1].
|
||||||
pub fn new(item: T, quality: Quality) -> QualityItem<T> {
|
pub fn new(item: T, quality: Quality) -> QualityItem<T> {
|
||||||
QualityItem { item, quality }
|
QualityItem { item, quality }
|
||||||
}
|
}
|
||||||
|
@ -116,7 +116,7 @@ impl<T: fmt::Display> fmt::Display for QualityItem<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: FromStr> FromStr for QualityItem<T> {
|
impl<T: str::FromStr> str::FromStr for QualityItem<T> {
|
||||||
type Err = ParseError;
|
type Err = ParseError;
|
||||||
|
|
||||||
fn from_str(qitem_str: &str) -> Result<Self, Self::Err> {
|
fn from_str(qitem_str: &str) -> Result<Self, Self::Err> {
|
||||||
|
@ -128,6 +128,7 @@ impl<T: FromStr> FromStr for QualityItem<T> {
|
||||||
let mut raw_item = qitem_str;
|
let mut raw_item = qitem_str;
|
||||||
let mut quality = 1f32;
|
let mut quality = 1f32;
|
||||||
|
|
||||||
|
// TODO: MSRV(1.52): use rsplit_once
|
||||||
let parts: Vec<_> = qitem_str.rsplitn(2, ';').map(str::trim).collect();
|
let parts: Vec<_> = qitem_str.rsplitn(2, ';').map(str::trim).collect();
|
||||||
|
|
||||||
if parts.len() == 2 {
|
if parts.len() == 2 {
|
||||||
|
|
|
@ -12,7 +12,8 @@ where
|
||||||
I: Iterator<Item = &'a HeaderValue> + 'a,
|
I: Iterator<Item = &'a HeaderValue> + 'a,
|
||||||
T: FromStr,
|
T: FromStr,
|
||||||
{
|
{
|
||||||
let mut result = Vec::new();
|
let size_guess = all.size_hint().1.unwrap_or(2);
|
||||||
|
let mut result = Vec::with_capacity(size_guess);
|
||||||
|
|
||||||
for h in all {
|
for h in all {
|
||||||
let s = h.to_str().map_err(|_| ParseError::Header)?;
|
let s = h.to_str().map_err(|_| ParseError::Header)?;
|
||||||
|
@ -26,6 +27,7 @@ where
|
||||||
.filter_map(|x| x.trim().parse().ok()),
|
.filter_map(|x| x.trim().parse().ok()),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(result)
|
Ok(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -34,10 +36,12 @@ where
|
||||||
pub fn from_one_raw_str<T: FromStr>(val: Option<&HeaderValue>) -> Result<T, ParseError> {
|
pub fn from_one_raw_str<T: FromStr>(val: Option<&HeaderValue>) -> Result<T, ParseError> {
|
||||||
if let Some(line) = val {
|
if let Some(line) = val {
|
||||||
let line = line.to_str().map_err(|_| ParseError::Header)?;
|
let line = line.to_str().map_err(|_| ParseError::Header)?;
|
||||||
|
|
||||||
if !line.is_empty() {
|
if !line.is_empty() {
|
||||||
return T::from_str(line).or(Err(ParseError::Header));
|
return T::from_str(line).or(Err(ParseError::Header));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Err(ParseError::Header)
|
Err(ParseError::Header)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,13 +52,16 @@ where
|
||||||
T: fmt::Display,
|
T: fmt::Display,
|
||||||
{
|
{
|
||||||
let mut iter = parts.iter();
|
let mut iter = parts.iter();
|
||||||
|
|
||||||
if let Some(part) = iter.next() {
|
if let Some(part) = iter.next() {
|
||||||
fmt::Display::fmt(part, f)?;
|
fmt::Display::fmt(part, f)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
for part in iter {
|
for part in iter {
|
||||||
f.write_str(", ")?;
|
f.write_str(", ")?;
|
||||||
fmt::Display::fmt(part, f)?;
|
fmt::Display::fmt(part, f)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,3 +72,32 @@ pub fn http_percent_encode(f: &mut fmt::Formatter<'_>, bytes: &[u8]) -> fmt::Res
|
||||||
let encoded = percent_encoding::percent_encode(bytes, HTTP_VALUE);
|
let encoded = percent_encoding::percent_encode(bytes, HTTP_VALUE);
|
||||||
fmt::Display::fmt(&encoded, f)
|
fmt::Display::fmt(&encoded, f)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn comma_delimited_parsing() {
|
||||||
|
let headers = vec![];
|
||||||
|
let res: Vec<usize> = from_comma_delimited(headers.iter()).unwrap();
|
||||||
|
assert_eq!(res, vec![0; 0]);
|
||||||
|
|
||||||
|
let headers = vec![
|
||||||
|
HeaderValue::from_static("1, 2"),
|
||||||
|
HeaderValue::from_static("3,4"),
|
||||||
|
];
|
||||||
|
let res: Vec<usize> = from_comma_delimited(headers.iter()).unwrap();
|
||||||
|
assert_eq!(res, vec![1, 2, 3, 4]);
|
||||||
|
|
||||||
|
let headers = vec![
|
||||||
|
HeaderValue::from_static(""),
|
||||||
|
HeaderValue::from_static(","),
|
||||||
|
HeaderValue::from_static(" "),
|
||||||
|
HeaderValue::from_static("1 ,"),
|
||||||
|
HeaderValue::from_static(""),
|
||||||
|
];
|
||||||
|
let res: Vec<usize> = from_comma_delimited(headers.iter()).unwrap();
|
||||||
|
assert_eq!(res, vec![1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -66,7 +66,7 @@ mod route;
|
||||||
/// Creates resource handler, allowing multiple HTTP method guards.
|
/// Creates resource handler, allowing multiple HTTP method guards.
|
||||||
///
|
///
|
||||||
/// # Syntax
|
/// # Syntax
|
||||||
/// ```text
|
/// ```plain
|
||||||
/// #[route("path", method="HTTP_METHOD"[, attributes])]
|
/// #[route("path", method="HTTP_METHOD"[, attributes])]
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
|
@ -112,7 +112,7 @@ concat!("
|
||||||
Creates route handler with `actix_web::guard::", stringify!($variant), "`.
|
Creates route handler with `actix_web::guard::", stringify!($variant), "`.
|
||||||
|
|
||||||
# Syntax
|
# Syntax
|
||||||
```text
|
```plain
|
||||||
#[", stringify!($method), r#"("path"[, attributes])]
|
#[", stringify!($method), r#"("path"[, attributes])]
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
@ -14,3 +14,5 @@ cargo test --lib --tests -p=actix-test --all-features
|
||||||
cargo test --lib --tests -p=actix-files
|
cargo test --lib --tests -p=actix-files
|
||||||
cargo test --lib --tests -p=actix-multipart --all-features
|
cargo test --lib --tests -p=actix-multipart --all-features
|
||||||
cargo test --lib --tests -p=actix-web-actors --all-features
|
cargo test --lib --tests -p=actix-web-actors --all-features
|
||||||
|
|
||||||
|
cargo test --workspace --doc
|
||||||
|
|
|
@ -16,7 +16,7 @@ crate::http::header::common_header! {
|
||||||
/// in-line image
|
/// in-line image
|
||||||
///
|
///
|
||||||
/// # ABNF
|
/// # ABNF
|
||||||
/// ```text
|
/// ```plain
|
||||||
/// Accept = #( media-range [ accept-params ] )
|
/// Accept = #( media-range [ accept-params ] )
|
||||||
///
|
///
|
||||||
/// media-range = ( "*/*"
|
/// media-range = ( "*/*"
|
||||||
|
@ -77,7 +77,7 @@ crate::http::header::common_header! {
|
||||||
/// ])
|
/// ])
|
||||||
/// );
|
/// );
|
||||||
/// ```
|
/// ```
|
||||||
(Accept, header::ACCEPT) => (QualityItem<Mime>)+
|
(Accept, header::ACCEPT) => (QualityItem<Mime>)*
|
||||||
|
|
||||||
test_parse_and_format {
|
test_parse_and_format {
|
||||||
// Tests from the RFC
|
// Tests from the RFC
|
||||||
|
@ -88,6 +88,7 @@ crate::http::header::common_header! {
|
||||||
QualityItem::new("audio/*".parse().unwrap(), q(200)),
|
QualityItem::new("audio/*".parse().unwrap(), q(200)),
|
||||||
qitem("audio/basic".parse().unwrap()),
|
qitem("audio/basic".parse().unwrap()),
|
||||||
])));
|
])));
|
||||||
|
|
||||||
crate::http::header::common_header_test!(
|
crate::http::header::common_header_test!(
|
||||||
test2,
|
test2,
|
||||||
vec![b"text/plain; q=0.5, text/html, text/x-dvi; q=0.8, text/x-c"],
|
vec![b"text/plain; q=0.5, text/html, text/x-dvi; q=0.8, text/x-c"],
|
||||||
|
@ -99,6 +100,7 @@ crate::http::header::common_header! {
|
||||||
q(800)),
|
q(800)),
|
||||||
qitem("text/x-c".parse().unwrap()),
|
qitem("text/x-c".parse().unwrap()),
|
||||||
])));
|
])));
|
||||||
|
|
||||||
// Custom tests
|
// Custom tests
|
||||||
crate::http::header::common_header_test!(
|
crate::http::header::common_header_test!(
|
||||||
test3,
|
test3,
|
||||||
|
@ -116,8 +118,9 @@ crate::http::header::common_header! {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_fuzzing1() {
|
fn test_fuzzing1() {
|
||||||
use actix_http::test::TestRequest;
|
let req = test::TestRequest::default()
|
||||||
let req = TestRequest::default().insert_header((crate::http::header::ACCEPT, "chunk#;e")).finish();
|
.insert_header((header::ACCEPT, "chunk#;e"))
|
||||||
|
.finish();
|
||||||
let header = Accept::parse(&req);
|
let header = Accept::parse(&req);
|
||||||
assert!(header.is_ok());
|
assert!(header.is_ok());
|
||||||
}
|
}
|
||||||
|
@ -154,7 +157,11 @@ impl Accept {
|
||||||
/// [q-factor weighting] and specificity.
|
/// [q-factor weighting] and specificity.
|
||||||
///
|
///
|
||||||
/// [q-factor weighting]: https://datatracker.ietf.org/doc/html/rfc7231#section-5.3.2
|
/// [q-factor weighting]: https://datatracker.ietf.org/doc/html/rfc7231#section-5.3.2
|
||||||
pub fn mime_precedence(&self) -> Vec<Mime> {
|
pub fn ranked(&self) -> Vec<Mime> {
|
||||||
|
if self.is_empty() {
|
||||||
|
return vec![];
|
||||||
|
}
|
||||||
|
|
||||||
let mut types = self.0.clone();
|
let mut types = self.0.clone();
|
||||||
|
|
||||||
// use stable sort so items with equal q-factor and specificity retain listed order
|
// use stable sort so items with equal q-factor and specificity retain listed order
|
||||||
|
@ -201,12 +208,29 @@ impl Accept {
|
||||||
/// If no q-factors are provided, the first mime type is chosen. Note that items without
|
/// If no q-factors are provided, the first mime type is chosen. Note that items without
|
||||||
/// q-factors are given the maximum preference value.
|
/// q-factors are given the maximum preference value.
|
||||||
///
|
///
|
||||||
/// Returns `None` if contained list is empty.
|
/// As per the spec, will return [`Mime::STAR_STAR`] (indicating no preference) if the contained
|
||||||
|
/// list is empty.
|
||||||
///
|
///
|
||||||
/// [q-factor weighting]: https://datatracker.ietf.org/doc/html/rfc7231#section-5.3.2
|
/// [q-factor weighting]: https://datatracker.ietf.org/doc/html/rfc7231#section-5.3.2
|
||||||
pub fn mime_preference(&self) -> Option<Mime> {
|
pub fn preference(&self) -> Mime {
|
||||||
let types = self.mime_precedence();
|
use actix_http::header::q;
|
||||||
types.first().cloned()
|
|
||||||
|
let mut max_item = None;
|
||||||
|
let mut max_pref = q(0);
|
||||||
|
|
||||||
|
// uses manual max lookup loop since we want the first occurrence in the case of same
|
||||||
|
// preference but `Iterator::max_by_key` would give us the last occurrence
|
||||||
|
|
||||||
|
for pref in &self.0 {
|
||||||
|
// only change if strictly greater
|
||||||
|
// equal items, even while unsorted, still have higher preference if they appear first
|
||||||
|
if pref.quality > max_pref {
|
||||||
|
max_pref = pref.quality;
|
||||||
|
max_item = Some(pref.item.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
max_item.unwrap_or(mime::STAR_STAR)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -216,12 +240,12 @@ mod tests {
|
||||||
use crate::http::header::q;
|
use crate::http::header::q;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_mime_precedence() {
|
fn ranking_precedence() {
|
||||||
let test = Accept(vec![]);
|
let test = Accept(vec![]);
|
||||||
assert!(test.mime_precedence().is_empty());
|
assert!(test.ranked().is_empty());
|
||||||
|
|
||||||
let test = Accept(vec![qitem(mime::APPLICATION_JSON)]);
|
let test = Accept(vec![qitem(mime::APPLICATION_JSON)]);
|
||||||
assert_eq!(test.mime_precedence(), vec!(mime::APPLICATION_JSON));
|
assert_eq!(test.ranked(), vec!(mime::APPLICATION_JSON));
|
||||||
|
|
||||||
let test = Accept(vec![
|
let test = Accept(vec![
|
||||||
qitem(mime::TEXT_HTML),
|
qitem(mime::TEXT_HTML),
|
||||||
|
@ -230,7 +254,7 @@ mod tests {
|
||||||
QualityItem::new(mime::STAR_STAR, q(0.8)),
|
QualityItem::new(mime::STAR_STAR, q(0.8)),
|
||||||
]);
|
]);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
test.mime_precedence(),
|
test.ranked(),
|
||||||
vec![
|
vec![
|
||||||
mime::TEXT_HTML,
|
mime::TEXT_HTML,
|
||||||
"application/xhtml+xml".parse().unwrap(),
|
"application/xhtml+xml".parse().unwrap(),
|
||||||
|
@ -245,20 +269,20 @@ mod tests {
|
||||||
qitem(mime::IMAGE_PNG),
|
qitem(mime::IMAGE_PNG),
|
||||||
]);
|
]);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
test.mime_precedence(),
|
test.ranked(),
|
||||||
vec![mime::IMAGE_PNG, mime::IMAGE_STAR, mime::STAR_STAR]
|
vec![mime::IMAGE_PNG, mime::IMAGE_STAR, mime::STAR_STAR]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_mime_preference() {
|
fn preference_selection() {
|
||||||
let test = Accept(vec![
|
let test = Accept(vec![
|
||||||
qitem(mime::TEXT_HTML),
|
qitem(mime::TEXT_HTML),
|
||||||
"application/xhtml+xml".parse().unwrap(),
|
"application/xhtml+xml".parse().unwrap(),
|
||||||
QualityItem::new("application/xml".parse().unwrap(), q(0.9)),
|
QualityItem::new("application/xml".parse().unwrap(), q(0.9)),
|
||||||
QualityItem::new(mime::STAR_STAR, q(0.8)),
|
QualityItem::new(mime::STAR_STAR, q(0.8)),
|
||||||
]);
|
]);
|
||||||
assert_eq!(test.mime_preference(), Some(mime::TEXT_HTML));
|
assert_eq!(test.preference(), mime::TEXT_HTML);
|
||||||
|
|
||||||
let test = Accept(vec![
|
let test = Accept(vec![
|
||||||
QualityItem::new("video/*".parse().unwrap(), q(0.8)),
|
QualityItem::new("video/*".parse().unwrap(), q(0.8)),
|
||||||
|
@ -267,6 +291,6 @@ mod tests {
|
||||||
qitem(mime::IMAGE_SVG),
|
qitem(mime::IMAGE_SVG),
|
||||||
QualityItem::new(mime::IMAGE_STAR, q(0.8)),
|
QualityItem::new(mime::IMAGE_STAR, q(0.8)),
|
||||||
]);
|
]);
|
||||||
assert_eq!(test.mime_preference(), Some(mime::IMAGE_PNG));
|
assert_eq!(test.preference(), mime::IMAGE_PNG);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,7 +12,7 @@ crate::http::header::common_header! {
|
||||||
/// those charsets.
|
/// those charsets.
|
||||||
///
|
///
|
||||||
/// # ABNF
|
/// # ABNF
|
||||||
/// ```text
|
/// ```plain
|
||||||
/// Accept-Charset = 1#( ( charset / "*" ) [ weight ] )
|
/// Accept-Charset = 1#( ( charset / "*" ) [ weight ] )
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
use header::{Encoding, QualityItem};
|
use actix_http::header::QualityItem;
|
||||||
|
|
||||||
header! {
|
use super::{common_header, Encoding};
|
||||||
|
use crate::http::header;
|
||||||
|
|
||||||
|
common_header! {
|
||||||
/// `Accept-Encoding` header, defined
|
/// `Accept-Encoding` header, defined
|
||||||
/// in [RFC 7231](https://datatracker.ietf.org/doc/html/rfc7231#section-5.3.4)
|
/// in [RFC 7231](https://datatracker.ietf.org/doc/html/rfc7231#section-5.3.4)
|
||||||
///
|
///
|
||||||
|
@ -11,7 +14,7 @@ header! {
|
||||||
/// preferred.
|
/// preferred.
|
||||||
///
|
///
|
||||||
/// # ABNF
|
/// # ABNF
|
||||||
/// ```text
|
/// ```plain
|
||||||
/// Accept-Encoding = #( codings [ weight ] )
|
/// Accept-Encoding = #( codings [ weight ] )
|
||||||
/// codings = content-coding / "identity" / "*"
|
/// codings = content-coding / "identity" / "*"
|
||||||
/// ```
|
/// ```
|
||||||
|
@ -28,7 +31,7 @@ header! {
|
||||||
/// use actix_web::HttpResponse;
|
/// use actix_web::HttpResponse;
|
||||||
/// use actix_web::http::header::{AcceptEncoding, Encoding, qitem};
|
/// use actix_web::http::header::{AcceptEncoding, Encoding, qitem};
|
||||||
///
|
///
|
||||||
/// let mut builder = HttpResponse::new();
|
/// let mut builder = HttpResponse::Ok();
|
||||||
/// builder.insert_header(
|
/// builder.insert_header(
|
||||||
/// AcceptEncoding(vec![qitem(Encoding::Chunked)])
|
/// AcceptEncoding(vec![qitem(Encoding::Chunked)])
|
||||||
/// );
|
/// );
|
||||||
|
@ -37,7 +40,7 @@ header! {
|
||||||
/// use actix_web::HttpResponse;
|
/// use actix_web::HttpResponse;
|
||||||
/// use actix_web::http::header::{AcceptEncoding, Encoding, qitem};
|
/// use actix_web::http::header::{AcceptEncoding, Encoding, qitem};
|
||||||
///
|
///
|
||||||
/// let mut builder = HttpResponse::new();
|
/// let mut builder = HttpResponse::Ok();
|
||||||
/// builder.insert_header(
|
/// builder.insert_header(
|
||||||
/// AcceptEncoding(vec![
|
/// AcceptEncoding(vec![
|
||||||
/// qitem(Encoding::Chunked),
|
/// qitem(Encoding::Chunked),
|
||||||
|
@ -50,7 +53,7 @@ header! {
|
||||||
/// use actix_web::HttpResponse;
|
/// use actix_web::HttpResponse;
|
||||||
/// use actix_web::http::header::{AcceptEncoding, Encoding, QualityItem, q, qitem};
|
/// use actix_web::http::header::{AcceptEncoding, Encoding, QualityItem, q, qitem};
|
||||||
///
|
///
|
||||||
/// let mut builder = HttpResponse::new();
|
/// let mut builder = HttpResponse::Ok();
|
||||||
/// builder.insert_header(
|
/// builder.insert_header(
|
||||||
/// AcceptEncoding(vec![
|
/// AcceptEncoding(vec![
|
||||||
/// qitem(Encoding::Chunked),
|
/// qitem(Encoding::Chunked),
|
||||||
|
@ -59,16 +62,18 @@ header! {
|
||||||
/// ])
|
/// ])
|
||||||
/// );
|
/// );
|
||||||
/// ```
|
/// ```
|
||||||
(AcceptEncoding, "Accept-Encoding") => (QualityItem<Encoding>)*
|
(AcceptEncoding, header::ACCEPT_ENCODING) => (QualityItem<Encoding>)*
|
||||||
|
|
||||||
test_parse_and_format {
|
test_parse_and_format {
|
||||||
// From the RFC
|
// From the RFC
|
||||||
crate::http::header::common_header_test!(test1, vec![b"compress, gzip"]);
|
common_header_test!(test1, vec![b"compress, gzip"]);
|
||||||
crate::http::header::common_header_test!(test2, vec![b""], Some(AcceptEncoding(vec![])));
|
common_header_test!(test2, vec![b""], Some(AcceptEncoding(vec![])));
|
||||||
crate::http::header::common_header_test!(test3, vec![b"*"]);
|
common_header_test!(test3, vec![b"*"]);
|
||||||
|
|
||||||
// Note: Removed quality 1 from gzip
|
// Note: Removed quality 1 from gzip
|
||||||
crate::http::header::common_header_test!(test4, vec![b"compress;q=0.5, gzip"]);
|
common_header_test!(test4, vec![b"compress;q=0.5, gzip"]);
|
||||||
|
|
||||||
// Note: Removed quality 1 from gzip
|
// Note: Removed quality 1 from gzip
|
||||||
crate::http::header::common_header_test!(test5, vec![b"gzip, identity; q=0.5, *;q=0"]);
|
common_header_test!(test5, vec![b"gzip, identity; q=0.5, *;q=0"]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,66 +1,225 @@
|
||||||
use language_tags::LanguageTag;
|
use language_tags::LanguageTag;
|
||||||
|
|
||||||
use super::{QualityItem, ACCEPT_LANGUAGE};
|
use super::{common_header, Preference, QualityItem};
|
||||||
|
use crate::http::header;
|
||||||
|
|
||||||
crate::http::header::common_header! {
|
common_header! {
|
||||||
/// `Accept-Language` header, defined
|
/// `Accept-Language` header, defined
|
||||||
/// in [RFC 7231 §5.3.5](https://datatracker.ietf.org/doc/html/rfc7231#section-5.3.5)
|
/// in [RFC 7231 §5.3.5](https://datatracker.ietf.org/doc/html/rfc7231#section-5.3.5)
|
||||||
///
|
///
|
||||||
/// The `Accept-Language` header field can be used by user agents to
|
/// The `Accept-Language` header field can be used by user agents to indicate the set of natural
|
||||||
/// indicate the set of natural languages that are preferred in the
|
/// languages that are preferred in the response.
|
||||||
/// response.
|
///
|
||||||
|
/// The `Accept-Language` header is defined in
|
||||||
|
/// [RFC 7231 §5.3.5](https://datatracker.ietf.org/doc/html/rfc7231#section-5.3.5) using language
|
||||||
|
/// ranges defined in [RFC 4647 §2.1](https://datatracker.ietf.org/doc/html/rfc4647#section-2.1).
|
||||||
///
|
///
|
||||||
/// # ABNF
|
/// # ABNF
|
||||||
///
|
/// ```plain
|
||||||
/// ```text
|
|
||||||
/// Accept-Language = 1#( language-range [ weight ] )
|
/// Accept-Language = 1#( language-range [ weight ] )
|
||||||
/// language-range = <language-range, see [RFC4647], Section 2.1>
|
/// language-range = (1*8ALPHA *("-" 1*8alphanum)) / "*"
|
||||||
|
/// alphanum = ALPHA / DIGIT
|
||||||
|
/// weight = OWS ";" OWS "q=" qvalue
|
||||||
|
/// qvalue = ( "0" [ "." 0*3DIGIT ] )
|
||||||
|
/// / ( "1" [ "." 0*3("0") ] )
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// # Example values
|
/// # Example Values
|
||||||
/// * `da, en-gb;q=0.8, en;q=0.7`
|
/// - `da, en-gb;q=0.8, en;q=0.7`
|
||||||
/// * `en-us;q=1.0, en;q=0.5, fr`
|
/// - `en-us;q=1.0, en;q=0.5, fr`
|
||||||
|
/// - `fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5`
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
|
||||||
/// ```
|
/// ```
|
||||||
/// use actix_web::HttpResponse;
|
/// use actix_web::HttpResponse;
|
||||||
/// use actix_web::http::header::{AcceptLanguage, LanguageTag, qitem};
|
/// use actix_web::http::header::{AcceptLanguage, qitem};
|
||||||
///
|
///
|
||||||
/// let mut builder = HttpResponse::Ok();
|
/// let mut builder = HttpResponse::Ok();
|
||||||
/// let langtag = LanguageTag::parse("en-US").unwrap();
|
|
||||||
/// builder.insert_header(
|
/// builder.insert_header(
|
||||||
/// AcceptLanguage(vec![
|
/// AcceptLanguage(vec![
|
||||||
/// qitem(langtag),
|
/// qitem("en-US".parse().unwrap())
|
||||||
/// ])
|
/// ])
|
||||||
/// );
|
/// );
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// use actix_web::HttpResponse;
|
/// use actix_web::HttpResponse;
|
||||||
/// use actix_web::http::header::{AcceptLanguage, LanguageTag, QualityItem, q, qitem};
|
/// use actix_web::http::header::{AcceptLanguage, QualityItem, q, qitem};
|
||||||
///
|
///
|
||||||
/// let mut builder = HttpResponse::Ok();
|
/// let mut builder = HttpResponse::Ok();
|
||||||
/// builder.insert_header(
|
/// builder.insert_header(
|
||||||
/// AcceptLanguage(vec![
|
/// AcceptLanguage(vec![
|
||||||
/// qitem(LanguageTag::parse("da").unwrap()),
|
/// qitem("da".parse().unwrap()),
|
||||||
/// QualityItem::new(LanguageTag::parse("en-GB").unwrap(), q(800)),
|
/// QualityItem::new("en-GB".parse().unwrap(), q(800)),
|
||||||
/// QualityItem::new(LanguageTag::parse("en").unwrap(), q(700)),
|
/// QualityItem::new("en".parse().unwrap(), q(700)),
|
||||||
/// ])
|
/// ])
|
||||||
/// );
|
/// );
|
||||||
/// ```
|
/// ```
|
||||||
(AcceptLanguage, ACCEPT_LANGUAGE) => (QualityItem<LanguageTag>)+
|
(AcceptLanguage, header::ACCEPT_LANGUAGE) => (QualityItem<Preference<LanguageTag>>)*
|
||||||
|
|
||||||
test_accept_language {
|
test_parse_and_format {
|
||||||
// From the RFC
|
common_header_test!(no_headers, vec![b""; 0], Some(AcceptLanguage(vec![])));
|
||||||
crate::http::header::common_header_test!(test1, vec![b"da, en-gb;q=0.8, en;q=0.7"]);
|
|
||||||
// Own test
|
common_header_test!(empty_header, vec![b""; 1], Some(AcceptLanguage(vec![])));
|
||||||
crate::http::header::common_header_test!(
|
|
||||||
test2, vec![b"en-US, en; q=0.5, fr"],
|
common_header_test!(
|
||||||
|
example_from_rfc,
|
||||||
|
vec![b"da, en-gb;q=0.8, en;q=0.7"]
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
common_header_test!(
|
||||||
|
not_ordered_by_weight,
|
||||||
|
vec![b"en-US, en; q=0.5, fr"],
|
||||||
Some(AcceptLanguage(vec![
|
Some(AcceptLanguage(vec![
|
||||||
qitem("en-US".parse().unwrap()),
|
qitem("en-US".parse().unwrap()),
|
||||||
QualityItem::new("en".parse().unwrap(), q(500)),
|
QualityItem::new("en".parse().unwrap(), q(500)),
|
||||||
qitem("fr".parse().unwrap()),
|
qitem("fr".parse().unwrap()),
|
||||||
])));
|
]))
|
||||||
|
);
|
||||||
|
|
||||||
|
common_header_test!(
|
||||||
|
has_wildcard,
|
||||||
|
vec![b"fr-CH, fr; q=0.9, en; q=0.8, de; q=0.7, *; q=0.5"],
|
||||||
|
Some(AcceptLanguage(vec![
|
||||||
|
qitem("fr-CH".parse().unwrap()),
|
||||||
|
QualityItem::new("fr".parse().unwrap(), q(900)),
|
||||||
|
QualityItem::new("en".parse().unwrap(), q(800)),
|
||||||
|
QualityItem::new("de".parse().unwrap(), q(700)),
|
||||||
|
QualityItem::new("*".parse().unwrap(), q(500)),
|
||||||
|
]))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AcceptLanguage {
|
||||||
|
/// Returns a sorted list of languages from highest to lowest precedence, accounting
|
||||||
|
/// for [q-factor weighting].
|
||||||
|
///
|
||||||
|
/// [q-factor weighting]: https://datatracker.ietf.org/doc/html/rfc7231#section-5.3.2
|
||||||
|
pub fn ranked(&self) -> Vec<Preference<LanguageTag>> {
|
||||||
|
if self.0.is_empty() {
|
||||||
|
return vec![];
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut types = self.0.clone();
|
||||||
|
|
||||||
|
// use stable sort so items with equal q-factor retain listed order
|
||||||
|
types.sort_by(|a, b| {
|
||||||
|
// sort by q-factor descending
|
||||||
|
b.quality.cmp(&a.quality)
|
||||||
|
});
|
||||||
|
|
||||||
|
types.into_iter().map(|qitem| qitem.item).collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Extracts the most preferable language, accounting for [q-factor weighting].
|
||||||
|
///
|
||||||
|
/// If no q-factors are provided, the first language is chosen. Note that items without
|
||||||
|
/// q-factors are given the maximum preference value.
|
||||||
|
///
|
||||||
|
/// As per the spec, returns [`Preference::Any`] if contained list is empty.
|
||||||
|
///
|
||||||
|
/// [q-factor weighting]: https://datatracker.ietf.org/doc/html/rfc7231#section-5.3.2
|
||||||
|
pub fn preference(&self) -> Preference<LanguageTag> {
|
||||||
|
use actix_http::header::q;
|
||||||
|
|
||||||
|
let mut max_item = None;
|
||||||
|
let mut max_pref = q(0);
|
||||||
|
|
||||||
|
// uses manual max lookup loop since we want the first occurrence in the case of same
|
||||||
|
// preference but `Iterator::max_by_key` would give us the last occurrence
|
||||||
|
|
||||||
|
for pref in &self.0 {
|
||||||
|
// only change if strictly greater
|
||||||
|
// equal items, even while unsorted, still have higher preference if they appear first
|
||||||
|
if pref.quality > max_pref {
|
||||||
|
max_pref = pref.quality;
|
||||||
|
max_item = Some(pref.item.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
max_item.unwrap_or(Preference::Any)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use crate::http::header::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn ranking_precedence() {
|
||||||
|
let test = AcceptLanguage(vec![]);
|
||||||
|
assert!(test.ranked().is_empty());
|
||||||
|
|
||||||
|
let test = AcceptLanguage(vec![qitem("fr-CH".parse().unwrap())]);
|
||||||
|
assert_eq!(test.ranked(), vec!("fr-CH".parse().unwrap()));
|
||||||
|
|
||||||
|
let test = AcceptLanguage(vec![
|
||||||
|
QualityItem::new("fr".parse().unwrap(), q(900)),
|
||||||
|
QualityItem::new("fr-CH".parse().unwrap(), q(1000)),
|
||||||
|
QualityItem::new("en".parse().unwrap(), q(800)),
|
||||||
|
QualityItem::new("*".parse().unwrap(), q(500)),
|
||||||
|
QualityItem::new("de".parse().unwrap(), q(700)),
|
||||||
|
]);
|
||||||
|
assert_eq!(
|
||||||
|
test.ranked(),
|
||||||
|
vec![
|
||||||
|
"fr-CH".parse().unwrap(),
|
||||||
|
"fr".parse().unwrap(),
|
||||||
|
"en".parse().unwrap(),
|
||||||
|
"de".parse().unwrap(),
|
||||||
|
"*".parse().unwrap(),
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
let test = AcceptLanguage(vec![
|
||||||
|
qitem("fr".parse().unwrap()),
|
||||||
|
qitem("fr-CH".parse().unwrap()),
|
||||||
|
qitem("en".parse().unwrap()),
|
||||||
|
qitem("*".parse().unwrap()),
|
||||||
|
qitem("de".parse().unwrap()),
|
||||||
|
]);
|
||||||
|
assert_eq!(
|
||||||
|
test.ranked(),
|
||||||
|
vec![
|
||||||
|
"fr".parse().unwrap(),
|
||||||
|
"fr-CH".parse().unwrap(),
|
||||||
|
"en".parse().unwrap(),
|
||||||
|
"*".parse().unwrap(),
|
||||||
|
"de".parse().unwrap(),
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn preference_selection() {
|
||||||
|
let test = AcceptLanguage(vec![
|
||||||
|
QualityItem::new("fr".parse().unwrap(), q(900)),
|
||||||
|
QualityItem::new("fr-CH".parse().unwrap(), q(1000)),
|
||||||
|
QualityItem::new("en".parse().unwrap(), q(800)),
|
||||||
|
QualityItem::new("*".parse().unwrap(), q(500)),
|
||||||
|
QualityItem::new("de".parse().unwrap(), q(700)),
|
||||||
|
]);
|
||||||
|
assert_eq!(
|
||||||
|
test.preference(),
|
||||||
|
Preference::Specific("fr-CH".parse().unwrap())
|
||||||
|
);
|
||||||
|
|
||||||
|
let test = AcceptLanguage(vec![
|
||||||
|
qitem("fr".parse().unwrap()),
|
||||||
|
qitem("fr-CH".parse().unwrap()),
|
||||||
|
qitem("en".parse().unwrap()),
|
||||||
|
qitem("*".parse().unwrap()),
|
||||||
|
qitem("de".parse().unwrap()),
|
||||||
|
]);
|
||||||
|
assert_eq!(
|
||||||
|
test.preference(),
|
||||||
|
Preference::Specific("fr".parse().unwrap())
|
||||||
|
);
|
||||||
|
|
||||||
|
let test = AcceptLanguage(vec![]);
|
||||||
|
assert_eq!(test.preference(), Preference::Any);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,7 +12,7 @@ crate::http::header::common_header! {
|
||||||
/// with the resource.
|
/// with the resource.
|
||||||
///
|
///
|
||||||
/// # ABNF
|
/// # ABNF
|
||||||
/// ```text
|
/// ```plain
|
||||||
/// Allow = #method
|
/// Allow = #method
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
|
|
|
@ -0,0 +1,70 @@
|
||||||
|
use std::{
|
||||||
|
fmt::{self, Write as _},
|
||||||
|
str,
|
||||||
|
};
|
||||||
|
|
||||||
|
/// A wrapper for types used in header values where wildcard (`*`) items are allowed but the
|
||||||
|
/// underlying type does not support them.
|
||||||
|
///
|
||||||
|
/// For example, we use the `language-tags` crate for the [`AcceptLanguage`](super::AcceptLanguage)
|
||||||
|
/// typed header but it does parse `*` successfully. On the other hand, the `mime` crate, used for
|
||||||
|
/// [`Accept`](super::Accept), has first-party support for wildcard items so this wrapper is not
|
||||||
|
/// used in those header types.
|
||||||
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Hash)]
|
||||||
|
pub enum AnyOrSome<T> {
|
||||||
|
/// A wildcard value.
|
||||||
|
Any,
|
||||||
|
|
||||||
|
/// A valid `T`.
|
||||||
|
Item(T),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> AnyOrSome<T> {
|
||||||
|
/// Returns true if item is wildcard (`*`) variant.
|
||||||
|
pub fn is_any(&self) -> bool {
|
||||||
|
matches!(self, Self::Any)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns true if item is a valid item (`T`) variant.
|
||||||
|
pub fn is_item(&self) -> bool {
|
||||||
|
matches!(self, Self::Item(_))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns reference to value in `Item` variant, if it is set.
|
||||||
|
pub fn item(&self) -> Option<&T> {
|
||||||
|
match self {
|
||||||
|
AnyOrSome::Item(ref item) => Some(item),
|
||||||
|
AnyOrSome::Any => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Consumes the container, returning the value in the `Item` variant, if it is set.
|
||||||
|
pub fn into_item(self) -> Option<T> {
|
||||||
|
match self {
|
||||||
|
AnyOrSome::Item(item) => Some(item),
|
||||||
|
AnyOrSome::Any => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: fmt::Display> fmt::Display for AnyOrSome<T> {
|
||||||
|
#[inline]
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
match self {
|
||||||
|
AnyOrSome::Any => f.write_char('*'),
|
||||||
|
AnyOrSome::Item(item) => fmt::Display::fmt(item, f),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: str::FromStr> str::FromStr for AnyOrSome<T> {
|
||||||
|
type Err = T::Err;
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||||
|
match s.trim() {
|
||||||
|
"*" => Ok(Self::Any),
|
||||||
|
other => other.parse().map(AnyOrSome::Item),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,94 +1,99 @@
|
||||||
use std::fmt::{self, Write};
|
use std::{fmt, str};
|
||||||
use std::str::FromStr;
|
|
||||||
|
|
||||||
use super::{fmt_comma_delimited, from_comma_delimited, Header, IntoHeaderValue, Writer};
|
|
||||||
|
|
||||||
|
use super::common_header;
|
||||||
use crate::http::header;
|
use crate::http::header;
|
||||||
|
|
||||||
/// `Cache-Control` header, defined
|
common_header! {
|
||||||
/// in [RFC 7234 §5.2](https://datatracker.ietf.org/doc/html/rfc7234#section-5.2).
|
/// `Cache-Control` header, defined
|
||||||
///
|
/// in [RFC 7234 §5.2](https://datatracker.ietf.org/doc/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
|
/// The `Cache-Control` header field is used to specify directives for
|
||||||
/// unidirectional in that the presence of a directive in a request does
|
/// caches along the request/response chain. Such cache directives are
|
||||||
/// not imply that the same directive is to be given in the response.
|
/// 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
|
/// # ABNF
|
||||||
/// Cache-Control = 1#cache-directive
|
/// ```text
|
||||||
/// cache-directive = token [ "=" ( token / quoted-string ) ]
|
/// Cache-Control = 1#cache-directive
|
||||||
/// ```
|
/// cache-directive = token [ "=" ( token / quoted-string ) ]
|
||||||
///
|
/// ```
|
||||||
/// # Example Values
|
///
|
||||||
///
|
/// # Example Values
|
||||||
/// * `no-cache`
|
/// * `no-cache`
|
||||||
/// * `private, community="UCI"`
|
/// * `private, community="UCI"`
|
||||||
/// * `max-age=30`
|
/// * `max-age=30`
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
/// ```
|
/// ```
|
||||||
/// use actix_web::HttpResponse;
|
/// use actix_web::HttpResponse;
|
||||||
/// use actix_web::http::header::{CacheControl, CacheDirective};
|
/// use actix_web::http::header::{CacheControl, CacheDirective};
|
||||||
///
|
///
|
||||||
/// let mut builder = HttpResponse::Ok();
|
/// let mut builder = HttpResponse::Ok();
|
||||||
/// builder.insert_header(CacheControl(vec![CacheDirective::MaxAge(86400u32)]));
|
/// builder.insert_header(CacheControl(vec![CacheDirective::MaxAge(86400u32)]));
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// use actix_web::HttpResponse;
|
/// use actix_web::HttpResponse;
|
||||||
/// use actix_web::http::header::{CacheControl, CacheDirective};
|
/// use actix_web::http::header::{CacheControl, CacheDirective};
|
||||||
///
|
///
|
||||||
/// let mut builder = HttpResponse::Ok();
|
/// let mut builder = HttpResponse::Ok();
|
||||||
/// builder.insert_header(CacheControl(vec![
|
/// builder.insert_header(CacheControl(vec![
|
||||||
/// CacheDirective::NoCache,
|
/// CacheDirective::NoCache,
|
||||||
/// CacheDirective::Private,
|
/// CacheDirective::Private,
|
||||||
/// CacheDirective::MaxAge(360u32),
|
/// CacheDirective::MaxAge(360u32),
|
||||||
/// CacheDirective::Extension("foo".to_owned(), Some("bar".to_owned())),
|
/// CacheDirective::Extension("foo".to_owned(), Some("bar".to_owned())),
|
||||||
/// ]));
|
/// ]));
|
||||||
/// ```
|
/// ```
|
||||||
#[derive(PartialEq, Clone, Debug)]
|
(CacheControl, header::CACHE_CONTROL) => (CacheDirective)+
|
||||||
pub struct CacheControl(pub Vec<CacheDirective>);
|
|
||||||
|
|
||||||
crate::http::header::common_header_deref!(CacheControl => Vec<CacheDirective>);
|
test_parse_and_format {
|
||||||
|
common_header_test!(no_headers, vec![b""; 0], None);
|
||||||
|
common_header_test!(empty_header, vec![b""; 1], None);
|
||||||
|
common_header_test!(bad_syntax, vec![b"foo="], None);
|
||||||
|
|
||||||
// TODO: this could just be the crate::http::header::common_header! macro
|
common_header_test!(
|
||||||
impl Header for CacheControl {
|
multiple_headers,
|
||||||
fn name() -> header::HeaderName {
|
vec![&b"no-cache"[..], &b"private"[..]],
|
||||||
header::CACHE_CONTROL
|
Some(CacheControl(vec![
|
||||||
}
|
CacheDirective::NoCache,
|
||||||
|
CacheDirective::Private,
|
||||||
|
]))
|
||||||
|
);
|
||||||
|
|
||||||
#[inline]
|
common_header_test!(
|
||||||
fn parse<T>(msg: &T) -> Result<Self, crate::error::ParseError>
|
argument,
|
||||||
where
|
vec![b"max-age=100, private"],
|
||||||
T: crate::HttpMessage,
|
Some(CacheControl(vec![
|
||||||
{
|
CacheDirective::MaxAge(100),
|
||||||
let directives = from_comma_delimited(msg.headers().get_all(&Self::name()))?;
|
CacheDirective::Private,
|
||||||
if !directives.is_empty() {
|
]))
|
||||||
Ok(CacheControl(directives))
|
);
|
||||||
} else {
|
|
||||||
Err(crate::error::ParseError::Header)
|
common_header_test!(
|
||||||
|
extension,
|
||||||
|
vec![b"foo, bar=baz"],
|
||||||
|
Some(CacheControl(vec![
|
||||||
|
CacheDirective::Extension("foo".to_owned(), None),
|
||||||
|
CacheDirective::Extension("bar".to_owned(), Some("baz".to_owned())),
|
||||||
|
]))
|
||||||
|
);
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_quote_form() {
|
||||||
|
let req = test::TestRequest::default()
|
||||||
|
.insert_header((header::CACHE_CONTROL, "max-age=\"200\""))
|
||||||
|
.finish();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
Header::parse(&req).ok(),
|
||||||
|
Some(CacheControl(vec![CacheDirective::MaxAge(200)]))
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for CacheControl {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
||||||
fmt_comma_delimited(f, &self.0[..])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl IntoHeaderValue for CacheControl {
|
|
||||||
type Error = header::InvalidHeaderValue;
|
|
||||||
|
|
||||||
fn try_into_value(self) -> Result<header::HeaderValue, Self::Error> {
|
|
||||||
let mut writer = Writer::new();
|
|
||||||
let _ = write!(&mut writer, "{}", self);
|
|
||||||
header::HeaderValue::from_maybe_shared(writer.take())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// `CacheControl` contains a list of these directives.
|
/// `CacheControl` contains a list of these directives.
|
||||||
#[derive(PartialEq, Clone, Debug)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub enum CacheDirective {
|
pub enum CacheDirective {
|
||||||
/// "no-cache"
|
/// "no-cache"
|
||||||
NoCache,
|
NoCache,
|
||||||
|
@ -126,38 +131,40 @@ pub enum CacheDirective {
|
||||||
impl fmt::Display for CacheDirective {
|
impl fmt::Display for CacheDirective {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
use self::CacheDirective::*;
|
use self::CacheDirective::*;
|
||||||
fmt::Display::fmt(
|
|
||||||
match *self {
|
|
||||||
NoCache => "no-cache",
|
|
||||||
NoStore => "no-store",
|
|
||||||
NoTransform => "no-transform",
|
|
||||||
OnlyIfCached => "only-if-cached",
|
|
||||||
|
|
||||||
MaxAge(secs) => return write!(f, "max-age={}", secs),
|
let dir_str = match self {
|
||||||
MaxStale(secs) => return write!(f, "max-stale={}", secs),
|
NoCache => "no-cache",
|
||||||
MinFresh(secs) => return write!(f, "min-fresh={}", secs),
|
NoStore => "no-store",
|
||||||
|
NoTransform => "no-transform",
|
||||||
|
OnlyIfCached => "only-if-cached",
|
||||||
|
|
||||||
MustRevalidate => "must-revalidate",
|
MaxAge(secs) => return write!(f, "max-age={}", secs),
|
||||||
Public => "public",
|
MaxStale(secs) => return write!(f, "max-stale={}", secs),
|
||||||
Private => "private",
|
MinFresh(secs) => return write!(f, "min-fresh={}", secs),
|
||||||
ProxyRevalidate => "proxy-revalidate",
|
|
||||||
SMaxAge(secs) => return write!(f, "s-maxage={}", secs),
|
|
||||||
|
|
||||||
Extension(ref name, None) => &name[..],
|
MustRevalidate => "must-revalidate",
|
||||||
Extension(ref name, Some(ref arg)) => {
|
Public => "public",
|
||||||
return write!(f, "{}={}", name, arg);
|
Private => "private",
|
||||||
}
|
ProxyRevalidate => "proxy-revalidate",
|
||||||
},
|
SMaxAge(secs) => return write!(f, "s-maxage={}", secs),
|
||||||
f,
|
|
||||||
)
|
Extension(name, None) => name.as_str(),
|
||||||
|
Extension(name, Some(arg)) => return write!(f, "{}={}", name, arg),
|
||||||
|
};
|
||||||
|
|
||||||
|
f.write_str(dir_str)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromStr for CacheDirective {
|
impl str::FromStr for CacheDirective {
|
||||||
type Err = Option<<u32 as FromStr>::Err>;
|
type Err = Option<<u32 as str::FromStr>::Err>;
|
||||||
fn from_str(s: &str) -> Result<CacheDirective, Option<<u32 as FromStr>::Err>> {
|
|
||||||
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||||
use self::CacheDirective::*;
|
use self::CacheDirective::*;
|
||||||
|
|
||||||
match s {
|
match s {
|
||||||
|
"" => Err(None),
|
||||||
|
|
||||||
"no-cache" => Ok(NoCache),
|
"no-cache" => Ok(NoCache),
|
||||||
"no-store" => Ok(NoStore),
|
"no-store" => Ok(NoStore),
|
||||||
"no-transform" => Ok(NoTransform),
|
"no-transform" => Ok(NoTransform),
|
||||||
|
@ -166,7 +173,7 @@ impl FromStr for CacheDirective {
|
||||||
"public" => Ok(Public),
|
"public" => Ok(Public),
|
||||||
"private" => Ok(Private),
|
"private" => Ok(Private),
|
||||||
"proxy-revalidate" => Ok(ProxyRevalidate),
|
"proxy-revalidate" => Ok(ProxyRevalidate),
|
||||||
"" => Err(None),
|
|
||||||
_ => match s.find('=') {
|
_ => match s.find('=') {
|
||||||
Some(idx) if idx + 1 < s.len() => {
|
Some(idx) if idx + 1 < s.len() => {
|
||||||
match (&s[..idx], (&s[idx + 1..]).trim_matches('"')) {
|
match (&s[..idx], (&s[idx + 1..]).trim_matches('"')) {
|
||||||
|
@ -183,76 +190,3 @@ impl FromStr for CacheDirective {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use super::*;
|
|
||||||
use crate::http::header::Header;
|
|
||||||
use actix_http::test::TestRequest;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_parse_multiple_headers() {
|
|
||||||
let req = TestRequest::default()
|
|
||||||
.insert_header((header::CACHE_CONTROL, "no-cache, private"))
|
|
||||||
.finish();
|
|
||||||
let cache = Header::parse(&req);
|
|
||||||
assert_eq!(
|
|
||||||
cache.ok(),
|
|
||||||
Some(CacheControl(vec![
|
|
||||||
CacheDirective::NoCache,
|
|
||||||
CacheDirective::Private,
|
|
||||||
]))
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_parse_argument() {
|
|
||||||
let req = TestRequest::default()
|
|
||||||
.insert_header((header::CACHE_CONTROL, "max-age=100, private"))
|
|
||||||
.finish();
|
|
||||||
let cache = Header::parse(&req);
|
|
||||||
assert_eq!(
|
|
||||||
cache.ok(),
|
|
||||||
Some(CacheControl(vec![
|
|
||||||
CacheDirective::MaxAge(100),
|
|
||||||
CacheDirective::Private,
|
|
||||||
]))
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_parse_quote_form() {
|
|
||||||
let req = TestRequest::default()
|
|
||||||
.insert_header((header::CACHE_CONTROL, "max-age=\"200\""))
|
|
||||||
.finish();
|
|
||||||
let cache = Header::parse(&req);
|
|
||||||
assert_eq!(
|
|
||||||
cache.ok(),
|
|
||||||
Some(CacheControl(vec![CacheDirective::MaxAge(200)]))
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_parse_extension() {
|
|
||||||
let req = TestRequest::default()
|
|
||||||
.insert_header((header::CACHE_CONTROL, "foo, bar=baz"))
|
|
||||||
.finish();
|
|
||||||
let cache = Header::parse(&req);
|
|
||||||
assert_eq!(
|
|
||||||
cache.ok(),
|
|
||||||
Some(CacheControl(vec![
|
|
||||||
CacheDirective::Extension("foo".to_owned(), None),
|
|
||||||
CacheDirective::Extension("bar".to_owned(), Some("baz".to_owned())),
|
|
||||||
]))
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_parse_bad_syntax() {
|
|
||||||
let req = TestRequest::default()
|
|
||||||
.insert_header((header::CACHE_CONTROL, "foo="))
|
|
||||||
.finish();
|
|
||||||
let cache: Result<CacheControl, _> = Header::parse(&req);
|
|
||||||
assert_eq!(cache.ok(), None)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -220,7 +220,7 @@ impl DispositionParam {
|
||||||
/// itself, *Content-Disposition* has no effect.
|
/// itself, *Content-Disposition* has no effect.
|
||||||
///
|
///
|
||||||
/// # ABNF
|
/// # ABNF
|
||||||
/// ```text
|
/// ```plain
|
||||||
/// content-disposition = "Content-Disposition" ":"
|
/// content-disposition = "Content-Disposition" ":"
|
||||||
/// disposition-type *( ";" disposition-parm )
|
/// disposition-type *( ";" disposition-parm )
|
||||||
///
|
///
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
use super::{QualityItem, CONTENT_LANGUAGE};
|
|
||||||
use language_tags::LanguageTag;
|
use language_tags::LanguageTag;
|
||||||
|
|
||||||
crate::http::header::common_header! {
|
use super::{common_header, QualityItem, CONTENT_LANGUAGE};
|
||||||
|
|
||||||
|
common_header! {
|
||||||
/// `Content-Language` header, defined
|
/// `Content-Language` header, defined
|
||||||
/// in [RFC 7231 §3.1.3.2](https://datatracker.ietf.org/doc/html/rfc7231#section-3.1.3.2)
|
/// in [RFC 7231 §3.1.3.2](https://datatracker.ietf.org/doc/html/rfc7231#section-3.1.3.2)
|
||||||
///
|
///
|
||||||
|
@ -11,7 +12,7 @@ crate::http::header::common_header! {
|
||||||
/// representation.
|
/// representation.
|
||||||
///
|
///
|
||||||
/// # ABNF
|
/// # ABNF
|
||||||
/// ```text
|
/// ```plain
|
||||||
/// Content-Language = 1#language-tag
|
/// Content-Language = 1#language-tag
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
|
|
|
@ -75,7 +75,7 @@ crate::http::header::common_header! {
|
||||||
/// in [RFC 7233 §4.2](https://datatracker.ietf.org/doc/html/rfc7233#section-4.2)
|
/// in [RFC 7233 §4.2](https://datatracker.ietf.org/doc/html/rfc7233#section-4.2)
|
||||||
///
|
///
|
||||||
/// # ABNF
|
/// # ABNF
|
||||||
/// ```text
|
/// ```plain
|
||||||
/// Content-Range = byte-content-range
|
/// Content-Range = byte-content-range
|
||||||
/// / other-content-range
|
/// / other-content-range
|
||||||
///
|
///
|
||||||
|
@ -91,7 +91,7 @@ crate::http::header::common_header! {
|
||||||
/// other-content-range = other-range-unit SP other-range-resp
|
/// other-content-range = other-range-unit SP other-range-resp
|
||||||
/// other-range-resp = *CHAR
|
/// other-range-resp = *CHAR
|
||||||
/// ```
|
/// ```
|
||||||
#[derive(PartialEq, Clone, Debug)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub enum ContentRangeSpec {
|
pub enum ContentRangeSpec {
|
||||||
/// Byte range
|
/// Byte range
|
||||||
Bytes {
|
Bytes {
|
||||||
|
|
|
@ -18,7 +18,7 @@ crate::http::header::common_header! {
|
||||||
/// this is an issue, it's possible to implement `Header` on a custom struct.
|
/// this is an issue, it's possible to implement `Header` on a custom struct.
|
||||||
///
|
///
|
||||||
/// # ABNF
|
/// # ABNF
|
||||||
/// ```text
|
/// ```plain
|
||||||
/// Content-Type = media-type
|
/// Content-Type = media-type
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
|
@ -110,5 +110,3 @@ impl ContentType {
|
||||||
ContentType(mime::APPLICATION_OCTET_STREAM)
|
ContentType(mime::APPLICATION_OCTET_STREAM)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Eq for ContentType {}
|
|
||||||
|
|
|
@ -9,7 +9,7 @@ crate::http::header::common_header! {
|
||||||
/// message was originated.
|
/// message was originated.
|
||||||
///
|
///
|
||||||
/// # ABNF
|
/// # ABNF
|
||||||
/// ```text
|
/// ```plain
|
||||||
/// Date = HTTP-date
|
/// Date = HTTP-date
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
|
|
|
@ -4,26 +4,33 @@ pub use self::Encoding::{
|
||||||
Brotli, Chunked, Compress, Deflate, EncodingExt, Gzip, Identity, Trailers, Zstd,
|
Brotli, Chunked, Compress, Deflate, EncodingExt, Gzip, Identity, Trailers, Zstd,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// A value to represent an encoding used in `Transfer-Encoding`
|
/// A value to represent an encoding used in `Transfer-Encoding` or `Accept-Encoding` header.
|
||||||
/// or `Accept-Encoding` header.
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
#[derive(Clone, PartialEq, Debug)]
|
|
||||||
pub enum Encoding {
|
pub enum Encoding {
|
||||||
/// The `chunked` encoding.
|
/// The `chunked` encoding.
|
||||||
Chunked,
|
Chunked,
|
||||||
|
|
||||||
/// The `br` encoding.
|
/// The `br` encoding.
|
||||||
Brotli,
|
Brotli,
|
||||||
|
|
||||||
/// The `gzip` encoding.
|
/// The `gzip` encoding.
|
||||||
Gzip,
|
Gzip,
|
||||||
|
|
||||||
/// The `deflate` encoding.
|
/// The `deflate` encoding.
|
||||||
Deflate,
|
Deflate,
|
||||||
|
|
||||||
/// The `compress` encoding.
|
/// The `compress` encoding.
|
||||||
Compress,
|
Compress,
|
||||||
|
|
||||||
/// The `identity` encoding.
|
/// The `identity` encoding.
|
||||||
Identity,
|
Identity,
|
||||||
|
|
||||||
/// The `trailers` encoding.
|
/// The `trailers` encoding.
|
||||||
Trailers,
|
Trailers,
|
||||||
|
|
||||||
/// The `zstd` encoding.
|
/// The `zstd` encoding.
|
||||||
Zstd,
|
Zstd,
|
||||||
|
|
||||||
/// Some other encoding that is less common, can be any String.
|
/// Some other encoding that is less common, can be any String.
|
||||||
EncodingExt(String),
|
EncodingExt(String),
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,7 +26,7 @@ fn check_slice_validity(slice: &str) -> bool {
|
||||||
/// `W/"xyzzy"`.
|
/// `W/"xyzzy"`.
|
||||||
///
|
///
|
||||||
/// # ABNF
|
/// # ABNF
|
||||||
/// ```text
|
/// ```plain
|
||||||
/// entity-tag = [ weak ] opaque-tag
|
/// entity-tag = [ weak ] opaque-tag
|
||||||
/// weak = %x57.2F ; "W/", case-sensitive
|
/// weak = %x57.2F ; "W/", case-sensitive
|
||||||
/// opaque-tag = DQUOTE *etagc DQUOTE
|
/// opaque-tag = DQUOTE *etagc DQUOTE
|
||||||
|
|
|
@ -15,7 +15,7 @@ crate::http::header::common_header! {
|
||||||
/// prefixed by a weakness indicator.
|
/// prefixed by a weakness indicator.
|
||||||
///
|
///
|
||||||
/// # ABNF
|
/// # ABNF
|
||||||
/// ```text
|
/// ```plain
|
||||||
/// ETag = entity-tag
|
/// ETag = entity-tag
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
|
|
|
@ -12,7 +12,7 @@ crate::http::header::common_header! {
|
||||||
/// time.
|
/// time.
|
||||||
///
|
///
|
||||||
/// # ABNF
|
/// # ABNF
|
||||||
/// ```text
|
/// ```plain
|
||||||
/// Expires = HTTP-date
|
/// Expires = HTTP-date
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use super::{EntityTag, IF_MATCH};
|
use super::{common_header, EntityTag, IF_MATCH};
|
||||||
|
|
||||||
crate::http::header::common_header! {
|
common_header! {
|
||||||
/// `If-Match` header, defined
|
/// `If-Match` header, defined
|
||||||
/// in [RFC 7232 §3.1](https://datatracker.ietf.org/doc/html/rfc7232#section-3.1)
|
/// in [RFC 7232 §3.1](https://datatracker.ietf.org/doc/html/rfc7232#section-3.1)
|
||||||
///
|
///
|
||||||
|
@ -17,7 +17,7 @@ crate::http::header::common_header! {
|
||||||
/// there have been any changes to the representation data.
|
/// there have been any changes to the representation data.
|
||||||
///
|
///
|
||||||
/// # ABNF
|
/// # ABNF
|
||||||
/// ```text
|
/// ```plain
|
||||||
/// If-Match = "*" / 1#entity-tag
|
/// If-Match = "*" / 1#entity-tag
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
|
|
|
@ -11,7 +11,7 @@ crate::http::header::common_header! {
|
||||||
/// data has not changed.
|
/// data has not changed.
|
||||||
///
|
///
|
||||||
/// # ABNF
|
/// # ABNF
|
||||||
/// ```text
|
/// ```plain
|
||||||
/// If-Unmodified-Since = HTTP-date
|
/// If-Unmodified-Since = HTTP-date
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
|
|
|
@ -16,7 +16,7 @@ crate::http::header::common_header! {
|
||||||
/// the representation data.
|
/// the representation data.
|
||||||
///
|
///
|
||||||
/// # ABNF
|
/// # ABNF
|
||||||
/// ```text
|
/// ```plain
|
||||||
/// If-None-Match = "*" / 1#entity-tag
|
/// If-None-Match = "*" / 1#entity-tag
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
|
|
|
@ -25,7 +25,7 @@ use crate::HttpMessage;
|
||||||
/// in Range; otherwise, send me the entire representation.
|
/// in Range; otherwise, send me the entire representation.
|
||||||
///
|
///
|
||||||
/// # ABNF
|
/// # ABNF
|
||||||
/// ```text
|
/// ```plain
|
||||||
/// If-Range = entity-tag / HTTP-date
|
/// If-Range = entity-tag / HTTP-date
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
|
@ -107,10 +107,11 @@ impl IntoHeaderValue for IfRange {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test_if_range {
|
mod test_parse_and_format {
|
||||||
|
use std::str;
|
||||||
|
|
||||||
use super::IfRange as HeaderField;
|
use super::IfRange as HeaderField;
|
||||||
use crate::http::header::*;
|
use crate::http::header::*;
|
||||||
use std::str;
|
|
||||||
|
|
||||||
crate::http::header::common_header_test!(test1, vec![b"Sat, 29 Oct 1994 19:43:31 GMT"]);
|
crate::http::header::common_header_test!(test1, vec![b"Sat, 29 Oct 1994 19:43:31 GMT"]);
|
||||||
crate::http::header::common_header_test!(test2, vec![b"\"abc\""]);
|
crate::http::header::common_header_test!(test2, vec![b"\"abc\""]);
|
||||||
|
|
|
@ -11,7 +11,7 @@ crate::http::header::common_header! {
|
||||||
/// the user agent does not have an entity-tag for the representation.
|
/// the user agent does not have an entity-tag for the representation.
|
||||||
///
|
///
|
||||||
/// # ABNF
|
/// # ABNF
|
||||||
/// ```text
|
/// ```plain
|
||||||
/// If-Unmodified-Since = HTTP-date
|
/// If-Unmodified-Since = HTTP-date
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
|
|
|
@ -10,7 +10,7 @@ crate::http::header::common_header! {
|
||||||
/// conclusion of handling the request.
|
/// conclusion of handling the request.
|
||||||
///
|
///
|
||||||
/// # ABNF
|
/// # ABNF
|
||||||
/// ```text
|
/// ```plain
|
||||||
/// Expires = HTTP-date
|
/// Expires = HTTP-date
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
|
|
|
@ -1,33 +1,17 @@
|
||||||
macro_rules! common_header_deref {
|
|
||||||
($from:ty => $to:ty) => {
|
|
||||||
impl ::std::ops::Deref for $from {
|
|
||||||
type Target = $to;
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
fn deref(&self) -> &$to {
|
|
||||||
&self.0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ::std::ops::DerefMut for $from {
|
|
||||||
#[inline]
|
|
||||||
fn deref_mut(&mut self) -> &mut $to {
|
|
||||||
&mut self.0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
macro_rules! common_header_test_module {
|
macro_rules! common_header_test_module {
|
||||||
($id:ident, $tm:ident{$($tf:item)*}) => {
|
($id:ident, $tm:ident{$($tf:item)*}) => {
|
||||||
#[allow(unused_imports)]
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod $tm {
|
mod $tm {
|
||||||
use std::str;
|
#![allow(unused_imports)]
|
||||||
use actix_http::http::Method;
|
|
||||||
use mime::*;
|
use ::core::str;
|
||||||
use $crate::http::header::*;
|
|
||||||
use super::$id as HeaderField;
|
use ::actix_http::{http::Method, test};
|
||||||
|
use ::mime::*;
|
||||||
|
|
||||||
|
use $crate::http::header::{self, *};
|
||||||
|
use super::{$id as HeaderField, *};
|
||||||
|
|
||||||
$($tf)*
|
$($tf)*
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -38,18 +22,23 @@ macro_rules! common_header_test {
|
||||||
($id:ident, $raw:expr) => {
|
($id:ident, $raw:expr) => {
|
||||||
#[test]
|
#[test]
|
||||||
fn $id() {
|
fn $id() {
|
||||||
use actix_http::test;
|
use ::actix_http::test;
|
||||||
|
|
||||||
let raw = $raw;
|
let raw = $raw;
|
||||||
let a: Vec<Vec<u8>> = raw.iter().map(|x| x.to_vec()).collect();
|
let headers = raw.iter().map(|x| x.to_vec()).collect::<Vec<_>>();
|
||||||
|
|
||||||
let mut req = test::TestRequest::default();
|
let mut req = test::TestRequest::default();
|
||||||
for item in a {
|
|
||||||
req = req.insert_header((HeaderField::name(), item)).take();
|
for item in headers {
|
||||||
|
req = req.append_header((HeaderField::name(), item)).take();
|
||||||
}
|
}
|
||||||
|
|
||||||
let req = req.finish();
|
let req = req.finish();
|
||||||
let value = HeaderField::parse(&req);
|
let value = HeaderField::parse(&req);
|
||||||
|
|
||||||
let result = format!("{}", value.unwrap());
|
let result = format!("{}", value.unwrap());
|
||||||
let expected = String::from_utf8(raw[0].to_vec()).unwrap();
|
let expected = ::std::string::String::from_utf8(raw[0].to_vec()).unwrap();
|
||||||
|
|
||||||
let result_cmp: Vec<String> = result
|
let result_cmp: Vec<String> = result
|
||||||
.to_ascii_lowercase()
|
.to_ascii_lowercase()
|
||||||
.split(' ')
|
.split(' ')
|
||||||
|
@ -60,154 +49,181 @@ macro_rules! common_header_test {
|
||||||
.split(' ')
|
.split(' ')
|
||||||
.map(|x| x.to_owned())
|
.map(|x| x.to_owned())
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
assert_eq!(result_cmp.concat(), expected_cmp.concat());
|
assert_eq!(result_cmp.concat(), expected_cmp.concat());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
($id:ident, $raw:expr, $typed:expr) => {
|
|
||||||
|
($id:ident, $raw:expr, $exp:expr) => {
|
||||||
#[test]
|
#[test]
|
||||||
fn $id() {
|
fn $id() {
|
||||||
use actix_http::test;
|
use actix_http::test;
|
||||||
|
|
||||||
let a: Vec<Vec<u8>> = $raw.iter().map(|x| x.to_vec()).collect();
|
let headers = $raw.iter().map(|x| x.to_vec()).collect::<Vec<_>>();
|
||||||
let mut req = test::TestRequest::default();
|
let mut req = test::TestRequest::default();
|
||||||
for item in a {
|
|
||||||
req.insert_header((HeaderField::name(), item));
|
for item in headers {
|
||||||
|
req.append_header((HeaderField::name(), item));
|
||||||
}
|
}
|
||||||
|
|
||||||
let req = req.finish();
|
let req = req.finish();
|
||||||
let val = HeaderField::parse(&req);
|
let val = HeaderField::parse(&req);
|
||||||
let typed: Option<HeaderField> = $typed;
|
|
||||||
// Test parsing
|
let exp: ::core::option::Option<HeaderField> = $exp;
|
||||||
assert_eq!(val.ok(), typed);
|
|
||||||
// Test formatting
|
// test parsing
|
||||||
if typed.is_some() {
|
assert_eq!(val.ok(), exp);
|
||||||
|
|
||||||
|
// test formatting
|
||||||
|
if let Some(exp) = exp {
|
||||||
let raw = &($raw)[..];
|
let raw = &($raw)[..];
|
||||||
let mut iter = raw.iter().map(|b| str::from_utf8(&b[..]).unwrap());
|
let mut iter = raw.iter().map(|b| str::from_utf8(&b[..]).unwrap());
|
||||||
let mut joined = String::new();
|
let mut joined = String::new();
|
||||||
joined.push_str(iter.next().unwrap());
|
if let Some(s) = iter.next() {
|
||||||
for s in iter {
|
|
||||||
joined.push_str(", ");
|
|
||||||
joined.push_str(s);
|
joined.push_str(s);
|
||||||
|
for s in iter {
|
||||||
|
joined.push_str(", ");
|
||||||
|
joined.push_str(s);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
assert_eq!(format!("{}", typed.unwrap()), joined);
|
assert_eq!(format!("{}", exp), joined);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! common_header {
|
macro_rules! common_header {
|
||||||
// $a:meta: Attributes associated with the header item (usually docs)
|
// TODO: these docs are wrong, there's no $n or $nn
|
||||||
|
// $attrs:meta: Attributes associated with the header item (usually docs)
|
||||||
// $id:ident: Identifier of the header
|
// $id:ident: Identifier of the header
|
||||||
// $n:expr: Lowercase name of the header
|
// $n:expr: Lowercase name of the header
|
||||||
// $nn:expr: Nice name of the header
|
// $nn:expr: Nice name of the header
|
||||||
|
|
||||||
// List header, zero or more items
|
// List header, zero or more items
|
||||||
($(#[$a:meta])*($id:ident, $name:expr) => ($item:ty)*) => {
|
($(#[$attrs:meta])*($id:ident, $name:expr) => ($item:ty)*) => {
|
||||||
$(#[$a])*
|
$(#[$attrs])*
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Debug, Clone, PartialEq, Eq, ::derive_more::Deref, ::derive_more::DerefMut)]
|
||||||
pub struct $id(pub Vec<$item>);
|
pub struct $id(pub Vec<$item>);
|
||||||
crate::http::header::common_header_deref!($id => Vec<$item>);
|
|
||||||
impl $crate::http::header::Header for $id {
|
impl $crate::http::header::Header for $id {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn name() -> $crate::http::header::HeaderName {
|
fn name() -> $crate::http::header::HeaderName {
|
||||||
$name
|
$name
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn parse<T>(msg: &T) -> Result<Self, $crate::error::ParseError>
|
fn parse<M: $crate::HttpMessage>(msg: &M) -> Result<Self, $crate::error::ParseError> {
|
||||||
where T: $crate::HttpMessage
|
let headers = msg.headers().get_all(Self::name());
|
||||||
{
|
$crate::http::header::from_comma_delimited(headers).map($id)
|
||||||
$crate::http::header::from_comma_delimited(
|
|
||||||
msg.headers().get_all(Self::name())).map($id)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl std::fmt::Display for $id {
|
|
||||||
|
impl ::core::fmt::Display for $id {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> ::std::fmt::Result {
|
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
|
||||||
$crate::http::header::fmt_comma_delimited(f, &self.0[..])
|
$crate::http::header::fmt_comma_delimited(f, &self.0[..])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl $crate::http::header::IntoHeaderValue for $id {
|
impl $crate::http::header::IntoHeaderValue for $id {
|
||||||
type Error = $crate::http::header::InvalidHeaderValue;
|
type Error = $crate::http::header::InvalidHeaderValue;
|
||||||
|
|
||||||
|
#[inline]
|
||||||
fn try_into_value(self) -> Result<$crate::http::header::HeaderValue, Self::Error> {
|
fn try_into_value(self) -> Result<$crate::http::header::HeaderValue, Self::Error> {
|
||||||
use std::fmt::Write;
|
use ::core::fmt::Write;
|
||||||
let mut writer = $crate::http::header::Writer::new();
|
let mut writer = $crate::http::header::Writer::new();
|
||||||
let _ = write!(&mut writer, "{}", self);
|
let _ = write!(&mut writer, "{}", self);
|
||||||
$crate::http::header::HeaderValue::from_maybe_shared(writer.take())
|
$crate::http::header::HeaderValue::from_maybe_shared(writer.take())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// List header, one or more items
|
// List header, one or more items
|
||||||
($(#[$a:meta])*($id:ident, $name:expr) => ($item:ty)+) => {
|
($(#[$attrs:meta])*($id:ident, $name:expr) => ($item:ty)+) => {
|
||||||
$(#[$a])*
|
$(#[$attrs])*
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Debug, Clone, PartialEq, Eq, ::derive_more::Deref, ::derive_more::DerefMut)]
|
||||||
pub struct $id(pub Vec<$item>);
|
pub struct $id(pub Vec<$item>);
|
||||||
crate::http::header::common_header_deref!($id => Vec<$item>);
|
|
||||||
impl $crate::http::header::Header for $id {
|
impl $crate::http::header::Header for $id {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn name() -> $crate::http::header::HeaderName {
|
fn name() -> $crate::http::header::HeaderName {
|
||||||
$name
|
$name
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn parse<T>(msg: &T) -> Result<Self, $crate::error::ParseError>
|
fn parse<M: $crate::HttpMessage>(msg: &M) -> Result<Self, $crate::error::ParseError>{
|
||||||
where T: $crate::HttpMessage
|
let headers = msg.headers().get_all(Self::name());
|
||||||
{
|
|
||||||
$crate::http::header::from_comma_delimited(
|
$crate::http::header::from_comma_delimited(headers)
|
||||||
msg.headers().get_all(Self::name())).map($id)
|
.and_then(|items| {
|
||||||
|
if items.is_empty() {
|
||||||
|
Err($crate::error::ParseError::Header)
|
||||||
|
} else {
|
||||||
|
Ok($id(items))
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl std::fmt::Display for $id {
|
|
||||||
|
impl ::core::fmt::Display for $id {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
|
||||||
$crate::http::header::fmt_comma_delimited(f, &self.0[..])
|
$crate::http::header::fmt_comma_delimited(f, &self.0[..])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl $crate::http::header::IntoHeaderValue for $id {
|
impl $crate::http::header::IntoHeaderValue for $id {
|
||||||
type Error = $crate::http::header::InvalidHeaderValue;
|
type Error = $crate::http::header::InvalidHeaderValue;
|
||||||
|
|
||||||
|
#[inline]
|
||||||
fn try_into_value(self) -> Result<$crate::http::header::HeaderValue, Self::Error> {
|
fn try_into_value(self) -> Result<$crate::http::header::HeaderValue, Self::Error> {
|
||||||
use std::fmt::Write;
|
use ::core::fmt::Write;
|
||||||
let mut writer = $crate::http::header::Writer::new();
|
let mut writer = $crate::http::header::Writer::new();
|
||||||
let _ = write!(&mut writer, "{}", self);
|
let _ = write!(&mut writer, "{}", self);
|
||||||
$crate::http::header::HeaderValue::from_maybe_shared(writer.take())
|
$crate::http::header::HeaderValue::from_maybe_shared(writer.take())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Single value header
|
// Single value header
|
||||||
($(#[$a:meta])*($id:ident, $name:expr) => [$value:ty]) => {
|
($(#[$attrs:meta])*($id:ident, $name:expr) => [$value:ty]) => {
|
||||||
$(#[$a])*
|
$(#[$attrs])*
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Debug, Clone, PartialEq, Eq, ::derive_more::Deref, ::derive_more::DerefMut)]
|
||||||
pub struct $id(pub $value);
|
pub struct $id(pub $value);
|
||||||
crate::http::header::common_header_deref!($id => $value);
|
|
||||||
impl $crate::http::header::Header for $id {
|
impl $crate::http::header::Header for $id {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn name() -> $crate::http::header::HeaderName {
|
fn name() -> $crate::http::header::HeaderName {
|
||||||
$name
|
$name
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn parse<T>(msg: &T) -> Result<Self, $crate::error::ParseError>
|
fn parse<M: $crate::HttpMessage>(msg: &M) -> Result<Self, $crate::error::ParseError> {
|
||||||
where T: $crate::HttpMessage
|
let header = msg.headers().get(Self::name());
|
||||||
{
|
$crate::http::header::from_one_raw_str(header).map($id)
|
||||||
$crate::http::header::from_one_raw_str(
|
|
||||||
msg.headers().get(Self::name())).map($id)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl std::fmt::Display for $id {
|
|
||||||
|
impl ::core::fmt::Display for $id {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
|
||||||
std::fmt::Display::fmt(&self.0, f)
|
::core::fmt::Display::fmt(&self.0, f)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl $crate::http::header::IntoHeaderValue for $id {
|
impl $crate::http::header::IntoHeaderValue for $id {
|
||||||
type Error = $crate::http::header::InvalidHeaderValue;
|
type Error = $crate::http::header::InvalidHeaderValue;
|
||||||
|
|
||||||
|
#[inline]
|
||||||
fn try_into_value(self) -> Result<$crate::http::header::HeaderValue, Self::Error> {
|
fn try_into_value(self) -> Result<$crate::http::header::HeaderValue, Self::Error> {
|
||||||
self.0.try_into_value()
|
self.0.try_into_value()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// List header, one or more items with "*" option
|
// List header, one or more items with "*" option
|
||||||
($(#[$a:meta])*($id:ident, $name:expr) => {Any / ($item:ty)+}) => {
|
($(#[$attrs:meta])*($id:ident, $name:expr) => {Any / ($item:ty)+}) => {
|
||||||
$(#[$a])*
|
$(#[$attrs])*
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
pub enum $id {
|
pub enum $id {
|
||||||
/// Any value is a match
|
/// Any value is a match
|
||||||
|
@ -215,42 +231,47 @@ macro_rules! common_header {
|
||||||
/// Only the listed items are a match
|
/// Only the listed items are a match
|
||||||
Items(Vec<$item>),
|
Items(Vec<$item>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl $crate::http::header::Header for $id {
|
impl $crate::http::header::Header for $id {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn name() -> $crate::http::header::HeaderName {
|
fn name() -> $crate::http::header::HeaderName {
|
||||||
$name
|
$name
|
||||||
}
|
}
|
||||||
#[inline]
|
|
||||||
fn parse<T>(msg: &T) -> Result<Self, $crate::error::ParseError>
|
|
||||||
where T: $crate::HttpMessage
|
|
||||||
{
|
|
||||||
let any = msg.headers().get(Self::name()).and_then(|hdr| {
|
|
||||||
hdr.to_str().ok().and_then(|hdr| Some(hdr.trim() == "*"))});
|
|
||||||
|
|
||||||
if let Some(true) = any {
|
#[inline]
|
||||||
|
fn parse<M: $crate::HttpMessage>(msg: &M) -> Result<Self, $crate::error::ParseError> {
|
||||||
|
let is_any = msg
|
||||||
|
.headers()
|
||||||
|
.get(Self::name())
|
||||||
|
.and_then(|hdr| hdr.to_str().ok())
|
||||||
|
.map(|hdr| hdr.trim() == "*");
|
||||||
|
|
||||||
|
if let Some(true) = is_any {
|
||||||
Ok($id::Any)
|
Ok($id::Any)
|
||||||
} else {
|
} else {
|
||||||
Ok($id::Items(
|
let headers = msg.headers().get_all(Self::name());
|
||||||
$crate::http::header::from_comma_delimited(
|
Ok($id::Items($crate::http::header::from_comma_delimited(headers)?))
|
||||||
msg.headers().get_all(Self::name()))?))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl std::fmt::Display for $id {
|
|
||||||
|
impl ::core::fmt::Display for $id {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
|
||||||
match *self {
|
match *self {
|
||||||
$id::Any => f.write_str("*"),
|
$id::Any => f.write_str("*"),
|
||||||
$id::Items(ref fields) => $crate::http::header::fmt_comma_delimited(
|
$id::Items(ref fields) =>
|
||||||
f, &fields[..])
|
$crate::http::header::fmt_comma_delimited(f, &fields[..])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl $crate::http::header::IntoHeaderValue for $id {
|
impl $crate::http::header::IntoHeaderValue for $id {
|
||||||
type Error = $crate::http::header::InvalidHeaderValue;
|
type Error = $crate::http::header::InvalidHeaderValue;
|
||||||
|
|
||||||
|
#[inline]
|
||||||
fn try_into_value(self) -> Result<$crate::http::header::HeaderValue, Self::Error> {
|
fn try_into_value(self) -> Result<$crate::http::header::HeaderValue, Self::Error> {
|
||||||
use std::fmt::Write;
|
use ::core::fmt::Write;
|
||||||
let mut writer = $crate::http::header::Writer::new();
|
let mut writer = $crate::http::header::Writer::new();
|
||||||
let _ = write!(&mut writer, "{}", self);
|
let _ = write!(&mut writer, "{}", self);
|
||||||
$crate::http::header::HeaderValue::from_maybe_shared(writer.take())
|
$crate::http::header::HeaderValue::from_maybe_shared(writer.take())
|
||||||
|
@ -259,32 +280,32 @@ macro_rules! common_header {
|
||||||
};
|
};
|
||||||
|
|
||||||
// optional test module
|
// optional test module
|
||||||
($(#[$a:meta])*($id:ident, $name:expr) => ($item:ty)* $tm:ident{$($tf:item)*}) => {
|
($(#[$attrs:meta])*($id:ident, $name:expr) => ($item:ty)* $tm:ident{$($tf:item)*}) => {
|
||||||
crate::http::header::common_header! {
|
crate::http::header::common_header! {
|
||||||
$(#[$a])*
|
$(#[$attrs])*
|
||||||
($id, $name) => ($item)*
|
($id, $name) => ($item)*
|
||||||
}
|
}
|
||||||
|
|
||||||
crate::http::header::common_header_test_module! { $id, $tm { $($tf)* }}
|
crate::http::header::common_header_test_module! { $id, $tm { $($tf)* }}
|
||||||
};
|
};
|
||||||
($(#[$a:meta])*($id:ident, $n:expr) => ($item:ty)+ $tm:ident{$($tf:item)*}) => {
|
($(#[$attrs:meta])*($id:ident, $n:expr) => ($item:ty)+ $tm:ident{$($tf:item)*}) => {
|
||||||
crate::http::header::common_header! {
|
crate::http::header::common_header! {
|
||||||
$(#[$a])*
|
$(#[$attrs])*
|
||||||
($id, $n) => ($item)+
|
($id, $n) => ($item)+
|
||||||
}
|
}
|
||||||
|
|
||||||
crate::http::header::common_header_test_module! { $id, $tm { $($tf)* }}
|
crate::http::header::common_header_test_module! { $id, $tm { $($tf)* }}
|
||||||
};
|
};
|
||||||
($(#[$a:meta])*($id:ident, $name:expr) => [$item:ty] $tm:ident{$($tf:item)*}) => {
|
($(#[$attrs:meta])*($id:ident, $name:expr) => [$item:ty] $tm:ident{$($tf:item)*}) => {
|
||||||
crate::http::header::common_header! {
|
crate::http::header::common_header! {
|
||||||
$(#[$a])* ($id, $name) => [$item]
|
$(#[$attrs])* ($id, $name) => [$item]
|
||||||
}
|
}
|
||||||
|
|
||||||
crate::http::header::common_header_test_module! { $id, $tm { $($tf)* }}
|
crate::http::header::common_header_test_module! { $id, $tm { $($tf)* }}
|
||||||
};
|
};
|
||||||
($(#[$a:meta])*($id:ident, $name:expr) => {Any / ($item:ty)+} $tm:ident{$($tf:item)*}) => {
|
($(#[$attrs:meta])*($id:ident, $name:expr) => {Any / ($item:ty)+} $tm:ident{$($tf:item)*}) => {
|
||||||
crate::http::header::common_header! {
|
crate::http::header::common_header! {
|
||||||
$(#[$a])*
|
$(#[$attrs])*
|
||||||
($id, $name) => {Any / ($item)+}
|
($id, $name) => {Any / ($item)+}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -292,7 +313,7 @@ macro_rules! common_header {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) use {common_header, common_header_deref, common_header_test_module};
|
pub(crate) use {common_header, common_header_test_module};
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
pub(crate) use common_header_test;
|
pub(crate) use common_header_test;
|
||||||
|
|
|
@ -1,17 +1,54 @@
|
||||||
//! A Collection of Header implementations for common HTTP Headers.
|
//! A Collection of Header implementations for common HTTP Headers.
|
||||||
//!
|
//!
|
||||||
//! ## Mime
|
//! ## Mime Types
|
||||||
//!
|
|
||||||
//! Several header fields use MIME values for their contents. Keeping with the strongly-typed theme,
|
//! Several header fields use MIME values for their contents. Keeping with the strongly-typed theme,
|
||||||
//! the [mime] crate is used in such headers as [`ContentType`] and [`Accept`].
|
//! the [mime] crate is used in such headers as [`ContentType`] and [`Accept`].
|
||||||
|
|
||||||
use bytes::{Bytes, BytesMut};
|
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
pub use self::accept_charset::AcceptCharset;
|
use bytes::{Bytes, BytesMut};
|
||||||
|
|
||||||
|
// re-export from actix-http
|
||||||
|
// - header name / value types
|
||||||
|
// - relevant traits for converting to header name / value
|
||||||
|
// - all const header names
|
||||||
|
// - header map
|
||||||
|
// - the few typed headers from actix-http
|
||||||
|
// - header parsing utils
|
||||||
pub use actix_http::http::header::*;
|
pub use actix_http::http::header::*;
|
||||||
//pub use self::accept_encoding::AcceptEncoding;
|
|
||||||
|
mod accept;
|
||||||
|
mod accept_charset;
|
||||||
|
mod accept_encoding;
|
||||||
|
mod accept_language;
|
||||||
|
mod allow;
|
||||||
|
mod cache_control;
|
||||||
|
mod content_disposition;
|
||||||
|
mod content_language;
|
||||||
|
mod content_range;
|
||||||
|
mod content_type;
|
||||||
|
mod date;
|
||||||
|
mod encoding;
|
||||||
|
mod entity;
|
||||||
|
mod etag;
|
||||||
|
mod expires;
|
||||||
|
mod if_match;
|
||||||
|
mod if_modified_since;
|
||||||
|
mod if_none_match;
|
||||||
|
mod if_range;
|
||||||
|
mod if_unmodified_since;
|
||||||
|
mod last_modified;
|
||||||
|
mod macros;
|
||||||
|
mod preference;
|
||||||
|
// mod range;
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
pub(crate) use macros::common_header_test;
|
||||||
|
pub(crate) use macros::{common_header, common_header_test_module};
|
||||||
|
|
||||||
pub use self::accept::Accept;
|
pub use self::accept::Accept;
|
||||||
|
pub use self::accept_charset::AcceptCharset;
|
||||||
|
pub use self::accept_encoding::AcceptEncoding;
|
||||||
pub use self::accept_language::AcceptLanguage;
|
pub use self::accept_language::AcceptLanguage;
|
||||||
pub use self::allow::Allow;
|
pub use self::allow::Allow;
|
||||||
pub use self::cache_control::{CacheControl, CacheDirective};
|
pub use self::cache_control::{CacheControl, CacheDirective};
|
||||||
|
@ -30,11 +67,10 @@ pub use self::if_none_match::IfNoneMatch;
|
||||||
pub use self::if_range::IfRange;
|
pub use self::if_range::IfRange;
|
||||||
pub use self::if_unmodified_since::IfUnmodifiedSince;
|
pub use self::if_unmodified_since::IfUnmodifiedSince;
|
||||||
pub use self::last_modified::LastModified;
|
pub use self::last_modified::LastModified;
|
||||||
|
pub use self::preference::Preference;
|
||||||
//pub use self::range::{Range, ByteRangeSpec};
|
//pub use self::range::{Range, ByteRangeSpec};
|
||||||
pub(crate) use actix_http::http::header::{
|
|
||||||
fmt_comma_delimited, from_comma_delimited, from_one_raw_str,
|
|
||||||
};
|
|
||||||
|
|
||||||
|
/// Format writer ([`fmt::Write`]) for a [`BytesMut`].
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
struct Writer {
|
struct Writer {
|
||||||
buf: BytesMut,
|
buf: BytesMut,
|
||||||
|
@ -62,30 +98,3 @@ impl fmt::Write for Writer {
|
||||||
fmt::write(self, args)
|
fmt::write(self, args)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mod accept_charset;
|
|
||||||
// mod accept_encoding;
|
|
||||||
mod accept;
|
|
||||||
mod accept_language;
|
|
||||||
mod allow;
|
|
||||||
mod cache_control;
|
|
||||||
mod content_disposition;
|
|
||||||
mod content_language;
|
|
||||||
mod content_range;
|
|
||||||
mod content_type;
|
|
||||||
mod date;
|
|
||||||
mod encoding;
|
|
||||||
mod entity;
|
|
||||||
mod etag;
|
|
||||||
mod expires;
|
|
||||||
mod if_match;
|
|
||||||
mod if_modified_since;
|
|
||||||
mod if_none_match;
|
|
||||||
mod if_range;
|
|
||||||
mod if_unmodified_since;
|
|
||||||
mod last_modified;
|
|
||||||
|
|
||||||
mod macros;
|
|
||||||
#[cfg(test)]
|
|
||||||
pub(crate) use macros::common_header_test;
|
|
||||||
pub(crate) use macros::{common_header, common_header_deref, common_header_test_module};
|
|
||||||
|
|
|
@ -0,0 +1,70 @@
|
||||||
|
use std::{
|
||||||
|
fmt::{self, Write as _},
|
||||||
|
str,
|
||||||
|
};
|
||||||
|
|
||||||
|
/// A wrapper for types used in header values where wildcard (`*`) items are allowed but the
|
||||||
|
/// underlying type does not support them.
|
||||||
|
///
|
||||||
|
/// For example, we use the `language-tags` crate for the [`AcceptLanguage`](super::AcceptLanguage)
|
||||||
|
/// typed header but it does not parse `*` successfully. On the other hand, the `mime` crate, used
|
||||||
|
/// for [`Accept`](super::Accept), has first-party support for wildcard items so this wrapper is not
|
||||||
|
/// used in those header types.
|
||||||
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Hash)]
|
||||||
|
pub enum Preference<T> {
|
||||||
|
/// A wildcard value.
|
||||||
|
Any,
|
||||||
|
|
||||||
|
/// A valid `T`.
|
||||||
|
Specific(T),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Preference<T> {
|
||||||
|
/// Returns true if preference is the any/wildcard (`*`) value.
|
||||||
|
pub fn is_any(&self) -> bool {
|
||||||
|
matches!(self, Self::Any)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns true if preference is the specific item (`T`) variant.
|
||||||
|
pub fn is_specific(&self) -> bool {
|
||||||
|
matches!(self, Self::Specific(_))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns reference to value in `Specific` variant, if it is set.
|
||||||
|
pub fn item(&self) -> Option<&T> {
|
||||||
|
match self {
|
||||||
|
Preference::Specific(ref item) => Some(item),
|
||||||
|
Preference::Any => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Consumes the container, returning the value in the `Specific` variant, if it is set.
|
||||||
|
pub fn into_item(self) -> Option<T> {
|
||||||
|
match self {
|
||||||
|
Preference::Specific(item) => Some(item),
|
||||||
|
Preference::Any => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: fmt::Display> fmt::Display for Preference<T> {
|
||||||
|
#[inline]
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
match self {
|
||||||
|
Preference::Any => f.write_char('*'),
|
||||||
|
Preference::Specific(item) => fmt::Display::fmt(item, f),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: str::FromStr> str::FromStr for Preference<T> {
|
||||||
|
type Err = T::Err;
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||||
|
match s.trim() {
|
||||||
|
"*" => Ok(Self::Any),
|
||||||
|
other => other.parse().map(Preference::Specific),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,8 +1,11 @@
|
||||||
use std::fmt::{self, Display};
|
// TODO: reinstate module
|
||||||
use std::str::FromStr;
|
|
||||||
|
|
||||||
use super::parsing::from_one_raw_str;
|
use std::{
|
||||||
use super::{Header, Raw};
|
fmt::{self, Display},
|
||||||
|
str::FromStr,
|
||||||
|
};
|
||||||
|
|
||||||
|
use super::{parsing::from_one_raw_str, Header, Raw};
|
||||||
|
|
||||||
/// `Range` header, defined
|
/// `Range` header, defined
|
||||||
/// in [RFC 7233 §3.1](https://datatracker.ietf.org/doc/html/rfc7233#section-3.1)
|
/// in [RFC 7233 §3.1](https://datatracker.ietf.org/doc/html/rfc7233#section-3.1)
|
||||||
|
@ -12,7 +15,7 @@ use super::{Header, Raw};
|
||||||
/// representation data.
|
/// representation data.
|
||||||
///
|
///
|
||||||
/// # ABNF
|
/// # ABNF
|
||||||
/// ```text
|
/// ```plain
|
||||||
/// Range = byte-ranges-specifier / other-ranges-specifier
|
/// Range = byte-ranges-specifier / other-ranges-specifier
|
||||||
/// other-ranges-specifier = other-range-unit "=" other-range-set
|
/// other-ranges-specifier = other-range-unit "=" other-range-set
|
||||||
/// other-range-set = 1*VCHAR
|
/// other-range-set = 1*VCHAR
|
||||||
|
|
Loading…
Reference in New Issue