1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-06-02 21:39:26 +00:00
actix-web/src/route.rs

178 lines
5.3 KiB
Rust
Raw Normal View History

2017-10-15 21:17:41 +00:00
use std::io;
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;
use actix::Actor;
2017-10-15 22:52:52 +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-10-29 13:05:31 +00:00
use task::{Task, DrainFut};
2017-10-24 06:39:01 +00:00
use body::BinaryBody;
2017-10-07 04:48:14 +00:00
use context::HttpContext;
2017-10-08 21:56:51 +00:00
use resource::Reply;
2017-10-09 03:16:48 +00:00
use payload::Payload;
use httprequest::HttpRequest;
2017-10-24 06:25:32 +00:00
use httpresponse::HttpResponse;
2017-10-15 22:52:52 +00:00
use httpcodes::HTTPExpectationFailed;
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)]
#[cfg_attr(feature="cargo-clippy", allow(large_enum_variant))]
pub enum Frame {
Message(HttpResponse),
2017-10-24 06:39:01 +00:00
Payload(Option<BinaryBody>),
2017-10-29 13:05:31 +00:00
Drain(Rc<RefCell<DrainFut>>),
2017-10-07 04:48:14 +00:00
}
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
fn handle(&self, req: &mut HttpRequest, payload: Payload, state: Rc<S>) -> 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-10-22 16:13:29 +00:00
/// Request handling result.
pub type RouteResult<T> = Result<Reply<T>, HttpResponse>;
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-10-10 06:07:32 +00:00
pub trait Route: Actor {
2017-10-15 22:59:26 +00:00
/// Shared state. State is shared with all routes within same application
/// and could be accessed with `HttpContext::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-10-15 22:52:52 +00:00
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 {
2017-10-24 06:25:32 +00:00
Err(HTTPExpectationFailed.with_body("Unknown Expect"))
2017-10-15 22:52:52 +00:00
}
} else {
2017-10-24 06:25:32 +00:00
Err(HTTPExpectationFailed.with_body("Unknown Expect"))
2017-10-15 22:52:52 +00:00
}
} else {
Ok(())
}
} else {
Ok(())
}
}
2017-10-08 06:59:57 +00:00
/// Handle incoming request. Route actor can return
2017-10-15 22:59:26 +00:00
/// result immediately with `Reply::reply`.
2017-10-16 08:19:23 +00:00
/// Actor itself can be returned with `Reply::stream` for handling streaming
/// request/response or websocket connection.
2017-10-15 22:59:26 +00:00
/// In that case `HttpContext::start` and `HttpContext::write` has to be used
/// for writing response.
fn request(req: &mut HttpRequest,
2017-10-22 16:13:29 +00:00
payload: Payload, ctx: &mut Self::Context) -> RouteResult<Self>;
2017-10-07 04:48:14 +00:00
2017-10-08 06:59:57 +00:00
/// This method creates `RouteFactory` for this actor.
2017-10-07 04:48:14 +00:00
fn factory() -> RouteFactory<Self, Self::State> {
RouteFactory(PhantomData)
}
}
2017-10-08 21:56:51 +00:00
/// This is used for routes registration within `Resource`
2017-10-07 04:48:14 +00:00
pub struct RouteFactory<A: Route<State=S>, S>(PhantomData<A>);
impl<A, S> RouteHandler<S> for RouteFactory<A, S>
2017-10-10 06:07:32 +00:00
where A: Actor<Context=HttpContext<A>> + Route<State=S>,
2017-10-07 04:48:14 +00:00
S: 'static
{
fn handle(&self, req: &mut HttpRequest, payload: Payload, state: Rc<A::State>) -> Task
2017-10-07 04:48:14 +00:00
{
2017-10-07 06:14:13 +00:00
let mut ctx = HttpContext::new(state);
2017-10-15 22:52:52 +00:00
// handle EXPECT header
if req.headers().contains_key(header::EXPECT) {
2017-10-23 00:33:24 +00:00
if let Err(resp) = A::expect(req, &mut ctx) {
2017-10-15 22:52:52 +00:00
return Task::reply(resp)
}
}
2017-10-22 16:13:29 +00:00
match A::request(req, payload, &mut ctx) {
Ok(reply) => reply.into(ctx),
Err(err) => Task::reply(err),
}
2017-10-07 04:48:14 +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>
where F: Fn(&mut HttpRequest, Payload, &S) -> R + 'static,
2017-10-15 21:17:41 +00:00
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,
2017-10-15 21:17:41 +00: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>
where F: Fn(&mut HttpRequest, Payload, &S) -> R + 'static,
2017-10-15 21:17:41 +00:00
R: Into<HttpResponse> + 'static,
S: 'static,
{
fn handle(&self, req: &mut HttpRequest, payload: Payload, state: Rc<S>) -> Task
2017-10-15 21:17:41 +00:00
{
Task::reply((self.f)(req, payload, &state).into())
}
}
/// Async route handler
pub(crate)
struct StreamHandler<S, R, F>
where F: Fn(&mut HttpRequest, Payload, &S) -> R + 'static,
2017-10-15 21:17:41 +00:00
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,
2017-10-15 21:17:41 +00:00
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,
2017-10-15 21:17:41 +00:00
R: Stream<Item=Frame, Error=()> + 'static,
S: 'static,
{
fn handle(&self, req: &mut HttpRequest, payload: Payload, state: Rc<S>) -> Task
2017-10-15 21:17:41 +00:00
{
Task::with_stream(
(self.f)(req, payload, &state).map_err(
|_| io::Error::new(io::ErrorKind::Other, ""))
)
}
}