2017-10-15 23:17:41 +02:00
|
|
|
use std::io;
|
2017-10-07 06:48:14 +02:00
|
|
|
use std::rc::Rc;
|
|
|
|
use std::marker::PhantomData;
|
|
|
|
|
|
|
|
use actix::Actor;
|
|
|
|
use bytes::Bytes;
|
2017-10-16 00:52:52 +02:00
|
|
|
use http::{header, Version};
|
2017-10-15 23:17:41 +02:00
|
|
|
use futures::Stream;
|
2017-10-07 06:48:14 +02:00
|
|
|
|
|
|
|
use task::Task;
|
|
|
|
use context::HttpContext;
|
2017-10-08 23:56:51 +02:00
|
|
|
use resource::Reply;
|
2017-10-09 05:16:48 +02:00
|
|
|
use payload::Payload;
|
2017-10-15 07:52:38 +02:00
|
|
|
use httprequest::HttpRequest;
|
2017-10-16 00:52:52 +02:00
|
|
|
use httpresponse::{Body, HttpResponse};
|
|
|
|
use httpcodes::HTTPExpectationFailed;
|
2017-10-07 06:48:14 +02:00
|
|
|
|
2017-10-07 08:14:13 +02:00
|
|
|
#[doc(hidden)]
|
2017-10-07 06:48:14 +02:00
|
|
|
#[derive(Debug)]
|
|
|
|
#[cfg_attr(feature="cargo-clippy", allow(large_enum_variant))]
|
|
|
|
pub enum Frame {
|
2017-10-13 23:43:17 +02:00
|
|
|
Message(HttpResponse),
|
2017-10-07 06:48:14 +02:00
|
|
|
Payload(Option<Bytes>),
|
|
|
|
}
|
|
|
|
|
2017-10-08 23:56:51 +02:00
|
|
|
/// Trait defines object that could be regestered as resource route
|
2017-10-07 06:48:14 +02:00
|
|
|
pub trait RouteHandler<S>: 'static {
|
2017-10-10 08:07:32 +02:00
|
|
|
/// Handle request
|
2017-10-09 05:16:48 +02:00
|
|
|
fn handle(&self, req: HttpRequest, payload: Payload, state: Rc<S>) -> Task;
|
2017-10-10 08:07:32 +02:00
|
|
|
|
|
|
|
/// Set route prefix
|
|
|
|
fn set_prefix(&mut self, _prefix: String) {}
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
|
2017-10-16 00:59:26 +02:00
|
|
|
/// Actors with ability to handle http requests.
|
2017-10-16 00:52:52 +02:00
|
|
|
#[allow(unused_variables)]
|
2017-10-10 08:07:32 +02:00
|
|
|
pub trait Route: Actor {
|
2017-10-16 00:59:26 +02:00
|
|
|
/// Shared state. State is shared with all routes within same application
|
|
|
|
/// and could be accessed with `HttpContext::state()` method.
|
2017-10-07 06:48:14 +02:00
|
|
|
type State;
|
|
|
|
|
2017-10-16 00:52:52 +02:00
|
|
|
/// Handle `EXPECT` header. By default respond with `HTTP/1.1 100 Continue`
|
|
|
|
fn expect(req: &HttpRequest, ctx: &mut Self::Context) -> Result<(), HttpResponse>
|
|
|
|
where Self: Actor<Context=HttpContext<Self>>
|
|
|
|
{
|
|
|
|
// handle expect header only for HTTP/1.1
|
|
|
|
if req.version() == Version::HTTP_11 {
|
|
|
|
if let Some(expect) = req.headers().get(header::EXPECT) {
|
|
|
|
if let Ok(expect) = expect.to_str() {
|
|
|
|
if expect.to_lowercase() == "100-continue" {
|
|
|
|
ctx.write("HTTP/1.1 100 Continue\r\n\r\n");
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(HTTPExpectationFailed.with_body(
|
|
|
|
Body::Binary("Unknown Expect".into())))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Err(HTTPExpectationFailed.with_body(
|
|
|
|
Body::Binary("Unknown Expect".into())))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-08 08:59:57 +02:00
|
|
|
/// Handle incoming request. Route actor can return
|
2017-10-16 00:59:26 +02:00
|
|
|
/// result immediately with `Reply::reply`.
|
2017-10-08 08:59:57 +02:00
|
|
|
/// Actor itself could be returned for handling streaming request/response.
|
2017-10-16 00:59:26 +02:00
|
|
|
/// In that case `HttpContext::start` and `HttpContext::write` has to be used
|
|
|
|
/// for writing response.
|
2017-10-10 08:07:32 +02:00
|
|
|
fn request(req: HttpRequest, payload: Payload, ctx: &mut Self::Context) -> Reply<Self>;
|
2017-10-07 06:48:14 +02:00
|
|
|
|
2017-10-08 08:59:57 +02:00
|
|
|
/// This method creates `RouteFactory` for this actor.
|
2017-10-07 06:48:14 +02:00
|
|
|
fn factory() -> RouteFactory<Self, Self::State> {
|
|
|
|
RouteFactory(PhantomData)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-08 23:56:51 +02:00
|
|
|
/// This is used for routes registration within `Resource`
|
2017-10-07 06:48:14 +02:00
|
|
|
pub struct RouteFactory<A: Route<State=S>, S>(PhantomData<A>);
|
|
|
|
|
|
|
|
impl<A, S> RouteHandler<S> for RouteFactory<A, S>
|
2017-10-10 08:07:32 +02:00
|
|
|
where A: Actor<Context=HttpContext<A>> + Route<State=S>,
|
2017-10-07 06:48:14 +02:00
|
|
|
S: 'static
|
|
|
|
{
|
2017-10-09 05:16:48 +02:00
|
|
|
fn handle(&self, req: HttpRequest, payload: Payload, state: Rc<A::State>) -> Task
|
2017-10-07 06:48:14 +02:00
|
|
|
{
|
2017-10-07 08:14:13 +02:00
|
|
|
let mut ctx = HttpContext::new(state);
|
2017-10-16 00:52:52 +02:00
|
|
|
|
|
|
|
// handle EXPECT header
|
|
|
|
if req.headers().contains_key(header::EXPECT) {
|
|
|
|
if let Err(resp) = A::expect(&req, &mut ctx) {
|
|
|
|
return Task::reply(resp)
|
|
|
|
}
|
|
|
|
}
|
2017-10-07 06:48:14 +02:00
|
|
|
A::request(req, payload, &mut ctx).into(ctx)
|
|
|
|
}
|
|
|
|
}
|
2017-10-15 23:17:41 +02:00
|
|
|
|
2017-10-16 00:59:26 +02:00
|
|
|
/// Fn() route handler
|
2017-10-15 23:17:41 +02:00
|
|
|
pub(crate)
|
|
|
|
struct FnHandler<S, R, F>
|
|
|
|
where F: Fn(HttpRequest, Payload, &S) -> R + 'static,
|
|
|
|
R: Into<HttpResponse>,
|
|
|
|
S: 'static,
|
|
|
|
{
|
|
|
|
f: Box<F>,
|
|
|
|
s: PhantomData<S>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S, R, F> FnHandler<S, R, F>
|
|
|
|
where F: Fn(HttpRequest, Payload, &S) -> R + 'static,
|
|
|
|
R: Into<HttpResponse> + 'static,
|
|
|
|
S: 'static,
|
|
|
|
{
|
|
|
|
pub fn new(f: F) -> Self {
|
|
|
|
FnHandler{f: Box::new(f), s: PhantomData}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S, R, F> RouteHandler<S> for FnHandler<S, R, F>
|
|
|
|
where F: Fn(HttpRequest, Payload, &S) -> R + 'static,
|
|
|
|
R: Into<HttpResponse> + 'static,
|
|
|
|
S: 'static,
|
|
|
|
{
|
|
|
|
fn handle(&self, req: HttpRequest, payload: Payload, state: Rc<S>) -> Task
|
|
|
|
{
|
|
|
|
Task::reply((self.f)(req, payload, &state).into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Async route handler
|
|
|
|
pub(crate)
|
|
|
|
struct StreamHandler<S, R, F>
|
|
|
|
where F: Fn(HttpRequest, Payload, &S) -> R + 'static,
|
|
|
|
R: Stream<Item=Frame, Error=()> + 'static,
|
|
|
|
S: 'static,
|
|
|
|
{
|
|
|
|
f: Box<F>,
|
|
|
|
s: PhantomData<S>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S, R, F> StreamHandler<S, R, F>
|
|
|
|
where F: Fn(HttpRequest, Payload, &S) -> R + 'static,
|
|
|
|
R: Stream<Item=Frame, Error=()> + 'static,
|
|
|
|
S: 'static,
|
|
|
|
{
|
|
|
|
pub fn new(f: F) -> Self {
|
|
|
|
StreamHandler{f: Box::new(f), s: PhantomData}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S, R, F> RouteHandler<S> for StreamHandler<S, R, F>
|
|
|
|
where F: Fn(HttpRequest, Payload, &S) -> R + 'static,
|
|
|
|
R: Stream<Item=Frame, Error=()> + 'static,
|
|
|
|
S: 'static,
|
|
|
|
{
|
|
|
|
fn handle(&self, req: HttpRequest, payload: Payload, state: Rc<S>) -> Task
|
|
|
|
{
|
|
|
|
Task::with_stream(
|
|
|
|
(self.f)(req, payload, &state).map_err(
|
|
|
|
|_| io::Error::new(io::ErrorKind::Other, ""))
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|