mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-24 00:21:08 +01:00
make extractor config type explicit
This commit is contained in:
parent
4f30fa9d46
commit
ee33f52736
@ -9,6 +9,8 @@
|
|||||||
* Removed `Decompress` middleware. Bytes, String, Json, Form extractors
|
* Removed `Decompress` middleware. Bytes, String, Json, Form extractors
|
||||||
automatically decompress payload.
|
automatically decompress payload.
|
||||||
|
|
||||||
|
* Make extractor config type explicit. Add `FromRequest::Config` associated type.
|
||||||
|
|
||||||
|
|
||||||
## [1.0.0-alpha.5] - 2019-04-12
|
## [1.0.0-alpha.5] - 2019-04-12
|
||||||
|
|
||||||
|
@ -546,6 +546,7 @@ impl PathBufWrp {
|
|||||||
impl FromRequest for PathBufWrp {
|
impl FromRequest for PathBufWrp {
|
||||||
type Error = UriSegmentError;
|
type Error = UriSegmentError;
|
||||||
type Future = Result<Self, Self::Error>;
|
type Future = Result<Self, Self::Error>;
|
||||||
|
type Config = ();
|
||||||
|
|
||||||
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
|
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
|
||||||
PathBufWrp::get_pathbuf(req.match_info().path())
|
PathBufWrp::get_pathbuf(req.match_info().path())
|
||||||
|
@ -33,6 +33,7 @@ use crate::server::Multipart;
|
|||||||
impl FromRequest for Multipart {
|
impl FromRequest for Multipart {
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
type Future = Result<Multipart, Error>;
|
type Future = 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 {
|
||||||
|
@ -1,5 +1,9 @@
|
|||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
|
## [0.1.0-alpha.6] - 2019-04-xx
|
||||||
|
|
||||||
|
* Update actix-web
|
||||||
|
|
||||||
## [0.1.0-alpha.4] - 2019-04-08
|
## [0.1.0-alpha.4] - 2019-04-08
|
||||||
|
|
||||||
* Update actix-web
|
* Update actix-web
|
||||||
|
@ -175,6 +175,7 @@ impl Session {
|
|||||||
impl FromRequest for Session {
|
impl FromRequest for Session {
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
type Future = Result<Session, Error>;
|
type Future = Result<Session, Error>;
|
||||||
|
type Config = ();
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
|
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
|
||||||
|
@ -89,6 +89,7 @@ impl<T> Clone for Data<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<T: 'static> FromRequest for Data<T> {
|
impl<T: 'static> FromRequest for Data<T> {
|
||||||
|
type Config = ();
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
type Future = Result<Self, Error>;
|
type Future = Result<Self, Error>;
|
||||||
|
|
||||||
@ -233,6 +234,7 @@ impl<T> Clone for RouteData<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<T: 'static> FromRequest for RouteData<T> {
|
impl<T: 'static> FromRequest for RouteData<T> {
|
||||||
|
type Config = ();
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
type Future = Result<Self, Error>;
|
type Future = Result<Self, Error>;
|
||||||
|
|
||||||
|
@ -17,6 +17,9 @@ pub trait FromRequest: Sized {
|
|||||||
/// Future that resolves to a Self
|
/// Future that resolves to a Self
|
||||||
type Future: IntoFuture<Item = Self, Error = Self::Error>;
|
type Future: IntoFuture<Item = Self, Error = Self::Error>;
|
||||||
|
|
||||||
|
/// Configuration for this extractor
|
||||||
|
type Config: Default + 'static;
|
||||||
|
|
||||||
/// Convert request to a Self
|
/// Convert request to a Self
|
||||||
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future;
|
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future;
|
||||||
|
|
||||||
@ -26,6 +29,14 @@ 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
|
||||||
@ -48,6 +59,7 @@ pub trait FromRequest: Sized {
|
|||||||
/// impl FromRequest for Thing {
|
/// impl FromRequest for Thing {
|
||||||
/// type Error = Error;
|
/// type Error = Error;
|
||||||
/// type Future = Result<Self, Self::Error>;
|
/// type Future = 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() {
|
||||||
@ -80,6 +92,7 @@ where
|
|||||||
T: FromRequest,
|
T: FromRequest,
|
||||||
T::Future: 'static,
|
T::Future: 'static,
|
||||||
{
|
{
|
||||||
|
type Config = T::Config;
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
type Future = Box<Future<Item = Option<T>, Error = Error>>;
|
type Future = Box<Future<Item = Option<T>, Error = Error>>;
|
||||||
|
|
||||||
@ -119,6 +132,7 @@ where
|
|||||||
/// impl FromRequest for Thing {
|
/// impl FromRequest for Thing {
|
||||||
/// type Error = Error;
|
/// type Error = Error;
|
||||||
/// type Future = Result<Thing, Error>;
|
/// type Future = 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() {
|
||||||
@ -149,6 +163,7 @@ where
|
|||||||
T::Future: 'static,
|
T::Future: 'static,
|
||||||
T::Error: 'static,
|
T::Error: 'static,
|
||||||
{
|
{
|
||||||
|
type Config = T::Config;
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
type Future = Box<Future<Item = Result<T, T::Error>, Error = Error>>;
|
type Future = Box<Future<Item = Result<T, T::Error>, Error = Error>>;
|
||||||
|
|
||||||
@ -167,6 +182,7 @@ where
|
|||||||
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
impl FromRequest for () {
|
impl FromRequest for () {
|
||||||
|
type Config = ();
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
type Future = Result<(), Error>;
|
type Future = Result<(), Error>;
|
||||||
|
|
||||||
@ -183,6 +199,7 @@ 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 {
|
||||||
|
@ -141,6 +141,7 @@ struct IdentityItem {
|
|||||||
/// # fn main() {}
|
/// # fn main() {}
|
||||||
/// ```
|
/// ```
|
||||||
impl FromRequest for Identity {
|
impl FromRequest for Identity {
|
||||||
|
type Config = ();
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
type Future = Result<Identity, Error>;
|
type Future = Result<Identity, Error>;
|
||||||
|
|
||||||
|
@ -266,6 +266,7 @@ impl Drop for HttpRequest {
|
|||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
impl FromRequest for HttpRequest {
|
impl FromRequest for HttpRequest {
|
||||||
|
type Config = ();
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
type Future = Result<Self, Error>;
|
type Future = Result<Self, Error>;
|
||||||
|
|
||||||
|
@ -292,7 +292,7 @@ impl Route {
|
|||||||
/// configuration or specific state available via `RouteData<T>` extractor.
|
/// configuration or specific state available via `RouteData<T>` extractor.
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// use actix_web::{web, App};
|
/// use actix_web::{web, App, FromRequest};
|
||||||
///
|
///
|
||||||
/// /// extract text data from request
|
/// /// extract text data from request
|
||||||
/// fn index(body: String) -> String {
|
/// fn index(body: String) -> String {
|
||||||
@ -304,13 +304,15 @@ impl Route {
|
|||||||
/// web::resource("/index.html").route(
|
/// web::resource("/index.html").route(
|
||||||
/// web::get()
|
/// web::get()
|
||||||
/// // limit size of the payload
|
/// // limit size of the payload
|
||||||
/// .data(web::PayloadConfig::new(4096))
|
/// .data(String::configure(|cfg| {
|
||||||
|
/// cfg.limit(4096)
|
||||||
|
/// }))
|
||||||
/// // register handler
|
/// // register handler
|
||||||
/// .to(index)
|
/// .to(index)
|
||||||
/// ));
|
/// ));
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
pub fn data<C: 'static>(mut self, data: C) -> Self {
|
pub fn data<T: 'static>(mut self, data: T) -> Self {
|
||||||
if self.data.is_none() {
|
if self.data.is_none() {
|
||||||
self.data = Some(Extensions::new());
|
self.data = Some(Extensions::new());
|
||||||
}
|
}
|
||||||
|
@ -73,6 +73,7 @@ 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 = Box<Future<Item = Self, Error = Error>>;
|
type Future = Box<Future<Item = Self, Error = Error>>;
|
||||||
|
|
||||||
@ -115,7 +116,7 @@ impl<T: fmt::Display> fmt::Display for Form<T> {
|
|||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// #[macro_use] extern crate serde_derive;
|
/// #[macro_use] extern crate serde_derive;
|
||||||
/// use actix_web::{web, App, Result};
|
/// use actix_web::{web, App, FromRequest, Result};
|
||||||
///
|
///
|
||||||
/// #[derive(Deserialize)]
|
/// #[derive(Deserialize)]
|
||||||
/// struct FormData {
|
/// struct FormData {
|
||||||
@ -133,7 +134,9 @@ impl<T: fmt::Display> fmt::Display for Form<T> {
|
|||||||
/// web::resource("/index.html")
|
/// web::resource("/index.html")
|
||||||
/// .route(web::get()
|
/// .route(web::get()
|
||||||
/// // change `Form` extractor configuration
|
/// // change `Form` extractor configuration
|
||||||
/// .data(web::FormConfig::default().limit(4097))
|
/// .data(
|
||||||
|
/// web::Form::<FormData>::configure(|cfg| cfg.limit(4097))
|
||||||
|
/// )
|
||||||
/// .to(index))
|
/// .to(index))
|
||||||
/// );
|
/// );
|
||||||
/// }
|
/// }
|
||||||
|
@ -168,6 +168,7 @@ impl<T> FromRequest for Json<T>
|
|||||||
where
|
where
|
||||||
T: DeserializeOwned + 'static,
|
T: DeserializeOwned + 'static,
|
||||||
{
|
{
|
||||||
|
type Config = JsonConfig;
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
type Future = Box<Future<Item = Self, Error = Error>>;
|
type Future = Box<Future<Item = Self, Error = Error>>;
|
||||||
|
|
||||||
@ -205,7 +206,7 @@ where
|
|||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// #[macro_use] extern crate serde_derive;
|
/// #[macro_use] extern crate serde_derive;
|
||||||
/// use actix_web::{error, web, App, HttpResponse};
|
/// use actix_web::{error, web, App, FromRequest, HttpResponse};
|
||||||
///
|
///
|
||||||
/// #[derive(Deserialize)]
|
/// #[derive(Deserialize)]
|
||||||
/// struct Info {
|
/// struct Info {
|
||||||
@ -222,10 +223,12 @@ where
|
|||||||
/// web::resource("/index.html").route(
|
/// web::resource("/index.html").route(
|
||||||
/// web::post().data(
|
/// web::post().data(
|
||||||
/// // change json extractor configuration
|
/// // change json extractor configuration
|
||||||
/// web::JsonConfig::default().limit(4096)
|
/// web::Json::<Info>::configure(|cfg| {
|
||||||
|
/// cfg.limit(4096)
|
||||||
/// .error_handler(|err, req| { // <- create custom error response
|
/// .error_handler(|err, req| { // <- create custom error response
|
||||||
/// error::InternalError::from_response(
|
/// error::InternalError::from_response(
|
||||||
/// err, HttpResponse::Conflict().finish()).into()
|
/// err, HttpResponse::Conflict().finish()).into()
|
||||||
|
/// })
|
||||||
/// }))
|
/// }))
|
||||||
/// .to(index))
|
/// .to(index))
|
||||||
/// );
|
/// );
|
||||||
|
@ -156,6 +156,7 @@ impl<T> FromRequest for Path<T>
|
|||||||
where
|
where
|
||||||
T: de::DeserializeOwned,
|
T: de::DeserializeOwned,
|
||||||
{
|
{
|
||||||
|
type Config = ();
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
type Future = Result<Self, Error>;
|
type Future = Result<Self, Error>;
|
||||||
|
|
||||||
|
@ -86,6 +86,7 @@ impl Stream for Payload {
|
|||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
impl FromRequest for Payload {
|
impl FromRequest for Payload {
|
||||||
|
type Config = PayloadConfig;
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
type Future = Result<Payload, Error>;
|
type Future = Result<Payload, Error>;
|
||||||
|
|
||||||
@ -121,6 +122,7 @@ impl FromRequest for Payload {
|
|||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
impl FromRequest for Bytes {
|
impl FromRequest for Bytes {
|
||||||
|
type Config = PayloadConfig;
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
type Future =
|
type Future =
|
||||||
Either<Box<Future<Item = Bytes, Error = Error>>, FutureResult<Bytes, Error>>;
|
Either<Box<Future<Item = Bytes, Error = Error>>, FutureResult<Bytes, Error>>;
|
||||||
@ -156,7 +158,7 @@ impl FromRequest for Bytes {
|
|||||||
/// ## Example
|
/// ## Example
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// use actix_web::{web, App};
|
/// use actix_web::{web, App, FromRequest};
|
||||||
///
|
///
|
||||||
/// /// extract text data from request
|
/// /// extract text data from request
|
||||||
/// fn index(text: String) -> String {
|
/// fn index(text: String) -> String {
|
||||||
@ -167,12 +169,15 @@ impl FromRequest for Bytes {
|
|||||||
/// let app = App::new().service(
|
/// let app = App::new().service(
|
||||||
/// web::resource("/index.html").route(
|
/// web::resource("/index.html").route(
|
||||||
/// web::get()
|
/// web::get()
|
||||||
/// .data(web::PayloadConfig::new(4096)) // <- limit size of the payload
|
/// .data(String::configure(|cfg| { // <- limit size of the payload
|
||||||
|
/// cfg.limit(4096)
|
||||||
|
/// }))
|
||||||
/// .to(index)) // <- register handler with extractor params
|
/// .to(index)) // <- register handler with extractor params
|
||||||
/// );
|
/// );
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
impl FromRequest for String {
|
impl FromRequest for String {
|
||||||
|
type Config = PayloadConfig;
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
type Future =
|
type Future =
|
||||||
Either<Box<Future<Item = String, Error = Error>>, FutureResult<String, Error>>;
|
Either<Box<Future<Item = String, Error = Error>>, FutureResult<String, Error>>;
|
||||||
@ -228,7 +233,9 @@ pub struct PayloadConfig {
|
|||||||
impl PayloadConfig {
|
impl PayloadConfig {
|
||||||
/// Create `PayloadConfig` instance and set max size of payload.
|
/// Create `PayloadConfig` instance and set max size of payload.
|
||||||
pub fn new(limit: usize) -> Self {
|
pub fn new(limit: usize) -> Self {
|
||||||
Self::default().limit(limit)
|
let mut cfg = Self::default();
|
||||||
|
cfg.limit = limit;
|
||||||
|
cfg
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Change max size of payload. By default max size is 256Kb
|
/// Change max size of payload. By default max size is 256Kb
|
||||||
|
@ -115,6 +115,7 @@ impl<T> FromRequest for Query<T>
|
|||||||
where
|
where
|
||||||
T: de::DeserializeOwned,
|
T: de::DeserializeOwned,
|
||||||
{
|
{
|
||||||
|
type Config = ();
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
type Future = Result<Self, Error>;
|
type Future = Result<Self, Error>;
|
||||||
|
|
||||||
|
@ -16,10 +16,8 @@ use flate2::Compression;
|
|||||||
use futures::stream::once;
|
use futures::stream::once;
|
||||||
use rand::{distributions::Alphanumeric, Rng};
|
use rand::{distributions::Alphanumeric, Rng};
|
||||||
|
|
||||||
use actix_web::{http, test, web, App, HttpResponse, HttpServer};
|
|
||||||
|
|
||||||
#[cfg(any(feature = "brotli", feature = "flate2-zlib", feature = "flate2-rust"))]
|
|
||||||
use actix_web::middleware::{BodyEncoding, Compress};
|
use actix_web::middleware::{BodyEncoding, Compress};
|
||||||
|
use actix_web::{http, test, web, App, HttpResponse, HttpServer};
|
||||||
|
|
||||||
const STR: &str = "Hello World Hello World Hello World Hello World Hello World \
|
const STR: &str = "Hello World Hello World Hello World Hello World Hello World \
|
||||||
Hello World Hello World Hello World Hello World Hello World \
|
Hello World Hello World Hello World Hello World Hello World \
|
||||||
|
Loading…
Reference in New Issue
Block a user