mirror of
https://github.com/actix/actix-web.git
synced 2024-12-21 23:56:35 +00:00
static form extract future (#2181)
This commit is contained in:
parent
a7cd4e85cf
commit
07036b5640
1 changed files with 39 additions and 14 deletions
|
@ -12,7 +12,7 @@ use std::{
|
||||||
use actix_http::Payload;
|
use actix_http::Payload;
|
||||||
use bytes::BytesMut;
|
use bytes::BytesMut;
|
||||||
use encoding_rs::{Encoding, UTF_8};
|
use encoding_rs::{Encoding, UTF_8};
|
||||||
use futures_core::future::LocalBoxFuture;
|
use futures_core::{future::LocalBoxFuture, ready};
|
||||||
use futures_util::{FutureExt as _, StreamExt as _};
|
use futures_util::{FutureExt as _, StreamExt as _};
|
||||||
use serde::{de::DeserializeOwned, Serialize};
|
use serde::{de::DeserializeOwned, Serialize};
|
||||||
|
|
||||||
|
@ -123,11 +123,10 @@ where
|
||||||
{
|
{
|
||||||
type Config = FormConfig;
|
type Config = FormConfig;
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
type Future = LocalBoxFuture<'static, Result<Self, Error>>;
|
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 req2 = req.clone();
|
|
||||||
let (limit, err_handler) = req
|
let (limit, err_handler) = req
|
||||||
.app_data::<Self::Config>()
|
.app_data::<Self::Config>()
|
||||||
.or_else(|| {
|
.or_else(|| {
|
||||||
|
@ -137,16 +136,42 @@ where
|
||||||
.map(|c| (c.limit, c.err_handler.clone()))
|
.map(|c| (c.limit, c.err_handler.clone()))
|
||||||
.unwrap_or((16384, None));
|
.unwrap_or((16384, None));
|
||||||
|
|
||||||
UrlEncoded::new(req, payload)
|
FormExtractFut {
|
||||||
.limit(limit)
|
fut: UrlEncoded::new(req, payload).limit(limit),
|
||||||
.map(move |res| match res {
|
req: req.clone(),
|
||||||
Err(err) => match err_handler {
|
err_handler,
|
||||||
Some(err_handler) => Err((err_handler)(err, &req2)),
|
}
|
||||||
None => Err(err.into()),
|
}
|
||||||
},
|
}
|
||||||
Ok(item) => Ok(Form(item)),
|
|
||||||
})
|
type FormErrHandler = Option<Rc<dyn Fn(UrlencodedError, &HttpRequest) -> Error>>;
|
||||||
.boxed_local()
|
|
||||||
|
pub struct FormExtractFut<T> {
|
||||||
|
fut: UrlEncoded<T>,
|
||||||
|
err_handler: FormErrHandler,
|
||||||
|
req: HttpRequest,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Future for FormExtractFut<T>
|
||||||
|
where
|
||||||
|
T: DeserializeOwned + 'static,
|
||||||
|
{
|
||||||
|
type Output = Result<Form<T>, Error>;
|
||||||
|
|
||||||
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
|
let this = self.get_mut();
|
||||||
|
|
||||||
|
let res = ready!(Pin::new(&mut this.fut).poll(cx));
|
||||||
|
|
||||||
|
let res = match res {
|
||||||
|
Err(err) => match &this.err_handler {
|
||||||
|
Some(err_handler) => Err((err_handler)(err, &this.req)),
|
||||||
|
None => Err(err.into()),
|
||||||
|
},
|
||||||
|
Ok(item) => Ok(Form(item)),
|
||||||
|
};
|
||||||
|
|
||||||
|
Poll::Ready(res)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -193,7 +218,7 @@ impl<T: Serialize> Responder for Form<T> {
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct FormConfig {
|
pub struct FormConfig {
|
||||||
limit: usize,
|
limit: usize,
|
||||||
err_handler: Option<Rc<dyn Fn(UrlencodedError, &HttpRequest) -> Error>>,
|
err_handler: FormErrHandler,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FormConfig {
|
impl FormConfig {
|
||||||
|
|
Loading…
Reference in a new issue