2017-10-07 04:48:14 +00:00
|
|
|
use std::rc::Rc;
|
2017-10-29 13:05:31 +00:00
|
|
|
use std::cell::RefCell;
|
2017-10-07 04:48:14 +00:00
|
|
|
use std::marker::PhantomData;
|
2017-11-29 18:31:24 +00:00
|
|
|
use std::result::Result as StdResult;
|
2017-10-07 04:48:14 +00:00
|
|
|
|
|
|
|
use actix::Actor;
|
2017-11-29 18:31:24 +00:00
|
|
|
// use http::{header, Version};
|
2017-10-15 21:17:41 +00:00
|
|
|
use futures::Stream;
|
2017-10-07 04:48:14 +00:00
|
|
|
|
2017-11-29 03:49:17 +00:00
|
|
|
use task::{Task, DrainFut, IoContext};
|
2017-11-10 21:42:32 +00:00
|
|
|
use body::Binary;
|
2017-11-29 18:31:24 +00:00
|
|
|
use error::{Error}; //, ExpectError, Result};
|
2017-10-07 04:48:14 +00:00
|
|
|
use context::HttpContext;
|
2017-10-15 05:52:38 +00:00
|
|
|
use httprequest::HttpRequest;
|
2017-10-24 06:25:32 +00:00
|
|
|
use httpresponse::HttpResponse;
|
2017-10-07 04:48:14 +00:00
|
|
|
|
2017-10-07 06:14:13 +00:00
|
|
|
#[doc(hidden)]
|
2017-10-07 04:48:14 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Frame {
|
2017-10-13 21:43:17 +00:00
|
|
|
Message(HttpResponse),
|
2017-11-10 21:42:32 +00:00
|
|
|
Payload(Option<Binary>),
|
2017-10-29 13:05:31 +00:00
|
|
|
Drain(Rc<RefCell<DrainFut>>),
|
2017-10-07 04:48:14 +00:00
|
|
|
}
|
|
|
|
|
2017-11-03 20:35:34 +00:00
|
|
|
impl Frame {
|
|
|
|
pub fn eof() -> Frame {
|
|
|
|
Frame::Payload(None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-08 21:56:51 +00:00
|
|
|
/// Trait defines object that could be regestered as resource route
|
2017-10-16 08:19:23 +00:00
|
|
|
#[allow(unused_variables)]
|
2017-10-07 04:48:14 +00:00
|
|
|
pub trait RouteHandler<S>: 'static {
|
2017-10-10 06:07:32 +00:00
|
|
|
/// Handle request
|
2017-11-29 03:49:17 +00:00
|
|
|
fn handle(&self, req: HttpRequest<S>, task: &mut Task);
|
2017-10-10 06:07:32 +00:00
|
|
|
|
|
|
|
/// Set route prefix
|
2017-10-16 08:19:23 +00:00
|
|
|
fn set_prefix(&mut self, prefix: String) {}
|
2017-10-07 04:48:14 +00:00
|
|
|
}
|
|
|
|
|
2017-11-29 18:31:24 +00:00
|
|
|
/*
|
2017-10-15 22:59:26 +00:00
|
|
|
/// Actors with ability to handle http requests.
|
2017-10-15 22:52:52 +00:00
|
|
|
#[allow(unused_variables)]
|
2017-11-29 18:31:24 +00:00
|
|
|
pub trait RouteState {
|
2017-10-15 22:59:26 +00:00
|
|
|
/// Shared state. State is shared with all routes within same application
|
2017-11-27 05:18:38 +00:00
|
|
|
/// and could be accessed with `HttpRequest::state()` method.
|
2017-10-07 04:48:14 +00:00
|
|
|
type State;
|
|
|
|
|
2017-10-16 08:19:23 +00:00
|
|
|
/// Handle `EXPECT` header. By default respones with `HTTP/1.1 100 Continue`
|
2017-11-29 03:49:17 +00:00
|
|
|
fn expect(req: &mut HttpRequest<Self::State>, ctx: &mut Self::Context) -> Result<()>
|
2017-10-15 22:52:52 +00:00
|
|
|
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 {
|
2017-11-20 03:51:14 +00:00
|
|
|
Err(ExpectError::UnknownExpect.into())
|
2017-10-15 22:52:52 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-11-20 03:51:14 +00:00
|
|
|
Err(ExpectError::Encoding.into())
|
2017-10-15 22:52:52 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-29 18:31:24 +00:00
|
|
|
/// Handle incoming request with http actor.
|
|
|
|
fn handle(req: HttpRequest<Self::State>) -> Result<Reply>
|
|
|
|
where Self: Default, Self: Actor<Context=HttpContext<Self>>
|
|
|
|
{
|
|
|
|
Ok(HttpContext::new(req, Self::default()).into())
|
2017-10-07 04:48:14 +00:00
|
|
|
}
|
2017-11-29 18:31:24 +00:00
|
|
|
}*/
|
2017-10-15 21:17:41 +00:00
|
|
|
|
2017-10-15 22:59:26 +00:00
|
|
|
/// Fn() route handler
|
2017-10-15 21:17:41 +00:00
|
|
|
pub(crate)
|
|
|
|
struct FnHandler<S, R, F>
|
2017-11-27 05:18:38 +00:00
|
|
|
where F: Fn(HttpRequest<S>) -> R + 'static,
|
2017-11-29 17:17:00 +00:00
|
|
|
R: Into<Reply>,
|
2017-10-15 21:17:41 +00:00
|
|
|
S: 'static,
|
|
|
|
{
|
|
|
|
f: Box<F>,
|
|
|
|
s: PhantomData<S>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S, R, F> FnHandler<S, R, F>
|
2017-11-27 05:18:38 +00:00
|
|
|
where F: Fn(HttpRequest<S>) -> R + 'static,
|
2017-11-29 17:17:00 +00:00
|
|
|
R: Into<Reply> + 'static,
|
2017-10-15 21:17:41 +00:00
|
|
|
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>
|
2017-11-27 05:18:38 +00:00
|
|
|
where F: Fn(HttpRequest<S>) -> R + 'static,
|
2017-11-29 17:17:00 +00:00
|
|
|
R: Into<Reply> + 'static,
|
2017-10-15 21:17:41 +00:00
|
|
|
S: 'static,
|
|
|
|
{
|
2017-11-29 03:49:17 +00:00
|
|
|
fn handle(&self, req: HttpRequest<S>, task: &mut Task) {
|
2017-11-29 17:17:00 +00:00
|
|
|
(self.f)(req).into().into(task)
|
2017-10-15 21:17:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Async route handler
|
|
|
|
pub(crate)
|
|
|
|
struct StreamHandler<S, R, F>
|
2017-11-27 05:18:38 +00:00
|
|
|
where F: Fn(HttpRequest<S>) -> R + 'static,
|
2017-11-16 06:06:28 +00:00
|
|
|
R: Stream<Item=Frame, Error=Error> + 'static,
|
2017-10-15 21:17:41 +00:00
|
|
|
S: 'static,
|
|
|
|
{
|
|
|
|
f: Box<F>,
|
|
|
|
s: PhantomData<S>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S, R, F> StreamHandler<S, R, F>
|
2017-11-27 05:18:38 +00:00
|
|
|
where F: Fn(HttpRequest<S>) -> R + 'static,
|
2017-11-16 06:06:28 +00:00
|
|
|
R: Stream<Item=Frame, Error=Error> + 'static,
|
2017-10-15 21:17:41 +00:00
|
|
|
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>
|
2017-11-27 05:18:38 +00:00
|
|
|
where F: Fn(HttpRequest<S>) -> R + 'static,
|
2017-11-16 06:06:28 +00:00
|
|
|
R: Stream<Item=Frame, Error=Error> + 'static,
|
2017-10-15 21:17:41 +00:00
|
|
|
S: 'static,
|
|
|
|
{
|
2017-11-29 03:49:17 +00:00
|
|
|
fn handle(&self, req: HttpRequest<S>, task: &mut Task) {
|
|
|
|
task.stream((self.f)(req))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enum ReplyItem {
|
|
|
|
Message(HttpResponse),
|
|
|
|
Actor(Box<IoContext<Item=Frame, Error=Error>>),
|
|
|
|
Stream(Box<Stream<Item=Frame, Error=Error>>),
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Represents response process.
|
|
|
|
pub struct Reply(ReplyItem);
|
|
|
|
|
|
|
|
impl Reply
|
|
|
|
{
|
|
|
|
/// Create actor response
|
2017-11-29 18:31:24 +00:00
|
|
|
pub fn actor<A, S>(ctx: HttpContext<A, S>) -> Reply
|
|
|
|
where A: Actor<Context=HttpContext<A, S>>, S: 'static
|
|
|
|
{
|
|
|
|
Reply(ReplyItem::Actor(Box::new(ctx)))
|
2017-11-29 03:49:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create async response
|
2017-11-29 18:31:24 +00:00
|
|
|
pub fn stream<S>(stream: S) -> Reply
|
2017-11-29 03:49:17 +00:00
|
|
|
where S: Stream<Item=Frame, Error=Error> + 'static
|
|
|
|
{
|
2017-11-29 18:31:24 +00:00
|
|
|
Reply(ReplyItem::Stream(Box::new(stream)))
|
2017-11-29 03:49:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Send response
|
2017-11-29 18:31:24 +00:00
|
|
|
pub fn reply<R: Into<HttpResponse>>(response: R) -> Reply {
|
|
|
|
Reply(ReplyItem::Message(response.into()))
|
2017-11-29 03:49:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn into(self, task: &mut Task)
|
|
|
|
{
|
|
|
|
match self.0 {
|
|
|
|
ReplyItem::Message(msg) => {
|
|
|
|
task.reply(msg)
|
|
|
|
},
|
|
|
|
ReplyItem::Actor(ctx) => {
|
|
|
|
task.context(ctx)
|
|
|
|
}
|
|
|
|
ReplyItem::Stream(stream) => {
|
|
|
|
task.stream(stream)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-29 17:17:00 +00:00
|
|
|
impl<T: Into<HttpResponse>> From<T> for Reply
|
2017-11-29 03:49:17 +00:00
|
|
|
{
|
|
|
|
fn from(item: T) -> Self {
|
|
|
|
Reply(ReplyItem::Message(item.into()))
|
2017-10-15 21:17:41 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-29 18:31:24 +00:00
|
|
|
|
|
|
|
impl<E: Into<Error>> From<StdResult<Reply, E>> for Reply {
|
|
|
|
fn from(res: StdResult<Reply, E>) -> Self {
|
|
|
|
match res {
|
|
|
|
Ok(val) => val,
|
|
|
|
Err(err) => err.into().into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<A: Actor<Context=HttpContext<A, S>>, S: 'static> From<HttpContext<A, S>> for Reply
|
|
|
|
{
|
|
|
|
fn from(item: HttpContext<A, S>) -> Self {
|
|
|
|
Reply(ReplyItem::Actor(Box::new(item)))
|
|
|
|
}
|
|
|
|
}
|