use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
use futures::future::{ok, FutureExt, LocalBoxFuture, Ready};
use crate::http::Payload;
use super::error::ErrorRenderer;
use super::httprequest::HttpRequest;
pub trait FromRequest<Err>: Sized {
type Error;
type Future: Future<Output = Result<Self, Self::Error>>;
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future;
fn extract(req: &HttpRequest) -> Self::Future {
Self::from_request(req, &mut Payload::None)
}
}
impl<T, Err> FromRequest<Err> for Option<T>
where
T: FromRequest<Err> + 'static,
T::Future: 'static,
Err: ErrorRenderer,
<T as FromRequest<Err>>::Error: Into<Err::Container>,
{
type Error = Err::Container;
type Future = LocalBoxFuture<'static, Result<Option<T>, Self::Error>>;
#[inline]
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
T::from_request(req, payload)
.then(|r| match r {
Ok(v) => ok(Some(v)),
Err(e) => {
log::debug!("Error for Option<T> extractor: {}", e.into());
ok(None)
}
})
.boxed_local()
}
}
impl<T, E> FromRequest<E> for Result<T, T::Error>
where
T: FromRequest<E> + 'static,
T::Error: 'static,
T::Future: 'static,
E: ErrorRenderer,
{
type Error = T::Error;
type Future = LocalBoxFuture<'static, Result<Result<T, T::Error>, Self::Error>>;
#[inline]
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
T::from_request(req, payload)
.then(|res| match res {
Ok(v) => ok(Ok(v)),
Err(e) => ok(Err(e)),
})
.boxed_local()
}
}
#[doc(hidden)]
impl<E: ErrorRenderer> FromRequest<E> for () {
type Error = E::Container;
type Future = Ready<Result<(), E::Container>>;
fn from_request(_: &HttpRequest, _: &mut Payload) -> Self::Future {
ok(())
}
}
macro_rules! tuple_from_req ({$fut_type:ident, $(($n:tt, $T:ident)),+} => {
#[allow(unused_parens)]
impl<Err: ErrorRenderer, $($T: FromRequest<Err> + 'static),+> FromRequest<Err> for ($($T,)+)
where
$(<$T as $crate::web::FromRequest<Err>>::Error: Into<Err::Container>),+
{
type Error = Err::Container;
type Future = $fut_type<Err, $($T),+>;
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
$fut_type {
items: <($(Option<$T>,)+)>::default(),
$($T: $T::from_request(req, payload),)+
}
}
}
pin_project_lite::pin_project! {
#[doc(hidden)]
pub struct $fut_type<Err: ErrorRenderer, $($T: FromRequest<Err>),+>
{
items: ($(Option<$T>,)+),
$(#[pin] $T: $T::Future),+
}
}
impl<Err: ErrorRenderer, $($T: FromRequest<Err>),+> Future for $fut_type<Err, $($T),+>
where
$(<$T as $crate::web::FromRequest<Err>>::Error: Into<Err::Container>),+
{
type Output = Result<($($T,)+), Err::Container>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project();
let mut ready = true;
$(
if this.items.$n.is_none() {
match this.$T.poll(cx) {
Poll::Ready(Ok(item)) => {
this.items.$n = Some(item);
}
Poll::Pending => ready = false,
Poll::Ready(Err(e)) => return Poll::Ready(Err(e.into())),
}
}
)+
if ready {
Poll::Ready(Ok(
($(this.items.$n.take().unwrap(),)+)
))
} else {
Poll::Pending
}
}
}
});
#[allow(non_snake_case)]
#[rustfmt::skip]
mod m {
use super::*;
tuple_from_req!(TupleFromRequest1, (0, A));
tuple_from_req!(TupleFromRequest2, (0, A), (1, B));
tuple_from_req!(TupleFromRequest3, (0, A), (1, B), (2, C));
tuple_from_req!(TupleFromRequest4, (0, A), (1, B), (2, C), (3, D));
tuple_from_req!(TupleFromRequest5, (0, A), (1, B), (2, C), (3, D), (4, E));
tuple_from_req!(TupleFromRequest6, (0, A), (1, B), (2, C), (3, D), (4, E), (5, F));
tuple_from_req!(TupleFromRequest7, (0, A), (1, B), (2, C), (3, D), (4, E), (5, F), (6, G));
tuple_from_req!(TupleFromRequest8, (0, A), (1, B), (2, C), (3, D), (4, E), (5, F), (6, G), (7, H));
tuple_from_req!(TupleFromRequest9, (0, A), (1, B), (2, C), (3, D), (4, E), (5, F), (6, G), (7, H), (8, I));
tuple_from_req!(TupleFromRequest10, (0, A), (1, B), (2, C), (3, D), (4, E), (5, F), (6, G), (7, H), (8, I), (9, J));
}
#[cfg(test)]
mod tests {
use bytes::Bytes;
use serde_derive::Deserialize;
use crate::http::header;
use crate::web::error::UrlencodedError;
use crate::web::test::{from_request, TestRequest};
use crate::web::types::{Form, FormConfig};
#[derive(Deserialize, Debug, PartialEq)]
struct Info {
hello: String,
}
#[crate::rt_test]
async fn test_option() {
let (req, mut pl) = TestRequest::with_header(
header::CONTENT_TYPE,
"application/x-www-form-urlencoded",
)
.data(FormConfig::default().limit(4096))
.to_http_parts();
let r = from_request::<Option<Form<Info>>>(&req, &mut pl)
.await
.unwrap();
assert_eq!(r, None);
let (req, mut pl) = TestRequest::with_header(
header::CONTENT_TYPE,
"application/x-www-form-urlencoded",
)
.header(header::CONTENT_LENGTH, "9")
.set_payload(Bytes::from_static(b"hello=world"))
.to_http_parts();
let r = from_request::<Option<Form<Info>>>(&req, &mut pl)
.await
.unwrap();
assert_eq!(
r,
Some(Form(Info {
hello: "world".into()
}))
);
let (req, mut pl) = TestRequest::with_header(
header::CONTENT_TYPE,
"application/x-www-form-urlencoded",
)
.header(header::CONTENT_LENGTH, "9")
.set_payload(Bytes::from_static(b"bye=world"))
.to_http_parts();
let r = from_request::<Option<Form<Info>>>(&req, &mut pl)
.await
.unwrap();
assert_eq!(r, None);
}
#[crate::rt_test]
async fn test_result() {
let (req, mut pl) = TestRequest::with_header(
header::CONTENT_TYPE,
"application/x-www-form-urlencoded",
)
.header(header::CONTENT_LENGTH, "11")
.set_payload(Bytes::from_static(b"hello=world"))
.to_http_parts();
let r = from_request::<Result<Form<Info>, UrlencodedError>>(&req, &mut pl)
.await
.unwrap();
assert_eq!(
r.unwrap(),
Form(Info {
hello: "world".into()
})
);
let (req, mut pl) = TestRequest::with_header(
header::CONTENT_TYPE,
"application/x-www-form-urlencoded",
)
.header(header::CONTENT_LENGTH, "9")
.set_payload(Bytes::from_static(b"bye=world"))
.to_http_parts();
let r = from_request::<Result<Form<Info>, UrlencodedError>>(&req, &mut pl)
.await
.unwrap();
assert!(r.is_err());
}
}