mirror of
https://github.com/fafhrd91/actix-web
synced 2025-07-03 17:41:30 +02:00
Compare commits
25 Commits
test-v0.1.
...
improve-ty
Author | SHA1 | Date | |
---|---|---|---|
30da2e5618 | |||
43a0e8102f | |||
183fbdfd74 | |||
57ee49a618 | |||
075d871e63 | |||
c4b20df56a | |||
a75212695a | |||
2f9c97461a | |||
0df275c478 | |||
a0eb0d22be | |||
303843dcda | |||
75b026b740 | |||
697238fadc | |||
e045418038 | |||
a978b417f3 | |||
fa82b698b7 | |||
fc4cdf81eb | |||
654dc64a09 | |||
cf54388534 | |||
39243095b5 | |||
89c6d62656 | |||
52bbbd1d73 | |||
3e6e9779dc | |||
9bdd334bb4 | |||
bcbbc115aa |
8
.github/workflows/ci.yml
vendored
8
.github/workflows/ci.yml
vendored
@ -159,7 +159,7 @@ jobs:
|
||||
with: { file: cobertura.xml }
|
||||
|
||||
rustdoc:
|
||||
name: rustdoc
|
||||
name: doc tests
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
@ -177,12 +177,6 @@ jobs:
|
||||
- name: Cache Dependencies
|
||||
uses: Swatinem/rust-cache@v1.3.0
|
||||
|
||||
# - name: Install cargo-hack
|
||||
# uses: actions-rs/cargo@v1
|
||||
# with:
|
||||
# command: install
|
||||
# args: cargo-hack
|
||||
|
||||
- name: doc tests
|
||||
uses: actions-rs/cargo@v1
|
||||
timeout-minutes: 60
|
||||
|
18
CHANGES.md
18
CHANGES.md
@ -1,6 +1,24 @@
|
||||
# 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
|
||||
### Changed
|
||||
* Update `actix-tls` to `3.0.0-rc.1`. [#2474]
|
||||
|
||||
[#2474]: https://github.com/actix/actix-web/pull/2474
|
||||
|
||||
|
||||
## 4.0.0-beta.12 - 2021-11-22
|
||||
|
17
Cargo.toml
17
Cargo.toml
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "actix-web"
|
||||
version = "4.0.0-beta.12"
|
||||
version = "4.0.0-beta.13"
|
||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||
description = "Actix Web is a powerful, pragmatic, and extremely fast web framework for Rust"
|
||||
keywords = ["actix", "http", "web", "framework", "async"]
|
||||
@ -75,9 +75,9 @@ actix-rt = "2.3"
|
||||
actix-server = "2.0.0-beta.9"
|
||||
actix-service = "2.0.0"
|
||||
actix-utils = "3.0.0"
|
||||
actix-tls = { version = "3.0.0-beta.9", default-features = false, optional = true }
|
||||
actix-tls = { version = "3.0.0-rc.1", default-features = false, optional = true }
|
||||
|
||||
actix-http = "3.0.0-beta.13"
|
||||
actix-http = "3.0.0-beta.14"
|
||||
actix-router = "0.5.0-beta.2"
|
||||
actix-web-codegen = "0.5.0-beta.5"
|
||||
|
||||
@ -96,7 +96,7 @@ once_cell = "1.5"
|
||||
log = "0.4"
|
||||
mime = "0.3"
|
||||
paste = "1"
|
||||
pin-project = "1.0.0"
|
||||
pin-project-lite = "0.2.7"
|
||||
regex = "1.4"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
@ -143,6 +143,15 @@ actix-web-actors = { path = "actix-web-actors" }
|
||||
actix-web-codegen = { path = "actix-web-codegen" }
|
||||
awc = { path = "awc" }
|
||||
|
||||
# uncomment for quick testing against local actix-net repo
|
||||
# actix-service = { path = "../actix-net/actix-service" }
|
||||
# actix-macros = { path = "../actix-net/actix-macros" }
|
||||
# actix-rt = { path = "../actix-net/actix-rt" }
|
||||
# actix-codec = { path = "../actix-net/actix-codec" }
|
||||
# actix-utils = { path = "../actix-net/actix-utils" }
|
||||
# actix-tls = { path = "../actix-net/actix-tls" }
|
||||
# actix-server = { path = "../actix-net/actix-server" }
|
||||
|
||||
[[test]]
|
||||
name = "test_server"
|
||||
required-features = ["compress-brotli", "compress-gzip", "compress-zstd", "cookies"]
|
||||
|
@ -6,10 +6,10 @@
|
||||
<p>
|
||||
|
||||
[](https://crates.io/crates/actix-web)
|
||||
[](https://docs.rs/actix-web/4.0.0-beta.12)
|
||||
[](https://docs.rs/actix-web/4.0.0-beta.13)
|
||||
[](https://blog.rust-lang.org/2021/05/06/Rust-1.52.0.html)
|
||||

|
||||
[](https://deps.rs/crate/actix-web/4.0.0-beta.12)
|
||||
[](https://deps.rs/crate/actix-web/4.0.0-beta.13)
|
||||
<br />
|
||||
[](https://github.com/actix/actix-web/actions)
|
||||
[](https://codecov.io/gh/actix/actix-web)
|
||||
|
@ -23,8 +23,9 @@ experimental-io-uring = ["actix-web/experimental-io-uring", "tokio-uring"]
|
||||
|
||||
[dependencies]
|
||||
actix-web = { version = "4.0.0-beta.11", default-features = false }
|
||||
actix-http = "3.0.0-beta.13"
|
||||
actix-service = "2.0.0"
|
||||
actix-http = "3.0.0-beta.14"
|
||||
actix-service = "2"
|
||||
actix-utils = "3"
|
||||
|
||||
askama_escape = "0.10"
|
||||
bitflags = "1"
|
||||
|
@ -1,9 +1,9 @@
|
||||
use std::{
|
||||
future::{ready, Ready},
|
||||
path::{Path, PathBuf},
|
||||
str::FromStr,
|
||||
};
|
||||
|
||||
use actix_utils::future::{ready, Ready};
|
||||
use actix_web::{dev::Payload, FromRequest, HttpRequest};
|
||||
|
||||
use crate::error::UriSegmentError;
|
||||
|
@ -3,6 +3,12 @@
|
||||
## Unreleased - 2021-xx-xx
|
||||
|
||||
|
||||
## 3.0.0-beta.8 - 2021-11-30
|
||||
* Update `actix-tls` to `3.0.0-rc.1`. [#2474]
|
||||
|
||||
[#2474]: https://github.com/actix/actix-web/pull/2474
|
||||
|
||||
|
||||
## 3.0.0-beta.7 - 2021-11-22
|
||||
* Fix compatibility with experimental `io-uring` feature of `actix-rt`. [#2408]
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "actix-http-test"
|
||||
version = "3.0.0-beta.7"
|
||||
version = "3.0.0-beta.8"
|
||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||
description = "Various helpers for Actix applications to use during testing"
|
||||
keywords = ["http", "web", "framework", "async", "futures"]
|
||||
@ -31,7 +31,7 @@ openssl = ["tls-openssl", "awc/openssl"]
|
||||
[dependencies]
|
||||
actix-service = "2.0.0"
|
||||
actix-codec = "0.4.1"
|
||||
actix-tls = "3.0.0-beta.9"
|
||||
actix-tls = "3.0.0-rc.1"
|
||||
actix-utils = "3.0.0"
|
||||
actix-rt = "2.2"
|
||||
actix-server = "2.0.0-beta.9"
|
||||
@ -52,4 +52,4 @@ tokio = { version = "1.2", features = ["sync"] }
|
||||
|
||||
[dev-dependencies]
|
||||
actix-web = { version = "4.0.0-beta.11", default-features = false, features = ["cookies"] }
|
||||
actix-http = "3.0.0-beta.13"
|
||||
actix-http = "3.0.0-beta.14"
|
||||
|
@ -3,11 +3,11 @@
|
||||
> Various helpers for Actix applications to use during testing.
|
||||
|
||||
[](https://crates.io/crates/actix-http-test)
|
||||
[](https://docs.rs/actix-http-test/3.0.0-beta.7)
|
||||
[](https://docs.rs/actix-http-test/3.0.0-beta.8)
|
||||
[](https://blog.rust-lang.org/2021/05/06/Rust-1.52.0.html)
|
||||

|
||||
<br>
|
||||
[](https://deps.rs/crate/actix-http-test/3.0.0-beta.7)
|
||||
[](https://deps.rs/crate/actix-http-test/3.0.0-beta.8)
|
||||
[](https://crates.io/crates/actix-http-test)
|
||||
[](https://discord.gg/NWpN5mmg3x)
|
||||
|
||||
|
@ -3,6 +3,18 @@
|
||||
## Unreleased - 2021-xx-xx
|
||||
|
||||
|
||||
## 3.0.0-beta.14 - 2021-11-30
|
||||
### Changed
|
||||
* Guarantee ordering of `header::GetAll` iterator to be same as insertion order. [#2467]
|
||||
* Expose `header::map` module. [#2467]
|
||||
* Implement `ExactSizeIterator` and `FusedIterator` for all `HeaderMap` iterators. [#2470]
|
||||
* Update `actix-tls` to `3.0.0-rc.1`. [#2474]
|
||||
|
||||
[#2467]: https://github.com/actix/actix-web/pull/2467
|
||||
[#2470]: https://github.com/actix/actix-web/pull/2470
|
||||
[#2474]: https://github.com/actix/actix-web/pull/2474
|
||||
|
||||
|
||||
## 3.0.0-beta.13 - 2021-11-22
|
||||
### Added
|
||||
* `body::AnyBody::empty` for quickly creating an empty body. [#2446]
|
||||
|
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "actix-http"
|
||||
version = "3.0.0-beta.13"
|
||||
version = "3.0.0-beta.14"
|
||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||
description = "HTTP primitives for the Actix ecosystem"
|
||||
keywords = ["actix", "http", "framework", "async", "futures"]
|
||||
@ -73,7 +73,7 @@ sha-1 = "0.9"
|
||||
smallvec = "1.6.1"
|
||||
|
||||
# tls
|
||||
actix-tls = { version = "3.0.0-beta.9", default-features = false, optional = true }
|
||||
actix-tls = { version = "3.0.0-rc.1", default-features = false, optional = true }
|
||||
|
||||
# compression
|
||||
brotli2 = { version="0.3.2", optional = true }
|
||||
@ -83,7 +83,7 @@ zstd = { version = "0.9", optional = true }
|
||||
[dev-dependencies]
|
||||
actix-server = "2.0.0-beta.9"
|
||||
actix-http-test = { version = "3.0.0-beta.7", features = ["openssl"] }
|
||||
actix-tls = { version = "3.0.0-beta.9", features = ["openssl"] }
|
||||
actix-tls = { version = "3.0.0-rc.1", features = ["openssl"] }
|
||||
async-stream = "0.3"
|
||||
criterion = { version = "0.3", features = ["html_reports"] }
|
||||
env_logger = "0.9"
|
||||
|
@ -3,11 +3,11 @@
|
||||
> HTTP primitives for the Actix ecosystem.
|
||||
|
||||
[](https://crates.io/crates/actix-http)
|
||||
[](https://docs.rs/actix-http/3.0.0-beta.13)
|
||||
[](https://docs.rs/actix-http/3.0.0-beta.14)
|
||||
[](https://blog.rust-lang.org/2021/05/06/Rust-1.52.0.html)
|
||||

|
||||
<br />
|
||||
[](https://deps.rs/crate/actix-http/3.0.0-beta.13)
|
||||
[](https://deps.rs/crate/actix-http/3.0.0-beta.14)
|
||||
[](https://crates.io/crates/actix-http)
|
||||
[](https://discord.gg/NWpN5mmg3x)
|
||||
|
||||
|
@ -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(
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -103,7 +103,10 @@ mod openssl {
|
||||
use super::*;
|
||||
|
||||
use actix_tls::accept::{
|
||||
openssl::{Acceptor, SslAcceptor, SslError, TlsStream},
|
||||
openssl::{
|
||||
reexports::{Error as SslError, SslAcceptor},
|
||||
Acceptor, TlsStream,
|
||||
},
|
||||
TlsError,
|
||||
};
|
||||
|
||||
@ -164,7 +167,7 @@ mod rustls {
|
||||
|
||||
use actix_service::ServiceFactoryExt as _;
|
||||
use actix_tls::accept::{
|
||||
rustls::{Acceptor, ServerConfig, TlsStream},
|
||||
rustls::{reexports::ServerConfig, Acceptor, TlsStream},
|
||||
TlsError,
|
||||
};
|
||||
|
||||
|
@ -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,
|
||||
|
@ -103,7 +103,10 @@ where
|
||||
mod openssl {
|
||||
use actix_service::ServiceFactoryExt as _;
|
||||
use actix_tls::accept::{
|
||||
openssl::{Acceptor, SslAcceptor, SslError, TlsStream},
|
||||
openssl::{
|
||||
reexports::{Error as SslError, SslAcceptor},
|
||||
Acceptor, TlsStream,
|
||||
},
|
||||
TlsError,
|
||||
};
|
||||
|
||||
@ -151,7 +154,7 @@ mod rustls {
|
||||
|
||||
use actix_service::ServiceFactoryExt as _;
|
||||
use actix_tls::accept::{
|
||||
rustls::{Acceptor, ServerConfig, TlsStream},
|
||||
rustls::{reexports::ServerConfig, Acceptor, TlsStream},
|
||||
TlsError,
|
||||
};
|
||||
|
||||
|
@ -1,11 +1,12 @@
|
||||
//! Helper trait for types that can be effectively borrowed as a [HeaderValue].
|
||||
//!
|
||||
//! [HeaderValue]: crate::http::HeaderValue
|
||||
//! Sealed [`AsHeaderName`] trait and implementations.
|
||||
|
||||
use std::{borrow::Cow, str::FromStr};
|
||||
use std::{borrow::Cow, str::FromStr as _};
|
||||
|
||||
use http::header::{HeaderName, InvalidHeaderName};
|
||||
|
||||
/// Sealed trait implemented for types that can be effectively borrowed as a [`HeaderValue`].
|
||||
///
|
||||
/// [`HeaderValue`]: crate::http::HeaderValue
|
||||
pub trait AsHeaderName: Sealed {}
|
||||
|
||||
pub struct Seal;
|
||||
|
@ -1,4 +1,6 @@
|
||||
use std::convert::TryFrom;
|
||||
//! [`IntoHeaderPair`] trait and implementations.
|
||||
|
||||
use std::convert::TryFrom as _;
|
||||
|
||||
use http::{
|
||||
header::{HeaderName, InvalidHeaderName, InvalidHeaderValue},
|
||||
@ -7,7 +9,10 @@ use http::{
|
||||
|
||||
use super::{Header, IntoHeaderValue};
|
||||
|
||||
/// Transforms structures into header K/V pairs for inserting into `HeaderMap`s.
|
||||
/// An interface for types that can be converted into a [`HeaderName`]/[`HeaderValue`] pair for
|
||||
/// insertion into a [`HeaderMap`].
|
||||
///
|
||||
/// [`HeaderMap`]: crate::http::HeaderMap
|
||||
pub trait IntoHeaderPair: Sized {
|
||||
type Error: Into<HttpError>;
|
||||
|
||||
|
@ -1,10 +1,12 @@
|
||||
use std::convert::TryFrom;
|
||||
//! [`IntoHeaderValue`] trait and implementations.
|
||||
|
||||
use std::convert::TryFrom as _;
|
||||
|
||||
use bytes::Bytes;
|
||||
use http::{header::InvalidHeaderValue, Error as HttpError, HeaderValue};
|
||||
use mime::Mime;
|
||||
|
||||
/// A trait for any object that can be Converted to a `HeaderValue`
|
||||
/// An interface for types that can be converted into a [`HeaderValue`].
|
||||
pub trait IntoHeaderValue: Sized {
|
||||
/// The type returned in the event of a conversion error.
|
||||
type Error: Into<HttpError>;
|
||||
|
@ -1,6 +1,6 @@
|
||||
//! A multi-value [`HeaderMap`] and its iterators.
|
||||
|
||||
use std::{borrow::Cow, collections::hash_map, ops};
|
||||
use std::{borrow::Cow, collections::hash_map, iter, ops};
|
||||
|
||||
use ahash::AHashMap;
|
||||
use http::header::{HeaderName, HeaderValue};
|
||||
@ -288,7 +288,7 @@ impl HeaderMap {
|
||||
/// Returns an iterator over all values associated with a header name.
|
||||
///
|
||||
/// The returned iterator does not incur any allocations and will yield no items if there are no
|
||||
/// values associated with the key. Iteration order is **not** guaranteed to be the same as
|
||||
/// values associated with the key. Iteration order is guaranteed to be the same as
|
||||
/// insertion order.
|
||||
///
|
||||
/// # Examples
|
||||
@ -355,6 +355,19 @@ impl HeaderMap {
|
||||
///
|
||||
/// assert_eq!(map.len(), 1);
|
||||
/// ```
|
||||
///
|
||||
/// A convenience method is provided on the returned iterator to check if the insertion replaced
|
||||
/// any values.
|
||||
/// ```
|
||||
/// # use actix_http::http::{header, HeaderMap, HeaderValue};
|
||||
/// let mut map = HeaderMap::new();
|
||||
///
|
||||
/// let removed = map.insert(header::ACCEPT, HeaderValue::from_static("text/plain"));
|
||||
/// assert!(removed.is_empty());
|
||||
///
|
||||
/// let removed = map.insert(header::ACCEPT, HeaderValue::from_static("text/html"));
|
||||
/// assert!(!removed.is_empty());
|
||||
/// ```
|
||||
pub fn insert(&mut self, key: HeaderName, val: HeaderValue) -> Removed {
|
||||
let value = self.inner.insert(key, Value::one(val));
|
||||
Removed::new(value)
|
||||
@ -393,6 +406,9 @@ impl HeaderMap {
|
||||
|
||||
/// Removes all headers for a particular header name from the map.
|
||||
///
|
||||
/// Providing an invalid header names (as a string argument) will have no effect and return
|
||||
/// without error.
|
||||
///
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// # use actix_http::http::{header, HeaderMap, HeaderValue};
|
||||
@ -409,6 +425,21 @@ impl HeaderMap {
|
||||
/// assert!(removed.next().is_none());
|
||||
///
|
||||
/// assert!(map.is_empty());
|
||||
/// ```
|
||||
///
|
||||
/// A convenience method is provided on the returned iterator to check if the `remove` call
|
||||
/// actually removed any values.
|
||||
/// ```
|
||||
/// # use actix_http::http::{header, HeaderMap, HeaderValue};
|
||||
/// let mut map = HeaderMap::new();
|
||||
///
|
||||
/// let removed = map.remove("accept");
|
||||
/// assert!(removed.is_empty());
|
||||
///
|
||||
/// map.insert(header::ACCEPT, HeaderValue::from_static("text/html"));
|
||||
/// let removed = map.remove("accept");
|
||||
/// assert!(!removed.is_empty());
|
||||
/// ```
|
||||
pub fn remove(&mut self, key: impl AsHeaderName) -> Removed {
|
||||
let value = match key.try_as_name(super::as_name::Seal) {
|
||||
Ok(Cow::Borrowed(name)) => self.inner.remove(name),
|
||||
@ -550,7 +581,8 @@ impl HeaderMap {
|
||||
}
|
||||
}
|
||||
|
||||
/// Note that this implementation will clone a [HeaderName] for each value.
|
||||
/// Note that this implementation will clone a [HeaderName] for each value. Consider using
|
||||
/// [`drain`](Self::drain) to control header name cloning.
|
||||
impl IntoIterator for HeaderMap {
|
||||
type Item = (HeaderName, HeaderValue);
|
||||
type IntoIter = IntoIter;
|
||||
@ -571,7 +603,7 @@ impl<'a> IntoIterator for &'a HeaderMap {
|
||||
}
|
||||
}
|
||||
|
||||
/// Iterator for all values with the same header name.
|
||||
/// Iterator over borrowed values with the same associated name.
|
||||
///
|
||||
/// See [`HeaderMap::get_all`].
|
||||
#[derive(Debug)]
|
||||
@ -613,18 +645,36 @@ impl<'a> Iterator for GetAll<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Iterator for owned [`HeaderValue`]s with the same associated [`HeaderName`] returned from methods
|
||||
/// on [`HeaderMap`] that remove or replace items.
|
||||
impl ExactSizeIterator for GetAll<'_> {}
|
||||
|
||||
impl iter::FusedIterator for GetAll<'_> {}
|
||||
|
||||
/// Iterator over removed, owned values with the same associated name.
|
||||
///
|
||||
/// Returned from methods that remove or replace items. See [`HeaderMap::insert`]
|
||||
/// and [`HeaderMap::remove`].
|
||||
#[derive(Debug)]
|
||||
pub struct Removed {
|
||||
inner: Option<smallvec::IntoIter<[HeaderValue; 4]>>,
|
||||
}
|
||||
|
||||
impl<'a> Removed {
|
||||
impl Removed {
|
||||
fn new(value: Option<Value>) -> Self {
|
||||
let inner = value.map(|value| value.inner.into_iter());
|
||||
Self { inner }
|
||||
}
|
||||
|
||||
/// Returns true if iterator contains no elements, without consuming it.
|
||||
///
|
||||
/// If called immediately after [`HeaderMap::insert`] or [`HeaderMap::remove`], it will indicate
|
||||
/// wether any items were actually replaced or removed, respectively.
|
||||
pub fn is_empty(&self) -> bool {
|
||||
match self.inner {
|
||||
// size hint lower bound of smallvec is the correct length
|
||||
Some(ref iter) => iter.size_hint().0 == 0,
|
||||
None => true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Iterator for Removed {
|
||||
@ -644,7 +694,11 @@ impl Iterator for Removed {
|
||||
}
|
||||
}
|
||||
|
||||
/// Iterator over all [`HeaderName`]s in the map.
|
||||
impl ExactSizeIterator for Removed {}
|
||||
|
||||
impl iter::FusedIterator for Removed {}
|
||||
|
||||
/// Iterator over all names in the map.
|
||||
#[derive(Debug)]
|
||||
pub struct Keys<'a>(hash_map::Keys<'a, HeaderName, Value>);
|
||||
|
||||
@ -662,6 +716,11 @@ impl<'a> Iterator for Keys<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
impl ExactSizeIterator for Keys<'_> {}
|
||||
|
||||
impl iter::FusedIterator for Keys<'_> {}
|
||||
|
||||
/// Iterator over borrowed name-value pairs.
|
||||
#[derive(Debug)]
|
||||
pub struct Iter<'a> {
|
||||
inner: hash_map::Iter<'a, HeaderName, Value>,
|
||||
@ -713,6 +772,10 @@ impl<'a> Iterator for Iter<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
impl ExactSizeIterator for Iter<'_> {}
|
||||
|
||||
impl iter::FusedIterator for Iter<'_> {}
|
||||
|
||||
/// Iterator over drained name-value pairs.
|
||||
///
|
||||
/// Iterator items are `(Option<HeaderName>, HeaderValue)` to avoid cloning.
|
||||
@ -764,6 +827,10 @@ impl<'a> Iterator for Drain<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
impl ExactSizeIterator for Drain<'_> {}
|
||||
|
||||
impl iter::FusedIterator for Drain<'_> {}
|
||||
|
||||
/// Iterator over owned name-value pairs.
|
||||
///
|
||||
/// Implementation necessarily clones header names for each value.
|
||||
@ -814,12 +881,27 @@ impl Iterator for IntoIter {
|
||||
}
|
||||
}
|
||||
|
||||
impl ExactSizeIterator for IntoIter {}
|
||||
|
||||
impl iter::FusedIterator for IntoIter {}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::iter::FusedIterator;
|
||||
|
||||
use http::header;
|
||||
use static_assertions::assert_impl_all;
|
||||
|
||||
use super::*;
|
||||
|
||||
assert_impl_all!(HeaderMap: IntoIterator);
|
||||
assert_impl_all!(Keys<'_>: Iterator, ExactSizeIterator, FusedIterator);
|
||||
assert_impl_all!(GetAll<'_>: Iterator, ExactSizeIterator, FusedIterator);
|
||||
assert_impl_all!(Removed: Iterator, ExactSizeIterator, FusedIterator);
|
||||
assert_impl_all!(Iter<'_>: Iterator, ExactSizeIterator, FusedIterator);
|
||||
assert_impl_all!(IntoIter: Iterator, ExactSizeIterator, FusedIterator);
|
||||
assert_impl_all!(Drain<'_>: Iterator, ExactSizeIterator, FusedIterator);
|
||||
|
||||
#[test]
|
||||
fn create() {
|
||||
let map = HeaderMap::new();
|
||||
@ -945,6 +1027,56 @@ mod tests {
|
||||
assert_eq!(vals.next(), removed.next().as_ref());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_all_iteration_order_matches_insertion_order() {
|
||||
let mut map = HeaderMap::new();
|
||||
|
||||
let mut vals = map.get_all(header::COOKIE);
|
||||
assert!(vals.next().is_none());
|
||||
|
||||
map.append(header::COOKIE, HeaderValue::from_static("1"));
|
||||
let mut vals = map.get_all(header::COOKIE);
|
||||
assert_eq!(vals.next().unwrap().as_bytes(), b"1");
|
||||
assert!(vals.next().is_none());
|
||||
|
||||
map.append(header::COOKIE, HeaderValue::from_static("2"));
|
||||
let mut vals = map.get_all(header::COOKIE);
|
||||
assert_eq!(vals.next().unwrap().as_bytes(), b"1");
|
||||
assert_eq!(vals.next().unwrap().as_bytes(), b"2");
|
||||
assert!(vals.next().is_none());
|
||||
|
||||
map.append(header::COOKIE, HeaderValue::from_static("3"));
|
||||
map.append(header::COOKIE, HeaderValue::from_static("4"));
|
||||
map.append(header::COOKIE, HeaderValue::from_static("5"));
|
||||
let mut vals = map.get_all(header::COOKIE);
|
||||
assert_eq!(vals.next().unwrap().as_bytes(), b"1");
|
||||
assert_eq!(vals.next().unwrap().as_bytes(), b"2");
|
||||
assert_eq!(vals.next().unwrap().as_bytes(), b"3");
|
||||
assert_eq!(vals.next().unwrap().as_bytes(), b"4");
|
||||
assert_eq!(vals.next().unwrap().as_bytes(), b"5");
|
||||
assert!(vals.next().is_none());
|
||||
|
||||
let _ = map.insert(header::COOKIE, HeaderValue::from_static("6"));
|
||||
let mut vals = map.get_all(header::COOKIE);
|
||||
assert_eq!(vals.next().unwrap().as_bytes(), b"6");
|
||||
assert!(vals.next().is_none());
|
||||
|
||||
let _ = map.insert(header::COOKIE, HeaderValue::from_static("7"));
|
||||
let _ = map.insert(header::COOKIE, HeaderValue::from_static("8"));
|
||||
let mut vals = map.get_all(header::COOKIE);
|
||||
assert_eq!(vals.next().unwrap().as_bytes(), b"8");
|
||||
assert!(vals.next().is_none());
|
||||
|
||||
map.append(header::COOKIE, HeaderValue::from_static("9"));
|
||||
let mut vals = map.get_all(header::COOKIE);
|
||||
assert_eq!(vals.next().unwrap().as_bytes(), b"8");
|
||||
assert_eq!(vals.next().unwrap().as_bytes(), b"9");
|
||||
assert!(vals.next().is_none());
|
||||
|
||||
// check for fused-ness
|
||||
assert!(vals.next().is_none());
|
||||
}
|
||||
|
||||
fn owned_pair<'a>(
|
||||
(name, val): (&'a HeaderName, &'a HeaderValue),
|
||||
) -> (HeaderName, HeaderValue) {
|
||||
|
@ -29,16 +29,14 @@ pub use http::header::{
|
||||
X_FRAME_OPTIONS, X_XSS_PROTECTION,
|
||||
};
|
||||
|
||||
use crate::error::ParseError;
|
||||
use crate::HttpMessage;
|
||||
use crate::{error::ParseError, HttpMessage};
|
||||
|
||||
mod as_name;
|
||||
mod into_pair;
|
||||
mod into_value;
|
||||
mod utils;
|
||||
|
||||
pub(crate) mod map;
|
||||
pub mod map;
|
||||
mod shared;
|
||||
mod utils;
|
||||
|
||||
#[doc(hidden)]
|
||||
pub use self::shared::*;
|
||||
@ -46,18 +44,18 @@ pub use self::shared::*;
|
||||
pub use self::as_name::AsHeaderName;
|
||||
pub use self::into_pair::IntoHeaderPair;
|
||||
pub use self::into_value::IntoHeaderValue;
|
||||
#[doc(hidden)]
|
||||
pub use self::map::GetAll;
|
||||
pub use self::map::HeaderMap;
|
||||
pub use self::utils::*;
|
||||
pub use self::utils::{
|
||||
fmt_comma_delimited, from_comma_delimited, from_one_raw_str, http_percent_encode,
|
||||
};
|
||||
|
||||
/// A trait for any object that already represents a valid header field and value.
|
||||
/// An interface for types that already represent a valid header.
|
||||
pub trait Header: IntoHeaderValue {
|
||||
/// Returns the name of the header field
|
||||
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`.
|
||||
@ -68,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'"')
|
||||
|
@ -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
|
||||
@ -88,20 +87,20 @@ impl Charset {
|
||||
Iso_8859_8_E => "ISO-8859-8-E",
|
||||
Iso_8859_8_I => "ISO-8859-8-I",
|
||||
Gb2312 => "GB2312",
|
||||
Big5 => "big5",
|
||||
Big5 => "Big5",
|
||||
Koi8_R => "KOI8-R",
|
||||
Ext(ref s) => s,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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> {
|
||||
@ -128,7 +127,7 @@ impl FromStr for Charset {
|
||||
"ISO-8859-8-E" => Iso_8859_8_E,
|
||||
"ISO-8859-8-I" => Iso_8859_8_I,
|
||||
"GB2312" => Gb2312,
|
||||
"big5" => Big5,
|
||||
"BIG5" => Big5,
|
||||
"KOI8-R" => Koi8_R,
|
||||
s => Ext(s.to_owned()),
|
||||
})
|
||||
|
@ -9,14 +9,17 @@ use crate::{
|
||||
HttpMessage,
|
||||
};
|
||||
|
||||
/// Error return when a content encoding is unknown.
|
||||
///
|
||||
/// Example: 'compress'
|
||||
/// Error returned when a content encoding is unknown.
|
||||
#[derive(Debug, Display, Error)]
|
||||
#[display(fmt = "unsupported content encoding")]
|
||||
pub struct ContentEncodingParseError;
|
||||
|
||||
/// Represents a supported content encoding.
|
||||
///
|
||||
/// Includes a commonly-used subset of media types appropriate for use as HTTP content encodings.
|
||||
/// See [IANA HTTP Content Coding Registry].
|
||||
///
|
||||
/// [IANA HTTP Content Coding Registry]: https://www.iana.org/assignments/http-parameters/http-parameters.xhtml
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
#[non_exhaustive]
|
||||
pub enum ContentEncoding {
|
||||
@ -32,7 +35,7 @@ pub enum ContentEncoding {
|
||||
/// Gzip algorithm.
|
||||
Gzip,
|
||||
|
||||
// Zstd algorithm.
|
||||
/// Zstd algorithm.
|
||||
Zstd,
|
||||
|
||||
/// Indicates the identity function (i.e. no compression, nor modification).
|
||||
|
@ -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> {
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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::*;
|
||||
|
@ -1,3 +1,5 @@
|
||||
//! Header parsing utilities.
|
||||
|
||||
use std::{fmt, str::FromStr};
|
||||
|
||||
use super::HeaderValue;
|
||||
@ -10,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() {
|
||||
@ -22,6 +27,7 @@ where
|
||||
.filter_map(|x| x.trim().parse().ok()),
|
||||
)
|
||||
}
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
@ -30,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)
|
||||
}
|
||||
|
||||
@ -44,19 +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]);
|
||||
}
|
||||
}
|
||||
|
@ -197,7 +197,10 @@ where
|
||||
mod openssl {
|
||||
use actix_service::ServiceFactoryExt as _;
|
||||
use actix_tls::accept::{
|
||||
openssl::{Acceptor, SslAcceptor, SslError, TlsStream},
|
||||
openssl::{
|
||||
reexports::{Error as SslError, SslAcceptor},
|
||||
Acceptor, TlsStream,
|
||||
},
|
||||
TlsError,
|
||||
};
|
||||
|
||||
@ -270,7 +273,7 @@ mod rustls {
|
||||
|
||||
use actix_service::ServiceFactoryExt as _;
|
||||
use actix_tls::accept::{
|
||||
rustls::{Acceptor, ServerConfig, TlsStream},
|
||||
rustls::{reexports::ServerConfig, Acceptor, TlsStream},
|
||||
TlsError,
|
||||
};
|
||||
|
||||
|
@ -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.
|
||||
|
@ -20,7 +20,7 @@ use actix_http::{
|
||||
};
|
||||
use actix_http_test::test_server;
|
||||
use actix_service::{fn_factory_with_config, fn_service};
|
||||
use actix_tls::connect::tls::rustls::webpki_roots_cert_store;
|
||||
use actix_tls::connect::rustls::webpki_roots_cert_store;
|
||||
use actix_utils::future::{err, ok};
|
||||
use bytes::{Bytes, BytesMut};
|
||||
use derive_more::{Display, Error};
|
||||
|
@ -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]
|
||||
|
@ -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"]
|
||||
@ -20,7 +20,6 @@ actix-utils = "3.0.0"
|
||||
bytes = "1"
|
||||
derive_more = "0.99.5"
|
||||
futures-core = { version = "0.3.7", default-features = false, features = ["alloc"] }
|
||||
futures-util = { version = "0.3.7", default-features = false, features = ["alloc"] }
|
||||
httparse = "1.3"
|
||||
local-waker = "0.1"
|
||||
log = "0.4"
|
||||
@ -29,6 +28,7 @@ twoway = "0.2"
|
||||
|
||||
[dev-dependencies]
|
||||
actix-rt = "2.2"
|
||||
actix-http = "3.0.0-beta.13"
|
||||
actix-http = "3.0.0-beta.14"
|
||||
futures-util = { version = "0.3.7", default-features = false, features = ["alloc"] }
|
||||
tokio = { version = "1", features = ["sync"] }
|
||||
tokio-stream = "0.1"
|
||||
|
@ -3,11 +3,11 @@
|
||||
> Multipart form support for Actix Web.
|
||||
|
||||
[](https://crates.io/crates/actix-multipart)
|
||||
[](https://docs.rs/actix-multipart/0.4.0-beta.8)
|
||||
[](https://docs.rs/actix-multipart/0.4.0-beta.9)
|
||||
[](https://blog.rust-lang.org/2021/05/06/Rust-1.52.0.html)
|
||||

|
||||
<br />
|
||||
[](https://deps.rs/crate/actix-multipart/0.4.0-beta.8)
|
||||
[](https://deps.rs/crate/actix-multipart/0.4.0-beta.9)
|
||||
[](https://crates.io/crates/actix-multipart)
|
||||
[](https://discord.gg/NWpN5mmg3x)
|
||||
|
||||
|
@ -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,
|
||||
|
@ -17,7 +17,6 @@ use actix_web::{
|
||||
};
|
||||
use bytes::{Bytes, BytesMut};
|
||||
use futures_core::stream::{LocalBoxStream, Stream};
|
||||
use futures_util::stream::StreamExt as _;
|
||||
use local_waker::LocalWaker;
|
||||
|
||||
use crate::error::MultipartError;
|
||||
@ -33,7 +32,7 @@ const MAX_HEADERS: usize = 32;
|
||||
pub struct Multipart {
|
||||
safety: Safety,
|
||||
error: Option<MultipartError>,
|
||||
inner: Option<Rc<RefCell<InnerMultipart>>>,
|
||||
inner: Option<InnerMultipart>,
|
||||
}
|
||||
|
||||
enum InnerMultipartItem {
|
||||
@ -67,7 +66,7 @@ impl Multipart {
|
||||
/// Create multipart instance for boundary.
|
||||
pub fn new<S>(headers: &HeaderMap, stream: S) -> Multipart
|
||||
where
|
||||
S: Stream<Item = Result<Bytes, PayloadError>> + Unpin + 'static,
|
||||
S: Stream<Item = Result<Bytes, PayloadError>> + 'static,
|
||||
{
|
||||
match Self::boundary(headers) {
|
||||
Ok(boundary) => Multipart::from_boundary(boundary, stream),
|
||||
@ -77,39 +76,32 @@ impl Multipart {
|
||||
|
||||
/// Extract boundary info from headers.
|
||||
pub(crate) fn boundary(headers: &HeaderMap) -> Result<String, MultipartError> {
|
||||
if let Some(content_type) = headers.get(&header::CONTENT_TYPE) {
|
||||
if let Ok(content_type) = content_type.to_str() {
|
||||
if let Ok(ct) = content_type.parse::<mime::Mime>() {
|
||||
if let Some(boundary) = ct.get_param(mime::BOUNDARY) {
|
||||
Ok(boundary.as_str().to_owned())
|
||||
} else {
|
||||
Err(MultipartError::Boundary)
|
||||
}
|
||||
} else {
|
||||
Err(MultipartError::ParseContentType)
|
||||
}
|
||||
} else {
|
||||
Err(MultipartError::ParseContentType)
|
||||
}
|
||||
} else {
|
||||
Err(MultipartError::NoContentType)
|
||||
}
|
||||
headers
|
||||
.get(&header::CONTENT_TYPE)
|
||||
.ok_or(MultipartError::NoContentType)?
|
||||
.to_str()
|
||||
.ok()
|
||||
.and_then(|content_type| content_type.parse::<mime::Mime>().ok())
|
||||
.ok_or(MultipartError::ParseContentType)?
|
||||
.get_param(mime::BOUNDARY)
|
||||
.map(|boundary| boundary.as_str().to_owned())
|
||||
.ok_or(MultipartError::Boundary)
|
||||
}
|
||||
|
||||
/// Create multipart instance for given boundary and stream
|
||||
pub(crate) fn from_boundary<S>(boundary: String, stream: S) -> Multipart
|
||||
where
|
||||
S: Stream<Item = Result<Bytes, PayloadError>> + Unpin + 'static,
|
||||
S: Stream<Item = Result<Bytes, PayloadError>> + 'static,
|
||||
{
|
||||
Multipart {
|
||||
error: None,
|
||||
safety: Safety::new(),
|
||||
inner: Some(Rc::new(RefCell::new(InnerMultipart {
|
||||
inner: Some(InnerMultipart {
|
||||
boundary,
|
||||
payload: PayloadRef::new(PayloadBuffer::new(Box::new(stream))),
|
||||
payload: PayloadRef::new(PayloadBuffer::new(stream)),
|
||||
state: InnerState::FirstBoundary,
|
||||
item: InnerMultipartItem::None,
|
||||
}))),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
@ -126,20 +118,27 @@ impl Multipart {
|
||||
impl Stream for Multipart {
|
||||
type Item = Result<Field, MultipartError>;
|
||||
|
||||
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
||||
if let Some(err) = self.error.take() {
|
||||
Poll::Ready(Some(Err(err)))
|
||||
} else if self.safety.current() {
|
||||
let this = self.get_mut();
|
||||
let mut inner = this.inner.as_mut().unwrap().borrow_mut();
|
||||
if let Some(mut payload) = inner.payload.get_mut(&this.safety) {
|
||||
payload.poll_stream(cx)?;
|
||||
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
||||
let this = self.get_mut();
|
||||
|
||||
match this.inner.as_mut() {
|
||||
Some(inner) => {
|
||||
if let Some(mut buffer) = inner.payload.get_mut(&this.safety) {
|
||||
// check safety and poll read payload to buffer.
|
||||
buffer.poll_stream(cx)?;
|
||||
} else if !this.safety.is_clean() {
|
||||
// safety violation
|
||||
return Poll::Ready(Some(Err(MultipartError::NotConsumed)));
|
||||
} else {
|
||||
return Poll::Pending;
|
||||
}
|
||||
|
||||
inner.poll(&this.safety, cx)
|
||||
}
|
||||
inner.poll(&this.safety, cx)
|
||||
} else if !self.safety.is_clean() {
|
||||
Poll::Ready(Some(Err(MultipartError::NotConsumed)))
|
||||
} else {
|
||||
Poll::Pending
|
||||
None => Poll::Ready(Some(Err(this
|
||||
.error
|
||||
.take()
|
||||
.expect("Multipart polled after finish")))),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -160,17 +159,15 @@ impl InnerMultipart {
|
||||
Ok(httparse::Status::Complete((_, hdrs))) => {
|
||||
// convert headers
|
||||
let mut headers = HeaderMap::with_capacity(hdrs.len());
|
||||
|
||||
for h in hdrs {
|
||||
if let Ok(name) = HeaderName::try_from(h.name) {
|
||||
if let Ok(value) = HeaderValue::try_from(h.value) {
|
||||
headers.append(name, value);
|
||||
} else {
|
||||
return Err(ParseError::Header.into());
|
||||
}
|
||||
} else {
|
||||
return Err(ParseError::Header.into());
|
||||
}
|
||||
let name =
|
||||
HeaderName::try_from(h.name).map_err(|_| ParseError::Header)?;
|
||||
let value = HeaderValue::try_from(h.value)
|
||||
.map_err(|_| ParseError::Header)?;
|
||||
headers.append(name, value);
|
||||
}
|
||||
|
||||
Ok(Some(headers))
|
||||
}
|
||||
Ok(httparse::Status::Partial) => Err(ParseError::Header.into()),
|
||||
@ -340,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)
|
||||
@ -466,17 +463,19 @@ impl Stream for Field {
|
||||
type Item = Result<Bytes, MultipartError>;
|
||||
|
||||
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
||||
if self.safety.current() {
|
||||
let mut inner = self.inner.borrow_mut();
|
||||
if let Some(mut payload) = inner.payload.as_ref().unwrap().get_mut(&self.safety) {
|
||||
payload.poll_stream(cx)?;
|
||||
}
|
||||
inner.poll(&self.safety)
|
||||
} else if !self.safety.is_clean() {
|
||||
Poll::Ready(Some(Err(MultipartError::NotConsumed)))
|
||||
let this = self.get_mut();
|
||||
let mut inner = this.inner.borrow_mut();
|
||||
if let Some(mut buffer) = inner.payload.as_ref().unwrap().get_mut(&this.safety) {
|
||||
// check safety and poll read payload to buffer.
|
||||
buffer.poll_stream(cx)?;
|
||||
} else if !this.safety.is_clean() {
|
||||
// safety violation
|
||||
return Poll::Ready(Some(Err(MultipartError::NotConsumed)));
|
||||
} else {
|
||||
Poll::Pending
|
||||
return Poll::Pending;
|
||||
}
|
||||
|
||||
inner.poll(&this.safety)
|
||||
}
|
||||
}
|
||||
|
||||
@ -690,10 +689,7 @@ impl PayloadRef {
|
||||
}
|
||||
}
|
||||
|
||||
fn get_mut<'a, 'b>(&'a self, s: &'b Safety) -> Option<RefMut<'a, PayloadBuffer>>
|
||||
where
|
||||
'a: 'b,
|
||||
{
|
||||
fn get_mut(&self, s: &Safety) -> Option<RefMut<'_, PayloadBuffer>> {
|
||||
if s.current() {
|
||||
Some(self.payload.borrow_mut())
|
||||
} else {
|
||||
@ -710,8 +706,11 @@ impl Clone for PayloadRef {
|
||||
}
|
||||
}
|
||||
|
||||
/// Counter. It tracks of number of clones of payloads and give access to payload only to top most
|
||||
/// task panics if Safety get destroyed and it not top most task.
|
||||
/// Counter. It tracks of number of clones of payloads and give access to payload only to top most.
|
||||
/// * When dropped, parent task is awakened. This is to support the case where Field is
|
||||
/// dropped in a separate task than Multipart.
|
||||
/// * Assumes that parent owners don't move to different tasks; only the top-most is allowed to.
|
||||
/// * If dropped and is not top most owner, is_clean flag is set to false.
|
||||
#[derive(Debug)]
|
||||
struct Safety {
|
||||
task: LocalWaker,
|
||||
@ -754,9 +753,9 @@ impl Safety {
|
||||
|
||||
impl Drop for Safety {
|
||||
fn drop(&mut self) {
|
||||
// parent task is dead
|
||||
if Rc::strong_count(&self.payload) != self.level {
|
||||
self.clean.set(true);
|
||||
// Multipart dropped leaving a Field
|
||||
self.clean.set(false);
|
||||
}
|
||||
|
||||
self.task.wake();
|
||||
@ -779,7 +778,7 @@ impl PayloadBuffer {
|
||||
PayloadBuffer {
|
||||
eof: false,
|
||||
buf: BytesMut::new(),
|
||||
stream: stream.boxed_local(),
|
||||
stream: Box::pin(stream),
|
||||
}
|
||||
}
|
||||
|
||||
@ -857,10 +856,12 @@ mod tests {
|
||||
|
||||
use actix_http::h1::Payload;
|
||||
use actix_web::http::header::{DispositionParam, DispositionType};
|
||||
use actix_web::rt;
|
||||
use actix_web::test::TestRequest;
|
||||
use actix_web::FromRequest;
|
||||
use bytes::Bytes;
|
||||
use futures_util::future::lazy;
|
||||
use futures_util::{future::lazy, StreamExt};
|
||||
use std::time::Duration;
|
||||
use tokio::sync::mpsc;
|
||||
use tokio_stream::wrappers::UnboundedReceiverStream;
|
||||
|
||||
@ -1290,4 +1291,44 @@ mod tests {
|
||||
MultipartError::NoContentDisposition,
|
||||
));
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_drop_multipart_dont_hang() {
|
||||
let (sender, payload) = create_stream();
|
||||
let (bytes, headers) = create_simple_request_with_header();
|
||||
sender.send(Ok(bytes)).unwrap();
|
||||
drop(sender); // eof
|
||||
|
||||
let mut multipart = Multipart::new(&headers, payload);
|
||||
let mut field = multipart.next().await.unwrap().unwrap();
|
||||
|
||||
drop(multipart);
|
||||
|
||||
// should fail immediately
|
||||
match field.next().await {
|
||||
Some(Err(MultipartError::NotConsumed)) => {}
|
||||
_ => panic!(),
|
||||
};
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_drop_field_awaken_multipart() {
|
||||
let (sender, payload) = create_stream();
|
||||
let (bytes, headers) = create_simple_request_with_header();
|
||||
sender.send(Ok(bytes)).unwrap();
|
||||
drop(sender); // eof
|
||||
|
||||
let mut multipart = Multipart::new(&headers, payload);
|
||||
let mut field = multipart.next().await.unwrap().unwrap();
|
||||
|
||||
let task = rt::spawn(async move {
|
||||
rt::time::sleep(Duration::from_secs(1)).await;
|
||||
assert_eq!(field.next().await.unwrap().unwrap(), "test");
|
||||
drop(field);
|
||||
});
|
||||
|
||||
// dropping field should awaken current task
|
||||
let _ = multipart.next().await.unwrap().unwrap();
|
||||
task.await.unwrap();
|
||||
}
|
||||
}
|
||||
|
@ -1770,6 +1770,12 @@ mod tests {
|
||||
match_methods_agree!(["/v{v}", "/ver/{v}"] => "", "s/v", "/v1", "/v1/xx", "/ver/i3/5", "/ver/1");
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn duplicate_segment_name() {
|
||||
ResourceDef::new("/user/{id}/post/{id}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn invalid_dynamic_segment_delimiter() {
|
||||
|
@ -29,7 +29,7 @@ openssl = ["tls-openssl", "actix-http/openssl", "awc/openssl"]
|
||||
|
||||
[dependencies]
|
||||
actix-codec = "0.4.1"
|
||||
actix-http = "3.0.0-beta.13"
|
||||
actix-http = "3.0.0-beta.14"
|
||||
actix-http-test = "3.0.0-beta.7"
|
||||
actix-service = "2.0.0"
|
||||
actix-utils = "3.0.0"
|
||||
|
@ -16,7 +16,7 @@ path = "src/lib.rs"
|
||||
[dependencies]
|
||||
actix = { version = "0.12.0", default-features = false }
|
||||
actix-codec = "0.4.1"
|
||||
actix-http = "3.0.0-beta.13"
|
||||
actix-http = "3.0.0-beta.14"
|
||||
actix-web = { version = "4.0.0-beta.11", default-features = false }
|
||||
|
||||
bytes = "1"
|
||||
|
@ -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])]
|
||||
```
|
||||
|
||||
|
@ -3,7 +3,14 @@
|
||||
## Unreleased - 2021-xx-xx
|
||||
|
||||
|
||||
## 3.0.0-beta.12 - 2021-11-30
|
||||
* Update `actix-tls` to `3.0.0-rc.1`. [#2474]
|
||||
|
||||
[#2474]: https://github.com/actix/actix-web/pull/2474
|
||||
|
||||
|
||||
## 3.0.0-beta.11 - 2021-11-22
|
||||
* No significant changes from `3.0.0-beta.10`.
|
||||
|
||||
|
||||
## 3.0.0-beta.10 - 2021-11-15
|
||||
|
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "awc"
|
||||
version = "3.0.0-beta.11"
|
||||
version = "3.0.0-beta.12"
|
||||
authors = [
|
||||
"Nikolay Kim <fafhrd91@gmail.com>",
|
||||
"fakeshadow <24548779@qq.com>",
|
||||
@ -60,9 +60,9 @@ dangerous-h2c = []
|
||||
[dependencies]
|
||||
actix-codec = "0.4.1"
|
||||
actix-service = "2.0.0"
|
||||
actix-http = "3.0.0-beta.13"
|
||||
actix-http = "3.0.0-beta.14"
|
||||
actix-rt = { version = "2.1", default-features = false }
|
||||
actix-tls = { version = "3.0.0-beta.9", features = ["connect"] }
|
||||
actix-tls = { version = "3.0.0-rc.1", features = ["connect", "uri"] }
|
||||
actix-utils = "3.0.0"
|
||||
|
||||
ahash = "0.7"
|
||||
@ -94,11 +94,11 @@ trust-dns-resolver = { version = "0.20.0", optional = true }
|
||||
|
||||
[dev-dependencies]
|
||||
actix-web = { version = "4.0.0-beta.11", features = ["openssl"] }
|
||||
actix-http = { version = "3.0.0-beta.13", features = ["openssl"] }
|
||||
actix-http = { version = "3.0.0-beta.14", features = ["openssl"] }
|
||||
actix-http-test = { version = "3.0.0-beta.7", features = ["openssl"] }
|
||||
actix-utils = "3.0.0"
|
||||
actix-server = "2.0.0-beta.9"
|
||||
actix-tls = { version = "3.0.0-beta.9", features = ["openssl", "rustls"] }
|
||||
actix-tls = { version = "3.0.0-rc.1", features = ["openssl", "rustls"] }
|
||||
actix-test = { version = "0.1.0-beta.7", features = ["openssl", "rustls"] }
|
||||
|
||||
brotli2 = "0.3.2"
|
||||
|
@ -3,9 +3,9 @@
|
||||
> Async HTTP and WebSocket client library.
|
||||
|
||||
[](https://crates.io/crates/awc)
|
||||
[](https://docs.rs/awc/3.0.0-beta.11)
|
||||
[](https://docs.rs/awc/3.0.0-beta.12)
|
||||

|
||||
[](https://deps.rs/crate/awc/3.0.0-beta.11)
|
||||
[](https://deps.rs/crate/awc/3.0.0-beta.12)
|
||||
[](https://discord.gg/NWpN5mmg3x)
|
||||
|
||||
## Documentation & Resources
|
||||
|
@ -1,18 +1,16 @@
|
||||
use std::convert::TryFrom;
|
||||
use std::fmt;
|
||||
use std::net::IpAddr;
|
||||
use std::rc::Rc;
|
||||
use std::time::Duration;
|
||||
use std::{convert::TryFrom, fmt, net::IpAddr, rc::Rc, time::Duration};
|
||||
|
||||
use actix_http::http::{self, header, Error as HttpError, HeaderMap, HeaderName, Uri};
|
||||
use actix_rt::net::{ActixStream, TcpStream};
|
||||
use actix_service::{boxed, Service};
|
||||
|
||||
use crate::client::{Connector, ConnectorService, TcpConnect, TcpConnectError, TcpConnection};
|
||||
use crate::connect::DefaultConnector;
|
||||
use crate::error::SendRequestError;
|
||||
use crate::middleware::{NestTransform, Redirect, Transform};
|
||||
use crate::{Client, ClientConfig, ConnectRequest, ConnectResponse};
|
||||
use crate::{
|
||||
client::{ConnectInfo, Connector, ConnectorService, TcpConnectError, TcpConnection},
|
||||
connect::DefaultConnector,
|
||||
error::SendRequestError,
|
||||
middleware::{NestTransform, Redirect, Transform},
|
||||
Client, ClientConfig, ConnectRequest, ConnectResponse,
|
||||
};
|
||||
|
||||
/// An HTTP Client builder
|
||||
///
|
||||
@ -35,7 +33,7 @@ impl ClientBuilder {
|
||||
#[allow(clippy::new_ret_no_self)]
|
||||
pub fn new() -> ClientBuilder<
|
||||
impl Service<
|
||||
TcpConnect<Uri>,
|
||||
ConnectInfo<Uri>,
|
||||
Response = TcpConnection<Uri, TcpStream>,
|
||||
Error = TcpConnectError,
|
||||
> + Clone,
|
||||
@ -58,7 +56,7 @@ impl ClientBuilder {
|
||||
|
||||
impl<S, Io, M> ClientBuilder<S, M>
|
||||
where
|
||||
S: Service<TcpConnect<Uri>, Response = TcpConnection<Uri, Io>, Error = TcpConnectError>
|
||||
S: Service<ConnectInfo<Uri>, Response = TcpConnection<Uri, Io>, Error = TcpConnectError>
|
||||
+ Clone
|
||||
+ 'static,
|
||||
Io: ActixStream + fmt::Debug + 'static,
|
||||
@ -67,7 +65,7 @@ where
|
||||
pub fn connector<S1, Io1>(self, connector: Connector<S1>) -> ClientBuilder<S1, M>
|
||||
where
|
||||
S1: Service<
|
||||
TcpConnect<Uri>,
|
||||
ConnectInfo<Uri>,
|
||||
Response = TcpConnection<Uri, Io1>,
|
||||
Error = TcpConnectError,
|
||||
> + Clone
|
||||
|
@ -15,8 +15,8 @@ use actix_rt::{
|
||||
};
|
||||
use actix_service::Service;
|
||||
use actix_tls::connect::{
|
||||
new_connector, Connect as TcpConnect, ConnectError as TcpConnectError,
|
||||
Connection as TcpConnection, Resolver,
|
||||
ConnectError as TcpConnectError, ConnectInfo, Connection as TcpConnection,
|
||||
Connector as TcpConnector, Resolver,
|
||||
};
|
||||
use futures_core::{future::LocalBoxFuture, ready};
|
||||
use http::Uri;
|
||||
@ -28,13 +28,15 @@ use super::error::ConnectError;
|
||||
use super::pool::ConnectionPool;
|
||||
use super::Connect;
|
||||
|
||||
enum SslConnector {
|
||||
#[allow(dead_code)]
|
||||
enum OurTlsConnector {
|
||||
#[allow(dead_code)] // only dead when no TLS feature is enabled
|
||||
None,
|
||||
|
||||
#[cfg(feature = "openssl")]
|
||||
Openssl(actix_tls::connect::ssl::openssl::SslConnector),
|
||||
Openssl(actix_tls::connect::openssl::reexports::SslConnector),
|
||||
|
||||
#[cfg(feature = "rustls")]
|
||||
Rustls(std::sync::Arc<actix_tls::connect::ssl::rustls::ClientConfig>),
|
||||
Rustls(std::sync::Arc<actix_tls::connect::rustls::reexports::ClientConfig>),
|
||||
}
|
||||
|
||||
/// Manages HTTP client network connectivity.
|
||||
@ -53,21 +55,22 @@ enum SslConnector {
|
||||
pub struct Connector<T> {
|
||||
connector: T,
|
||||
config: ConnectorConfig,
|
||||
#[allow(dead_code)]
|
||||
ssl: SslConnector,
|
||||
|
||||
#[allow(dead_code)] // only dead when no TLS feature is enabled
|
||||
ssl: OurTlsConnector,
|
||||
}
|
||||
|
||||
impl Connector<()> {
|
||||
#[allow(clippy::new_ret_no_self, clippy::let_unit_value)]
|
||||
pub fn new() -> Connector<
|
||||
impl Service<
|
||||
TcpConnect<Uri>,
|
||||
ConnectInfo<Uri>,
|
||||
Response = TcpConnection<Uri, TcpStream>,
|
||||
Error = actix_tls::connect::ConnectError,
|
||||
> + Clone,
|
||||
> {
|
||||
Connector {
|
||||
connector: new_connector(resolver::resolver()),
|
||||
connector: TcpConnector::new(resolver::resolver()).service(),
|
||||
config: ConnectorConfig::default(),
|
||||
ssl: Self::build_ssl(vec![b"h2".to_vec(), b"http/1.1".to_vec()]),
|
||||
}
|
||||
@ -75,16 +78,16 @@ impl Connector<()> {
|
||||
|
||||
/// Provides an empty TLS connector when no TLS feature is enabled.
|
||||
#[cfg(not(any(feature = "openssl", feature = "rustls")))]
|
||||
fn build_ssl(_: Vec<Vec<u8>>) -> SslConnector {
|
||||
SslConnector::None
|
||||
fn build_ssl(_: Vec<Vec<u8>>) -> OurTlsConnector {
|
||||
OurTlsConnector::None
|
||||
}
|
||||
|
||||
/// Build TLS connector with rustls, based on supplied ALPN protocols
|
||||
///
|
||||
/// Note that if both `openssl` and `rustls` features are enabled, rustls will be used.
|
||||
#[cfg(feature = "rustls")]
|
||||
fn build_ssl(protocols: Vec<Vec<u8>>) -> SslConnector {
|
||||
use actix_tls::connect::tls::rustls::{webpki_roots_cert_store, ClientConfig};
|
||||
fn build_ssl(protocols: Vec<Vec<u8>>) -> OurTlsConnector {
|
||||
use actix_tls::connect::rustls::{reexports::ClientConfig, webpki_roots_cert_store};
|
||||
|
||||
let mut config = ClientConfig::builder()
|
||||
.with_safe_defaults()
|
||||
@ -93,13 +96,13 @@ impl Connector<()> {
|
||||
|
||||
config.alpn_protocols = protocols;
|
||||
|
||||
SslConnector::Rustls(std::sync::Arc::new(config))
|
||||
OurTlsConnector::Rustls(std::sync::Arc::new(config))
|
||||
}
|
||||
|
||||
/// Build TLS connector with openssl, based on supplied ALPN protocols
|
||||
#[cfg(all(feature = "openssl", not(feature = "rustls")))]
|
||||
fn build_ssl(protocols: Vec<Vec<u8>>) -> SslConnector {
|
||||
use actix_tls::connect::tls::openssl::{SslConnector as OpensslConnector, SslMethod};
|
||||
fn build_ssl(protocols: Vec<Vec<u8>>) -> OurTlsConnector {
|
||||
use actix_tls::connect::openssl::reexports::{SslConnector, SslMethod};
|
||||
use bytes::{BufMut, BytesMut};
|
||||
|
||||
let mut alpn = BytesMut::with_capacity(20);
|
||||
@ -108,12 +111,12 @@ impl Connector<()> {
|
||||
alpn.put(proto.as_slice());
|
||||
}
|
||||
|
||||
let mut ssl = OpensslConnector::builder(SslMethod::tls()).unwrap();
|
||||
let mut ssl = SslConnector::builder(SslMethod::tls()).unwrap();
|
||||
if let Err(err) = ssl.set_alpn_protos(&alpn) {
|
||||
log::error!("Can not set ALPN protocol: {:?}", err);
|
||||
}
|
||||
|
||||
SslConnector::Openssl(ssl.build())
|
||||
OurTlsConnector::Openssl(ssl.build())
|
||||
}
|
||||
}
|
||||
|
||||
@ -123,7 +126,7 @@ impl<S> Connector<S> {
|
||||
where
|
||||
Io1: ActixStream + fmt::Debug + 'static,
|
||||
S1: Service<
|
||||
TcpConnect<Uri>,
|
||||
ConnectInfo<Uri>,
|
||||
Response = TcpConnection<Uri, Io1>,
|
||||
Error = TcpConnectError,
|
||||
> + Clone,
|
||||
@ -136,7 +139,7 @@ impl<S> Connector<S> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<S, Io> Connector<S>
|
||||
impl<S, IO> Connector<S>
|
||||
where
|
||||
// Note:
|
||||
// Input Io type is bound to ActixStream trait but internally in client module they
|
||||
@ -145,8 +148,8 @@ where
|
||||
//
|
||||
// This remap is to hide ActixStream's trait methods. They are not meant to be called
|
||||
// from user code.
|
||||
Io: ActixStream + fmt::Debug + 'static,
|
||||
S: Service<TcpConnect<Uri>, Response = TcpConnection<Uri, Io>, Error = TcpConnectError>
|
||||
IO: ActixStream + fmt::Debug + 'static,
|
||||
S: Service<ConnectInfo<Uri>, Response = TcpConnection<Uri, IO>, Error = TcpConnectError>
|
||||
+ Clone
|
||||
+ 'static,
|
||||
{
|
||||
@ -166,18 +169,21 @@ where
|
||||
|
||||
#[cfg(feature = "openssl")]
|
||||
/// Use custom `SslConnector` instance.
|
||||
pub fn ssl(mut self, connector: actix_tls::connect::ssl::openssl::SslConnector) -> Self {
|
||||
self.ssl = SslConnector::Openssl(connector);
|
||||
pub fn ssl(
|
||||
mut self,
|
||||
connector: actix_tls::connect::openssl::reexports::SslConnector,
|
||||
) -> Self {
|
||||
self.ssl = OurTlsConnector::Openssl(connector);
|
||||
self
|
||||
}
|
||||
|
||||
#[cfg(feature = "rustls")]
|
||||
/// Use custom `SslConnector` instance.
|
||||
/// Use custom `ClientConfig` instance.
|
||||
pub fn rustls(
|
||||
mut self,
|
||||
connector: std::sync::Arc<actix_tls::connect::ssl::rustls::ClientConfig>,
|
||||
connector: std::sync::Arc<actix_tls::connect::rustls::reexports::ClientConfig>,
|
||||
) -> Self {
|
||||
self.ssl = SslConnector::Rustls(connector);
|
||||
self.ssl = OurTlsConnector::Rustls(connector);
|
||||
self
|
||||
}
|
||||
|
||||
@ -266,7 +272,7 @@ where
|
||||
/// Finish configuration process and create connector service.
|
||||
/// The Connector builder always concludes by calling `finish()` last in
|
||||
/// its combinator chain.
|
||||
pub fn finish(self) -> ConnectorService<S, Io> {
|
||||
pub fn finish(self) -> ConnectorService<S, IO> {
|
||||
let local_address = self.config.local_address;
|
||||
let timeout = self.config.timeout;
|
||||
|
||||
@ -279,19 +285,18 @@ where
|
||||
};
|
||||
|
||||
let tls_service = match self.ssl {
|
||||
SslConnector::None => {
|
||||
OurTlsConnector::None => {
|
||||
#[cfg(not(feature = "dangerous-h2c"))]
|
||||
{
|
||||
None
|
||||
}
|
||||
|
||||
#[cfg(feature = "dangerous-h2c")]
|
||||
{
|
||||
use std::{
|
||||
future::{ready, Ready},
|
||||
io,
|
||||
};
|
||||
use std::io;
|
||||
|
||||
use actix_tls::connect::Connection;
|
||||
use actix_utils::future::{ready, Ready};
|
||||
|
||||
impl IntoConnectionIo for TcpConnection<Uri, Box<dyn ConnectionIo>> {
|
||||
fn into_connection_io(self) -> (Box<dyn ConnectionIo>, Protocol) {
|
||||
@ -307,17 +312,17 @@ where
|
||||
#[derive(Clone)]
|
||||
struct NoOpTlsConnectorService;
|
||||
|
||||
impl<T, U> Service<Connection<T, U>> for NoOpTlsConnectorService
|
||||
impl<R, IO> Service<Connection<R, IO>> for NoOpTlsConnectorService
|
||||
where
|
||||
U: ActixStream + 'static,
|
||||
IO: ActixStream + 'static,
|
||||
{
|
||||
type Response = Connection<T, Box<dyn ConnectionIo>>;
|
||||
type Response = Connection<R, Box<dyn ConnectionIo>>;
|
||||
type Error = io::Error;
|
||||
type Future = Ready<Result<Self::Response, Self::Error>>;
|
||||
|
||||
actix_service::always_ready!();
|
||||
|
||||
fn call(&self, connection: Connection<T, U>) -> Self::Future {
|
||||
fn call(&self, connection: Connection<R, IO>) -> Self::Future {
|
||||
let (io, connection) = connection.replace_io(());
|
||||
let (_, connection) = connection.replace_io(Box::new(io) as _);
|
||||
|
||||
@ -336,13 +341,14 @@ where
|
||||
Some(actix_service::boxed::rc_service(tls_service))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "openssl")]
|
||||
SslConnector::Openssl(tls) => {
|
||||
OurTlsConnector::Openssl(tls) => {
|
||||
const H2: &[u8] = b"h2";
|
||||
|
||||
use actix_tls::connect::ssl::openssl::{OpensslConnector, SslStream};
|
||||
use actix_tls::connect::openssl::{reexports::AsyncSslStream, TlsConnector};
|
||||
|
||||
impl<Io: ConnectionIo> IntoConnectionIo for TcpConnection<Uri, SslStream<Io>> {
|
||||
impl<IO: ConnectionIo> IntoConnectionIo for TcpConnection<Uri, AsyncSslStream<IO>> {
|
||||
fn into_connection_io(self) -> (Box<dyn ConnectionIo>, Protocol) {
|
||||
let sock = self.into_parts().0;
|
||||
let h2 = sock
|
||||
@ -361,19 +367,20 @@ where
|
||||
|
||||
let tls_service = TlsConnectorService {
|
||||
tcp_service: tcp_service_inner,
|
||||
tls_service: OpensslConnector::service(tls),
|
||||
tls_service: TlsConnector::service(tls),
|
||||
timeout: handshake_timeout,
|
||||
};
|
||||
|
||||
Some(actix_service::boxed::rc_service(tls_service))
|
||||
}
|
||||
|
||||
#[cfg(feature = "rustls")]
|
||||
SslConnector::Rustls(tls) => {
|
||||
OurTlsConnector::Rustls(tls) => {
|
||||
const H2: &[u8] = b"h2";
|
||||
|
||||
use actix_tls::connect::ssl::rustls::{RustlsConnector, TlsStream};
|
||||
use actix_tls::connect::rustls::{reexports::AsyncTlsStream, TlsConnector};
|
||||
|
||||
impl<Io: ConnectionIo> IntoConnectionIo for TcpConnection<Uri, TlsStream<Io>> {
|
||||
impl<Io: ConnectionIo> IntoConnectionIo for TcpConnection<Uri, AsyncTlsStream<Io>> {
|
||||
fn into_connection_io(self) -> (Box<dyn ConnectionIo>, Protocol) {
|
||||
let sock = self.into_parts().0;
|
||||
let h2 = sock
|
||||
@ -393,7 +400,7 @@ where
|
||||
|
||||
let tls_service = TlsConnectorService {
|
||||
tcp_service: tcp_service_inner,
|
||||
tls_service: RustlsConnector::service(tls),
|
||||
tls_service: TlsConnector::service(tls),
|
||||
timeout: handshake_timeout,
|
||||
};
|
||||
|
||||
@ -462,26 +469,28 @@ where
|
||||
|
||||
/// service for establish tcp connection and do client tls handshake.
|
||||
/// operation is canceled when timeout limit reached.
|
||||
struct TlsConnectorService<S, St> {
|
||||
/// tcp connection is canceled on `TcpConnectorInnerService`'s timeout setting.
|
||||
tcp_service: S,
|
||||
/// tls connection is canceled on `TlsConnectorService`'s timeout setting.
|
||||
tls_service: St,
|
||||
struct TlsConnectorService<Tcp, Tls> {
|
||||
/// TCP connection is canceled on `TcpConnectorInnerService`'s timeout setting.
|
||||
tcp_service: Tcp,
|
||||
|
||||
/// TLS connection is canceled on `TlsConnectorService`'s timeout setting.
|
||||
tls_service: Tls,
|
||||
|
||||
timeout: Duration,
|
||||
}
|
||||
|
||||
impl<S, St, Io> Service<Connect> for TlsConnectorService<S, St>
|
||||
impl<Tcp, Tls, IO> Service<Connect> for TlsConnectorService<Tcp, Tls>
|
||||
where
|
||||
S: Service<Connect, Response = TcpConnection<Uri, Io>, Error = ConnectError>
|
||||
Tcp: Service<Connect, Response = TcpConnection<Uri, IO>, Error = ConnectError>
|
||||
+ Clone
|
||||
+ 'static,
|
||||
St: Service<TcpConnection<Uri, Io>, Error = std::io::Error> + Clone + 'static,
|
||||
Io: ConnectionIo,
|
||||
St::Response: IntoConnectionIo,
|
||||
Tls: Service<TcpConnection<Uri, IO>, Error = std::io::Error> + Clone + 'static,
|
||||
Tls::Response: IntoConnectionIo,
|
||||
IO: ConnectionIo,
|
||||
{
|
||||
type Response = (Box<dyn ConnectionIo>, Protocol);
|
||||
type Error = ConnectError;
|
||||
type Future = TlsConnectorFuture<St, S::Future, St::Future>;
|
||||
type Future = TlsConnectorFuture<Tls, Tcp::Future, Tls::Future>;
|
||||
|
||||
fn poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||
ready!(self.tcp_service.poll_ready(cx))?;
|
||||
@ -581,7 +590,7 @@ impl<S: Clone> TcpConnectorInnerService<S> {
|
||||
|
||||
impl<S, Io> Service<Connect> for TcpConnectorInnerService<S>
|
||||
where
|
||||
S: Service<TcpConnect<Uri>, Response = TcpConnection<Uri, Io>, Error = TcpConnectError>
|
||||
S: Service<ConnectInfo<Uri>, Response = TcpConnection<Uri, Io>, Error = TcpConnectError>
|
||||
+ Clone
|
||||
+ 'static,
|
||||
{
|
||||
@ -592,7 +601,7 @@ where
|
||||
actix_service::forward_ready!(service);
|
||||
|
||||
fn call(&self, req: Connect) -> Self::Future {
|
||||
let mut req = TcpConnect::new(req.uri).set_addr(req.addr);
|
||||
let mut req = ConnectInfo::new(req.uri).set_addr(req.addr);
|
||||
|
||||
if let Some(local_addr) = self.local_address {
|
||||
req = req.set_local_addr(local_addr);
|
||||
@ -631,8 +640,8 @@ where
|
||||
}
|
||||
|
||||
/// Connector service for pooled Plain/Tls Tcp connections.
|
||||
pub type ConnectorService<S, Io> = ConnectorServicePriv<
|
||||
TcpConnectorService<TcpConnectorInnerService<S>>,
|
||||
pub type ConnectorService<Svc, IO> = ConnectorServicePriv<
|
||||
TcpConnectorService<TcpConnectorInnerService<Svc>>,
|
||||
Rc<
|
||||
dyn Service<
|
||||
Connect,
|
||||
@ -644,7 +653,7 @@ pub type ConnectorService<S, Io> = ConnectorServicePriv<
|
||||
>,
|
||||
>,
|
||||
>,
|
||||
Io,
|
||||
IO,
|
||||
Box<dyn ConnectionIo>,
|
||||
>;
|
||||
|
||||
@ -743,7 +752,7 @@ mod resolver {
|
||||
use super::*;
|
||||
|
||||
pub(super) fn resolver() -> Resolver {
|
||||
Resolver::Default
|
||||
Resolver::default()
|
||||
}
|
||||
}
|
||||
|
||||
@ -785,8 +794,7 @@ mod resolver {
|
||||
}
|
||||
}
|
||||
|
||||
// dns struct is cached in thread local.
|
||||
// so new client constructor can reuse the existing dns resolver.
|
||||
// resolver struct is cached in thread local so new clients can reuse the existing instance
|
||||
thread_local! {
|
||||
static TRUST_DNS_RESOLVER: RefCell<Option<Resolver>> = RefCell::new(None);
|
||||
}
|
||||
@ -794,8 +802,10 @@ mod resolver {
|
||||
// get from thread local or construct a new trust-dns resolver.
|
||||
TRUST_DNS_RESOLVER.with(|local| {
|
||||
let resolver = local.borrow().as_ref().map(Clone::clone);
|
||||
|
||||
match resolver {
|
||||
Some(resolver) => resolver,
|
||||
|
||||
None => {
|
||||
let (cfg, opts) = match read_system_conf() {
|
||||
Ok((cfg, opts)) => (cfg, opts),
|
||||
@ -808,8 +818,9 @@ mod resolver {
|
||||
let resolver = TokioAsyncResolver::tokio(cfg, opts).unwrap();
|
||||
|
||||
// box trust dns resolver and put it in thread local.
|
||||
let resolver = Resolver::new_custom(TrustDnsResolver(resolver));
|
||||
let resolver = Resolver::custom(TrustDnsResolver(resolver));
|
||||
*local.borrow_mut() = Some(resolver.clone());
|
||||
|
||||
resolver
|
||||
}
|
||||
}
|
||||
@ -840,9 +851,9 @@ mod tests {
|
||||
.await;
|
||||
|
||||
let connector = Connector {
|
||||
connector: new_connector(resolver::resolver()),
|
||||
connector: TcpConnector::new(resolver::resolver()).service(),
|
||||
config: ConnectorConfig::default(),
|
||||
ssl: SslConnector::None,
|
||||
ssl: OurTlsConnector::None,
|
||||
};
|
||||
|
||||
let client = Client::builder().connector(connector).finish();
|
||||
|
@ -7,7 +7,7 @@ use actix_http::{
|
||||
http::Error as HttpError,
|
||||
};
|
||||
#[cfg(feature = "openssl")]
|
||||
use actix_tls::accept::openssl::SslError;
|
||||
use actix_tls::accept::openssl::reexports::Error as OpenSslError;
|
||||
|
||||
/// A set of errors that can occur while connecting to an HTTP host
|
||||
#[derive(Debug, Display, From)]
|
||||
@ -20,7 +20,7 @@ pub enum ConnectError {
|
||||
/// SSL error
|
||||
#[cfg(feature = "openssl")]
|
||||
#[display(fmt = "{}", _0)]
|
||||
SslError(SslError),
|
||||
SslError(OpenSslError),
|
||||
|
||||
/// Failed to resolve the hostname
|
||||
#[display(fmt = "Failed resolving hostname: {}", _0)]
|
||||
|
@ -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) => {
|
||||
|
@ -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,
|
||||
|
@ -11,7 +11,7 @@ mod h2proto;
|
||||
mod pool;
|
||||
|
||||
pub use actix_tls::connect::{
|
||||
Connect as TcpConnect, ConnectError as TcpConnectError, Connection as TcpConnection,
|
||||
ConnectError as TcpConnectError, ConnectInfo, Connection as TcpConnection,
|
||||
};
|
||||
|
||||
pub use self::connection::{Connection, ConnectionIo};
|
||||
|
@ -137,7 +137,7 @@ use actix_http::{
|
||||
use actix_rt::net::TcpStream;
|
||||
use actix_service::Service;
|
||||
|
||||
use self::client::{TcpConnect, TcpConnectError, TcpConnection};
|
||||
use self::client::{ConnectInfo, TcpConnectError, TcpConnection};
|
||||
|
||||
/// An asynchronous HTTP and WebSocket client.
|
||||
///
|
||||
@ -186,7 +186,7 @@ impl Client {
|
||||
/// This function is equivalent of `ClientBuilder::new()`.
|
||||
pub fn builder() -> ClientBuilder<
|
||||
impl Service<
|
||||
TcpConnect<Uri>,
|
||||
ConnectInfo<Uri>,
|
||||
Response = TcpConnection<Uri, TcpStream>,
|
||||
Error = TcpConnectError,
|
||||
> + Clone,
|
||||
|
@ -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);
|
||||
|
||||
|
@ -127,7 +127,7 @@ async fn test_timeout() {
|
||||
});
|
||||
|
||||
let connector = awc::Connector::new()
|
||||
.connector(actix_tls::connect::default_connector())
|
||||
.connector(actix_tls::connect::ConnectorService::default())
|
||||
.timeout(Duration::from_secs(15));
|
||||
|
||||
let client = awc::Client::builder()
|
||||
|
@ -14,7 +14,7 @@ use std::{
|
||||
use actix_http::HttpService;
|
||||
use actix_http_test::test_server;
|
||||
use actix_service::{fn_service, map_config, ServiceFactoryExt};
|
||||
use actix_tls::connect::tls::rustls::webpki_roots_cert_store;
|
||||
use actix_tls::connect::rustls::webpki_roots_cert_store;
|
||||
use actix_utils::future::ok;
|
||||
use actix_web::{dev::AppConfig, http::Version, web, App, HttpResponse};
|
||||
use rustls::{
|
||||
|
111
scripts/bump
Executable file
111
scripts/bump
Executable file
@ -0,0 +1,111 @@
|
||||
#!/bin/sh
|
||||
|
||||
# developed on macOS and probably doesn't work on Linux yet due to minor
|
||||
# differences in flags on sed
|
||||
|
||||
# requires github cli tool for automatic release draft creation
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
DIR=$1
|
||||
|
||||
LINUX=""
|
||||
MACOS=""
|
||||
|
||||
if [ "$(uname)" = "Darwin" ]; then
|
||||
MACOS="1"
|
||||
fi
|
||||
|
||||
CARGO_MANIFEST=$DIR/Cargo.toml
|
||||
CHANGELOG_FILE=$DIR/CHANGES.md
|
||||
README_FILE=$DIR/README.md
|
||||
|
||||
# get current version
|
||||
PACKAGE_NAME="$(sed -nE 's/^name ?= ?"([^"]+)"$/\1/ p' "$CARGO_MANIFEST" | head -n 1)"
|
||||
CURRENT_VERSION="$(sed -nE 's/^version ?= ?"([^"]+)"$/\1/ p' "$CARGO_MANIFEST")"
|
||||
|
||||
CHANGE_CHUNK_FILE="$(mktemp)"
|
||||
echo saving changelog to $CHANGE_CHUNK_FILE
|
||||
echo
|
||||
|
||||
# get changelog chunk and save to temp file
|
||||
cat "$CHANGELOG_FILE" |
|
||||
# skip up to unreleased heading
|
||||
sed '1,/Unreleased/ d' |
|
||||
# take up to previous version heading
|
||||
sed "/$CURRENT_VERSION/ q" |
|
||||
# drop last line
|
||||
sed '$d' \
|
||||
>"$CHANGE_CHUNK_FILE"
|
||||
|
||||
# if word count of changelog chunk is 0 then insert filler changelog chunk
|
||||
if [ "$(wc -w "$CHANGE_CHUNK_FILE" | awk '{ print $1 }')" = "0" ]; then
|
||||
echo "* No significant changes since \`$CURRENT_VERSION\`." >"$CHANGE_CHUNK_FILE"
|
||||
fi
|
||||
|
||||
if [ -n "${2-}" ]; then
|
||||
NEW_VERSION="$2"
|
||||
else
|
||||
echo
|
||||
echo "--- Changes since $CURRENT_VERSION ----"
|
||||
cat "$CHANGE_CHUNK_FILE"
|
||||
echo
|
||||
read -p "Update version to: " NEW_VERSION
|
||||
fi
|
||||
|
||||
DATE="$(date -u +"%Y-%m-%d")"
|
||||
echo "updating from $CURRENT_VERSION => $NEW_VERSION ($DATE)"
|
||||
|
||||
# update package.version field
|
||||
sed -i.bak -E "s/^version ?= ?\"[^\"]+\"$/version = \"$NEW_VERSION\"/" "$CARGO_MANIFEST"
|
||||
|
||||
# update readme
|
||||
[ -f "$README_FILE" ] && sed -i.bak -E "s#$CURRENT_VERSION([/)])#$NEW_VERSION\1#g" "$README_FILE"
|
||||
|
||||
# update changelog file
|
||||
(
|
||||
sed '/Unreleased/ q' "$CHANGELOG_FILE" # up to unreleased heading
|
||||
echo # blank line
|
||||
echo # blank line
|
||||
echo "## $NEW_VERSION - $DATE" # new version heading
|
||||
cat "$CHANGE_CHUNK_FILE" # previously unreleased changes
|
||||
sed "/$CURRENT_VERSION/ q" "$CHANGELOG_FILE" | tail -n 1 # the previous version heading
|
||||
sed "1,/$CURRENT_VERSION/ d" "$CHANGELOG_FILE" # everything after previous version heading
|
||||
) >"$CHANGELOG_FILE.bak"
|
||||
mv "$CHANGELOG_FILE.bak" "$CHANGELOG_FILE"
|
||||
|
||||
# done; remove backup files
|
||||
rm -f $CARGO_MANIFEST.bak
|
||||
rm -f $CHANGELOG_FILE.bak
|
||||
rm -f $README_FILE.bak
|
||||
|
||||
echo "manifest, changelog, and readme updated"
|
||||
echo
|
||||
echo "check other references:"
|
||||
rg "$PACKAGE_NAME =" || true
|
||||
rg "package = \"$PACKAGE_NAME\"" || true
|
||||
|
||||
if [ $MACOS ]; then
|
||||
printf "prepare $PACKAGE_NAME release $NEW_VERSION" | pbcopy
|
||||
else
|
||||
echo
|
||||
echo "commit message:"
|
||||
echo "prepare $PACKAGE_NAME release $NEW_VERSION"
|
||||
fi
|
||||
|
||||
SHORT_PACKAGE_NAME="$(echo $PACKAGE_NAME | sed 's/^actix-web-//' | sed 's/^actix-//')"
|
||||
GIT_TAG="$(echo $SHORT_PACKAGE_NAME-v$NEW_VERSION)"
|
||||
RELEASE_TITLE="$(echo $PACKAGE_NAME: v$NEW_VERSION)"
|
||||
|
||||
echo
|
||||
echo "GitHub release command:"
|
||||
echo "gh release create \"$GIT_TAG\" --draft --title \"$RELEASE_TITLE\" --notes-file \"$CHANGE_CHUNK_FILE\" --prerelease"
|
||||
|
||||
read -p "Submit draft GH release: (y/N) " GH_RELEASE
|
||||
GH_RELEASE="${GH_RELEASE:-n}"
|
||||
|
||||
if [ "$GH_RELEASE" = 'y' ] || [ "$GH_RELEASE" = 'Y' ]; then
|
||||
gh release create "$GIT_TAG" --draft --title "$RELEASE_TITLE" --notes-file "$CHANGE_CHUNK_FILE" --prerelease
|
||||
fi
|
||||
|
||||
echo
|
16
scripts/ci-test
Executable file
16
scripts/ci-test
Executable file
@ -0,0 +1,16 @@
|
||||
#!/bin/sh
|
||||
|
||||
# run tests matching what CI does for non-linux feature sets
|
||||
|
||||
set -x
|
||||
|
||||
cargo test --lib --tests -p=actix-router --all-features
|
||||
cargo test --lib --tests -p=actix-http --all-features
|
||||
cargo test --lib --tests -p=actix-web --features=rustls,openssl -- --skip=test_reading_deflate_encoding_large_random_rustls
|
||||
cargo test --lib --tests -p=actix-web-codegen --all-features
|
||||
cargo test --lib --tests -p=awc --all-features
|
||||
cargo test --lib --tests -p=actix-http-test --all-features
|
||||
cargo test --lib --tests -p=actix-test --all-features
|
||||
cargo test --lib --tests -p=actix-files
|
||||
cargo test --lib --tests -p=actix-multipart --all-features
|
||||
cargo test --lib --tests -p=actix-web-actors --all-features
|
@ -49,7 +49,7 @@ downcast_dyn!(ResponseError);
|
||||
impl ResponseError for Box<dyn StdError + 'static> {}
|
||||
|
||||
#[cfg(feature = "openssl")]
|
||||
impl ResponseError for actix_tls::accept::openssl::SslError {}
|
||||
impl ResponseError for actix_tls::accept::openssl::reexports::Error {}
|
||||
|
||||
impl ResponseError for serde::de::value::Error {
|
||||
fn status_code(&self) -> StatusCode {
|
||||
|
231
src/extract.rs
231
src/extract.rs
@ -10,6 +10,7 @@ use std::{
|
||||
use actix_http::http::{Method, Uri};
|
||||
use actix_utils::future::{ok, Ready};
|
||||
use futures_core::ready;
|
||||
use pin_project_lite::pin_project;
|
||||
|
||||
use crate::{dev::Payload, Error, HttpRequest};
|
||||
|
||||
@ -139,10 +140,11 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
#[pin_project::pin_project]
|
||||
pub struct FromRequestOptFuture<Fut> {
|
||||
#[pin]
|
||||
fut: Fut,
|
||||
pin_project! {
|
||||
pub struct FromRequestOptFuture<Fut> {
|
||||
#[pin]
|
||||
fut: Fut,
|
||||
}
|
||||
}
|
||||
|
||||
impl<Fut, T, E> Future for FromRequestOptFuture<Fut>
|
||||
@ -226,10 +228,11 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
#[pin_project::pin_project]
|
||||
pub struct FromRequestResFuture<Fut> {
|
||||
#[pin]
|
||||
fut: Fut,
|
||||
pin_project! {
|
||||
pub struct FromRequestResFuture<Fut> {
|
||||
#[pin]
|
||||
fut: Fut,
|
||||
}
|
||||
}
|
||||
|
||||
impl<Fut, T, E> Future for FromRequestResFuture<Fut>
|
||||
@ -297,102 +300,104 @@ impl FromRequest for () {
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! tuple_from_req ({$fut_type:ident, $(($n:tt, $T:ident)),+} => {
|
||||
|
||||
// This module is a trick to get around the inability of
|
||||
// `macro_rules!` macros to make new idents. We want to make
|
||||
// a new `FutWrapper` struct for each distinct invocation of
|
||||
// this macro. Ideally, we would name it something like
|
||||
// `FutWrapper_$fut_type`, but this can't be done in a macro_rules
|
||||
// macro.
|
||||
//
|
||||
// Instead, we put everything in a module named `$fut_type`, thus allowing
|
||||
// us to use the name `FutWrapper` without worrying about conflicts.
|
||||
// This macro only exists to generate trait impls for tuples - these
|
||||
// are inherently global, so users don't have to care about this
|
||||
// weird trick.
|
||||
#[allow(non_snake_case)]
|
||||
mod $fut_type {
|
||||
|
||||
// Bring everything into scope, so we don't need
|
||||
// redundant imports
|
||||
use super::*;
|
||||
|
||||
/// A helper struct to allow us to pin-project through
|
||||
/// to individual fields
|
||||
#[pin_project::pin_project]
|
||||
struct FutWrapper<$($T: FromRequest),+>($(#[pin] $T::Future),+);
|
||||
|
||||
/// FromRequest implementation for tuple
|
||||
#[doc(hidden)]
|
||||
#[allow(unused_parens)]
|
||||
impl<$($T: FromRequest + 'static),+> FromRequest for ($($T,)+)
|
||||
{
|
||||
type Error = Error;
|
||||
type Future = $fut_type<$($T),+>;
|
||||
|
||||
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
|
||||
$fut_type {
|
||||
items: <($(Option<$T>,)+)>::default(),
|
||||
futs: FutWrapper($($T::from_request(req, payload),)+),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
#[pin_project::pin_project]
|
||||
pub struct $fut_type<$($T: FromRequest),+> {
|
||||
items: ($(Option<$T>,)+),
|
||||
#[pin]
|
||||
futs: FutWrapper<$($T,)+>,
|
||||
}
|
||||
|
||||
impl<$($T: FromRequest),+> Future for $fut_type<$($T),+>
|
||||
{
|
||||
type Output = Result<($($T,)+), Error>;
|
||||
|
||||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||
let mut this = self.project();
|
||||
|
||||
let mut ready = true;
|
||||
$(
|
||||
if this.items.$n.is_none() {
|
||||
match this.futs.as_mut().project().$n.poll(cx) {
|
||||
Poll::Ready(Ok(item)) => {
|
||||
this.items.$n = Some(item);
|
||||
}
|
||||
Poll::Pending => ready = false,
|
||||
Poll::Ready(Err(e)) => return Poll::Ready(Err(e.into())),
|
||||
}
|
||||
}
|
||||
)+
|
||||
|
||||
if ready {
|
||||
Poll::Ready(Ok(
|
||||
($(this.items.$n.take().unwrap(),)+)
|
||||
))
|
||||
} else {
|
||||
Poll::Pending
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
#[rustfmt::skip]
|
||||
mod m {
|
||||
#[doc(hidden)]
|
||||
#[allow(non_snake_case)]
|
||||
mod tuple_from_req {
|
||||
use super::*;
|
||||
|
||||
tuple_from_req!(TupleFromRequest1, (0, A));
|
||||
tuple_from_req!(TupleFromRequest2, (0, A), (1, B));
|
||||
tuple_from_req!(TupleFromRequest3, (0, A), (1, B), (2, C));
|
||||
tuple_from_req!(TupleFromRequest4, (0, A), (1, B), (2, C), (3, D));
|
||||
tuple_from_req!(TupleFromRequest5, (0, A), (1, B), (2, C), (3, D), (4, E));
|
||||
tuple_from_req!(TupleFromRequest6, (0, A), (1, B), (2, C), (3, D), (4, E), (5, F));
|
||||
tuple_from_req!(TupleFromRequest7, (0, A), (1, B), (2, C), (3, D), (4, E), (5, F), (6, G));
|
||||
tuple_from_req!(TupleFromRequest8, (0, A), (1, B), (2, C), (3, D), (4, E), (5, F), (6, G), (7, H));
|
||||
tuple_from_req!(TupleFromRequest9, (0, A), (1, B), (2, C), (3, D), (4, E), (5, F), (6, G), (7, H), (8, I));
|
||||
tuple_from_req!(TupleFromRequest10, (0, A), (1, B), (2, C), (3, D), (4, E), (5, F), (6, G), (7, H), (8, I), (9, J));
|
||||
macro_rules! tuple_from_req {
|
||||
($fut: ident; $($T: ident),*) => {
|
||||
/// FromRequest implementation for tuple
|
||||
#[allow(unused_parens)]
|
||||
impl<$($T: FromRequest + 'static),+> FromRequest for ($($T,)+)
|
||||
{
|
||||
type Error = Error;
|
||||
type Future = $fut<$($T),+>;
|
||||
|
||||
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
|
||||
$fut {
|
||||
$(
|
||||
$T: ExtractFuture::Future {
|
||||
fut: $T::from_request(req, payload)
|
||||
},
|
||||
)+
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pin_project! {
|
||||
pub struct $fut<$($T: FromRequest),+> {
|
||||
$(
|
||||
#[pin]
|
||||
$T: ExtractFuture<$T::Future, $T>,
|
||||
)+
|
||||
}
|
||||
}
|
||||
|
||||
impl<$($T: FromRequest),+> Future for $fut<$($T),+>
|
||||
{
|
||||
type Output = Result<($($T,)+), Error>;
|
||||
|
||||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||
let mut this = self.project();
|
||||
|
||||
let mut ready = true;
|
||||
$(
|
||||
match this.$T.as_mut().project() {
|
||||
ExtractProj::Future { fut } => match fut.poll(cx) {
|
||||
Poll::Ready(Ok(output)) => {
|
||||
let _ = this.$T.as_mut().project_replace(ExtractFuture::Done { output });
|
||||
},
|
||||
Poll::Ready(Err(e)) => return Poll::Ready(Err(e.into())),
|
||||
Poll::Pending => ready = false,
|
||||
},
|
||||
ExtractProj::Done { .. } => {},
|
||||
ExtractProj::Empty => unreachable!("FromRequest polled after finished"),
|
||||
}
|
||||
)+
|
||||
|
||||
if ready {
|
||||
Poll::Ready(Ok(
|
||||
($(
|
||||
match this.$T.project_replace(ExtractFuture::Empty) {
|
||||
ExtractReplaceProj::Done { output } => output,
|
||||
_ => unreachable!("FromRequest polled after finished"),
|
||||
},
|
||||
)+)
|
||||
))
|
||||
} else {
|
||||
Poll::Pending
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
pin_project! {
|
||||
#[project = ExtractProj]
|
||||
#[project_replace = ExtractReplaceProj]
|
||||
enum ExtractFuture<Fut, Res> {
|
||||
Future {
|
||||
#[pin]
|
||||
fut: Fut
|
||||
},
|
||||
Done {
|
||||
output: Res,
|
||||
},
|
||||
Empty
|
||||
}
|
||||
}
|
||||
|
||||
tuple_from_req! { TupleFromRequest1; A }
|
||||
tuple_from_req! { TupleFromRequest2; A, B }
|
||||
tuple_from_req! { TupleFromRequest3; A, B, C }
|
||||
tuple_from_req! { TupleFromRequest4; A, B, C, D }
|
||||
tuple_from_req! { TupleFromRequest5; A, B, C, D, E }
|
||||
tuple_from_req! { TupleFromRequest6; A, B, C, D, E, F }
|
||||
tuple_from_req! { TupleFromRequest7; A, B, C, D, E, F, G }
|
||||
tuple_from_req! { TupleFromRequest8; A, B, C, D, E, F, G, H }
|
||||
tuple_from_req! { TupleFromRequest9; A, B, C, D, E, F, G, H, I }
|
||||
tuple_from_req! { TupleFromRequest10; A, B, C, D, E, F, G, H, I, J }
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@ -494,4 +499,26 @@ mod tests {
|
||||
let method = Method::extract(&req).await.unwrap();
|
||||
assert_eq!(method, Method::GET);
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_concurrent() {
|
||||
let (req, mut pl) = TestRequest::default()
|
||||
.uri("/foo/bar")
|
||||
.method(Method::GET)
|
||||
.insert_header((header::CONTENT_TYPE, "application/x-www-form-urlencoded"))
|
||||
.insert_header((header::CONTENT_LENGTH, "11"))
|
||||
.set_payload(Bytes::from_static(b"hello=world"))
|
||||
.to_http_parts();
|
||||
let (method, uri, form) = <(Method, Uri, Form<Info>)>::from_request(&req, &mut pl)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(method, Method::GET);
|
||||
assert_eq!(uri.path(), "/foo/bar");
|
||||
assert_eq!(
|
||||
form,
|
||||
Form(Info {
|
||||
hello: "world".into()
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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"]);
|
||||
}
|
||||
|
@ -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"]);
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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,
|
||||
|
70
src/http/header/any_or_some.rs
Normal file
70
src/http/header/any_or_some.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 parse `*` successfully. On the other hand, the `mime` crate, used for
|
||||
/// [`Accept`](super::Accept), has first-party support for wildcard items so this wrapper is not
|
||||
/// used in those header types.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Hash)]
|
||||
pub enum AnyOrSome<T> {
|
||||
/// A wildcard value.
|
||||
Any,
|
||||
|
||||
/// A valid `T`.
|
||||
Item(T),
|
||||
}
|
||||
|
||||
impl<T> AnyOrSome<T> {
|
||||
/// Returns true if item is wildcard (`*`) variant.
|
||||
pub fn is_any(&self) -> bool {
|
||||
matches!(self, Self::Any)
|
||||
}
|
||||
|
||||
/// Returns true if item is a valid item (`T`) variant.
|
||||
pub fn is_item(&self) -> bool {
|
||||
matches!(self, Self::Item(_))
|
||||
}
|
||||
|
||||
/// Returns reference to value in `Item` variant, if it is set.
|
||||
pub fn item(&self) -> Option<&T> {
|
||||
match self {
|
||||
AnyOrSome::Item(ref item) => Some(item),
|
||||
AnyOrSome::Any => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Consumes the container, returning the value in the `Item` variant, if it is set.
|
||||
pub fn into_item(self) -> Option<T> {
|
||||
match self {
|
||||
AnyOrSome::Item(item) => Some(item),
|
||||
AnyOrSome::Any => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: fmt::Display> fmt::Display for AnyOrSome<T> {
|
||||
#[inline]
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
AnyOrSome::Any => f.write_char('*'),
|
||||
AnyOrSome::Item(item) => fmt::Display::fmt(item, f),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: str::FromStr> str::FromStr for AnyOrSome<T> {
|
||||
type Err = T::Err;
|
||||
|
||||
#[inline]
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
match s.trim() {
|
||||
"*" => Ok(Self::Any),
|
||||
other => other.parse().map(AnyOrSome::Item),
|
||||
}
|
||||
}
|
||||
}
|
@ -1,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,
|
||||
|
@ -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
|
||||
|
@ -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"]);
|
||||
}
|
||||
|
@ -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 {
|
||||
|
@ -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 {}
|
||||
|
@ -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"]);
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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\""],
|
||||
|
@ -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"]);
|
||||
}
|
||||
|
@ -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\""],
|
||||
|
@ -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"]);
|
||||
}
|
||||
|
@ -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\""]);
|
||||
|
@ -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\""]);
|
||||
|
@ -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"]);
|
||||
}
|
||||
|
@ -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"]);
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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};
|
||||
|
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),
|
||||
}
|
||||
}
|
||||
}
|
@ -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),
|
||||
|
@ -10,6 +10,7 @@ use std::{
|
||||
use actix_http::body::{AnyBody, MessageBody};
|
||||
use actix_service::{Service, Transform};
|
||||
use futures_core::{future::LocalBoxFuture, ready};
|
||||
use pin_project_lite::pin_project;
|
||||
|
||||
use crate::{error::Error, service::ServiceResponse};
|
||||
|
||||
@ -89,10 +90,11 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
#[pin_project::pin_project]
|
||||
pub struct CompatMiddlewareFuture<Fut> {
|
||||
#[pin]
|
||||
fut: Fut,
|
||||
pin_project! {
|
||||
pub struct CompatMiddlewareFuture<Fut> {
|
||||
#[pin]
|
||||
fut: Fut,
|
||||
}
|
||||
}
|
||||
|
||||
impl<Fut, T, E> Future for CompatMiddlewareFuture<Fut>
|
||||
|
@ -20,7 +20,7 @@ use actix_utils::future::{ok, Either, Ready};
|
||||
use bytes::Bytes;
|
||||
use futures_core::ready;
|
||||
use once_cell::sync::Lazy;
|
||||
use pin_project::pin_project;
|
||||
use pin_project_lite::pin_project;
|
||||
|
||||
use crate::{
|
||||
dev::BodyEncoding,
|
||||
@ -162,15 +162,16 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
#[pin_project]
|
||||
pub struct CompressResponse<S, B>
|
||||
where
|
||||
S: Service<ServiceRequest>,
|
||||
{
|
||||
#[pin]
|
||||
fut: S::Future,
|
||||
encoding: ContentEncoding,
|
||||
_phantom: PhantomData<B>,
|
||||
pin_project! {
|
||||
pub struct CompressResponse<S, B>
|
||||
where
|
||||
S: Service<ServiceRequest>,
|
||||
{
|
||||
#[pin]
|
||||
fut: S::Future,
|
||||
encoding: ContentEncoding,
|
||||
_phantom: PhantomData<B>,
|
||||
}
|
||||
}
|
||||
|
||||
impl<S, B> Future for CompressResponse<S, B>
|
||||
|
@ -11,6 +11,7 @@ use std::{
|
||||
|
||||
use actix_utils::future::{ready, Ready};
|
||||
use futures_core::ready;
|
||||
use pin_project_lite::pin_project;
|
||||
|
||||
use crate::{
|
||||
dev::{Service, Transform},
|
||||
@ -153,12 +154,13 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
#[pin_project::pin_project]
|
||||
pub struct DefaultHeaderFuture<S: Service<ServiceRequest>, B> {
|
||||
#[pin]
|
||||
fut: S::Future,
|
||||
inner: Rc<Inner>,
|
||||
_body: PhantomData<B>,
|
||||
pin_project! {
|
||||
pub struct DefaultHeaderFuture<S: Service<ServiceRequest>, B> {
|
||||
#[pin]
|
||||
fut: S::Future,
|
||||
inner: Rc<Inner>,
|
||||
_body: PhantomData<B>,
|
||||
}
|
||||
}
|
||||
|
||||
impl<S, B> Future for DefaultHeaderFuture<S, B>
|
||||
|
@ -10,6 +10,7 @@ use std::{
|
||||
use actix_service::{Service, Transform};
|
||||
use ahash::AHashMap;
|
||||
use futures_core::{future::LocalBoxFuture, ready};
|
||||
use pin_project_lite::pin_project;
|
||||
|
||||
use crate::{
|
||||
dev::{ServiceRequest, ServiceResponse},
|
||||
@ -130,19 +131,21 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
#[pin_project::pin_project(project = ErrorHandlersProj)]
|
||||
pub enum ErrorHandlersFuture<Fut, B>
|
||||
where
|
||||
Fut: Future,
|
||||
{
|
||||
ServiceFuture {
|
||||
#[pin]
|
||||
fut: Fut,
|
||||
handlers: Handlers<B>,
|
||||
},
|
||||
HandlerFuture {
|
||||
fut: LocalBoxFuture<'static, Fut::Output>,
|
||||
},
|
||||
pin_project! {
|
||||
#[project = ErrorHandlersProj]
|
||||
pub enum ErrorHandlersFuture<Fut, B>
|
||||
where
|
||||
Fut: Future,
|
||||
{
|
||||
ServiceFuture {
|
||||
#[pin]
|
||||
fut: Fut,
|
||||
handlers: Handlers<B>,
|
||||
},
|
||||
HandlerFuture {
|
||||
fut: LocalBoxFuture<'static, Fut::Output>,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
impl<Fut, B> Future for ErrorHandlersFuture<Fut, B>
|
||||
|
@ -13,10 +13,11 @@ use std::{
|
||||
};
|
||||
|
||||
use actix_service::{Service, Transform};
|
||||
use actix_utils::future::{ok, Ready};
|
||||
use actix_utils::future::{ready, Ready};
|
||||
use bytes::Bytes;
|
||||
use futures_core::ready;
|
||||
use log::{debug, warn};
|
||||
use pin_project_lite::pin_project;
|
||||
use regex::{Regex, RegexSet};
|
||||
use time::{format_description::well_known::Rfc3339, OffsetDateTime};
|
||||
|
||||
@ -180,8 +181,8 @@ where
|
||||
{
|
||||
type Response = ServiceResponse<StreamLog<B>>;
|
||||
type Error = Error;
|
||||
type InitError = ();
|
||||
type Transform = LoggerMiddleware<S>;
|
||||
type InitError = ();
|
||||
type Future = Ready<Result<Self::Transform, Self::InitError>>;
|
||||
|
||||
fn new_transform(&self, service: S) -> Self::Future {
|
||||
@ -195,10 +196,10 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
ok(LoggerMiddleware {
|
||||
ready(Ok(LoggerMiddleware {
|
||||
service,
|
||||
inner: self.0.clone(),
|
||||
})
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
@ -246,17 +247,18 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
#[pin_project::pin_project]
|
||||
pub struct LoggerResponse<S, B>
|
||||
where
|
||||
B: MessageBody,
|
||||
S: Service<ServiceRequest>,
|
||||
{
|
||||
#[pin]
|
||||
fut: S::Future,
|
||||
time: OffsetDateTime,
|
||||
format: Option<Format>,
|
||||
_phantom: PhantomData<B>,
|
||||
pin_project! {
|
||||
pub struct LoggerResponse<S, B>
|
||||
where
|
||||
B: MessageBody,
|
||||
S: Service<ServiceRequest>,
|
||||
{
|
||||
#[pin]
|
||||
fut: S::Future,
|
||||
time: OffsetDateTime,
|
||||
format: Option<Format>,
|
||||
_phantom: PhantomData<B>,
|
||||
}
|
||||
}
|
||||
|
||||
impl<S, B> Future for LoggerResponse<S, B>
|
||||
@ -296,28 +298,25 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
use pin_project::{pin_project, pinned_drop};
|
||||
|
||||
#[pin_project(PinnedDrop)]
|
||||
pub struct StreamLog<B> {
|
||||
#[pin]
|
||||
body: B,
|
||||
format: Option<Format>,
|
||||
size: usize,
|
||||
time: OffsetDateTime,
|
||||
}
|
||||
|
||||
#[pinned_drop]
|
||||
impl<B> PinnedDrop for StreamLog<B> {
|
||||
fn drop(self: Pin<&mut Self>) {
|
||||
if let Some(ref format) = self.format {
|
||||
let render = |fmt: &mut fmt::Formatter<'_>| {
|
||||
for unit in &format.0 {
|
||||
unit.render(fmt, self.size, self.time)?;
|
||||
}
|
||||
Ok(())
|
||||
};
|
||||
log::info!("{}", FormatDisplay(&render));
|
||||
pin_project! {
|
||||
pub struct StreamLog<B> {
|
||||
#[pin]
|
||||
body: B,
|
||||
format: Option<Format>,
|
||||
size: usize,
|
||||
time: OffsetDateTime,
|
||||
}
|
||||
impl<B> PinnedDrop for StreamLog<B> {
|
||||
fn drop(this: Pin<&mut Self>) {
|
||||
if let Some(ref format) = this.format {
|
||||
let render = |fmt: &mut fmt::Formatter<'_>| {
|
||||
for unit in &format.0 {
|
||||
unit.render(fmt, this.size, this.time)?;
|
||||
}
|
||||
Ok(())
|
||||
};
|
||||
log::info!("{}", FormatDisplay(&render));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -296,7 +296,7 @@ impl Future for HttpResponse<AnyBody> {
|
||||
|
||||
#[cfg(feature = "cookies")]
|
||||
pub struct CookieIter<'a> {
|
||||
iter: header::GetAll<'a>,
|
||||
iter: header::map::GetAll<'a>,
|
||||
}
|
||||
|
||||
#[cfg(feature = "cookies")]
|
||||
|
@ -15,9 +15,9 @@ use actix_service::{
|
||||
};
|
||||
|
||||
#[cfg(feature = "openssl")]
|
||||
use actix_tls::accept::openssl::{AlpnError, SslAcceptor, SslAcceptorBuilder};
|
||||
use actix_tls::accept::openssl::reexports::{AlpnError, SslAcceptor, SslAcceptorBuilder};
|
||||
#[cfg(feature = "rustls")]
|
||||
use actix_tls::accept::rustls::ServerConfig as RustlsServerConfig;
|
||||
use actix_tls::accept::rustls::reexports::ServerConfig as RustlsServerConfig;
|
||||
|
||||
use crate::{config::AppConfig, Error};
|
||||
|
||||
@ -108,11 +108,11 @@ where
|
||||
/// [Extensions] container so that request-local data can be passed to middleware and handlers.
|
||||
///
|
||||
/// For example:
|
||||
/// - `actix_tls::openssl::SslStream<actix_web::rt::net::TcpStream>` when using openssl.
|
||||
/// - `actix_tls::rustls::TlsStream<actix_web::rt::net::TcpStream>` when using rustls.
|
||||
/// - `actix_tls::accept::openssl::TlsStream<actix_web::rt::net::TcpStream>` when using openssl.
|
||||
/// - `actix_tls::accept::rustls::TlsStream<actix_web::rt::net::TcpStream>` when using rustls.
|
||||
/// - `actix_web::rt::net::TcpStream` when no encryption is used.
|
||||
///
|
||||
/// See `on_connect` example for additional details.
|
||||
/// See the `on_connect` example for additional details.
|
||||
pub fn on_connect<CB>(self, f: CB) -> HttpServer<F, I, S, B>
|
||||
where
|
||||
CB: Fn(&dyn Any, &mut Extensions) + Send + Sync + 'static,
|
||||
|
@ -561,7 +561,6 @@ where
|
||||
/// The max number of services can be grouped together is 12.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use actix_web::{services, web, App};
|
||||
///
|
||||
|
@ -9,6 +9,7 @@ use std::{
|
||||
|
||||
use bytes::Bytes;
|
||||
use futures_core::ready;
|
||||
use pin_project_lite::pin_project;
|
||||
|
||||
use crate::{
|
||||
dev,
|
||||
@ -198,37 +199,40 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
#[pin_project::pin_project]
|
||||
pub struct EitherExtractFut<L, R>
|
||||
where
|
||||
R: FromRequest,
|
||||
L: FromRequest,
|
||||
{
|
||||
req: HttpRequest,
|
||||
#[pin]
|
||||
state: EitherExtractState<L, R>,
|
||||
pin_project! {
|
||||
pub struct EitherExtractFut<L, R>
|
||||
where
|
||||
R: FromRequest,
|
||||
L: FromRequest,
|
||||
{
|
||||
req: HttpRequest,
|
||||
#[pin]
|
||||
state: EitherExtractState<L, R>,
|
||||
}
|
||||
}
|
||||
|
||||
#[pin_project::pin_project(project = EitherExtractProj)]
|
||||
pub enum EitherExtractState<L, R>
|
||||
where
|
||||
L: FromRequest,
|
||||
R: FromRequest,
|
||||
{
|
||||
Bytes {
|
||||
#[pin]
|
||||
bytes: <Bytes as FromRequest>::Future,
|
||||
},
|
||||
Left {
|
||||
#[pin]
|
||||
left: L::Future,
|
||||
fallback: Bytes,
|
||||
},
|
||||
Right {
|
||||
#[pin]
|
||||
right: R::Future,
|
||||
left_err: Option<L::Error>,
|
||||
},
|
||||
pin_project! {
|
||||
#[project = EitherExtractProj]
|
||||
pub enum EitherExtractState<L, R>
|
||||
where
|
||||
L: FromRequest,
|
||||
R: FromRequest,
|
||||
{
|
||||
Bytes {
|
||||
#[pin]
|
||||
bytes: <Bytes as FromRequest>::Future,
|
||||
},
|
||||
Left {
|
||||
#[pin]
|
||||
left: L::Future,
|
||||
fallback: Bytes,
|
||||
},
|
||||
Right {
|
||||
#[pin]
|
||||
right: R::Future,
|
||||
left_err: Option<L::Error>,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
impl<R, RF, RE, L, LF, LE> Future for EitherExtractFut<L, R>
|
||||
|
Reference in New Issue
Block a user