1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-30 18:34:36 +01:00

simplify application prefix impl

This commit is contained in:
Nikolay Kim 2018-07-15 15:50:56 +06:00
parent 9570c1cccd
commit b759dddf5a
3 changed files with 21 additions and 41 deletions

View File

@ -58,10 +58,14 @@ impl<S: 'static> HttpHandler for HttpApplication<S> {
fn handle(&self, msg: Request) -> Result<Pipeline<S, Inner<S>>, 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<S> {
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(),
}
}

View File

@ -357,18 +357,11 @@ impl<S: 'static> Router<S> {
/// 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<Params> {
pub fn match_with_params(&self, req: &Request, plen: usize) -> Option<Params> {
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");
}

View File

@ -304,7 +304,7 @@ impl<S: 'static> RouteHandler<S> for Scope<S> {
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)