2021-07-16 19:43:48 +01:00
|
|
|
use std::{
|
2021-07-16 21:41:57 +01:00
|
|
|
borrow::{Borrow, Cow},
|
2021-07-16 19:43:48 +01:00
|
|
|
cmp::min,
|
|
|
|
collections::HashMap,
|
2021-07-16 21:41:57 +01:00
|
|
|
hash::{BuildHasher, Hash, Hasher},
|
2021-07-16 19:43:48 +01:00
|
|
|
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};
|
2019-12-31 15:14:53 +06:00
|
|
|
use crate::{IntoPattern, Resource, ResourcePath};
|
2019-01-05 13:19:25 -08:00
|
|
|
|
|
|
|
const MAX_DYNAMIC_SEGMENTS: usize = 16;
|
|
|
|
|
2021-06-06 05:38:58 +03:00
|
|
|
/// 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)";
|
|
|
|
|
2021-07-16 19:43:48 +01:00
|
|
|
/// Describes an entry in a resource table.
|
2019-01-05 13:19:25 -08:00
|
|
|
///
|
2021-07-16 19:43:48 +01: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,
|
2021-07-16 19:43:48 +01:00
|
|
|
|
|
|
|
/// Pattern type.
|
|
|
|
pat_type: PatternType,
|
|
|
|
|
|
|
|
/// Optional name of resource definition. Defaults to "".
|
2019-02-09 07:24:35 -08:00
|
|
|
name: String,
|
2021-07-16 19:43:48 +01:00
|
|
|
|
|
|
|
/// 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,
|
2021-07-16 19:43:48 +01:00
|
|
|
|
|
|
|
/// 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 {
|
2021-07-16 19:43:48 +01:00
|
|
|
/// Literal slice of pattern.
|
2021-07-15 17:09:01 +03:00
|
|
|
Const(String),
|
2021-07-16 19:43:48 +01:00
|
|
|
|
|
|
|
/// Name of dynamic segment.
|
2019-01-05 13:19:25 -08:00
|
|
|
Var(String),
|
2021-07-16 21:41:57 +01:00
|
|
|
|
|
|
|
/// 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 {
|
2021-07-16 19:43:48 +01:00
|
|
|
/// Single constant/literal segment.
|
2019-01-05 13:19:25 -08:00
|
|
|
Static(String),
|
2021-07-16 19:43:48 +01:00
|
|
|
|
|
|
|
/// Single constant/literal prefix segment.
|
2019-01-05 13:19:25 -08:00
|
|
|
Prefix(String),
|
2021-07-16 19:43:48 +01:00
|
|
|
|
|
|
|
/// Single regular expression and list of dynamic segment names.
|
2021-07-15 17:09:01 +03:00
|
|
|
Dynamic(Regex, Vec<&'static str>),
|
2021-07-16 19:43:48 +01:00
|
|
|
|
|
|
|
/// 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() {
|
2021-07-16 20:17:00 +03:00
|
|
|
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
|
|
|
|
2021-07-16 20:17:00 +03: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,
|
2021-07-16 19:43:48 +01:00
|
|
|
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 {
|
2021-07-16 20:17:00 +03:00
|
|
|
ResourceDef::from_single_pattern(path, true)
|
2019-02-09 07:24:35 -08:00
|
|
|
}
|
|
|
|
|
2021-07-16 19:43:48 +01:00
|
|
|
/// Parse path pattern and create new `Pattern` instance, inserting a `/` to beginning of
|
|
|
|
/// the pattern if absent.
|
2019-03-05 21:03:53 -08:00
|
|
|
///
|
|
|
|
/// Use `prefix` type instead of `static`.
|
|
|
|
///
|
2019-12-25 15:10:01 +04:00
|
|
|
/// Panics if path regex pattern is malformed.
|
2019-03-05 21:03:53 -08:00
|
|
|
pub fn root_prefix(path: &str) -> Self {
|
2021-07-16 20:17:00 +03:00
|
|
|
ResourceDef::from_single_pattern(&insert_slash(path), true)
|
2019-03-05 21:03:53 -08:00
|
|
|
}
|
|
|
|
|
2021-07-16 19:43:48 +01:00
|
|
|
/// Resource ID.
|
2019-03-08 12:33:44 -08:00
|
|
|
pub fn id(&self) -> u16 {
|
|
|
|
self.id
|
|
|
|
}
|
|
|
|
|
2021-07-16 19:43:48 +01:00
|
|
|
/// 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
|
|
|
}
|
|
|
|
|
2021-07-16 20:17:00 +03:00
|
|
|
/// Parse path pattern and create a new instance
|
|
|
|
fn from_single_pattern(pattern: &str, for_prefix: bool) -> Self {
|
|
|
|
let pattern = pattern.to_owned();
|
2021-07-16 19:43:48 +01:00
|
|
|
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 {
|
2021-07-16 19:43:48 +01:00
|
|
|
pat_type,
|
2021-07-16 20:17:00 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-16 19:43:48 +01: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 {
|
2021-07-16 19:43:48 +01:00
|
|
|
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.
|
2019-03-08 15:34:40 -08:00
|
|
|
pub fn is_prefix_match(&self, path: &str) -> Option<usize> {
|
2021-07-16 19:43:48 +01:00
|
|
|
let path_len = path.len();
|
2019-03-08 15:34:40 -08:00
|
|
|
let path = if path.is_empty() { "/" } else { path };
|
|
|
|
|
2021-07-16 19:43:48 +01:00
|
|
|
match self.pat_type {
|
|
|
|
PatternType::Static(ref segment) => {
|
|
|
|
if segment == path {
|
|
|
|
Some(path_len)
|
2019-03-08 15:34:40 -08:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2021-07-16 19:43:48 +01:00
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
2019-03-08 15:34:40 -08:00
|
|
|
} else {
|
2021-07-16 19:43:48 +01:00
|
|
|
return None;
|
2019-03-08 15:34:40 -08:00
|
|
|
}
|
|
|
|
};
|
2021-07-16 19:43:48 +01:00
|
|
|
|
|
|
|
Some(min(path_len, prefix_len))
|
2019-03-08 15:34:40 -08:00
|
|
|
}
|
2021-07-16 19:43:48 +01:00
|
|
|
|
|
|
|
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
|
|
|
}
|
2019-03-08 15:34:40 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-21 22:46:59 +01:00
|
|
|
/// Is the given path and parameters a match against this pattern.
|
2019-03-04 11:47:03 -08:00
|
|
|
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();
|
|
|
|
|
2021-07-16 19:43:48 +01:00
|
|
|
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
|
|
|
}
|
2021-07-16 19:43:48 +01:00
|
|
|
|
2021-07-15 17:34:49 +03:00
|
|
|
(path.path().len(), None)
|
2019-05-15 10:21:29 -07:00
|
|
|
}
|
2021-07-16 19:43:48 +01: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 = {
|
2021-07-16 19:43:48 +01:00
|
|
|
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 {
|
2021-07-16 19:43:48 +01:00
|
|
|
return false;
|
2019-12-25 15:10:01 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2021-07-16 19:43:48 +01:00
|
|
|
|
2021-07-15 17:34:49 +03:00
|
|
|
(min(path.path().len(), len), None)
|
2019-12-25 15:10:01 +04:00
|
|
|
}
|
2021-07-16 19:43:48 +01: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-16 19:43:48 +01:00
|
|
|
|
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-16 19:43:48 +01:00
|
|
|
|
2021-07-15 17:09:01 +03:00
|
|
|
(captures[0].len(), Some(names))
|
2019-05-15 10:21:29 -07:00
|
|
|
}
|
2021-07-16 19:43:48 +01: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) => ¶ms[idx],
|
|
|
|
_ => return false,
|
|
|
|
};
|
2021-07-16 19:43:48 +01:00
|
|
|
|
2021-07-15 17:09:01 +03:00
|
|
|
let captures = match pattern.captures(path.path()) {
|
|
|
|
Some(captures) => captures,
|
|
|
|
_ => return false,
|
|
|
|
};
|
2021-07-16 19:43:48 +01:00
|
|
|
|
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-16 19:43:48 +01: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
|
|
|
}
|
|
|
|
|
2021-07-16 21:41:57 +01: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
|
2019-12-31 15:14:53 +06:00
|
|
|
where
|
2021-07-16 21:41:57 +01:00
|
|
|
F: FnMut(Option<&str>) -> Option<I>,
|
2019-12-31 15:14:53 +06:00
|
|
|
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),
|
2021-07-16 21:41:57 +01:00
|
|
|
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,
|
|
|
|
},
|
2021-07-16 21:41:57 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2021-07-16 19:43:48 +01:00
|
|
|
|
2019-03-08 15:34:40 -08:00
|
|
|
true
|
|
|
|
}
|
2019-01-05 13:19:25 -08:00
|
|
|
|
2021-07-16 21:41:57 +01: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())
|
|
|
|
}
|
|
|
|
|
2021-07-16 21:41:57 +01:00
|
|
|
// 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)]
|
2019-12-31 18:02:43 +06:00
|
|
|
pub fn resource_path_named<K, V, S>(
|
|
|
|
&self,
|
|
|
|
path: &mut String,
|
|
|
|
elements: &HashMap<K, V, S>,
|
|
|
|
) -> bool
|
|
|
|
where
|
2021-07-16 21:41:57 +01:00
|
|
|
K: Borrow<str> + Eq + Hash,
|
2019-12-31 18:02:43 +06:00
|
|
|
V: AsRef<str>,
|
2021-07-16 21:41:57 +01:00
|
|
|
S: BuildHasher,
|
2019-12-31 18:02:43 +06:00
|
|
|
{
|
2021-07-16 21:41:57 +01:00
|
|
|
self.resource_path_from_map(path, elements)
|
2019-12-31 18:02:43 +06:00
|
|
|
}
|
|
|
|
|
2021-07-16 21:41:57 +01:00
|
|
|
/// 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 = ".*";
|
2021-07-16 19:43:48 +01:00
|
|
|
|
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");
|
2021-07-16 19:43:48 +01:00
|
|
|
|
2021-07-16 21:41:57 +01:00
|
|
|
let (mut param, mut unprocessed) = pattern.split_at(close_idx + 1);
|
|
|
|
|
|
|
|
// remove outer curly brackets
|
|
|
|
param = ¶m[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");
|
|
|
|
}
|
2021-07-16 21:41:57 +01:00
|
|
|
|
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 {
|
2021-07-16 21:41:57 +01:00
|
|
|
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
|
|
|
};
|
2021-07-16 19:43:48 +01:00
|
|
|
|
2021-07-16 21:41:57 +01: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
|
|
|
}
|
|
|
|
|
2021-07-16 20:17:00 +03:00
|
|
|
fn parse(
|
2021-07-16 21:41:57 +01:00
|
|
|
pattern: &str,
|
|
|
|
for_prefix: bool,
|
2021-07-16 20:17:00 +03:00
|
|
|
force_dynamic: bool,
|
|
|
|
) -> (PatternType, Vec<PatternElement>) {
|
2021-07-16 21:41:57 +01:00
|
|
|
let mut unprocessed = pattern;
|
|
|
|
|
|
|
|
if !force_dynamic && unprocessed.find('{').is_none() && !unprocessed.ends_with('*') {
|
|
|
|
// pattern is static
|
|
|
|
|
2021-07-16 20:17:00 +03:00
|
|
|
let tp = if for_prefix {
|
2021-07-16 21:41:57 +01:00
|
|
|
PatternType::Prefix(unprocessed.to_owned())
|
2019-04-22 21:19:22 -07:00
|
|
|
} else {
|
2021-07-16 21:41:57 +01:00
|
|
|
PatternType::Static(unprocessed.to_owned())
|
2019-04-22 21:19:22 -07:00
|
|
|
};
|
2021-07-16 21:41:57 +01: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
|
|
|
|
2020-11-25 01:41:14 +00:00
|
|
|
let mut elements = Vec::new();
|
2021-06-06 05:38:58 +03:00
|
|
|
let mut re = format!("{}^", REGEX_FLAGS);
|
2020-11-25 01:41:14 +00:00
|
|
|
let mut dyn_elements = 0;
|
2021-07-16 21:41:57 +01:00
|
|
|
let mut has_tail_segment = false;
|
2019-01-05 13:19:25 -08:00
|
|
|
|
2021-07-16 21:41:57 +01:00
|
|
|
while let Some(idx) = unprocessed.find('{') {
|
|
|
|
let (prefix, rem) = unprocessed.split_at(idx);
|
2021-07-16 19:43:48 +01:00
|
|
|
|
2021-07-16 21:41:57 +01:00
|
|
|
elements.push(PatternElement::Const(prefix.to_owned()));
|
2019-01-05 13:19:25 -08:00
|
|
|
re.push_str(&escape(prefix));
|
2021-07-16 19:43:48 +01:00
|
|
|
|
2021-07-16 21:41:57 +01:00
|
|
|
let (param_pattern, re_part, rem) = Self::parse_param(rem);
|
2021-07-16 19:43:48 +01:00
|
|
|
|
2021-07-16 21:41:57 +01:00
|
|
|
if matches!(param_pattern, PatternElement::Tail(_)) {
|
|
|
|
has_tail_segment = true;
|
2019-04-22 21:19:22 -07:00
|
|
|
}
|
|
|
|
|
2020-11-25 01:41:14 +00:00
|
|
|
elements.push(param_pattern);
|
2019-01-05 13:19:25 -08:00
|
|
|
re.push_str(&re_part);
|
2021-07-16 19:43:48 +01:00
|
|
|
|
2021-07-16 21:41:57 +01:00
|
|
|
unprocessed = rem;
|
2020-11-25 01:41:14 +00:00
|
|
|
dyn_elements += 1;
|
2019-01-05 13:19:25 -08:00
|
|
|
}
|
|
|
|
|
2021-07-16 21:41:57 +01:00
|
|
|
if let Some(path) = unprocessed.strip_suffix('*') {
|
|
|
|
// unnamed tail segment
|
|
|
|
|
|
|
|
elements.push(PatternElement::Const(path.to_owned()));
|
|
|
|
elements.push(PatternElement::Tail(None));
|
|
|
|
|
2021-07-16 20:17:00 +03:00
|
|
|
re.push_str(&escape(path));
|
|
|
|
re.push_str("(.*)");
|
|
|
|
|
2021-07-16 21:41:57 +01:00
|
|
|
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
|
|
|
|
2020-11-25 01:41:14 +00:00
|
|
|
if dyn_elements > MAX_DYNAMIC_SEGMENTS {
|
2019-01-05 13:19:25 -08:00
|
|
|
panic!(
|
2020-11-25 01:41:14 +00:00
|
|
|
"Only {} dynamic segments are allowed, provided: {}",
|
|
|
|
MAX_DYNAMIC_SEGMENTS, dyn_elements
|
2019-01-05 13:19:25 -08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-16 21:41:57 +01: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
|
|
|
}
|
2021-07-16 20:17:00 +03:00
|
|
|
|
|
|
|
let re = match Regex::new(&re) {
|
|
|
|
Ok(re) => re,
|
2021-07-16 21:41:57 +01:00
|
|
|
Err(err) => panic!("Wrong path pattern: \"{}\" {}", pattern, err),
|
2021-07-16 20:17:00 +03:00
|
|
|
};
|
2021-07-16 21:41:57 +01:00
|
|
|
|
|
|
|
// `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.
|
2021-07-16 20:17:00 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-16 19:43:48 +01:00
|
|
|
pub(crate) fn insert_slash(path: &str) -> Cow<'_, str> {
|
2019-03-05 21:03:53 -08:00
|
|
|
if !path.is_empty() && !path.starts_with('/') {
|
2021-07-16 21:41:57 +01:00
|
|
|
let mut new_path = String::with_capacity(path.len() + 1);
|
|
|
|
new_path.push('/');
|
2021-07-16 19:43:48 +01:00
|
|
|
new_path.push_str(path);
|
|
|
|
Cow::Owned(new_path)
|
|
|
|
} else {
|
|
|
|
Cow::Borrowed(path)
|
|
|
|
}
|
2019-03-05 21:03:53 -08:00
|
|
|
}
|
|
|
|
|
2019-01-05 13:19:25 -08:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
2021-07-16 21:41:57 +01:00
|
|
|
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~"));
|
|
|
|
|
2021-07-15 17:34:49 +03:00
|
|
|
let mut path = Path::new("/name");
|
|
|
|
assert!(re.match_path(&mut path));
|
|
|
|
assert_eq!(path.unprocessed(), "");
|
|
|
|
|
2019-03-08 15:34:40 -08:00
|
|
|
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"));
|
2021-07-15 17:34:49 +03:00
|
|
|
|
|
|
|
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]
|
2021-07-16 21:41:57 +01:00
|
|
|
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");
|
2021-07-15 17:34:49 +03:00
|
|
|
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");
|
2021-07-15 17:34:49 +03:00
|
|
|
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"));
|
|
|
|
|
2020-11-25 01:41:14 +00:00
|
|
|
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");
|
2020-11-25 01:41:14 +00:00
|
|
|
assert_eq!(path.get("id").unwrap(), "adage32");
|
2021-07-15 17:34:49 +03:00
|
|
|
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");
|
2021-07-15 17:34:49 +03:00
|
|
|
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]
|
2021-07-16 21:41:57 +01:00
|
|
|
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}}",
|
2021-07-16 20:17:00 +03:00
|
|
|
"/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");
|
2021-07-15 17:34:49 +03:00
|
|
|
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");
|
2021-07-15 17:34:49 +03:00
|
|
|
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"));
|
|
|
|
|
2020-11-25 01:41:14 +00:00
|
|
|
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");
|
2020-11-25 01:41:14 +00:00
|
|
|
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"));
|
|
|
|
|
2021-07-16 20:17:00 +03:00
|
|
|
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]
|
2021-07-16 21:41:57 +01:00
|
|
|
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]
|
2021-07-16 21:41:57 +01:00
|
|
|
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"));
|
2021-07-16 21:41:57 +01:00
|
|
|
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"));
|
2021-07-16 21:41:57 +01:00
|
|
|
assert!(!re.is_match("/foo/profile"));
|
|
|
|
}
|
2021-07-16 20:17:00 +03:00
|
|
|
|
2021-07-16 21:41:57 +01:00
|
|
|
#[test]
|
|
|
|
fn dynamic_tail() {
|
2021-07-16 20:17:00 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-06-06 05:38:58 +03:00
|
|
|
#[test]
|
2021-07-16 21:41:57 +01:00
|
|
|
fn newline() {
|
2021-06-06 05:38:58 +03:00
|
|
|
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]
|
2021-07-16 21:41:57 +01:00
|
|
|
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]
|
2021-07-16 21:41:57 +01:00
|
|
|
fn prefix_static() {
|
2019-02-09 07:24:35 -08:00
|
|
|
let re = ResourceDef::prefix("/name");
|
2021-07-16 21:41:57 +01:00
|
|
|
|
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~"));
|
|
|
|
|
2021-07-15 17:34:49 +03:00
|
|
|
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");
|
|
|
|
|
2019-03-08 15:34:40 -08:00
|
|
|
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"));
|
2019-03-05 21:03:53 -08:00
|
|
|
|
|
|
|
let re = ResourceDef::root_prefix("name/");
|
|
|
|
assert!(re.is_match("/name/"));
|
|
|
|
assert!(re.is_match("/name/gs"));
|
|
|
|
assert!(!re.is_match("/name"));
|
2021-07-15 17:34:49 +03:00
|
|
|
|
|
|
|
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]
|
2021-07-16 21:41:57 +01:00
|
|
|
fn prefix_dynamic() {
|
2019-02-09 07:24:35 -08:00
|
|
|
let re = ResourceDef::prefix("/{name}/");
|
2021-07-16 21:41:57 +01:00
|
|
|
|
2019-01-05 13:19:25 -08:00
|
|
|
assert!(re.is_match("/name/"));
|
|
|
|
assert!(re.is_match("/name/gs"));
|
|
|
|
assert!(!re.is_match("/name"));
|
|
|
|
|
2019-03-08 15:34:40 -08:00
|
|
|
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");
|
2021-07-15 17:34:49 +03:00
|
|
|
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");
|
2021-07-15 17:34:49 +03:00
|
|
|
assert_eq!(path.unprocessed(), "subpath1/subpath2/index.html");
|
2019-01-05 13:19:25 -08:00
|
|
|
}
|
2019-12-31 18:02:43 +06:00
|
|
|
|
|
|
|
#[test]
|
2021-07-16 21:41:57 +01:00
|
|
|
fn build_path_list() {
|
2019-12-31 18:02:43 +06:00
|
|
|
let mut s = String::new();
|
|
|
|
let resource = ResourceDef::new("/user/{item1}/test");
|
2021-07-16 21:41:57 +01:00
|
|
|
assert!(resource.resource_path_from_iter(&mut s, &mut (&["user1"]).iter()));
|
2019-12-31 18:02:43 +06:00
|
|
|
assert_eq!(s, "/user/user1/test");
|
|
|
|
|
|
|
|
let mut s = String::new();
|
|
|
|
let resource = ResourceDef::new("/user/{item1}/{item2}/test");
|
2021-07-16 21:41:57 +01:00
|
|
|
assert!(resource.resource_path_from_iter(&mut s, &mut (&["item", "item2"]).iter()));
|
2019-12-31 18:02:43 +06:00
|
|
|
assert_eq!(s, "/user/item/item2/test");
|
|
|
|
|
|
|
|
let mut s = String::new();
|
|
|
|
let resource = ResourceDef::new("/user/{item1}/{item2}");
|
2021-07-16 21:41:57 +01:00
|
|
|
assert!(resource.resource_path_from_iter(&mut s, &mut (&["item", "item2"]).iter()));
|
2019-12-31 18:02:43 +06:00
|
|
|
assert_eq!(s, "/user/item/item2");
|
|
|
|
|
|
|
|
let mut s = String::new();
|
|
|
|
let resource = ResourceDef::new("/user/{item1}/{item2}/");
|
2021-07-16 21:41:57 +01:00
|
|
|
assert!(resource.resource_path_from_iter(&mut s, &mut (&["item", "item2"]).iter()));
|
2019-12-31 18:02:43 +06:00
|
|
|
assert_eq!(s, "/user/item/item2/");
|
|
|
|
|
|
|
|
let mut s = String::new();
|
2021-07-16 21:41:57 +01:00
|
|
|
assert!(!resource.resource_path_from_iter(&mut s, &mut (&["item"]).iter()));
|
2019-12-31 18:02:43 +06:00
|
|
|
|
|
|
|
let mut s = String::new();
|
2021-07-16 21:41:57 +01:00
|
|
|
assert!(resource.resource_path_from_iter(&mut s, &mut (&["item", "item2"]).iter()));
|
2019-12-31 18:02:43 +06:00
|
|
|
assert_eq!(s, "/user/item/item2/");
|
2021-07-16 21:41:57 +01:00
|
|
|
assert!(!resource.resource_path_from_iter(&mut s, &mut (&["item"]).iter()));
|
2019-12-31 18:02:43 +06:00
|
|
|
|
|
|
|
let mut s = String::new();
|
2021-07-16 21:41:57 +01:00
|
|
|
assert!(
|
|
|
|
resource.resource_path_from_iter(&mut s, &mut vec!["item", "item2"].into_iter())
|
|
|
|
);
|
2019-12-31 18:02:43 +06:00
|
|
|
assert_eq!(s, "/user/item/item2/");
|
2021-07-16 21:41:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn build_path_map() {
|
|
|
|
let resource = ResourceDef::new("/user/{item1}/{item2}/");
|
2019-12-31 18:02:43 +06:00
|
|
|
|
|
|
|
let mut map = HashMap::new();
|
|
|
|
map.insert("item1", "item");
|
|
|
|
|
|
|
|
let mut s = String::new();
|
2021-07-16 21:41:57 +01:00
|
|
|
assert!(!resource.resource_path_from_map(&mut s, &map));
|
2019-12-31 18:02:43 +06:00
|
|
|
|
|
|
|
map.insert("item2", "item2");
|
2021-07-16 21:41:57 +01:00
|
|
|
|
|
|
|
let mut s = String::new();
|
|
|
|
assert!(resource.resource_path_from_map(&mut s, &map));
|
2019-12-31 18:02:43 +06:00
|
|
|
assert_eq!(s, "/user/item/item2/");
|
|
|
|
}
|
2021-07-16 21:41:57 +01:00
|
|
|
|
|
|
|
#[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
|
|
|
}
|