1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-07-03 09:36:36 +02:00

Compare commits

...

13 Commits

47 changed files with 805 additions and 439 deletions

View File

@ -1,6 +1,17 @@
# Changes
## Unreleased - 2021-xx-xx
### Added
* Methods on `AcceptLanguage`: `ranked` and `preference`. [#2480]
### Changed
* Rename `Accept::{mime_precedence => ranked}`. [#2480]
* Rename `Accept::{mime_preference => preference}`. [#2480]
### Fixed
* Accept wildcard `*` items in `AcceptLanguage`. [#2480]
[#2480]: https://github.com/actix/actix-web/pull/2480
## 4.0.0-beta.13 - 2021-11-30

View File

@ -174,7 +174,7 @@ pub(crate) trait MessageType: Sized {
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 {
// Chunked encoding
Ok(PayloadLength::Payload(PayloadType::Payload(

View File

@ -71,15 +71,16 @@ pub(crate) trait MessageType: Sized {
| StatusCode::PROCESSING
| StatusCode::NO_CONTENT => {
// skip content-length and transfer-encoding headers
// see https://tools.ietf.org/html/rfc7230#section-3.3.1
// and https://tools.ietf.org/html/rfc7230#section-3.3.2
// see https://datatracker.ietf.org/doc/html/rfc7230#section-3.3.1
// and https://datatracker.ietf.org/doc/html/rfc7230#section-3.3.2
skip_len = true;
length = BodySize::None
}
StatusCode::NOT_MODIFIED => {
// 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;
length = BodySize::None;
}

View File

@ -304,7 +304,7 @@ fn prepare_response(
for (key, value) in head.headers.iter() {
match *key {
// 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
CONNECTION | TRANSFER_ENCODING => continue,
CONTENT_LENGTH if skip_len => continue,

View File

@ -55,7 +55,7 @@ pub trait Header: IntoHeaderValue {
fn name() -> HeaderName;
/// 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`.
@ -66,7 +66,7 @@ impl From<http::HeaderMap> for HeaderMap {
}
/// 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
.add(b' ')
.add(b'"')

View File

@ -1,14 +1,13 @@
use std::fmt::{self, Display};
use std::str::FromStr;
use std::{fmt, str};
use self::Charset::*;
/// A Mime charset.
/// A MIME character set.
///
/// The string representation is normalized to upper case.
///
/// 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)]
pub enum Charset {
/// US ASCII
@ -95,13 +94,13 @@ impl Charset {
}
}
impl Display for Charset {
impl fmt::Display for Charset {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.label())
}
}
impl FromStr for Charset {
impl str::FromStr for Charset {
type Err = crate::Error;
fn from_str(s: &str) -> Result<Charset, crate::Error> {

View File

@ -1,17 +1,17 @@
// Originally from hyper v0.11.27 src/header/parsing.rs
use std::{fmt, str::FromStr};
use language_tags::LanguageTag;
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 REQUIRED character set name (`charset`).
/// - The OPTIONAL language information (`language_tag`).
/// - 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)]
pub struct ExtendedValue {
/// The character set that is used to encode the `value` to a string.
@ -24,17 +24,17 @@ pub struct ExtendedValue {
pub value: Vec<u8>,
}
/// Parses extended header parameter values (`ext-value`), as defined in
/// [RFC 5987](https://tools.ietf.org/html/rfc5987#section-3.2).
/// Parses extended header parameter values (`ext-value`), as defined
/// in [RFC 5987 §3.2](https://datatracker.ietf.org/doc/html/rfc5987#section-3.2).
///
/// Extended values are denoted by parameter names that end with `*`.
///
/// ## ABNF
///
/// ```text
/// ```plain
/// ext-value = charset "'" [ language ] "'" value-chars
/// ; like RFC 2231's <extended-initial-value>
/// ; (see [RFC2231], Section 7)
/// ; (see [RFC 2231 §7])
///
/// charset = "UTF-8" / "ISO-8859-1" / mime-charset
///
@ -43,22 +43,26 @@ pub struct ExtendedValue {
/// / "!" / "#" / "$" / "%" / "&"
/// / "+" / "-" / "^" / "_" / "`"
/// / "{" / "}" / "~"
/// ; as <mime-charset> in Section 2.3 of [RFC2978]
/// ; as <mime-charset> in [RFC 2978 §2.3]
/// ; except that the single quote is not included
/// ; SHOULD be registered in the IANA charset registry
///
/// language = <Language-Tag, defined in [RFC5646], Section 2.1>
/// language = <Language-Tag, defined in [RFC 5646 §2.1]>
///
/// value-chars = *( pct-encoded / attr-char )
///
/// pct-encoded = "%" HEXDIG HEXDIG
/// ; see [RFC3986], Section 2.1
/// ; see [RFC 3986 §2.1]
///
/// attr-char = ALPHA / DIGIT
/// / "!" / "#" / "$" / "&" / "+" / "-" / "."
/// / "^" / "_" / "`" / "|" / "~"
/// ; token except ( "*" / "'" / "%" )
/// ```
///
/// [RFC 2231 §7]: https://datatracker.ietf.org/doc/html/rfc2231#section-7
/// [RFC 2978 §2.3]: https://datatracker.ietf.org/doc/html/rfc2978#section-2.3
/// [RFC 3986 §2.1]: https://datatracker.ietf.org/doc/html/rfc5646#section-2.1
pub fn parse_extended_value(
val: &str,
) -> Result<ExtendedValue, crate::error::ParseError> {

View File

@ -8,7 +8,7 @@ use crate::{
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)]
pub struct HttpDate(SystemTime);

View File

@ -1,8 +1,7 @@
use std::{
cmp,
convert::{TryFrom, TryInto},
fmt,
str::{self, FromStr},
fmt, str,
};
use derive_more::{Display, Error};
@ -26,9 +25,9 @@ const MAX_FLOAT_QUALITY: f32 = 1.0;
/// a value between 0 and 1000 e.g. `Quality(532)` matches the quality
/// `q=0.532`.
///
/// [RFC7231 Section 5.3.1](https://tools.ietf.org/html/rfc7231#section-5.3.1)
/// gives more information on quality values in HTTP header fields.
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
/// [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.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Quality(u16);
impl Quality {
@ -79,20 +78,21 @@ impl TryFrom<f32> for Quality {
}
}
/// Represents an item with a quality value as defined in
/// [RFC7231](https://tools.ietf.org/html/rfc7231#section-5.3.1).
#[derive(Clone, PartialEq, Debug)]
/// 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).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct QualityItem<T> {
/// The actual contents of the field.
/// The wrapped contents of the field.
pub item: T,
/// The quality (client or server preference) for the value.
pub quality: Quality,
}
impl<T> QualityItem<T> {
/// Creates a new `QualityItem` from an item and a quality.
/// The item can be of any type.
/// The quality should be a value in the range [0, 1].
/// 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].
pub fn new(item: T, quality: Quality) -> QualityItem<T> {
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;
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 quality = 1f32;
// TODO: MSRV(1.52): use rsplit_once
let parts: Vec<_> = qitem_str.rsplitn(2, ';').map(str::trim).collect();
if parts.len() == 2 {
@ -224,7 +225,7 @@ mod tests {
}
}
impl FromStr for Encoding {
impl str::FromStr for Encoding {
type Err = crate::error::ParseError;
fn from_str(s: &str) -> Result<Encoding, crate::error::ParseError> {
use Encoding::*;

View File

@ -12,9 +12,12 @@ where
I: Iterator<Item = &'a HeaderValue> + 'a,
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 {
let s = h.to_str().map_err(|_| ParseError::Header)?;
result.extend(
s.split(',')
.filter_map(|x| match x.trim() {
@ -24,6 +27,7 @@ where
.filter_map(|x| x.trim().parse().ok()),
)
}
Ok(result)
}
@ -32,10 +36,12 @@ where
pub fn from_one_raw_str<T: FromStr>(val: Option<&HeaderValue>) -> Result<T, ParseError> {
if let Some(line) = val {
let line = line.to_str().map_err(|_| ParseError::Header)?;
if !line.is_empty() {
return T::from_str(line).or(Err(ParseError::Header));
}
}
Err(ParseError::Header)
}
@ -46,20 +52,45 @@ where
T: fmt::Display,
{
let mut iter = parts.iter();
if let Some(part) = iter.next() {
fmt::Display::fmt(part, f)?;
}
for part in iter {
f.write_str(", ")?;
fmt::Display::fmt(part, f)?;
}
Ok(())
}
/// 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]
pub fn http_percent_encode(f: &mut fmt::Formatter<'_>, bytes: &[u8]) -> fmt::Result {
let encoded = percent_encoding::percent_encode(bytes, HTTP_VALUE);
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]);
}
}

View File

@ -3,7 +3,9 @@ use std::{
fmt,
};
/// Operation codes as part of RFC6455.
/// Operation codes defined in [RFC 6455 §11.8].
///
/// [RFC 6455]: https://datatracker.ietf.org/doc/html/rfc6455#section-11.8
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
pub enum OpCode {
/// Indicates a continuation frame of a fragmented message.
@ -105,7 +107,7 @@ pub enum CloseCode {
Abnormal,
/// Indicates that an endpoint is terminating the connection because it has received data within
/// a message that was not consistent with the type of the message (e.g., non-UTF-8 \[RFC3629\]
/// a message that was not consistent with the type of the message (e.g., non-UTF-8 \[RFC 3629\]
/// data within a text message).
Invalid,
@ -220,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";
/// Hashes the `Sec-WebSocket-Key` header according to the WebSocket spec.

View File

@ -3,6 +3,12 @@
## Unreleased - 2021-xx-xx
## 0.4.0-beta.9 - 2021-12-01
* Polling `Field` after dropping `Multipart` now fails immediately instead of hanging forever. [#2463]
[#2463]: https://github.com/actix/actix-web/pull/2463
## 0.4.0-beta.8 - 2021-11-22
* Ensure a correct Content-Disposition header is included in every part of a multipart message. [#2451]
* Added `MultipartError::NoContentDisposition` variant. [#2451]
@ -10,10 +16,8 @@
* Added `Field::name` method for getting the field name. [#2451]
* `MultipartError` now marks variants with inner errors as the source. [#2451]
* `MultipartError` is now marked as non-exhaustive. [#2451]
* Polling `Field` after dropping `Multipart` now fails immediately instead of hanging forever. [#2463]
[#2451]: https://github.com/actix/actix-web/pull/2451
[#2463]: https://github.com/actix/actix-web/pull/2463
## 0.4.0-beta.7 - 2021-10-20

View File

@ -1,6 +1,6 @@
[package]
name = "actix-multipart"
version = "0.4.0-beta.8"
version = "0.4.0-beta.9"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Multipart form support for Actix Web"
keywords = ["http", "web", "framework", "async", "futures"]

View File

@ -3,11 +3,11 @@
> Multipart form support for Actix Web.
[![crates.io](https://img.shields.io/crates/v/actix-multipart?label=latest)](https://crates.io/crates/actix-multipart)
[![Documentation](https://docs.rs/actix-multipart/badge.svg?version=0.4.0-beta.8)](https://docs.rs/actix-multipart/0.4.0-beta.8)
[![Documentation](https://docs.rs/actix-multipart/badge.svg?version=0.4.0-beta.9)](https://docs.rs/actix-multipart/0.4.0-beta.9)
[![Version](https://img.shields.io/badge/rustc-1.52+-ab6000.svg)](https://blog.rust-lang.org/2021/05/06/Rust-1.52.0.html)
![MIT or Apache 2.0 licensed](https://img.shields.io/crates/l/actix-multipart.svg)
<br />
[![dependency status](https://deps.rs/crate/actix-multipart/0.4.0-beta.8/status.svg)](https://deps.rs/crate/actix-multipart/0.4.0-beta.8)
[![dependency status](https://deps.rs/crate/actix-multipart/0.4.0-beta.9/status.svg)](https://deps.rs/crate/actix-multipart/0.4.0-beta.9)
[![Download](https://img.shields.io/crates/d/actix-multipart.svg)](https://crates.io/crates/actix-multipart)
[![Chat on Discord](https://img.shields.io/discord/771444961383153695?label=chat&logo=discord)](https://discord.gg/NWpN5mmg3x)

View File

@ -10,7 +10,7 @@ use derive_more::{Display, Error, From};
pub enum MultipartError {
/// 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".
#[display(fmt = "No Content-Disposition `form-data` header")]
NoContentDisposition,

View File

@ -337,8 +337,8 @@ impl InnerMultipart {
return Poll::Pending;
};
// According to [RFC 7578](https://tools.ietf.org/html/rfc7578#section-4.2) a
// Content-Disposition header must always be present and set to "form-data".
// According to RFC 7578 §4.2, a Content-Disposition header must always be present and
// set to "form-data".
let content_disposition = headers
.get(&header::CONTENT_DISPOSITION)

View File

@ -66,7 +66,7 @@ mod route;
/// Creates resource handler, allowing multiple HTTP method guards.
///
/// # Syntax
/// ```text
/// ```plain
/// #[route("path", method="HTTP_METHOD"[, attributes])]
/// ```
///
@ -112,7 +112,7 @@ concat!("
Creates route handler with `actix_web::guard::", stringify!($variant), "`.
# Syntax
```text
```plain
#[", stringify!($method), r#"("path"[, attributes])]
```

View File

@ -66,8 +66,7 @@ where
let mut framed = Framed::new(io, h1::ClientCodec::default());
// Check EXPECT header and enable expect handle flag accordingly.
//
// RFC: https://tools.ietf.org/html/rfc7231#section-5.1.1
// See https://datatracker.ietf.org/doc/html/rfc7231#section-5.1.1
let is_expect = if head.as_ref().headers.contains_key(EXPECT) {
match body.size() {
BodySize::None | BodySize::Sized(0) => {

View File

@ -90,7 +90,7 @@ where
for (key, value) in headers {
match *key {
// 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
CONNECTION | TRANSFER_ENCODING => continue,
CONTENT_LENGTH if skip_len => continue,

View File

@ -312,9 +312,8 @@ impl WebsocketsRequest {
);
}
// Generate a random key for the `Sec-WebSocket-Key` header.
// a base64-encoded (see Section 4 of [RFC4648]) value that,
// when decoded, is 16 bytes in length (RFC 6455)
// Generate a random key for the `Sec-WebSocket-Key` header which is a base64-encoded
// (see RFC 4648 §4) value that, when decoded, is 16 bytes in length (RFC 6455 §1.3).
let sec_key: [u8; 16] = rand::random();
let key = base64::encode(&sec_key);

View File

@ -6,7 +6,8 @@ use super::{qitem, QualityItem};
use crate::http::header;
crate::http::header::common_header! {
/// `Accept` header, defined in [RFC7231](http://tools.ietf.org/html/rfc7231#section-5.3.2)
/// `Accept` header, defined
/// in [RFC 7231 §5.3.2](https://datatracker.ietf.org/doc/html/rfc7231#section-5.3.2)
///
/// The `Accept` header field can be used by user agents to specify
/// response media types that are acceptable. Accept header fields can
@ -15,8 +16,7 @@ crate::http::header::common_header! {
/// in-line image
///
/// # ABNF
///
/// ```text
/// ```plain
/// Accept = #( media-range [ accept-params ] )
///
/// media-range = ( "*/*"
@ -27,7 +27,7 @@ crate::http::header::common_header! {
/// accept-ext = OWS ";" OWS token [ "=" ( token / quoted-string ) ]
/// ```
///
/// # Example values
/// # Example Values
/// * `audio/*; q=0.2, audio/basic`
/// * `text/plain; q=0.5, text/html, text/x-dvi; q=0.8, text/x-c`
///
@ -77,9 +77,9 @@ crate::http::header::common_header! {
/// ])
/// );
/// ```
(Accept, header::ACCEPT) => (QualityItem<Mime>)+
(Accept, header::ACCEPT) => (QualityItem<Mime>)*
test_accept {
test_parse_and_format {
// Tests from the RFC
crate::http::header::common_header_test!(
test1,
@ -88,6 +88,7 @@ crate::http::header::common_header! {
QualityItem::new("audio/*".parse().unwrap(), q(200)),
qitem("audio/basic".parse().unwrap()),
])));
crate::http::header::common_header_test!(
test2,
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)),
qitem("text/x-c".parse().unwrap()),
])));
// Custom tests
crate::http::header::common_header_test!(
test3,
@ -153,8 +155,12 @@ impl Accept {
/// Returns a sorted list of mime types from highest to lowest preference, accounting for
/// [q-factor weighting] and specificity.
///
/// [q-factor weighting]: https://tools.ietf.org/html/rfc7231#section-5.3.2
pub fn mime_precedence(&self) -> Vec<Mime> {
/// [q-factor weighting]: https://datatracker.ietf.org/doc/html/rfc7231#section-5.3.2
pub fn ranked(&self) -> Vec<Mime> {
if self.is_empty() {
return vec![];
}
let mut types = self.0.clone();
// use stable sort so items with equal q-factor and specificity retain listed order
@ -201,12 +207,29 @@ impl Accept {
/// If no q-factors are provided, the first mime type is chosen. Note that items without
/// 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
pub fn mime_preference(&self) -> Option<Mime> {
let types = self.mime_precedence();
types.first().cloned()
/// [q-factor weighting]: https://datatracker.ietf.org/doc/html/rfc7231#section-5.3.2
pub fn preference(&self) -> Mime {
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(mime::STAR_STAR)
}
}
@ -216,12 +239,12 @@ mod tests {
use crate::http::header::q;
#[test]
fn test_mime_precedence() {
fn ranking_precedence() {
let test = Accept(vec![]);
assert!(test.mime_precedence().is_empty());
assert!(test.ranked().is_empty());
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![
qitem(mime::TEXT_HTML),
@ -230,7 +253,7 @@ mod tests {
QualityItem::new(mime::STAR_STAR, q(0.8)),
]);
assert_eq!(
test.mime_precedence(),
test.ranked(),
vec![
mime::TEXT_HTML,
"application/xhtml+xml".parse().unwrap(),
@ -245,20 +268,20 @@ mod tests {
qitem(mime::IMAGE_PNG),
]);
assert_eq!(
test.mime_precedence(),
test.ranked(),
vec![mime::IMAGE_PNG, mime::IMAGE_STAR, mime::STAR_STAR]
);
}
#[test]
fn test_mime_preference() {
fn preference_selection() {
let test = Accept(vec![
qitem(mime::TEXT_HTML),
"application/xhtml+xml".parse().unwrap(),
QualityItem::new("application/xml".parse().unwrap(), q(0.9)),
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![
QualityItem::new("video/*".parse().unwrap(), q(0.8)),
@ -267,6 +290,6 @@ mod tests {
qitem(mime::IMAGE_SVG),
QualityItem::new(mime::IMAGE_STAR, q(0.8)),
]);
assert_eq!(test.mime_preference(), Some(mime::IMAGE_PNG));
assert_eq!(test.preference(), mime::IMAGE_PNG);
}
}

View File

@ -2,7 +2,7 @@ use super::{Charset, QualityItem, ACCEPT_CHARSET};
crate::http::header::common_header! {
/// `Accept-Charset` header, defined in
/// [RFC7231](http://tools.ietf.org/html/rfc7231#section-5.3.3)
/// [RFC 7231 §5.3.3](https://datatracker.ietf.org/doc/html/rfc7231#section-5.3.3)
///
/// The `Accept-Charset` header field can be sent by a user agent to
/// indicate what charsets are acceptable in textual response content.
@ -12,12 +12,11 @@ crate::http::header::common_header! {
/// those charsets.
///
/// # ABNF
///
/// ```text
/// ```plain
/// Accept-Charset = 1#( ( charset / "*" ) [ weight ] )
/// ```
///
/// # Example values
/// # Example Values
/// * `iso-8859-5, unicode-1-1;q=0.8`
///
/// # Examples
@ -55,7 +54,7 @@ crate::http::header::common_header! {
/// ```
(AcceptCharset, ACCEPT_CHARSET) => (QualityItem<Charset>)+
test_accept_charset {
test_parse_and_format {
// Test case from RFC
crate::http::header::common_header_test!(test1, vec![b"iso-8859-5, unicode-1-1;q=0.8"]);
}

View File

@ -1,8 +1,10 @@
// TODO: reinstate module
use header::{Encoding, QualityItem};
header! {
/// `Accept-Encoding` header, defined in
/// [RFC7231](http://tools.ietf.org/html/rfc7231#section-5.3.4)
/// `Accept-Encoding` header, defined
/// in [RFC 7231](https://datatracker.ietf.org/doc/html/rfc7231#section-5.3.4)
///
/// The `Accept-Encoding` header field can be used by user agents to
/// indicate what response content-codings are
@ -11,13 +13,12 @@ header! {
/// preferred.
///
/// # ABNF
///
/// ```text
/// ```plain
/// Accept-Encoding = #( codings [ weight ] )
/// codings = content-coding / "identity" / "*"
/// ```
///
/// # Example values
/// # Example Values
/// * `compress, gzip`
/// * ``
/// * `*`
@ -60,15 +61,17 @@ header! {
/// ])
/// );
/// ```
(AcceptEncoding, "Accept-Encoding") => (QualityItem<Encoding>)*
(AcceptEncoding, header::ACCEPT_ENCODING) => (QualityItem<Encoding>)*
test_accept_encoding {
test_parse_and_format {
// From the RFC
crate::http::header::common_header_test!(test1, vec![b"compress, gzip"]);
crate::http::header::common_header_test!(test2, vec![b""], Some(AcceptEncoding(vec![])));
crate::http::header::common_header_test!(test3, vec![b"*"]);
// Note: Removed quality 1 from gzip
crate::http::header::common_header_test!(test4, vec![b"compress;q=0.5, gzip"]);
// Note: Removed quality 1 from gzip
crate::http::header::common_header_test!(test5, vec![b"gzip, identity; q=0.5, *;q=0"]);
}

View File

@ -1,66 +1,224 @@
use language_tags::LanguageTag;
use super::{QualityItem, ACCEPT_LANGUAGE};
use super::{common_header, Preference, QualityItem};
use crate::http::header;
crate::http::header::common_header! {
/// `Accept-Language` header, defined in
/// [RFC7231](http://tools.ietf.org/html/rfc7231#section-5.3.5)
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 languages that are preferred in the
/// response.
/// The `Accept-Language` header field can be used by user agents to indicate the set of natural
/// languages that are preferred in the 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
///
/// ```text
/// ```plain
/// 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
/// * `da, en-gb;q=0.8, en;q=0.7`
/// * `en-us;q=1.0, en;q=0.5, fr`
/// # Example Values
/// - `da, en-gb;q=0.8, en;q=0.7`
/// - `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
///
/// ```
/// 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 langtag = LanguageTag::parse("en-US").unwrap();
/// builder.insert_header(
/// AcceptLanguage(vec![
/// qitem(langtag),
/// qitem("en-US".parse().unwrap())
/// ])
/// );
/// ```
///
/// ```
/// 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();
/// builder.insert_header(
/// AcceptLanguage(vec![
/// qitem(LanguageTag::parse("da").unwrap()),
/// QualityItem::new(LanguageTag::parse("en-GB").unwrap(), q(800)),
/// QualityItem::new(LanguageTag::parse("en").unwrap(), q(700)),
/// qitem("da".parse().unwrap()),
/// QualityItem::new("en-GB".parse().unwrap(), q(800)),
/// QualityItem::new("en".parse().unwrap(), q(700)),
/// ])
/// );
/// ```
(AcceptLanguage, ACCEPT_LANGUAGE) => (QualityItem<LanguageTag>)+
(AcceptLanguage, header::ACCEPT_LANGUAGE) => (QualityItem<Preference<LanguageTag>>)*
test_accept_language {
// From the RFC
crate::http::header::common_header_test!(test1, vec![b"da, en-gb;q=0.8, en;q=0.7"]);
// Own test
crate::http::header::common_header_test!(
test2, vec![b"en-US, en; q=0.5, fr"],
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!(
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![
qitem("en-US".parse().unwrap()),
QualityItem::new("en".parse().unwrap(), q(500)),
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);
}
}

View File

@ -1,27 +1,27 @@
use crate::http::header;
use actix_http::http::Method;
use crate::http::header;
crate::http::header::common_header! {
/// `Allow` header, defined in [RFC7231](http://tools.ietf.org/html/rfc7231#section-7.4.1)
/// `Allow` header, defined
/// in [RFC 7231 §7.4.1](https://datatracker.ietf.org/doc/html/rfc7231#section-7.4.1)
///
/// The `Allow` header field lists the set of methods advertised as
/// supported by the target resource. The purpose of this field is
/// supported by the target resource. The purpose of this field is
/// strictly to inform the recipient of valid request methods associated
/// with the resource.
///
/// # ABNF
///
/// ```text
/// ```plain
/// Allow = #method
/// ```
///
/// # Example values
/// # Example Values
/// * `GET, HEAD, PUT`
/// * `OPTIONS, GET, PUT, POST, DELETE, HEAD, TRACE, CONNECT, PATCH, fOObAr`
/// * ``
///
/// # Examples
///
/// ```
/// use actix_web::HttpResponse;
/// use actix_web::http::{header::Allow, Method};
@ -47,7 +47,7 @@ crate::http::header::common_header! {
/// ```
(Allow, header::ALLOW) => (Method)*
test_allow {
test_parse_and_format {
// From the RFC
crate::http::header::common_header_test!(
test1,

View 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 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),
}
}
}

View File

@ -1,11 +1,14 @@
use std::fmt::{self, Write};
use std::str::FromStr;
use derive_more::{Deref, DerefMut};
use super::{fmt_comma_delimited, from_comma_delimited, Header, IntoHeaderValue, Writer};
use crate::http::header;
/// `Cache-Control` header, defined in [RFC7234](https://tools.ietf.org/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
@ -13,13 +16,12 @@ use crate::http::header;
/// not imply that the same directive is to be given in the response.
///
/// # ABNF
///
/// ```text
/// ```plain
/// Cache-Control = 1#cache-directive
/// cache-directive = token [ "=" ( token / quoted-string ) ]
/// ```
///
/// # Example values
/// # Example Values
///
/// * `no-cache`
/// * `private, community="UCI"`
@ -46,11 +48,9 @@ use crate::http::header;
/// CacheDirective::Extension("foo".to_owned(), Some("bar".to_owned())),
/// ]));
/// ```
#[derive(PartialEq, Clone, Debug)]
#[derive(Debug, Clone, PartialEq, Eq, Deref, DerefMut)]
pub struct CacheControl(pub Vec<CacheDirective>);
crate::http::header::common_header_deref!(CacheControl => Vec<CacheDirective>);
// TODO: this could just be the crate::http::header::common_header! macro
impl Header for CacheControl {
fn name() -> header::HeaderName {
@ -88,7 +88,7 @@ impl IntoHeaderValue for CacheControl {
}
/// `CacheControl` contains a list of these directives.
#[derive(PartialEq, Clone, Debug)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CacheDirective {
/// "no-cache"
NoCache,

View File

@ -1,10 +1,14 @@
//! # References
//! The `Content-Disposition` header and associated types.
//!
//! "The Content-Disposition Header Field" <https://www.ietf.org/rfc/rfc2183.txt>
//! "The Content-Disposition Header Field in the Hypertext Transfer Protocol (HTTP)" <https://www.ietf.org/rfc/rfc6266.txt>
//! "Returning Values from Forms: multipart/form-data" <https://www.ietf.org/rfc/rfc7578.txt>
//! Browser conformance tests at: <http://greenbytes.de/tech/tc2231/>
//! IANA assignment: <http://www.iana.org/assignments/cont-disp/cont-disp.xhtml>
//! # References
//! - "The Content-Disposition Header Field":
//! <https://datatracker.ietf.org/doc/html/rfc2183>
//! - "The Content-Disposition Header Field in the Hypertext Transfer Protocol (HTTP)":
//! <https://datatracker.ietf.org/doc/html/rfc6266>
//! - "Returning Values from Forms: multipart/form-data":
//! <https://datatracker.ietf.org/doc/html/rfc7578>
//! - Browser conformance tests at: <http://greenbytes.de/tech/tc2231/>
//! - IANA assignment: <http://www.iana.org/assignments/cont-disp/cont-disp.xhtml>
use once_cell::sync::Lazy;
use regex::Regex;
@ -41,8 +45,9 @@ pub enum DispositionType {
/// rather than process it normally (as per its media type).
Attachment,
/// Used in *multipart/form-data* as defined in [RFC7578](https://tools.ietf.org/html/rfc7578)
/// to carry the field name and optional filename.
/// Used in *multipart/form-data* as defined in
/// [RFC 7578](https://datatracker.ietf.org/doc/html/rfc7578) to carry the field name and
/// optional filename.
FormData,
/// Extension type. Should be handled by recipients the same way as Attachment.
@ -82,26 +87,29 @@ pub enum DispositionParam {
/// A plain file name.
///
/// It is [not supposed](https://tools.ietf.org/html/rfc6266#appendix-D) to contain any
/// non-ASCII characters when used in a *Content-Disposition* HTTP response header, where
/// It is [not supposed](https://datatracker.ietf.org/doc/html/rfc6266#appendix-D) to contain
/// any non-ASCII characters when used in a *Content-Disposition* HTTP response header, where
/// [`FilenameExt`](DispositionParam::FilenameExt) with charset UTF-8 may be used instead
/// in case there are Unicode characters in file names.
Filename(String),
/// An extended file name. It must not exist for `ContentType::Formdata` according to
/// [RFC7578 Section 4.2](https://tools.ietf.org/html/rfc7578#section-4.2).
/// [RFC 7578 §4.2](https://datatracker.ietf.org/doc/html/rfc7578#section-4.2).
FilenameExt(ExtendedValue),
/// An unrecognized regular parameter as defined in
/// [RFC5987](https://tools.ietf.org/html/rfc5987) as *reg-parameter*, in
/// [RFC6266](https://tools.ietf.org/html/rfc6266) as *token "=" value*. Recipients should
/// ignore unrecognizable parameters.
/// [RFC 5987 §3.2.1](https://datatracker.ietf.org/doc/html/rfc5987#section-3.2.1) as
/// `reg-parameter`, in
/// [RFC 6266 §4.1](https://datatracker.ietf.org/doc/html/rfc6266#section-4.1) as
/// `token "=" value`. Recipients should ignore unrecognizable parameters.
Unknown(String, String),
/// An unrecognized extended parameter as defined in
/// [RFC5987](https://tools.ietf.org/html/rfc5987) as *ext-parameter*, in
/// [RFC6266](https://tools.ietf.org/html/rfc6266) as *ext-token "=" ext-value*. The single
/// trailing asterisk is not included. Recipients should ignore unrecognizable parameters.
/// [RFC 5987 §3.2.1](https://datatracker.ietf.org/doc/html/rfc5987#section-3.2.1) as
/// `ext-parameter`, in
/// [RFC 6266 §4.1](https://datatracker.ietf.org/doc/html/rfc6266#section-4.1) as
/// `ext-token "=" ext-value`. The single trailing asterisk is not included. Recipients should
/// ignore unrecognizable parameters.
UnknownExt(String, ExtendedValue),
}
@ -195,10 +203,10 @@ impl DispositionParam {
}
/// A *Content-Disposition* header. It is compatible to be used either as
/// [a response header for the main body](https://mdn.io/Content-Disposition#As_a_response_header_for_the_main_body)
/// as (re)defined in [RFC6266](https://tools.ietf.org/html/rfc6266), or 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)
/// 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)
/// as (re)defined in [RFC7587](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
/// the content is expected to be displayed *inline* in the browser, that is, as a Web page or as
@ -212,7 +220,7 @@ impl DispositionParam {
/// itself, *Content-Disposition* has no effect.
///
/// # ABNF
/// ```text
/// ```plain
/// content-disposition = "Content-Disposition" ":"
/// disposition-type *( ";" disposition-parm )
///
@ -233,19 +241,17 @@ impl DispositionParam {
/// ```
///
/// # Note
/// *filename* is [not supposed](https://datatracker.ietf.org/doc/html/rfc6266#appendix-D) to
/// contain any non-ASCII characters when used in a *Content-Disposition* HTTP response header,
/// where filename* with charset UTF-8 may be used instead in case there are Unicode characters in
/// file names. Filename is [acceptable](https://datatracker.ietf.org/doc/html/rfc7578#section-4.2)
/// to be UTF-8 encoded directly in a *Content-Disposition* header for
/// *multipart/form-data*, though.
///
/// filename is [not supposed](https://tools.ietf.org/html/rfc6266#appendix-D) to contain any
/// non-ASCII characters when used in a *Content-Disposition* HTTP response header, where
/// filename* with charset UTF-8 may be used instead in case there are Unicode characters in file
/// names.
/// filename is [acceptable](https://tools.ietf.org/html/rfc7578#section-4.2) to be UTF-8 encoded
/// directly in a *Content-Disposition* header for *multipart/form-data*, though.
///
/// filename* [must not](https://tools.ietf.org/html/rfc7578#section-4.2) be used within
/// *filename* [must not](https://datatracker.ietf.org/doc/html/rfc7578#section-4.2) be used within
/// *multipart/form-data*.
///
/// # Example
///
/// # Examples
/// ```
/// use actix_web::http::header::{
/// Charset, ContentDisposition, DispositionParam, DispositionType,
@ -291,11 +297,11 @@ impl DispositionParam {
/// ```
///
/// # Security Note
///
/// 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
/// information that may be present. See [RFC2183](https://tools.ietf.org/html/rfc2183#section-2.3).
// TODO: private fields and use smallvec
/// information that may be present.
/// 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)]
pub struct ContentDisposition {
/// The disposition type
@ -342,7 +348,7 @@ impl ContentDisposition {
} else {
// regular parameters
let value = if left.starts_with('\"') {
// quoted-string: defined in RFC6266 -> RFC2616 Section 3.6
// quoted-string: defined in RFC 6266 -> RFC 2616 Section 3.6
let mut escaping = false;
let mut quoted_string = vec![];
let mut end = None;
@ -393,22 +399,22 @@ impl ContentDisposition {
Ok(cd)
}
/// Returns `true` if it is [`Inline`](DispositionType::Inline).
/// Returns `true` if type is [`Inline`](DispositionType::Inline).
pub fn is_inline(&self) -> bool {
matches!(self.disposition, DispositionType::Inline)
}
/// Returns `true` if it is [`Attachment`](DispositionType::Attachment).
/// Returns `true` if type is [`Attachment`](DispositionType::Attachment).
pub fn is_attachment(&self) -> bool {
matches!(self.disposition, DispositionType::Attachment)
}
/// Returns `true` if it is [`FormData`](DispositionType::FormData).
/// Returns `true` if type is [`FormData`](DispositionType::FormData).
pub fn is_form_data(&self) -> bool {
matches!(self.disposition, DispositionType::FormData)
}
/// Returns `true` if it is [`Ext`](DispositionType::Ext) and the `disp_type` matches.
/// Returns `true` if type is [`Ext`](DispositionType::Ext) and the `disp_type` matches.
pub fn is_ext(&self, disp_type: impl AsRef<str>) -> bool {
matches!(
self.disposition,
@ -487,7 +493,9 @@ impl fmt::Display for DispositionParam {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// All ASCII control characters (0-30, 127) including horizontal tab, double quote, and
// backslash should be escaped in quoted-string (i.e. "foobar").
// Ref: RFC6266 S4.1 -> RFC2616 S3.6
//
// Ref: RFC 6266 §4.1 -> RFC 2616 §3.6
//
// filename-parm = "filename" "=" value
// value = token | quoted-string
// quoted-string = ( <"> *(qdtext | quoted-pair ) <"> )
@ -501,7 +509,7 @@ impl fmt::Display for DispositionParam {
// CTL = <any US-ASCII control character
// (octets 0 - 31) and DEL (127)>
//
// Ref: RFC7578 S4.2 -> RFC2183 S2 -> RFC2045 S5.1
// Ref: RFC 7578 S4.2 -> RFC 2183 S2 -> RFC 2045 S5.1
// parameter := attribute "=" value
// attribute := token
// ; Matching of attributes
@ -746,7 +754,7 @@ mod tests {
#[test]
fn from_raw_with_unicode() {
/* RFC7578 Section 4.2:
/* RFC 7578 Section 4.2:
Some commonly deployed systems use multipart/form-data with file names directly encoded
including octets outside the US-ASCII range. The encoding used for the file names is
typically UTF-8, although HTML forms will use the charset associated with the form.
@ -819,9 +827,9 @@ mod tests {
#[test]
fn test_from_raw_unnecessary_percent_decode() {
// In fact, RFC7578 (multipart/form-data) Section 2 and 4.2 suggests that filename with
// In fact, RFC 7578 (multipart/form-data) Section 2 and 4.2 suggests that filename with
// non-ASCII characters MAY be percent-encoded.
// On the contrary, RFC6266 or other RFCs related to Content-Disposition response header
// On the contrary, RFC 6266 or other RFCs related to Content-Disposition response header
// do not mention such percent-encoding.
// So, it appears to be undecidable whether to percent-decode or not without
// knowing the usage scenario (multipart/form-data v.s. HTTP response header) and

View File

@ -1,9 +1,10 @@
use super::{QualityItem, CONTENT_LANGUAGE};
use language_tags::LanguageTag;
crate::http::header::common_header! {
/// `Content-Language` header, defined in
/// [RFC7231](https://tools.ietf.org/html/rfc7231#section-3.1.3.2)
use super::{common_header, QualityItem, CONTENT_LANGUAGE};
common_header! {
/// `Content-Language` header, defined
/// in [RFC 7231 §3.1.3.2](https://datatracker.ietf.org/doc/html/rfc7231#section-3.1.3.2)
///
/// The `Content-Language` header field describes the natural language(s)
/// of the intended audience for the representation. Note that this
@ -11,18 +12,15 @@ crate::http::header::common_header! {
/// representation.
///
/// # ABNF
///
/// ```text
/// ```plain
/// Content-Language = 1#language-tag
/// ```
///
/// # Example values
///
/// # Example Values
/// * `da`
/// * `mi, en`
///
/// # Examples
///
/// ```
/// use actix_web::HttpResponse;
/// use actix_web::http::header::{ContentLanguage, LanguageTag, qitem};
@ -49,7 +47,7 @@ crate::http::header::common_header! {
/// ```
(ContentLanguage, CONTENT_LANGUAGE) => (QualityItem<LanguageTag>)+
test_content_language {
test_parse_and_format {
crate::http::header::common_header_test!(test1, vec![b"da"]);
crate::http::header::common_header_test!(test2, vec![b"mi, en"]);
}

View File

@ -1,15 +1,17 @@
use std::fmt::{self, Display, Write};
use std::str::FromStr;
use std::{
fmt::{self, Display, Write},
str::FromStr,
};
use super::{HeaderValue, IntoHeaderValue, InvalidHeaderValue, Writer, CONTENT_RANGE};
use crate::error::ParseError;
crate::http::header::common_header! {
/// `Content-Range` header, defined in
/// [RFC7233](http://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)
(ContentRange, CONTENT_RANGE) => [ContentRangeSpec]
test_content_range {
test_parse_and_format {
crate::http::header::common_header_test!(test_bytes,
vec![b"bytes 0-499/500"],
Some(ContentRange(ContentRangeSpec::Bytes {
@ -69,11 +71,11 @@ crate::http::header::common_header! {
}
}
/// Content-Range, described in [RFC7233](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
///
/// ```text
/// ```plain
/// Content-Range = byte-content-range
/// / other-content-range
///
@ -89,7 +91,7 @@ crate::http::header::common_header! {
/// other-content-range = other-range-unit SP other-range-resp
/// other-range-resp = *CHAR
/// ```
#[derive(PartialEq, Clone, Debug)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ContentRangeSpec {
/// Byte range
Bytes {

View File

@ -2,8 +2,8 @@ use super::CONTENT_TYPE;
use mime::Mime;
crate::http::header::common_header! {
/// `Content-Type` header, defined in
/// [RFC7231](http://tools.ietf.org/html/rfc7231#section-3.1.1.5)
/// `Content-Type` header, defined
/// in [RFC 7231 §3.1.1.5](https://datatracker.ietf.org/doc/html/rfc7231#section-3.1.1.5)
///
/// The `Content-Type` header field indicates the media type of the
/// associated representation: either the representation enclosed in the
@ -18,18 +18,15 @@ crate::http::header::common_header! {
/// this is an issue, it's possible to implement `Header` on a custom struct.
///
/// # ABNF
///
/// ```text
/// ```plain
/// Content-Type = media-type
/// ```
///
/// # Example values
///
/// # Example Values
/// * `text/html; charset=utf-8`
/// * `application/json`
///
/// # Examples
///
/// ```
/// use actix_web::HttpResponse;
/// use actix_web::http::header::ContentType;
@ -51,7 +48,7 @@ crate::http::header::common_header! {
/// ```
(ContentType, CONTENT_TYPE) => [Mime]
test_content_type {
test_parse_and_format {
crate::http::header::common_header_test!(
test1,
vec![b"text/html"],
@ -113,5 +110,3 @@ impl ContentType {
ContentType(mime::APPLICATION_OCTET_STREAM)
}
}
impl Eq for ContentType {}

View File

@ -2,19 +2,18 @@ use super::{HttpDate, DATE};
use std::time::SystemTime;
crate::http::header::common_header! {
/// `Date` header, defined in [RFC7231](http://tools.ietf.org/html/rfc7231#section-7.1.1.2)
/// `Date` header, defined
/// in [RFC 7231 §7.1.1.2](https://datatracker.ietf.org/doc/html/rfc7231#section-7.1.1.2)
///
/// The `Date` header field represents the date and time at which the
/// message was originated.
///
/// # ABNF
///
/// ```text
/// ```plain
/// Date = HTTP-date
/// ```
///
/// # Example values
///
/// # Example Values
/// * `Tue, 15 Nov 1994 08:12:31 GMT`
///
/// # Example
@ -31,7 +30,7 @@ crate::http::header::common_header! {
/// ```
(Date, DATE) => [HttpDate]
test_date {
test_parse_and_format {
crate::http::header::common_header_test!(test1, vec![b"Tue, 15 Nov 1994 08:12:31 GMT"]);
}
}

View File

@ -1,5 +1,7 @@
use std::fmt::{self, Display, Write};
use std::str::FromStr;
use std::{
fmt::{self, Display, Write},
str::FromStr,
};
use super::{HeaderValue, IntoHeaderValue, InvalidHeaderValue, Writer};
@ -15,7 +17,8 @@ fn check_slice_validity(slice: &str) -> bool {
slice.bytes().all(entity_validate_char)
}
/// An entity tag, defined in [RFC7232](https://tools.ietf.org/html/rfc7232#section-2.3)
/// An entity tag, defined
/// in [RFC 7232 §2.3](https://datatracker.ietf.org/doc/html/rfc7232#section-2.3)
///
/// An entity tag consists of a string enclosed by two literal double quotes.
/// Preceding the first double quote is an optional weakness indicator,
@ -23,8 +26,7 @@ fn check_slice_validity(slice: &str) -> bool {
/// `W/"xyzzy"`.
///
/// # ABNF
///
/// ```text
/// ```plain
/// entity-tag = [ weak ] opaque-tag
/// weak = %x57.2F ; "W/", case-sensitive
/// opaque-tag = DQUOTE *etagc DQUOTE

View File

@ -1,7 +1,8 @@
use super::{EntityTag, ETAG};
crate::http::header::common_header! {
/// `ETag` header, defined in [RFC7232](http://tools.ietf.org/html/rfc7232#section-2.3)
/// `ETag` header, defined in
/// [RFC 7232 §2.3](https://datatracker.ietf.org/doc/html/rfc7232#section-2.3)
///
/// The `ETag` header field in a response provides the current entity-tag
/// for the selected representation, as determined at the conclusion of
@ -14,19 +15,16 @@ crate::http::header::common_header! {
/// prefixed by a weakness indicator.
///
/// # ABNF
///
/// ```text
/// ```plain
/// ETag = entity-tag
/// ```
///
/// # Example values
///
/// # Example Values
/// * `"xyzzy"`
/// * `W/"xyzzy"`
/// * `""`
///
/// # Examples
///
/// ```
/// use actix_web::HttpResponse;
/// use actix_web::http::header::{ETag, EntityTag};
@ -48,7 +46,7 @@ crate::http::header::common_header! {
/// ```
(ETag, ETAG) => [EntityTag]
test_etag {
test_parse_and_format {
// From the RFC
crate::http::header::common_header_test!(test1,
vec![b"\"xyzzy\""],

View File

@ -1,7 +1,8 @@
use super::{HttpDate, EXPIRES};
crate::http::header::common_header! {
/// `Expires` header, defined in [RFC7234](http://tools.ietf.org/html/rfc7234#section-5.3)
/// `Expires` header, defined
/// in [RFC 7234 §5.3](https://datatracker.ietf.org/doc/html/rfc7234#section-5.3)
///
/// The `Expires` header field gives the date/time after which the
/// response is considered stale.
@ -11,12 +12,11 @@ crate::http::header::common_header! {
/// time.
///
/// # ABNF
///
/// ```text
/// ```plain
/// Expires = HTTP-date
/// ```
///
/// # Example values
/// # Example Values
/// * `Thu, 01 Dec 1994 16:00:00 GMT`
///
/// # Example
@ -34,7 +34,7 @@ crate::http::header::common_header! {
/// ```
(Expires, EXPIRES) => [HttpDate]
test_expires {
test_parse_and_format {
// Test case from RFC
crate::http::header::common_header_test!(test1, vec![b"Thu, 01 Dec 1994 16:00:00 GMT"]);
}

View File

@ -1,8 +1,8 @@
use super::{EntityTag, IF_MATCH};
use super::{common_header, EntityTag, IF_MATCH};
crate::http::header::common_header! {
/// `If-Match` header, defined in
/// [RFC7232](https://tools.ietf.org/html/rfc7232#section-3.1)
common_header! {
/// `If-Match` header, defined
/// in [RFC 7232 §3.1](https://datatracker.ietf.org/doc/html/rfc7232#section-3.1)
///
/// The `If-Match` header field makes the request method conditional on
/// the recipient origin server either having at least one current
@ -17,18 +17,15 @@ crate::http::header::common_header! {
/// there have been any changes to the representation data.
///
/// # ABNF
///
/// ```text
/// ```plain
/// If-Match = "*" / 1#entity-tag
/// ```
///
/// # Example values
///
/// # Example Values
/// * `"xyzzy"`
/// * "xyzzy", "r2d2xxxx", "c3piozzzz"
///
/// # Examples
///
/// ```
/// use actix_web::HttpResponse;
/// use actix_web::http::header::IfMatch;
@ -52,7 +49,7 @@ crate::http::header::common_header! {
/// ```
(IfMatch, IF_MATCH) => {Any / (EntityTag)+}
test_if_match {
test_parse_and_format {
crate::http::header::common_header_test!(
test1,
vec![b"\"xyzzy\""],

View File

@ -1,8 +1,8 @@
use super::{HttpDate, IF_MODIFIED_SINCE};
crate::http::header::common_header! {
/// `If-Modified-Since` header, defined in
/// [RFC7232](http://tools.ietf.org/html/rfc7232#section-3.3)
/// `If-Modified-Since` header, defined
/// in [RFC 7232 §3.3](https://datatracker.ietf.org/doc/html/rfc7232#section-3.3)
///
/// The `If-Modified-Since` header field makes a GET or HEAD request
/// method conditional on the selected representation's modification date
@ -11,12 +11,11 @@ crate::http::header::common_header! {
/// data has not changed.
///
/// # ABNF
///
/// ```text
/// ```plain
/// If-Unmodified-Since = HTTP-date
/// ```
///
/// # Example values
/// # Example Values
/// * `Sat, 29 Oct 1994 19:43:31 GMT`
///
/// # Example
@ -34,7 +33,7 @@ crate::http::header::common_header! {
/// ```
(IfModifiedSince, IF_MODIFIED_SINCE) => [HttpDate]
test_if_modified_since {
test_parse_and_format {
// Test case from RFC
crate::http::header::common_header_test!(test1, vec![b"Sat, 29 Oct 1994 19:43:31 GMT"]);
}

View File

@ -1,8 +1,8 @@
use super::{EntityTag, IF_NONE_MATCH};
crate::http::header::common_header! {
/// `If-None-Match` header, defined in
/// [RFC7232](https://tools.ietf.org/html/rfc7232#section-3.2)
/// `If-None-Match` header, defined
/// in [RFC 7232 §3.2](https://datatracker.ietf.org/doc/html/rfc7232#section-3.2)
///
/// The `If-None-Match` header field makes the request method conditional
/// on a recipient cache or origin server either not having any current
@ -16,13 +16,11 @@ crate::http::header::common_header! {
/// the representation data.
///
/// # ABNF
///
/// ```text
/// ```plain
/// If-None-Match = "*" / 1#entity-tag
/// ```
///
/// # Example values
///
/// # Example Values
/// * `"xyzzy"`
/// * `W/"xyzzy"`
/// * `"xyzzy", "r2d2xxxx", "c3piozzzz"`
@ -30,7 +28,6 @@ crate::http::header::common_header! {
/// * `*`
///
/// # Examples
///
/// ```
/// use actix_web::HttpResponse;
/// use actix_web::http::header::IfNoneMatch;
@ -54,7 +51,7 @@ crate::http::header::common_header! {
/// ```
(IfNoneMatch, IF_NONE_MATCH) => {Any / (EntityTag)+}
test_if_none_match {
test_parse_and_format {
crate::http::header::common_header_test!(test1, vec![b"\"xyzzy\""]);
crate::http::header::common_header_test!(test2, vec![b"W/\"xyzzy\""]);
crate::http::header::common_header_test!(test3, vec![b"\"xyzzy\", \"r2d2xxxx\", \"c3piozzzz\""]);

View File

@ -8,7 +8,8 @@ use crate::error::ParseError;
use crate::http::header;
use crate::HttpMessage;
/// `If-Range` header, defined in [RFC7233](http://tools.ietf.org/html/rfc7233#section-3.2)
/// `If-Range` header, defined
/// in [RFC 7233 §3.2](https://datatracker.ietf.org/doc/html/rfc7233#section-3.2)
///
/// If a client has a partial copy of a representation and wishes to have
/// an up-to-date copy of the entire representation, it could use the
@ -24,18 +25,16 @@ use crate::HttpMessage;
/// in Range; otherwise, send me the entire representation.
///
/// # ABNF
///
/// ```text
/// ```plain
/// If-Range = entity-tag / HTTP-date
/// ```
///
/// # Example values
/// # Example Values
///
/// * `Sat, 29 Oct 1994 19:43:31 GMT`
/// * `\"xyzzy\"`
///
/// # Examples
///
/// ```
/// use actix_web::HttpResponse;
/// use actix_web::http::header::{EntityTag, IfRange};
@ -108,10 +107,11 @@ impl IntoHeaderValue for IfRange {
}
#[cfg(test)]
mod test_if_range {
mod test_parse_and_format {
use std::str;
use super::IfRange as HeaderField;
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!(test2, vec![b"\"abc\""]);

View File

@ -1,8 +1,8 @@
use super::{HttpDate, IF_UNMODIFIED_SINCE};
crate::http::header::common_header! {
/// `If-Unmodified-Since` header, defined in
/// [RFC7232](http://tools.ietf.org/html/rfc7232#section-3.4)
/// `If-Unmodified-Since` header, defined
/// in [RFC 7232 §3.4](https://datatracker.ietf.org/doc/html/rfc7232#section-3.4)
///
/// The `If-Unmodified-Since` header field makes the request method
/// conditional on the selected representation's last modification date
@ -11,13 +11,11 @@ crate::http::header::common_header! {
/// the user agent does not have an entity-tag for the representation.
///
/// # ABNF
///
/// ```text
/// ```plain
/// If-Unmodified-Since = HTTP-date
/// ```
///
/// # Example values
///
/// # Example Values
/// * `Sat, 29 Oct 1994 19:43:31 GMT`
///
/// # Example
@ -35,7 +33,7 @@ crate::http::header::common_header! {
/// ```
(IfUnmodifiedSince, IF_UNMODIFIED_SINCE) => [HttpDate]
test_if_unmodified_since {
test_parse_and_format {
// Test case from RFC
crate::http::header::common_header_test!(test1, vec![b"Sat, 29 Oct 1994 19:43:31 GMT"]);
}

View File

@ -1,8 +1,8 @@
use super::{HttpDate, LAST_MODIFIED};
crate::http::header::common_header! {
/// `Last-Modified` header, defined in
/// [RFC7232](http://tools.ietf.org/html/rfc7232#section-2.2)
/// `Last-Modified` header, defined
/// in [RFC 7232 §2.2](https://datatracker.ietf.org/doc/html/rfc7232#section-2.2)
///
/// The `Last-Modified` header field in a response provides a timestamp
/// indicating the date and time at which the origin server believes the
@ -10,13 +10,11 @@ crate::http::header::common_header! {
/// conclusion of handling the request.
///
/// # ABNF
///
/// ```text
/// ```plain
/// Expires = HTTP-date
/// ```
///
/// # Example values
///
/// # Example Values
/// * `Sat, 29 Oct 1994 19:43:31 GMT`
///
/// # Example
@ -34,8 +32,8 @@ crate::http::header::common_header! {
/// ```
(LastModified, LAST_MODIFIED) => [HttpDate]
test_last_modified {
// Test case from RFC
crate::http::header::common_header_test!(test1, vec![b"Sat, 29 Oct 1994 19:43:31 GMT"]);
}
test_parse_and_format {
// Test case from RFC
crate::http::header::common_header_test!(test1, vec![b"Sat, 29 Oct 1994 19:43:31 GMT"]);
}
}

View File

@ -1,33 +1,14 @@
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 {
($id:ident, $tm:ident{$($tf:item)*}) => {
#[allow(unused_imports)]
#[cfg(test)]
mod $tm {
#![allow(unused_imports)]
use std::str;
use actix_http::http::Method;
use mime::*;
use $crate::http::header::*;
use super::$id as HeaderField;
use super::{$id as HeaderField, *};
$($tf)*
}
}
@ -42,14 +23,19 @@ macro_rules! common_header_test {
let raw = $raw;
let a: Vec<Vec<u8>> = raw.iter().map(|x| x.to_vec()).collect();
let mut req = test::TestRequest::default();
for item in a {
req = req.insert_header((HeaderField::name(), item)).take();
}
let req = req.finish();
let value = HeaderField::parse(&req);
let result = format!("{}", value.unwrap());
let expected = String::from_utf8(raw[0].to_vec()).unwrap();
let result_cmp: Vec<String> = result
.to_ascii_lowercase()
.split(' ')
@ -60,10 +46,12 @@ macro_rules! common_header_test {
.split(' ')
.map(|x| x.to_owned())
.collect();
assert_eq!(result_cmp.concat(), expected_cmp.concat());
}
};
($id:ident, $raw:expr, $typed:expr) => {
($id:ident, $raw:expr, $exp:expr) => {
#[test]
fn $id() {
use actix_http::test;
@ -75,128 +63,137 @@ macro_rules! common_header_test {
}
let req = req.finish();
let val = HeaderField::parse(&req);
let typed: Option<HeaderField> = $typed;
// Test parsing
assert_eq!(val.ok(), typed);
// Test formatting
if typed.is_some() {
let exp: Option<HeaderField> = $exp;
// test parsing
assert_eq!(val.ok(), exp);
// test formatting
if let Some(exp) = exp {
let raw = &($raw)[..];
let mut iter = raw.iter().map(|b| str::from_utf8(&b[..]).unwrap());
let mut joined = String::new();
joined.push_str(iter.next().unwrap());
for s in iter {
joined.push_str(", ");
if let Some(s) = iter.next() {
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 {
// $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
// $n:expr: Lowercase name of the header
// $nn:expr: Nice name of the header
// List header, zero or more items
($(#[$a:meta])*($id:ident, $name:expr) => ($item:ty)*) => {
$(#[$a])*
#[derive(Clone, Debug, PartialEq)]
($(#[$attrs:meta])*($id:ident, $name:expr) => ($item:ty)*) => {
$(#[$attrs])*
#[derive(Debug, Clone, PartialEq, Eq, ::derive_more::Deref, ::derive_more::DerefMut)]
pub struct $id(pub Vec<$item>);
crate::http::header::common_header_deref!($id => Vec<$item>);
impl $crate::http::header::Header for $id {
#[inline]
fn name() -> $crate::http::header::HeaderName {
$name
}
#[inline]
fn parse<T>(msg: &T) -> Result<Self, $crate::error::ParseError>
where T: $crate::HttpMessage
{
$crate::http::header::from_comma_delimited(
msg.headers().get_all(Self::name())).map($id)
fn parse<M: $crate::HttpMessage>(msg: &M) -> Result<Self, $crate::error::ParseError> {
let headers = msg.headers().get_all(Self::name());
$crate::http::header::from_comma_delimited(headers).map($id)
}
}
impl std::fmt::Display for $id {
impl ::core::fmt::Display for $id {
#[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[..])
}
}
impl $crate::http::header::IntoHeaderValue for $id {
type Error = $crate::http::header::InvalidHeaderValue;
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 _ = write!(&mut writer, "{}", self);
$crate::http::header::HeaderValue::from_maybe_shared(writer.take())
}
}
};
// List header, one or more items
($(#[$a:meta])*($id:ident, $name:expr) => ($item:ty)+) => {
$(#[$a])*
#[derive(Clone, Debug, PartialEq)]
($(#[$attrs:meta])*($id:ident, $name:expr) => ($item:ty)+) => {
$(#[$attrs])*
#[derive(Debug, Clone, PartialEq, Eq, ::derive_more::Deref, ::derive_more::DerefMut)]
pub struct $id(pub Vec<$item>);
crate::http::header::common_header_deref!($id => Vec<$item>);
impl $crate::http::header::Header for $id {
#[inline]
fn name() -> $crate::http::header::HeaderName {
$name
}
#[inline]
fn parse<T>(msg: &T) -> Result<Self, $crate::error::ParseError>
where T: $crate::HttpMessage
{
fn parse<M: $crate::HttpMessage>(msg: &M) -> Result<Self, $crate::error::ParseError> {
$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]
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[..])
}
}
impl $crate::http::header::IntoHeaderValue for $id {
type Error = $crate::http::header::InvalidHeaderValue;
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 _ = write!(&mut writer, "{}", self);
$crate::http::header::HeaderValue::from_maybe_shared(writer.take())
}
}
};
// Single value header
($(#[$a:meta])*($id:ident, $name:expr) => [$value:ty]) => {
$(#[$a])*
#[derive(Clone, Debug, PartialEq)]
($(#[$attrs:meta])*($id:ident, $name:expr) => [$value:ty]) => {
$(#[$attrs])*
#[derive(Debug, Clone, PartialEq, Eq, ::derive_more::Deref, ::derive_more::DerefMut)]
pub struct $id(pub $value);
crate::http::header::common_header_deref!($id => $value);
impl $crate::http::header::Header for $id {
#[inline]
fn name() -> $crate::http::header::HeaderName {
$name
}
#[inline]
fn parse<T>(msg: &T) -> Result<Self, $crate::error::ParseError>
where T: $crate::HttpMessage
{
$crate::http::header::from_one_raw_str(
msg.headers().get(Self::name())).map($id)
fn parse<M: $crate::HttpMessage>(msg: &M) -> Result<Self, $crate::error::ParseError> {
let header = msg.headers().get(Self::name());
$crate::http::header::from_one_raw_str(header).map($id)
}
}
impl std::fmt::Display for $id {
impl ::core::fmt::Display for $id {
#[inline]
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(&self.0, f)
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
::core::fmt::Display::fmt(&self.0, f)
}
}
impl $crate::http::header::IntoHeaderValue for $id {
type Error = $crate::http::header::InvalidHeaderValue;
@ -205,9 +202,10 @@ macro_rules! common_header {
}
}
};
// List header, one or more items with "*" option
($(#[$a:meta])*($id:ident, $name:expr) => {Any / ($item:ty)+}) => {
$(#[$a])*
($(#[$attrs:meta])*($id:ident, $name:expr) => {Any / ($item:ty)+}) => {
$(#[$attrs])*
#[derive(Clone, Debug, PartialEq)]
pub enum $id {
/// Any value is a match
@ -215,42 +213,46 @@ macro_rules! common_header {
/// Only the listed items are a match
Items(Vec<$item>),
}
impl $crate::http::header::Header for $id {
#[inline]
fn name() -> $crate::http::header::HeaderName {
$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)
} else {
Ok($id::Items(
$crate::http::header::from_comma_delimited(
msg.headers().get_all(Self::name()))?))
let headers = msg.headers().get_all(Self::name());
Ok($id::Items($crate::http::header::from_comma_delimited(headers)?))
}
}
}
impl std::fmt::Display for $id {
impl ::core::fmt::Display for $id {
#[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 {
$id::Any => f.write_str("*"),
$id::Items(ref fields) => $crate::http::header::fmt_comma_delimited(
f, &fields[..])
$id::Items(ref fields) =>
$crate::http::header::fmt_comma_delimited(f, &fields[..])
}
}
}
impl $crate::http::header::IntoHeaderValue for $id {
type Error = $crate::http::header::InvalidHeaderValue;
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 _ = write!(&mut writer, "{}", self);
$crate::http::header::HeaderValue::from_maybe_shared(writer.take())
@ -259,32 +261,32 @@ macro_rules! common_header {
};
// 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! {
$(#[$a])*
$(#[$attrs])*
($id, $name) => ($item)*
}
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! {
$(#[$a])*
$(#[$attrs])*
($id, $n) => ($item)+
}
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! {
$(#[$a])* ($id, $name) => [$item]
$(#[$attrs])* ($id, $name) => [$item]
}
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! {
$(#[$a])*
$(#[$attrs])*
($id, $name) => {Any / ($item)+}
}
@ -292,7 +294,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)]
pub(crate) use common_header_test;

View File

@ -1,15 +1,52 @@
//! 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,
//! the [mime] crate is used in such headers as [`ContentType`] and [`Accept`].
use bytes::{Bytes, BytesMut};
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::*;
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;
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_charset::AcceptCharset;
//pub use self::accept_encoding::AcceptEncoding;
pub use self::accept::Accept;
pub use self::accept_language::AcceptLanguage;
@ -30,11 +67,10 @@ pub use self::if_none_match::IfNoneMatch;
pub use self::if_range::IfRange;
pub use self::if_unmodified_since::IfUnmodifiedSince;
pub use self::last_modified::LastModified;
pub use self::preference::Preference;
//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)]
struct Writer {
buf: BytesMut,
@ -62,30 +98,3 @@ impl fmt::Write for Writer {
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};

View 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),
}
}
}

View File

@ -1,19 +1,21 @@
use std::fmt::{self, Display};
use std::str::FromStr;
// TODO: reinstate module
use super::parsing::from_one_raw_str;
use super::{Header, Raw};
use std::{
fmt::{self, Display},
str::FromStr,
};
/// `Range` header, defined in [RFC7233](https://tools.ietf.org/html/rfc7233#section-3.1)
use super::{parsing::from_one_raw_str, Header, Raw};
/// `Range` header, defined
/// in [RFC 7233 §3.1](https://datatracker.ietf.org/doc/html/rfc7233#section-3.1)
///
/// The "Range" header field on a GET request modifies the method
/// semantics to request transfer of only one or more sub-ranges of the
/// selected representation data, rather than the entire selected
/// The "Range" header field on a GET request modifies the method semantics to request transfer of
/// only one or more sub-ranges of the selected representation data, rather than the entire selected
/// representation data.
///
/// # ABNF
///
/// ```text
/// ```plain
/// Range = byte-ranges-specifier / other-ranges-specifier
/// other-ranges-specifier = other-range-unit "=" other-range-set
/// other-range-set = 1*VCHAR
@ -27,8 +29,7 @@ use super::{Header, Raw};
/// last-byte-pos = 1*DIGIT
/// ```
///
/// # Example values
///
/// # Example Values
/// * `bytes=1000-`
/// * `bytes=-2000`
/// * `bytes=0-1,30-40`
@ -37,7 +38,6 @@ use super::{Header, Raw};
/// * `custom_unit=xxx-yyy`
///
/// # Examples
///
/// ```
/// use hyper::header::{Headers, Range, ByteRangeSpec};
///
@ -63,6 +63,7 @@ use super::{Header, Raw};
pub enum Range {
/// Byte range
Bytes(Vec<ByteRangeSpec>),
/// Custom range, with unit not registered at IANA
/// (`other-range-unit`: String , `other-range-set`: String)
Unregistered(String, String),
@ -74,8 +75,10 @@ pub enum Range {
pub enum ByteRangeSpec {
/// Get all bytes between x and y ("x-y")
FromTo(u64, u64),
/// Get all bytes starting from x ("x-")
AllFrom(u64),
/// Get last x bytes ("-x")
Last(u64),
}
@ -93,7 +96,7 @@ impl ByteRangeSpec {
/// simply ignore the range header and serve the full entity using a `200
/// OK` status code.
///
/// This function closely follows [RFC 7233][1] section 2.1.
/// This function closely follows [RFC 7233 §2.1].
/// As such, it considers ranges to be satisfiable if they meet the
/// following conditions:
///
@ -112,7 +115,7 @@ impl ByteRangeSpec {
/// value of last-byte-pos with a value that is one less than the current
/// length of the selected representation).
///
/// [1]: https://tools.ietf.org/html/rfc7233
/// [RFC 7233 §2.1]: https://datatracker.ietf.org/doc/html/rfc7233
pub fn to_satisfiable_range(&self, full_length: u64) -> Option<(u64, u64)> {
// If the full length is zero, there is no satisfiable end-inclusive range.
if full_length == 0 {
@ -238,9 +241,7 @@ impl FromStr for ByteRangeSpec {
.or(Err(::Error::Header))
.map(ByteRangeSpec::AllFrom),
(Some(start), Some(end)) => match (start.parse(), end.parse()) {
(Ok(start), Ok(end)) if start <= end => {
Ok(ByteRangeSpec::FromTo(start, end))
}
(Ok(start), Ok(end)) if start <= end => Ok(ByteRangeSpec::FromTo(start, end)),
_ => Err(::Error::Header),
},
_ => Err(::Error::Header),
@ -248,16 +249,6 @@ impl FromStr for ByteRangeSpec {
}
}
fn from_comma_delimited<T: FromStr>(s: &str) -> Vec<T> {
s.split(',')
.filter_map(|x| match x.trim() {
"" => None,
y => Some(y),
})
.filter_map(|x| x.parse().ok())
.collect()
}
impl Header for Range {
fn header_name() -> &'static str {
static NAME: &'static str = "Range";
@ -286,8 +277,7 @@ mod tests {
assert_eq!(r2, r3);
let r: Range = Header::parse_header(&"bytes=1-100,200-".into()).unwrap();
let r2: Range =
Header::parse_header(&"bytes= 1-100 , 101-xxx, 200- ".into()).unwrap();
let r2: Range = Header::parse_header(&"bytes= 1-100 , 101-xxx, 200- ".into()).unwrap();
let r3 = Range::Bytes(vec![
ByteRangeSpec::FromTo(1, 100),
ByteRangeSpec::AllFrom(200),

View File

@ -561,7 +561,6 @@ where
/// The max number of services can be grouped together is 12.
///
/// # Examples
///
/// ```
/// use actix_web::{services, web, App};
///