1
0
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:
Nikolay Kim 2019-04-13 16:35:25 -07:00
parent 4f30fa9d46
commit ee33f52736
16 changed files with 62 additions and 17 deletions

View File

@ -9,6 +9,8 @@
* Removed `Decompress` middleware. Bytes, String, Json, Form extractors
automatically decompress payload.
* Make extractor config type explicit. Add `FromRequest::Config` associated type.
## [1.0.0-alpha.5] - 2019-04-12

View File

@ -546,6 +546,7 @@ impl PathBufWrp {
impl FromRequest for PathBufWrp {
type Error = UriSegmentError;
type Future = Result<Self, Self::Error>;
type Config = ();
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
PathBufWrp::get_pathbuf(req.match_info().path())

View File

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

View File

@ -1,5 +1,9 @@
# Changes
## [0.1.0-alpha.6] - 2019-04-xx
* Update actix-web
## [0.1.0-alpha.4] - 2019-04-08
* Update actix-web

View File

@ -175,6 +175,7 @@ impl Session {
impl FromRequest for Session {
type Error = Error;
type Future = Result<Session, Error>;
type Config = ();
#[inline]
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {

View File

@ -89,6 +89,7 @@ impl<T> Clone for Data<T> {
}
impl<T: 'static> FromRequest for Data<T> {
type Config = ();
type Error = Error;
type Future = Result<Self, Error>;
@ -233,6 +234,7 @@ impl<T> Clone for RouteData<T> {
}
impl<T: 'static> FromRequest for RouteData<T> {
type Config = ();
type Error = Error;
type Future = Result<Self, Error>;

View File

@ -17,6 +17,9 @@ pub trait FromRequest: Sized {
/// Future that resolves to a Self
type Future: IntoFuture<Item = Self, Error = Self::Error>;
/// Configuration for this extractor
type Config: Default + 'static;
/// Convert request to a Self
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future;
@ -26,6 +29,14 @@ pub trait FromRequest: Sized {
fn extract(req: &HttpRequest) -> Self::Future {
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
@ -48,6 +59,7 @@ pub trait FromRequest: Sized {
/// impl FromRequest for Thing {
/// type Error = Error;
/// type Future = Result<Self, Self::Error>;
/// type Config = ();
///
/// fn from_request(req: &HttpRequest, payload: &mut dev::Payload) -> Self::Future {
/// if rand::random() {
@ -80,6 +92,7 @@ where
T: FromRequest,
T::Future: 'static,
{
type Config = T::Config;
type Error = Error;
type Future = Box<Future<Item = Option<T>, Error = Error>>;
@ -119,6 +132,7 @@ where
/// impl FromRequest for Thing {
/// type Error = Error;
/// type Future = Result<Thing, Error>;
/// type Config = ();
///
/// fn from_request(req: &HttpRequest, payload: &mut dev::Payload) -> Self::Future {
/// if rand::random() {
@ -149,6 +163,7 @@ where
T::Future: 'static,
T::Error: 'static,
{
type Config = T::Config;
type Error = Error;
type Future = Box<Future<Item = Result<T, T::Error>, Error = Error>>;
@ -167,6 +182,7 @@ where
#[doc(hidden)]
impl FromRequest for () {
type Config = ();
type Error = 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 Future = $fut_type<$($T),+>;
type Config = ($($T::Config),+);
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
$fut_type {

View File

@ -141,6 +141,7 @@ struct IdentityItem {
/// # fn main() {}
/// ```
impl FromRequest for Identity {
type Config = ();
type Error = Error;
type Future = Result<Identity, Error>;

View File

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

View File

@ -292,7 +292,7 @@ impl Route {
/// configuration or specific state available via `RouteData<T>` extractor.
///
/// ```rust
/// use actix_web::{web, App};
/// use actix_web::{web, App, FromRequest};
///
/// /// extract text data from request
/// fn index(body: String) -> String {
@ -304,13 +304,15 @@ impl Route {
/// web::resource("/index.html").route(
/// web::get()
/// // limit size of the payload
/// .data(web::PayloadConfig::new(4096))
/// .data(String::configure(|cfg| {
/// cfg.limit(4096)
/// }))
/// // register handler
/// .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() {
self.data = Some(Extensions::new());
}

View File

@ -73,6 +73,7 @@ impl<T> FromRequest for Form<T>
where
T: DeserializeOwned + 'static,
{
type Config = FormConfig;
type Error = Error;
type Future = Box<Future<Item = Self, Error = Error>>;
@ -115,7 +116,7 @@ impl<T: fmt::Display> fmt::Display for Form<T> {
///
/// ```rust
/// #[macro_use] extern crate serde_derive;
/// use actix_web::{web, App, Result};
/// use actix_web::{web, App, FromRequest, Result};
///
/// #[derive(Deserialize)]
/// struct FormData {
@ -133,7 +134,9 @@ impl<T: fmt::Display> fmt::Display for Form<T> {
/// web::resource("/index.html")
/// .route(web::get()
/// // change `Form` extractor configuration
/// .data(web::FormConfig::default().limit(4097))
/// .data(
/// web::Form::<FormData>::configure(|cfg| cfg.limit(4097))
/// )
/// .to(index))
/// );
/// }

View File

@ -168,6 +168,7 @@ impl<T> FromRequest for Json<T>
where
T: DeserializeOwned + 'static,
{
type Config = JsonConfig;
type Error = Error;
type Future = Box<Future<Item = Self, Error = Error>>;
@ -205,7 +206,7 @@ where
///
/// ```rust
/// #[macro_use] extern crate serde_derive;
/// use actix_web::{error, web, App, HttpResponse};
/// use actix_web::{error, web, App, FromRequest, HttpResponse};
///
/// #[derive(Deserialize)]
/// struct Info {
@ -222,10 +223,12 @@ where
/// web::resource("/index.html").route(
/// web::post().data(
/// // 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::InternalError::from_response(
/// err, HttpResponse::Conflict().finish()).into()
/// })
/// }))
/// .to(index))
/// );

View File

@ -156,6 +156,7 @@ impl<T> FromRequest for Path<T>
where
T: de::DeserializeOwned,
{
type Config = ();
type Error = Error;
type Future = Result<Self, Error>;

View File

@ -86,6 +86,7 @@ impl Stream for Payload {
/// }
/// ```
impl FromRequest for Payload {
type Config = PayloadConfig;
type Error = Error;
type Future = Result<Payload, Error>;
@ -121,6 +122,7 @@ impl FromRequest for Payload {
/// }
/// ```
impl FromRequest for Bytes {
type Config = PayloadConfig;
type Error = Error;
type Future =
Either<Box<Future<Item = Bytes, Error = Error>>, FutureResult<Bytes, Error>>;
@ -156,7 +158,7 @@ impl FromRequest for Bytes {
/// ## Example
///
/// ```rust
/// use actix_web::{web, App};
/// use actix_web::{web, App, FromRequest};
///
/// /// extract text data from request
/// fn index(text: String) -> String {
@ -167,12 +169,15 @@ impl FromRequest for Bytes {
/// let app = App::new().service(
/// web::resource("/index.html").route(
/// 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
/// );
/// }
/// ```
impl FromRequest for String {
type Config = PayloadConfig;
type Error = Error;
type Future =
Either<Box<Future<Item = String, Error = Error>>, FutureResult<String, Error>>;
@ -228,7 +233,9 @@ pub struct PayloadConfig {
impl PayloadConfig {
/// Create `PayloadConfig` instance and set max size of payload.
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

View File

@ -115,6 +115,7 @@ impl<T> FromRequest for Query<T>
where
T: de::DeserializeOwned,
{
type Config = ();
type Error = Error;
type Future = Result<Self, Error>;

View File

@ -16,10 +16,8 @@ use flate2::Compression;
use futures::stream::once;
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::{http, test, web, App, HttpResponse, HttpServer};
const STR: &str = "Hello World Hello World Hello World Hello World Hello World \
Hello World Hello World Hello World Hello World Hello World \