mirror of https://github.com/fafhrd91/actix-web
Merge branch 'master' into feat/files/filter
This commit is contained in:
commit
85ad2c44ec
|
@ -44,17 +44,11 @@ jobs:
|
||||||
profile: minimal
|
profile: minimal
|
||||||
override: true
|
override: true
|
||||||
|
|
||||||
- name: Install ${{ matrix.version }}
|
|
||||||
uses: actions-rs/toolchain@v1
|
|
||||||
with:
|
|
||||||
toolchain: ${{ matrix.version }}-${{ matrix.target.triple }}
|
|
||||||
profile: minimal
|
|
||||||
override: true
|
|
||||||
|
|
||||||
- name: Generate Cargo.lock
|
- name: Generate Cargo.lock
|
||||||
uses: actions-rs/cargo@v1
|
uses: actions-rs/cargo@v1
|
||||||
with:
|
with:
|
||||||
command: generate-lockfile
|
command: generate-lockfile
|
||||||
|
|
||||||
- name: Cache Dependencies
|
- name: Cache Dependencies
|
||||||
uses: Swatinem/rust-cache@v1.2.0
|
uses: Swatinem/rust-cache@v1.2.0
|
||||||
|
|
||||||
|
@ -102,6 +96,7 @@ jobs:
|
||||||
run: |
|
run: |
|
||||||
cargo install cargo-tarpaulin --vers "^0.13"
|
cargo install cargo-tarpaulin --vers "^0.13"
|
||||||
cargo tarpaulin --out Xml --verbose
|
cargo tarpaulin --out Xml --verbose
|
||||||
|
|
||||||
- name: Upload to Codecov
|
- name: Upload to Codecov
|
||||||
if: >
|
if: >
|
||||||
matrix.target.os == 'ubuntu-latest'
|
matrix.target.os == 'ubuntu-latest'
|
||||||
|
|
|
@ -11,11 +11,15 @@
|
||||||
* Change compression algorithm features flags. [#2250]
|
* Change compression algorithm features flags. [#2250]
|
||||||
* Deprecate `App::data` and `App::data_factory`. [#2271]
|
* Deprecate `App::data` and `App::data_factory`. [#2271]
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
* Scope and Resource middleware can access data items set on their own layer. [#2288]
|
||||||
|
|
||||||
[#2177]: https://github.com/actix/actix-web/pull/2177
|
[#2177]: https://github.com/actix/actix-web/pull/2177
|
||||||
[#2250]: https://github.com/actix/actix-web/pull/2250
|
[#2250]: https://github.com/actix/actix-web/pull/2250
|
||||||
[#2271]: https://github.com/actix/actix-web/pull/2271
|
[#2271]: https://github.com/actix/actix-web/pull/2271
|
||||||
[#2262]: https://github.com/actix/actix-web/pull/2262
|
[#2262]: https://github.com/actix/actix-web/pull/2262
|
||||||
[#2263]: https://github.com/actix/actix-web/pull/2263
|
[#2263]: https://github.com/actix/actix-web/pull/2263
|
||||||
|
[#2288]: https://github.com/actix/actix-web/pull/2288
|
||||||
|
|
||||||
|
|
||||||
## 4.0.0-beta.7 - 2021-06-17
|
## 4.0.0-beta.7 - 2021-06-17
|
||||||
|
|
|
@ -4,6 +4,10 @@
|
||||||
### Changed
|
### Changed
|
||||||
* Change compression algorithm features flags. [#2250]
|
* Change compression algorithm features flags. [#2250]
|
||||||
|
|
||||||
|
### Removed
|
||||||
|
* `downcast` and `downcast_get_type_id` macros. [#2291]
|
||||||
|
|
||||||
|
[#2291]: https://github.com/actix/actix-web/pull/2291
|
||||||
[#2250]: https://github.com/actix/actix-web/pull/2250
|
[#2250]: https://github.com/actix/actix-web/pull/2250
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -27,9 +27,6 @@
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate log;
|
extern crate log;
|
||||||
|
|
||||||
#[macro_use]
|
|
||||||
mod macros;
|
|
||||||
|
|
||||||
pub mod body;
|
pub mod body;
|
||||||
mod builder;
|
mod builder;
|
||||||
pub mod client;
|
pub mod client;
|
||||||
|
|
|
@ -1,110 +0,0 @@
|
||||||
#[macro_export]
|
|
||||||
#[doc(hidden)]
|
|
||||||
macro_rules! downcast_get_type_id {
|
|
||||||
() => {
|
|
||||||
/// A helper method to get the type ID of the type
|
|
||||||
/// this trait is implemented on.
|
|
||||||
/// This method is unsafe to *implement*, since `downcast_ref` relies
|
|
||||||
/// on the returned `TypeId` to perform a cast.
|
|
||||||
///
|
|
||||||
/// Unfortunately, Rust has no notion of a trait method that is
|
|
||||||
/// unsafe to implement (marking it as `unsafe` makes it unsafe
|
|
||||||
/// to *call*). As a workaround, we require this method
|
|
||||||
/// to return a private type along with the `TypeId`. This
|
|
||||||
/// private type (`PrivateHelper`) has a private constructor,
|
|
||||||
/// making it impossible for safe code to construct outside of
|
|
||||||
/// this module. This ensures that safe code cannot violate
|
|
||||||
/// type-safety by implementing this method.
|
|
||||||
///
|
|
||||||
/// We also take `PrivateHelper` as a parameter, to ensure that
|
|
||||||
/// safe code cannot obtain a `PrivateHelper` instance by
|
|
||||||
/// delegating to an existing implementation of `__private_get_type_id__`
|
|
||||||
#[doc(hidden)]
|
|
||||||
fn __private_get_type_id__(
|
|
||||||
&self,
|
|
||||||
_: PrivateHelper,
|
|
||||||
) -> (std::any::TypeId, PrivateHelper)
|
|
||||||
where
|
|
||||||
Self: 'static,
|
|
||||||
{
|
|
||||||
(std::any::TypeId::of::<Self>(), PrivateHelper(()))
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
//Generate implementation for dyn $name
|
|
||||||
#[doc(hidden)]
|
|
||||||
#[macro_export]
|
|
||||||
macro_rules! downcast {
|
|
||||||
($name:ident) => {
|
|
||||||
/// A struct with a private constructor, for use with
|
|
||||||
/// `__private_get_type_id__`. Its single field is private,
|
|
||||||
/// ensuring that it can only be constructed from this module
|
|
||||||
#[doc(hidden)]
|
|
||||||
pub struct PrivateHelper(());
|
|
||||||
|
|
||||||
impl dyn $name + 'static {
|
|
||||||
/// Downcasts generic body to a specific type.
|
|
||||||
pub fn downcast_ref<T: $name + 'static>(&self) -> Option<&T> {
|
|
||||||
if self.__private_get_type_id__(PrivateHelper(())).0
|
|
||||||
== std::any::TypeId::of::<T>()
|
|
||||||
{
|
|
||||||
// SAFETY: external crates cannot override the default
|
|
||||||
// implementation of `__private_get_type_id__`, since
|
|
||||||
// it requires returning a private type. We can therefore
|
|
||||||
// rely on the returned `TypeId`, which ensures that this
|
|
||||||
// case is correct.
|
|
||||||
unsafe { Some(&*(self as *const dyn $name as *const T)) }
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Downcasts a generic body to a mutable specific type.
|
|
||||||
pub fn downcast_mut<T: $name + 'static>(&mut self) -> Option<&mut T> {
|
|
||||||
if self.__private_get_type_id__(PrivateHelper(())).0
|
|
||||||
== std::any::TypeId::of::<T>()
|
|
||||||
{
|
|
||||||
// SAFETY: external crates cannot override the default
|
|
||||||
// implementation of `__private_get_type_id__`, since
|
|
||||||
// it requires returning a private type. We can therefore
|
|
||||||
// rely on the returned `TypeId`, which ensures that this
|
|
||||||
// case is correct.
|
|
||||||
unsafe {
|
|
||||||
Some(&mut *(self as *const dyn $name as *const T as *mut T))
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
#![allow(clippy::upper_case_acronyms)]
|
|
||||||
|
|
||||||
trait MB {
|
|
||||||
downcast_get_type_id!();
|
|
||||||
}
|
|
||||||
|
|
||||||
downcast!(MB);
|
|
||||||
|
|
||||||
impl MB for String {}
|
|
||||||
impl MB for () {}
|
|
||||||
|
|
||||||
#[actix_rt::test]
|
|
||||||
async fn test_any_casting() {
|
|
||||||
let mut body = String::from("hello cast");
|
|
||||||
let resp_body: &mut dyn MB = &mut body;
|
|
||||||
let body = resp_body.downcast_ref::<String>().unwrap();
|
|
||||||
assert_eq!(body, "hello cast");
|
|
||||||
let body = &mut resp_body.downcast_mut::<String>().unwrap();
|
|
||||||
body.push('!');
|
|
||||||
let body = resp_body.downcast_ref::<String>().unwrap();
|
|
||||||
assert_eq!(body, "hello cast!");
|
|
||||||
let not_body = resp_body.downcast_ref::<()>();
|
|
||||||
assert!(not_body.is_none());
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -43,13 +43,14 @@ impl App<AppEntry, Body> {
|
||||||
/// Create application builder. Application can be configured with a builder-like pattern.
|
/// Create application builder. Application can be configured with a builder-like pattern.
|
||||||
#[allow(clippy::new_without_default)]
|
#[allow(clippy::new_without_default)]
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
let fref = Rc::new(RefCell::new(None));
|
let factory_ref = Rc::new(RefCell::new(None));
|
||||||
|
|
||||||
App {
|
App {
|
||||||
endpoint: AppEntry::new(fref.clone()),
|
endpoint: AppEntry::new(factory_ref.clone()),
|
||||||
data_factories: Vec::new(),
|
data_factories: Vec::new(),
|
||||||
services: Vec::new(),
|
services: Vec::new(),
|
||||||
default: None,
|
default: None,
|
||||||
factory_ref: fref,
|
factory_ref,
|
||||||
external: Vec::new(),
|
external: Vec::new(),
|
||||||
extensions: Extensions::new(),
|
extensions: Extensions::new(),
|
||||||
_phantom: PhantomData,
|
_phantom: PhantomData,
|
||||||
|
|
|
@ -1,22 +1,22 @@
|
||||||
use std::cell::RefCell;
|
use std::{cell::RefCell, mem, rc::Rc};
|
||||||
use std::rc::Rc;
|
|
||||||
|
|
||||||
use actix_http::{Extensions, Request};
|
use actix_http::{Extensions, Request};
|
||||||
use actix_router::{Path, ResourceDef, Router, Url};
|
use actix_router::{Path, ResourceDef, Router, Url};
|
||||||
use actix_service::boxed::{self, BoxService, BoxServiceFactory};
|
use actix_service::{
|
||||||
use actix_service::{fn_service, Service, ServiceFactory};
|
boxed::{self, BoxService, BoxServiceFactory},
|
||||||
|
fn_service, Service, ServiceFactory,
|
||||||
|
};
|
||||||
use futures_core::future::LocalBoxFuture;
|
use futures_core::future::LocalBoxFuture;
|
||||||
use futures_util::future::join_all;
|
use futures_util::future::join_all;
|
||||||
|
|
||||||
use crate::data::FnDataFactory;
|
|
||||||
use crate::error::Error;
|
|
||||||
use crate::guard::Guard;
|
|
||||||
use crate::request::{HttpRequest, HttpRequestPool};
|
|
||||||
use crate::rmap::ResourceMap;
|
|
||||||
use crate::service::{AppServiceFactory, ServiceRequest, ServiceResponse};
|
|
||||||
use crate::{
|
use crate::{
|
||||||
config::{AppConfig, AppService},
|
config::{AppConfig, AppService},
|
||||||
HttpResponse,
|
data::FnDataFactory,
|
||||||
|
guard::Guard,
|
||||||
|
request::{HttpRequest, HttpRequestPool},
|
||||||
|
rmap::ResourceMap,
|
||||||
|
service::{AppServiceFactory, ServiceRequest, ServiceResponse},
|
||||||
|
Error, HttpResponse,
|
||||||
};
|
};
|
||||||
|
|
||||||
type Guards = Vec<Box<dyn Guard>>;
|
type Guards = Vec<Box<dyn Guard>>;
|
||||||
|
@ -75,7 +75,7 @@ where
|
||||||
let mut config = AppService::new(config, default.clone());
|
let mut config = AppService::new(config, default.clone());
|
||||||
|
|
||||||
// register services
|
// register services
|
||||||
std::mem::take(&mut *self.services.borrow_mut())
|
mem::take(&mut *self.services.borrow_mut())
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.for_each(|mut srv| srv.register(&mut config));
|
.for_each(|mut srv| srv.register(&mut config));
|
||||||
|
|
||||||
|
@ -98,7 +98,7 @@ where
|
||||||
});
|
});
|
||||||
|
|
||||||
// external resources
|
// external resources
|
||||||
for mut rdef in std::mem::take(&mut *self.external.borrow_mut()) {
|
for mut rdef in mem::take(&mut *self.external.borrow_mut()) {
|
||||||
rmap.add(&mut rdef, None);
|
rmap.add(&mut rdef, None);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -94,9 +94,9 @@ impl AppService {
|
||||||
F: IntoServiceFactory<S, ServiceRequest>,
|
F: IntoServiceFactory<S, ServiceRequest>,
|
||||||
S: ServiceFactory<
|
S: ServiceFactory<
|
||||||
ServiceRequest,
|
ServiceRequest,
|
||||||
Config = (),
|
|
||||||
Response = ServiceResponse,
|
Response = ServiceResponse,
|
||||||
Error = Error,
|
Error = Error,
|
||||||
|
Config = (),
|
||||||
InitError = (),
|
InitError = (),
|
||||||
> + 'static,
|
> + 'static,
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,6 +1,4 @@
|
||||||
#[macro_export]
|
macro_rules! downcast_get_type_id {
|
||||||
#[doc(hidden)]
|
|
||||||
macro_rules! __downcast_get_type_id {
|
|
||||||
() => {
|
() => {
|
||||||
/// A helper method to get the type ID of the type
|
/// A helper method to get the type ID of the type
|
||||||
/// this trait is implemented on.
|
/// this trait is implemented on.
|
||||||
|
@ -30,10 +28,8 @@ macro_rules! __downcast_get_type_id {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
//Generate implementation for dyn $name
|
// Generate implementation for dyn $name
|
||||||
#[doc(hidden)]
|
macro_rules! downcast_dyn {
|
||||||
#[macro_export]
|
|
||||||
macro_rules! __downcast_dyn {
|
|
||||||
($name:ident) => {
|
($name:ident) => {
|
||||||
/// A struct with a private constructor, for use with
|
/// A struct with a private constructor, for use with
|
||||||
/// `__private_get_type_id__`. Its single field is private,
|
/// `__private_get_type_id__`. Its single field is private,
|
||||||
|
@ -80,15 +76,17 @@ macro_rules! __downcast_dyn {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) use {downcast_dyn, downcast_get_type_id};
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
#![allow(clippy::upper_case_acronyms)]
|
#![allow(clippy::upper_case_acronyms)]
|
||||||
|
|
||||||
trait MB {
|
trait MB {
|
||||||
__downcast_get_type_id!();
|
downcast_get_type_id!();
|
||||||
}
|
}
|
||||||
|
|
||||||
__downcast_dyn!(MB);
|
downcast_dyn!(MB);
|
||||||
|
|
||||||
impl MB for String {}
|
impl MB for String {}
|
||||||
impl MB for () {}
|
impl MB for () {}
|
||||||
|
|
|
@ -18,6 +18,7 @@ mod response_error;
|
||||||
pub use self::error::Error;
|
pub use self::error::Error;
|
||||||
pub use self::internal::*;
|
pub use self::internal::*;
|
||||||
pub use self::response_error::ResponseError;
|
pub use self::response_error::ResponseError;
|
||||||
|
pub(crate) use macros::{downcast_dyn, downcast_get_type_id};
|
||||||
|
|
||||||
/// A convenience [`Result`](std::result::Result) for Actix Web operations.
|
/// A convenience [`Result`](std::result::Result) for Actix Web operations.
|
||||||
///
|
///
|
||||||
|
|
|
@ -9,7 +9,7 @@ use std::{
|
||||||
use actix_http::{body::AnyBody, header, Response, StatusCode};
|
use actix_http::{body::AnyBody, header, Response, StatusCode};
|
||||||
use bytes::BytesMut;
|
use bytes::BytesMut;
|
||||||
|
|
||||||
use crate::{__downcast_dyn, __downcast_get_type_id};
|
use crate::error::{downcast_dyn, downcast_get_type_id};
|
||||||
use crate::{helpers, HttpResponse};
|
use crate::{helpers, HttpResponse};
|
||||||
|
|
||||||
/// Errors that can generate responses.
|
/// Errors that can generate responses.
|
||||||
|
@ -41,10 +41,10 @@ pub trait ResponseError: fmt::Debug + fmt::Display {
|
||||||
res.set_body(AnyBody::from(buf))
|
res.set_body(AnyBody::from(buf))
|
||||||
}
|
}
|
||||||
|
|
||||||
__downcast_get_type_id!();
|
downcast_get_type_id!();
|
||||||
}
|
}
|
||||||
|
|
||||||
__downcast_dyn!(ResponseError);
|
downcast_dyn!(ResponseError);
|
||||||
|
|
||||||
impl ResponseError for Box<dyn StdError + 'static> {}
|
impl ResponseError for Box<dyn StdError + 'static> {}
|
||||||
|
|
||||||
|
@ -57,6 +57,10 @@ impl ResponseError for serde::de::value::Error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl ResponseError for serde_json::Error {}
|
||||||
|
|
||||||
|
impl ResponseError for serde_urlencoded::ser::Error {}
|
||||||
|
|
||||||
impl ResponseError for std::str::Utf8Error {
|
impl ResponseError for std::str::Utf8Error {
|
||||||
fn status_code(&self) -> StatusCode {
|
fn status_code(&self) -> StatusCode {
|
||||||
StatusCode::BAD_REQUEST
|
StatusCode::BAD_REQUEST
|
||||||
|
|
|
@ -5,7 +5,7 @@ use mime::Mime;
|
||||||
use super::{qitem, QualityItem};
|
use super::{qitem, QualityItem};
|
||||||
use crate::http::header;
|
use crate::http::header;
|
||||||
|
|
||||||
crate::__define_common_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 [RFC7231](http://tools.ietf.org/html/rfc7231#section-5.3.2)
|
||||||
///
|
///
|
||||||
/// The `Accept` header field can be used by user agents to specify
|
/// The `Accept` header field can be used by user agents to specify
|
||||||
|
@ -81,14 +81,14 @@ crate::__define_common_header! {
|
||||||
|
|
||||||
test_accept {
|
test_accept {
|
||||||
// Tests from the RFC
|
// Tests from the RFC
|
||||||
crate::__common_header_test!(
|
crate::http::header::common_header_test!(
|
||||||
test1,
|
test1,
|
||||||
vec![b"audio/*; q=0.2, audio/basic"],
|
vec![b"audio/*; q=0.2, audio/basic"],
|
||||||
Some(Accept(vec![
|
Some(Accept(vec![
|
||||||
QualityItem::new("audio/*".parse().unwrap(), q(200)),
|
QualityItem::new("audio/*".parse().unwrap(), q(200)),
|
||||||
qitem("audio/basic".parse().unwrap()),
|
qitem("audio/basic".parse().unwrap()),
|
||||||
])));
|
])));
|
||||||
crate::__common_header_test!(
|
crate::http::header::common_header_test!(
|
||||||
test2,
|
test2,
|
||||||
vec![b"text/plain; q=0.5, text/html, text/x-dvi; q=0.8, text/x-c"],
|
vec![b"text/plain; q=0.5, text/html, text/x-dvi; q=0.8, text/x-c"],
|
||||||
Some(Accept(vec![
|
Some(Accept(vec![
|
||||||
|
@ -100,13 +100,13 @@ crate::__define_common_header! {
|
||||||
qitem("text/x-c".parse().unwrap()),
|
qitem("text/x-c".parse().unwrap()),
|
||||||
])));
|
])));
|
||||||
// Custom tests
|
// Custom tests
|
||||||
crate::__common_header_test!(
|
crate::http::header::common_header_test!(
|
||||||
test3,
|
test3,
|
||||||
vec![b"text/plain; charset=utf-8"],
|
vec![b"text/plain; charset=utf-8"],
|
||||||
Some(Accept(vec![
|
Some(Accept(vec![
|
||||||
qitem(mime::TEXT_PLAIN_UTF_8),
|
qitem(mime::TEXT_PLAIN_UTF_8),
|
||||||
])));
|
])));
|
||||||
crate::__common_header_test!(
|
crate::http::header::common_header_test!(
|
||||||
test4,
|
test4,
|
||||||
vec![b"text/plain; charset=utf-8; q=0.5"],
|
vec![b"text/plain; charset=utf-8; q=0.5"],
|
||||||
Some(Accept(vec
|
/// [RFC7231](http://tools.ietf.org/html/rfc7231#section-5.3.3)
|
||||||
///
|
///
|
||||||
|
@ -57,6 +57,6 @@ crate::__define_common_header! {
|
||||||
|
|
||||||
test_accept_charset {
|
test_accept_charset {
|
||||||
// Test case from RFC
|
// Test case from RFC
|
||||||
crate::__common_header_test!(test1, vec![b"iso-8859-5, unicode-1-1;q=0.8"]);
|
crate::http::header::common_header_test!(test1, vec![b"iso-8859-5, unicode-1-1;q=0.8"]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -64,12 +64,12 @@ header! {
|
||||||
|
|
||||||
test_accept_encoding {
|
test_accept_encoding {
|
||||||
// From the RFC
|
// From the RFC
|
||||||
crate::__common_header_test!(test1, vec![b"compress, gzip"]);
|
crate::http::header::common_header_test!(test1, vec![b"compress, gzip"]);
|
||||||
crate::__common_header_test!(test2, vec![b""], Some(AcceptEncoding(vec![])));
|
crate::http::header::common_header_test!(test2, vec![b""], Some(AcceptEncoding(vec![])));
|
||||||
crate::__common_header_test!(test3, vec![b"*"]);
|
crate::http::header::common_header_test!(test3, vec![b"*"]);
|
||||||
// Note: Removed quality 1 from gzip
|
// Note: Removed quality 1 from gzip
|
||||||
crate::__common_header_test!(test4, vec![b"compress;q=0.5, gzip"]);
|
crate::http::header::common_header_test!(test4, vec![b"compress;q=0.5, gzip"]);
|
||||||
// Note: Removed quality 1 from gzip
|
// Note: Removed quality 1 from gzip
|
||||||
crate::__common_header_test!(test5, vec![b"gzip, identity; q=0.5, *;q=0"]);
|
crate::http::header::common_header_test!(test5, vec![b"gzip, identity; q=0.5, *;q=0"]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@ use language_tags::LanguageTag;
|
||||||
|
|
||||||
use super::{QualityItem, ACCEPT_LANGUAGE};
|
use super::{QualityItem, ACCEPT_LANGUAGE};
|
||||||
|
|
||||||
crate::__define_common_header! {
|
crate::http::header::common_header! {
|
||||||
/// `Accept-Language` header, defined in
|
/// `Accept-Language` header, defined in
|
||||||
/// [RFC7231](http://tools.ietf.org/html/rfc7231#section-5.3.5)
|
/// [RFC7231](http://tools.ietf.org/html/rfc7231#section-5.3.5)
|
||||||
///
|
///
|
||||||
|
@ -53,9 +53,9 @@ crate::__define_common_header! {
|
||||||
|
|
||||||
test_accept_language {
|
test_accept_language {
|
||||||
// From the RFC
|
// From the RFC
|
||||||
crate::__common_header_test!(test1, vec![b"da, en-gb;q=0.8, en;q=0.7"]);
|
crate::http::header::common_header_test!(test1, vec![b"da, en-gb;q=0.8, en;q=0.7"]);
|
||||||
// Own test
|
// Own test
|
||||||
crate::__common_header_test!(
|
crate::http::header::common_header_test!(
|
||||||
test2, vec![b"en-US, en; q=0.5, fr"],
|
test2, vec![b"en-US, en; q=0.5, fr"],
|
||||||
Some(AcceptLanguage(vec
|
/// `Allow` header, defined in [RFC7231](http://tools.ietf.org/html/rfc7231#section-7.4.1)
|
||||||
///
|
///
|
||||||
/// The `Allow` header field lists the set of methods advertised as
|
/// The `Allow` header field lists the set of methods advertised as
|
||||||
|
@ -49,12 +49,12 @@ crate::__define_common_header! {
|
||||||
|
|
||||||
test_allow {
|
test_allow {
|
||||||
// From the RFC
|
// From the RFC
|
||||||
crate::__common_header_test!(
|
crate::http::header::common_header_test!(
|
||||||
test1,
|
test1,
|
||||||
vec![b"GET, HEAD, PUT"],
|
vec![b"GET, HEAD, PUT"],
|
||||||
Some(HeaderField(vec![Method::GET, Method::HEAD, Method::PUT])));
|
Some(HeaderField(vec![Method::GET, Method::HEAD, Method::PUT])));
|
||||||
// Own tests
|
// Own tests
|
||||||
crate::__common_header_test!(
|
crate::http::header::common_header_test!(
|
||||||
test2,
|
test2,
|
||||||
vec![b"OPTIONS, GET, PUT, POST, DELETE, HEAD, TRACE, CONNECT, PATCH"],
|
vec![b"OPTIONS, GET, PUT, POST, DELETE, HEAD, TRACE, CONNECT, PATCH"],
|
||||||
Some(HeaderField(vec![
|
Some(HeaderField(vec![
|
||||||
|
@ -67,7 +67,7 @@ crate::__define_common_header! {
|
||||||
Method::TRACE,
|
Method::TRACE,
|
||||||
Method::CONNECT,
|
Method::CONNECT,
|
||||||
Method::PATCH])));
|
Method::PATCH])));
|
||||||
crate::__common_header_test!(
|
crate::http::header::common_header_test!(
|
||||||
test3,
|
test3,
|
||||||
vec![b""],
|
vec![b""],
|
||||||
Some(HeaderField(Vec::<Method>::new())));
|
Some(HeaderField(Vec::<Method>::new())));
|
||||||
|
|
|
@ -49,9 +49,9 @@ use crate::http::header;
|
||||||
#[derive(PartialEq, Clone, Debug)]
|
#[derive(PartialEq, Clone, Debug)]
|
||||||
pub struct CacheControl(pub Vec<CacheDirective>);
|
pub struct CacheControl(pub Vec<CacheDirective>);
|
||||||
|
|
||||||
crate::__common_header_deref!(CacheControl => Vec<CacheDirective>);
|
crate::http::header::common_header_deref!(CacheControl => Vec<CacheDirective>);
|
||||||
|
|
||||||
// TODO: this could just be the __define_common_header! macro
|
// TODO: this could just be the crate::http::header::common_header! macro
|
||||||
impl Header for CacheControl {
|
impl Header for CacheControl {
|
||||||
fn name() -> header::HeaderName {
|
fn name() -> header::HeaderName {
|
||||||
header::CACHE_CONTROL
|
header::CACHE_CONTROL
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use super::{QualityItem, CONTENT_LANGUAGE};
|
use super::{QualityItem, CONTENT_LANGUAGE};
|
||||||
use language_tags::LanguageTag;
|
use language_tags::LanguageTag;
|
||||||
|
|
||||||
crate::__define_common_header! {
|
crate::http::header::common_header! {
|
||||||
/// `Content-Language` header, defined in
|
/// `Content-Language` header, defined in
|
||||||
/// [RFC7231](https://tools.ietf.org/html/rfc7231#section-3.1.3.2)
|
/// [RFC7231](https://tools.ietf.org/html/rfc7231#section-3.1.3.2)
|
||||||
///
|
///
|
||||||
|
@ -50,7 +50,7 @@ crate::__define_common_header! {
|
||||||
(ContentLanguage, CONTENT_LANGUAGE) => (QualityItem<LanguageTag>)+
|
(ContentLanguage, CONTENT_LANGUAGE) => (QualityItem<LanguageTag>)+
|
||||||
|
|
||||||
test_content_language {
|
test_content_language {
|
||||||
crate::__common_header_test!(test1, vec![b"da"]);
|
crate::http::header::common_header_test!(test1, vec![b"da"]);
|
||||||
crate::__common_header_test!(test2, vec![b"mi, en"]);
|
crate::http::header::common_header_test!(test2, vec![b"mi, en"]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,65 +4,65 @@ use std::str::FromStr;
|
||||||
use super::{HeaderValue, IntoHeaderValue, InvalidHeaderValue, Writer, CONTENT_RANGE};
|
use super::{HeaderValue, IntoHeaderValue, InvalidHeaderValue, Writer, CONTENT_RANGE};
|
||||||
use crate::error::ParseError;
|
use crate::error::ParseError;
|
||||||
|
|
||||||
crate::__define_common_header! {
|
crate::http::header::common_header! {
|
||||||
/// `Content-Range` header, defined in
|
/// `Content-Range` header, defined in
|
||||||
/// [RFC7233](http://tools.ietf.org/html/rfc7233#section-4.2)
|
/// [RFC7233](http://tools.ietf.org/html/rfc7233#section-4.2)
|
||||||
(ContentRange, CONTENT_RANGE) => [ContentRangeSpec]
|
(ContentRange, CONTENT_RANGE) => [ContentRangeSpec]
|
||||||
|
|
||||||
test_content_range {
|
test_content_range {
|
||||||
crate::__common_header_test!(test_bytes,
|
crate::http::header::common_header_test!(test_bytes,
|
||||||
vec![b"bytes 0-499/500"],
|
vec![b"bytes 0-499/500"],
|
||||||
Some(ContentRange(ContentRangeSpec::Bytes {
|
Some(ContentRange(ContentRangeSpec::Bytes {
|
||||||
range: Some((0, 499)),
|
range: Some((0, 499)),
|
||||||
instance_length: Some(500)
|
instance_length: Some(500)
|
||||||
})));
|
})));
|
||||||
|
|
||||||
crate::__common_header_test!(test_bytes_unknown_len,
|
crate::http::header::common_header_test!(test_bytes_unknown_len,
|
||||||
vec![b"bytes 0-499/*"],
|
vec![b"bytes 0-499/*"],
|
||||||
Some(ContentRange(ContentRangeSpec::Bytes {
|
Some(ContentRange(ContentRangeSpec::Bytes {
|
||||||
range: Some((0, 499)),
|
range: Some((0, 499)),
|
||||||
instance_length: None
|
instance_length: None
|
||||||
})));
|
})));
|
||||||
|
|
||||||
crate::__common_header_test!(test_bytes_unknown_range,
|
crate::http::header::common_header_test!(test_bytes_unknown_range,
|
||||||
vec![b"bytes */500"],
|
vec![b"bytes */500"],
|
||||||
Some(ContentRange(ContentRangeSpec::Bytes {
|
Some(ContentRange(ContentRangeSpec::Bytes {
|
||||||
range: None,
|
range: None,
|
||||||
instance_length: Some(500)
|
instance_length: Some(500)
|
||||||
})));
|
})));
|
||||||
|
|
||||||
crate::__common_header_test!(test_unregistered,
|
crate::http::header::common_header_test!(test_unregistered,
|
||||||
vec![b"seconds 1-2"],
|
vec![b"seconds 1-2"],
|
||||||
Some(ContentRange(ContentRangeSpec::Unregistered {
|
Some(ContentRange(ContentRangeSpec::Unregistered {
|
||||||
unit: "seconds".to_owned(),
|
unit: "seconds".to_owned(),
|
||||||
resp: "1-2".to_owned()
|
resp: "1-2".to_owned()
|
||||||
})));
|
})));
|
||||||
|
|
||||||
crate::__common_header_test!(test_no_len,
|
crate::http::header::common_header_test!(test_no_len,
|
||||||
vec![b"bytes 0-499"],
|
vec![b"bytes 0-499"],
|
||||||
None::<ContentRange>);
|
None::<ContentRange>);
|
||||||
|
|
||||||
crate::__common_header_test!(test_only_unit,
|
crate::http::header::common_header_test!(test_only_unit,
|
||||||
vec![b"bytes"],
|
vec![b"bytes"],
|
||||||
None::<ContentRange>);
|
None::<ContentRange>);
|
||||||
|
|
||||||
crate::__common_header_test!(test_end_less_than_start,
|
crate::http::header::common_header_test!(test_end_less_than_start,
|
||||||
vec![b"bytes 499-0/500"],
|
vec![b"bytes 499-0/500"],
|
||||||
None::<ContentRange>);
|
None::<ContentRange>);
|
||||||
|
|
||||||
crate::__common_header_test!(test_blank,
|
crate::http::header::common_header_test!(test_blank,
|
||||||
vec![b""],
|
vec![b""],
|
||||||
None::<ContentRange>);
|
None::<ContentRange>);
|
||||||
|
|
||||||
crate::__common_header_test!(test_bytes_many_spaces,
|
crate::http::header::common_header_test!(test_bytes_many_spaces,
|
||||||
vec![b"bytes 1-2/500 3"],
|
vec![b"bytes 1-2/500 3"],
|
||||||
None::<ContentRange>);
|
None::<ContentRange>);
|
||||||
|
|
||||||
crate::__common_header_test!(test_bytes_many_slashes,
|
crate::http::header::common_header_test!(test_bytes_many_slashes,
|
||||||
vec![b"bytes 1-2/500/600"],
|
vec![b"bytes 1-2/500/600"],
|
||||||
None::<ContentRange>);
|
None::<ContentRange>);
|
||||||
|
|
||||||
crate::__common_header_test!(test_bytes_many_dashes,
|
crate::http::header::common_header_test!(test_bytes_many_dashes,
|
||||||
vec![b"bytes 1-2-3/500"],
|
vec![b"bytes 1-2-3/500"],
|
||||||
None::<ContentRange>);
|
None::<ContentRange>);
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use super::CONTENT_TYPE;
|
use super::CONTENT_TYPE;
|
||||||
use mime::Mime;
|
use mime::Mime;
|
||||||
|
|
||||||
crate::__define_common_header! {
|
crate::http::header::common_header! {
|
||||||
/// `Content-Type` header, defined in
|
/// `Content-Type` header, defined in
|
||||||
/// [RFC7231](http://tools.ietf.org/html/rfc7231#section-3.1.1.5)
|
/// [RFC7231](http://tools.ietf.org/html/rfc7231#section-3.1.1.5)
|
||||||
///
|
///
|
||||||
|
@ -52,7 +52,7 @@ crate::__define_common_header! {
|
||||||
(ContentType, CONTENT_TYPE) => [Mime]
|
(ContentType, CONTENT_TYPE) => [Mime]
|
||||||
|
|
||||||
test_content_type {
|
test_content_type {
|
||||||
crate::__common_header_test!(
|
crate::http::header::common_header_test!(
|
||||||
test1,
|
test1,
|
||||||
vec![b"text/html"],
|
vec![b"text/html"],
|
||||||
Some(HeaderField(mime::TEXT_HTML)));
|
Some(HeaderField(mime::TEXT_HTML)));
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use super::{HttpDate, DATE};
|
use super::{HttpDate, DATE};
|
||||||
use std::time::SystemTime;
|
use std::time::SystemTime;
|
||||||
|
|
||||||
crate::__define_common_header! {
|
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 [RFC7231](http://tools.ietf.org/html/rfc7231#section-7.1.1.2)
|
||||||
///
|
///
|
||||||
/// The `Date` header field represents the date and time at which the
|
/// The `Date` header field represents the date and time at which the
|
||||||
|
@ -32,7 +32,7 @@ crate::__define_common_header! {
|
||||||
(Date, DATE) => [HttpDate]
|
(Date, DATE) => [HttpDate]
|
||||||
|
|
||||||
test_date {
|
test_date {
|
||||||
crate::__common_header_test!(test1, vec![b"Tue, 15 Nov 1994 08:12:31 GMT"]);
|
crate::http::header::common_header_test!(test1, vec![b"Tue, 15 Nov 1994 08:12:31 GMT"]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use super::{EntityTag, ETAG};
|
use super::{EntityTag, ETAG};
|
||||||
|
|
||||||
crate::__define_common_header! {
|
crate::http::header::common_header! {
|
||||||
/// `ETag` header, defined in [RFC7232](http://tools.ietf.org/html/rfc7232#section-2.3)
|
/// `ETag` header, defined in [RFC7232](http://tools.ietf.org/html/rfc7232#section-2.3)
|
||||||
///
|
///
|
||||||
/// The `ETag` header field in a response provides the current entity-tag
|
/// The `ETag` header field in a response provides the current entity-tag
|
||||||
|
@ -50,50 +50,50 @@ crate::__define_common_header! {
|
||||||
|
|
||||||
test_etag {
|
test_etag {
|
||||||
// From the RFC
|
// From the RFC
|
||||||
crate::__common_header_test!(test1,
|
crate::http::header::common_header_test!(test1,
|
||||||
vec![b"\"xyzzy\""],
|
vec![b"\"xyzzy\""],
|
||||||
Some(ETag(EntityTag::new(false, "xyzzy".to_owned()))));
|
Some(ETag(EntityTag::new(false, "xyzzy".to_owned()))));
|
||||||
crate::__common_header_test!(test2,
|
crate::http::header::common_header_test!(test2,
|
||||||
vec![b"W/\"xyzzy\""],
|
vec![b"W/\"xyzzy\""],
|
||||||
Some(ETag(EntityTag::new(true, "xyzzy".to_owned()))));
|
Some(ETag(EntityTag::new(true, "xyzzy".to_owned()))));
|
||||||
crate::__common_header_test!(test3,
|
crate::http::header::common_header_test!(test3,
|
||||||
vec![b"\"\""],
|
vec![b"\"\""],
|
||||||
Some(ETag(EntityTag::new(false, "".to_owned()))));
|
Some(ETag(EntityTag::new(false, "".to_owned()))));
|
||||||
// Own tests
|
// Own tests
|
||||||
crate::__common_header_test!(test4,
|
crate::http::header::common_header_test!(test4,
|
||||||
vec![b"\"foobar\""],
|
vec![b"\"foobar\""],
|
||||||
Some(ETag(EntityTag::new(false, "foobar".to_owned()))));
|
Some(ETag(EntityTag::new(false, "foobar".to_owned()))));
|
||||||
crate::__common_header_test!(test5,
|
crate::http::header::common_header_test!(test5,
|
||||||
vec![b"\"\""],
|
vec![b"\"\""],
|
||||||
Some(ETag(EntityTag::new(false, "".to_owned()))));
|
Some(ETag(EntityTag::new(false, "".to_owned()))));
|
||||||
crate::__common_header_test!(test6,
|
crate::http::header::common_header_test!(test6,
|
||||||
vec![b"W/\"weak-etag\""],
|
vec![b"W/\"weak-etag\""],
|
||||||
Some(ETag(EntityTag::new(true, "weak-etag".to_owned()))));
|
Some(ETag(EntityTag::new(true, "weak-etag".to_owned()))));
|
||||||
crate::__common_header_test!(test7,
|
crate::http::header::common_header_test!(test7,
|
||||||
vec![b"W/\"\x65\x62\""],
|
vec![b"W/\"\x65\x62\""],
|
||||||
Some(ETag(EntityTag::new(true, "\u{0065}\u{0062}".to_owned()))));
|
Some(ETag(EntityTag::new(true, "\u{0065}\u{0062}".to_owned()))));
|
||||||
crate::__common_header_test!(test8,
|
crate::http::header::common_header_test!(test8,
|
||||||
vec![b"W/\"\""],
|
vec![b"W/\"\""],
|
||||||
Some(ETag(EntityTag::new(true, "".to_owned()))));
|
Some(ETag(EntityTag::new(true, "".to_owned()))));
|
||||||
crate::__common_header_test!(test9,
|
crate::http::header::common_header_test!(test9,
|
||||||
vec![b"no-dquotes"],
|
vec![b"no-dquotes"],
|
||||||
None::<ETag>);
|
None::<ETag>);
|
||||||
crate::__common_header_test!(test10,
|
crate::http::header::common_header_test!(test10,
|
||||||
vec![b"w/\"the-first-w-is-case-sensitive\""],
|
vec![b"w/\"the-first-w-is-case-sensitive\""],
|
||||||
None::<ETag>);
|
None::<ETag>);
|
||||||
crate::__common_header_test!(test11,
|
crate::http::header::common_header_test!(test11,
|
||||||
vec![b""],
|
vec![b""],
|
||||||
None::<ETag>);
|
None::<ETag>);
|
||||||
crate::__common_header_test!(test12,
|
crate::http::header::common_header_test!(test12,
|
||||||
vec![b"\"unmatched-dquotes1"],
|
vec![b"\"unmatched-dquotes1"],
|
||||||
None::<ETag>);
|
None::<ETag>);
|
||||||
crate::__common_header_test!(test13,
|
crate::http::header::common_header_test!(test13,
|
||||||
vec![b"unmatched-dquotes2\""],
|
vec![b"unmatched-dquotes2\""],
|
||||||
None::<ETag>);
|
None::<ETag>);
|
||||||
crate::__common_header_test!(test14,
|
crate::http::header::common_header_test!(test14,
|
||||||
vec![b"matched-\"dquotes\""],
|
vec![b"matched-\"dquotes\""],
|
||||||
None::<ETag>);
|
None::<ETag>);
|
||||||
crate::__common_header_test!(test15,
|
crate::http::header::common_header_test!(test15,
|
||||||
vec![b"\""],
|
vec![b"\""],
|
||||||
None::<ETag>);
|
None::<ETag>);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use super::{HttpDate, EXPIRES};
|
use super::{HttpDate, EXPIRES};
|
||||||
|
|
||||||
crate::__define_common_header! {
|
crate::http::header::common_header! {
|
||||||
/// `Expires` header, defined in [RFC7234](http://tools.ietf.org/html/rfc7234#section-5.3)
|
/// `Expires` header, defined in [RFC7234](http://tools.ietf.org/html/rfc7234#section-5.3)
|
||||||
///
|
///
|
||||||
/// The `Expires` header field gives the date/time after which the
|
/// The `Expires` header field gives the date/time after which the
|
||||||
|
@ -36,6 +36,6 @@ crate::__define_common_header! {
|
||||||
|
|
||||||
test_expires {
|
test_expires {
|
||||||
// Test case from RFC
|
// Test case from RFC
|
||||||
crate::__common_header_test!(test1, vec![b"Thu, 01 Dec 1994 16:00:00 GMT"]);
|
crate::http::header::common_header_test!(test1, vec![b"Thu, 01 Dec 1994 16:00:00 GMT"]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use super::{EntityTag, IF_MATCH};
|
use super::{EntityTag, IF_MATCH};
|
||||||
|
|
||||||
crate::__define_common_header! {
|
crate::http::header::common_header! {
|
||||||
/// `If-Match` header, defined in
|
/// `If-Match` header, defined in
|
||||||
/// [RFC7232](https://tools.ietf.org/html/rfc7232#section-3.1)
|
/// [RFC7232](https://tools.ietf.org/html/rfc7232#section-3.1)
|
||||||
///
|
///
|
||||||
|
@ -53,18 +53,18 @@ crate::__define_common_header! {
|
||||||
(IfMatch, IF_MATCH) => {Any / (EntityTag)+}
|
(IfMatch, IF_MATCH) => {Any / (EntityTag)+}
|
||||||
|
|
||||||
test_if_match {
|
test_if_match {
|
||||||
crate::__common_header_test!(
|
crate::http::header::common_header_test!(
|
||||||
test1,
|
test1,
|
||||||
vec![b"\"xyzzy\""],
|
vec![b"\"xyzzy\""],
|
||||||
Some(HeaderField::Items(
|
Some(HeaderField::Items(
|
||||||
vec![EntityTag::new(false, "xyzzy".to_owned())])));
|
vec![EntityTag::new(false, "xyzzy".to_owned())])));
|
||||||
crate::__common_header_test!(
|
crate::http::header::common_header_test!(
|
||||||
test2,
|
test2,
|
||||||
vec![b"\"xyzzy\", \"r2d2xxxx\", \"c3piozzzz\""],
|
vec![b"\"xyzzy\", \"r2d2xxxx\", \"c3piozzzz\""],
|
||||||
Some(HeaderField::Items(
|
Some(HeaderField::Items(
|
||||||
vec![EntityTag::new(false, "xyzzy".to_owned()),
|
vec![EntityTag::new(false, "xyzzy".to_owned()),
|
||||||
EntityTag::new(false, "r2d2xxxx".to_owned()),
|
EntityTag::new(false, "r2d2xxxx".to_owned()),
|
||||||
EntityTag::new(false, "c3piozzzz".to_owned())])));
|
EntityTag::new(false, "c3piozzzz".to_owned())])));
|
||||||
crate::__common_header_test!(test3, vec![b"*"], Some(IfMatch::Any));
|
crate::http::header::common_header_test!(test3, vec![b"*"], Some(IfMatch::Any));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use super::{HttpDate, IF_MODIFIED_SINCE};
|
use super::{HttpDate, IF_MODIFIED_SINCE};
|
||||||
|
|
||||||
crate::__define_common_header! {
|
crate::http::header::common_header! {
|
||||||
/// `If-Modified-Since` header, defined in
|
/// `If-Modified-Since` header, defined in
|
||||||
/// [RFC7232](http://tools.ietf.org/html/rfc7232#section-3.3)
|
/// [RFC7232](http://tools.ietf.org/html/rfc7232#section-3.3)
|
||||||
///
|
///
|
||||||
|
@ -36,6 +36,6 @@ crate::__define_common_header! {
|
||||||
|
|
||||||
test_if_modified_since {
|
test_if_modified_since {
|
||||||
// Test case from RFC
|
// Test case from RFC
|
||||||
crate::__common_header_test!(test1, vec![b"Sat, 29 Oct 1994 19:43:31 GMT"]);
|
crate::http::header::common_header_test!(test1, vec![b"Sat, 29 Oct 1994 19:43:31 GMT"]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use super::{EntityTag, IF_NONE_MATCH};
|
use super::{EntityTag, IF_NONE_MATCH};
|
||||||
|
|
||||||
crate::__define_common_header! {
|
crate::http::header::common_header! {
|
||||||
/// `If-None-Match` header, defined in
|
/// `If-None-Match` header, defined in
|
||||||
/// [RFC7232](https://tools.ietf.org/html/rfc7232#section-3.2)
|
/// [RFC7232](https://tools.ietf.org/html/rfc7232#section-3.2)
|
||||||
///
|
///
|
||||||
|
@ -55,11 +55,11 @@ crate::__define_common_header! {
|
||||||
(IfNoneMatch, IF_NONE_MATCH) => {Any / (EntityTag)+}
|
(IfNoneMatch, IF_NONE_MATCH) => {Any / (EntityTag)+}
|
||||||
|
|
||||||
test_if_none_match {
|
test_if_none_match {
|
||||||
crate::__common_header_test!(test1, vec![b"\"xyzzy\""]);
|
crate::http::header::common_header_test!(test1, vec![b"\"xyzzy\""]);
|
||||||
crate::__common_header_test!(test2, vec![b"W/\"xyzzy\""]);
|
crate::http::header::common_header_test!(test2, vec![b"W/\"xyzzy\""]);
|
||||||
crate::__common_header_test!(test3, vec![b"\"xyzzy\", \"r2d2xxxx\", \"c3piozzzz\""]);
|
crate::http::header::common_header_test!(test3, vec![b"\"xyzzy\", \"r2d2xxxx\", \"c3piozzzz\""]);
|
||||||
crate::__common_header_test!(test4, vec![b"W/\"xyzzy\", W/\"r2d2xxxx\", W/\"c3piozzzz\""]);
|
crate::http::header::common_header_test!(test4, vec![b"W/\"xyzzy\", W/\"r2d2xxxx\", W/\"c3piozzzz\""]);
|
||||||
crate::__common_header_test!(test5, vec![b"*"]);
|
crate::http::header::common_header_test!(test5, vec![b"*"]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -113,7 +113,7 @@ mod test_if_range {
|
||||||
use crate::http::header::*;
|
use crate::http::header::*;
|
||||||
use std::str;
|
use std::str;
|
||||||
|
|
||||||
crate::__common_header_test!(test1, vec![b"Sat, 29 Oct 1994 19:43:31 GMT"]);
|
crate::http::header::common_header_test!(test1, vec![b"Sat, 29 Oct 1994 19:43:31 GMT"]);
|
||||||
crate::__common_header_test!(test2, vec![b"\"abc\""]);
|
crate::http::header::common_header_test!(test2, vec![b"\"abc\""]);
|
||||||
crate::__common_header_test!(test3, vec![b"this-is-invalid"], None::<IfRange>);
|
crate::http::header::common_header_test!(test3, vec![b"this-is-invalid"], None::<IfRange>);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use super::{HttpDate, IF_UNMODIFIED_SINCE};
|
use super::{HttpDate, IF_UNMODIFIED_SINCE};
|
||||||
|
|
||||||
crate::__define_common_header! {
|
crate::http::header::common_header! {
|
||||||
/// `If-Unmodified-Since` header, defined in
|
/// `If-Unmodified-Since` header, defined in
|
||||||
/// [RFC7232](http://tools.ietf.org/html/rfc7232#section-3.4)
|
/// [RFC7232](http://tools.ietf.org/html/rfc7232#section-3.4)
|
||||||
///
|
///
|
||||||
|
@ -37,6 +37,6 @@ crate::__define_common_header! {
|
||||||
|
|
||||||
test_if_unmodified_since {
|
test_if_unmodified_since {
|
||||||
// Test case from RFC
|
// Test case from RFC
|
||||||
crate::__common_header_test!(test1, vec![b"Sat, 29 Oct 1994 19:43:31 GMT"]);
|
crate::http::header::common_header_test!(test1, vec![b"Sat, 29 Oct 1994 19:43:31 GMT"]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use super::{HttpDate, LAST_MODIFIED};
|
use super::{HttpDate, LAST_MODIFIED};
|
||||||
|
|
||||||
crate::__define_common_header! {
|
crate::http::header::common_header! {
|
||||||
/// `Last-Modified` header, defined in
|
/// `Last-Modified` header, defined in
|
||||||
/// [RFC7232](http://tools.ietf.org/html/rfc7232#section-2.2)
|
/// [RFC7232](http://tools.ietf.org/html/rfc7232#section-2.2)
|
||||||
///
|
///
|
||||||
|
@ -36,6 +36,6 @@ crate::__define_common_header! {
|
||||||
|
|
||||||
test_last_modified {
|
test_last_modified {
|
||||||
// Test case from RFC
|
// Test case from RFC
|
||||||
crate::__common_header_test!(test1, vec![b"Sat, 29 Oct 1994 19:43:31 GMT"]);
|
crate::http::header::common_header_test!(test1, vec![b"Sat, 29 Oct 1994 19:43:31 GMT"]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,4 @@
|
||||||
#[doc(hidden)]
|
macro_rules! common_header_deref {
|
||||||
#[macro_export]
|
|
||||||
macro_rules! __common_header_deref {
|
|
||||||
($from:ty => $to:ty) => {
|
($from:ty => $to:ty) => {
|
||||||
impl ::std::ops::Deref for $from {
|
impl ::std::ops::Deref for $from {
|
||||||
type Target = $to;
|
type Target = $to;
|
||||||
|
@ -20,9 +18,7 @@ macro_rules! __common_header_deref {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc(hidden)]
|
macro_rules! common_header_test_module {
|
||||||
#[macro_export]
|
|
||||||
macro_rules! __common_header_test_module {
|
|
||||||
($id:ident, $tm:ident{$($tf:item)*}) => {
|
($id:ident, $tm:ident{$($tf:item)*}) => {
|
||||||
#[allow(unused_imports)]
|
#[allow(unused_imports)]
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -37,9 +33,8 @@ macro_rules! __common_header_test_module {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc(hidden)]
|
#[cfg(test)]
|
||||||
#[macro_export]
|
macro_rules! common_header_test {
|
||||||
macro_rules! __common_header_test {
|
|
||||||
($id:ident, $raw:expr) => {
|
($id:ident, $raw:expr) => {
|
||||||
#[test]
|
#[test]
|
||||||
fn $id() {
|
fn $id() {
|
||||||
|
@ -99,9 +94,7 @@ macro_rules! __common_header_test {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc(hidden)]
|
macro_rules! common_header {
|
||||||
#[macro_export]
|
|
||||||
macro_rules! __define_common_header {
|
|
||||||
// $a:meta: Attributes associated with the header item (usually docs)
|
// $a:meta: Attributes associated with the header item (usually docs)
|
||||||
// $id:ident: Identifier of the header
|
// $id:ident: Identifier of the header
|
||||||
// $n:expr: Lowercase name of the header
|
// $n:expr: Lowercase name of the header
|
||||||
|
@ -112,7 +105,7 @@ macro_rules! __define_common_header {
|
||||||
$(#[$a])*
|
$(#[$a])*
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
pub struct $id(pub Vec<$item>);
|
pub struct $id(pub Vec<$item>);
|
||||||
crate::__common_header_deref!($id => Vec<$item>);
|
crate::http::header::common_header_deref!($id => Vec<$item>);
|
||||||
impl $crate::http::header::Header for $id {
|
impl $crate::http::header::Header for $id {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn name() -> $crate::http::header::HeaderName {
|
fn name() -> $crate::http::header::HeaderName {
|
||||||
|
@ -148,7 +141,7 @@ macro_rules! __define_common_header {
|
||||||
$(#[$a])*
|
$(#[$a])*
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
pub struct $id(pub Vec<$item>);
|
pub struct $id(pub Vec<$item>);
|
||||||
crate::__common_header_deref!($id => Vec<$item>);
|
crate::http::header::common_header_deref!($id => Vec<$item>);
|
||||||
impl $crate::http::header::Header for $id {
|
impl $crate::http::header::Header for $id {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn name() -> $crate::http::header::HeaderName {
|
fn name() -> $crate::http::header::HeaderName {
|
||||||
|
@ -184,7 +177,7 @@ macro_rules! __define_common_header {
|
||||||
$(#[$a])*
|
$(#[$a])*
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
pub struct $id(pub $value);
|
pub struct $id(pub $value);
|
||||||
crate::__common_header_deref!($id => $value);
|
crate::http::header::common_header_deref!($id => $value);
|
||||||
impl $crate::http::header::Header for $id {
|
impl $crate::http::header::Header for $id {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn name() -> $crate::http::header::HeaderName {
|
fn name() -> $crate::http::header::HeaderName {
|
||||||
|
@ -267,34 +260,39 @@ macro_rules! __define_common_header {
|
||||||
|
|
||||||
// optional test module
|
// optional test module
|
||||||
($(#[$a:meta])*($id:ident, $name:expr) => ($item:ty)* $tm:ident{$($tf:item)*}) => {
|
($(#[$a:meta])*($id:ident, $name:expr) => ($item:ty)* $tm:ident{$($tf:item)*}) => {
|
||||||
crate::__define_common_header! {
|
crate::http::header::common_header! {
|
||||||
$(#[$a])*
|
$(#[$a])*
|
||||||
($id, $name) => ($item)*
|
($id, $name) => ($item)*
|
||||||
}
|
}
|
||||||
|
|
||||||
crate::__common_header_test_module! { $id, $tm { $($tf)* }}
|
crate::http::header::common_header_test_module! { $id, $tm { $($tf)* }}
|
||||||
};
|
};
|
||||||
($(#[$a:meta])*($id:ident, $n:expr) => ($item:ty)+ $tm:ident{$($tf:item)*}) => {
|
($(#[$a:meta])*($id:ident, $n:expr) => ($item:ty)+ $tm:ident{$($tf:item)*}) => {
|
||||||
crate::__define_common_header! {
|
crate::http::header::common_header! {
|
||||||
$(#[$a])*
|
$(#[$a])*
|
||||||
($id, $n) => ($item)+
|
($id, $n) => ($item)+
|
||||||
}
|
}
|
||||||
|
|
||||||
crate::__common_header_test_module! { $id, $tm { $($tf)* }}
|
crate::http::header::common_header_test_module! { $id, $tm { $($tf)* }}
|
||||||
};
|
};
|
||||||
($(#[$a:meta])*($id:ident, $name:expr) => [$item:ty] $tm:ident{$($tf:item)*}) => {
|
($(#[$a:meta])*($id:ident, $name:expr) => [$item:ty] $tm:ident{$($tf:item)*}) => {
|
||||||
crate::__define_common_header! {
|
crate::http::header::common_header! {
|
||||||
$(#[$a])* ($id, $name) => [$item]
|
$(#[$a])* ($id, $name) => [$item]
|
||||||
}
|
}
|
||||||
|
|
||||||
crate::__common_header_test_module! { $id, $tm { $($tf)* }}
|
crate::http::header::common_header_test_module! { $id, $tm { $($tf)* }}
|
||||||
};
|
};
|
||||||
($(#[$a:meta])*($id:ident, $name:expr) => {Any / ($item:ty)+} $tm:ident{$($tf:item)*}) => {
|
($(#[$a:meta])*($id:ident, $name:expr) => {Any / ($item:ty)+} $tm:ident{$($tf:item)*}) => {
|
||||||
crate::__define_common_header! {
|
crate::http::header::common_header! {
|
||||||
$(#[$a])*
|
$(#[$a])*
|
||||||
($id, $name) => {Any / ($item)+}
|
($id, $name) => {Any / ($item)+}
|
||||||
}
|
}
|
||||||
|
|
||||||
crate::__common_header_test_module! { $id, $tm { $($tf)* }}
|
crate::http::header::common_header_test_module! { $id, $tm { $($tf)* }}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) use {common_header, common_header_deref, common_header_test_module};
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
pub(crate) use common_header_test;
|
||||||
|
|
|
@ -84,4 +84,8 @@ mod if_none_match;
|
||||||
mod if_range;
|
mod if_range;
|
||||||
mod if_unmodified_since;
|
mod if_unmodified_since;
|
||||||
mod last_modified;
|
mod last_modified;
|
||||||
|
|
||||||
mod macros;
|
mod macros;
|
||||||
|
#[cfg(test)]
|
||||||
|
pub(crate) use macros::common_header_test;
|
||||||
|
pub(crate) use macros::{common_header, common_header_deref, common_header_test_module};
|
||||||
|
|
|
@ -400,34 +400,28 @@ where
|
||||||
*rdef.name_mut() = name.clone();
|
*rdef.name_mut() = name.clone();
|
||||||
}
|
}
|
||||||
|
|
||||||
config.register_service(rdef, guards, self, None)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> IntoServiceFactory<T, ServiceRequest> for Resource<T>
|
|
||||||
where
|
|
||||||
T: ServiceFactory<
|
|
||||||
ServiceRequest,
|
|
||||||
Config = (),
|
|
||||||
Response = ServiceResponse,
|
|
||||||
Error = Error,
|
|
||||||
InitError = (),
|
|
||||||
>,
|
|
||||||
{
|
|
||||||
fn into_factory(self) -> T {
|
|
||||||
*self.factory_ref.borrow_mut() = Some(ResourceFactory {
|
*self.factory_ref.borrow_mut() = Some(ResourceFactory {
|
||||||
routes: self.routes,
|
routes: self.routes,
|
||||||
app_data: self.app_data.map(Rc::new),
|
|
||||||
default: self.default,
|
default: self.default,
|
||||||
});
|
});
|
||||||
|
|
||||||
self.endpoint
|
let resource_data = self.app_data.map(Rc::new);
|
||||||
|
|
||||||
|
// wraps endpoint service (including middleware) call and injects app data for this scope
|
||||||
|
let endpoint = apply_fn_factory(self.endpoint, move |mut req: ServiceRequest, srv| {
|
||||||
|
if let Some(ref data) = resource_data {
|
||||||
|
req.add_data_container(Rc::clone(data));
|
||||||
|
}
|
||||||
|
|
||||||
|
srv.call(req)
|
||||||
|
});
|
||||||
|
|
||||||
|
config.register_service(rdef, guards, endpoint, None)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ResourceFactory {
|
pub struct ResourceFactory {
|
||||||
routes: Vec<Route>,
|
routes: Vec<Route>,
|
||||||
app_data: Option<Rc<Extensions>>,
|
|
||||||
default: HttpNewService,
|
default: HttpNewService,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -446,8 +440,6 @@ impl ServiceFactory<ServiceRequest> for ResourceFactory {
|
||||||
// construct route service factory futures
|
// construct route service factory futures
|
||||||
let factory_fut = join_all(self.routes.iter().map(|route| route.new_service(())));
|
let factory_fut = join_all(self.routes.iter().map(|route| route.new_service(())));
|
||||||
|
|
||||||
let app_data = self.app_data.clone();
|
|
||||||
|
|
||||||
Box::pin(async move {
|
Box::pin(async move {
|
||||||
let default = default_fut.await?;
|
let default = default_fut.await?;
|
||||||
let routes = factory_fut
|
let routes = factory_fut
|
||||||
|
@ -455,18 +447,13 @@ impl ServiceFactory<ServiceRequest> for ResourceFactory {
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.collect::<Result<Vec<_>, _>>()?;
|
.collect::<Result<Vec<_>, _>>()?;
|
||||||
|
|
||||||
Ok(ResourceService {
|
Ok(ResourceService { routes, default })
|
||||||
routes,
|
|
||||||
app_data,
|
|
||||||
default,
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ResourceService {
|
pub struct ResourceService {
|
||||||
routes: Vec<RouteService>,
|
routes: Vec<RouteService>,
|
||||||
app_data: Option<Rc<Extensions>>,
|
|
||||||
default: HttpService,
|
default: HttpService,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -480,18 +467,10 @@ impl Service<ServiceRequest> for ResourceService {
|
||||||
fn call(&self, mut req: ServiceRequest) -> Self::Future {
|
fn call(&self, mut req: ServiceRequest) -> Self::Future {
|
||||||
for route in self.routes.iter() {
|
for route in self.routes.iter() {
|
||||||
if route.check(&mut req) {
|
if route.check(&mut req) {
|
||||||
if let Some(ref app_data) = self.app_data {
|
|
||||||
req.add_data_container(app_data.clone());
|
|
||||||
}
|
|
||||||
|
|
||||||
return route.call(req);
|
return route.call(req);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(ref app_data) = self.app_data {
|
|
||||||
req.add_data_container(app_data.clone());
|
|
||||||
}
|
|
||||||
|
|
||||||
self.default.call(req)
|
self.default.call(req)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -528,11 +507,14 @@ mod tests {
|
||||||
use actix_service::Service;
|
use actix_service::Service;
|
||||||
use actix_utils::future::ok;
|
use actix_utils::future::ok;
|
||||||
|
|
||||||
use crate::http::{header, HeaderValue, Method, StatusCode};
|
use crate::{
|
||||||
use crate::middleware::DefaultHeaders;
|
guard,
|
||||||
use crate::service::ServiceRequest;
|
http::{header, HeaderValue, Method, StatusCode},
|
||||||
use crate::test::{call_service, init_service, TestRequest};
|
middleware::DefaultHeaders,
|
||||||
use crate::{guard, web, App, Error, HttpResponse};
|
service::{ServiceRequest, ServiceResponse},
|
||||||
|
test::{call_service, init_service, TestRequest},
|
||||||
|
web, App, Error, HttpMessage, HttpResponse,
|
||||||
|
};
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_middleware() {
|
async fn test_middleware() {
|
||||||
|
@ -753,4 +735,39 @@ mod tests {
|
||||||
let resp = call_service(&srv, req).await;
|
let resp = call_service(&srv, req).await;
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_middleware_app_data() {
|
||||||
|
let srv = init_service(
|
||||||
|
App::new().service(
|
||||||
|
web::resource("test")
|
||||||
|
.app_data(1usize)
|
||||||
|
.wrap_fn(|req, srv| {
|
||||||
|
assert_eq!(req.app_data::<usize>(), Some(&1usize));
|
||||||
|
req.extensions_mut().insert(1usize);
|
||||||
|
srv.call(req)
|
||||||
|
})
|
||||||
|
.route(web::get().to(HttpResponse::Ok))
|
||||||
|
.default_service(|req: ServiceRequest| async move {
|
||||||
|
let (req, _) = req.into_parts();
|
||||||
|
|
||||||
|
assert_eq!(req.extensions().get::<usize>(), Some(&1));
|
||||||
|
|
||||||
|
Ok(ServiceResponse::new(
|
||||||
|
req,
|
||||||
|
HttpResponse::BadRequest().finish(),
|
||||||
|
))
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
let req = TestRequest::get().uri("/test").to_request();
|
||||||
|
let resp = call_service(&srv, req).await;
|
||||||
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
|
|
||||||
|
let req = TestRequest::post().uri("/test").to_request();
|
||||||
|
let resp = call_service(&srv, req).await;
|
||||||
|
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
83
src/scope.rs
83
src/scope.rs
|
@ -427,7 +427,6 @@ where
|
||||||
|
|
||||||
// complete scope pipeline creation
|
// complete scope pipeline creation
|
||||||
*self.factory_ref.borrow_mut() = Some(ScopeFactory {
|
*self.factory_ref.borrow_mut() = Some(ScopeFactory {
|
||||||
app_data: self.app_data.take().map(Rc::new),
|
|
||||||
default,
|
default,
|
||||||
services: cfg
|
services: cfg
|
||||||
.into_services()
|
.into_services()
|
||||||
|
@ -449,18 +448,28 @@ where
|
||||||
Some(self.guards)
|
Some(self.guards)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let scope_data = self.app_data.map(Rc::new);
|
||||||
|
|
||||||
|
// wraps endpoint service (including middleware) call and injects app data for this scope
|
||||||
|
let endpoint = apply_fn_factory(self.endpoint, move |mut req: ServiceRequest, srv| {
|
||||||
|
if let Some(ref data) = scope_data {
|
||||||
|
req.add_data_container(Rc::clone(data));
|
||||||
|
}
|
||||||
|
|
||||||
|
srv.call(req)
|
||||||
|
});
|
||||||
|
|
||||||
// register final service
|
// register final service
|
||||||
config.register_service(
|
config.register_service(
|
||||||
ResourceDef::root_prefix(&self.rdef),
|
ResourceDef::root_prefix(&self.rdef),
|
||||||
guards,
|
guards,
|
||||||
self.endpoint,
|
endpoint,
|
||||||
Some(Rc::new(rmap)),
|
Some(Rc::new(rmap)),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ScopeFactory {
|
pub struct ScopeFactory {
|
||||||
app_data: Option<Rc<Extensions>>,
|
|
||||||
services: Rc<[(ResourceDef, HttpNewService, RefCell<Option<Guards>>)]>,
|
services: Rc<[(ResourceDef, HttpNewService, RefCell<Option<Guards>>)]>,
|
||||||
default: Rc<HttpNewService>,
|
default: Rc<HttpNewService>,
|
||||||
}
|
}
|
||||||
|
@ -488,8 +497,6 @@ impl ServiceFactory<ServiceRequest> for ScopeFactory {
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
|
|
||||||
let app_data = self.app_data.clone();
|
|
||||||
|
|
||||||
Box::pin(async move {
|
Box::pin(async move {
|
||||||
let default = default_fut.await?;
|
let default = default_fut.await?;
|
||||||
|
|
||||||
|
@ -505,17 +512,12 @@ impl ServiceFactory<ServiceRequest> for ScopeFactory {
|
||||||
})
|
})
|
||||||
.finish();
|
.finish();
|
||||||
|
|
||||||
Ok(ScopeService {
|
Ok(ScopeService { router, default })
|
||||||
app_data,
|
|
||||||
router,
|
|
||||||
default,
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ScopeService {
|
pub struct ScopeService {
|
||||||
app_data: Option<Rc<Extensions>>,
|
|
||||||
router: Router<HttpService, Vec<Box<dyn Guard>>>,
|
router: Router<HttpService, Vec<Box<dyn Guard>>>,
|
||||||
default: HttpService,
|
default: HttpService,
|
||||||
}
|
}
|
||||||
|
@ -539,10 +541,6 @@ impl Service<ServiceRequest> for ScopeService {
|
||||||
true
|
true
|
||||||
});
|
});
|
||||||
|
|
||||||
if let Some(ref app_data) = self.app_data {
|
|
||||||
req.add_data_container(app_data.clone());
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Some((srv, _info)) = res {
|
if let Some((srv, _info)) = res {
|
||||||
srv.call(req)
|
srv.call(req)
|
||||||
} else {
|
} else {
|
||||||
|
@ -581,12 +579,15 @@ mod tests {
|
||||||
use actix_utils::future::ok;
|
use actix_utils::future::ok;
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
|
|
||||||
use crate::dev::Body;
|
use crate::{
|
||||||
use crate::http::{header, HeaderValue, Method, StatusCode};
|
dev::Body,
|
||||||
use crate::middleware::DefaultHeaders;
|
guard,
|
||||||
use crate::service::ServiceRequest;
|
http::{header, HeaderValue, Method, StatusCode},
|
||||||
use crate::test::{call_service, init_service, read_body, TestRequest};
|
middleware::DefaultHeaders,
|
||||||
use crate::{guard, web, App, HttpRequest, HttpResponse};
|
service::{ServiceRequest, ServiceResponse},
|
||||||
|
test::{call_service, init_service, read_body, TestRequest},
|
||||||
|
web, App, HttpMessage, HttpRequest, HttpResponse,
|
||||||
|
};
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_scope() {
|
async fn test_scope() {
|
||||||
|
@ -918,10 +919,7 @@ mod tests {
|
||||||
async fn test_default_resource_propagation() {
|
async fn test_default_resource_propagation() {
|
||||||
let srv = init_service(
|
let srv = init_service(
|
||||||
App::new()
|
App::new()
|
||||||
.service(
|
.service(web::scope("/app1").default_service(web::to(HttpResponse::BadRequest)))
|
||||||
web::scope("/app1")
|
|
||||||
.default_service(web::resource("").to(HttpResponse::BadRequest)),
|
|
||||||
)
|
|
||||||
.service(web::scope("/app2"))
|
.service(web::scope("/app2"))
|
||||||
.default_service(|r: ServiceRequest| {
|
.default_service(|r: ServiceRequest| {
|
||||||
ok(r.into_response(HttpResponse::MethodNotAllowed()))
|
ok(r.into_response(HttpResponse::MethodNotAllowed()))
|
||||||
|
@ -993,6 +991,41 @@ mod tests {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_middleware_app_data() {
|
||||||
|
let srv = init_service(
|
||||||
|
App::new().service(
|
||||||
|
web::scope("app")
|
||||||
|
.app_data(1usize)
|
||||||
|
.wrap_fn(|req, srv| {
|
||||||
|
assert_eq!(req.app_data::<usize>(), Some(&1usize));
|
||||||
|
req.extensions_mut().insert(1usize);
|
||||||
|
srv.call(req)
|
||||||
|
})
|
||||||
|
.route("/test", web::get().to(HttpResponse::Ok))
|
||||||
|
.default_service(|req: ServiceRequest| async move {
|
||||||
|
let (req, _) = req.into_parts();
|
||||||
|
|
||||||
|
assert_eq!(req.extensions().get::<usize>(), Some(&1));
|
||||||
|
|
||||||
|
Ok(ServiceResponse::new(
|
||||||
|
req,
|
||||||
|
HttpResponse::BadRequest().finish(),
|
||||||
|
))
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
let req = TestRequest::with_uri("/app/test").to_request();
|
||||||
|
let resp = call_service(&srv, req).await;
|
||||||
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
|
|
||||||
|
let req = TestRequest::with_uri("/app/default").to_request();
|
||||||
|
let resp = call_service(&srv, req).await;
|
||||||
|
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
||||||
|
}
|
||||||
|
|
||||||
// allow deprecated {App, Scope}::data
|
// allow deprecated {App, Scope}::data
|
||||||
#[allow(deprecated)]
|
#[allow(deprecated)]
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
|
|
Loading…
Reference in New Issue