diff --git a/src/httprequest.rs b/src/httprequest.rs index 91ee9eb13..02edcae9a 100644 --- a/src/httprequest.rs +++ b/src/httprequest.rs @@ -462,14 +462,16 @@ mod tests { let mut resource = Resource::new(ResourceDef::new("/user/{name}.html")); resource.name("index"); let mut router = Router::<()>::new(); - router.set_prefix("/prefix/"); router.register_resource(resource); - let info = router.default_route_info(); + let mut info = router.default_route_info(); + info.set_prefix(7); assert!(info.has_route("/user/test.html")); assert!(!info.has_route("/prefix/user/test.html")); - let req = TestRequest::with_header(header::HOST, "www.rust-lang.org") + let req = TestRequest::with_uri("/prefix/test") + .prefix(7) + .header(header::HOST, "www.rust-lang.org") .finish_with_router(router); let url = req.url_for("index", &["test"]); assert_eq!( @@ -483,14 +485,15 @@ mod tests { let mut resource = Resource::new(ResourceDef::new("/index.html")); resource.name("index"); let mut router = Router::<()>::new(); - router.set_prefix("/prefix/"); router.register_resource(resource); - let info = router.default_route_info(); + let mut info = router.default_route_info(); + info.set_prefix(7); assert!(info.has_route("/index.html")); assert!(!info.has_route("/prefix/index.html")); - let req = TestRequest::default() + let req = TestRequest::with_uri("/prefix/test") + .prefix(7) .header(header::HOST, "www.rust-lang.org") .finish_with_router(router); let url = req.url_for_static("index"); diff --git a/src/router.rs b/src/router.rs index 603bc5608..468cc236f 100644 --- a/src/router.rs +++ b/src/router.rs @@ -49,6 +49,7 @@ pub struct ResourceInfo { router: Rc, resource: ResourceId, params: Params, + prefix: u16, } impl ResourceInfo { @@ -72,6 +73,10 @@ impl ResourceInfo { } } + pub(crate) fn set_prefix(&mut self, prefix: u16) { + self.prefix = prefix; + } + /// Get a reference to the Params object. /// /// Params is a container for url parameters. @@ -91,6 +96,7 @@ impl ResourceInfo { p.add(item.0.clone(), item.1.clone()); } + self.prefix = info.params.tail; self.params = p; } @@ -106,7 +112,8 @@ impl ResourceInfo { I: AsRef, { if let Some(pattern) = self.router.named.get(name) { - let path = pattern.resource_path(elements, &self.router.prefix)?; + let path = + pattern.resource_path(elements, &req.path()[..(self.prefix as usize)])?; if path.starts_with('/') { let conn = req.connection_info(); Ok(Url::parse(&format!( @@ -142,8 +149,6 @@ impl ResourceInfo { } struct Inner { - prefix: String, - prefix_len: usize, named: HashMap, patterns: Vec, } @@ -158,8 +163,6 @@ impl Router { pub(crate) fn new() -> Self { Router { defs: Rc::new(Inner { - prefix: String::new(), - prefix_len: 0, named: HashMap::new(), patterns: Vec::new(), }), @@ -169,25 +172,11 @@ impl Router { } } - /// Router prefix - #[inline] - pub fn prefix(&self) -> &str { - &self.defs.prefix - } - - /// Set router prefix - #[inline] - pub fn set_prefix(&mut self, prefix: &str) { - let prefix = prefix.trim().trim_right_matches('/').to_owned(); - let inner = Rc::get_mut(&mut self.defs).unwrap(); - inner.prefix_len = prefix.len(); - inner.prefix = prefix; - } - #[inline] pub(crate) fn route_info_params(&self, idx: u16, params: Params) -> ResourceInfo { ResourceInfo { params, + prefix: 0, router: self.defs.clone(), resource: ResourceId::Normal(idx), } @@ -200,6 +189,7 @@ impl Router { ResourceInfo { params, + prefix: 0, router: self.defs.clone(), resource: ResourceId::Default, } @@ -211,6 +201,7 @@ impl Router { params: Params::new(), router: self.defs.clone(), resource: ResourceId::Default, + prefix: 0, } } @@ -391,6 +382,7 @@ impl Router { } } ResourceInfo { + prefix: tail as u16, params: Params::new(), router: self.defs.clone(), resource: ResourceId::Default, @@ -851,7 +843,6 @@ mod tests { #[test] fn test_recognizer_with_prefix() { let mut router = Router::<()>::new(); - router.set_prefix("/test"); router.register_resource(Resource::new(ResourceDef::new("/name"))); router.register_resource(Resource::new(ResourceDef::new("/name/{val}"))); @@ -871,7 +862,6 @@ mod tests { // same patterns let mut router = Router::<()>::new(); - router.set_prefix("/test2"); router.register_resource(Resource::new(ResourceDef::new("/name"))); router.register_resource(Resource::new(ResourceDef::new("/name/{val}"))); diff --git a/src/server/h1.rs b/src/server/h1.rs index 5b83dcc08..511b32bce 100644 --- a/src/server/h1.rs +++ b/src/server/h1.rs @@ -543,11 +543,6 @@ mod tests { err: None, } } - fn feed_data(&mut self, data: &'static str) { - let mut b = BytesMut::from(self.buf.as_ref()); - b.extend(data.as_bytes()); - self.buf = b.take().freeze(); - } } impl AsyncRead for Buffer {} diff --git a/src/test.rs b/src/test.rs index 909d15f34..c2e5c7569 100644 --- a/src/test.rs +++ b/src/test.rs @@ -417,6 +417,7 @@ pub struct TestRequest { params: Params, cookies: Option>>, payload: Option, + prefix: u16, } impl Default for TestRequest<()> { @@ -430,6 +431,7 @@ impl Default for TestRequest<()> { params: Params::new(), cookies: None, payload: None, + prefix: 0, } } } @@ -467,6 +469,7 @@ impl TestRequest { params: Params::new(), cookies: None, payload: None, + prefix: 0, } } @@ -527,6 +530,12 @@ impl TestRequest { self } + /// Set request's prefix + pub fn prefix(mut self, prefix: u16) -> Self { + self.prefix = prefix; + self + } + /// Complete request creation and generate `HttpRequest` instance pub fn finish(self) -> HttpRequest { let TestRequest { @@ -538,6 +547,7 @@ impl TestRequest { mut params, cookies, payload, + prefix, } = self; let router = Router::<()>::new(); @@ -552,9 +562,10 @@ impl TestRequest { *inner.payload.borrow_mut() = payload; } params.set_url(req.url().clone()); + let mut info = router.route_info_params(0, params); + info.set_prefix(prefix); - let mut req = - HttpRequest::new(req, Rc::new(state), router.route_info_params(0, params)); + let mut req = HttpRequest::new(req, Rc::new(state), info); req.set_cookies(cookies); req } @@ -571,6 +582,7 @@ impl TestRequest { mut params, cookies, payload, + prefix, } = self; let pool = RequestPool::pool(ServerSettings::default()); @@ -584,8 +596,9 @@ impl TestRequest { *inner.payload.borrow_mut() = payload; } params.set_url(req.url().clone()); - let mut req = - HttpRequest::new(req, Rc::new(state), router.route_info_params(0, params)); + let mut info = router.route_info_params(0, params); + info.set_prefix(prefix); + let mut req = HttpRequest::new(req, Rc::new(state), info); req.set_cookies(cookies); req }