mirror of
https://github.com/fafhrd91/actix-web
synced 2025-01-31 19:10:07 +01:00
remove boxed futures on Json extract type (#1832)
This commit is contained in:
parent
1a361273e7
commit
97f615c245
@ -1,14 +1,16 @@
|
|||||||
//! Json extractor/responder
|
//! Json extractor/responder
|
||||||
|
|
||||||
use std::future::Future;
|
use std::future::Future;
|
||||||
|
use std::marker::PhantomData;
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::task::{Context, Poll};
|
use std::task::{Context, Poll};
|
||||||
use std::{fmt, ops};
|
use std::{fmt, ops};
|
||||||
|
|
||||||
use bytes::BytesMut;
|
use bytes::BytesMut;
|
||||||
use futures_util::future::{err, ok, FutureExt, LocalBoxFuture, Ready};
|
use futures_util::future::{ready, Ready};
|
||||||
use futures_util::StreamExt;
|
use futures_util::ready;
|
||||||
|
use futures_util::stream::Stream;
|
||||||
use serde::de::DeserializeOwned;
|
use serde::de::DeserializeOwned;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
|
|
||||||
@ -127,12 +129,12 @@ impl<T: Serialize> Responder for Json<T> {
|
|||||||
fn respond_to(self, _: &HttpRequest) -> Self::Future {
|
fn respond_to(self, _: &HttpRequest) -> Self::Future {
|
||||||
let body = match serde_json::to_string(&self.0) {
|
let body = match serde_json::to_string(&self.0) {
|
||||||
Ok(body) => body,
|
Ok(body) => body,
|
||||||
Err(e) => return err(e.into()),
|
Err(e) => return ready(Err(e.into())),
|
||||||
};
|
};
|
||||||
|
|
||||||
ok(Response::build(StatusCode::OK)
|
ready(Ok(Response::build(StatusCode::OK)
|
||||||
.content_type("application/json")
|
.content_type("application/json")
|
||||||
.body(body))
|
.body(body)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -173,37 +175,64 @@ where
|
|||||||
T: DeserializeOwned + 'static,
|
T: DeserializeOwned + 'static,
|
||||||
{
|
{
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
type Future = LocalBoxFuture<'static, Result<Self, Error>>;
|
type Future = JsonExtractFut<T>;
|
||||||
type Config = JsonConfig;
|
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 {
|
||||||
let req2 = req.clone();
|
|
||||||
let config = JsonConfig::from_req(req);
|
let config = JsonConfig::from_req(req);
|
||||||
|
|
||||||
let limit = config.limit;
|
let limit = config.limit;
|
||||||
let ctype = config.content_type.clone();
|
let ctype = config.content_type.clone();
|
||||||
let err_handler = config.err_handler.clone();
|
let err_handler = config.err_handler.clone();
|
||||||
|
|
||||||
JsonBody::new(req, payload, ctype)
|
JsonExtractFut {
|
||||||
.limit(limit)
|
req: Some(req.clone()),
|
||||||
.map(move |res| match res {
|
fut: JsonBody::new(req, payload, ctype).limit(limit),
|
||||||
Err(e) => {
|
err_handler,
|
||||||
log::debug!(
|
}
|
||||||
"Failed to deserialize Json from payload. \
|
}
|
||||||
Request path: {}",
|
}
|
||||||
req2.path()
|
|
||||||
);
|
|
||||||
|
|
||||||
if let Some(err) = err_handler {
|
type JsonErrorHandler =
|
||||||
Err((*err)(e, &req2))
|
Option<Arc<dyn Fn(JsonPayloadError, &HttpRequest) -> Error + Send + Sync>>;
|
||||||
} else {
|
|
||||||
Err(e.into())
|
pub struct JsonExtractFut<T> {
|
||||||
}
|
req: Option<HttpRequest>,
|
||||||
|
fut: JsonBody<T>,
|
||||||
|
err_handler: JsonErrorHandler,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Future for JsonExtractFut<T>
|
||||||
|
where
|
||||||
|
T: DeserializeOwned + 'static,
|
||||||
|
{
|
||||||
|
type Output = Result<Json<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(e) => {
|
||||||
|
let req = this.req.take().unwrap();
|
||||||
|
log::debug!(
|
||||||
|
"Failed to deserialize Json from payload. \
|
||||||
|
Request path: {}",
|
||||||
|
req.path()
|
||||||
|
);
|
||||||
|
|
||||||
|
if let Some(err) = this.err_handler.as_ref() {
|
||||||
|
Err((*err)(e, &req))
|
||||||
|
} else {
|
||||||
|
Err(e.into())
|
||||||
}
|
}
|
||||||
Ok(data) => Ok(Json(data)),
|
}
|
||||||
})
|
Ok(data) => Ok(Json(data)),
|
||||||
.boxed_local()
|
};
|
||||||
|
|
||||||
|
Poll::Ready(res)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -248,8 +277,7 @@ where
|
|||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct JsonConfig {
|
pub struct JsonConfig {
|
||||||
limit: usize,
|
limit: usize,
|
||||||
err_handler:
|
err_handler: JsonErrorHandler,
|
||||||
Option<Arc<dyn Fn(JsonPayloadError, &HttpRequest) -> Error + Send + Sync>>,
|
|
||||||
content_type: Option<Arc<dyn Fn(mime::Mime) -> bool + Send + Sync>>,
|
content_type: Option<Arc<dyn Fn(mime::Mime) -> bool + Send + Sync>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -308,17 +336,22 @@ impl Default for JsonConfig {
|
|||||||
/// * content type is not `application/json`
|
/// * content type is not `application/json`
|
||||||
/// (unless specified in [`JsonConfig`])
|
/// (unless specified in [`JsonConfig`])
|
||||||
/// * content length is greater than 256k
|
/// * content length is greater than 256k
|
||||||
pub struct JsonBody<U> {
|
pub enum JsonBody<U> {
|
||||||
limit: usize,
|
Error(Option<JsonPayloadError>),
|
||||||
length: Option<usize>,
|
Body {
|
||||||
#[cfg(feature = "compress")]
|
limit: usize,
|
||||||
stream: Option<Decompress<Payload>>,
|
length: Option<usize>,
|
||||||
#[cfg(not(feature = "compress"))]
|
#[cfg(feature = "compress")]
|
||||||
stream: Option<Payload>,
|
payload: Decompress<Payload>,
|
||||||
err: Option<JsonPayloadError>,
|
#[cfg(not(feature = "compress"))]
|
||||||
fut: Option<LocalBoxFuture<'static, Result<U, JsonPayloadError>>>,
|
payload: Payload,
|
||||||
|
buf: BytesMut,
|
||||||
|
_res: PhantomData<U>,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<U> Unpin for JsonBody<U> {}
|
||||||
|
|
||||||
impl<U> JsonBody<U>
|
impl<U> JsonBody<U>
|
||||||
where
|
where
|
||||||
U: DeserializeOwned + 'static,
|
U: DeserializeOwned + 'static,
|
||||||
@ -340,39 +373,58 @@ where
|
|||||||
};
|
};
|
||||||
|
|
||||||
if !json {
|
if !json {
|
||||||
return JsonBody {
|
return JsonBody::Error(Some(JsonPayloadError::ContentType));
|
||||||
limit: 262_144,
|
|
||||||
length: None,
|
|
||||||
stream: None,
|
|
||||||
fut: None,
|
|
||||||
err: Some(JsonPayloadError::ContentType),
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let len = req
|
let length = req
|
||||||
.headers()
|
.headers()
|
||||||
.get(&CONTENT_LENGTH)
|
.get(&CONTENT_LENGTH)
|
||||||
.and_then(|l| l.to_str().ok())
|
.and_then(|l| l.to_str().ok())
|
||||||
.and_then(|s| s.parse::<usize>().ok());
|
.and_then(|s| s.parse::<usize>().ok());
|
||||||
|
|
||||||
|
// Notice the content_length is not checked against limit of json config here.
|
||||||
|
// As the internal usage always call JsonBody::limit after JsonBody::new.
|
||||||
|
// And limit check to return an error variant of JsonBody happens there.
|
||||||
|
|
||||||
#[cfg(feature = "compress")]
|
#[cfg(feature = "compress")]
|
||||||
let payload = Decompress::from_headers(payload.take(), req.headers());
|
let payload = Decompress::from_headers(payload.take(), req.headers());
|
||||||
#[cfg(not(feature = "compress"))]
|
#[cfg(not(feature = "compress"))]
|
||||||
let payload = payload.take();
|
let payload = payload.take();
|
||||||
|
|
||||||
JsonBody {
|
JsonBody::Body {
|
||||||
limit: 262_144,
|
limit: 262_144,
|
||||||
length: len,
|
length,
|
||||||
stream: Some(payload),
|
payload,
|
||||||
fut: None,
|
buf: BytesMut::with_capacity(8192),
|
||||||
err: None,
|
_res: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Change max size of payload. By default max size is 256Kb
|
/// Change max size of payload. By default max size is 256Kb
|
||||||
pub fn limit(mut self, limit: usize) -> Self {
|
pub fn limit(self, limit: usize) -> Self {
|
||||||
self.limit = limit;
|
match self {
|
||||||
self
|
JsonBody::Body {
|
||||||
|
length,
|
||||||
|
payload,
|
||||||
|
buf,
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
if let Some(len) = length {
|
||||||
|
if len > limit {
|
||||||
|
return JsonBody::Error(Some(JsonPayloadError::Overflow));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
JsonBody::Body {
|
||||||
|
limit,
|
||||||
|
length,
|
||||||
|
payload,
|
||||||
|
buf,
|
||||||
|
_res: PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
JsonBody::Error(e) => JsonBody::Error(e),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -382,41 +434,34 @@ where
|
|||||||
{
|
{
|
||||||
type Output = Result<U, JsonPayloadError>;
|
type Output = Result<U, JsonPayloadError>;
|
||||||
|
|
||||||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
if let Some(ref mut fut) = self.fut {
|
let this = self.get_mut();
|
||||||
return Pin::new(fut).poll(cx);
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(err) = self.err.take() {
|
match this {
|
||||||
return Poll::Ready(Err(err));
|
JsonBody::Body {
|
||||||
}
|
limit,
|
||||||
|
buf,
|
||||||
let limit = self.limit;
|
payload,
|
||||||
if let Some(len) = self.length.take() {
|
..
|
||||||
if len > limit {
|
} => loop {
|
||||||
return Poll::Ready(Err(JsonPayloadError::Overflow));
|
let res = ready!(Pin::new(&mut *payload).poll_next(cx));
|
||||||
}
|
match res {
|
||||||
}
|
Some(chunk) => {
|
||||||
let mut stream = self.stream.take().unwrap();
|
let chunk = chunk?;
|
||||||
|
if (buf.len() + chunk.len()) > *limit {
|
||||||
self.fut = Some(
|
return Poll::Ready(Err(JsonPayloadError::Overflow));
|
||||||
async move {
|
} else {
|
||||||
let mut body = BytesMut::with_capacity(8192);
|
buf.extend_from_slice(&chunk);
|
||||||
|
}
|
||||||
while let Some(item) = stream.next().await {
|
}
|
||||||
let chunk = item?;
|
None => {
|
||||||
if (body.len() + chunk.len()) > limit {
|
let json = serde_json::from_slice::<U>(&buf)?;
|
||||||
return Err(JsonPayloadError::Overflow);
|
return Poll::Ready(Ok(json));
|
||||||
} else {
|
|
||||||
body.extend_from_slice(&chunk);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(serde_json::from_slice::<U>(&body)?)
|
},
|
||||||
}
|
JsonBody::Error(e) => Poll::Ready(Err(e.take().unwrap())),
|
||||||
.boxed_local(),
|
}
|
||||||
);
|
|
||||||
|
|
||||||
self.poll(cx)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user