1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-06-02 21:39:26 +00:00
actix-web/src/handler.rs

192 lines
5.3 KiB
Rust
Raw Normal View History

2019-11-20 17:33:22 +00:00
use std::future::Future;
2018-04-13 23:02:01 +00:00
use std::marker::PhantomData;
2019-11-20 17:33:22 +00:00
use std::pin::Pin;
use std::task::{Context, Poll};
2017-10-07 04:48:14 +00:00
2019-11-20 17:33:22 +00:00
use actix_service::{Service, ServiceFactory};
2021-04-01 14:26:13 +00:00
use actix_utils::future::{ready, Ready};
use futures_core::ready;
2019-11-20 17:33:22 +00:00
use pin_project::pin_project;
2018-05-02 00:30:06 +00:00
use crate::{
service::{ServiceRequest, ServiceResponse},
2021-06-17 16:57:58 +00:00
Error, FromRequest, HttpRequest, HttpResponse, Responder,
};
2017-11-03 20:35:34 +00:00
/// A request handler is an async function that accepts zero or more parameters that can be
/// extracted from a request (i.e., [`impl FromRequest`](crate::FromRequest)) and returns a type
/// that can be converted into an [`HttpResponse`] (that is, it impls the [`Responder`] trait).
2020-12-26 21:46:19 +00:00
///
/// 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-02 06:51:32 +00:00
where
2020-12-26 21:46:19 +00:00
R: Future,
R::Output: Responder,
2019-03-02 06:51:32 +00:00
{
fn call(&self, param: T) -> R;
}
2018-05-02 00:19:15 +00:00
2019-03-02 06:51:32 +00:00
#[doc(hidden)]
2020-12-23 15:47:07 +00:00
/// Extract arguments from request, run factory function and make response.
2020-12-26 21:46:19 +00:00
pub struct HandlerService<F, T, R>
2019-03-02 06:51:32 +00:00
where
2020-12-26 21:46:19 +00:00
F: Handler<T, R>,
2020-12-23 15:47:07 +00:00
T: FromRequest,
2020-12-26 21:46:19 +00:00
R: Future,
R::Output: Responder,
2019-03-02 06:51:32 +00:00
{
hnd: F,
2021-01-04 00:49:02 +00:00
_phantom: PhantomData<(T, R)>,
2019-03-02 06:51:32 +00:00
}
2018-05-02 00:19:15 +00:00
2020-12-26 21:46:19 +00:00
impl<F, T, R> HandlerService<F, T, R>
2019-03-02 06:51:32 +00:00
where
2020-12-26 21:46:19 +00:00
F: Handler<T, R>,
2020-12-23 15:47:07 +00:00
T: FromRequest,
2020-12-26 21:46:19 +00:00
R: Future,
R::Output: Responder,
2019-03-02 06:51:32 +00:00
{
pub fn new(hnd: F) -> Self {
2020-12-26 21:46:19 +00:00
Self {
2019-03-02 06:51:32 +00:00
hnd,
2021-01-04 00:49:02 +00:00
_phantom: PhantomData,
2017-12-09 21:25:06 +00:00
}
}
2017-11-29 03:49:17 +00:00
}
2020-12-26 21:46:19 +00:00
impl<F, T, R> Clone for HandlerService<F, T, R>
2019-03-02 06:51:32 +00:00
where
2020-12-26 21:46:19 +00:00
F: Handler<T, R>,
2020-12-23 15:47:07 +00:00
T: FromRequest,
2020-12-26 21:46:19 +00:00
R: Future,
R::Output: Responder,
2019-03-02 06:51:32 +00:00
{
fn clone(&self) -> Self {
2020-12-26 21:46:19 +00:00
Self {
2019-03-02 06:51:32 +00:00
hnd: self.hnd.clone(),
2021-01-04 00:49:02 +00:00
_phantom: PhantomData,
}
}
}
impl<F, T, R> ServiceFactory<ServiceRequest> for HandlerService<F, T, R>
2019-03-02 06:51:32 +00:00
where
2020-12-26 21:46:19 +00:00
F: Handler<T, R>,
2020-12-23 15:47:07 +00:00
T: FromRequest,
2020-12-26 21:46:19 +00:00
R: Future,
R::Output: Responder,
{
type Response = ServiceResponse;
type Error = Error;
2020-12-19 16:33:34 +00:00
type Config = ();
2020-12-23 15:47:07 +00:00
type Service = Self;
2020-12-19 16:33:34 +00:00
type InitError = ();
2019-11-20 17:33:22 +00:00
type Future = Ready<Result<Self::Service, ()>>;
2018-07-15 09:12:21 +00:00
2019-12-02 15:37:13 +00:00
fn new_service(&self, _: ()) -> Self::Future {
2020-12-23 15:47:07 +00:00
ready(Ok(self.clone()))
2019-03-02 06:51:32 +00:00
}
2017-11-29 21:26:55 +00:00
}
2021-01-15 05:38:50 +00:00
/// HandlerService is both it's ServiceFactory and Service Type.
impl<F, T, R> Service<ServiceRequest> for HandlerService<F, T, R>
where
2020-12-26 21:46:19 +00:00
F: Handler<T, R>,
2020-12-23 15:47:07 +00:00
T: FromRequest,
2020-12-26 21:46:19 +00:00
R: Future,
R::Output: Responder,
{
type Response = ServiceResponse;
type Error = Error;
2020-12-26 21:46:19 +00:00
type Future = HandlerServiceFuture<F, T, R>;
2019-03-02 06:51:32 +00:00
actix_service::always_ready!();
2017-11-29 21:26:55 +00:00
fn call(&self, req: ServiceRequest) -> Self::Future {
let (req, mut payload) = req.into_parts();
2019-11-20 17:33:22 +00:00
let fut = T::from_request(&req, &mut payload);
2020-12-23 15:47:07 +00:00
HandlerServiceFuture::Extract(fut, Some(req), self.hnd.clone())
2017-11-29 21:26:55 +00:00
}
}
2020-12-23 15:47:07 +00:00
#[doc(hidden)]
#[pin_project(project = HandlerProj)]
2020-12-26 21:46:19 +00:00
pub enum HandlerServiceFuture<F, T, R>
2020-12-23 15:47:07 +00:00
where
2020-12-26 21:46:19 +00:00
F: Handler<T, R>,
2020-12-23 15:47:07 +00:00
T: FromRequest,
2020-12-26 21:46:19 +00:00
R: Future,
R::Output: Responder,
2020-12-23 15:47:07 +00:00
{
Extract(#[pin] T::Future, Option<HttpRequest>, F),
Handle(#[pin] R, Option<HttpRequest>),
2017-11-29 21:26:55 +00:00
}
2020-12-26 21:46:19 +00:00
impl<F, T, R> Future for HandlerServiceFuture<F, T, R>
where
2020-12-26 21:46:19 +00:00
F: Handler<T, R>,
2020-12-23 15:47:07 +00:00
T: FromRequest,
2020-12-26 21:46:19 +00:00
R: Future,
R::Output: Responder,
{
2020-12-23 15:47:07 +00: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 21:26:55 +00:00
2019-12-07 18:46:51 +00:00
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
2020-12-19 16:33:34 +00:00
loop {
match self.as_mut().project() {
2020-12-23 15:47:07 +00:00
HandlerProj::Extract(fut, req, handle) => {
match ready!(fut.poll(cx)) {
2020-12-19 16:33:34 +00:00
Ok(item) => {
2020-12-23 15:47:07 +00:00
let fut = handle.call(item);
let state = HandlerServiceFuture::Handle(fut, req.take());
self.as_mut().set(state);
2020-12-19 16:33:34 +00:00
}
Err(err) => {
2020-12-23 15:47:07 +00:00
let req = req.take().unwrap();
let res = HttpResponse::from_error(err.into());
2020-12-23 15:47:07 +00:00
return Poll::Ready(Ok(ServiceResponse::new(req, res)));
}
};
}
HandlerProj::Handle(fut, req) => {
let res = ready!(fut.poll(cx));
let req = req.take().unwrap();
let res = res.respond_to(&req);
2020-12-23 15:47:07 +00:00
return Poll::Ready(Ok(ServiceResponse::new(req, res)));
2020-12-19 16:33:34 +00:00
}
2019-11-20 17:33:22 +00:00
}
}
2018-03-29 22:41:13 +00:00
}
}
2019-03-02 06:51:32 +00:00
/// FromRequest trait impl for tuples
2021-03-19 16:30:53 +00:00
macro_rules! factory_tuple ({ $($param:ident)* } => {
impl<Func, $($param,)* Res> Handler<($($param,)*), Res> for Func
where Func: Fn($($param),*) -> Res + Clone + 'static,
2020-12-26 21:46:19 +00:00
Res: Future,
Res::Output: Responder,
2019-03-02 06:51:32 +00:00
{
2021-03-19 16:30:53 +00:00
#[allow(non_snake_case)]
fn call(&self, ($($param,)*): ($($param,)*)) -> Res {
(self)($($param,)*)
2019-03-02 06:51:32 +00:00
}
2018-03-29 22:41:13 +00:00
}
2019-03-02 06:51:32 +00:00
});
2021-03-19 16:30:53 +00:00
factory_tuple! {}
factory_tuple! { A }
factory_tuple! { A B }
factory_tuple! { A B C }
factory_tuple! { A B C D }
factory_tuple! { A B C D E }
factory_tuple! { A B C D E F }
factory_tuple! { A B C D E F G }
factory_tuple! { A B C D E F G H }
factory_tuple! { A B C D E F G H I }
factory_tuple! { A B C D E F G H I J }
factory_tuple! { A B C D E F G H I J K }
factory_tuple! { A B C D E F G H I J K L }