1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-24 00:21:08 +01:00

Remove FromRequest::Config (#2233)

Co-authored-by: Jonas Platte <jplatte@users.noreply.github.com>
Co-authored-by: Igor Aleksanov <popzxc@yandex.ru>
Co-authored-by: Rob Ede <robjtede@icloud.com>
This commit is contained in:
Arniu Tseng 2021-09-11 08:11:16 +08:00 committed by GitHub
parent 46699e3429
commit 8ae278cb68
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 66 additions and 58 deletions

View File

@ -1,7 +1,10 @@
# Changes # Changes
## Unreleased - 2021-xx-xx ## Unreleased - 2021-xx-xx
### Changed
* Asscociated type `FromRequest::Config` was removed. [#2233]
[#2233]: https://github.com/actix/actix-web/pull/2233
## 4.0.0-beta.9 - 2021-09-09 ## 4.0.0-beta.9 - 2021-09-09
### Added ### Added

View File

@ -18,6 +18,7 @@ edition = "2018"
[package.metadata.docs.rs] [package.metadata.docs.rs]
# features that docs.rs will build with # features that docs.rs will build with
features = ["openssl", "rustls", "compress-brotli", "compress-gzip", "compress-zstd", "cookies", "secure-cookies"] features = ["openssl", "rustls", "compress-brotli", "compress-gzip", "compress-zstd", "cookies", "secure-cookies"]
rustdoc-args = ["--cfg", "docsrs"]
[lib] [lib]
name = "actix_web" name = "actix_web"

View File

@ -11,6 +11,8 @@
Alternatively, explicitly require trailing slashes: `NormalizePath::new(TrailingSlash::Always)`. Alternatively, explicitly require trailing slashes: `NormalizePath::new(TrailingSlash::Always)`.
* The `type Config` of `FromRequest` was removed.
* Feature flag `compress` has been split into its supported algorithm (brotli, gzip, zstd). * Feature flag `compress` has been split into its supported algorithm (brotli, gzip, zstd).
By default all compression algorithms are enabled. By default all compression algorithms are enabled.
To select algorithm you want to include with `middleware::Compress` use following flags: To select algorithm you want to include with `middleware::Compress` use following flags:

View File

@ -59,7 +59,6 @@ impl AsRef<Path> for PathBufWrap {
impl FromRequest for PathBufWrap { impl FromRequest for PathBufWrap {
type Error = UriSegmentError; type Error = UriSegmentError;
type Future = Ready<Result<Self, Self::Error>>; type Future = Ready<Result<Self, Self::Error>>;
type Config = ();
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future { fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
ready(req.match_info().path().parse()) ready(req.match_info().path().parse())

View File

@ -33,7 +33,6 @@ use crate::server::Multipart;
impl FromRequest for Multipart { impl FromRequest for Multipart {
type Error = Error; type Error = Error;
type Future = Ready<Result<Multipart, Error>>; type Future = Ready<Result<Multipart, Error>>;
type Config = ();
#[inline] #[inline]
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future { fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {

View File

@ -120,7 +120,6 @@ where
} }
impl<T: ?Sized + 'static> FromRequest for Data<T> { impl<T: ?Sized + 'static> FromRequest for Data<T> {
type Config = ();
type Error = Error; type Error = Error;
type Future = Ready<Result<Self, Error>>; type Future = Ready<Result<Self, Error>>;

View File

@ -13,13 +13,42 @@ use futures_core::ready;
use crate::{dev::Payload, Error, HttpRequest}; use crate::{dev::Payload, Error, HttpRequest};
/// Trait implemented by types that can be extracted from request. /// A type that implements [`FromRequest`] is called an **extractor** and can extract data
/// from the request. Examples of types that implement this trait are [`Json`], [`Form`], [`Path`].
/// ///
/// Types that implement this trait can be used with `Route` handlers. /// An extractor can be customized by injecting the corresponding configuration with one of:
///
/// - [`App::app_data()`](`crate::App::app_data`)
/// - [`Scope::app_data()`](`crate::Scope::app_data`)
/// - [`Resource::app_data()`](`crate::Resource::app_data`)
///
/// Here are some built-in extractors and their corresponding configuration.
/// Please refer to the respective documentation for details.
///
/// | Extractor | Configuration |
/// |-------------|-------------------|
/// | [`Json`] | [`JsonConfig`] |
/// | [`Form`] | [`FormConfig`] |
/// | [`Path`] | [`PathConfig`] |
/// | [`Query`] | [`QueryConfig`] |
/// | [`Payload`] | [`PayloadConfig`] |
/// | [`String`] | [`PayloadConfig`] |
/// | [`Bytes`] | [`PayloadConfig`] |
///
/// [`Json`]: crate::web::Json
/// [`JsonConfig`]: crate::web::JsonConfig
/// [`Form`]: crate::web::Form
/// [`FormConfig`]: crate::web::FormConfig
/// [`Path`]: crate::web::Path
/// [`PathConfig`]: crate::web::PathConfig
/// [`Query`]: crate::web::Query
/// [`QueryConfig`]: crate::web::QueryConfig
/// [`Payload`]: crate::web::Payload
/// [`PayloadConfig`]: crate::web::PayloadConfig
/// [`String`]: FromRequest#impl-FromRequest-for-String
/// [`Bytes`]: crate::web::Bytes#impl-FromRequest
#[cfg_attr(docsrs, doc(alias = "Extractor"))]
pub trait FromRequest: Sized { pub trait FromRequest: Sized {
/// Configuration for this extractor.
type Config: Default + 'static;
/// The associated error which can be returned. /// The associated error which can be returned.
type Error: Into<Error>; type Error: Into<Error>;
@ -35,14 +64,6 @@ pub trait FromRequest: Sized {
fn extract(req: &HttpRequest) -> Self::Future { fn extract(req: &HttpRequest) -> Self::Future {
Self::from_request(req, &mut Payload::None) Self::from_request(req, &mut Payload::None)
} }
/// Create and configure config instance.
fn configure<F>(f: F) -> Self::Config
where
F: FnOnce(Self::Config) -> Self::Config,
{
f(Self::Config::default())
}
} }
/// Optionally extract a field from the request /// Optionally extract a field from the request
@ -65,7 +86,6 @@ pub trait FromRequest: Sized {
/// impl FromRequest for Thing { /// impl FromRequest for Thing {
/// type Error = Error; /// type Error = Error;
/// type Future = Ready<Result<Self, Self::Error>>; /// type Future = Ready<Result<Self, Self::Error>>;
/// type Config = ();
/// ///
/// fn from_request(req: &HttpRequest, payload: &mut dev::Payload) -> Self::Future { /// fn from_request(req: &HttpRequest, payload: &mut dev::Payload) -> Self::Future {
/// if rand::random() { /// if rand::random() {
@ -100,7 +120,6 @@ where
{ {
type Error = Error; type Error = Error;
type Future = FromRequestOptFuture<T::Future>; type Future = FromRequestOptFuture<T::Future>;
type Config = T::Config;
#[inline] #[inline]
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future { fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
@ -156,7 +175,6 @@ where
/// impl FromRequest for Thing { /// impl FromRequest for Thing {
/// type Error = Error; /// type Error = Error;
/// type Future = Ready<Result<Thing, Error>>; /// type Future = Ready<Result<Thing, Error>>;
/// type Config = ();
/// ///
/// fn from_request(req: &HttpRequest, payload: &mut dev::Payload) -> Self::Future { /// fn from_request(req: &HttpRequest, payload: &mut dev::Payload) -> Self::Future {
/// if rand::random() { /// if rand::random() {
@ -189,7 +207,6 @@ where
{ {
type Error = Error; type Error = Error;
type Future = FromRequestResFuture<T::Future>; type Future = FromRequestResFuture<T::Future>;
type Config = T::Config;
#[inline] #[inline]
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future { fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
@ -233,7 +250,6 @@ where
impl FromRequest for Uri { impl FromRequest for Uri {
type Error = Infallible; type Error = Infallible;
type Future = Ready<Result<Self, Self::Error>>; type Future = Ready<Result<Self, Self::Error>>;
type Config = ();
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future { fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
ok(req.uri().clone()) ok(req.uri().clone())
@ -255,7 +271,6 @@ impl FromRequest for Uri {
impl FromRequest for Method { impl FromRequest for Method {
type Error = Infallible; type Error = Infallible;
type Future = Ready<Result<Self, Self::Error>>; type Future = Ready<Result<Self, Self::Error>>;
type Config = ();
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future { fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
ok(req.method().clone()) ok(req.method().clone())
@ -266,7 +281,6 @@ impl FromRequest for Method {
impl FromRequest for () { impl FromRequest for () {
type Error = Infallible; type Error = Infallible;
type Future = Ready<Result<Self, Self::Error>>; type Future = Ready<Result<Self, Self::Error>>;
type Config = ();
fn from_request(_: &HttpRequest, _: &mut Payload) -> Self::Future { fn from_request(_: &HttpRequest, _: &mut Payload) -> Self::Future {
ok(()) ok(())
@ -306,7 +320,6 @@ macro_rules! tuple_from_req ({$fut_type:ident, $(($n:tt, $T:ident)),+} => {
{ {
type Error = Error; type Error = Error;
type Future = $fut_type<$($T),+>; type Future = $fut_type<$($T),+>;
type Config = ($($T::Config),+);
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future { fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
$fut_type { $fut_type {

View File

@ -209,7 +209,6 @@ impl ConnectionInfo {
impl FromRequest for ConnectionInfo { impl FromRequest for ConnectionInfo {
type Error = Infallible; type Error = Infallible;
type Future = Ready<Result<Self, Self::Error>>; type Future = Ready<Result<Self, Self::Error>>;
type Config = ();
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future { fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
ok(req.connection_info().clone()) ok(req.connection_info().clone())
@ -252,7 +251,6 @@ impl ResponseError for MissingPeerAddr {}
impl FromRequest for PeerAddr { impl FromRequest for PeerAddr {
type Error = MissingPeerAddr; type Error = MissingPeerAddr;
type Future = Ready<Result<Self, Self::Error>>; type Future = Ready<Result<Self, Self::Error>>;
type Config = ();
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future { fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
match req.peer_addr() { match req.peer_addr() {

View File

@ -358,7 +358,6 @@ impl Drop for HttpRequest {
/// } /// }
/// ``` /// ```
impl FromRequest for HttpRequest { impl FromRequest for HttpRequest {
type Config = ();
type Error = Error; type Error = Error;
type Future = Ready<Result<Self, Error>>; type Future = Ready<Result<Self, Error>>;

View File

@ -64,7 +64,6 @@ impl<T: Clone + 'static> Deref for ReqData<T> {
} }
impl<T: Clone + 'static> FromRequest for ReqData<T> { impl<T: Clone + 'static> FromRequest for ReqData<T> {
type Config = ();
type Error = Error; type Error = Error;
type Future = Ready<Result<Self, Error>>; type Future = Ready<Result<Self, Error>>;

View File

@ -187,7 +187,6 @@ where
{ {
type Error = EitherExtractError<L::Error, R::Error>; type Error = EitherExtractError<L::Error, R::Error>;
type Future = EitherExtractFut<L, R>; type Future = EitherExtractFut<L, R>;
type Config = ();
fn from_request(req: &HttpRequest, payload: &mut dev::Payload) -> Self::Future { fn from_request(req: &HttpRequest, payload: &mut dev::Payload) -> Self::Future {
EitherExtractFut { EitherExtractFut {

View File

@ -126,20 +126,12 @@ impl<T> FromRequest for Form<T>
where where
T: DeserializeOwned + 'static, T: DeserializeOwned + 'static,
{ {
type Config = FormConfig;
type Error = Error; type Error = Error;
type Future = FormExtractFut<T>; type Future = FormExtractFut<T>;
#[inline] #[inline]
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future { fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
let (limit, err_handler) = req let FormConfig { limit, err_handler } = FormConfig::from_req(req).clone();
.app_data::<Self::Config>()
.or_else(|| {
req.app_data::<web::Data<Self::Config>>()
.map(|d| d.as_ref())
})
.map(|c| (c.limit, c.err_handler.clone()))
.unwrap_or((16384, None));
FormExtractFut { FormExtractFut {
fut: UrlEncoded::new(req, payload).limit(limit), fut: UrlEncoded::new(req, payload).limit(limit),
@ -241,14 +233,26 @@ impl FormConfig {
self.err_handler = Some(Rc::new(f)); self.err_handler = Some(Rc::new(f));
self self
} }
/// Extract payload config from app data.
///
/// Checks both `T` and `Data<T>`, in that order, and falls back to the default payload config.
fn from_req(req: &HttpRequest) -> &Self {
req.app_data::<Self>()
.or_else(|| req.app_data::<web::Data<Self>>().map(|d| d.as_ref()))
.unwrap_or(&DEFAULT_CONFIG)
}
} }
/// Allow shared refs used as default.
const DEFAULT_CONFIG: FormConfig = FormConfig {
limit: 16_384, // 2^14 bytes (~16kB)
err_handler: None,
};
impl Default for FormConfig { impl Default for FormConfig {
fn default() -> Self { fn default() -> Self {
FormConfig { DEFAULT_CONFIG
limit: 16_384, // 2^14 bytes (~16kB)
err_handler: None,
}
} }
} }

View File

@ -62,7 +62,6 @@ where
{ {
type Error = ParseError; type Error = ParseError;
type Future = Ready<Result<Self, Self::Error>>; type Future = Ready<Result<Self, Self::Error>>;
type Config = ();
#[inline] #[inline]
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future { fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {

View File

@ -130,7 +130,6 @@ impl<T: Serialize> Responder for Json<T> {
impl<T: DeserializeOwned + 'static> FromRequest for Json<T> { impl<T: DeserializeOwned + 'static> FromRequest for Json<T> {
type Error = Error; type Error = Error;
type Future = JsonExtractFut<T>; type Future = JsonExtractFut<T>;
type Config = JsonConfig;
#[inline] #[inline]
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future { fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {

View File

@ -97,12 +97,11 @@ where
{ {
type Error = Error; type Error = Error;
type Future = Ready<Result<Self, Self::Error>>; type Future = Ready<Result<Self, Self::Error>>;
type Config = PathConfig;
#[inline] #[inline]
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future { fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
let error_handler = req let error_handler = req
.app_data::<Self::Config>() .app_data::<PathConfig>()
.and_then(|c| c.ehandler.clone()); .and_then(|c| c.ehandler.clone());
ready( ready(

View File

@ -63,7 +63,6 @@ impl Stream for Payload {
/// See [here](#usage) for example of usage as an extractor. /// See [here](#usage) for example of usage as an extractor.
impl FromRequest for Payload { impl FromRequest for Payload {
type Config = PayloadConfig;
type Error = Error; type Error = Error;
type Future = Ready<Result<Payload, Error>>; type Future = Ready<Result<Payload, Error>>;
@ -90,7 +89,6 @@ impl FromRequest for Payload {
/// } /// }
/// ``` /// ```
impl FromRequest for Bytes { impl FromRequest for Bytes {
type Config = PayloadConfig;
type Error = Error; type Error = Error;
type Future = Either<BytesExtractFut, Ready<Result<Bytes, Error>>>; type Future = Either<BytesExtractFut, Ready<Result<Bytes, Error>>>;
@ -126,8 +124,7 @@ impl<'a> Future for BytesExtractFut {
/// ///
/// Text extractor automatically decode body according to the request's charset. /// Text extractor automatically decode body according to the request's charset.
/// ///
/// [**PayloadConfig**](PayloadConfig) allows to configure /// Use [`PayloadConfig`] to configure extraction process.
/// extraction process.
/// ///
/// # Examples /// # Examples
/// ``` /// ```
@ -139,7 +136,6 @@ impl<'a> Future for BytesExtractFut {
/// format!("Body {}!", text) /// format!("Body {}!", text)
/// } /// }
impl FromRequest for String { impl FromRequest for String {
type Config = PayloadConfig;
type Error = Error; type Error = Error;
type Future = Either<StringExtractFut, Ready<Result<String, Error>>>; type Future = Either<StringExtractFut, Ready<Result<String, Error>>>;
@ -198,14 +194,15 @@ fn bytes_to_string(body: Bytes, encoding: &'static Encoding) -> Result<String, E
/// Configuration for request payloads. /// Configuration for request payloads.
/// ///
/// Applies to the built-in `Bytes` and `String` extractors. Note that the `Payload` extractor does /// Applies to the built-in [`Bytes`] and [`String`] extractors.
/// not automatically check conformance with this configuration to allow more flexibility when /// Note that the [`Payload`] extractor does not automatically check
/// building extractors on top of `Payload`. /// conformance with this configuration to allow more flexibility when
/// building extractors on top of [`Payload`].
/// ///
/// By default, the payload size limit is 256kB and there is no mime type condition. /// By default, the payload size limit is 256kB and there is no mime type condition.
/// ///
/// To use this, add an instance of it to your app or service through one of the /// To use this, add an instance of it to your [`app`](crate::App), [`scope`](crate::Scope)
/// `.app_data()` methods. /// or [`resource`](crate::Resource) through the associated `.app_data()` method.
#[derive(Clone)] #[derive(Clone)]
pub struct PayloadConfig { pub struct PayloadConfig {
limit: usize, limit: usize,

View File

@ -109,12 +109,11 @@ impl<T: fmt::Display> fmt::Display for Query<T> {
impl<T: DeserializeOwned> FromRequest for Query<T> { impl<T: DeserializeOwned> FromRequest for Query<T> {
type Error = Error; type Error = Error;
type Future = Ready<Result<Self, Error>>; type Future = Ready<Result<Self, Error>>;
type Config = QueryConfig;
#[inline] #[inline]
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future { fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
let error_handler = req let error_handler = req
.app_data::<Self::Config>() .app_data::<QueryConfig>()
.and_then(|c| c.err_handler.clone()); .and_then(|c| c.err_handler.clone());
serde_urlencoded::from_str::<T>(req.query_string()) serde_urlencoded::from_str::<T>(req.query_string())