Skip to content

Commit

Permalink
Merge pull request iron#123 from Hoverbear/feat/update
Browse files Browse the repository at this point in the history
(feat) Make compatible with Iron 0.4.
  • Loading branch information
Andrew Hobden authored Jul 26, 2016
2 parents 63ff7c1 + d361212 commit 55024c0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
name = "router"
authors = ["Jonathan Reem <[email protected]>"]
license = "MIT"
version = "0.1.1"
version = "0.2.0"
description = "A router for the Iron framework."
repository = "https://github.com/iron/router"
documentation = "http://ironframework.io/doc/router/index.html"
keywords = ["iron", "web", "http", "routing", "router"]

[dependencies]
route-recognizer = "0.1"
iron = "0.3"
iron = "0.4"
25 changes: 17 additions & 8 deletions src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use iron::{Request, Response, Handler, IronResult, IronError};
use iron::{status, method, headers};
use iron::typemap::Key;
use iron::modifiers::Redirect;
use iron::Url;

use recognizer::Router as Recognizer;
use recognizer::{Match, Params};
Expand Down Expand Up @@ -137,16 +138,24 @@ impl Router {
// Tests for a match by adding or removing a trailing slash.
fn redirect_slash(&self, req : &Request) -> Option<IronError> {
let mut url = req.url.clone();
let mut path = url.path.join("/");
let mut path = url.path().join("/");

if let Some(last_char) = path.chars().last() {
if last_char == '/' {
path.pop();
url.path.pop();
} else {
path.push('/');
url.path.push(String::new());
// Unwrap generic URL to get access to its path components.
let mut generic_url = url.into_generic_url();
{
let mut path_segments = generic_url.path_segments_mut().unwrap();
if last_char == '/' {
// We didn't recognize anything without a trailing slash; try again with one appended.
path.pop();
path_segments.pop();
} else {
// We didn't recognize anything with a trailing slash; try again without it.
path.push('/');
path_segments.push("");
}
}
url = Url::from_generic_url(generic_url).unwrap();
}

self.recognize(&req.method, &path).and(
Expand All @@ -167,7 +176,7 @@ impl Key for Router { type Value = Params; }

impl Handler for Router {
fn handle(&self, req: &mut Request) -> IronResult<Response> {
let path = req.url.path.join("/");
let path = req.url.path().join("/");

self.handle_method(req, &path).unwrap_or_else(||
match req.method {
Expand Down

0 comments on commit 55024c0

Please sign in to comment.