2017-10-07 06:48:14 +02:00
|
|
|
use std::rc::Rc;
|
|
|
|
use std::marker::PhantomData;
|
|
|
|
|
|
|
|
use actix::Actor;
|
|
|
|
use bytes::Bytes;
|
|
|
|
use futures::unsync::mpsc::Receiver;
|
|
|
|
|
|
|
|
use task::Task;
|
|
|
|
use context::HttpContext;
|
2017-10-07 08:14:13 +02:00
|
|
|
use resource::HttpMessage;
|
2017-10-07 08:36:36 +02:00
|
|
|
use httpmessage::{HttpRequest, HttpResponse, IntoHttpResponse};
|
2017-10-07 06:48:14 +02:00
|
|
|
|
2017-10-07 08:14:13 +02:00
|
|
|
/// Stream of `PayloadItem`'s
|
2017-10-07 06:48:14 +02:00
|
|
|
pub type Payload = Receiver<PayloadItem>;
|
|
|
|
|
2017-10-07 08:14:13 +02:00
|
|
|
/// `PayloadItem` represents one payload item
|
2017-10-07 06:48:14 +02:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum PayloadItem {
|
2017-10-07 08:14:13 +02:00
|
|
|
/// Indicates end of payload stream
|
2017-10-07 06:48:14 +02:00
|
|
|
Eof,
|
2017-10-07 08:14:13 +02:00
|
|
|
/// Chunk of bytes
|
2017-10-07 06:48:14 +02:00
|
|
|
Chunk(Bytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PayloadItem {
|
2017-10-07 08:14:13 +02:00
|
|
|
/// Is item an eof
|
2017-10-07 06:48:14 +02:00
|
|
|
pub fn is_eof(&self) -> bool {
|
|
|
|
match *self {
|
|
|
|
PayloadItem::Eof => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
2017-10-07 08:14:13 +02:00
|
|
|
/// Is item a chunk
|
2017-10-07 06:48:14 +02:00
|
|
|
pub fn is_chunk(&self) -> bool {
|
|
|
|
!self.is_eof()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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-07 08:14:13 +02:00
|
|
|
Message(HttpResponse),
|
2017-10-07 06:48:14 +02:00
|
|
|
Payload(Option<Bytes>),
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait RouteHandler<S>: 'static {
|
|
|
|
fn handle(&self, req: HttpRequest, payload: Option<Payload>, state: Rc<S>) -> Task;
|
|
|
|
}
|
|
|
|
|
2017-10-07 08:14:13 +02:00
|
|
|
/// Actors with ability to handle http requests
|
2017-10-07 06:48:14 +02:00
|
|
|
pub trait Route: Actor<Context=HttpContext<Self>> {
|
|
|
|
type State;
|
|
|
|
|
|
|
|
fn request(req: HttpRequest,
|
|
|
|
payload: Option<Payload>,
|
2017-10-07 08:14:13 +02:00
|
|
|
ctx: &mut HttpContext<Self>) -> HttpMessage<Self>;
|
2017-10-07 06:48:14 +02:00
|
|
|
|
|
|
|
fn factory() -> RouteFactory<Self, Self::State> {
|
|
|
|
RouteFactory(PhantomData)
|
|
|
|
}
|
2017-10-07 08:36:36 +02:00
|
|
|
|
|
|
|
/// Create async response
|
2017-10-07 09:22:09 +02:00
|
|
|
fn http_stream(act: Self) -> HttpMessage<Self> {
|
2017-10-07 08:36:36 +02:00
|
|
|
HttpMessage::stream(act)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create response
|
2017-10-07 09:22:09 +02:00
|
|
|
fn http_reply<I>(req: HttpRequest, msg: I) -> HttpMessage<Self>
|
2017-10-07 08:36:36 +02:00
|
|
|
where I: IntoHttpResponse
|
|
|
|
{
|
|
|
|
HttpMessage::reply(req, msg)
|
|
|
|
}
|
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>
|
|
|
|
where A: Route<State=S>,
|
|
|
|
S: 'static
|
|
|
|
{
|
|
|
|
fn handle(&self, req: HttpRequest, payload: Option<Payload>, state: Rc<A::State>) -> Task
|
|
|
|
{
|
2017-10-07 08:14:13 +02:00
|
|
|
let mut ctx = HttpContext::new(state);
|
2017-10-07 06:48:14 +02:00
|
|
|
A::request(req, payload, &mut ctx).into(ctx)
|
|
|
|
}
|
|
|
|
}
|