2018-04-13 23:02:01 +00:00
|
|
|
use std::marker::PhantomData;
|
|
|
|
use std::ops::Deref;
|
2018-06-21 08:17:27 +00:00
|
|
|
use std::rc::Rc;
|
2017-10-07 04:48:14 +00:00
|
|
|
|
2018-05-02 00:30:06 +00:00
|
|
|
use futures::future::{err, ok, Future};
|
|
|
|
use futures::{Async, Poll};
|
|
|
|
|
2017-11-29 21:26:55 +00:00
|
|
|
use error::Error;
|
2018-06-11 11:05:41 +00:00
|
|
|
use http::StatusCode;
|
2017-10-15 05:52:38 +00:00
|
|
|
use httprequest::HttpRequest;
|
2017-10-24 06:25:32 +00:00
|
|
|
use httpresponse::HttpResponse;
|
2018-07-12 09:30:01 +00:00
|
|
|
use resource::Resource;
|
2017-11-03 20:35:34 +00:00
|
|
|
|
2018-01-15 21:47:25 +00:00
|
|
|
/// Trait defines object that could be registered as route handler
|
2017-10-16 08:19:23 +00:00
|
|
|
#[allow(unused_variables)]
|
2017-11-29 21:26:55 +00:00
|
|
|
pub trait Handler<S>: 'static {
|
2017-11-29 23:07:49 +00:00
|
|
|
/// The type of value that handler will return.
|
2017-12-14 17:43:42 +00:00
|
|
|
type Result: Responder;
|
2017-11-29 21:26:55 +00:00
|
|
|
|
2017-10-10 06:07:32 +00:00
|
|
|
/// Handle request
|
2018-06-25 04:58:04 +00:00
|
|
|
fn handle(&self, req: &HttpRequest<S>) -> Self::Result;
|
2017-10-07 04:48:14 +00:00
|
|
|
}
|
|
|
|
|
2017-12-14 17:43:42 +00:00
|
|
|
/// Trait implemented by types that generate responses for clients.
|
|
|
|
///
|
|
|
|
/// Types that implement this trait can be used as the return type of a handler.
|
|
|
|
pub trait Responder {
|
2017-12-03 22:22:04 +00:00
|
|
|
/// The associated item which can be returned.
|
2018-05-03 23:22:08 +00:00
|
|
|
type Item: Into<AsyncResult<HttpResponse>>;
|
2017-12-03 22:22:04 +00:00
|
|
|
|
|
|
|
/// The associated error which can be returned.
|
|
|
|
type Error: Into<Error>;
|
|
|
|
|
2018-05-03 23:22:08 +00:00
|
|
|
/// Convert itself to `AsyncResult` or `Error`.
|
2018-05-04 18:44:22 +00:00
|
|
|
fn respond_to<S: 'static>(
|
|
|
|
self, req: &HttpRequest<S>,
|
|
|
|
) -> Result<Self::Item, Self::Error>;
|
2017-12-03 00:37:21 +00:00
|
|
|
}
|
|
|
|
|
2018-03-29 20:12:28 +00:00
|
|
|
/// Trait implemented by types that can be extracted from request.
|
|
|
|
///
|
|
|
|
/// Types that implement this trait can be used with `Route::with()` method.
|
2018-05-02 13:07:30 +00:00
|
|
|
pub trait FromRequest<S>: Sized {
|
2018-04-04 05:06:18 +00:00
|
|
|
/// Configuration for conversion process
|
|
|
|
type Config: Default;
|
|
|
|
|
|
|
|
/// Future that resolves to a Self
|
2018-05-03 23:22:08 +00:00
|
|
|
type Result: Into<AsyncResult<Self>>;
|
2018-03-29 20:12:28 +00:00
|
|
|
|
2018-04-04 05:06:18 +00:00
|
|
|
/// Convert request to a Self
|
2018-05-02 13:07:30 +00:00
|
|
|
fn from_request(req: &HttpRequest<S>, cfg: &Self::Config) -> Self::Result;
|
|
|
|
|
|
|
|
/// Convert request to a Self
|
|
|
|
///
|
|
|
|
/// This method uses default extractor configuration
|
2018-05-02 13:09:50 +00:00
|
|
|
fn extract(req: &HttpRequest<S>) -> Self::Result {
|
2018-05-02 13:07:30 +00:00
|
|
|
Self::from_request(req, &Self::Config::default())
|
|
|
|
}
|
2018-03-29 20:12:28 +00:00
|
|
|
}
|
|
|
|
|
2018-03-11 16:36:54 +00:00
|
|
|
/// Combines two different responder types into a single type
|
2018-03-10 18:12:44 +00:00
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # extern crate actix_web;
|
|
|
|
/// # extern crate futures;
|
|
|
|
/// # use futures::future::Future;
|
2018-06-01 16:37:14 +00:00
|
|
|
/// use actix_web::{AsyncResponder, Either, Error, HttpRequest, HttpResponse};
|
2018-03-10 18:12:44 +00:00
|
|
|
/// use futures::future::result;
|
|
|
|
///
|
2018-06-01 16:37:14 +00:00
|
|
|
/// type RegisterResult =
|
|
|
|
/// Either<HttpResponse, Box<Future<Item = HttpResponse, Error = Error>>>;
|
2018-03-11 16:28:22 +00:00
|
|
|
///
|
2018-03-10 18:12:44 +00:00
|
|
|
/// fn index(req: HttpRequest) -> RegisterResult {
|
2018-06-01 16:37:14 +00:00
|
|
|
/// if is_a_variant() {
|
|
|
|
/// // <- choose variant A
|
|
|
|
/// Either::A(HttpResponse::BadRequest().body("Bad data"))
|
2018-03-10 18:12:44 +00:00
|
|
|
/// } else {
|
2018-06-01 16:37:14 +00:00
|
|
|
/// Either::B(
|
|
|
|
/// // <- variant B
|
2018-03-31 06:07:33 +00:00
|
|
|
/// result(Ok(HttpResponse::Ok()
|
2018-06-01 16:37:14 +00:00
|
|
|
/// .content_type("text/html")
|
|
|
|
/// .body("Hello!")))
|
|
|
|
/// .responder(),
|
|
|
|
/// )
|
2018-03-10 18:12:44 +00:00
|
|
|
/// }
|
|
|
|
/// }
|
2018-03-11 16:28:22 +00:00
|
|
|
/// # fn is_a_variant() -> bool { true }
|
2018-03-10 18:12:44 +00:00
|
|
|
/// # fn main() {}
|
|
|
|
/// ```
|
2018-03-10 17:39:43 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Either<A, B> {
|
|
|
|
/// First branch of the type
|
|
|
|
A(A),
|
|
|
|
/// Second branch of the type
|
|
|
|
B(B),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<A, B> Responder for Either<A, B>
|
2018-04-13 23:02:01 +00:00
|
|
|
where
|
|
|
|
A: Responder,
|
|
|
|
B: Responder,
|
2018-03-10 17:39:43 +00:00
|
|
|
{
|
2018-05-03 23:22:08 +00:00
|
|
|
type Item = AsyncResult<HttpResponse>;
|
2018-03-10 17:39:43 +00:00
|
|
|
type Error = Error;
|
|
|
|
|
2018-05-04 18:44:22 +00:00
|
|
|
fn respond_to<S: 'static>(
|
|
|
|
self, req: &HttpRequest<S>,
|
|
|
|
) -> Result<AsyncResult<HttpResponse>, Error> {
|
2018-03-10 17:39:43 +00:00
|
|
|
match self {
|
|
|
|
Either::A(a) => match a.respond_to(req) {
|
|
|
|
Ok(val) => Ok(val.into()),
|
|
|
|
Err(err) => Err(err.into()),
|
|
|
|
},
|
|
|
|
Either::B(b) => match b.respond_to(req) {
|
|
|
|
Ok(val) => Ok(val.into()),
|
|
|
|
Err(err) => Err(err.into()),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-02 23:19:18 +00:00
|
|
|
impl<A, B, I, E> Future for Either<A, B>
|
2018-04-13 23:02:01 +00:00
|
|
|
where
|
|
|
|
A: Future<Item = I, Error = E>,
|
|
|
|
B: Future<Item = I, Error = E>,
|
2018-04-02 23:19:18 +00:00
|
|
|
{
|
|
|
|
type Item = I;
|
|
|
|
type Error = E;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<I, E> {
|
|
|
|
match *self {
|
|
|
|
Either::A(ref mut fut) => fut.poll(),
|
|
|
|
Either::B(ref mut fut) => fut.poll(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-11 11:05:41 +00:00
|
|
|
impl<T> Responder for Option<T>
|
|
|
|
where
|
|
|
|
T: Responder,
|
|
|
|
{
|
|
|
|
type Item = AsyncResult<HttpResponse>;
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn respond_to<S: 'static>(
|
|
|
|
self, req: &HttpRequest<S>,
|
|
|
|
) -> Result<AsyncResult<HttpResponse>, Error> {
|
|
|
|
match self {
|
|
|
|
Some(t) => match t.respond_to(req) {
|
|
|
|
Ok(val) => Ok(val.into()),
|
|
|
|
Err(err) => Err(err.into()),
|
|
|
|
},
|
|
|
|
None => Ok(req.build_response(StatusCode::NOT_FOUND).finish().into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-31 07:16:55 +00:00
|
|
|
/// Convenience trait that converts `Future` object to a `Boxed` future
|
2018-03-30 16:34:03 +00:00
|
|
|
///
|
|
|
|
/// For example loading json from request's body is async operation.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # extern crate actix_web;
|
|
|
|
/// # extern crate futures;
|
|
|
|
/// # #[macro_use] extern crate serde_derive;
|
2018-03-31 06:07:33 +00:00
|
|
|
/// use actix_web::{
|
2018-06-01 16:37:14 +00:00
|
|
|
/// App, AsyncResponder, Error, HttpMessage, HttpRequest, HttpResponse,
|
|
|
|
/// };
|
|
|
|
/// use futures::future::Future;
|
2018-03-30 16:34:03 +00:00
|
|
|
///
|
|
|
|
/// #[derive(Deserialize, Debug)]
|
|
|
|
/// struct MyObj {
|
|
|
|
/// name: String,
|
|
|
|
/// }
|
|
|
|
///
|
2018-06-01 16:37:14 +00:00
|
|
|
/// fn index(mut req: HttpRequest) -> Box<Future<Item = HttpResponse, Error = Error>> {
|
2018-03-30 16:34:03 +00:00
|
|
|
/// req.json() // <- get JsonBody future
|
|
|
|
/// .from_err()
|
|
|
|
/// .and_then(|val: MyObj| { // <- deserialized value
|
2018-03-31 06:07:33 +00:00
|
|
|
/// Ok(HttpResponse::Ok().into())
|
2018-03-30 16:34:03 +00:00
|
|
|
/// })
|
|
|
|
/// // Construct boxed future by using `AsyncResponder::responder()` method
|
|
|
|
/// .responder()
|
|
|
|
/// }
|
|
|
|
/// # fn main() {}
|
|
|
|
/// ```
|
2017-12-21 04:30:54 +00:00
|
|
|
pub trait AsyncResponder<I, E>: Sized {
|
2018-06-02 13:51:58 +00:00
|
|
|
/// Convert to a boxed future
|
2018-04-13 23:02:01 +00:00
|
|
|
fn responder(self) -> Box<Future<Item = I, Error = E>>;
|
2017-12-21 04:30:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<F, I, E> AsyncResponder<I, E> for F
|
2018-04-13 23:02:01 +00:00
|
|
|
where
|
|
|
|
F: Future<Item = I, Error = E> + 'static,
|
|
|
|
I: Responder + 'static,
|
|
|
|
E: Into<Error> + 'static,
|
2017-12-21 04:30:54 +00:00
|
|
|
{
|
2018-04-13 23:02:01 +00:00
|
|
|
fn responder(self) -> Box<Future<Item = I, Error = E>> {
|
2017-12-21 04:30:54 +00:00
|
|
|
Box::new(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-29 21:26:55 +00:00
|
|
|
/// Handler<S> for Fn()
|
|
|
|
impl<F, R, S> Handler<S> for F
|
2018-04-13 23:02:01 +00:00
|
|
|
where
|
2018-06-25 04:58:04 +00:00
|
|
|
F: Fn(&HttpRequest<S>) -> R + 'static,
|
2018-04-13 23:02:01 +00:00
|
|
|
R: Responder + 'static,
|
2017-10-15 21:17:41 +00:00
|
|
|
{
|
2017-11-29 21:26:55 +00:00
|
|
|
type Result = R;
|
2017-10-15 21:17:41 +00:00
|
|
|
|
2018-06-25 04:58:04 +00:00
|
|
|
fn handle(&self, req: &HttpRequest<S>) -> R {
|
2017-11-29 21:26:55 +00:00
|
|
|
(self)(req)
|
2017-10-15 21:17:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-03 23:22:08 +00:00
|
|
|
/// Represents async result
|
2018-05-02 00:19:15 +00:00
|
|
|
///
|
2018-05-03 23:22:08 +00:00
|
|
|
/// Result could be in tree different forms.
|
|
|
|
/// * Ok(T) - ready item
|
|
|
|
/// * Err(E) - error happen during reply process
|
|
|
|
/// * Future<T, E> - reply process completes in the future
|
|
|
|
pub struct AsyncResult<I, E = Error>(Option<AsyncResultItem<I, E>>);
|
2018-05-02 00:19:15 +00:00
|
|
|
|
2018-05-03 23:22:08 +00:00
|
|
|
impl<I, E> Future for AsyncResult<I, E> {
|
2018-05-02 23:33:29 +00:00
|
|
|
type Item = I;
|
|
|
|
type Error = E;
|
2018-05-02 00:30:06 +00:00
|
|
|
|
2018-05-02 23:33:29 +00:00
|
|
|
fn poll(&mut self) -> Poll<I, E> {
|
|
|
|
let res = self.0.take().expect("use after resolve");
|
|
|
|
match res {
|
2018-05-03 23:22:08 +00:00
|
|
|
AsyncResultItem::Ok(msg) => Ok(Async::Ready(msg)),
|
|
|
|
AsyncResultItem::Err(err) => Err(err),
|
|
|
|
AsyncResultItem::Future(mut fut) => match fut.poll() {
|
2018-05-02 00:30:06 +00:00
|
|
|
Ok(Async::NotReady) => {
|
2018-05-03 23:22:08 +00:00
|
|
|
self.0 = Some(AsyncResultItem::Future(fut));
|
2018-05-02 00:30:06 +00:00
|
|
|
Ok(Async::NotReady)
|
|
|
|
}
|
|
|
|
Ok(Async::Ready(msg)) => Ok(Async::Ready(msg)),
|
|
|
|
Err(err) => Err(err),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-03 23:22:08 +00:00
|
|
|
pub(crate) enum AsyncResultItem<I, E> {
|
2018-05-02 23:33:29 +00:00
|
|
|
Ok(I),
|
|
|
|
Err(E),
|
|
|
|
Future(Box<Future<Item = I, Error = E>>),
|
2017-11-29 03:49:17 +00:00
|
|
|
}
|
|
|
|
|
2018-05-03 23:22:08 +00:00
|
|
|
impl<I, E> AsyncResult<I, E> {
|
2017-11-29 03:49:17 +00:00
|
|
|
/// Create async response
|
2017-12-13 05:32:58 +00:00
|
|
|
#[inline]
|
2018-05-03 23:22:08 +00:00
|
|
|
pub fn async(fut: Box<Future<Item = I, Error = E>>) -> AsyncResult<I, E> {
|
|
|
|
AsyncResult(Some(AsyncResultItem::Future(fut)))
|
2017-11-29 03:49:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Send response
|
2017-12-13 05:32:58 +00:00
|
|
|
#[inline]
|
2018-05-03 23:22:08 +00:00
|
|
|
pub fn ok<R: Into<I>>(ok: R) -> AsyncResult<I, E> {
|
|
|
|
AsyncResult(Some(AsyncResultItem::Ok(ok.into())))
|
2017-11-29 03:49:17 +00:00
|
|
|
}
|
|
|
|
|
2018-05-02 00:19:15 +00:00
|
|
|
/// Send error
|
|
|
|
#[inline]
|
2018-05-04 18:44:22 +00:00
|
|
|
pub fn err<R: Into<E>>(err: R) -> AsyncResult<I, E> {
|
2018-05-03 23:22:08 +00:00
|
|
|
AsyncResult(Some(AsyncResultItem::Err(err.into())))
|
2018-05-02 00:19:15 +00:00
|
|
|
}
|
|
|
|
|
2017-12-13 05:32:58 +00:00
|
|
|
#[inline]
|
2018-05-03 23:22:08 +00:00
|
|
|
pub(crate) fn into(self) -> AsyncResultItem<I, E> {
|
2018-05-02 23:33:29 +00:00
|
|
|
self.0.expect("use after resolve")
|
2017-11-29 03:49:17 +00:00
|
|
|
}
|
2017-12-09 21:25:06 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
2018-05-02 23:48:42 +00:00
|
|
|
pub(crate) fn as_msg(&self) -> &I {
|
|
|
|
match self.0.as_ref().unwrap() {
|
2018-05-03 23:22:08 +00:00
|
|
|
&AsyncResultItem::Ok(ref resp) => resp,
|
2018-05-02 00:19:15 +00:00
|
|
|
_ => panic!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2018-05-02 23:33:29 +00:00
|
|
|
pub(crate) fn as_err(&self) -> Option<&E> {
|
2018-05-02 23:48:42 +00:00
|
|
|
match self.0.as_ref().unwrap() {
|
2018-05-03 23:22:08 +00:00
|
|
|
&AsyncResultItem::Err(ref err) => Some(err),
|
2017-12-09 21:25:06 +00:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2017-11-29 03:49:17 +00:00
|
|
|
}
|
|
|
|
|
2018-05-03 23:22:08 +00:00
|
|
|
impl Responder for AsyncResult<HttpResponse> {
|
|
|
|
type Item = AsyncResult<HttpResponse>;
|
2017-12-03 22:22:04 +00:00
|
|
|
type Error = Error;
|
|
|
|
|
2018-05-04 18:44:22 +00:00
|
|
|
fn respond_to<S>(
|
|
|
|
self, _: &HttpRequest<S>,
|
|
|
|
) -> Result<AsyncResult<HttpResponse>, Error> {
|
2017-12-03 22:22:04 +00:00
|
|
|
Ok(self)
|
2017-12-03 00:37:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-14 17:43:42 +00:00
|
|
|
impl Responder for HttpResponse {
|
2018-05-03 23:22:08 +00:00
|
|
|
type Item = AsyncResult<HttpResponse>;
|
2017-12-03 22:22:04 +00:00
|
|
|
type Error = Error;
|
|
|
|
|
2017-12-16 04:00:12 +00:00
|
|
|
#[inline]
|
2018-05-04 18:44:22 +00:00
|
|
|
fn respond_to<S>(
|
|
|
|
self, _: &HttpRequest<S>,
|
|
|
|
) -> Result<AsyncResult<HttpResponse>, Error> {
|
2018-05-03 23:22:08 +00:00
|
|
|
Ok(AsyncResult(Some(AsyncResultItem::Ok(self))))
|
2017-10-15 21:17:41 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-29 18:31:24 +00:00
|
|
|
|
2018-05-03 23:22:08 +00:00
|
|
|
impl<T> From<T> for AsyncResult<T> {
|
2017-12-16 04:00:12 +00:00
|
|
|
#[inline]
|
2018-05-03 23:22:08 +00:00
|
|
|
fn from(resp: T) -> AsyncResult<T> {
|
|
|
|
AsyncResult(Some(AsyncResultItem::Ok(resp)))
|
2017-12-02 05:29:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-13 23:02:01 +00:00
|
|
|
impl<T: Responder, E: Into<Error>> Responder for Result<T, E> {
|
2017-12-14 17:43:42 +00:00
|
|
|
type Item = <T as Responder>::Item;
|
2017-12-04 00:57:25 +00:00
|
|
|
type Error = Error;
|
2017-12-03 22:22:04 +00:00
|
|
|
|
2018-05-04 18:44:22 +00:00
|
|
|
fn respond_to<S: 'static>(self, req: &HttpRequest<S>) -> Result<Self::Item, Error> {
|
2017-12-03 00:37:21 +00:00
|
|
|
match self {
|
2017-12-14 17:43:42 +00:00
|
|
|
Ok(val) => match val.respond_to(req) {
|
2017-12-04 00:57:25 +00:00
|
|
|
Ok(val) => Ok(val),
|
|
|
|
Err(err) => Err(err.into()),
|
|
|
|
},
|
|
|
|
Err(err) => Err(err.into()),
|
2017-12-03 00:37:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-03 23:22:08 +00:00
|
|
|
impl<T, E: Into<Error>> From<Result<AsyncResult<T>, E>> for AsyncResult<T> {
|
2018-01-03 07:43:17 +00:00
|
|
|
#[inline]
|
2018-05-03 23:22:08 +00:00
|
|
|
fn from(res: Result<AsyncResult<T>, E>) -> Self {
|
2017-11-29 18:31:24 +00:00
|
|
|
match res {
|
|
|
|
Ok(val) => val,
|
2018-05-03 23:22:08 +00:00
|
|
|
Err(err) => AsyncResult(Some(AsyncResultItem::Err(err.into()))),
|
2017-12-03 00:37:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-03 23:22:08 +00:00
|
|
|
impl<T, E: Into<Error>> From<Result<T, E>> for AsyncResult<T> {
|
2018-01-03 07:43:17 +00:00
|
|
|
#[inline]
|
2018-05-02 00:19:15 +00:00
|
|
|
fn from(res: Result<T, E>) -> Self {
|
2018-01-03 07:43:17 +00:00
|
|
|
match res {
|
2018-05-03 23:22:08 +00:00
|
|
|
Ok(val) => AsyncResult(Some(AsyncResultItem::Ok(val))),
|
|
|
|
Err(err) => AsyncResult(Some(AsyncResultItem::Err(err.into()))),
|
2018-05-02 00:19:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T, E: Into<Error>> From<Result<Box<Future<Item = T, Error = Error>>, E>>
|
2018-05-03 23:22:08 +00:00
|
|
|
for AsyncResult<T>
|
2018-05-02 00:19:15 +00:00
|
|
|
{
|
|
|
|
#[inline]
|
|
|
|
fn from(res: Result<Box<Future<Item = T, Error = Error>>, E>) -> Self {
|
|
|
|
match res {
|
2018-05-03 23:22:08 +00:00
|
|
|
Ok(fut) => AsyncResult(Some(AsyncResultItem::Future(fut))),
|
|
|
|
Err(err) => AsyncResult(Some(AsyncResultItem::Err(err.into()))),
|
2018-01-03 07:43:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-03 23:22:08 +00:00
|
|
|
impl<T> From<Box<Future<Item = T, Error = Error>>> for AsyncResult<T> {
|
2018-01-03 07:43:17 +00:00
|
|
|
#[inline]
|
2018-05-03 23:22:08 +00:00
|
|
|
fn from(fut: Box<Future<Item = T, Error = Error>>) -> AsyncResult<T> {
|
|
|
|
AsyncResult(Some(AsyncResultItem::Future(fut)))
|
2018-01-03 07:43:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-26 22:58:30 +00:00
|
|
|
/// Convenience type alias
|
2018-04-13 23:02:01 +00:00
|
|
|
pub type FutureResponse<I, E = Error> = Box<Future<Item = I, Error = E>>;
|
2018-03-26 22:58:30 +00:00
|
|
|
|
2018-04-13 23:02:01 +00:00
|
|
|
impl<I, E> Responder for Box<Future<Item = I, Error = E>>
|
|
|
|
where
|
|
|
|
I: Responder + 'static,
|
|
|
|
E: Into<Error> + 'static,
|
2017-11-30 23:13:56 +00:00
|
|
|
{
|
2018-05-03 23:22:08 +00:00
|
|
|
type Item = AsyncResult<HttpResponse>;
|
2017-12-03 22:22:04 +00:00
|
|
|
type Error = Error;
|
|
|
|
|
2017-12-16 04:00:12 +00:00
|
|
|
#[inline]
|
2018-05-04 18:44:22 +00:00
|
|
|
fn respond_to<S: 'static>(
|
|
|
|
self, req: &HttpRequest<S>,
|
|
|
|
) -> Result<AsyncResult<HttpResponse>, Error> {
|
|
|
|
let req = req.clone();
|
2018-05-17 19:20:20 +00:00
|
|
|
let fut = self
|
|
|
|
.map_err(|e| e.into())
|
2018-05-04 18:44:22 +00:00
|
|
|
.then(move |r| match r.respond_to(&req) {
|
2018-05-02 23:33:29 +00:00
|
|
|
Ok(reply) => match reply.into().into() {
|
2018-05-03 23:22:08 +00:00
|
|
|
AsyncResultItem::Ok(resp) => ok(resp),
|
2018-04-29 16:09:08 +00:00
|
|
|
_ => panic!("Nested async replies are not supported"),
|
|
|
|
},
|
|
|
|
Err(e) => err(e),
|
|
|
|
});
|
2018-05-03 23:22:08 +00:00
|
|
|
Ok(AsyncResult::async(Box::new(fut)))
|
2017-11-30 23:13:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-29 21:26:55 +00:00
|
|
|
pub(crate) trait RouteHandler<S>: 'static {
|
2018-06-25 04:58:04 +00:00
|
|
|
fn handle(&self, &HttpRequest<S>) -> AsyncResult<HttpResponse>;
|
2018-06-21 08:17:27 +00:00
|
|
|
|
|
|
|
fn has_default_resource(&self) -> bool {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2018-07-12 09:30:01 +00:00
|
|
|
fn default_resource(&mut self, _: Rc<Resource<S>>) {
|
2018-06-21 08:17:27 +00:00
|
|
|
unimplemented!()
|
|
|
|
}
|
2017-11-29 21:26:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Route handler wrapper for Handler
|
2018-04-13 23:02:01 +00:00
|
|
|
pub(crate) struct WrapHandler<S, H, R>
|
|
|
|
where
|
|
|
|
H: Handler<S, Result = R>,
|
|
|
|
R: Responder,
|
|
|
|
S: 'static,
|
2017-11-29 21:26:55 +00:00
|
|
|
{
|
|
|
|
h: H,
|
|
|
|
s: PhantomData<S>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S, H, R> WrapHandler<S, H, R>
|
2018-04-13 23:02:01 +00:00
|
|
|
where
|
|
|
|
H: Handler<S, Result = R>,
|
|
|
|
R: Responder,
|
|
|
|
S: 'static,
|
2017-11-29 21:26:55 +00:00
|
|
|
{
|
|
|
|
pub fn new(h: H) -> Self {
|
2018-05-17 19:20:20 +00:00
|
|
|
WrapHandler { h, s: PhantomData }
|
2017-11-29 21:26:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S, H, R> RouteHandler<S> for WrapHandler<S, H, R>
|
2018-04-13 23:02:01 +00:00
|
|
|
where
|
|
|
|
H: Handler<S, Result = R>,
|
|
|
|
R: Responder + 'static,
|
|
|
|
S: 'static,
|
2017-11-29 21:26:55 +00:00
|
|
|
{
|
2018-06-25 04:58:04 +00:00
|
|
|
fn handle(&self, req: &HttpRequest<S>) -> AsyncResult<HttpResponse> {
|
|
|
|
match self.h.handle(req).respond_to(req) {
|
2017-12-03 22:22:04 +00:00
|
|
|
Ok(reply) => reply.into(),
|
2018-05-04 18:44:22 +00:00
|
|
|
Err(err) => AsyncResult::err(err.into()),
|
2017-12-03 22:22:04 +00:00
|
|
|
}
|
2017-11-29 21:26:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Async route handler
|
2018-04-13 23:02:01 +00:00
|
|
|
pub(crate) struct AsyncHandler<S, H, F, R, E>
|
|
|
|
where
|
2018-06-25 04:58:04 +00:00
|
|
|
H: Fn(&HttpRequest<S>) -> F + 'static,
|
2018-04-13 23:02:01 +00:00
|
|
|
F: Future<Item = R, Error = E> + 'static,
|
|
|
|
R: Responder + 'static,
|
|
|
|
E: Into<Error> + 'static,
|
|
|
|
S: 'static,
|
2017-11-29 21:26:55 +00:00
|
|
|
{
|
2017-12-20 20:51:39 +00:00
|
|
|
h: Box<H>,
|
2017-11-29 21:26:55 +00:00
|
|
|
s: PhantomData<S>,
|
|
|
|
}
|
|
|
|
|
2017-12-20 20:51:39 +00:00
|
|
|
impl<S, H, F, R, E> AsyncHandler<S, H, F, R, E>
|
2018-04-13 23:02:01 +00:00
|
|
|
where
|
2018-06-25 04:58:04 +00:00
|
|
|
H: Fn(&HttpRequest<S>) -> F + 'static,
|
2018-04-13 23:02:01 +00:00
|
|
|
F: Future<Item = R, Error = E> + 'static,
|
|
|
|
R: Responder + 'static,
|
|
|
|
E: Into<Error> + 'static,
|
|
|
|
S: 'static,
|
2017-11-29 21:26:55 +00:00
|
|
|
{
|
2017-12-20 20:51:39 +00:00
|
|
|
pub fn new(h: H) -> Self {
|
2018-04-13 23:02:01 +00:00
|
|
|
AsyncHandler {
|
|
|
|
h: Box::new(h),
|
|
|
|
s: PhantomData,
|
|
|
|
}
|
2017-11-29 21:26:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-20 20:51:39 +00:00
|
|
|
impl<S, H, F, R, E> RouteHandler<S> for AsyncHandler<S, H, F, R, E>
|
2018-04-13 23:02:01 +00:00
|
|
|
where
|
2018-06-25 04:58:04 +00:00
|
|
|
H: Fn(&HttpRequest<S>) -> F + 'static,
|
2018-04-13 23:02:01 +00:00
|
|
|
F: Future<Item = R, Error = E> + 'static,
|
|
|
|
R: Responder + 'static,
|
|
|
|
E: Into<Error> + 'static,
|
|
|
|
S: 'static,
|
2017-11-29 21:26:55 +00:00
|
|
|
{
|
2018-06-25 04:58:04 +00:00
|
|
|
fn handle(&self, req: &HttpRequest<S>) -> AsyncResult<HttpResponse> {
|
|
|
|
let req = req.clone();
|
|
|
|
let fut = (self.h)(&req).map_err(|e| e.into()).then(move |r| {
|
2018-05-17 19:20:20 +00:00
|
|
|
match r.respond_to(&req) {
|
2018-05-02 23:33:29 +00:00
|
|
|
Ok(reply) => match reply.into().into() {
|
2018-05-04 18:44:22 +00:00
|
|
|
AsyncResultItem::Ok(resp) => Either::A(ok(resp)),
|
|
|
|
AsyncResultItem::Err(e) => Either::A(err(e)),
|
|
|
|
AsyncResultItem::Future(fut) => Either::B(fut),
|
2018-04-13 23:02:01 +00:00
|
|
|
},
|
2018-05-04 18:44:22 +00:00
|
|
|
Err(e) => Either::A(err(e)),
|
2018-05-17 19:20:20 +00:00
|
|
|
}
|
|
|
|
});
|
2018-05-03 23:22:08 +00:00
|
|
|
AsyncResult::async(Box::new(fut))
|
2017-11-29 21:26:55 +00:00
|
|
|
}
|
|
|
|
}
|
2018-03-29 22:41:13 +00:00
|
|
|
|
2018-04-02 21:00:18 +00:00
|
|
|
/// Access an application state
|
2018-03-29 22:41:13 +00:00
|
|
|
///
|
|
|
|
/// `S` - application state type
|
|
|
|
///
|
|
|
|
/// ## Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # extern crate bytes;
|
|
|
|
/// # extern crate actix_web;
|
|
|
|
/// # extern crate futures;
|
|
|
|
/// #[macro_use] extern crate serde_derive;
|
2018-06-01 16:37:14 +00:00
|
|
|
/// use actix_web::{http, App, Path, State};
|
2018-03-29 22:41:13 +00:00
|
|
|
///
|
|
|
|
/// /// Application state
|
2018-06-01 16:37:14 +00:00
|
|
|
/// struct MyApp {
|
|
|
|
/// msg: &'static str,
|
|
|
|
/// }
|
2018-03-29 22:41:13 +00:00
|
|
|
///
|
|
|
|
/// #[derive(Deserialize)]
|
|
|
|
/// struct Info {
|
|
|
|
/// username: String,
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// /// extract path info using serde
|
2018-05-11 22:01:15 +00:00
|
|
|
/// fn index(data: (State<MyApp>, Path<Info>)) -> String {
|
|
|
|
/// let (state, path) = data;
|
|
|
|
/// format!("{} {}!", state.msg, path.username)
|
2018-03-29 22:41:13 +00:00
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
2018-06-01 16:37:14 +00:00
|
|
|
/// let app = App::with_state(MyApp { msg: "Welcome" }).resource(
|
|
|
|
/// "/{username}/index.html", // <- define path parameters
|
|
|
|
/// |r| r.method(http::Method::GET).with(index),
|
|
|
|
/// ); // <- use `with` extractor
|
2018-03-29 22:41:13 +00:00
|
|
|
/// }
|
|
|
|
/// ```
|
2018-04-13 23:02:01 +00:00
|
|
|
pub struct State<S>(HttpRequest<S>);
|
2018-03-29 22:41:13 +00:00
|
|
|
|
|
|
|
impl<S> Deref for State<S> {
|
|
|
|
type Target = S;
|
|
|
|
|
|
|
|
fn deref(&self) -> &S {
|
|
|
|
self.0.state()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-02 13:07:30 +00:00
|
|
|
impl<S> FromRequest<S> for State<S> {
|
2018-04-04 05:06:18 +00:00
|
|
|
type Config = ();
|
2018-05-02 00:19:15 +00:00
|
|
|
type Result = State<S>;
|
2018-03-29 22:41:13 +00:00
|
|
|
|
|
|
|
#[inline]
|
2018-05-02 13:07:30 +00:00
|
|
|
fn from_request(req: &HttpRequest<S>, _: &Self::Config) -> Self::Result {
|
|
|
|
State(req.clone())
|
2018-03-29 22:41:13 +00:00
|
|
|
}
|
|
|
|
}
|