1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-24 16:32:59 +01:00
actix-web/src/route.rs

222 lines
6.0 KiB
Rust
Raw Normal View History

2017-10-07 06:48:14 +02:00
use std::rc::Rc;
2017-10-29 14:05:31 +01:00
use std::cell::RefCell;
2017-10-07 06:48:14 +02:00
use std::marker::PhantomData;
use actix::Actor;
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
2017-11-29 04:49:17 +01:00
use task::{Task, DrainFut, IoContext};
2017-11-10 22:42:32 +01:00
use body::Binary;
2017-11-29 04:49:17 +01:00
use error::{Error, ExpectError, Result};
2017-10-07 06:48:14 +02:00
use context::HttpContext;
use httprequest::HttpRequest;
2017-10-24 08:25:32 +02:00
use httpresponse::HttpResponse;
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)]
pub enum Frame {
Message(HttpResponse),
2017-11-10 22:42:32 +01:00
Payload(Option<Binary>),
2017-10-29 14:05:31 +01:00
Drain(Rc<RefCell<DrainFut>>),
2017-10-07 06:48:14 +02:00
}
2017-11-03 21:35:34 +01:00
impl Frame {
pub fn eof() -> Frame {
Frame::Payload(None)
}
}
2017-10-08 23:56:51 +02:00
/// Trait defines object that could be regestered as resource route
2017-10-16 10:19:23 +02:00
#[allow(unused_variables)]
2017-10-07 06:48:14 +02:00
pub trait RouteHandler<S>: 'static {
2017-10-10 08:07:32 +02:00
/// Handle request
2017-11-29 04:49:17 +01:00
fn handle(&self, req: HttpRequest<S>, task: &mut Task);
2017-10-10 08:07:32 +02:00
/// Set route prefix
2017-10-16 10:19:23 +02:00
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
2017-11-27 06:18:38 +01:00
/// and could be accessed with `HttpRequest::state()` method.
2017-10-07 06:48:14 +02:00
type State;
2017-10-16 10:19:23 +02:00
/// Handle `EXPECT` header. By default respones with `HTTP/1.1 100 Continue`
2017-11-29 04:49:17 +01:00
fn expect(req: &mut HttpRequest<Self::State>, ctx: &mut Self::Context) -> Result<()>
2017-10-16 00:52:52 +02: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 04:51:14 +01:00
Err(ExpectError::UnknownExpect.into())
2017-10-16 00:52:52 +02:00
}
} else {
2017-11-20 04:51:14 +01:00
Err(ExpectError::Encoding.into())
2017-10-16 00:52:52 +02:00
}
} 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-16 10:19:23 +02:00
/// Actor itself can be returned with `Reply::stream` for handling streaming
/// request/response or websocket connection.
2017-10-16 00:59:26 +02:00
/// In that case `HttpContext::start` and `HttpContext::write` has to be used
/// for writing response.
2017-11-29 04:49:17 +01:00
fn request(req: HttpRequest<Self::State>, ctx: Self::Context) -> Result<Reply>;
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-11-29 04:49:17 +01:00
fn handle(&self, mut req: HttpRequest<A::State>, task: &mut Task) {
2017-11-27 06:47:33 +01:00
let mut ctx = HttpContext::new(req.clone_state());
2017-10-16 00:52:52 +02:00
// handle EXPECT header
if req.headers().contains_key(header::EXPECT) {
2017-11-27 05:32:12 +01:00
if let Err(resp) = A::expect(&mut req, &mut ctx) {
2017-11-29 04:49:17 +01:00
task.reply(resp)
2017-10-16 00:52:52 +02:00
}
}
2017-11-29 04:49:17 +01:00
match A::request(req, ctx) {
Ok(reply) => reply.into(task),
Err(err) => task.reply(err),
2017-10-22 18:13:29 +02:00
}
2017-10-07 06:48:14 +02:00
}
}
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>
2017-11-27 06:18:38 +01:00
where F: Fn(HttpRequest<S>) -> R + 'static,
2017-10-15 23:17:41 +02:00
R: Into<HttpResponse>,
S: 'static,
{
f: Box<F>,
s: PhantomData<S>,
}
impl<S, R, F> FnHandler<S, R, F>
2017-11-27 06:18:38 +01:00
where F: Fn(HttpRequest<S>) -> R + 'static,
2017-10-15 23:17:41 +02:00
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>
2017-11-27 06:18:38 +01:00
where F: Fn(HttpRequest<S>) -> R + 'static,
2017-10-15 23:17:41 +02:00
R: Into<HttpResponse> + 'static,
S: 'static,
{
2017-11-29 04:49:17 +01:00
fn handle(&self, req: HttpRequest<S>, task: &mut Task) {
task.reply((self.f)(req).into())
2017-10-15 23:17:41 +02:00
}
}
/// Async route handler
pub(crate)
struct StreamHandler<S, R, F>
2017-11-27 06:18:38 +01:00
where F: Fn(HttpRequest<S>) -> R + 'static,
2017-11-16 07:06:28 +01:00
R: Stream<Item=Frame, Error=Error> + 'static,
2017-10-15 23:17:41 +02:00
S: 'static,
{
f: Box<F>,
s: PhantomData<S>,
}
impl<S, R, F> StreamHandler<S, R, F>
2017-11-27 06:18:38 +01:00
where F: Fn(HttpRequest<S>) -> R + 'static,
2017-11-16 07:06:28 +01:00
R: Stream<Item=Frame, Error=Error> + 'static,
2017-10-15 23:17:41 +02: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 06:18:38 +01:00
where F: Fn(HttpRequest<S>) -> R + 'static,
2017-11-16 07:06:28 +01:00
R: Stream<Item=Frame, Error=Error> + 'static,
2017-10-15 23:17:41 +02:00
S: 'static,
{
2017-11-29 04:49:17 +01: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
pub(crate) fn async<C: IoContext>(ctx: C) -> Result<Reply> {
Ok(Reply(ReplyItem::Actor(Box::new(ctx))))
}
/// Create async response
pub fn stream<S>(stream: S) -> Result<Reply>
where S: Stream<Item=Frame, Error=Error> + 'static
{
Ok(Reply(ReplyItem::Stream(Box::new(stream))))
}
/// Send response
pub fn reply<R: Into<HttpResponse>>(response: R) -> Result<Reply> {
Ok(Reply(ReplyItem::Message(response.into())))
}
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)
}
}
}
}
impl<T> From<T> for Reply
where T: Into<HttpResponse>
{
fn from(item: T) -> Self {
Reply(ReplyItem::Message(item.into()))
2017-10-15 23:17:41 +02:00
}
}