diff --git a/actix-router/src/resource.rs b/actix-router/src/resource.rs index 230f8e88..c42fd4ed 100644 --- a/actix-router/src/resource.rs +++ b/actix-router/src/resource.rs @@ -56,7 +56,7 @@ impl ResourceDef { let mut re_set = Vec::new(); for path in set { - let (pattern, _, _) = ResourceDef::parse(&path, false); + let (pattern, _, _) = ResourceDef::parse(&path, false, true); let re = match Regex::new(&pattern) { Ok(re) => re, @@ -116,7 +116,7 @@ impl ResourceDef { /// Parse path pattern and create new `Pattern` instance with custom prefix fn with_prefix(path: &str, for_prefix: bool) -> Self { let path = path.to_owned(); - let (pattern, elements, is_dynamic) = ResourceDef::parse(&path, for_prefix); + let (pattern, elements, is_dynamic) = ResourceDef::parse(&path, for_prefix, false); let tp = if is_dynamic { let re = match Regex::new(&pattern) { @@ -400,18 +400,17 @@ impl ResourceDef { ) } - fn parse(mut pattern: &str, mut for_prefix: bool) -> (String, Vec, bool) { - if pattern.find('{').is_none() { - return if let Some(path) = pattern.strip_suffix('*') { - let re = format!("{}^{}(.*)", REGEX_FLAGS, path); - (re, vec![PatternElement::Const(String::from(path))], true) - } else { - ( - String::from(pattern), - vec![PatternElement::Const(String::from(pattern))], - false, - ) - }; + fn parse( + mut pattern: &str, + mut for_prefix: bool, + force_dynamic: bool, + ) -> (String, Vec, bool) { + if !force_dynamic && pattern.find('{').is_none() && !pattern.ends_with('*') { + return ( + String::from(pattern), + vec![PatternElement::Const(String::from(pattern))], + false, + ); } let mut elements = Vec::new(); @@ -433,6 +432,13 @@ impl ResourceDef { dyn_elements += 1; } + if let Some(path) = pattern.strip_suffix('*') { + elements.push(PatternElement::Const(String::from(path))); + re.push_str(&escape(path)); + re.push_str("(.*)"); + pattern = ""; + } + elements.push(PatternElement::Const(String::from(pattern))); re.push_str(&escape(pattern));