mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-27 09:42:57 +01:00
feat: use PRITIT for FromRequest
This commit is contained in:
parent
fdef224a06
commit
9a4090761c
@ -3,7 +3,6 @@ use std::{
|
|||||||
str::FromStr,
|
str::FromStr,
|
||||||
};
|
};
|
||||||
|
|
||||||
use actix_utils::future::{ready, Ready};
|
|
||||||
use actix_web::{dev::Payload, FromRequest, HttpRequest};
|
use actix_web::{dev::Payload, FromRequest, HttpRequest};
|
||||||
|
|
||||||
use crate::error::UriSegmentError;
|
use crate::error::UriSegmentError;
|
||||||
@ -88,10 +87,10 @@ 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>>;
|
|
||||||
|
|
||||||
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
|
#[inline]
|
||||||
ready(req.match_info().unprocessed().parse())
|
async fn from_request(req: &HttpRequest, _: &mut Payload) -> Result<Self, Self::Error> {
|
||||||
|
req.match_info().unprocessed().parse()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -100,6 +100,7 @@ serde_json = "1.0"
|
|||||||
serde_urlencoded = "0.7"
|
serde_urlencoded = "0.7"
|
||||||
smallvec = "1.6.1"
|
smallvec = "1.6.1"
|
||||||
socket2 = "0.5"
|
socket2 = "0.5"
|
||||||
|
tokio = { version = "1.24.2", features = ["macros"] }
|
||||||
time = { version = "0.3", default-features = false, features = ["formatting"] }
|
time = { version = "0.3", default-features = false, features = ["formatting"] }
|
||||||
url = "2.1"
|
url = "2.1"
|
||||||
|
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
use std::{any::type_name, ops::Deref, sync::Arc};
|
use std::{any::type_name, ops::Deref, sync::Arc};
|
||||||
|
|
||||||
use actix_http::Extensions;
|
use actix_http::Extensions;
|
||||||
use actix_utils::future::{err, ok, Ready};
|
|
||||||
use futures_core::future::LocalBoxFuture;
|
use futures_core::future::LocalBoxFuture;
|
||||||
use serde::{de, Serialize};
|
use serde::{de, Serialize};
|
||||||
|
|
||||||
@ -159,12 +158,11 @@ where
|
|||||||
|
|
||||||
impl<T: ?Sized + 'static> FromRequest for Data<T> {
|
impl<T: ?Sized + 'static> FromRequest for Data<T> {
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
type Future = Ready<Result<Self, Error>>;
|
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
|
async fn from_request(req: &HttpRequest, _: &mut Payload) -> Result<Self, Self::Error> {
|
||||||
if let Some(st) = req.app_data::<Data<T>>() {
|
if let Some(st) = req.app_data::<Data<T>>() {
|
||||||
ok(st.clone())
|
Ok(st.clone())
|
||||||
} else {
|
} else {
|
||||||
log::debug!(
|
log::debug!(
|
||||||
"Failed to extract `Data<{}>` for `{}` handler. For the Data extractor to work \
|
"Failed to extract `Data<{}>` for `{}` handler. For the Data extractor to work \
|
||||||
@ -174,7 +172,7 @@ impl<T: ?Sized + 'static> FromRequest for Data<T> {
|
|||||||
req.match_name().unwrap_or_else(|| req.path())
|
req.match_name().unwrap_or_else(|| req.path())
|
||||||
);
|
);
|
||||||
|
|
||||||
err(error::ErrorInternalServerError(
|
Err(error::ErrorInternalServerError(
|
||||||
"Requested application data is not configured correctly. \
|
"Requested application data is not configured correctly. \
|
||||||
View/enable debug logs for more details.",
|
View/enable debug logs for more details.",
|
||||||
))
|
))
|
||||||
|
@ -3,13 +3,11 @@
|
|||||||
use std::{
|
use std::{
|
||||||
convert::Infallible,
|
convert::Infallible,
|
||||||
future::Future,
|
future::Future,
|
||||||
marker::PhantomData,
|
|
||||||
pin::Pin,
|
pin::Pin,
|
||||||
task::{Context, Poll},
|
task::{Context, Poll},
|
||||||
};
|
};
|
||||||
|
|
||||||
use actix_http::{Method, Uri};
|
use actix_http::{Method, Uri};
|
||||||
use actix_utils::future::{ok, Ready};
|
|
||||||
use futures_core::ready;
|
use futures_core::ready;
|
||||||
use pin_project_lite::pin_project;
|
use pin_project_lite::pin_project;
|
||||||
|
|
||||||
@ -66,33 +64,17 @@ pub trait FromRequest: Sized {
|
|||||||
/// The associated error which can be returned.
|
/// The associated error which can be returned.
|
||||||
type Error: Into<Error>;
|
type Error: Into<Error>;
|
||||||
|
|
||||||
/// Future that resolves to a `Self`.
|
/// Creates a `Self` from request parts asynchronously.
|
||||||
///
|
fn from_request(
|
||||||
/// To use an async function or block, the futures must be boxed. The following snippet will be
|
req: &HttpRequest,
|
||||||
/// common when creating async/await extractors (that do not consume the body).
|
payload: &mut Payload,
|
||||||
///
|
) -> impl Future<Output = Result<Self, Self::Error>>;
|
||||||
/// ```ignore
|
|
||||||
/// type Future = Pin<Box<dyn Future<Output = Result<Self, Self::Error>>>>;
|
|
||||||
/// // or
|
|
||||||
/// type Future = futures_util::future::LocalBoxFuture<'static, Result<Self, Self::Error>>;
|
|
||||||
///
|
|
||||||
/// fn from_request(req: HttpRequest, ...) -> Self::Future {
|
|
||||||
/// let req = req.clone();
|
|
||||||
/// Box::pin(async move {
|
|
||||||
/// ...
|
|
||||||
/// })
|
|
||||||
/// }
|
|
||||||
/// ```
|
|
||||||
type Future: Future<Output = Result<Self, Self::Error>>;
|
|
||||||
|
|
||||||
/// Create a `Self` from request parts asynchronously.
|
|
||||||
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future;
|
|
||||||
|
|
||||||
/// Create a `Self` from request head asynchronously.
|
/// Create a `Self` from request head asynchronously.
|
||||||
///
|
///
|
||||||
/// This method is short for `T::from_request(req, &mut Payload::None)`.
|
/// This method is short for `T::from_request(req, &mut Payload::None)`.
|
||||||
fn extract(req: &HttpRequest) -> Self::Future {
|
fn extract(req: &HttpRequest) -> impl Future<Output = Result<Self, Self::Error>> {
|
||||||
Self::from_request(req, &mut Payload::None)
|
async { Self::from_request(req, &mut Payload::None).await }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -146,12 +128,19 @@ where
|
|||||||
T: FromRequest,
|
T: FromRequest,
|
||||||
{
|
{
|
||||||
type Error = Infallible;
|
type Error = Infallible;
|
||||||
type Future = FromRequestOptFuture<T::Future>;
|
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
|
async fn from_request(req: &HttpRequest, payload: &mut Payload) -> Result<Self, Self::Error> {
|
||||||
FromRequestOptFuture {
|
match T::from_request(req, payload).await {
|
||||||
fut: T::from_request(req, payload),
|
Ok(t) => Ok(Some(t)),
|
||||||
|
Err(err) => {
|
||||||
|
log::debug!(
|
||||||
|
"Error from `Option<{}>` extractor: {}",
|
||||||
|
std::any::type_name::<T>(),
|
||||||
|
err.into()
|
||||||
|
);
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -203,9 +192,11 @@ where
|
|||||||
///
|
///
|
||||||
/// impl FromRequest for Thing {
|
/// impl FromRequest for Thing {
|
||||||
/// type Error = Error;
|
/// type Error = Error;
|
||||||
/// type Future = Ready<Result<Thing, Error>>;
|
|
||||||
///
|
///
|
||||||
/// fn from_request(req: &HttpRequest, payload: &mut dev::Payload) -> Self::Future {
|
/// async fn from_request(
|
||||||
|
/// req: &HttpRequest,
|
||||||
|
/// payload: &mut dev::Payload,
|
||||||
|
/// ) -> Result<Self, Self::Error> {
|
||||||
/// if rand::random() {
|
/// if rand::random() {
|
||||||
/// ok(Thing { name: "thingy".into() })
|
/// ok(Thing { name: "thingy".into() })
|
||||||
/// } else {
|
/// } else {
|
||||||
@ -232,36 +223,10 @@ where
|
|||||||
T::Error: Into<E>,
|
T::Error: Into<E>,
|
||||||
{
|
{
|
||||||
type Error = Infallible;
|
type Error = Infallible;
|
||||||
type Future = FromRequestResFuture<T::Future, E>;
|
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
|
async fn from_request(req: &HttpRequest, payload: &mut Payload) -> Result<Self, Self::Error> {
|
||||||
FromRequestResFuture {
|
Ok(T::from_request(req, payload).await.map_err(Into::into))
|
||||||
fut: T::from_request(req, payload),
|
|
||||||
_phantom: PhantomData,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pin_project! {
|
|
||||||
pub struct FromRequestResFuture<Fut, E> {
|
|
||||||
#[pin]
|
|
||||||
fut: Fut,
|
|
||||||
_phantom: PhantomData<E>,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<Fut, T, Ei, E> Future for FromRequestResFuture<Fut, E>
|
|
||||||
where
|
|
||||||
Fut: Future<Output = Result<T, Ei>>,
|
|
||||||
Ei: Into<E>,
|
|
||||||
{
|
|
||||||
type Output = Result<Result<T, E>, Infallible>;
|
|
||||||
|
|
||||||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
|
||||||
let this = self.project();
|
|
||||||
let res = ready!(this.fut.poll(cx));
|
|
||||||
Poll::Ready(Ok(res.map_err(Into::into)))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -279,10 +244,9 @@ where
|
|||||||
/// ```
|
/// ```
|
||||||
impl FromRequest for Uri {
|
impl FromRequest for Uri {
|
||||||
type Error = Infallible;
|
type Error = Infallible;
|
||||||
type Future = Ready<Result<Self, Self::Error>>;
|
|
||||||
|
|
||||||
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
|
async fn from_request(req: &HttpRequest, _: &mut Payload) -> Result<Self, Self::Error> {
|
||||||
ok(req.uri().clone())
|
Ok(req.uri().clone())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -300,10 +264,9 @@ 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>>;
|
|
||||||
|
|
||||||
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
|
async fn from_request(req: &HttpRequest, _: &mut Payload) -> Result<Self, Self::Error> {
|
||||||
ok(req.method().clone())
|
Ok(req.method().clone())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -319,88 +282,24 @@ mod tuple_from_req {
|
|||||||
impl<$($T: FromRequest + 'static),+> FromRequest for ($($T,)+)
|
impl<$($T: FromRequest + 'static),+> FromRequest for ($($T,)+)
|
||||||
{
|
{
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
type Future = $fut<$($T),+>;
|
|
||||||
|
|
||||||
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
|
async fn from_request(req: &HttpRequest, payload: &mut Payload) -> Result<Self, Self::Error> {
|
||||||
$fut {
|
|
||||||
$(
|
$(
|
||||||
$T: ExtractFuture::Future {
|
let $T = $T::from_request(req, payload).await.map_err(Into::into)?;
|
||||||
fut: $T::from_request(req, payload)
|
|
||||||
},
|
|
||||||
)+
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pin_project! {
|
|
||||||
pub struct $fut<$($T: FromRequest),+> {
|
|
||||||
$(
|
|
||||||
#[pin]
|
|
||||||
$T: ExtractFuture<$T::Future, $T>,
|
|
||||||
)+
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<$($T: FromRequest),+> Future for $fut<$($T),+>
|
|
||||||
{
|
|
||||||
type Output = Result<($($T,)+), Error>;
|
|
||||||
|
|
||||||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
|
||||||
let mut this = self.project();
|
|
||||||
|
|
||||||
let mut ready = true;
|
|
||||||
$(
|
|
||||||
match this.$T.as_mut().project() {
|
|
||||||
ExtractProj::Future { fut } => match fut.poll(cx) {
|
|
||||||
Poll::Ready(Ok(output)) => {
|
|
||||||
let _ = this.$T.as_mut().project_replace(ExtractFuture::Done { output });
|
|
||||||
},
|
|
||||||
Poll::Ready(Err(err)) => return Poll::Ready(Err(err.into())),
|
|
||||||
Poll::Pending => ready = false,
|
|
||||||
},
|
|
||||||
ExtractProj::Done { .. } => {},
|
|
||||||
ExtractProj::Empty => unreachable!("FromRequest polled after finished"),
|
|
||||||
}
|
|
||||||
)+
|
)+
|
||||||
|
|
||||||
if ready {
|
Ok(($($T,)+))
|
||||||
Poll::Ready(Ok(
|
|
||||||
($(
|
|
||||||
match this.$T.project_replace(ExtractFuture::Empty) {
|
|
||||||
ExtractReplaceProj::Done { output } => output,
|
|
||||||
_ => unreachable!("FromRequest polled after finished"),
|
|
||||||
},
|
|
||||||
)+)
|
|
||||||
))
|
|
||||||
} else {
|
|
||||||
Poll::Pending
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pin_project! {
|
|
||||||
#[project = ExtractProj]
|
|
||||||
#[project_replace = ExtractReplaceProj]
|
|
||||||
enum ExtractFuture<Fut, Res> {
|
|
||||||
Future {
|
|
||||||
#[pin]
|
|
||||||
fut: Fut
|
|
||||||
},
|
|
||||||
Done {
|
|
||||||
output: Res,
|
|
||||||
},
|
|
||||||
Empty
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FromRequest for () {
|
impl FromRequest for () {
|
||||||
type Error = Infallible;
|
type Error = Infallible;
|
||||||
type Future = Ready<Result<Self, Self::Error>>;
|
|
||||||
|
|
||||||
fn from_request(_: &HttpRequest, _: &mut Payload) -> Self::Future {
|
#[inline]
|
||||||
ok(())
|
async fn from_request(_: &HttpRequest, _: &mut Payload) -> Result<Self, Self::Error> {
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
use std::{convert::Infallible, net::SocketAddr};
|
use std::{convert::Infallible, net::SocketAddr};
|
||||||
|
|
||||||
use actix_utils::future::{err, ok, Ready};
|
|
||||||
use derive_more::{Display, Error};
|
use derive_more::{Display, Error};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
@ -198,10 +197,10 @@ impl ConnectionInfo {
|
|||||||
|
|
||||||
impl FromRequest for ConnectionInfo {
|
impl FromRequest for ConnectionInfo {
|
||||||
type Error = Infallible;
|
type Error = Infallible;
|
||||||
type Future = Ready<Result<Self, Self::Error>>;
|
|
||||||
|
|
||||||
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
|
#[inline]
|
||||||
ok(req.connection_info().clone())
|
async fn from_request(req: &HttpRequest, _: &mut Payload) -> Result<Self, Self::Error> {
|
||||||
|
Ok(req.connection_info().clone())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -240,14 +239,14 @@ 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>>;
|
|
||||||
|
|
||||||
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
|
#[inline]
|
||||||
|
async fn from_request(req: &HttpRequest, _: &mut Payload) -> Result<Self, Self::Error> {
|
||||||
match req.peer_addr() {
|
match req.peer_addr() {
|
||||||
Some(addr) => ok(PeerAddr(addr)),
|
Some(addr) => Ok(PeerAddr(addr)),
|
||||||
None => {
|
None => {
|
||||||
log::error!("Missing peer address.");
|
log::error!("Missing peer address.");
|
||||||
err(MissingPeerAddr)
|
Err(MissingPeerAddr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
use std::{
|
use std::{
|
||||||
cell::{Ref, RefCell, RefMut},
|
cell::{Ref, RefCell, RefMut},
|
||||||
|
convert::Infallible,
|
||||||
fmt, net,
|
fmt, net,
|
||||||
rc::Rc,
|
rc::Rc,
|
||||||
str,
|
str,
|
||||||
@ -7,7 +8,6 @@ use std::{
|
|||||||
|
|
||||||
use actix_http::{Message, RequestHead};
|
use actix_http::{Message, RequestHead};
|
||||||
use actix_router::{Path, Url};
|
use actix_router::{Path, Url};
|
||||||
use actix_utils::future::{ok, Ready};
|
|
||||||
#[cfg(feature = "cookies")]
|
#[cfg(feature = "cookies")]
|
||||||
use cookie::{Cookie, ParseError as CookieParseError};
|
use cookie::{Cookie, ParseError as CookieParseError};
|
||||||
use smallvec::SmallVec;
|
use smallvec::SmallVec;
|
||||||
@ -20,7 +20,7 @@ use crate::{
|
|||||||
http::{header::HeaderMap, Method, Uri, Version},
|
http::{header::HeaderMap, Method, Uri, Version},
|
||||||
info::ConnectionInfo,
|
info::ConnectionInfo,
|
||||||
rmap::ResourceMap,
|
rmap::ResourceMap,
|
||||||
Error, FromRequest, HttpMessage,
|
FromRequest, HttpMessage,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[cfg(feature = "cookies")]
|
#[cfg(feature = "cookies")]
|
||||||
@ -417,12 +417,11 @@ impl Drop for HttpRequest {
|
|||||||
/// );
|
/// );
|
||||||
/// ```
|
/// ```
|
||||||
impl FromRequest for HttpRequest {
|
impl FromRequest for HttpRequest {
|
||||||
type Error = Error;
|
type Error = Infallible;
|
||||||
type Future = Ready<Result<Self, Error>>;
|
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
|
async fn from_request(req: &HttpRequest, _: &mut Payload) -> Result<Self, Self::Error> {
|
||||||
ok(req.clone())
|
Ok(req.clone())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,4 @@
|
|||||||
use std::{any::type_name, ops::Deref};
|
use std::ops::Deref;
|
||||||
|
|
||||||
use actix_utils::future::{err, ok, Ready};
|
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
dev::Payload, error::ErrorInternalServerError, Error, FromRequest, HttpMessage as _,
|
dev::Payload, error::ErrorInternalServerError, Error, FromRequest, HttpMessage as _,
|
||||||
@ -66,19 +64,19 @@ 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 Error = Error;
|
type Error = Error;
|
||||||
type Future = Ready<Result<Self, Error>>;
|
|
||||||
|
|
||||||
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
|
#[inline]
|
||||||
|
async fn from_request(req: &HttpRequest, _: &mut Payload) -> Result<Self, Self::Error> {
|
||||||
if let Some(st) = req.extensions().get::<T>() {
|
if let Some(st) = req.extensions().get::<T>() {
|
||||||
ok(ReqData(st.clone()))
|
Ok(ReqData(st.clone()))
|
||||||
} else {
|
} else {
|
||||||
log::debug!(
|
log::debug!(
|
||||||
"Failed to construct App-level ReqData extractor. \
|
"Failed to extract App-level ReqData<{}>. Request path: {}",
|
||||||
Request path: {:?} (type: {})",
|
std::any::type_name::<T>(),
|
||||||
req.path(),
|
req.path(),
|
||||||
type_name::<T>(),
|
|
||||||
);
|
);
|
||||||
err(ErrorInternalServerError(
|
|
||||||
|
Err(ErrorInternalServerError(
|
||||||
"Missing expected request extension data",
|
"Missing expected request extension data",
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
@ -125,11 +125,11 @@ impl ServiceRequest {
|
|||||||
/// # todo!()
|
/// # todo!()
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
pub fn extract<T>(&mut self) -> <T as FromRequest>::Future
|
pub async fn extract<T: FromRequest>(&mut self) -> Result<T, <T as FromRequest>::Error>
|
||||||
where
|
where
|
||||||
T: FromRequest,
|
T: FromRequest,
|
||||||
{
|
{
|
||||||
T::from_request(&self.req, &mut self.payload)
|
T::from_request(&self.req, &mut self.payload).await
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Construct request from parts.
|
/// Construct request from parts.
|
||||||
|
@ -1,15 +1,6 @@
|
|||||||
//! For either helper, see [`Either`].
|
//! For either helper, see [`Either`].
|
||||||
|
|
||||||
use std::{
|
|
||||||
future::Future,
|
|
||||||
mem,
|
|
||||||
pin::Pin,
|
|
||||||
task::{Context, Poll},
|
|
||||||
};
|
|
||||||
|
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
use futures_core::ready;
|
|
||||||
use pin_project_lite::pin_project;
|
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
body::EitherBody,
|
body::EitherBody,
|
||||||
@ -158,7 +149,7 @@ where
|
|||||||
fn from(err: EitherExtractError<L, R>) -> Error {
|
fn from(err: EitherExtractError<L, R>) -> Error {
|
||||||
match err {
|
match err {
|
||||||
EitherExtractError::Bytes(err) => err,
|
EitherExtractError::Bytes(err) => err,
|
||||||
EitherExtractError::Extract(a_err, _b_err) => a_err.into(),
|
EitherExtractError::Extract(l_err, _r_err) => l_err.into(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -170,112 +161,25 @@ where
|
|||||||
R: FromRequest + 'static,
|
R: FromRequest + 'static,
|
||||||
{
|
{
|
||||||
type Error = EitherExtractError<L::Error, R::Error>;
|
type Error = EitherExtractError<L::Error, R::Error>;
|
||||||
type Future = EitherExtractFut<L, R>;
|
|
||||||
|
|
||||||
fn from_request(req: &HttpRequest, payload: &mut dev::Payload) -> Self::Future {
|
async fn from_request(
|
||||||
EitherExtractFut {
|
req: &HttpRequest,
|
||||||
req: req.clone(),
|
payload: &mut dev::Payload,
|
||||||
state: EitherExtractState::Bytes {
|
) -> Result<Self, Self::Error> {
|
||||||
bytes: Bytes::from_request(req, payload),
|
let buf = Bytes::from_request(req, payload)
|
||||||
|
.await
|
||||||
|
.map_err(EitherExtractError::Bytes)?;
|
||||||
|
|
||||||
|
match L::from_request(req, &mut payload_from_bytes(buf.clone())).await {
|
||||||
|
Ok(left) => Ok(Either::Left(left)),
|
||||||
|
Err(l_err) => match R::from_request(req, &mut payload_from_bytes(buf)).await {
|
||||||
|
Ok(right) => Ok(Either::Right(right)),
|
||||||
|
Err(r_err) => Err(EitherExtractError::Extract(l_err, r_err)),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pin_project! {
|
|
||||||
pub struct EitherExtractFut<L, R>
|
|
||||||
where
|
|
||||||
R: FromRequest,
|
|
||||||
L: FromRequest,
|
|
||||||
{
|
|
||||||
req: HttpRequest,
|
|
||||||
#[pin]
|
|
||||||
state: EitherExtractState<L, R>,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pin_project! {
|
|
||||||
#[project = EitherExtractProj]
|
|
||||||
pub enum EitherExtractState<L, R>
|
|
||||||
where
|
|
||||||
L: FromRequest,
|
|
||||||
R: FromRequest,
|
|
||||||
{
|
|
||||||
Bytes {
|
|
||||||
#[pin]
|
|
||||||
bytes: <Bytes as FromRequest>::Future,
|
|
||||||
},
|
|
||||||
Left {
|
|
||||||
#[pin]
|
|
||||||
left: L::Future,
|
|
||||||
fallback: Bytes,
|
|
||||||
},
|
|
||||||
Right {
|
|
||||||
#[pin]
|
|
||||||
right: R::Future,
|
|
||||||
left_err: Option<L::Error>,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<R, RF, RE, L, LF, LE> Future for EitherExtractFut<L, R>
|
|
||||||
where
|
|
||||||
L: FromRequest<Future = LF, Error = LE>,
|
|
||||||
R: FromRequest<Future = RF, Error = RE>,
|
|
||||||
LF: Future<Output = Result<L, LE>> + 'static,
|
|
||||||
RF: Future<Output = Result<R, RE>> + 'static,
|
|
||||||
LE: Into<Error>,
|
|
||||||
RE: Into<Error>,
|
|
||||||
{
|
|
||||||
type Output = Result<Either<L, R>, EitherExtractError<LE, RE>>;
|
|
||||||
|
|
||||||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
|
||||||
let mut this = self.project();
|
|
||||||
let ready = loop {
|
|
||||||
let next = match this.state.as_mut().project() {
|
|
||||||
EitherExtractProj::Bytes { bytes } => {
|
|
||||||
let res = ready!(bytes.poll(cx));
|
|
||||||
match res {
|
|
||||||
Ok(bytes) => {
|
|
||||||
let fallback = bytes.clone();
|
|
||||||
let left = L::from_request(this.req, &mut payload_from_bytes(bytes));
|
|
||||||
EitherExtractState::Left { left, fallback }
|
|
||||||
}
|
|
||||||
Err(err) => break Err(EitherExtractError::Bytes(err)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
EitherExtractProj::Left { left, fallback } => {
|
|
||||||
let res = ready!(left.poll(cx));
|
|
||||||
match res {
|
|
||||||
Ok(extracted) => break Ok(Either::Left(extracted)),
|
|
||||||
Err(left_err) => {
|
|
||||||
let right = R::from_request(
|
|
||||||
this.req,
|
|
||||||
&mut payload_from_bytes(mem::take(fallback)),
|
|
||||||
);
|
|
||||||
EitherExtractState::Right {
|
|
||||||
left_err: Some(left_err),
|
|
||||||
right,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
EitherExtractProj::Right { right, left_err } => {
|
|
||||||
let res = ready!(right.poll(cx));
|
|
||||||
match res {
|
|
||||||
Ok(data) => break Ok(Either::Right(data)),
|
|
||||||
Err(err) => {
|
|
||||||
break Err(EitherExtractError::Extract(left_err.take().unwrap(), err));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
this.state.set(next);
|
|
||||||
};
|
|
||||||
Poll::Ready(ready)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn payload_from_bytes(bytes: Bytes) -> dev::Payload {
|
fn payload_from_bytes(bytes: Bytes) -> dev::Payload {
|
||||||
let (_, mut h1_payload) = actix_http::h1::Payload::create(true);
|
let (_, mut h1_payload) = actix_http::h1::Payload::create(true);
|
||||||
h1_payload.unread_data(bytes);
|
h1_payload.unread_data(bytes);
|
||||||
|
@ -13,7 +13,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, ready};
|
use futures_core::future::LocalBoxFuture;
|
||||||
use futures_util::{FutureExt as _, StreamExt as _};
|
use futures_util::{FutureExt as _, StreamExt as _};
|
||||||
use serde::{de::DeserializeOwned, Serialize};
|
use serde::{de::DeserializeOwned, Serialize};
|
||||||
|
|
||||||
@ -126,51 +126,21 @@ where
|
|||||||
T: DeserializeOwned + 'static,
|
T: DeserializeOwned + 'static,
|
||||||
{
|
{
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
type Future = FormExtractFut<T>;
|
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
|
async fn from_request(req: &HttpRequest, payload: &mut Payload) -> Result<Self, Self::Error> {
|
||||||
let FormConfig { limit, err_handler } = FormConfig::from_req(req).clone();
|
let FormConfig { limit, err_handler } = FormConfig::from_req(req);
|
||||||
|
|
||||||
FormExtractFut {
|
match UrlEncoded::new(req, payload).limit(*limit).await {
|
||||||
fut: UrlEncoded::new(req, payload).limit(limit),
|
Ok(form) => Ok(Form(form)),
|
||||||
req: req.clone(),
|
Err(err) => Err(match err_handler {
|
||||||
err_handler,
|
Some(err_handler) => (err_handler)(err, req),
|
||||||
|
None => err.into(),
|
||||||
|
}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type FormErrHandler = Option<Rc<dyn Fn(UrlencodedError, &HttpRequest) -> Error>>;
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T: fmt::Display> fmt::Display for Form<T> {
|
impl<T: fmt::Display> fmt::Display for Form<T> {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
self.0.fmt(f)
|
self.0.fmt(f)
|
||||||
@ -198,6 +168,8 @@ impl<T: Serialize> Responder for Form<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type FormErrHandler = Option<Rc<dyn Fn(UrlencodedError, &HttpRequest) -> Error>>;
|
||||||
|
|
||||||
/// [`Form`] extractor configuration.
|
/// [`Form`] extractor configuration.
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
|
@ -2,8 +2,6 @@
|
|||||||
|
|
||||||
use std::{fmt, ops};
|
use std::{fmt, ops};
|
||||||
|
|
||||||
use actix_utils::future::{ready, Ready};
|
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
dev::Payload, error::ParseError, extract::FromRequest, http::header::Header as ParseHeader,
|
dev::Payload, error::ParseError, extract::FromRequest, http::header::Header as ParseHeader,
|
||||||
HttpRequest,
|
HttpRequest,
|
||||||
@ -61,13 +59,12 @@ where
|
|||||||
T: ParseHeader,
|
T: ParseHeader,
|
||||||
{
|
{
|
||||||
type Error = ParseError;
|
type Error = ParseError;
|
||||||
type Future = Ready<Result<Self, Self::Error>>;
|
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
|
async fn from_request(req: &HttpRequest, _: &mut Payload) -> Result<Self, Self::Error> {
|
||||||
match ParseHeader::parse(req) {
|
match ParseHeader::parse(req) {
|
||||||
Ok(header) => ready(Ok(Header(header))),
|
Ok(header) => Ok(Header(header)),
|
||||||
Err(err) => ready(Err(err)),
|
Err(err) => Err(err),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -138,10 +138,8 @@ impl<T: Serialize> Responder for Json<T> {
|
|||||||
/// See [here](#extractor) for example of usage as an extractor.
|
/// See [here](#extractor) for example of usage as an extractor.
|
||||||
impl<T: DeserializeOwned> FromRequest for Json<T> {
|
impl<T: DeserializeOwned> FromRequest for Json<T> {
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
type Future = JsonExtractFut<T>;
|
|
||||||
|
|
||||||
#[inline]
|
async fn from_request(req: &HttpRequest, payload: &mut Payload) -> Result<Self, Self::Error> {
|
||||||
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
|
|
||||||
let config = JsonConfig::from_req(req);
|
let config = JsonConfig::from_req(req);
|
||||||
|
|
||||||
let limit = config.limit;
|
let limit = config.limit;
|
||||||
@ -149,52 +147,30 @@ impl<T: DeserializeOwned> FromRequest for Json<T> {
|
|||||||
let ctype_fn = config.content_type.as_deref();
|
let ctype_fn = config.content_type.as_deref();
|
||||||
let err_handler = config.err_handler.clone();
|
let err_handler = config.err_handler.clone();
|
||||||
|
|
||||||
JsonExtractFut {
|
match JsonBody::new(req, payload, ctype_fn, ctype_required)
|
||||||
req: Some(req.clone()),
|
.limit(limit)
|
||||||
fut: JsonBody::new(req, payload, ctype_fn, ctype_required).limit(limit),
|
.await
|
||||||
err_handler,
|
{
|
||||||
|
Ok(data) => Ok(Json(data)),
|
||||||
|
Err(err) => {
|
||||||
|
log::debug!(
|
||||||
|
"Failed to deserialize Json<{}> from payload. \
|
||||||
|
Request path: {}",
|
||||||
|
std::any::type_name::<T>(),
|
||||||
|
req.path(),
|
||||||
|
);
|
||||||
|
|
||||||
|
Err(match err_handler.as_ref() {
|
||||||
|
Some(err_handler) => (err_handler)(err, req),
|
||||||
|
None => err.into(),
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type JsonErrorHandler = Option<Arc<dyn Fn(JsonPayloadError, &HttpRequest) -> Error + Send + Sync>>;
|
type JsonErrorHandler = Option<Arc<dyn Fn(JsonPayloadError, &HttpRequest) -> Error + Send + Sync>>;
|
||||||
|
|
||||||
pub struct JsonExtractFut<T> {
|
|
||||||
req: Option<HttpRequest>,
|
|
||||||
fut: JsonBody<T>,
|
|
||||||
err_handler: JsonErrorHandler,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T: DeserializeOwned> Future for JsonExtractFut<T> {
|
|
||||||
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(err) => {
|
|
||||||
let req = this.req.take().unwrap();
|
|
||||||
log::debug!(
|
|
||||||
"Failed to deserialize Json from payload. \
|
|
||||||
Request path: {}",
|
|
||||||
req.path()
|
|
||||||
);
|
|
||||||
|
|
||||||
if let Some(err_handler) = this.err_handler.as_ref() {
|
|
||||||
Err((*err_handler)(err, &req))
|
|
||||||
} else {
|
|
||||||
Err(err.into())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(data) => Ok(Json(data)),
|
|
||||||
};
|
|
||||||
|
|
||||||
Poll::Ready(res)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// `Json` extractor configuration.
|
/// `Json` extractor configuration.
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
|
@ -3,7 +3,6 @@
|
|||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use actix_router::PathDeserializer;
|
use actix_router::PathDeserializer;
|
||||||
use actix_utils::future::{ready, Ready};
|
|
||||||
use derive_more::{AsRef, Deref, DerefMut, Display, From};
|
use derive_more::{AsRef, Deref, DerefMut, Display, From};
|
||||||
use serde::de;
|
use serde::de;
|
||||||
|
|
||||||
@ -69,16 +68,13 @@ where
|
|||||||
T: de::DeserializeOwned,
|
T: de::DeserializeOwned,
|
||||||
{
|
{
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
type Future = Ready<Result<Self, Self::Error>>;
|
|
||||||
|
|
||||||
#[inline]
|
async fn from_request(req: &HttpRequest, _: &mut Payload) -> Result<Self, Self::Error> {
|
||||||
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
|
|
||||||
let error_handler = req
|
let error_handler = req
|
||||||
.app_data::<PathConfig>()
|
.app_data::<PathConfig>()
|
||||||
.or_else(|| req.app_data::<Data<PathConfig>>().map(Data::get_ref))
|
.or_else(|| req.app_data::<Data<PathConfig>>().map(Data::get_ref))
|
||||||
.and_then(|c| c.err_handler.clone());
|
.and_then(|c| c.err_handler.clone());
|
||||||
|
|
||||||
ready(
|
|
||||||
de::Deserialize::deserialize(PathDeserializer::new(req.match_info()))
|
de::Deserialize::deserialize(PathDeserializer::new(req.match_info()))
|
||||||
.map(Path)
|
.map(Path)
|
||||||
.map_err(move |err| {
|
.map_err(move |err| {
|
||||||
@ -94,8 +90,7 @@ where
|
|||||||
} else {
|
} else {
|
||||||
ErrorNotFound(err)
|
ErrorNotFound(err)
|
||||||
}
|
}
|
||||||
}),
|
})
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9,9 +9,8 @@ use std::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use actix_http::error::PayloadError;
|
use actix_http::error::PayloadError;
|
||||||
use actix_utils::future::{ready, Either, Ready};
|
|
||||||
use bytes::{Bytes, BytesMut};
|
use bytes::{Bytes, BytesMut};
|
||||||
use encoding_rs::{Encoding, UTF_8};
|
use encoding_rs::Encoding;
|
||||||
use futures_core::{ready, stream::Stream};
|
use futures_core::{ready, stream::Stream};
|
||||||
use mime::Mime;
|
use mime::Mime;
|
||||||
|
|
||||||
@ -131,11 +130,13 @@ impl Stream for Payload {
|
|||||||
/// See [here](#Examples) for example of usage as an extractor.
|
/// See [here](#Examples) for example of usage as an extractor.
|
||||||
impl FromRequest for Payload {
|
impl FromRequest for Payload {
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
type Future = Ready<Result<Self, Self::Error>>;
|
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn from_request(_: &HttpRequest, payload: &mut dev::Payload) -> Self::Future {
|
async fn from_request(
|
||||||
ready(Ok(Payload(payload.take())))
|
_: &HttpRequest,
|
||||||
|
payload: &mut dev::Payload,
|
||||||
|
) -> Result<Self, Self::Error> {
|
||||||
|
Ok(Payload(payload.take()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -157,33 +158,21 @@ impl FromRequest for Payload {
|
|||||||
/// ```
|
/// ```
|
||||||
impl FromRequest for Bytes {
|
impl FromRequest for Bytes {
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
type Future = Either<BytesExtractFut, Ready<Result<Bytes, Error>>>;
|
|
||||||
|
|
||||||
#[inline]
|
async fn from_request(
|
||||||
fn from_request(req: &HttpRequest, payload: &mut dev::Payload) -> Self::Future {
|
req: &HttpRequest,
|
||||||
|
payload: &mut dev::Payload,
|
||||||
|
) -> Result<Self, Self::Error> {
|
||||||
// allow both Config and Data<Config>
|
// allow both Config and Data<Config>
|
||||||
let cfg = PayloadConfig::from_req(req);
|
let cfg = PayloadConfig::from_req(req);
|
||||||
|
|
||||||
if let Err(err) = cfg.check_mimetype(req) {
|
// check content-type
|
||||||
return Either::right(ready(Err(err)));
|
cfg.check_mimetype(req)?;
|
||||||
}
|
|
||||||
|
|
||||||
Either::left(BytesExtractFut {
|
HttpMessageBody::new(req, payload)
|
||||||
body_fut: HttpMessageBody::new(req, payload).limit(cfg.limit),
|
.limit(cfg.limit)
|
||||||
})
|
.await
|
||||||
}
|
.map_err(Into::into)
|
||||||
}
|
|
||||||
|
|
||||||
/// Future for `Bytes` extractor.
|
|
||||||
pub struct BytesExtractFut {
|
|
||||||
body_fut: HttpMessageBody,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Future for BytesExtractFut {
|
|
||||||
type Output = Result<Bytes, Error>;
|
|
||||||
|
|
||||||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
|
||||||
Pin::new(&mut self.body_fut).poll(cx).map_err(Into::into)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -204,49 +193,29 @@ impl Future for BytesExtractFut {
|
|||||||
/// }
|
/// }
|
||||||
impl FromRequest for String {
|
impl FromRequest for String {
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
type Future = Either<StringExtractFut, Ready<Result<String, Error>>>;
|
|
||||||
|
|
||||||
#[inline]
|
async fn from_request(
|
||||||
fn from_request(req: &HttpRequest, payload: &mut dev::Payload) -> Self::Future {
|
req: &HttpRequest,
|
||||||
|
payload: &mut dev::Payload,
|
||||||
|
) -> Result<Self, Self::Error> {
|
||||||
let cfg = PayloadConfig::from_req(req);
|
let cfg = PayloadConfig::from_req(req);
|
||||||
|
|
||||||
// check content-type
|
// check content-type
|
||||||
if let Err(err) = cfg.check_mimetype(req) {
|
cfg.check_mimetype(req)?;
|
||||||
return Either::right(ready(Err(err)));
|
|
||||||
}
|
|
||||||
|
|
||||||
// check charset
|
// check charset
|
||||||
let encoding = match req.encoding() {
|
let encoding = req.encoding()?;
|
||||||
Ok(enc) => enc,
|
|
||||||
Err(err) => return Either::right(ready(Err(err.into()))),
|
|
||||||
};
|
|
||||||
let limit = cfg.limit;
|
let limit = cfg.limit;
|
||||||
let body_fut = HttpMessageBody::new(req, payload).limit(limit);
|
|
||||||
|
|
||||||
Either::left(StringExtractFut { body_fut, encoding })
|
let body = HttpMessageBody::new(req, payload).limit(limit).await?;
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Future for `String` extractor.
|
|
||||||
pub struct StringExtractFut {
|
|
||||||
body_fut: HttpMessageBody,
|
|
||||||
encoding: &'static Encoding,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Future for StringExtractFut {
|
|
||||||
type Output = Result<String, Error>;
|
|
||||||
|
|
||||||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
|
||||||
let encoding = self.encoding;
|
|
||||||
|
|
||||||
Pin::new(&mut self.body_fut).poll(cx).map(|out| {
|
|
||||||
let body = out?;
|
|
||||||
bytes_to_string(body, encoding)
|
bytes_to_string(body, encoding)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn bytes_to_string(body: Bytes, encoding: &'static Encoding) -> Result<String, Error> {
|
fn bytes_to_string(body: Bytes, encoding: &'static Encoding) -> Result<String, Error> {
|
||||||
|
use encoding_rs::UTF_8;
|
||||||
|
|
||||||
if encoding == UTF_8 {
|
if encoding == UTF_8 {
|
||||||
Ok(str::from_utf8(body.as_ref())
|
Ok(str::from_utf8(body.as_ref())
|
||||||
.map_err(|_| ErrorBadRequest("Can not decode body"))?
|
.map_err(|_| ErrorBadRequest("Can not decode body"))?
|
||||||
|
@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
use std::{fmt, ops, sync::Arc};
|
use std::{fmt, ops, sync::Arc};
|
||||||
|
|
||||||
use actix_utils::future::{err, ok, Ready};
|
|
||||||
use serde::de::DeserializeOwned;
|
use serde::de::DeserializeOwned;
|
||||||
|
|
||||||
use crate::{dev::Payload, error::QueryPayloadError, Error, FromRequest, HttpRequest};
|
use crate::{dev::Payload, error::QueryPayloadError, Error, FromRequest, HttpRequest};
|
||||||
@ -108,32 +107,27 @@ impl<T: fmt::Display> fmt::Display for Query<T> {
|
|||||||
/// See [here](#Examples) for example of usage as an extractor.
|
/// See [here](#Examples) for example of usage as an extractor.
|
||||||
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>>;
|
|
||||||
|
|
||||||
#[inline]
|
async fn from_request(req: &HttpRequest, _: &mut Payload) -> Result<Self, Self::Error> {
|
||||||
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
|
|
||||||
let error_handler = req
|
let error_handler = req
|
||||||
.app_data::<QueryConfig>()
|
.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())
|
||||||
.map(|val| ok(Query(val)))
|
.map(|val| Ok(Query(val)))
|
||||||
.unwrap_or_else(move |e| {
|
.unwrap_or_else(move |e| {
|
||||||
let e = QueryPayloadError::Deserialize(e);
|
let err = QueryPayloadError::Deserialize(e);
|
||||||
|
|
||||||
log::debug!(
|
log::debug!(
|
||||||
"Failed during Query extractor deserialization. \
|
"Failed during Query extractor deserialization. \
|
||||||
Request path: {:?}",
|
Request path: {}",
|
||||||
req.path()
|
req.path()
|
||||||
);
|
);
|
||||||
|
|
||||||
let e = if let Some(error_handler) = error_handler {
|
Err(match error_handler {
|
||||||
(error_handler)(e, req)
|
Some(error_handler) => (error_handler)(err, req),
|
||||||
} else {
|
None => err.into(),
|
||||||
e.into()
|
})
|
||||||
};
|
|
||||||
|
|
||||||
err(e)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
1
rust-toolchain
Normal file
1
rust-toolchain
Normal file
@ -0,0 +1 @@
|
|||||||
|
nightly
|
Loading…
Reference in New Issue
Block a user