1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-01-31 19:10:07 +01:00
actix-web/src/handler.rs

215 lines
6.2 KiB
Rust
Raw Normal View History

2019-11-20 23:33:22 +06:00
use std::future::Future;
2018-04-13 16:02:01 -07:00
use std::marker::PhantomData;
2019-11-20 23:33:22 +06:00
use std::pin::Pin;
use std::task::{Context, Poll};
2017-10-06 21:48:14 -07:00
2019-05-22 11:49:27 -07:00
use actix_http::{Error, Response};
2019-11-20 23:33:22 +06:00
use actix_service::{Service, ServiceFactory};
2020-12-23 23:47:07 +08:00
use futures_util::future::{ready, Ready};
2020-05-18 11:47:20 +09:00
use futures_util::ready;
2019-11-20 23:33:22 +06:00
use pin_project::pin_project;
2018-05-01 17:30:06 -07:00
use crate::extract::FromRequest;
2019-03-01 22:51:32 -08:00
use crate::request::HttpRequest;
use crate::responder::Responder;
2019-04-07 14:43:07 -07:00
use crate::service::{ServiceRequest, ServiceResponse};
2017-11-03 13:35:34 -07:00
2020-12-26 16:46:19 -05:00
/// A request handler is an async function that accepts zero or more parameters that can be
/// extracted from a request (ie, [`impl FromRequest`](crate::FromRequest)) and returns a type that can be converted into
/// an [`HttpResponse`](crate::HttpResponse) (ie, [`impl Responder`](crate::Responder)).
///
/// If you got the error `the trait Handler<_, _, _> is not implemented`, then your function is not
/// a valid handler. See [Request Handlers](https://actix.rs/docs/handlers/) for more information.
pub trait Handler<T, R>: Clone + 'static
2019-03-01 22:51:32 -08:00
where
2020-12-26 16:46:19 -05:00
R: Future,
R::Output: Responder,
2019-03-01 22:51:32 -08:00
{
fn call(&self, param: T) -> R;
}
2018-05-01 17:19:15 -07:00
2020-12-26 16:46:19 -05:00
impl<F, R> Handler<(), R> for F
2019-03-01 22:51:32 -08:00
where
F: Fn() -> R + Clone + 'static,
2020-12-26 16:46:19 -05:00
R: Future,
R::Output: Responder,
2019-03-01 22:51:32 -08:00
{
fn call(&self, _: ()) -> R {
(self)()
2017-11-28 19:49:17 -08:00
}
2019-03-01 22:51:32 -08:00
}
2017-12-09 13:25:06 -08:00
2019-03-01 22:51:32 -08:00
#[doc(hidden)]
2020-12-23 23:47:07 +08:00
/// Extract arguments from request, run factory function and make response.
2020-12-26 16:46:19 -05:00
pub struct HandlerService<F, T, R>
2019-03-01 22:51:32 -08:00
where
2020-12-26 16:46:19 -05:00
F: Handler<T, R>,
2020-12-23 23:47:07 +08:00
T: FromRequest,
2020-12-26 16:46:19 -05:00
R: Future,
R::Output: Responder,
2019-03-01 22:51:32 -08:00
{
hnd: F,
2020-12-26 16:46:19 -05:00
_t: PhantomData<(T, R)>,
2019-03-01 22:51:32 -08:00
}
2018-05-01 17:19:15 -07:00
2020-12-26 16:46:19 -05:00
impl<F, T, R> HandlerService<F, T, R>
2019-03-01 22:51:32 -08:00
where
2020-12-26 16:46:19 -05:00
F: Handler<T, R>,
2020-12-23 23:47:07 +08:00
T: FromRequest,
2020-12-26 16:46:19 -05:00
R: Future,
R::Output: Responder,
2019-03-01 22:51:32 -08:00
{
pub fn new(hnd: F) -> Self {
2020-12-26 16:46:19 -05:00
Self {
2019-03-01 22:51:32 -08:00
hnd,
_t: PhantomData,
2017-12-09 13:25:06 -08:00
}
}
2017-11-28 19:49:17 -08:00
}
2020-12-26 16:46:19 -05:00
impl<F, T, R> Clone for HandlerService<F, T, R>
2019-03-01 22:51:32 -08:00
where
2020-12-26 16:46:19 -05:00
F: Handler<T, R>,
2020-12-23 23:47:07 +08:00
T: FromRequest,
2020-12-26 16:46:19 -05:00
R: Future,
R::Output: Responder,
2019-03-01 22:51:32 -08:00
{
fn clone(&self) -> Self {
2020-12-26 16:46:19 -05:00
Self {
2019-03-01 22:51:32 -08:00
hnd: self.hnd.clone(),
_t: PhantomData,
}
}
}
2020-12-26 16:46:19 -05:00
impl<F, T, R> ServiceFactory for HandlerService<F, T, R>
2019-03-01 22:51:32 -08:00
where
2020-12-26 16:46:19 -05:00
F: Handler<T, R>,
2020-12-23 23:47:07 +08:00
T: FromRequest,
2020-12-26 16:46:19 -05:00
R: Future,
R::Output: Responder,
{
type Request = ServiceRequest;
type Response = ServiceResponse;
type Error = Error;
2020-12-20 00:33:34 +08:00
type Config = ();
2020-12-23 23:47:07 +08:00
type Service = Self;
2020-12-20 00:33:34 +08:00
type InitError = ();
2019-11-20 23:33:22 +06:00
type Future = Ready<Result<Self::Service, ()>>;
2018-07-15 15:12:21 +06:00
2019-12-02 21:37:13 +06:00
fn new_service(&self, _: ()) -> Self::Future {
2020-12-23 23:47:07 +08:00
ready(Ok(self.clone()))
2019-03-01 22:51:32 -08:00
}
2017-11-29 13:26:55 -08:00
}
2020-12-26 16:46:19 -05:00
// Handler is both it's ServiceHandler and Service Type.
impl<F, T, R> Service for HandlerService<F, T, R>
where
2020-12-26 16:46:19 -05:00
F: Handler<T, R>,
2020-12-23 23:47:07 +08:00
T: FromRequest,
2020-12-26 16:46:19 -05:00
R: Future,
R::Output: Responder,
{
type Request = ServiceRequest;
type Response = ServiceResponse;
type Error = Error;
2020-12-26 16:46:19 -05:00
type Future = HandlerServiceFuture<F, T, R>;
2019-03-01 22:51:32 -08:00
2019-12-08 00:46:51 +06:00
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
2019-11-20 23:33:22 +06:00
Poll::Ready(Ok(()))
2017-11-29 13:26:55 -08:00
}
2020-12-23 23:47:07 +08:00
fn call(&mut self, req: Self::Request) -> Self::Future {
let (req, mut payload) = req.into_parts();
2019-11-20 23:33:22 +06:00
let fut = T::from_request(&req, &mut payload);
2020-12-23 23:47:07 +08:00
HandlerServiceFuture::Extract(fut, Some(req), self.hnd.clone())
2017-11-29 13:26:55 -08:00
}
}
2020-12-23 23:47:07 +08:00
#[doc(hidden)]
#[pin_project(project = HandlerProj)]
2020-12-26 16:46:19 -05:00
pub enum HandlerServiceFuture<F, T, R>
2020-12-23 23:47:07 +08:00
where
2020-12-26 16:46:19 -05:00
F: Handler<T, R>,
2020-12-23 23:47:07 +08:00
T: FromRequest,
2020-12-26 16:46:19 -05:00
R: Future,
R::Output: Responder,
2020-12-23 23:47:07 +08:00
{
Extract(#[pin] T::Future, Option<HttpRequest>, F),
Handle(#[pin] R, Option<HttpRequest>),
2020-12-26 16:46:19 -05:00
Respond(#[pin] <R::Output as Responder>::Future, Option<HttpRequest>),
2017-11-29 13:26:55 -08:00
}
2020-12-26 16:46:19 -05:00
impl<F, T, R> Future for HandlerServiceFuture<F, T, R>
where
2020-12-26 16:46:19 -05:00
F: Handler<T, R>,
2020-12-23 23:47:07 +08:00
T: FromRequest,
2020-12-26 16:46:19 -05:00
R: Future,
R::Output: Responder,
{
2020-12-23 23:47:07 +08:00
// Error type in this future is a placeholder type.
// all instances of error must be converted to ServiceResponse and return in Ok.
type Output = Result<ServiceResponse, Error>;
2017-11-29 13:26:55 -08:00
2019-12-08 00:46:51 +06:00
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
2020-12-20 00:33:34 +08:00
loop {
match self.as_mut().project() {
2020-12-23 23:47:07 +08:00
HandlerProj::Extract(fut, req, handle) => {
match ready!(fut.poll(cx)) {
2020-12-20 00:33:34 +08:00
Ok(item) => {
2020-12-23 23:47:07 +08:00
let fut = handle.call(item);
let state = HandlerServiceFuture::Handle(fut, req.take());
self.as_mut().set(state);
2020-12-20 00:33:34 +08:00
}
2020-12-23 23:47:07 +08:00
Err(e) => {
let res: Response = e.into().into();
let req = req.take().unwrap();
return Poll::Ready(Ok(ServiceResponse::new(req, res)));
}
};
}
HandlerProj::Handle(fut, req) => {
let res = ready!(fut.poll(cx));
let fut = res.respond_to(req.as_ref().unwrap());
let state = HandlerServiceFuture::Respond(fut, req.take());
self.as_mut().set(state);
}
HandlerProj::Respond(fut, req) => {
let res = ready!(fut.poll(cx)).unwrap_or_else(|e| e.into().into());
let req = req.take().unwrap();
return Poll::Ready(Ok(ServiceResponse::new(req, res)));
2020-12-20 00:33:34 +08:00
}
2019-11-20 23:33:22 +06:00
}
}
2018-03-29 15:41:13 -07:00
}
}
2019-03-01 22:51:32 -08:00
/// FromRequest trait impl for tuples
macro_rules! factory_tuple ({ $(($n:tt, $T:ident)),+} => {
2020-12-26 16:46:19 -05:00
impl<Func, $($T,)+ Res> Handler<($($T,)+), Res> for Func
2019-03-01 22:51:32 -08:00
where Func: Fn($($T,)+) -> Res + Clone + 'static,
2020-12-26 16:46:19 -05:00
Res: Future,
Res::Output: Responder,
2019-03-01 22:51:32 -08:00
{
fn call(&self, param: ($($T,)+)) -> Res {
(self)($(param.$n,)+)
}
2018-03-29 15:41:13 -07:00
}
2019-03-01 22:51:32 -08:00
});
#[rustfmt::skip]
mod m {
use super::*;
factory_tuple!((0, A));
factory_tuple!((0, A), (1, B));
factory_tuple!((0, A), (1, B), (2, C));
factory_tuple!((0, A), (1, B), (2, C), (3, D));
factory_tuple!((0, A), (1, B), (2, C), (3, D), (4, E));
factory_tuple!((0, A), (1, B), (2, C), (3, D), (4, E), (5, F));
factory_tuple!((0, A), (1, B), (2, C), (3, D), (4, E), (5, F), (6, G));
factory_tuple!((0, A), (1, B), (2, C), (3, D), (4, E), (5, F), (6, G), (7, H));
factory_tuple!((0, A), (1, B), (2, C), (3, D), (4, E), (5, F), (6, G), (7, H), (8, I));
factory_tuple!((0, A), (1, B), (2, C), (3, D), (4, E), (5, F), (6, G), (7, H), (8, I), (9, J));
2018-03-29 15:41:13 -07:00
}