mirror of
https://github.com/fafhrd91/actix-web
synced 2025-01-19 14:14:41 +01:00
Merge branch 'master' into improve-typed-header-macro
This commit is contained in:
commit
57ee49a618
@ -174,7 +174,7 @@ pub(crate) trait MessageType: Sized {
|
|||||||
self.set_expect()
|
self.set_expect()
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://tools.ietf.org/html/rfc7230#section-3.3.3
|
// https://datatracker.ietf.org/doc/html/rfc7230#section-3.3.3
|
||||||
if chunked {
|
if chunked {
|
||||||
// Chunked encoding
|
// Chunked encoding
|
||||||
Ok(PayloadLength::Payload(PayloadType::Payload(
|
Ok(PayloadLength::Payload(PayloadType::Payload(
|
||||||
|
@ -71,15 +71,16 @@ pub(crate) trait MessageType: Sized {
|
|||||||
| StatusCode::PROCESSING
|
| StatusCode::PROCESSING
|
||||||
| StatusCode::NO_CONTENT => {
|
| StatusCode::NO_CONTENT => {
|
||||||
// skip content-length and transfer-encoding headers
|
// skip content-length and transfer-encoding headers
|
||||||
// see https://tools.ietf.org/html/rfc7230#section-3.3.1
|
// see https://datatracker.ietf.org/doc/html/rfc7230#section-3.3.1
|
||||||
// and https://tools.ietf.org/html/rfc7230#section-3.3.2
|
// and https://datatracker.ietf.org/doc/html/rfc7230#section-3.3.2
|
||||||
skip_len = true;
|
skip_len = true;
|
||||||
length = BodySize::None
|
length = BodySize::None
|
||||||
}
|
}
|
||||||
|
|
||||||
StatusCode::NOT_MODIFIED => {
|
StatusCode::NOT_MODIFIED => {
|
||||||
// 304 responses should never have a body but should retain a manually set
|
// 304 responses should never have a body but should retain a manually set
|
||||||
// content-length header see https://tools.ietf.org/html/rfc7232#section-4.1
|
// content-length header
|
||||||
|
// see https://datatracker.ietf.org/doc/html/rfc7232#section-4.1
|
||||||
skip_len = false;
|
skip_len = false;
|
||||||
length = BodySize::None;
|
length = BodySize::None;
|
||||||
}
|
}
|
||||||
|
@ -304,7 +304,7 @@ fn prepare_response(
|
|||||||
for (key, value) in head.headers.iter() {
|
for (key, value) in head.headers.iter() {
|
||||||
match *key {
|
match *key {
|
||||||
// TODO: consider skipping other headers according to:
|
// TODO: consider skipping other headers according to:
|
||||||
// https://tools.ietf.org/html/rfc7540#section-8.1.2.2
|
// https://datatracker.ietf.org/doc/html/rfc7540#section-8.1.2.2
|
||||||
// omit HTTP/1.x only headers
|
// omit HTTP/1.x only headers
|
||||||
CONNECTION | TRANSFER_ENCODING => continue,
|
CONNECTION | TRANSFER_ENCODING => continue,
|
||||||
CONTENT_LENGTH if skip_len => continue,
|
CONTENT_LENGTH if skip_len => continue,
|
||||||
|
@ -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`.
|
||||||
@ -66,7 +66,7 @@ impl From<http::HeaderMap> for HeaderMap {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// This encode set is used for HTTP header values and is defined at
|
/// This encode set is used for HTTP header values and is defined at
|
||||||
/// <https://tools.ietf.org/html/rfc5987#section-3.2>.
|
/// <https://datatracker.ietf.org/doc/html/rfc5987#section-3.2>.
|
||||||
pub(crate) const HTTP_VALUE: &AsciiSet = &CONTROLS
|
pub(crate) const HTTP_VALUE: &AsciiSet = &CONTROLS
|
||||||
.add(b' ')
|
.add(b' ')
|
||||||
.add(b'"')
|
.add(b'"')
|
||||||
|
@ -1,17 +1,17 @@
|
|||||||
|
// Originally from hyper v0.11.27 src/header/parsing.rs
|
||||||
|
|
||||||
use std::{fmt, str::FromStr};
|
use std::{fmt, str::FromStr};
|
||||||
|
|
||||||
use language_tags::LanguageTag;
|
use language_tags::LanguageTag;
|
||||||
|
|
||||||
use crate::header::{Charset, HTTP_VALUE};
|
use crate::header::{Charset, HTTP_VALUE};
|
||||||
|
|
||||||
// From hyper v0.11.27 src/header/parsing.rs
|
|
||||||
|
|
||||||
/// The value part of an extended parameter consisting of three parts:
|
/// The value part of an extended parameter consisting of three parts:
|
||||||
/// - The REQUIRED character set name (`charset`).
|
/// - The REQUIRED character set name (`charset`).
|
||||||
/// - The OPTIONAL language information (`language_tag`).
|
/// - The OPTIONAL language information (`language_tag`).
|
||||||
/// - A character sequence representing the actual value (`value`), separated by single quotes.
|
/// - A character sequence representing the actual value (`value`), separated by single quotes.
|
||||||
///
|
///
|
||||||
/// It is defined in [RFC 5987](https://tools.ietf.org/html/rfc5987#section-3.2).
|
/// It is defined in [RFC 5987 §3.2](https://datatracker.ietf.org/doc/html/rfc5987#section-3.2).
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
pub struct ExtendedValue {
|
pub struct ExtendedValue {
|
||||||
/// The character set that is used to encode the `value` to a string.
|
/// The character set that is used to encode the `value` to a string.
|
||||||
|
@ -25,8 +25,8 @@ const MAX_FLOAT_QUALITY: f32 = 1.0;
|
|||||||
/// a value between 0 and 1000 e.g. `Quality(532)` matches the quality
|
/// a value between 0 and 1000 e.g. `Quality(532)` matches the quality
|
||||||
/// `q=0.532`.
|
/// `q=0.532`.
|
||||||
///
|
///
|
||||||
/// [RFC7231 Section 5.3.1](https://tools.ietf.org/html/rfc7231#section-5.3.1)
|
/// [RFC 7231 §5.3.1](https://datatracker.ietf.org/doc/html/rfc7231#section-5.3.1) gives more
|
||||||
/// gives more information on quality values in HTTP header fields.
|
/// information on quality values in HTTP header fields.
|
||||||
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
|
||||||
pub struct Quality(u16);
|
pub struct Quality(u16);
|
||||||
|
|
||||||
@ -78,8 +78,9 @@ impl TryFrom<f32> for Quality {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Represents an item with a quality value as defined in
|
/// Represents an item with a quality value as defined
|
||||||
/// [RFC 7231](https://tools.ietf.org/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)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub struct QualityItem<T> {
|
pub struct QualityItem<T> {
|
||||||
/// The wrapped contents of the field.
|
/// The wrapped contents of the field.
|
||||||
|
@ -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,20 +52,45 @@ 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(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Percent encode a sequence of bytes with a character set defined in
|
/// Percent encode a sequence of bytes with a character set defined in
|
||||||
/// <https://tools.ietf.org/html/rfc5987#section-3.2>
|
/// <https://datatracker.ietf.org/doc/html/rfc5987#section-3.2>
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn http_percent_encode(f: &mut fmt::Formatter<'_>, bytes: &[u8]) -> fmt::Result {
|
pub fn http_percent_encode(f: &mut fmt::Formatter<'_>, bytes: &[u8]) -> fmt::Result {
|
||||||
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(""),
|
||||||
|
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]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -222,7 +222,8 @@ impl<T: Into<String>> From<(CloseCode, T)> for CloseReason {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The WebSocket GUID as stated in the spec. See <https://tools.ietf.org/html/rfc6455#section-1.3>.
|
/// The WebSocket GUID as stated in the spec.
|
||||||
|
/// See <https://datatracker.ietf.org/doc/html/rfc6455#section-1.3>.
|
||||||
static WS_GUID: &[u8] = b"258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
|
static WS_GUID: &[u8] = b"258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
|
||||||
|
|
||||||
/// Hashes the `Sec-WebSocket-Key` header according to the WebSocket spec.
|
/// Hashes the `Sec-WebSocket-Key` header according to the WebSocket spec.
|
||||||
|
@ -10,7 +10,7 @@ use derive_more::{Display, Error, From};
|
|||||||
pub enum MultipartError {
|
pub enum MultipartError {
|
||||||
/// Content-Disposition header is not found or is not equal to "form-data".
|
/// Content-Disposition header is not found or is not equal to "form-data".
|
||||||
///
|
///
|
||||||
/// According to [RFC 7578](https://tools.ietf.org/html/rfc7578#section-4.2) a
|
/// According to [RFC 7578 §4.2](https://datatracker.ietf.org/doc/html/rfc7578#section-4.2) a
|
||||||
/// Content-Disposition header must always be present and equal to "form-data".
|
/// Content-Disposition header must always be present and equal to "form-data".
|
||||||
#[display(fmt = "No Content-Disposition `form-data` header")]
|
#[display(fmt = "No Content-Disposition `form-data` header")]
|
||||||
NoContentDisposition,
|
NoContentDisposition,
|
||||||
|
@ -337,8 +337,8 @@ impl InnerMultipart {
|
|||||||
return Poll::Pending;
|
return Poll::Pending;
|
||||||
};
|
};
|
||||||
|
|
||||||
// According to [RFC 7578](https://tools.ietf.org/html/rfc7578#section-4.2) a
|
// According to RFC 7578 §4.2, a Content-Disposition header must always be present and
|
||||||
// Content-Disposition header must always be present and set to "form-data".
|
// set to "form-data".
|
||||||
|
|
||||||
let content_disposition = headers
|
let content_disposition = headers
|
||||||
.get(&header::CONTENT_DISPOSITION)
|
.get(&header::CONTENT_DISPOSITION)
|
||||||
|
@ -66,8 +66,7 @@ where
|
|||||||
let mut framed = Framed::new(io, h1::ClientCodec::default());
|
let mut framed = Framed::new(io, h1::ClientCodec::default());
|
||||||
|
|
||||||
// Check EXPECT header and enable expect handle flag accordingly.
|
// Check EXPECT header and enable expect handle flag accordingly.
|
||||||
//
|
// See https://datatracker.ietf.org/doc/html/rfc7231#section-5.1.1
|
||||||
// RFC: https://tools.ietf.org/html/rfc7231#section-5.1.1
|
|
||||||
let is_expect = if head.as_ref().headers.contains_key(EXPECT) {
|
let is_expect = if head.as_ref().headers.contains_key(EXPECT) {
|
||||||
match body.size() {
|
match body.size() {
|
||||||
BodySize::None | BodySize::Sized(0) => {
|
BodySize::None | BodySize::Sized(0) => {
|
||||||
|
@ -90,7 +90,7 @@ where
|
|||||||
for (key, value) in headers {
|
for (key, value) in headers {
|
||||||
match *key {
|
match *key {
|
||||||
// TODO: consider skipping other headers according to:
|
// TODO: consider skipping other headers according to:
|
||||||
// https://tools.ietf.org/html/rfc7540#section-8.1.2.2
|
// https://datatracker.ietf.org/doc/html/rfc7540#section-8.1.2.2
|
||||||
// omit HTTP/1.x only headers
|
// omit HTTP/1.x only headers
|
||||||
CONNECTION | TRANSFER_ENCODING => continue,
|
CONNECTION | TRANSFER_ENCODING => continue,
|
||||||
CONTENT_LENGTH if skip_len => continue,
|
CONTENT_LENGTH if skip_len => continue,
|
||||||
|
@ -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
|
||||||
@ -155,7 +155,7 @@ impl Accept {
|
|||||||
/// Returns a sorted list of mime types from highest to lowest preference, accounting for
|
/// Returns a sorted list of mime types from highest to lowest preference, accounting for
|
||||||
/// [q-factor weighting] and specificity.
|
/// [q-factor weighting] and specificity.
|
||||||
///
|
///
|
||||||
/// [q-factor weighting]: https://tools.ietf.org/html/rfc7231#section-5.3.2
|
/// [q-factor weighting]: https://datatracker.ietf.org/doc/html/rfc7231#section-5.3.2
|
||||||
pub fn ranked(&self) -> Vec<Mime> {
|
pub fn ranked(&self) -> Vec<Mime> {
|
||||||
if self.is_empty() {
|
if self.is_empty() {
|
||||||
return vec![];
|
return vec![];
|
||||||
@ -207,12 +207,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://tools.ietf.org/html/rfc7231#section-5.3.2
|
/// [q-factor weighting]: https://datatracker.ietf.org/doc/html/rfc7231#section-5.3.2
|
||||||
pub fn preference(&self) -> Option<Mime> {
|
pub fn preference(&self) -> Mime {
|
||||||
// PERF: creating a sorted list is not necessary
|
use actix_http::header::q;
|
||||||
self.ranked().into_iter().next()
|
|
||||||
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -264,7 +281,7 @@ mod tests {
|
|||||||
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.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)),
|
||||||
@ -273,6 +290,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.preference(), Some(mime::IMAGE_PNG));
|
assert_eq!(test.preference(), mime::IMAGE_PNG);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,12 @@
|
|||||||
use language_tags::LanguageTag;
|
use language_tags::LanguageTag;
|
||||||
|
|
||||||
use super::{common_header, AnyOrSome, QualityItem};
|
use super::{common_header, Preference, QualityItem};
|
||||||
use crate::http::header;
|
use crate::http::header;
|
||||||
|
|
||||||
common_header! {
|
common_header! {
|
||||||
|
/// `Accept-Language` header, defined
|
||||||
|
/// 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 indicate the set of natural
|
/// The `Accept-Language` header field can be used by user agents to indicate the set of natural
|
||||||
/// languages that are preferred in the response.
|
/// languages that are preferred in the response.
|
||||||
///
|
///
|
||||||
@ -29,32 +32,36 @@ common_header! {
|
|||||||
/// # 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();
|
||||||
/// builder.insert_header(
|
/// builder.insert_header(
|
||||||
/// AcceptLanguage(vec![
|
/// AcceptLanguage(vec![
|
||||||
/// qitem(LanguageTag::parse("en-US").unwrap())
|
/// 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, header::ACCEPT_LANGUAGE) => (QualityItem<AnyOrSome<LanguageTag>>)+
|
(AcceptLanguage, header::ACCEPT_LANGUAGE) => (QualityItem<Preference<LanguageTag>>)*
|
||||||
|
|
||||||
test_parse_and_format {
|
test_parse_and_format {
|
||||||
|
common_header_test!(no_headers, vec![b""; 0], Some(AcceptLanguage(vec![])));
|
||||||
|
|
||||||
|
common_header_test!(empty_header, vec![b""; 1], Some(AcceptLanguage(vec![])));
|
||||||
|
|
||||||
common_header_test!(
|
common_header_test!(
|
||||||
example_from_rfc,
|
example_from_rfc,
|
||||||
vec![b"da, en-gb;q=0.8, en;q=0.7"]
|
vec![b"da, en-gb;q=0.8, en;q=0.7"]
|
||||||
@ -89,7 +96,7 @@ impl AcceptLanguage {
|
|||||||
/// for [q-factor weighting].
|
/// for [q-factor weighting].
|
||||||
///
|
///
|
||||||
/// [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 ranked(&self) -> Vec<AnyOrSome<LanguageTag>> {
|
pub fn ranked(&self) -> Vec<Preference<LanguageTag>> {
|
||||||
if self.0.is_empty() {
|
if self.0.is_empty() {
|
||||||
return vec![];
|
return vec![];
|
||||||
}
|
}
|
||||||
@ -110,12 +117,28 @@ impl AcceptLanguage {
|
|||||||
/// If no q-factors are provided, the first language is chosen. Note that items without
|
/// If no q-factors are provided, the first language is chosen. Note that items without
|
||||||
/// q-factors are given the maximum preference value.
|
/// q-factors are given the maximum preference value.
|
||||||
///
|
///
|
||||||
/// As per the spec, returns [`AnyOrSome::Any`] if contained list is empty.
|
/// 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
|
/// [q-factor weighting]: https://datatracker.ietf.org/doc/html/rfc7231#section-5.3.2
|
||||||
pub fn preference(&self) -> AnyOrSome<LanguageTag> {
|
pub fn preference(&self) -> Preference<LanguageTag> {
|
||||||
// PERF: creating a sorted list is not necessary
|
use actix_http::header::q;
|
||||||
self.ranked().into_iter().next().unwrap_or(AnyOrSome::Any)
|
|
||||||
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -178,7 +201,10 @@ mod tests {
|
|||||||
QualityItem::new("*".parse().unwrap(), q(500)),
|
QualityItem::new("*".parse().unwrap(), q(500)),
|
||||||
QualityItem::new("de".parse().unwrap(), q(700)),
|
QualityItem::new("de".parse().unwrap(), q(700)),
|
||||||
]);
|
]);
|
||||||
assert_eq!(test.preference(), AnyOrSome::Item("fr-CH".parse().unwrap()));
|
assert_eq!(
|
||||||
|
test.preference(),
|
||||||
|
Preference::Specific("fr-CH".parse().unwrap())
|
||||||
|
);
|
||||||
|
|
||||||
let test = AcceptLanguage(vec![
|
let test = AcceptLanguage(vec![
|
||||||
qitem("fr".parse().unwrap()),
|
qitem("fr".parse().unwrap()),
|
||||||
@ -187,9 +213,12 @@ mod tests {
|
|||||||
qitem("*".parse().unwrap()),
|
qitem("*".parse().unwrap()),
|
||||||
qitem("de".parse().unwrap()),
|
qitem("de".parse().unwrap()),
|
||||||
]);
|
]);
|
||||||
assert_eq!(test.preference(), AnyOrSome::Item("fr".parse().unwrap()));
|
assert_eq!(
|
||||||
|
test.preference(),
|
||||||
|
Preference::Specific("fr".parse().unwrap())
|
||||||
|
);
|
||||||
|
|
||||||
let test = AcceptLanguage(vec![]);
|
let test = AcceptLanguage(vec![]);
|
||||||
assert_eq!(test.preference(), AnyOrSome::Any);
|
assert_eq!(test.preference(), Preference::Any);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -204,9 +204,9 @@ impl DispositionParam {
|
|||||||
|
|
||||||
/// A *Content-Disposition* header. It is compatible to be used either as
|
/// A *Content-Disposition* header. It is compatible to be used either as
|
||||||
/// [a response header for the main body](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition#as_a_response_header_for_the_main_body)
|
/// [a response header for the main body](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition#as_a_response_header_for_the_main_body)
|
||||||
/// as (re)defined in [RFC 6266](https://tools.ietf.org/html/rfc6266), or as
|
/// as (re)defined in [RFC 6266](https://datatracker.ietf.org/doc/html/rfc6266), or as
|
||||||
/// [a header for a multipart body](https://mdn.io/Content-Disposition#As_a_header_for_a_multipart_body)
|
/// [a header for a multipart body](https://mdn.io/Content-Disposition#As_a_header_for_a_multipart_body)
|
||||||
/// as (re)defined in [RFC 7587](https://tools.ietf.org/html/rfc7578).
|
/// as (re)defined in [RFC 7587](https://datatracker.ietf.org/doc/html/rfc7578).
|
||||||
///
|
///
|
||||||
/// In a regular HTTP response, the *Content-Disposition* response header is a header indicating if
|
/// In a regular HTTP response, the *Content-Disposition* response header is a header indicating if
|
||||||
/// the content is expected to be displayed *inline* in the browser, that is, as a Web page or as
|
/// the content is expected to be displayed *inline* in the browser, that is, as a Web page or as
|
||||||
@ -299,8 +299,9 @@ impl DispositionParam {
|
|||||||
/// # Security Note
|
/// # Security Note
|
||||||
/// If "filename" parameter is supplied, do not use the file name blindly, check and possibly
|
/// If "filename" parameter is supplied, do not use the file name blindly, check and possibly
|
||||||
/// change to match local file system conventions if applicable, and do not use directory path
|
/// change to match local file system conventions if applicable, and do not use directory path
|
||||||
/// information that may be present. See [RFC 2183](https://tools.ietf.org/html/rfc2183#section-2.3).
|
/// information that may be present.
|
||||||
// TODO: private fields and use smallvec
|
/// See [RFC 2183 §2.3](https://datatracker.ietf.org/doc/html/rfc2183#section-2.3).
|
||||||
|
// TODO: think about using private fields and smallvec
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
pub struct ContentDisposition {
|
pub struct ContentDisposition {
|
||||||
/// The disposition type
|
/// The disposition type
|
||||||
|
@ -71,7 +71,8 @@ crate::http::header::common_header! {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Content-Range, described in [RFC 7233](https://tools.ietf.org/html/rfc7233#section-4.2)
|
/// Content-Range header, defined
|
||||||
|
/// in [RFC 7233 §4.2](https://datatracker.ietf.org/doc/html/rfc7233#section-4.2)
|
||||||
///
|
///
|
||||||
/// # ABNF
|
/// # ABNF
|
||||||
/// ```plain
|
/// ```plain
|
||||||
|
@ -1,13 +1,14 @@
|
|||||||
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 {
|
||||||
|
#![allow(unused_imports)]
|
||||||
|
|
||||||
use std::str;
|
use std::str;
|
||||||
use actix_http::http::Method;
|
use actix_http::http::Method;
|
||||||
use mime::*;
|
use mime::*;
|
||||||
use $crate::http::header::*;
|
use $crate::http::header::*;
|
||||||
use super::$id as HeaderField;
|
use super::{$id as HeaderField, *};
|
||||||
$($tf)*
|
$($tf)*
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -50,7 +51,7 @@ macro_rules! common_header_test {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
($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;
|
||||||
@ -62,28 +63,31 @@ macro_rules! common_header_test {
|
|||||||
}
|
}
|
||||||
let req = req.finish();
|
let req = req.finish();
|
||||||
let val = HeaderField::parse(&req);
|
let val = HeaderField::parse(&req);
|
||||||
let typed: Option<HeaderField> = $typed;
|
let exp: Option<HeaderField> = $typed;
|
||||||
|
|
||||||
// Test parsing
|
// test parsing
|
||||||
assert_eq!(val.ok(), typed);
|
assert_eq!(val.ok(), exp);
|
||||||
|
|
||||||
// Test formatting
|
// test formatting
|
||||||
if typed.is_some() {
|
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() {
|
||||||
|
joined.push_str(s);
|
||||||
for s in iter {
|
for s in iter {
|
||||||
joined.push_str(", ");
|
joined.push_str(", ");
|
||||||
joined.push_str(s);
|
joined.push_str(s);
|
||||||
}
|
}
|
||||||
assert_eq!(format!("{}", typed.unwrap()), joined);
|
}
|
||||||
|
assert_eq!(format!("{}", exp), joined);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! common_header {
|
macro_rules! common_header {
|
||||||
|
// TODO: these docs are wrong, there's no $n or $nn
|
||||||
// $attrs:meta: Attributes associated with the header item (usually docs)
|
// $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
|
||||||
@ -102,7 +106,7 @@ macro_rules! common_header {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn parse<T: $crate::HttpMessage>(msg: &T) -> Result<Self, $crate::error::ParseError> {
|
fn parse<M: $crate::HttpMessage>(msg: &M) -> Result<Self, $crate::error::ParseError> {
|
||||||
let headers = msg.headers().get_all(Self::name());
|
let headers = msg.headers().get_all(Self::name());
|
||||||
$crate::http::header::from_comma_delimited(headers).map($id)
|
$crate::http::header::from_comma_delimited(headers).map($id)
|
||||||
}
|
}
|
||||||
@ -133,16 +137,13 @@ macro_rules! common_header {
|
|||||||
#[derive(Debug, Clone, PartialEq, Eq, ::derive_more::Deref, ::derive_more::DerefMut)]
|
#[derive(Debug, Clone, PartialEq, Eq, ::derive_more::Deref, ::derive_more::DerefMut)]
|
||||||
pub struct $id(pub Vec<$item>);
|
pub struct $id(pub 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
|
|
||||||
{
|
|
||||||
$crate::http::header::from_comma_delimited(
|
$crate::http::header::from_comma_delimited(
|
||||||
msg.headers().get_all(Self::name())).map($id)
|
msg.headers().get_all(Self::name())).map($id)
|
||||||
}
|
}
|
||||||
@ -180,7 +181,7 @@ macro_rules! common_header {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn parse<T: $crate::HttpMessage>(msg: &T) -> Result<Self, $crate::error::ParseError> {
|
fn parse<M: $crate::HttpMessage>(msg: &M) -> Result<Self, $crate::error::ParseError> {
|
||||||
let header = msg.headers().get(Self::name());
|
let header = msg.headers().get(Self::name());
|
||||||
$crate::http::header::from_one_raw_str(header).map($id)
|
$crate::http::header::from_one_raw_str(header).map($id)
|
||||||
}
|
}
|
||||||
@ -220,7 +221,7 @@ macro_rules! common_header {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn parse<T: $crate::HttpMessage>(msg: &T) -> Result<Self, $crate::error::ParseError> {
|
fn parse<M: $crate::HttpMessage>(msg: &M) -> Result<Self, $crate::error::ParseError> {
|
||||||
let is_any = msg
|
let is_any = msg
|
||||||
.headers()
|
.headers()
|
||||||
.get(Self::name())
|
.get(Self::name())
|
||||||
|
@ -22,7 +22,6 @@ mod accept_charset;
|
|||||||
mod accept;
|
mod accept;
|
||||||
mod accept_language;
|
mod accept_language;
|
||||||
mod allow;
|
mod allow;
|
||||||
mod any_or_some;
|
|
||||||
mod cache_control;
|
mod cache_control;
|
||||||
mod content_disposition;
|
mod content_disposition;
|
||||||
mod content_language;
|
mod content_language;
|
||||||
@ -40,6 +39,7 @@ mod if_range;
|
|||||||
mod if_unmodified_since;
|
mod if_unmodified_since;
|
||||||
mod last_modified;
|
mod last_modified;
|
||||||
mod macros;
|
mod macros;
|
||||||
|
mod preference;
|
||||||
// mod range;
|
// mod range;
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
@ -68,6 +68,7 @@ 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};
|
||||||
|
|
||||||
/// Format writer ([`fmt::Write`]) for a [`BytesMut`].
|
/// Format writer ([`fmt::Write`]) for a [`BytesMut`].
|
||||||
|
70
src/http/header/preference.rs
Normal file
70
src/http/header/preference.rs
Normal file
@ -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),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -96,7 +96,7 @@ impl ByteRangeSpec {
|
|||||||
/// simply ignore the range header and serve the full entity using a `200
|
/// simply ignore the range header and serve the full entity using a `200
|
||||||
/// OK` status code.
|
/// OK` status code.
|
||||||
///
|
///
|
||||||
/// This function closely follows [RFC 7233][1] section 2.1.
|
/// This function closely follows [RFC 7233 §2.1].
|
||||||
/// As such, it considers ranges to be satisfiable if they meet the
|
/// As such, it considers ranges to be satisfiable if they meet the
|
||||||
/// following conditions:
|
/// following conditions:
|
||||||
///
|
///
|
||||||
@ -115,7 +115,7 @@ impl ByteRangeSpec {
|
|||||||
/// value of last-byte-pos with a value that is one less than the current
|
/// value of last-byte-pos with a value that is one less than the current
|
||||||
/// length of the selected representation).
|
/// length of the selected representation).
|
||||||
///
|
///
|
||||||
/// [1]: https://tools.ietf.org/html/rfc7233
|
/// [RFC 7233 §2.1]: https://datatracker.ietf.org/doc/html/rfc7233
|
||||||
pub fn to_satisfiable_range(&self, full_length: u64) -> Option<(u64, u64)> {
|
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 the full length is zero, there is no satisfiable end-inclusive range.
|
||||||
if full_length == 0 {
|
if full_length == 0 {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user