diff --git a/CHANGES.md b/CHANGES.md index 0d7bf1e4b..383cc430e 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -4,7 +4,9 @@ * Better naming for websockets implementation -* A filter for cross-site request forgery #89 +* Add `Pattern::with_prefix()`, make it more usable outside of actix + +* Add csrf middleware for filter for cross-site request forgery #89 ## 0.4.1 (2018-03-01) diff --git a/src/router.rs b/src/router.rs index 63cc9045c..fc01bd3be 100644 --- a/src/router.rs +++ b/src/router.rs @@ -148,7 +148,12 @@ impl Pattern { /// /// Panics if path pattern is wrong. pub fn new(name: &str, path: &str) -> Self { - let (pattern, elements, is_dynamic) = Pattern::parse(path); + Pattern::with_prefix(name, path, "/") + } + + /// Parse path pattern and create new `Pattern` instance with custom prefix + pub fn with_prefix(name: &str, path: &str, prefix: &str) -> Self { + let (pattern, elements, is_dynamic) = Pattern::parse(path, prefix); let tp = if is_dynamic { let re = match Regex::new(&pattern) { @@ -188,7 +193,9 @@ impl Pattern { } } - pub fn match_with_params<'a>(&'a self, path: &'a str, params: &'a mut Params<'a>) -> bool { + pub fn match_with_params<'a>(&'a self, path: &'a str, params: &'a mut Params<'a>) + -> bool + { match self.tp { PatternType::Static(ref s) => s == path, PatternType::Dynamic(ref re, ref names) => { @@ -236,11 +243,11 @@ impl Pattern { Ok(path) } - fn parse(pattern: &str) -> (String, Vec, bool) { + fn parse(pattern: &str, prefix: &str) -> (String, Vec, bool) { const DEFAULT_PATTERN: &str = "[^/]+"; - let mut re1 = String::from("^/"); - let mut re2 = String::from("/"); + let mut re1 = String::from("^") + prefix; + let mut re2 = String::from(prefix); let mut el = String::new(); let mut in_param = false; let mut in_param_pattern = false;