mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-28 01:32:57 +01:00
simplify application prefix impl
This commit is contained in:
parent
9570c1cccd
commit
b759dddf5a
@ -58,10 +58,14 @@ impl<S: 'static> HttpHandler for HttpApplication<S> {
|
|||||||
|
|
||||||
fn handle(&self, msg: Request) -> Result<Pipeline<S, Inner<S>>, Request> {
|
fn handle(&self, msg: Request) -> Result<Pipeline<S, Inner<S>>, Request> {
|
||||||
let m = {
|
let m = {
|
||||||
let path = msg.path();
|
if self.prefix_len == 0 {
|
||||||
path.starts_with(&self.prefix)
|
true
|
||||||
&& (path.len() == self.prefix_len
|
} else {
|
||||||
|| path.split_at(self.prefix_len).1.starts_with('/'))
|
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 m {
|
||||||
if let Some(ref filters) = self.filters {
|
if let Some(ref filters) = self.filters {
|
||||||
@ -135,7 +139,7 @@ where
|
|||||||
App {
|
App {
|
||||||
parts: Some(ApplicationParts {
|
parts: Some(ApplicationParts {
|
||||||
state,
|
state,
|
||||||
prefix: "/".to_owned(),
|
prefix: "".to_owned(),
|
||||||
router: Router::new(),
|
router: Router::new(),
|
||||||
middlewares: Vec::new(),
|
middlewares: Vec::new(),
|
||||||
filters: Vec::new(),
|
filters: Vec::new(),
|
||||||
@ -498,12 +502,6 @@ where
|
|||||||
pub fn finish(&mut self) -> HttpApplication<S> {
|
pub fn finish(&mut self) -> HttpApplication<S> {
|
||||||
let mut parts = self.parts.take().expect("Use after finish");
|
let mut parts = self.parts.take().expect("Use after finish");
|
||||||
let prefix = parts.prefix.trim().trim_right_matches('/');
|
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();
|
parts.router.finish();
|
||||||
|
|
||||||
let inner = Rc::new(Inner {
|
let inner = Rc::new(Inner {
|
||||||
@ -517,12 +515,12 @@ where
|
|||||||
};
|
};
|
||||||
|
|
||||||
HttpApplication {
|
HttpApplication {
|
||||||
state: Rc::new(parts.state),
|
|
||||||
middlewares: Rc::new(parts.middlewares),
|
|
||||||
prefix,
|
|
||||||
prefix_len,
|
|
||||||
inner,
|
inner,
|
||||||
filters,
|
filters,
|
||||||
|
state: Rc::new(parts.state),
|
||||||
|
middlewares: Rc::new(parts.middlewares),
|
||||||
|
prefix: prefix.to_owned(),
|
||||||
|
prefix_len: prefix.len(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -357,18 +357,11 @@ impl<S: 'static> Router<S> {
|
|||||||
|
|
||||||
/// Query for matched resource
|
/// Query for matched resource
|
||||||
pub fn recognize(&self, req: &Request, state: &S, tail: usize) -> ResourceInfo {
|
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() {
|
if tail <= req.path().len() {
|
||||||
'outer: for (idx, resource) in self.patterns.iter().enumerate() {
|
'outer: for (idx, resource) in self.patterns.iter().enumerate() {
|
||||||
match resource {
|
match resource {
|
||||||
ResourcePattern::Resource(rdef) => {
|
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);
|
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?
|
/// Are the given path and parameters a match against this resource?
|
||||||
pub fn match_with_params(
|
pub fn match_with_params(&self, req: &Request, plen: usize) -> Option<Params> {
|
||||||
&self, req: &Request, plen: usize, insert: bool,
|
|
||||||
) -> Option<Params> {
|
|
||||||
let path = &req.path()[plen..];
|
let path = &req.path()[plen..];
|
||||||
if insert {
|
|
||||||
if path.is_empty() {
|
|
||||||
"/"
|
|
||||||
} else {
|
|
||||||
path
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
path
|
|
||||||
};
|
|
||||||
|
|
||||||
match self.tp {
|
match self.tp {
|
||||||
PatternType::Static(ref s) => if s != path {
|
PatternType::Static(ref s) => if s != path {
|
||||||
@ -942,11 +924,11 @@ mod tests {
|
|||||||
assert!(!re.is_match("/user/2345/sdg"));
|
assert!(!re.is_match("/user/2345/sdg"));
|
||||||
|
|
||||||
let req = TestRequest::with_uri("/user/profile").finish();
|
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");
|
assert_eq!(info.get("id").unwrap(), "profile");
|
||||||
|
|
||||||
let req = TestRequest::with_uri("/user/1245125").finish();
|
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");
|
assert_eq!(info.get("id").unwrap(), "1245125");
|
||||||
|
|
||||||
let re = ResourceDef::new("/v{version}/resource/{id}");
|
let re = ResourceDef::new("/v{version}/resource/{id}");
|
||||||
@ -955,7 +937,7 @@ mod tests {
|
|||||||
assert!(!re.is_match("/resource"));
|
assert!(!re.is_match("/resource"));
|
||||||
|
|
||||||
let req = TestRequest::with_uri("/v151/resource/adahg32").finish();
|
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("version").unwrap(), "151");
|
||||||
assert_eq!(info.get("id").unwrap(), "adahg32");
|
assert_eq!(info.get("id").unwrap(), "adahg32");
|
||||||
}
|
}
|
||||||
@ -983,12 +965,12 @@ mod tests {
|
|||||||
assert!(!re.is_match("/name"));
|
assert!(!re.is_match("/name"));
|
||||||
|
|
||||||
let req = TestRequest::with_uri("/test2/").finish();
|
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["name"], "test2");
|
||||||
assert_eq!(&info[0], "test2");
|
assert_eq!(&info[0], "test2");
|
||||||
|
|
||||||
let req = TestRequest::with_uri("/test2/subpath1/subpath2/index.html").finish();
|
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["name"], "test2");
|
||||||
assert_eq!(&info[0], "test2");
|
assert_eq!(&info[0], "test2");
|
||||||
}
|
}
|
||||||
|
@ -304,7 +304,7 @@ impl<S: 'static> RouteHandler<S> for Scope<S> {
|
|||||||
let tail = req.match_info().tail as usize;
|
let tail = req.match_info().tail as usize;
|
||||||
|
|
||||||
// recognize resources
|
// 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);
|
let req2 = req.with_route_info(info);
|
||||||
if self.middlewares.is_empty() {
|
if self.middlewares.is_empty() {
|
||||||
self.router.handle(&req2)
|
self.router.handle(&req2)
|
||||||
|
Loading…
Reference in New Issue
Block a user