2017-10-07 04:48:14 +00:00
|
|
|
use std::marker::PhantomData;
|
|
|
|
|
|
|
|
use actix::Actor;
|
2017-11-30 22:42:20 +00:00
|
|
|
use futures::Future;
|
2017-12-04 02:51:52 +00:00
|
|
|
use serde_json;
|
|
|
|
use serde::Serialize;
|
2017-12-09 19:39:13 +00:00
|
|
|
use regex::Regex;
|
|
|
|
use http::{header, StatusCode, Error as HttpError};
|
2017-10-07 04:48:14 +00:00
|
|
|
|
2017-12-09 19:39:13 +00:00
|
|
|
use body::Body;
|
2017-11-29 21:26:55 +00:00
|
|
|
use error::Error;
|
2017-12-01 23:45:15 +00:00
|
|
|
use context::{HttpContext, IoContext};
|
2017-10-15 05:52:38 +00:00
|
|
|
use httprequest::HttpRequest;
|
2017-10-24 06:25:32 +00:00
|
|
|
use httpresponse::HttpResponse;
|
2017-11-03 20:35:34 +00:00
|
|
|
|
2017-11-29 21:26:55 +00:00
|
|
|
/// Trait defines object that could be regestered as route handler
|
2017-10-16 08:19:23 +00:00
|
|
|
#[allow(unused_variables)]
|
2017-11-29 21:26:55 +00:00
|
|
|
pub trait Handler<S>: 'static {
|
2017-11-29 23:07:49 +00:00
|
|
|
|
|
|
|
/// The type of value that handler will return.
|
2017-12-14 17:43:42 +00:00
|
|
|
type Result: Responder;
|
2017-11-29 21:26:55 +00:00
|
|
|
|
2017-10-10 06:07:32 +00:00
|
|
|
/// Handle request
|
2017-11-29 21:26:55 +00:00
|
|
|
fn handle(&self, req: HttpRequest<S>) -> Self::Result;
|
2017-10-07 04:48:14 +00:00
|
|
|
}
|
|
|
|
|
2017-12-14 17:43:42 +00:00
|
|
|
/// Trait implemented by types that generate responses for clients.
|
|
|
|
///
|
|
|
|
/// Types that implement this trait can be used as the return type of a handler.
|
|
|
|
pub trait Responder {
|
2017-12-03 22:22:04 +00:00
|
|
|
/// The associated item which can be returned.
|
|
|
|
type Item: Into<Reply>;
|
|
|
|
|
|
|
|
/// The associated error which can be returned.
|
|
|
|
type Error: Into<Error>;
|
|
|
|
|
2017-12-14 17:43:42 +00:00
|
|
|
/// Convert itself to `Reply` or `Error`.
|
|
|
|
fn respond_to(self, req: HttpRequest) -> Result<Self::Item, Self::Error>;
|
2017-12-03 00:37:21 +00:00
|
|
|
}
|
|
|
|
|
2017-11-29 21:26:55 +00:00
|
|
|
/// Handler<S> for Fn()
|
|
|
|
impl<F, R, S> Handler<S> for F
|
2017-11-27 05:18:38 +00:00
|
|
|
where F: Fn(HttpRequest<S>) -> R + 'static,
|
2017-12-14 17:43:42 +00:00
|
|
|
R: Responder + 'static
|
2017-10-15 21:17:41 +00:00
|
|
|
{
|
2017-11-29 21:26:55 +00:00
|
|
|
type Result = R;
|
2017-10-15 21:17:41 +00:00
|
|
|
|
2017-11-29 21:26:55 +00:00
|
|
|
fn handle(&self, req: HttpRequest<S>) -> R {
|
|
|
|
(self)(req)
|
2017-10-15 21:17:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-29 21:26:55 +00:00
|
|
|
/// Represents response process.
|
|
|
|
pub struct Reply(ReplyItem);
|
2017-11-29 03:49:17 +00:00
|
|
|
|
2017-11-30 23:13:56 +00:00
|
|
|
pub(crate) enum ReplyItem {
|
2017-12-16 00:24:15 +00:00
|
|
|
Message(HttpResponse),
|
2017-11-30 22:42:20 +00:00
|
|
|
Actor(Box<IoContext>),
|
|
|
|
Future(Box<Future<Item=HttpResponse, Error=Error>>),
|
2017-11-29 03:49:17 +00:00
|
|
|
}
|
|
|
|
|
2017-11-29 21:26:55 +00:00
|
|
|
impl Reply {
|
2017-11-29 03:49:17 +00:00
|
|
|
|
|
|
|
/// Create actor response
|
2017-12-13 05:32:58 +00:00
|
|
|
#[inline]
|
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-12-13 05:32:58 +00:00
|
|
|
#[inline]
|
2017-11-30 22:42:20 +00:00
|
|
|
pub fn async<F>(fut: F) -> Reply
|
|
|
|
where F: Future<Item=HttpResponse, Error=Error> + 'static
|
2017-11-29 03:49:17 +00:00
|
|
|
{
|
2017-11-30 22:42:20 +00:00
|
|
|
Reply(ReplyItem::Future(Box::new(fut)))
|
2017-11-29 03:49:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Send response
|
2017-12-13 05:32:58 +00:00
|
|
|
#[inline]
|
2017-11-30 23:13:56 +00:00
|
|
|
pub fn response<R: Into<HttpResponse>>(response: R) -> Reply {
|
2017-12-16 00:24:15 +00:00
|
|
|
Reply(ReplyItem::Message(response.into()))
|
2017-11-29 03:49:17 +00:00
|
|
|
}
|
|
|
|
|
2017-12-13 05:32:58 +00:00
|
|
|
#[inline]
|
2017-11-30 23:13:56 +00:00
|
|
|
pub(crate) fn into(self) -> ReplyItem {
|
|
|
|
self.0
|
2017-11-29 03:49:17 +00:00
|
|
|
}
|
2017-12-09 21:25:06 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
pub(crate) fn as_response(&self) -> Option<&HttpResponse> {
|
|
|
|
match self.0 {
|
|
|
|
ReplyItem::Message(ref resp) => Some(resp),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2017-11-29 03:49:17 +00:00
|
|
|
}
|
|
|
|
|
2017-12-14 17:43:42 +00:00
|
|
|
impl Responder for Reply {
|
2017-12-03 22:22:04 +00:00
|
|
|
type Item = Reply;
|
|
|
|
type Error = Error;
|
|
|
|
|
2017-12-14 17:43:42 +00:00
|
|
|
fn respond_to(self, _: HttpRequest) -> Result<Reply, Error> {
|
2017-12-03 22:22:04 +00:00
|
|
|
Ok(self)
|
2017-12-03 00:37:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-14 17:43:42 +00:00
|
|
|
impl Responder for HttpResponse {
|
2017-12-03 22:22:04 +00:00
|
|
|
type Item = Reply;
|
|
|
|
type Error = Error;
|
|
|
|
|
2017-12-16 04:00:12 +00:00
|
|
|
#[inline]
|
2017-12-14 17:43:42 +00:00
|
|
|
fn respond_to(self, _: HttpRequest) -> Result<Reply, Error> {
|
2017-12-16 00:24:15 +00:00
|
|
|
Ok(Reply(ReplyItem::Message(self)))
|
2017-10-15 21:17:41 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-29 18:31:24 +00:00
|
|
|
|
2017-12-03 22:22:04 +00:00
|
|
|
impl From<HttpResponse> for Reply {
|
|
|
|
|
2017-12-16 04:00:12 +00:00
|
|
|
#[inline]
|
2017-12-03 22:22:04 +00:00
|
|
|
fn from(resp: HttpResponse) -> Reply {
|
2017-12-16 00:24:15 +00:00
|
|
|
Reply(ReplyItem::Message(resp))
|
2017-12-02 05:29:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-14 17:43:42 +00:00
|
|
|
impl<T: Responder, E: Into<Error>> Responder for Result<T, E>
|
2017-12-04 00:57:25 +00:00
|
|
|
{
|
2017-12-14 17:43:42 +00:00
|
|
|
type Item = <T as Responder>::Item;
|
2017-12-04 00:57:25 +00:00
|
|
|
type Error = Error;
|
2017-12-03 22:22:04 +00:00
|
|
|
|
2017-12-14 17:43:42 +00:00
|
|
|
fn respond_to(self, req: HttpRequest) -> Result<Self::Item, Self::Error> {
|
2017-12-03 00:37:21 +00:00
|
|
|
match self {
|
2017-12-14 17:43:42 +00:00
|
|
|
Ok(val) => match val.respond_to(req) {
|
2017-12-04 00:57:25 +00:00
|
|
|
Ok(val) => Ok(val),
|
|
|
|
Err(err) => Err(err.into()),
|
|
|
|
},
|
|
|
|
Err(err) => Err(err.into()),
|
2017-12-03 00:37:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-03 22:22:04 +00:00
|
|
|
impl<E: Into<Error>> From<Result<Reply, E>> for Reply {
|
|
|
|
fn from(res: Result<Reply, E>) -> Self {
|
2017-11-29 18:31:24 +00:00
|
|
|
match res {
|
|
|
|
Ok(val) => val,
|
2017-12-16 00:24:15 +00:00
|
|
|
Err(err) => Reply(ReplyItem::Message(err.into().into())),
|
2017-12-03 00:37:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-14 17:43:42 +00:00
|
|
|
impl<A: Actor<Context=HttpContext<A, S>>, S: 'static> Responder for HttpContext<A, S>
|
2017-11-29 18:31:24 +00:00
|
|
|
{
|
2017-12-03 22:22:04 +00:00
|
|
|
type Item = Reply;
|
|
|
|
type Error = Error;
|
|
|
|
|
2017-12-16 04:00:12 +00:00
|
|
|
#[inline]
|
2017-12-14 17:43:42 +00:00
|
|
|
fn respond_to(self, _: HttpRequest) -> Result<Reply, Error> {
|
2017-12-03 22:22:04 +00:00
|
|
|
Ok(Reply(ReplyItem::Actor(Box::new(self))))
|
2017-12-03 00:37:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<A: Actor<Context=HttpContext<A, S>>, S: 'static> From<HttpContext<A, S>> for Reply {
|
2017-12-03 22:22:04 +00:00
|
|
|
|
2017-12-16 04:00:12 +00:00
|
|
|
#[inline]
|
2017-12-03 00:37:21 +00:00
|
|
|
fn from(ctx: HttpContext<A, S>) -> Reply {
|
|
|
|
Reply(ReplyItem::Actor(Box::new(ctx)))
|
2017-11-29 18:31:24 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-29 21:26:55 +00:00
|
|
|
|
2017-12-14 17:43:42 +00:00
|
|
|
impl Responder for Box<Future<Item=HttpResponse, Error=Error>>
|
2017-11-30 23:13:56 +00:00
|
|
|
{
|
2017-12-03 22:22:04 +00:00
|
|
|
type Item = Reply;
|
|
|
|
type Error = Error;
|
|
|
|
|
2017-12-16 04:00:12 +00:00
|
|
|
#[inline]
|
2017-12-14 17:43:42 +00:00
|
|
|
fn respond_to(self, _: HttpRequest) -> Result<Reply, Error> {
|
2017-12-03 22:22:04 +00:00
|
|
|
Ok(Reply(ReplyItem::Future(self)))
|
2017-11-30 23:13:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-29 21:26:55 +00:00
|
|
|
/// Trait defines object that could be regestered as resource route
|
|
|
|
pub(crate) trait RouteHandler<S>: 'static {
|
2017-11-30 23:13:56 +00:00
|
|
|
fn handle(&self, req: HttpRequest<S>) -> Reply;
|
2017-11-29 21:26:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Route handler wrapper for Handler
|
|
|
|
pub(crate)
|
|
|
|
struct WrapHandler<S, H, R>
|
|
|
|
where H: Handler<S, Result=R>,
|
2017-12-14 17:43:42 +00:00
|
|
|
R: Responder,
|
2017-11-29 21:26:55 +00:00
|
|
|
S: 'static,
|
|
|
|
{
|
|
|
|
h: H,
|
|
|
|
s: PhantomData<S>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S, H, R> WrapHandler<S, H, R>
|
|
|
|
where H: Handler<S, Result=R>,
|
2017-12-14 17:43:42 +00:00
|
|
|
R: Responder,
|
2017-11-29 21:26:55 +00:00
|
|
|
S: 'static,
|
|
|
|
{
|
|
|
|
pub fn new(h: H) -> Self {
|
|
|
|
WrapHandler{h: h, s: PhantomData}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S, H, R> RouteHandler<S> for WrapHandler<S, H, R>
|
|
|
|
where H: Handler<S, Result=R>,
|
2017-12-14 17:43:42 +00:00
|
|
|
R: Responder + 'static,
|
2017-11-29 21:26:55 +00:00
|
|
|
S: 'static,
|
|
|
|
{
|
2017-11-30 23:13:56 +00:00
|
|
|
fn handle(&self, req: HttpRequest<S>) -> Reply {
|
2017-12-03 00:37:21 +00:00
|
|
|
let req2 = req.clone_without_state();
|
2017-12-14 17:43:42 +00:00
|
|
|
match self.h.handle(req).respond_to(req2) {
|
2017-12-03 22:22:04 +00:00
|
|
|
Ok(reply) => reply.into(),
|
|
|
|
Err(err) => Reply::response(err.into()),
|
|
|
|
}
|
2017-11-29 21:26:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Async route handler
|
|
|
|
pub(crate)
|
2017-12-02 05:58:19 +00:00
|
|
|
struct AsyncHandler<S, R, F>
|
2017-11-29 21:26:55 +00:00
|
|
|
where F: Fn(HttpRequest<S>) -> R + 'static,
|
2017-11-30 22:42:20 +00:00
|
|
|
R: Future<Item=HttpResponse, Error=Error> + 'static,
|
2017-11-29 21:26:55 +00:00
|
|
|
S: 'static,
|
|
|
|
{
|
|
|
|
f: Box<F>,
|
|
|
|
s: PhantomData<S>,
|
|
|
|
}
|
|
|
|
|
2017-12-02 05:58:19 +00:00
|
|
|
impl<S, R, F> AsyncHandler<S, R, F>
|
2017-11-29 21:26:55 +00:00
|
|
|
where F: Fn(HttpRequest<S>) -> R + 'static,
|
2017-11-30 22:42:20 +00:00
|
|
|
R: Future<Item=HttpResponse, Error=Error> + 'static,
|
2017-11-29 21:26:55 +00:00
|
|
|
S: 'static,
|
|
|
|
{
|
|
|
|
pub fn new(f: F) -> Self {
|
2017-12-02 05:58:19 +00:00
|
|
|
AsyncHandler{f: Box::new(f), s: PhantomData}
|
2017-11-29 21:26:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-02 05:58:19 +00:00
|
|
|
impl<S, R, F> RouteHandler<S> for AsyncHandler<S, R, F>
|
2017-11-29 21:26:55 +00:00
|
|
|
where F: Fn(HttpRequest<S>) -> R + 'static,
|
2017-11-30 22:42:20 +00:00
|
|
|
R: Future<Item=HttpResponse, Error=Error> + 'static,
|
2017-11-29 21:26:55 +00:00
|
|
|
S: 'static,
|
|
|
|
{
|
2017-11-30 23:13:56 +00:00
|
|
|
fn handle(&self, req: HttpRequest<S>) -> Reply {
|
|
|
|
Reply::async((self.f)(req))
|
2017-11-29 21:26:55 +00:00
|
|
|
}
|
|
|
|
}
|
2017-12-04 02:51:52 +00:00
|
|
|
|
|
|
|
|
2017-12-04 22:07:53 +00:00
|
|
|
/// Json response helper
|
|
|
|
///
|
|
|
|
/// The `Json` type allows you to respond with well-formed JSON data: simply return a value of
|
|
|
|
/// type Json<T> where T is the type of a structure to serialize into *JSON*. The
|
|
|
|
/// type `T` must implement the `Serialize` trait from *serde*.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # extern crate actix_web;
|
|
|
|
/// # #[macro_use] extern crate serde_derive;
|
|
|
|
/// # use actix_web::*;
|
|
|
|
/// #
|
|
|
|
/// #[derive(Serialize)]
|
|
|
|
/// struct MyObj {
|
|
|
|
/// name: String,
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn index(req: HttpRequest) -> Result<Json<MyObj>> {
|
|
|
|
/// Ok(Json(MyObj{name: req.match_info().query("name")?}))
|
|
|
|
/// }
|
|
|
|
/// # fn main() {}
|
|
|
|
/// ```
|
2017-12-04 02:51:52 +00:00
|
|
|
pub struct Json<T: Serialize> (pub T);
|
|
|
|
|
2017-12-14 17:43:42 +00:00
|
|
|
impl<T: Serialize> Responder for Json<T> {
|
2017-12-04 02:51:52 +00:00
|
|
|
type Item = HttpResponse;
|
|
|
|
type Error = Error;
|
|
|
|
|
2017-12-14 17:43:42 +00:00
|
|
|
fn respond_to(self, _: HttpRequest) -> Result<HttpResponse, Error> {
|
2017-12-04 02:51:52 +00:00
|
|
|
let body = serde_json::to_string(&self.0)?;
|
|
|
|
|
|
|
|
Ok(HttpResponse::Ok()
|
|
|
|
.content_type("application/json")
|
|
|
|
.body(body)?)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-09 19:55:55 +00:00
|
|
|
/// Path normalization helper
|
|
|
|
///
|
|
|
|
/// By normalizing it means:
|
2017-12-09 19:39:13 +00:00
|
|
|
///
|
|
|
|
/// - Add a trailing slash to the path.
|
|
|
|
/// - Double slashes are replaced by one.
|
|
|
|
///
|
|
|
|
/// The handler returns as soon as it finds a path that resolves
|
2017-12-09 19:55:55 +00:00
|
|
|
/// correctly. The order if all enable is 1) merge, 3) both merge and append
|
|
|
|
/// and 3) append. If the path resolves with
|
2017-12-09 19:39:13 +00:00
|
|
|
/// at least one of those conditions, it will redirect to the new path.
|
|
|
|
///
|
|
|
|
/// If *append* is *true* append slash when needed. If a resource is
|
|
|
|
/// defined with trailing slash and the request comes without it, it will
|
|
|
|
/// append it automatically.
|
|
|
|
///
|
|
|
|
/// If *merge* is *true*, merge multiple consecutive slashes in the path into one.
|
|
|
|
///
|
|
|
|
/// This handler designed to be use as a handler for application's *default resource*.
|
2017-12-09 19:55:55 +00:00
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # extern crate actix_web;
|
|
|
|
/// # #[macro_use] extern crate serde_derive;
|
|
|
|
/// # use actix_web::*;
|
|
|
|
/// #
|
|
|
|
/// # fn index(req: HttpRequest) -> httpcodes::StaticResponse {
|
|
|
|
/// # httpcodes::HTTPOk
|
|
|
|
/// # }
|
|
|
|
/// fn main() {
|
2017-12-11 22:16:29 +00:00
|
|
|
/// let app = Application::new()
|
2017-12-09 19:55:55 +00:00
|
|
|
/// .resource("/test/", |r| r.f(index))
|
|
|
|
/// .default_resource(|r| r.h(NormalizePath::default()))
|
|
|
|
/// .finish();
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
/// In this example `/test`, `/test///` will be redirected to `/test/` url.
|
2017-12-09 19:39:13 +00:00
|
|
|
pub struct NormalizePath {
|
|
|
|
append: bool,
|
|
|
|
merge: bool,
|
|
|
|
re_merge: Regex,
|
|
|
|
redirect: StatusCode,
|
|
|
|
not_found: StatusCode,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for NormalizePath {
|
2017-12-09 19:55:55 +00:00
|
|
|
/// Create default `NormalizePath` instance, *append* is set to *true*,
|
|
|
|
/// *merge* is set to *true* and *redirect* is set to `StatusCode::MOVED_PERMANENTLY`
|
2017-12-09 19:39:13 +00:00
|
|
|
fn default() -> NormalizePath {
|
|
|
|
NormalizePath {
|
|
|
|
append: true,
|
|
|
|
merge: true,
|
|
|
|
re_merge: Regex::new("//+").unwrap(),
|
|
|
|
redirect: StatusCode::MOVED_PERMANENTLY,
|
|
|
|
not_found: StatusCode::NOT_FOUND,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl NormalizePath {
|
2017-12-09 19:55:55 +00:00
|
|
|
/// Create new `NoramlizePath` instance
|
2017-12-09 19:39:13 +00:00
|
|
|
pub fn new(append: bool, merge: bool, redirect: StatusCode) -> NormalizePath {
|
|
|
|
NormalizePath {
|
|
|
|
append: append,
|
|
|
|
merge: merge,
|
|
|
|
re_merge: Regex::new("//+").unwrap(),
|
|
|
|
redirect: redirect,
|
|
|
|
not_found: StatusCode::NOT_FOUND,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S> Handler<S> for NormalizePath {
|
|
|
|
type Result = Result<HttpResponse, HttpError>;
|
|
|
|
|
|
|
|
fn handle(&self, req: HttpRequest<S>) -> Self::Result {
|
|
|
|
if let Some(router) = req.router() {
|
2017-12-09 21:25:06 +00:00
|
|
|
let query = req.query_string();
|
2017-12-09 19:39:13 +00:00
|
|
|
if self.merge {
|
|
|
|
// merge slashes
|
|
|
|
let p = self.re_merge.replace_all(req.path(), "/");
|
|
|
|
if p.len() != req.path().len() {
|
|
|
|
if router.has_route(p.as_ref()) {
|
2017-12-09 21:25:06 +00:00
|
|
|
let p = if !query.is_empty() { p + "?" + query } else { p };
|
2017-12-09 19:39:13 +00:00
|
|
|
return HttpResponse::build(self.redirect)
|
|
|
|
.header(header::LOCATION, p.as_ref())
|
|
|
|
.body(Body::Empty);
|
|
|
|
}
|
|
|
|
// merge slashes and append trailing slash
|
|
|
|
if self.append && !p.ends_with('/') {
|
|
|
|
let p = p.as_ref().to_owned() + "/";
|
|
|
|
if router.has_route(&p) {
|
2017-12-09 21:25:06 +00:00
|
|
|
let p = if !query.is_empty() { p + "?" + query } else { p };
|
2017-12-09 19:39:13 +00:00
|
|
|
return HttpResponse::build(self.redirect)
|
|
|
|
.header(header::LOCATION, p.as_str())
|
|
|
|
.body(Body::Empty);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// append trailing slash
|
|
|
|
if self.append && !req.path().ends_with('/') {
|
|
|
|
let p = req.path().to_owned() + "/";
|
|
|
|
if router.has_route(&p) {
|
2017-12-09 21:25:06 +00:00
|
|
|
let p = if !query.is_empty() { p + "?" + query } else { p };
|
2017-12-09 19:39:13 +00:00
|
|
|
return HttpResponse::build(self.redirect)
|
|
|
|
.header(header::LOCATION, p.as_str())
|
|
|
|
.body(Body::Empty);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(HttpResponse::new(self.not_found, Body::Empty))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-04 02:51:52 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2017-12-09 21:25:06 +00:00
|
|
|
use http::{header, Method};
|
|
|
|
use application::Application;
|
2017-12-04 02:51:52 +00:00
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
struct MyObj {
|
|
|
|
name: &'static str,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_json() {
|
|
|
|
let json = Json(MyObj{name: "test"});
|
2017-12-14 17:43:42 +00:00
|
|
|
let resp = json.respond_to(HttpRequest::default()).unwrap();
|
2017-12-04 02:51:52 +00:00
|
|
|
assert_eq!(resp.headers().get(header::CONTENT_TYPE).unwrap(), "application/json");
|
|
|
|
}
|
2017-12-09 21:25:06 +00:00
|
|
|
|
|
|
|
fn index(_req: HttpRequest) -> HttpResponse {
|
|
|
|
HttpResponse::new(StatusCode::OK, Body::Empty)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_normalize_path_trailing_slashes() {
|
2017-12-11 22:16:29 +00:00
|
|
|
let app = Application::new()
|
2017-12-09 21:25:06 +00:00
|
|
|
.resource("/resource1", |r| r.method(Method::GET).f(index))
|
|
|
|
.resource("/resource2/", |r| r.method(Method::GET).f(index))
|
|
|
|
.default_resource(|r| r.h(NormalizePath::default()))
|
|
|
|
.finish();
|
|
|
|
|
|
|
|
// trailing slashes
|
|
|
|
let params = vec![("/resource1", "", StatusCode::OK),
|
|
|
|
("/resource1/", "", StatusCode::NOT_FOUND),
|
|
|
|
("/resource2", "/resource2/", StatusCode::MOVED_PERMANENTLY),
|
|
|
|
("/resource2/", "", StatusCode::OK),
|
|
|
|
("/resource1?p1=1&p2=2", "", StatusCode::OK),
|
|
|
|
("/resource1/?p1=1&p2=2", "", StatusCode::NOT_FOUND),
|
|
|
|
("/resource2?p1=1&p2=2", "/resource2/?p1=1&p2=2",
|
|
|
|
StatusCode::MOVED_PERMANENTLY),
|
|
|
|
("/resource2/?p1=1&p2=2", "", StatusCode::OK)
|
|
|
|
];
|
|
|
|
for (path, target, code) in params {
|
|
|
|
let req = app.prepare_request(HttpRequest::from_path(path));
|
|
|
|
let resp = app.run(req);
|
|
|
|
let r = resp.as_response().unwrap();
|
|
|
|
assert_eq!(r.status(), code);
|
|
|
|
if !target.is_empty() {
|
|
|
|
assert_eq!(
|
|
|
|
target,
|
|
|
|
r.headers().get(header::LOCATION).unwrap().to_str().unwrap());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_normalize_path_trailing_slashes_disabled() {
|
2017-12-11 22:16:29 +00:00
|
|
|
let app = Application::new()
|
2017-12-09 21:25:06 +00:00
|
|
|
.resource("/resource1", |r| r.method(Method::GET).f(index))
|
|
|
|
.resource("/resource2/", |r| r.method(Method::GET).f(index))
|
|
|
|
.default_resource(|r| r.h(
|
|
|
|
NormalizePath::new(false, true, StatusCode::MOVED_PERMANENTLY)))
|
|
|
|
.finish();
|
|
|
|
|
|
|
|
// trailing slashes
|
|
|
|
let params = vec![("/resource1", StatusCode::OK),
|
|
|
|
("/resource1/", StatusCode::NOT_FOUND),
|
|
|
|
("/resource2", StatusCode::NOT_FOUND),
|
|
|
|
("/resource2/", StatusCode::OK),
|
|
|
|
("/resource1?p1=1&p2=2", StatusCode::OK),
|
|
|
|
("/resource1/?p1=1&p2=2", StatusCode::NOT_FOUND),
|
|
|
|
("/resource2?p1=1&p2=2", StatusCode::NOT_FOUND),
|
|
|
|
("/resource2/?p1=1&p2=2", StatusCode::OK)
|
|
|
|
];
|
|
|
|
for (path, code) in params {
|
|
|
|
let req = app.prepare_request(HttpRequest::from_path(path));
|
|
|
|
let resp = app.run(req);
|
|
|
|
let r = resp.as_response().unwrap();
|
|
|
|
assert_eq!(r.status(), code);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_normalize_path_merge_slashes() {
|
2017-12-11 22:16:29 +00:00
|
|
|
let app = Application::new()
|
2017-12-09 21:25:06 +00:00
|
|
|
.resource("/resource1", |r| r.method(Method::GET).f(index))
|
|
|
|
.resource("/resource1/a/b", |r| r.method(Method::GET).f(index))
|
|
|
|
.default_resource(|r| r.h(NormalizePath::default()))
|
|
|
|
.finish();
|
|
|
|
|
|
|
|
// trailing slashes
|
|
|
|
let params = vec![
|
|
|
|
("/resource1/a/b", "", StatusCode::OK),
|
|
|
|
("//resource1//a//b", "/resource1/a/b", StatusCode::MOVED_PERMANENTLY),
|
|
|
|
("//resource1//a//b/", "", StatusCode::NOT_FOUND),
|
|
|
|
("///resource1//a//b", "/resource1/a/b", StatusCode::MOVED_PERMANENTLY),
|
|
|
|
("/////resource1/a///b", "/resource1/a/b", StatusCode::MOVED_PERMANENTLY),
|
|
|
|
("/////resource1/a//b/", "", StatusCode::NOT_FOUND),
|
|
|
|
("/resource1/a/b?p=1", "", StatusCode::OK),
|
|
|
|
("//resource1//a//b?p=1", "/resource1/a/b?p=1", StatusCode::MOVED_PERMANENTLY),
|
|
|
|
("//resource1//a//b/?p=1", "", StatusCode::NOT_FOUND),
|
|
|
|
("///resource1//a//b?p=1", "/resource1/a/b?p=1", StatusCode::MOVED_PERMANENTLY),
|
|
|
|
("/////resource1/a///b?p=1", "/resource1/a/b?p=1", StatusCode::MOVED_PERMANENTLY),
|
|
|
|
("/////resource1/a//b/?p=1", "", StatusCode::NOT_FOUND),
|
|
|
|
];
|
|
|
|
for (path, target, code) in params {
|
|
|
|
let req = app.prepare_request(HttpRequest::from_path(path));
|
|
|
|
let resp = app.run(req);
|
|
|
|
let r = resp.as_response().unwrap();
|
|
|
|
assert_eq!(r.status(), code);
|
|
|
|
if !target.is_empty() {
|
|
|
|
assert_eq!(
|
|
|
|
target,
|
|
|
|
r.headers().get(header::LOCATION).unwrap().to_str().unwrap());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_normalize_path_merge_and_append_slashes() {
|
2017-12-11 22:16:29 +00:00
|
|
|
let app = Application::new()
|
2017-12-09 21:25:06 +00:00
|
|
|
.resource("/resource1", |r| r.method(Method::GET).f(index))
|
|
|
|
.resource("/resource2/", |r| r.method(Method::GET).f(index))
|
|
|
|
.resource("/resource1/a/b", |r| r.method(Method::GET).f(index))
|
|
|
|
.resource("/resource2/a/b/", |r| r.method(Method::GET).f(index))
|
|
|
|
.default_resource(|r| r.h(NormalizePath::default()))
|
|
|
|
.finish();
|
|
|
|
|
|
|
|
// trailing slashes
|
|
|
|
let params = vec![
|
|
|
|
("/resource1/a/b", "", StatusCode::OK),
|
|
|
|
("/resource1/a/b/", "", StatusCode::NOT_FOUND),
|
|
|
|
("//resource2//a//b", "/resource2/a/b/", StatusCode::MOVED_PERMANENTLY),
|
|
|
|
("//resource2//a//b/", "/resource2/a/b/", StatusCode::MOVED_PERMANENTLY),
|
|
|
|
("///resource1//a//b", "/resource1/a/b", StatusCode::MOVED_PERMANENTLY),
|
|
|
|
("///resource1//a//b/", "", StatusCode::NOT_FOUND),
|
|
|
|
("/////resource1/a///b", "/resource1/a/b", StatusCode::MOVED_PERMANENTLY),
|
|
|
|
("/////resource1/a///b/", "", StatusCode::NOT_FOUND),
|
|
|
|
("/resource2/a/b", "/resource2/a/b/", StatusCode::MOVED_PERMANENTLY),
|
|
|
|
("/resource2/a/b/", "", StatusCode::OK),
|
|
|
|
("//resource2//a//b", "/resource2/a/b/", StatusCode::MOVED_PERMANENTLY),
|
|
|
|
("//resource2//a//b/", "/resource2/a/b/", StatusCode::MOVED_PERMANENTLY),
|
|
|
|
("///resource2//a//b", "/resource2/a/b/", StatusCode::MOVED_PERMANENTLY),
|
|
|
|
("///resource2//a//b/", "/resource2/a/b/", StatusCode::MOVED_PERMANENTLY),
|
|
|
|
("/////resource2/a///b", "/resource2/a/b/", StatusCode::MOVED_PERMANENTLY),
|
|
|
|
("/////resource2/a///b/", "/resource2/a/b/", StatusCode::MOVED_PERMANENTLY),
|
|
|
|
("/resource1/a/b?p=1", "", StatusCode::OK),
|
|
|
|
("/resource1/a/b/?p=1", "", StatusCode::NOT_FOUND),
|
|
|
|
("//resource2//a//b?p=1", "/resource2/a/b/?p=1", StatusCode::MOVED_PERMANENTLY),
|
|
|
|
("//resource2//a//b/?p=1", "/resource2/a/b/?p=1", StatusCode::MOVED_PERMANENTLY),
|
|
|
|
("///resource1//a//b?p=1", "/resource1/a/b?p=1", StatusCode::MOVED_PERMANENTLY),
|
|
|
|
("///resource1//a//b/?p=1", "", StatusCode::NOT_FOUND),
|
|
|
|
("/////resource1/a///b?p=1", "/resource1/a/b?p=1", StatusCode::MOVED_PERMANENTLY),
|
|
|
|
("/////resource1/a///b/?p=1", "", StatusCode::NOT_FOUND),
|
|
|
|
("/resource2/a/b?p=1", "/resource2/a/b/?p=1", StatusCode::MOVED_PERMANENTLY),
|
|
|
|
("//resource2//a//b?p=1", "/resource2/a/b/?p=1", StatusCode::MOVED_PERMANENTLY),
|
|
|
|
("//resource2//a//b/?p=1", "/resource2/a/b/?p=1", StatusCode::MOVED_PERMANENTLY),
|
|
|
|
("///resource2//a//b?p=1", "/resource2/a/b/?p=1", StatusCode::MOVED_PERMANENTLY),
|
|
|
|
("///resource2//a//b/?p=1", "/resource2/a/b/?p=1", StatusCode::MOVED_PERMANENTLY),
|
|
|
|
("/////resource2/a///b?p=1", "/resource2/a/b/?p=1", StatusCode::MOVED_PERMANENTLY),
|
|
|
|
("/////resource2/a///b/?p=1", "/resource2/a/b/?p=1", StatusCode::MOVED_PERMANENTLY),
|
|
|
|
];
|
|
|
|
for (path, target, code) in params {
|
|
|
|
let req = app.prepare_request(HttpRequest::from_path(path));
|
|
|
|
let resp = app.run(req);
|
|
|
|
let r = resp.as_response().unwrap();
|
|
|
|
assert_eq!(r.status(), code);
|
|
|
|
if !target.is_empty() {
|
|
|
|
assert_eq!(
|
|
|
|
target, r.headers().get(header::LOCATION).unwrap().to_str().unwrap());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-04 02:51:52 +00:00
|
|
|
}
|