Skip to content

Commit

Permalink
More fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonTeixidor committed Aug 25, 2015
1 parent 9169c1d commit 3e7bda5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
6 changes: 3 additions & 3 deletions examples/mount.rs
Expand Up @@ -9,16 +9,16 @@ fn main() {
* Requests to /t should not end up in this route. Expected output for
* a request to /test/a is /a.
*/
server.mount("/test/", middleware! { |req|
server.mount("/test/".to_owned(), middleware! { |req|
format!("Got request with uri={}", req.origin.uri)
});

/*
* Fall-through behaviour, if StaticFilesHandler does not find a matching file,
* the request uri must be reset so that it can be matched against other middleware.
*/
server.mount("/static/files/", StaticFilesHandler::new("examples/assets/"));
server.mount("/static/files/a/", middleware! {
server.mount("/static/files/".to_owned(), StaticFilesHandler::new("examples/assets/"));
server.mount("/static/files/a/".to_owned(), middleware! {
"No static file called a!"
});

Expand Down
26 changes: 14 additions & 12 deletions src/mount.rs
Expand Up @@ -8,7 +8,7 @@ use hyper::uri::RequestUri::AbsolutePath;
use std::mem;

pub trait Mountable {
fn mount<M: Middleware>(&mut self, mount_point: &'static str, middleware: M);
fn mount<M: Middleware>(&mut self, mount_point: String, middleware: M);
}

impl Mountable for Nickel {
Expand All @@ -22,15 +22,18 @@ impl Mountable for Nickel {
/// use nickel::{Nickel, StaticFilesHandler, Mountable};
/// let mut server = Nickel::new();
///
/// server.mount("/static_files/", StaticFilesHandler::new("/path/to/serve/"));
/// server.mount("/static_files/".to_owned(), StaticFilesHandler::new("/path/to/serve/"));
/// ```
fn mount<M: Middleware>(&mut self, mount_point: &'static str, middleware: M) {
///
/// # Panics
/// Panics if mount_point does not have a leading and trailing slash.
fn mount<M: Middleware>(&mut self, mount_point: String, middleware: M) {
self.utilize(Mount::new(mount_point, middleware));
}
}

pub struct Mount<M: Middleware> {
mount_point: &'static str,
mount_point: String,
middleware: M
}

Expand All @@ -50,18 +53,17 @@ impl<M: Middleware> Mount<M> {
/// let mut server = Nickel::new();
///
/// server.utilize(
/// Mount::new("/static_files/",
/// Mount::new("/static_files/".to_owned(),
/// StaticFilesHandler::new("/path/to/serve/")
/// ));
/// ```
pub fn new(mount_point: &'static str, middleware: M) -> Mount<M> {
///
/// # Panics
/// Panics if mount_point does not have a leading and trailing slash.
pub fn new(mount_point: String, middleware: M) -> Mount<M> {
match (mount_point.chars().last(), mount_point.chars().nth(0)) {
(Some('/'), Some('/')) =>
Mount {
/*
* Ensure that the mount_point string begins with a / but does not end
* with one.
*/
mount_point: mount_point,
middleware: middleware
},
Expand All @@ -74,8 +76,8 @@ impl<M: Middleware> Middleware for Mount<M> {
fn invoke<'mw, 'conn>(&'mw self, req: &mut Request<'mw, 'conn>, res: Response<'mw>)
-> MiddlewareResult<'mw> {
let subpath = match req.origin.uri {
AbsolutePath(ref path) if path.starts_with(self.mount_point) => {
AbsolutePath("/".chars().chain(path[self.mount_point.len()..].chars()).collect())
AbsolutePath(ref path) if path.starts_with(&*self.mount_point) => {
AbsolutePath(format!("/{}", &path[self.mount_point.len()..]))
},
_ => return Ok(Continue(res))
};
Expand Down

0 comments on commit 3e7bda5

Please sign in to comment.