From d361212bc5f523dfc1062a0d38b594fa78fead7e Mon Sep 17 00:00:00 2001 From: Andrew Hobden Date: Fri, 22 Jul 2016 19:10:22 -0700 Subject: [PATCH] (feat) Make compatible with Iron 0.4. --- Cargo.toml | 4 ++-- src/router.rs | 25 +++++++++++++++++-------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1cfb8b7..30e3f83 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ name = "router" authors = ["Jonathan Reem "] 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" @@ -11,4 +11,4 @@ keywords = ["iron", "web", "http", "routing", "router"] [dependencies] route-recognizer = "0.1" -iron = "0.3" +iron = "0.4" diff --git a/src/router.rs b/src/router.rs index ca46b48..17cdd35 100644 --- a/src/router.rs +++ b/src/router.rs @@ -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}; @@ -137,16 +138,24 @@ impl Router { // Tests for a match by adding or removing a trailing slash. fn redirect_slash(&self, req : &Request) -> Option { 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( @@ -167,7 +176,7 @@ impl Key for Router { type Value = Params; } impl Handler for Router { fn handle(&self, req: &mut Request) -> IronResult { - let path = req.url.path.join("/"); + let path = req.url.path().join("/"); self.handle_method(req, &path).unwrap_or_else(|| match req.method {