Skip to content

Commit

Permalink
fix prefix and static file serving #168
Browse files Browse the repository at this point in the history
  • Loading branch information
fafhrd91 committed Apr 10, 2018
1 parent bb11fb3 commit 81ac905
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 10 deletions.
8 changes: 5 additions & 3 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,18 @@

* Added `ErrorHandlers` middleware

* Router cannot parse Non-ASCII characters in URL #137
* Fix router cannot parse Non-ASCII characters in URL #137

* Fix client connection pooling

* Fix long client urls #129

* Fix panic on invalid URL characters #130

* Fix client connection pooling

* Fix logger request duration calculation #152

* Fix prefix and static file serving #168

* Add `signed` and `private` `CookieSessionBackend`s


Expand Down
79 changes: 73 additions & 6 deletions src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub type Application<S> = App<S>;
pub struct HttpApplication<S=()> {
state: Rc<S>,
prefix: String,
prefix_len: usize,
router: Router,
inner: Rc<UnsafeCell<Inner<S>>>,
middlewares: Rc<Vec<Box<Middleware<S>>>>,
Expand Down Expand Up @@ -73,6 +74,7 @@ impl<S: 'static> HttpApplication<S> {
path.len() == prefix.len() ||
path.split_at(prefix.len()).1.starts_with('/'))
};

if m {
let path: &'static str = unsafe {
mem::transmute(&req.path()[inner.prefix+prefix.len()..]) };
Expand Down Expand Up @@ -106,8 +108,8 @@ impl<S: 'static> HttpHandler for HttpApplication<S> {
let m = {
let path = req.path();
path.starts_with(&self.prefix) && (
path.len() == self.prefix.len() ||
path.split_at(self.prefix.len()).1.starts_with('/'))
path.len() == self.prefix_len ||
path.split_at(self.prefix_len).1.starts_with('/'))
};
if m {
let mut req = req.with_state(Rc::clone(&self.state), self.router.clone());
Expand Down Expand Up @@ -420,8 +422,12 @@ impl<S> App<S> where S: 'static {
pub fn handler<H: Handler<S>>(mut self, path: &str, handler: H) -> App<S>
{
{
let path = path.trim().trim_right_matches('/').to_owned();
let mut path = path.trim().trim_right_matches('/').to_owned();
if !path.is_empty() && !path.starts_with('/') {
path.insert(0, '/')
}
let parts = self.parts.as_mut().expect("Use after finish");

parts.handlers.push((path, Box::new(WrapHandler::new(handler))));
}
self
Expand Down Expand Up @@ -471,17 +477,22 @@ impl<S> App<S> where S: 'static {
pub fn finish(&mut self) -> HttpApplication<S> {
let parts = self.parts.take().expect("Use after finish");
let prefix = parts.prefix.trim().trim_right_matches('/');
let (prefix, prefix_len) = if prefix.is_empty() {
("/".to_owned(), 0)
} else {
(prefix.to_owned(), prefix.len())
};

let mut resources = parts.resources;
for (_, pattern) in parts.external {
resources.push((pattern, None));
}

let (router, resources) = Router::new(prefix, parts.settings, resources);
let (router, resources) = Router::new(&prefix, parts.settings, resources);

let inner = Rc::new(UnsafeCell::new(
Inner {
prefix: prefix.len(),
prefix: prefix_len,
default: parts.default,
encoding: parts.encoding,
handlers: parts.handlers,
Expand All @@ -491,9 +502,10 @@ impl<S> App<S> where S: 'static {

HttpApplication {
state: Rc::new(parts.state),
prefix: prefix.to_owned(),
router: router.clone(),
middlewares: Rc::new(parts.middlewares),
prefix,
prefix_len,
inner,
}
}
Expand Down Expand Up @@ -670,6 +682,61 @@ mod tests {
assert_eq!(resp.as_response().unwrap().status(), StatusCode::NOT_FOUND);
}

#[test]
fn test_handler2() {
let mut app = App::new()
.handler("test", |_| HttpResponse::Ok())
.finish();

let req = TestRequest::with_uri("/test").finish();
let resp = app.run(req);
assert_eq!(resp.as_response().unwrap().status(), StatusCode::OK);

let req = TestRequest::with_uri("/test/").finish();
let resp = app.run(req);
assert_eq!(resp.as_response().unwrap().status(), StatusCode::OK);

let req = TestRequest::with_uri("/test/app").finish();
let resp = app.run(req);
assert_eq!(resp.as_response().unwrap().status(), StatusCode::OK);

let req = TestRequest::with_uri("/testapp").finish();
let resp = app.run(req);
assert_eq!(resp.as_response().unwrap().status(), StatusCode::NOT_FOUND);

let req = TestRequest::with_uri("/blah").finish();
let resp = app.run(req);
assert_eq!(resp.as_response().unwrap().status(), StatusCode::NOT_FOUND);
}

#[test]
fn test_handler_with_prefix() {
let mut app = App::new()
.prefix("prefix")
.handler("/test", |_| HttpResponse::Ok())
.finish();

let req = TestRequest::with_uri("/prefix/test").finish();
let resp = app.run(req);
assert_eq!(resp.as_response().unwrap().status(), StatusCode::OK);

let req = TestRequest::with_uri("/prefix/test/").finish();
let resp = app.run(req);
assert_eq!(resp.as_response().unwrap().status(), StatusCode::OK);

let req = TestRequest::with_uri("/prefix/test/app").finish();
let resp = app.run(req);
assert_eq!(resp.as_response().unwrap().status(), StatusCode::OK);

let req = TestRequest::with_uri("/prefix/testapp").finish();
let resp = app.run(req);
assert_eq!(resp.as_response().unwrap().status(), StatusCode::NOT_FOUND);

let req = TestRequest::with_uri("/prefix/blah").finish();
let resp = app.run(req);
assert_eq!(resp.as_response().unwrap().status(), StatusCode::NOT_FOUND);
}

#[test]
fn test_route() {
let mut app = App::new()
Expand Down
45 changes: 44 additions & 1 deletion src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,9 @@ impl<S: 'static> Handler<S> for StaticFiles<S> {
new_path.push_str(&el.to_string_lossy());
new_path.push('/');
}
if !new_path.ends_with('/') {
new_path.push('/');
}
new_path.push_str(redir_index);
HttpResponse::Found()
.header(header::LOCATION, new_path.as_str())
Expand All @@ -500,7 +503,8 @@ impl<S: 'static> Handler<S> for StaticFiles<S> {
#[cfg(test)]
mod tests {
use super::*;
use test::TestRequest;
use application::App;
use test::{self, TestRequest};
use http::{header, Method, StatusCode};

#[test]
Expand Down Expand Up @@ -603,4 +607,43 @@ mod tests {
assert_eq!(resp.status(), StatusCode::FOUND);
assert_eq!(resp.headers().get(header::LOCATION).unwrap(), "/examples/basics/Cargo.toml");
}

#[test]
fn integration_redirect_to_index_with_prefix() {
let mut srv = test::TestServer::with_factory(
|| App::new()
.prefix("public")
.handler("/", StaticFiles::new(".").index_file("Cargo.toml")));

let request = srv.get().uri(srv.url("/public")).finish().unwrap();
let response = srv.execute(request.send()).unwrap();
assert_eq!(response.status(), StatusCode::FOUND);
let loc = response.headers().get(header::LOCATION).unwrap().to_str().unwrap();
assert_eq!(loc, "/public/Cargo.toml");

let request = srv.get().uri(srv.url("/public/")).finish().unwrap();
let response = srv.execute(request.send()).unwrap();
assert_eq!(response.status(), StatusCode::FOUND);
let loc = response.headers().get(header::LOCATION).unwrap().to_str().unwrap();
assert_eq!(loc, "/public/Cargo.toml");
}

#[test]
fn integration_redirect_to_index() {
let mut srv = test::TestServer::with_factory(
|| App::new()
.handler("test", StaticFiles::new(".").index_file("Cargo.toml")));

let request = srv.get().uri(srv.url("/test")).finish().unwrap();
let response = srv.execute(request.send()).unwrap();
assert_eq!(response.status(), StatusCode::FOUND);
let loc = response.headers().get(header::LOCATION).unwrap().to_str().unwrap();
assert_eq!(loc, "/test/Cargo.toml");

let request = srv.get().uri(srv.url("/test/")).finish().unwrap();
let response = srv.execute(request.send()).unwrap();
assert_eq!(response.status(), StatusCode::FOUND);
let loc = response.headers().get(header::LOCATION).unwrap().to_str().unwrap();
assert_eq!(loc, "/test/Cargo.toml");
}
}

0 comments on commit 81ac905

Please sign in to comment.