mirror of
https://github.com/fafhrd91/actix-net
synced 2024-11-27 22:32:57 +01:00
parent
e0dd2a3d76
commit
8aca8d4d07
@ -167,13 +167,13 @@ impl ResourceDef {
|
|||||||
|
|
||||||
/// Is prefix path a match against this resource.
|
/// Is prefix path a match against this resource.
|
||||||
pub fn is_prefix_match(&self, path: &str) -> Option<usize> {
|
pub fn is_prefix_match(&self, path: &str) -> Option<usize> {
|
||||||
let plen = path.len();
|
let p_len = path.len();
|
||||||
let path = if path.is_empty() { "/" } else { path };
|
let path = if path.is_empty() { "/" } else { path };
|
||||||
|
|
||||||
match self.tp {
|
match self.tp {
|
||||||
PatternType::Static(ref s) => {
|
PatternType::Static(ref s) => {
|
||||||
if s == path {
|
if s == path {
|
||||||
Some(plen)
|
Some(p_len)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
@ -211,7 +211,7 @@ impl ResourceDef {
|
|||||||
} else {
|
} else {
|
||||||
return None;
|
return None;
|
||||||
};
|
};
|
||||||
Some(min(plen, len))
|
Some(min(p_len, len))
|
||||||
}
|
}
|
||||||
PatternType::DynamicSet(ref re, ref params) => {
|
PatternType::DynamicSet(ref re, ref params) => {
|
||||||
if let Some(idx) = re.matches(path).into_iter().next() {
|
if let Some(idx) = re.matches(path).into_iter().next() {
|
||||||
@ -252,11 +252,11 @@ impl ResourceDef {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
PatternType::Prefix(ref s) => {
|
PatternType::Prefix(ref s) => {
|
||||||
let rpath = path.path();
|
let r_path = path.path();
|
||||||
let len = if s == rpath {
|
let len = if s == r_path {
|
||||||
s.len()
|
s.len()
|
||||||
} else if rpath.starts_with(s)
|
} else if r_path.starts_with(s)
|
||||||
&& (s.ends_with('/') || rpath.split_at(s.len()).1.starts_with('/'))
|
&& (s.ends_with('/') || r_path.split_at(s.len()).1.starts_with('/'))
|
||||||
{
|
{
|
||||||
if s.ends_with('/') {
|
if s.ends_with('/') {
|
||||||
s.len() - 1
|
s.len() - 1
|
||||||
@ -266,8 +266,8 @@ impl ResourceDef {
|
|||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
let rpath_len = rpath.len();
|
let r_path_len = r_path.len();
|
||||||
path.skip(min(rpath_len, len) as u16);
|
path.skip(min(r_path_len, len) as u16);
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
PatternType::Dynamic(ref re, ref names, len) => {
|
PatternType::Dynamic(ref re, ref names, len) => {
|
||||||
@ -361,11 +361,11 @@ impl ResourceDef {
|
|||||||
}
|
}
|
||||||
PatternType::Prefix(ref s) => {
|
PatternType::Prefix(ref s) => {
|
||||||
let len = {
|
let len = {
|
||||||
let rpath = res.resource_path().path();
|
let r_path = res.resource_path().path();
|
||||||
if s == rpath {
|
if s == r_path {
|
||||||
s.len()
|
s.len()
|
||||||
} else if rpath.starts_with(s)
|
} else if r_path.starts_with(s)
|
||||||
&& (s.ends_with('/') || rpath.split_at(s.len()).1.starts_with('/'))
|
&& (s.ends_with('/') || r_path.split_at(s.len()).1.starts_with('/'))
|
||||||
{
|
{
|
||||||
if s.ends_with('/') {
|
if s.ends_with('/') {
|
||||||
s.len() - 1
|
s.len() - 1
|
||||||
@ -580,6 +580,8 @@ impl ResourceDef {
|
|||||||
mut for_prefix: bool,
|
mut for_prefix: bool,
|
||||||
) -> (String, Vec<PatternElement>, bool, usize) {
|
) -> (String, Vec<PatternElement>, bool, usize) {
|
||||||
if pattern.find('{').is_none() {
|
if pattern.find('{').is_none() {
|
||||||
|
// TODO: MSRV: 1.45
|
||||||
|
#[allow(clippy::manual_strip)]
|
||||||
return if pattern.ends_with('*') {
|
return if pattern.ends_with('*') {
|
||||||
let path = &pattern[..pattern.len() - 1];
|
let path = &pattern[..pattern.len() - 1];
|
||||||
let re = String::from("^") + path + "(.*)";
|
let re = String::from("^") + path + "(.*)";
|
||||||
@ -594,39 +596,39 @@ impl ResourceDef {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut elems = Vec::new();
|
let mut elements = Vec::new();
|
||||||
let mut re = String::from("^");
|
let mut re = String::from("^");
|
||||||
let mut dyn_elems = 0;
|
let mut dyn_elements = 0;
|
||||||
|
|
||||||
while let Some(idx) = pattern.find('{') {
|
while let Some(idx) = pattern.find('{') {
|
||||||
let (prefix, rem) = pattern.split_at(idx);
|
let (prefix, rem) = pattern.split_at(idx);
|
||||||
elems.push(PatternElement::Str(String::from(prefix)));
|
elements.push(PatternElement::Str(String::from(prefix)));
|
||||||
re.push_str(&escape(prefix));
|
re.push_str(&escape(prefix));
|
||||||
let (param_pattern, re_part, rem, tail) = Self::parse_param(rem);
|
let (param_pattern, re_part, rem, tail) = Self::parse_param(rem);
|
||||||
if tail {
|
if tail {
|
||||||
for_prefix = true;
|
for_prefix = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
elems.push(param_pattern);
|
elements.push(param_pattern);
|
||||||
re.push_str(&re_part);
|
re.push_str(&re_part);
|
||||||
pattern = rem;
|
pattern = rem;
|
||||||
dyn_elems += 1;
|
dyn_elements += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
elems.push(PatternElement::Str(String::from(pattern)));
|
elements.push(PatternElement::Str(String::from(pattern)));
|
||||||
re.push_str(&escape(pattern));
|
re.push_str(&escape(pattern));
|
||||||
|
|
||||||
if dyn_elems > MAX_DYNAMIC_SEGMENTS {
|
if dyn_elements > MAX_DYNAMIC_SEGMENTS {
|
||||||
panic!(
|
panic!(
|
||||||
"Only {} dynanic segments are allowed, provided: {}",
|
"Only {} dynamic segments are allowed, provided: {}",
|
||||||
MAX_DYNAMIC_SEGMENTS, dyn_elems
|
MAX_DYNAMIC_SEGMENTS, dyn_elements
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if !for_prefix {
|
if !for_prefix {
|
||||||
re.push('$');
|
re.push('$');
|
||||||
}
|
}
|
||||||
(re, elems, true, pattern.chars().count())
|
(re, elements, true, pattern.chars().count())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -718,10 +720,10 @@ mod tests {
|
|||||||
assert!(!re.is_match("/v/resource/1"));
|
assert!(!re.is_match("/v/resource/1"));
|
||||||
assert!(!re.is_match("/resource"));
|
assert!(!re.is_match("/resource"));
|
||||||
|
|
||||||
let mut path = Path::new("/v151/resource/adahg32");
|
let mut path = Path::new("/v151/resource/adage32");
|
||||||
assert!(re.match_path(&mut path));
|
assert!(re.match_path(&mut path));
|
||||||
assert_eq!(path.get("version").unwrap(), "151");
|
assert_eq!(path.get("version").unwrap(), "151");
|
||||||
assert_eq!(path.get("id").unwrap(), "adahg32");
|
assert_eq!(path.get("id").unwrap(), "adage32");
|
||||||
|
|
||||||
let re = ResourceDef::new("/{id:[[:digit:]]{6}}");
|
let re = ResourceDef::new("/{id:[[:digit:]]{6}}");
|
||||||
assert!(re.is_match("/012345"));
|
assert!(re.is_match("/012345"));
|
||||||
@ -759,10 +761,10 @@ mod tests {
|
|||||||
assert!(!re.is_match("/v/resource/1"));
|
assert!(!re.is_match("/v/resource/1"));
|
||||||
assert!(!re.is_match("/resource"));
|
assert!(!re.is_match("/resource"));
|
||||||
|
|
||||||
let mut path = Path::new("/v151/resource/adahg32");
|
let mut path = Path::new("/v151/resource/adage32");
|
||||||
assert!(re.match_path(&mut path));
|
assert!(re.match_path(&mut path));
|
||||||
assert_eq!(path.get("version").unwrap(), "151");
|
assert_eq!(path.get("version").unwrap(), "151");
|
||||||
assert_eq!(path.get("id").unwrap(), "adahg32");
|
assert_eq!(path.get("id").unwrap(), "adage32");
|
||||||
|
|
||||||
assert!(re.is_match("/012345"));
|
assert!(re.is_match("/012345"));
|
||||||
assert!(!re.is_match("/012"));
|
assert!(!re.is_match("/012"));
|
||||||
|
@ -182,11 +182,11 @@ impl Quoter {
|
|||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn from_hex(v: u8) -> Option<u8> {
|
fn from_hex(v: u8) -> Option<u8> {
|
||||||
if v >= b'0' && v <= b'9' {
|
if (b'0'..=b'9').contains(&v) {
|
||||||
Some(v - 0x30) // ord('0') == 0x30
|
Some(v - 0x30) // ord('0') == 0x30
|
||||||
} else if v >= b'A' && v <= b'F' {
|
} else if (b'A'..=b'F').contains(&v) {
|
||||||
Some(v - 0x41 + 10) // ord('A') == 0x41
|
Some(v - 0x41 + 10) // ord('A') == 0x41
|
||||||
} else if v >= b'a' && v <= b'f' {
|
} else if (b'a'..=b'f').contains(&v) {
|
||||||
Some(v - 0x61 + 10) // ord('a') == 0x61
|
Some(v - 0x61 + 10) // ord('a') == 0x61
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
|
Loading…
Reference in New Issue
Block a user