use std::io;
use std::rc::Rc;
use std::cell::RefCell;
use std::marker::PhantomData;
use actix::Actor;
use http::{header, Version};
use futures::Stream;
use task::{Task, DrainFut};
use body::BinaryBody;
use context::HttpContext;
use resource::Reply;
use payload::Payload;
use httprequest::HttpRequest;
use httpresponse::HttpResponse;
use httpcodes::HTTPExpectationFailed;
#[doc(hidden)]
#[derive(Debug)]
#[cfg_attr(feature="cargo-clippy", allow(large_enum_variant))]
pub enum Frame {
Message(HttpResponse),
Payload(Option<BinaryBody>),
Drain(Rc<RefCell<DrainFut>>),
}
impl Frame {
pub fn eof() -> Frame {
Frame::Payload(None)
}
}
#[allow(unused_variables)]
pub trait RouteHandler<S>: 'static {
fn handle(&self, req: &mut HttpRequest, payload: Payload, state: Rc<S>) -> Task;
fn set_prefix(&mut self, prefix: String) {}
}
pub type RouteResult<T> = Result<Reply<T>, HttpResponse>;
#[allow(unused_variables)]
pub trait Route: Actor {
type State;
fn expect(req: &HttpRequest, ctx: &mut Self::Context) -> Result<(), HttpResponse>
where Self: Actor<Context=HttpContext<Self>>
{
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("Unknown Expect"))
}
} else {
Err(HTTPExpectationFailed.with_body("Unknown Expect"))
}
} else {
Ok(())
}
} else {
Ok(())
}
}
fn request(req: &mut HttpRequest,
payload: Payload, ctx: &mut Self::Context) -> RouteResult<Self>;
fn factory() -> RouteFactory<Self, Self::State> {
RouteFactory(PhantomData)
}
}
pub struct RouteFactory<A: Route<State=S>, S>(PhantomData<A>);
impl<A, S> RouteHandler<S> for RouteFactory<A, S>
where A: Actor<Context=HttpContext<A>> + Route<State=S>,
S: 'static
{
fn handle(&self, req: &mut HttpRequest, payload: Payload, state: Rc<A::State>) -> Task
{
let mut ctx = HttpContext::new(state);
if req.headers().contains_key(header::EXPECT) {
if let Err(resp) = A::expect(req, &mut ctx) {
return Task::reply(resp)
}
}
match A::request(req, payload, &mut ctx) {
Ok(reply) => reply.into(ctx),
Err(err) => Task::reply(err),
}
}
}
pub(crate)
struct FnHandler<S, R, F>
where F: Fn(&mut 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(&mut 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(&mut HttpRequest, Payload, &S) -> R + 'static,
R: Into<HttpResponse> + 'static,
S: 'static,
{
fn handle(&self, req: &mut HttpRequest, payload: Payload, state: Rc<S>) -> Task
{
Task::reply((self.f)(req, payload, &state).into())
}
}
pub(crate)
struct StreamHandler<S, R, F>
where F: Fn(&mut 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(&mut 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(&mut HttpRequest, Payload, &S) -> R + 'static,
R: Stream<Item=Frame, Error=()> + 'static,
S: 'static,
{
fn handle(&self, req: &mut HttpRequest, payload: Payload, state: Rc<S>) -> Task
{
Task::with_stream(
(self.f)(req, payload, &state).map_err(
|_| io::Error::new(io::ErrorKind::Other, ""))
)
}
}