diff --git a/src/application.rs b/src/application.rs index 80ba7f52..ebf441ec 100644 --- a/src/application.rs +++ b/src/application.rs @@ -58,10 +58,14 @@ impl HttpHandler for HttpApplication { fn handle(&self, msg: Request) -> Result>, Request> { let m = { - let path = msg.path(); - path.starts_with(&self.prefix) - && (path.len() == self.prefix_len - || path.split_at(self.prefix_len).1.starts_with('/')) + if self.prefix_len == 0 { + true + } else { + let path = msg.path(); + path.starts_with(&self.prefix) + && (path.len() == self.prefix_len + || path.split_at(self.prefix_len).1.starts_with('/')) + } }; if m { if let Some(ref filters) = self.filters { @@ -135,7 +139,7 @@ where App { parts: Some(ApplicationParts { state, - prefix: "/".to_owned(), + prefix: "".to_owned(), router: Router::new(), middlewares: Vec::new(), filters: Vec::new(), @@ -498,12 +502,6 @@ where pub fn finish(&mut self) -> HttpApplication { let mut 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()) - }; - parts.router.finish(); let inner = Rc::new(Inner { @@ -517,12 +515,12 @@ where }; HttpApplication { - state: Rc::new(parts.state), - middlewares: Rc::new(parts.middlewares), - prefix, - prefix_len, inner, filters, + state: Rc::new(parts.state), + middlewares: Rc::new(parts.middlewares), + prefix: prefix.to_owned(), + prefix_len: prefix.len(), } } diff --git a/src/router.rs b/src/router.rs index d93ec9eb..603bc560 100644 --- a/src/router.rs +++ b/src/router.rs @@ -357,18 +357,11 @@ impl Router { /// Query for matched resource pub fn recognize(&self, req: &Request, state: &S, tail: usize) -> ResourceInfo { - self.match_with_params(req, state, tail, true) - } - - /// Query for matched resource - pub(crate) fn match_with_params( - &self, req: &Request, state: &S, tail: usize, insert: bool, - ) -> ResourceInfo { if tail <= req.path().len() { 'outer: for (idx, resource) in self.patterns.iter().enumerate() { match resource { ResourcePattern::Resource(rdef) => { - if let Some(params) = rdef.match_with_params(req, tail, insert) { + if let Some(params) = rdef.match_with_params(req, tail) { return self.route_info_params(idx as u16, params); } } @@ -528,19 +521,8 @@ impl ResourceDef { } /// Are the given path and parameters a match against this resource? - pub fn match_with_params( - &self, req: &Request, plen: usize, insert: bool, - ) -> Option { + pub fn match_with_params(&self, req: &Request, plen: usize) -> Option { let path = &req.path()[plen..]; - if insert { - if path.is_empty() { - "/" - } else { - path - } - } else { - path - }; match self.tp { PatternType::Static(ref s) => if s != path { @@ -942,11 +924,11 @@ mod tests { assert!(!re.is_match("/user/2345/sdg")); let req = TestRequest::with_uri("/user/profile").finish(); - let info = re.match_with_params(&req, 0, true).unwrap(); + let info = re.match_with_params(&req, 0).unwrap(); assert_eq!(info.get("id").unwrap(), "profile"); let req = TestRequest::with_uri("/user/1245125").finish(); - let info = re.match_with_params(&req, 0, true).unwrap(); + let info = re.match_with_params(&req, 0).unwrap(); assert_eq!(info.get("id").unwrap(), "1245125"); let re = ResourceDef::new("/v{version}/resource/{id}"); @@ -955,7 +937,7 @@ mod tests { assert!(!re.is_match("/resource")); let req = TestRequest::with_uri("/v151/resource/adahg32").finish(); - let info = re.match_with_params(&req, 0, true).unwrap(); + let info = re.match_with_params(&req, 0).unwrap(); assert_eq!(info.get("version").unwrap(), "151"); assert_eq!(info.get("id").unwrap(), "adahg32"); } @@ -983,12 +965,12 @@ mod tests { assert!(!re.is_match("/name")); let req = TestRequest::with_uri("/test2/").finish(); - let info = re.match_with_params(&req, 0, true).unwrap(); + let info = re.match_with_params(&req, 0).unwrap(); assert_eq!(&info["name"], "test2"); assert_eq!(&info[0], "test2"); let req = TestRequest::with_uri("/test2/subpath1/subpath2/index.html").finish(); - let info = re.match_with_params(&req, 0, true).unwrap(); + let info = re.match_with_params(&req, 0).unwrap(); assert_eq!(&info["name"], "test2"); assert_eq!(&info[0], "test2"); } diff --git a/src/scope.rs b/src/scope.rs index 94dbd860..d9502c94 100644 --- a/src/scope.rs +++ b/src/scope.rs @@ -304,7 +304,7 @@ impl RouteHandler for Scope { let tail = req.match_info().tail as usize; // recognize resources - let info = self.router.match_with_params(req, req.state(), tail, false); + let info = self.router.recognize(req, req.state(), tail); let req2 = req.with_route_info(info); if self.middlewares.is_empty() { self.router.handle(&req2)