1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-24 07:53:00 +01:00

add Pattern::with_prefix, make it usable outside of actix

This commit is contained in:
Nikolay Kim 2018-03-02 20:39:22 -08:00
parent feba5aeffd
commit 2158ad29ee
2 changed files with 15 additions and 6 deletions

View File

@ -4,7 +4,9 @@
* Better naming for websockets implementation * 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) ## 0.4.1 (2018-03-01)

View File

@ -148,7 +148,12 @@ impl Pattern {
/// ///
/// Panics if path pattern is wrong. /// Panics if path pattern is wrong.
pub fn new(name: &str, path: &str) -> Self { 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 tp = if is_dynamic {
let re = match Regex::new(&pattern) { 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 { match self.tp {
PatternType::Static(ref s) => s == path, PatternType::Static(ref s) => s == path,
PatternType::Dynamic(ref re, ref names) => { PatternType::Dynamic(ref re, ref names) => {
@ -236,11 +243,11 @@ impl Pattern {
Ok(path) Ok(path)
} }
fn parse(pattern: &str) -> (String, Vec<PatternElement>, bool) { fn parse(pattern: &str, prefix: &str) -> (String, Vec<PatternElement>, bool) {
const DEFAULT_PATTERN: &str = "[^/]+"; const DEFAULT_PATTERN: &str = "[^/]+";
let mut re1 = String::from("^/"); let mut re1 = String::from("^") + prefix;
let mut re2 = String::from("/"); let mut re2 = String::from(prefix);
let mut el = String::new(); let mut el = String::new();
let mut in_param = false; let mut in_param = false;
let mut in_param_pattern = false; let mut in_param_pattern = false;