1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-06-12 10:19:36 +00:00

Added RouteInfo::has_prefixed_route() method for route matching with prefix awareness

This commit is contained in:
Mathieu Amiot 2018-07-10 13:05:20 +02:00
parent 30c84786b7
commit 8f64508887
3 changed files with 36 additions and 0 deletions

View file

@ -4,6 +4,8 @@
### Added
* Add `.has_prefixed_route()` method to `router::RouteInfo` for route matching with prefix awareness
* Add `HttpMessage::readlines()` for reading line by line.
* Add `ClientRequestBuilder::form()` for sending `application/x-www-form-urlencoded` requests.

View file

@ -437,7 +437,9 @@ mod tests {
let info = router.default_route_info();
assert!(info.has_route("/user/test.html"));
assert!(info.has_prefixed_route("/user/test.html"));
assert!(!info.has_route("/test/unknown"));
assert!(!info.has_prefixed_route("/test/unknown"));
let req = TestRequest::with_header(header::HOST, "www.rust-lang.org")
.finish_with_router(router);
@ -467,7 +469,9 @@ mod tests {
let mut info = router.default_route_info();
info.set_prefix(7);
assert!(info.has_route("/user/test.html"));
assert!(!info.has_prefixed_route("/user/test.html"));
assert!(!info.has_route("/prefix/user/test.html"));
assert!(info.has_prefixed_route("/prefix/user/test.html"));
let req = TestRequest::with_uri("/prefix/test")
.prefix(7)
@ -490,7 +494,9 @@ mod tests {
let mut info = router.default_route_info();
info.set_prefix(7);
assert!(info.has_route("/index.html"));
assert!(!info.has_prefixed_route("/index.html"));
assert!(!info.has_route("/prefix/index.html"));
assert!(info.has_prefixed_route("/prefix/index.html"));
let req = TestRequest::with_uri("/prefix/test")
.prefix(7)
@ -513,6 +519,7 @@ mod tests {
let info = router.default_route_info();
assert!(!info.has_route("https://youtube.com/watch/unknown"));
assert!(!info.has_prefixed_route("https://youtube.com/watch/unknown"));
let req = TestRequest::default().finish_with_router(router);
let url = req.url_for("youtube", &["oHg5SJYRHA0"]);

View file

@ -1,6 +1,7 @@
use std::cmp::min;
use std::collections::HashMap;
use std::hash::{Hash, Hasher};
use std::path::Path;
use std::rc::Rc;
use regex::{escape, Regex};
@ -146,6 +147,32 @@ impl ResourceInfo {
}
false
}
/// Check if application contains matching route.
///
/// This method does take `prefix` into account
/// but behaves like `has_route` in case `prefix` is not set in the router.
///
/// For example if prefix is `/test` and router contains route `/name`, the
/// following path would be recognizable `/test/name` and `has_prefixed_route()` call
/// would return `true`.
/// It will not match against prefix in case it's not given. For example for `/name`
/// with a `/test` prefix would return `false`
pub fn has_prefixed_route(&self, path: &str) -> bool {
if self.prefix == 0 {
return self.has_route(path);
}
let path_matcher = Path::new(if path.is_empty() { "/" } else { path });
let router_prefix = Path::new(&path[..(self.prefix as usize)]);
if let Ok(p) = path_matcher.strip_prefix(router_prefix) {
if let Some(p_str) = p.to_str() {
return self.has_route(&format!("/{}", p_str));
}
}
false
}
}
struct Inner {