1
0
mirror of https://github.com/fafhrd91/actix-net synced 2025-01-19 13:04:40 +01:00
actix-net/actix-router/src/resource.rs

1052 lines
34 KiB
Rust
Raw Normal View History

use std::{
borrow::{Borrow, Cow},
cmp::min,
collections::HashMap,
hash::{BuildHasher, Hash, Hasher},
mem,
};
2019-01-05 13:19:25 -08:00
2019-12-25 15:10:01 +04:00
use regex::{escape, Regex, RegexSet};
2019-01-05 13:19:25 -08:00
use crate::path::{Path, PathItem};
use crate::{IntoPattern, Resource, ResourcePath};
2019-01-05 13:19:25 -08:00
const MAX_DYNAMIC_SEGMENTS: usize = 16;
/// Regex flags to allow '.' in regex to match '\n'
///
/// See the docs under: https://docs.rs/regex/1.5.4/regex/#grouping-and-flags
const REGEX_FLAGS: &str = "(?s-m)";
/// Describes an entry in a resource table.
2019-01-05 13:19:25 -08:00
///
/// Resource definition can contain at most 16 dynamic segments.
2019-01-05 13:19:25 -08:00
#[derive(Clone, Debug)]
2019-02-09 07:24:35 -08:00
pub struct ResourceDef {
2019-03-08 12:33:44 -08:00
id: u16,
/// Pattern type.
pat_type: PatternType,
/// Optional name of resource definition. Defaults to "".
2019-02-09 07:24:35 -08:00
name: String,
/// Pattern that generated the resource definition.
// TODO: Sort of, in dynamic set pattern type it is blank, consider change to option.
2019-01-05 13:19:25 -08:00
pattern: String,
/// List of elements that compose the pattern, in order.
///
/// `None` with pattern type is DynamicSet.
2021-07-15 17:09:01 +03:00
elements: Option<Vec<PatternElement>>,
2019-01-05 13:19:25 -08:00
}
#[derive(Debug, Clone, PartialEq)]
enum PatternElement {
/// Literal slice of pattern.
2021-07-15 17:09:01 +03:00
Const(String),
/// Name of dynamic segment.
2019-01-05 13:19:25 -08:00
Var(String),
/// Tail segment. If present in elements list, it will always be last.
///
/// Tail has optional name for patterns like `/foo/{tail}*` vs "/foo/*".
Tail(Option<String>),
2019-01-05 13:19:25 -08:00
}
#[derive(Clone, Debug)]
2021-02-03 10:25:31 +00:00
#[allow(clippy::large_enum_variant)]
2019-01-05 13:19:25 -08:00
enum PatternType {
/// Single constant/literal segment.
2019-01-05 13:19:25 -08:00
Static(String),
/// Single constant/literal prefix segment.
2019-01-05 13:19:25 -08:00
Prefix(String),
/// Single regular expression and list of dynamic segment names.
2021-07-15 17:09:01 +03:00
Dynamic(Regex, Vec<&'static str>),
/// Regular expression set and list of component expressions plus dynamic segment names.
2021-07-15 17:09:01 +03:00
DynamicSet(RegexSet, Vec<(Regex, Vec<&'static str>)>),
2019-01-05 13:19:25 -08:00
}
2019-02-09 07:24:35 -08:00
impl ResourceDef {
2019-01-05 13:19:25 -08:00
/// Parse path pattern and create new `Pattern` instance.
///
2019-12-25 15:10:01 +04:00
/// Panics if path pattern is malformed.
2019-12-25 19:54:20 +04:00
pub fn new<T: IntoPattern>(path: T) -> Self {
2019-12-25 15:34:21 +04:00
if path.is_single() {
ResourceDef::from_single_pattern(&path.patterns()[0], false)
2019-12-25 15:34:21 +04:00
} else {
let mut data = Vec::new();
let mut re_set = Vec::new();
2019-12-25 15:10:01 +04:00
for pattern in path.patterns() {
match ResourceDef::parse(&pattern, false, true) {
(PatternType::Dynamic(re, names), _) => {
re_set.push(re.as_str().to_owned());
data.push((re, names));
}
_ => unreachable!(),
}
2019-12-25 15:34:21 +04:00
}
2019-12-25 15:10:01 +04:00
2019-12-25 15:34:21 +04:00
ResourceDef {
id: 0,
pat_type: PatternType::DynamicSet(RegexSet::new(re_set).unwrap(), data),
2021-07-15 17:09:01 +03:00
elements: None,
2019-12-25 15:34:21 +04:00
name: String::new(),
pattern: "".to_owned(),
}
2019-12-25 15:10:01 +04:00
}
}
2019-01-05 13:19:25 -08:00
/// Parse path pattern and create new `Pattern` instance.
///
/// Use `prefix` type instead of `static`.
///
2019-12-25 15:10:01 +04:00
/// Panics if path regex pattern is malformed.
2019-01-05 13:19:25 -08:00
pub fn prefix(path: &str) -> Self {
ResourceDef::from_single_pattern(path, true)
2019-02-09 07:24:35 -08:00
}
/// Parse path pattern and create new `Pattern` instance, inserting a `/` to beginning of
/// the pattern if absent.
///
/// Use `prefix` type instead of `static`.
///
2019-12-25 15:10:01 +04:00
/// Panics if path regex pattern is malformed.
pub fn root_prefix(path: &str) -> Self {
ResourceDef::from_single_pattern(&insert_slash(path), true)
}
/// Resource ID.
2019-03-08 12:33:44 -08:00
pub fn id(&self) -> u16 {
self.id
}
/// Set resource ID.
2019-03-08 12:33:44 -08:00
pub fn set_id(&mut self, id: u16) {
self.id = id;
2019-01-05 13:19:25 -08:00
}
/// Parse path pattern and create a new instance
fn from_single_pattern(pattern: &str, for_prefix: bool) -> Self {
let pattern = pattern.to_owned();
let (pat_type, elements) = ResourceDef::parse(&pattern, for_prefix, false);
2019-01-05 13:19:25 -08:00
2019-02-09 07:24:35 -08:00
ResourceDef {
pat_type,
pattern,
2021-07-15 17:09:01 +03:00
elements: Some(elements),
2019-03-08 12:33:44 -08:00
id: 0,
2019-02-09 07:24:35 -08:00
name: String::new(),
2019-01-05 13:19:25 -08:00
}
}
/// Resource pattern name.
2019-03-08 12:33:44 -08:00
pub fn name(&self) -> &str {
&self.name
}
/// Mutable reference to a name of a resource definition.
pub fn name_mut(&mut self) -> &mut String {
&mut self.name
}
2019-01-05 13:19:25 -08:00
/// Path pattern of the resource
pub fn pattern(&self) -> &str {
&self.pattern
}
2020-09-21 22:46:59 +01:00
/// Check if path matches this pattern.
2021-01-26 09:45:43 +00:00
#[inline]
2019-01-05 13:19:25 -08:00
pub fn is_match(&self, path: &str) -> bool {
match self.pat_type {
2019-01-05 13:19:25 -08:00
PatternType::Static(ref s) => s == path,
PatternType::Prefix(ref s) => path.starts_with(s),
2021-07-15 17:09:01 +03:00
PatternType::Dynamic(ref re, _) => re.is_match(path),
2019-12-25 15:10:01 +04:00
PatternType::DynamicSet(ref re, _) => re.is_match(path),
2019-01-05 13:19:25 -08:00
}
}
2020-09-21 22:46:59 +01:00
/// Is prefix path a match against this resource.
pub fn is_prefix_match(&self, path: &str) -> Option<usize> {
let path_len = path.len();
let path = if path.is_empty() { "/" } else { path };
match self.pat_type {
PatternType::Static(ref segment) => {
if segment == path {
Some(path_len)
} else {
None
}
}
PatternType::Prefix(ref prefix) => {
let prefix_len = if path == prefix {
// path length === prefix segment length
path_len
} else {
let is_slash_next =
prefix.ends_with('/') || path.split_at(prefix.len()).1.starts_with('/');
if path.starts_with(prefix) && is_slash_next {
// enters this branch if segment delimiter ("/") is present after prefix
//
// i.e., path starts with prefix segment
// and prefix segment ends with /
// or first character in path after prefix segment length is /
//
// eg: Prefix("/test/") or Prefix("/test") would match:
// - /test/foo
// - /test/foo
if prefix.ends_with('/') {
prefix.len() - 1
} else {
prefix.len()
}
} else {
return None;
}
};
Some(min(path_len, prefix_len))
}
PatternType::Dynamic(ref re, _) => re.find(path).map(|m| m.end()),
2019-12-25 15:10:01 +04:00
PatternType::DynamicSet(ref re, ref params) => {
2021-07-15 17:09:01 +03:00
let idx = re.matches(path).into_iter().next()?;
let (ref pattern, _) = params[idx];
pattern.find(path).map(|m| m.end())
2019-12-25 15:10:01 +04:00
}
}
}
2020-09-21 22:46:59 +01:00
/// Is the given path and parameters a match against this pattern.
pub fn match_path<T: ResourcePath>(&self, path: &mut Path<T>) -> bool {
2021-07-15 17:09:01 +03:00
self.match_path_checked(path, &|_, _| true, &Some(()))
2019-01-05 13:19:25 -08:00
}
2019-05-15 10:21:29 -07:00
/// Is the given path and parameters a match against this pattern?
pub fn match_path_checked<R, T, F, U>(
&self,
res: &mut R,
check: &F,
user_data: &Option<U>,
) -> bool
where
T: ResourcePath,
R: Resource<T>,
F: Fn(&R, &Option<U>) -> bool,
{
2021-07-15 17:09:01 +03:00
let mut segments: [PathItem; MAX_DYNAMIC_SEGMENTS] = Default::default();
let path = res.resource_path();
let (matched_len, matched_vars) = match self.pat_type {
PatternType::Static(ref segment) => {
if segment != path.path() {
2021-07-15 17:09:01 +03:00
return false;
2019-05-15 10:21:29 -07:00
}
(path.path().len(), None)
2019-05-15 10:21:29 -07:00
}
PatternType::Prefix(ref prefix) => {
let path_str = path.path();
let path_len = path_str.len();
2019-12-25 15:10:01 +04:00
let len = {
if prefix == path_str {
// prefix length === path length
path_len
} else {
let is_slash_next = prefix.ends_with('/')
|| path_str.split_at(prefix.len()).1.starts_with('/');
if path_str.starts_with(prefix) && is_slash_next {
if prefix.ends_with('/') {
prefix.len() - 1
} else {
prefix.len()
}
2019-12-25 15:10:01 +04:00
} else {
return false;
2019-12-25 15:10:01 +04:00
}
}
};
(min(path.path().len(), len), None)
2019-12-25 15:10:01 +04:00
}
2021-07-15 17:09:01 +03:00
PatternType::Dynamic(ref re, ref names) => {
let captures = match re.captures(path.path()) {
Some(captures) => captures,
_ => return false,
};
2021-07-15 17:09:01 +03:00
for (no, name) in names.iter().enumerate() {
if let Some(m) = captures.name(&name) {
segments[no] = PathItem::Segment(m.start() as u16, m.end() as u16);
} else {
log::error!("Dynamic path match but not all segments found: {}", name);
return false;
2019-05-15 10:21:29 -07:00
}
}
2021-07-15 17:09:01 +03:00
(captures[0].len(), Some(names))
2019-05-15 10:21:29 -07:00
}
2019-12-25 15:10:01 +04:00
PatternType::DynamicSet(ref re, ref params) => {
2021-07-15 17:09:01 +03:00
let path = path.path();
let (pattern, names) = match re.matches(path).into_iter().next() {
Some(idx) => &params[idx],
_ => return false,
};
2021-07-15 17:09:01 +03:00
let captures = match pattern.captures(path.path()) {
Some(captures) => captures,
_ => return false,
};
2021-07-15 17:09:01 +03:00
for (no, name) in names.iter().enumerate() {
if let Some(m) = captures.name(&name) {
segments[no] = PathItem::Segment(m.start() as u16, m.end() as u16);
2019-05-15 10:21:29 -07:00
} else {
2021-07-15 17:09:01 +03:00
log::error!("Dynamic path match but not all segments found: {}", name);
2019-05-15 10:21:29 -07:00
return false;
}
2021-07-15 17:09:01 +03:00
}
2021-07-15 17:09:01 +03:00
(captures[0].len(), Some(names))
}
};
2019-12-25 15:10:01 +04:00
2021-07-15 17:09:01 +03:00
if !check(res, user_data) {
return false;
}
2019-12-25 15:10:01 +04:00
2021-07-15 17:09:01 +03:00
// Modify `path` to skip matched part and store matched segments
let path = res.resource_path();
if let Some(vars) = matched_vars {
for i in 0..vars.len() {
path.add(vars[i], mem::take(&mut segments[i]));
2019-05-15 10:21:29 -07:00
}
}
2021-07-15 17:09:01 +03:00
path.skip(matched_len as u16);
true
2019-05-15 10:21:29 -07:00
}
/// Builds resource path with a closure that maps variable elements' names to values.
///
/// Unnamed tail pattern elements will receive `None`.
2021-07-15 17:09:01 +03:00
fn build_resource_path<F, I>(&self, path: &mut String, mut vars: F) -> bool
where
F: FnMut(Option<&str>) -> Option<I>,
I: AsRef<str>,
{
2021-07-15 17:09:01 +03:00
for el in match self.elements {
Some(ref elements) => elements,
None => return false,
} {
match *el {
PatternElement::Const(ref val) => path.push_str(val),
PatternElement::Var(ref name) => match vars(Some(name)) {
2021-07-15 17:09:01 +03:00
Some(val) => path.push_str(val.as_ref()),
_ => return false,
},
PatternElement::Tail(ref name) => match vars(name.as_deref()) {
Some(val) => path.push_str(val.as_ref()),
None => return false,
},
2019-12-25 15:10:01 +04:00
}
}
true
}
2019-01-05 13:19:25 -08:00
/// Builds resource path from elements. Returns `true` on success.
///
/// If resource pattern has a tail segment, an element must be provided for it.
pub fn resource_path_from_iter<U, I>(&self, path: &mut String, elements: &mut U) -> bool
2021-07-15 17:09:01 +03:00
where
U: Iterator<Item = I>,
I: AsRef<str>,
{
self.build_resource_path(path, |_| elements.next())
}
// intentionally not deprecated yet
#[doc(hidden)]
pub fn resource_path<U, I>(&self, path: &mut String, elements: &mut U) -> bool
where
U: Iterator<Item = I>,
I: AsRef<str>,
{
self.resource_path_from_iter(path, elements)
}
/// Builds resource path from map of elements. Returns `true` on success.
///
/// If resource pattern has an unnamed tail segment, path building will fail.
pub fn resource_path_from_map<K, V, S>(
&self,
path: &mut String,
elements: &HashMap<K, V, S>,
) -> bool
where
K: Borrow<str> + Eq + Hash,
V: AsRef<str>,
S: BuildHasher,
{
self.build_resource_path(path, |name| {
name.and_then(|name| elements.get(name).map(AsRef::<str>::as_ref))
})
}
// intentionally not deprecated yet
#[doc(hidden)]
pub fn resource_path_named<K, V, S>(
&self,
path: &mut String,
elements: &HashMap<K, V, S>,
) -> bool
where
K: Borrow<str> + Eq + Hash,
V: AsRef<str>,
S: BuildHasher,
{
self.resource_path_from_map(path, elements)
}
/// Build resource path from map of elements, allowing tail segments to be appended.
///
/// If resource pattern does not define a tail segment, the `tail` parameter will be unused.
/// In this case, use [`resource_path_from_map`][Self::resource_path_from_map] instead.
///
/// Returns `true` on success.
pub fn resource_path_from_map_with_tail<K, V, S, T>(
&self,
path: &mut String,
elements: &HashMap<K, V, S>,
tail: T,
) -> bool
where
K: Borrow<str> + Eq + Hash,
V: AsRef<str>,
S: BuildHasher,
T: AsRef<str>,
{
self.build_resource_path(path, |name| match name {
Some(name) => elements.get(name).map(AsRef::<str>::as_ref),
None => Some(tail.as_ref()),
})
}
fn parse_param(pattern: &str) -> (PatternElement, String, &str) {
2019-01-05 13:19:25 -08:00
const DEFAULT_PATTERN: &str = "[^/]+";
2019-04-22 21:19:22 -07:00
const DEFAULT_PATTERN_TAIL: &str = ".*";
2019-01-05 13:19:25 -08:00
let mut params_nesting = 0usize;
let close_idx = pattern
.find(|c| match c {
'{' => {
params_nesting += 1;
false
}
'}' => {
params_nesting -= 1;
params_nesting == 0
}
_ => false,
})
.expect("malformed dynamic segment");
let (mut param, mut unprocessed) = pattern.split_at(close_idx + 1);
// remove outer curly brackets
param = &param[1..param.len() - 1];
let tail = unprocessed == "*";
2019-04-22 21:19:22 -07:00
2019-01-05 13:19:25 -08:00
let (name, pattern) = match param.find(':') {
Some(idx) => {
2019-04-22 21:19:22 -07:00
if tail {
panic!("Custom regex is not supported for remainder match");
}
2019-01-05 13:19:25 -08:00
let (name, pattern) = param.split_at(idx);
(name, &pattern[1..])
}
2019-04-22 21:19:22 -07:00
None => (
param,
if tail {
unprocessed = &unprocessed[1..];
2019-04-22 21:19:22 -07:00
DEFAULT_PATTERN_TAIL
} else {
DEFAULT_PATTERN
},
),
2019-01-05 13:19:25 -08:00
};
let element = if tail {
PatternElement::Tail(Some(name.to_string()))
} else {
PatternElement::Var(name.to_string())
};
let regex = format!(r"(?P<{}>{})", &name, &pattern);
(element, regex, unprocessed)
2019-01-05 13:19:25 -08:00
}
fn parse(
pattern: &str,
for_prefix: bool,
force_dynamic: bool,
) -> (PatternType, Vec<PatternElement>) {
let mut unprocessed = pattern;
if !force_dynamic && unprocessed.find('{').is_none() && !unprocessed.ends_with('*') {
// pattern is static
let tp = if for_prefix {
PatternType::Prefix(unprocessed.to_owned())
2019-04-22 21:19:22 -07:00
} else {
PatternType::Static(unprocessed.to_owned())
2019-04-22 21:19:22 -07:00
};
return (tp, vec![PatternElement::Const(unprocessed.to_owned())]);
2019-04-22 21:19:22 -07:00
}
2019-01-05 13:19:25 -08:00
let mut elements = Vec::new();
let mut re = format!("{}^", REGEX_FLAGS);
let mut dyn_elements = 0;
let mut has_tail_segment = false;
2019-01-05 13:19:25 -08:00
while let Some(idx) = unprocessed.find('{') {
let (prefix, rem) = unprocessed.split_at(idx);
elements.push(PatternElement::Const(prefix.to_owned()));
2019-01-05 13:19:25 -08:00
re.push_str(&escape(prefix));
let (param_pattern, re_part, rem) = Self::parse_param(rem);
if matches!(param_pattern, PatternElement::Tail(_)) {
has_tail_segment = true;
2019-04-22 21:19:22 -07:00
}
elements.push(param_pattern);
2019-01-05 13:19:25 -08:00
re.push_str(&re_part);
unprocessed = rem;
dyn_elements += 1;
2019-01-05 13:19:25 -08:00
}
if let Some(path) = unprocessed.strip_suffix('*') {
// unnamed tail segment
elements.push(PatternElement::Const(path.to_owned()));
elements.push(PatternElement::Tail(None));
re.push_str(&escape(path));
re.push_str("(.*)");
dyn_elements += 1;
} else if !has_tail_segment && !unprocessed.is_empty() {
// prevent `Const("")` element from being added after last dynamic segment
elements.push(PatternElement::Const(unprocessed.to_owned()));
re.push_str(&escape(unprocessed));
}
2019-01-05 13:19:25 -08:00
if dyn_elements > MAX_DYNAMIC_SEGMENTS {
2019-01-05 13:19:25 -08:00
panic!(
"Only {} dynamic segments are allowed, provided: {}",
MAX_DYNAMIC_SEGMENTS, dyn_elements
2019-01-05 13:19:25 -08:00
);
}
if !for_prefix && !has_tail_segment {
2020-09-06 17:41:42 +08:00
re.push('$');
2019-01-05 13:19:25 -08:00
}
let re = match Regex::new(&re) {
Ok(re) => re,
Err(err) => panic!("Wrong path pattern: \"{}\" {}", pattern, err),
};
// `Bok::leak(Box::new(name))` is an intentional memory leak. In typical applications the
// routing table is only constructed once (per worker) so leak is bounded. If you are
// constructing `ResourceDef`s more than once in your application's lifecycle you would
// expect a linear increase in leaked memory over time.
let names = re
.capture_names()
.filter_map(|name| name.map(|name| Box::leak(Box::new(name.to_owned())).as_str()))
.collect();
(PatternType::Dynamic(re, names), elements)
2019-01-05 13:19:25 -08:00
}
}
2019-02-09 07:24:35 -08:00
impl Eq for ResourceDef {}
impl PartialEq for ResourceDef {
fn eq(&self, other: &ResourceDef) -> bool {
2019-01-05 13:19:25 -08:00
self.pattern == other.pattern
}
}
2019-02-09 07:24:35 -08:00
impl Hash for ResourceDef {
2019-01-05 13:19:25 -08:00
fn hash<H: Hasher>(&self, state: &mut H) {
self.pattern.hash(state);
}
}
2019-02-09 07:24:35 -08:00
impl<'a> From<&'a str> for ResourceDef {
fn from(path: &'a str) -> ResourceDef {
ResourceDef::new(path)
}
}
impl From<String> for ResourceDef {
fn from(path: String) -> ResourceDef {
2019-12-25 15:34:21 +04:00
ResourceDef::new(path)
2019-02-09 07:24:35 -08:00
}
}
pub(crate) fn insert_slash(path: &str) -> Cow<'_, str> {
if !path.is_empty() && !path.starts_with('/') {
let mut new_path = String::with_capacity(path.len() + 1);
new_path.push('/');
new_path.push_str(path);
Cow::Owned(new_path)
} else {
Cow::Borrowed(path)
}
}
2019-01-05 13:19:25 -08:00
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_static() {
2019-02-09 07:24:35 -08:00
let re = ResourceDef::new("/");
2019-01-05 13:19:25 -08:00
assert!(re.is_match("/"));
assert!(!re.is_match("/a"));
2019-02-09 07:24:35 -08:00
let re = ResourceDef::new("/name");
2019-01-05 13:19:25 -08:00
assert!(re.is_match("/name"));
assert!(!re.is_match("/name1"));
assert!(!re.is_match("/name/"));
assert!(!re.is_match("/name~"));
let mut path = Path::new("/name");
assert!(re.match_path(&mut path));
assert_eq!(path.unprocessed(), "");
assert_eq!(re.is_prefix_match("/name"), Some(5));
assert_eq!(re.is_prefix_match("/name1"), None);
assert_eq!(re.is_prefix_match("/name/"), None);
assert_eq!(re.is_prefix_match("/name~"), None);
2019-02-09 07:24:35 -08:00
let re = ResourceDef::new("/name/");
2019-01-05 13:19:25 -08:00
assert!(re.is_match("/name/"));
assert!(!re.is_match("/name"));
assert!(!re.is_match("/name/gs"));
2019-02-09 07:24:35 -08:00
let re = ResourceDef::new("/user/profile");
2019-01-05 13:19:25 -08:00
assert!(re.is_match("/user/profile"));
assert!(!re.is_match("/user/profile/profile"));
let mut path = Path::new("/user/profile");
assert!(re.match_path(&mut path));
assert_eq!(path.unprocessed(), "");
2019-01-05 13:19:25 -08:00
}
#[test]
fn parse_param() {
2019-02-09 07:24:35 -08:00
let re = ResourceDef::new("/user/{id}");
2019-01-05 13:19:25 -08:00
assert!(re.is_match("/user/profile"));
assert!(re.is_match("/user/2345"));
assert!(!re.is_match("/user/2345/"));
assert!(!re.is_match("/user/2345/sdg"));
let mut path = Path::new("/user/profile");
assert!(re.match_path(&mut path));
assert_eq!(path.get("id").unwrap(), "profile");
assert_eq!(path.unprocessed(), "");
2019-01-05 13:19:25 -08:00
let mut path = Path::new("/user/1245125");
assert!(re.match_path(&mut path));
assert_eq!(path.get("id").unwrap(), "1245125");
assert_eq!(path.unprocessed(), "");
2019-01-05 13:19:25 -08:00
2019-02-09 07:24:35 -08:00
let re = ResourceDef::new("/v{version}/resource/{id}");
2019-01-05 13:19:25 -08:00
assert!(re.is_match("/v1/resource/320120"));
assert!(!re.is_match("/v/resource/1"));
assert!(!re.is_match("/resource"));
let mut path = Path::new("/v151/resource/adage32");
2019-01-05 13:19:25 -08:00
assert!(re.match_path(&mut path));
assert_eq!(path.get("version").unwrap(), "151");
assert_eq!(path.get("id").unwrap(), "adage32");
assert_eq!(path.unprocessed(), "");
2019-01-05 13:19:25 -08:00
2019-02-09 07:24:35 -08:00
let re = ResourceDef::new("/{id:[[:digit:]]{6}}");
2019-01-05 13:19:25 -08:00
assert!(re.is_match("/012345"));
assert!(!re.is_match("/012"));
assert!(!re.is_match("/01234567"));
assert!(!re.is_match("/XXXXXX"));
let mut path = Path::new("/012345");
assert!(re.match_path(&mut path));
assert_eq!(path.get("id").unwrap(), "012345");
assert_eq!(path.unprocessed(), "");
2019-01-05 13:19:25 -08:00
}
2020-01-28 20:27:33 +09:00
#[allow(clippy::cognitive_complexity)]
2019-12-25 15:10:01 +04:00
#[test]
fn dynamic_set() {
2019-12-25 19:54:20 +04:00
let re = ResourceDef::new(vec![
2019-12-25 15:34:21 +04:00
"/user/{id}",
"/v{version}/resource/{id}",
"/{id:[[:digit:]]{6}}",
"/static",
2019-12-25 15:34:21 +04:00
]);
2019-12-25 15:10:01 +04:00
assert!(re.is_match("/user/profile"));
assert!(re.is_match("/user/2345"));
assert!(!re.is_match("/user/2345/"));
assert!(!re.is_match("/user/2345/sdg"));
let mut path = Path::new("/user/profile");
assert!(re.match_path(&mut path));
assert_eq!(path.get("id").unwrap(), "profile");
assert_eq!(path.unprocessed(), "");
2019-12-25 15:10:01 +04:00
let mut path = Path::new("/user/1245125");
assert!(re.match_path(&mut path));
assert_eq!(path.get("id").unwrap(), "1245125");
assert_eq!(path.unprocessed(), "");
2019-12-25 15:10:01 +04:00
assert!(re.is_match("/v1/resource/320120"));
assert!(!re.is_match("/v/resource/1"));
assert!(!re.is_match("/resource"));
let mut path = Path::new("/v151/resource/adage32");
2019-12-25 15:10:01 +04:00
assert!(re.match_path(&mut path));
assert_eq!(path.get("version").unwrap(), "151");
assert_eq!(path.get("id").unwrap(), "adage32");
2019-12-25 15:10:01 +04:00
assert!(re.is_match("/012345"));
assert!(!re.is_match("/012"));
assert!(!re.is_match("/01234567"));
assert!(!re.is_match("/XXXXXX"));
assert!(re.is_match("/static"));
assert!(!re.is_match("/a/static"));
assert!(!re.is_match("/static/a"));
2019-12-25 15:10:01 +04:00
let mut path = Path::new("/012345");
assert!(re.match_path(&mut path));
assert_eq!(path.get("id").unwrap(), "012345");
2019-12-25 19:54:20 +04:00
let re = ResourceDef::new([
"/user/{id}",
"/v{version}/resource/{id}",
"/{id:[[:digit:]]{6}}",
]);
assert!(re.is_match("/user/profile"));
assert!(re.is_match("/user/2345"));
assert!(!re.is_match("/user/2345/"));
assert!(!re.is_match("/user/2345/sdg"));
let re = ResourceDef::new([
"/user/{id}".to_string(),
"/v{version}/resource/{id}".to_string(),
"/{id:[[:digit:]]{6}}".to_string(),
]);
assert!(re.is_match("/user/profile"));
assert!(re.is_match("/user/2345"));
assert!(!re.is_match("/user/2345/"));
assert!(!re.is_match("/user/2345/sdg"));
2019-12-25 15:10:01 +04:00
}
2019-04-22 21:19:22 -07:00
#[test]
fn parse_tail() {
2019-04-22 21:19:22 -07:00
let re = ResourceDef::new("/user/-{id}*");
let mut path = Path::new("/user/-profile");
assert!(re.match_path(&mut path));
assert_eq!(path.get("id").unwrap(), "profile");
let mut path = Path::new("/user/-2345");
assert!(re.match_path(&mut path));
assert_eq!(path.get("id").unwrap(), "2345");
let mut path = Path::new("/user/-2345/");
assert!(re.match_path(&mut path));
assert_eq!(path.get("id").unwrap(), "2345/");
let mut path = Path::new("/user/-2345/sdg");
assert!(re.match_path(&mut path));
assert_eq!(path.get("id").unwrap(), "2345/sdg");
}
#[test]
fn static_tail() {
2019-04-22 21:19:22 -07:00
let re = ResourceDef::new("/user*");
assert!(re.is_match("/user/profile"));
assert!(re.is_match("/user/2345"));
assert!(re.is_match("/user/2345/"));
assert!(re.is_match("/user/2345/sdg"));
assert!(!re.is_match("/foo/profile"));
2019-04-22 21:19:22 -07:00
let re = ResourceDef::new("/user/*");
assert!(re.is_match("/user/profile"));
assert!(re.is_match("/user/2345"));
assert!(re.is_match("/user/2345/"));
assert!(re.is_match("/user/2345/sdg"));
assert!(!re.is_match("/foo/profile"));
}
#[test]
fn dynamic_tail() {
let re = ResourceDef::new("/user/{id}/*");
assert!(!re.is_match("/user/2345"));
let mut path = Path::new("/user/2345/sdg");
assert!(re.match_path(&mut path));
assert_eq!(path.get("id").unwrap(), "2345");
2019-04-22 21:19:22 -07:00
}
#[test]
fn newline() {
let re = ResourceDef::new("/user/a\nb");
assert!(re.is_match("/user/a\nb"));
assert!(!re.is_match("/user/a\nb/profile"));
let re = ResourceDef::new("/a{x}b/test/a{y}b");
let mut path = Path::new("/a\nb/test/a\nb");
assert!(re.match_path(&mut path));
assert_eq!(path.get("x").unwrap(), "\n");
assert_eq!(path.get("y").unwrap(), "\n");
let re = ResourceDef::new("/user/*");
assert!(re.is_match("/user/a\nb/"));
let re = ResourceDef::new("/user/{id}*");
let mut path = Path::new("/user/a\nb/a\nb");
assert!(re.match_path(&mut path));
assert_eq!(path.get("id").unwrap(), "a\nb/a\nb");
let re = ResourceDef::new("/user/{id:.*}");
let mut path = Path::new("/user/a\nb/a\nb");
assert!(re.match_path(&mut path));
assert_eq!(path.get("id").unwrap(), "a\nb/a\nb");
}
2021-02-24 09:48:41 +00:00
#[cfg(feature = "http")]
2019-02-01 13:25:56 -08:00
#[test]
fn parse_urlencoded_param() {
2021-02-24 09:48:41 +00:00
use std::convert::TryFrom;
2019-02-09 07:24:35 -08:00
let re = ResourceDef::new("/user/{id}/test");
2019-02-01 13:25:56 -08:00
let mut path = Path::new("/user/2345/test");
assert!(re.match_path(&mut path));
assert_eq!(path.get("id").unwrap(), "2345");
let mut path = Path::new("/user/qwe%25/test");
assert!(re.match_path(&mut path));
assert_eq!(path.get("id").unwrap(), "qwe%25");
2021-02-24 09:48:41 +00:00
let uri = http::Uri::try_from("/user/qwe%25/test").unwrap();
2019-02-01 13:25:56 -08:00
let mut path = Path::new(uri);
assert!(re.match_path(&mut path));
assert_eq!(path.get("id").unwrap(), "qwe%25");
}
2019-01-05 13:19:25 -08:00
#[test]
fn prefix_static() {
2019-02-09 07:24:35 -08:00
let re = ResourceDef::prefix("/name");
2019-01-05 13:19:25 -08:00
assert!(re.is_match("/name"));
assert!(re.is_match("/name/"));
assert!(re.is_match("/name/test/test"));
assert!(re.is_match("/name1"));
assert!(re.is_match("/name~"));
let mut path = Path::new("/name");
assert!(re.match_path(&mut path));
assert_eq!(path.unprocessed(), "");
let mut path = Path::new("/name/test");
assert!(re.match_path(&mut path));
assert_eq!(path.unprocessed(), "/test");
assert_eq!(re.is_prefix_match("/name"), Some(5));
assert_eq!(re.is_prefix_match("/name/"), Some(5));
assert_eq!(re.is_prefix_match("/name/test/test"), Some(5));
assert_eq!(re.is_prefix_match("/name1"), None);
assert_eq!(re.is_prefix_match("/name~"), None);
2019-02-09 07:24:35 -08:00
let re = ResourceDef::prefix("/name/");
2019-01-05 13:19:25 -08:00
assert!(re.is_match("/name/"));
assert!(re.is_match("/name/gs"));
assert!(!re.is_match("/name"));
let re = ResourceDef::root_prefix("name/");
assert!(re.is_match("/name/"));
assert!(re.is_match("/name/gs"));
assert!(!re.is_match("/name"));
let mut path = Path::new("/name/gs");
assert!(re.match_path(&mut path));
assert_eq!(path.unprocessed(), "/gs");
2019-01-05 13:19:25 -08:00
}
#[test]
fn prefix_dynamic() {
2019-02-09 07:24:35 -08:00
let re = ResourceDef::prefix("/{name}/");
2019-01-05 13:19:25 -08:00
assert!(re.is_match("/name/"));
assert!(re.is_match("/name/gs"));
assert!(!re.is_match("/name"));
assert_eq!(re.is_prefix_match("/name/"), Some(6));
assert_eq!(re.is_prefix_match("/name/gs"), Some(6));
assert_eq!(re.is_prefix_match("/name"), None);
2019-01-05 13:19:25 -08:00
let mut path = Path::new("/test2/");
assert!(re.match_path(&mut path));
assert_eq!(&path["name"], "test2");
assert_eq!(&path[0], "test2");
assert_eq!(path.unprocessed(), "");
2019-01-05 13:19:25 -08:00
let mut path = Path::new("/test2/subpath1/subpath2/index.html");
assert!(re.match_path(&mut path));
assert_eq!(&path["name"], "test2");
assert_eq!(&path[0], "test2");
assert_eq!(path.unprocessed(), "subpath1/subpath2/index.html");
2019-01-05 13:19:25 -08:00
}
#[test]
fn build_path_list() {
let mut s = String::new();
let resource = ResourceDef::new("/user/{item1}/test");
assert!(resource.resource_path_from_iter(&mut s, &mut (&["user1"]).iter()));
assert_eq!(s, "/user/user1/test");
let mut s = String::new();
let resource = ResourceDef::new("/user/{item1}/{item2}/test");
assert!(resource.resource_path_from_iter(&mut s, &mut (&["item", "item2"]).iter()));
assert_eq!(s, "/user/item/item2/test");
let mut s = String::new();
let resource = ResourceDef::new("/user/{item1}/{item2}");
assert!(resource.resource_path_from_iter(&mut s, &mut (&["item", "item2"]).iter()));
assert_eq!(s, "/user/item/item2");
let mut s = String::new();
let resource = ResourceDef::new("/user/{item1}/{item2}/");
assert!(resource.resource_path_from_iter(&mut s, &mut (&["item", "item2"]).iter()));
assert_eq!(s, "/user/item/item2/");
let mut s = String::new();
assert!(!resource.resource_path_from_iter(&mut s, &mut (&["item"]).iter()));
let mut s = String::new();
assert!(resource.resource_path_from_iter(&mut s, &mut (&["item", "item2"]).iter()));
assert_eq!(s, "/user/item/item2/");
assert!(!resource.resource_path_from_iter(&mut s, &mut (&["item"]).iter()));
let mut s = String::new();
assert!(
resource.resource_path_from_iter(&mut s, &mut vec!["item", "item2"].into_iter())
);
assert_eq!(s, "/user/item/item2/");
}
#[test]
fn build_path_map() {
let resource = ResourceDef::new("/user/{item1}/{item2}/");
let mut map = HashMap::new();
map.insert("item1", "item");
let mut s = String::new();
assert!(!resource.resource_path_from_map(&mut s, &map));
map.insert("item2", "item2");
let mut s = String::new();
assert!(resource.resource_path_from_map(&mut s, &map));
assert_eq!(s, "/user/item/item2/");
}
#[test]
fn build_path_tail() {
let resource = ResourceDef::new("/user/{item1}/*");
let mut s = String::new();
assert!(!resource.resource_path_from_iter(&mut s, &mut (&["user1"]).iter()));
let mut s = String::new();
assert!(resource.resource_path_from_iter(&mut s, &mut (&["user1", "2345"]).iter()));
assert_eq!(s, "/user/user1/2345");
let mut s = String::new();
let mut map = HashMap::new();
map.insert("item1", "item");
assert!(!resource.resource_path_from_map(&mut s, &map));
let mut s = String::new();
assert!(resource.resource_path_from_map_with_tail(&mut s, &map, "2345"));
assert_eq!(s, "/user/item/2345");
let resource = ResourceDef::new("/user/{item1}*");
let mut s = String::new();
assert!(!resource.resource_path_from_iter(&mut s, &mut (&[""; 0]).iter()));
let mut s = String::new();
assert!(resource.resource_path_from_iter(&mut s, &mut (&["user1"]).iter()));
assert_eq!(s, "/user/user1");
let mut s = String::new();
let mut map = HashMap::new();
map.insert("item1", "item");
assert!(resource.resource_path_from_map(&mut s, &map));
assert_eq!(s, "/user/item");
}
#[test]
fn build_path_tail_when_resource_has_no_tail() {
let resource = ResourceDef::new("/user/{item1}");
let mut map = HashMap::new();
map.insert("item1", "item");
let mut s = String::new();
assert!(resource.resource_path_from_map_with_tail(&mut s, &map, "2345"));
assert_eq!(s, "/user/item");
}
#[test]
#[should_panic]
fn invalid_dynamic_segment_delimiter() {
ResourceDef::new("/user/{username");
}
#[test]
#[should_panic]
fn invalid_dynamic_segment_name() {
ResourceDef::new("/user/{}");
}
2019-01-05 13:19:25 -08:00
}