From 2158ad29ee97bae21c3a7a05e4867f3123f37b88 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Fri, 2 Mar 2018 20:39:22 -0800 Subject: [PATCH] add Pattern::with_prefix, make it usable outside of actix --- CHANGES.md | 4 +++- src/router.rs | 17 ++++++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) 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;