2018-04-14 01:02:01 +02:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::hash::{Hash, Hasher};
|
2017-12-07 01:26:27 +01:00
|
|
|
use std::rc::Rc;
|
|
|
|
|
2018-04-14 01:02:01 +02:00
|
|
|
use regex::{escape, Regex};
|
2018-06-19 21:27:41 +02:00
|
|
|
use smallvec::SmallVec;
|
2017-12-08 01:22:26 +01:00
|
|
|
|
2018-04-02 02:37:22 +02:00
|
|
|
use error::UrlGenerationError;
|
2017-12-08 01:22:26 +01:00
|
|
|
use httprequest::HttpRequest;
|
2018-06-19 12:45:26 +02:00
|
|
|
use param::ParamItem;
|
2018-04-14 01:02:01 +02:00
|
|
|
use resource::ResourceHandler;
|
2017-12-08 18:48:53 +01:00
|
|
|
use server::ServerSettings;
|
2017-12-07 01:26:27 +01:00
|
|
|
|
2018-06-22 09:33:32 +02:00
|
|
|
use string_cache::DefaultAtom as Atom;
|
|
|
|
|
2017-12-07 01:26:27 +01:00
|
|
|
/// Interface for application router.
|
2017-12-26 18:00:45 +01:00
|
|
|
pub struct Router(Rc<Inner>);
|
2017-12-08 01:22:26 +01:00
|
|
|
|
2017-12-26 18:00:45 +01:00
|
|
|
struct Inner {
|
2017-12-08 01:22:26 +01:00
|
|
|
prefix: String,
|
2017-12-29 23:04:13 +01:00
|
|
|
prefix_len: usize,
|
2018-04-02 02:37:22 +02:00
|
|
|
named: HashMap<String, (Resource, bool)>,
|
|
|
|
patterns: Vec<Resource>,
|
2017-12-08 18:48:53 +01:00
|
|
|
srv: ServerSettings,
|
2017-12-08 01:22:26 +01:00
|
|
|
}
|
2017-12-07 01:26:27 +01:00
|
|
|
|
2017-12-26 18:00:45 +01:00
|
|
|
impl Router {
|
2017-12-08 01:22:26 +01:00
|
|
|
/// Create new router
|
2018-04-14 01:02:01 +02:00
|
|
|
pub fn new<S>(
|
|
|
|
prefix: &str, settings: ServerSettings,
|
|
|
|
map: Vec<(Resource, Option<ResourceHandler<S>>)>,
|
|
|
|
) -> (Router, Vec<ResourceHandler<S>>) {
|
2017-12-07 01:26:27 +01:00
|
|
|
let prefix = prefix.trim().trim_right_matches('/').to_owned();
|
2017-12-08 01:22:26 +01:00
|
|
|
let mut named = HashMap::new();
|
|
|
|
let mut patterns = Vec::new();
|
2017-12-07 01:26:27 +01:00
|
|
|
let mut resources = Vec::new();
|
2017-12-08 01:22:26 +01:00
|
|
|
|
|
|
|
for (pattern, resource) in map {
|
|
|
|
if !pattern.name().is_empty() {
|
|
|
|
let name = pattern.name().into();
|
|
|
|
named.insert(name, (pattern.clone(), resource.is_none()));
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(resource) = resource {
|
|
|
|
patterns.push(pattern);
|
|
|
|
resources.push(resource);
|
|
|
|
}
|
2017-12-07 01:26:27 +01:00
|
|
|
}
|
|
|
|
|
2018-02-26 23:33:56 +01:00
|
|
|
let prefix_len = prefix.len();
|
2018-04-14 01:02:01 +02:00
|
|
|
(
|
|
|
|
Router(Rc::new(Inner {
|
|
|
|
prefix,
|
|
|
|
prefix_len,
|
|
|
|
named,
|
|
|
|
patterns,
|
|
|
|
srv: settings,
|
|
|
|
})),
|
|
|
|
resources,
|
|
|
|
)
|
2017-12-07 01:26:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Router prefix
|
|
|
|
#[inline]
|
2017-12-08 03:00:20 +01:00
|
|
|
pub fn prefix(&self) -> &str {
|
2017-12-08 01:22:26 +01:00
|
|
|
&self.0.prefix
|
2017-12-07 01:26:27 +01:00
|
|
|
}
|
|
|
|
|
2017-12-08 18:48:53 +01:00
|
|
|
/// Server settings
|
|
|
|
#[inline]
|
|
|
|
pub fn server_settings(&self) -> &ServerSettings {
|
|
|
|
&self.0.srv
|
|
|
|
}
|
|
|
|
|
2018-04-02 02:37:22 +02:00
|
|
|
pub(crate) fn get_resource(&self, idx: usize) -> &Resource {
|
|
|
|
&self.0.patterns[idx]
|
|
|
|
}
|
2018-04-02 19:27:37 +02:00
|
|
|
|
2017-12-07 01:26:27 +01:00
|
|
|
/// Query for matched resource
|
2017-12-26 18:00:45 +01:00
|
|
|
pub fn recognize<S>(&self, req: &mut HttpRequest<S>) -> Option<usize> {
|
2018-02-21 23:31:22 +01:00
|
|
|
if self.0.prefix_len > req.path().len() {
|
2018-04-14 01:02:01 +02:00
|
|
|
return None;
|
2017-12-08 01:22:26 +01:00
|
|
|
}
|
2018-02-21 23:31:22 +01:00
|
|
|
for (idx, pattern) in self.0.patterns.iter().enumerate() {
|
2018-06-19 12:45:26 +02:00
|
|
|
if pattern.match_with_params(req, self.0.prefix_len, true) {
|
|
|
|
let url = req.url().clone();
|
|
|
|
req.match_info_mut().set_url(url);
|
2018-04-02 02:37:22 +02:00
|
|
|
req.set_resource(idx);
|
2018-05-01 07:04:24 +02:00
|
|
|
req.set_prefix_len(self.0.prefix_len as u16);
|
2018-04-14 01:02:01 +02:00
|
|
|
return Some(idx);
|
2018-02-21 23:31:22 +01:00
|
|
|
}
|
2017-12-08 01:22:26 +01:00
|
|
|
}
|
2018-02-21 23:31:22 +01:00
|
|
|
None
|
2017-12-07 01:26:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Check if application contains matching route.
|
2017-12-29 23:04:13 +01:00
|
|
|
///
|
|
|
|
/// This method does not take `prefix` into account.
|
|
|
|
/// For example if prefix is `/test` and router contains route `/name`,
|
|
|
|
/// following path would be recognizable `/test/name` but `has_route()` call
|
|
|
|
/// would return `false`.
|
2017-12-07 01:26:27 +01:00
|
|
|
pub fn has_route(&self, path: &str) -> bool {
|
2018-04-29 18:09:08 +02:00
|
|
|
let path = if path.is_empty() { "/" } else { path };
|
2018-02-21 23:31:22 +01:00
|
|
|
|
|
|
|
for pattern in &self.0.patterns {
|
|
|
|
if pattern.is_match(path) {
|
2018-04-14 01:02:01 +02:00
|
|
|
return true;
|
2017-12-08 01:22:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
2017-12-07 01:26:27 +01:00
|
|
|
}
|
|
|
|
|
2017-12-07 01:34:54 +01:00
|
|
|
/// Build named resource path.
|
|
|
|
///
|
2018-04-14 01:02:01 +02:00
|
|
|
/// Check [`HttpRequest::url_for()`](../struct.HttpRequest.html#method.
|
|
|
|
/// url_for) for detailed information.
|
|
|
|
pub fn resource_path<U, I>(
|
2018-04-29 07:55:47 +02:00
|
|
|
&self, name: &str, elements: U,
|
2018-04-14 01:02:01 +02:00
|
|
|
) -> Result<String, UrlGenerationError>
|
|
|
|
where
|
|
|
|
U: IntoIterator<Item = I>,
|
|
|
|
I: AsRef<str>,
|
2017-12-07 01:26:27 +01:00
|
|
|
{
|
2017-12-08 01:22:26 +01:00
|
|
|
if let Some(pattern) = self.0.named.get(name) {
|
2018-04-02 02:37:22 +02:00
|
|
|
pattern.0.resource_path(self, elements)
|
2017-12-07 01:26:27 +01:00
|
|
|
} else {
|
|
|
|
Err(UrlGenerationError::ResourceNotFound)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-26 18:00:45 +01:00
|
|
|
impl Clone for Router {
|
|
|
|
fn clone(&self) -> Router {
|
2017-12-07 01:26:27 +01:00
|
|
|
Router(Rc::clone(&self.0))
|
|
|
|
}
|
|
|
|
}
|
2017-12-08 01:22:26 +01:00
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
enum PatternElement {
|
|
|
|
Str(String),
|
|
|
|
Var(String),
|
|
|
|
}
|
|
|
|
|
2018-02-22 14:48:18 +01:00
|
|
|
#[derive(Clone, Debug)]
|
2018-02-21 23:31:22 +01:00
|
|
|
enum PatternType {
|
|
|
|
Static(String),
|
2018-05-07 22:50:43 +02:00
|
|
|
Prefix(String),
|
2018-06-22 09:33:32 +02:00
|
|
|
Dynamic(Regex, Vec<Atom>, usize),
|
2018-02-21 23:31:22 +01:00
|
|
|
}
|
|
|
|
|
2018-04-02 19:27:37 +02:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq)]
|
|
|
|
/// Resource type
|
|
|
|
pub enum ResourceType {
|
|
|
|
/// Normal resource
|
|
|
|
Normal,
|
2018-05-06 18:07:30 +02:00
|
|
|
/// Resource for application default handler
|
2018-04-02 19:27:37 +02:00
|
|
|
Default,
|
|
|
|
/// External resource
|
|
|
|
External,
|
|
|
|
/// Unknown resource type
|
|
|
|
Unset,
|
|
|
|
}
|
|
|
|
|
2018-05-06 18:07:30 +02:00
|
|
|
/// Resource type describes an entry in resources table
|
2017-12-08 01:22:26 +01:00
|
|
|
#[derive(Clone)]
|
2018-04-02 02:37:22 +02:00
|
|
|
pub struct Resource {
|
2018-02-21 23:31:22 +01:00
|
|
|
tp: PatternType,
|
2018-04-02 19:27:37 +02:00
|
|
|
rtp: ResourceType,
|
2017-12-08 01:22:26 +01:00
|
|
|
name: String,
|
|
|
|
pattern: String,
|
|
|
|
elements: Vec<PatternElement>,
|
|
|
|
}
|
|
|
|
|
2018-04-02 02:37:22 +02:00
|
|
|
impl Resource {
|
|
|
|
/// Parse path pattern and create new `Resource` instance.
|
2017-12-08 01:22:26 +01:00
|
|
|
///
|
|
|
|
/// Panics if path pattern is wrong.
|
2018-02-21 23:31:22 +01:00
|
|
|
pub fn new(name: &str, path: &str) -> Self {
|
2018-05-07 22:50:43 +02:00
|
|
|
Resource::with_prefix(name, path, "/", false)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parse path pattern and create new `Resource` instance.
|
|
|
|
///
|
|
|
|
/// Use `prefix` type instead of `static`.
|
|
|
|
///
|
|
|
|
/// Panics if path regex pattern is wrong.
|
|
|
|
pub fn prefix(name: &str, path: &str) -> Self {
|
|
|
|
Resource::with_prefix(name, path, "/", true)
|
2018-03-03 05:39:22 +01:00
|
|
|
}
|
|
|
|
|
2018-04-02 02:37:22 +02:00
|
|
|
/// Construct external resource
|
|
|
|
///
|
|
|
|
/// Panics if path pattern is wrong.
|
|
|
|
pub fn external(name: &str, path: &str) -> Self {
|
2018-05-07 22:50:43 +02:00
|
|
|
let mut resource = Resource::with_prefix(name, path, "/", false);
|
2018-04-02 19:27:37 +02:00
|
|
|
resource.rtp = ResourceType::External;
|
2018-04-02 02:37:22 +02:00
|
|
|
resource
|
|
|
|
}
|
|
|
|
|
2018-04-02 19:27:37 +02:00
|
|
|
/// Unset resource type
|
|
|
|
pub(crate) fn unset() -> Resource {
|
|
|
|
Resource {
|
|
|
|
tp: PatternType::Static("".to_owned()),
|
|
|
|
rtp: ResourceType::Unset,
|
|
|
|
name: "".to_owned(),
|
|
|
|
pattern: "".to_owned(),
|
|
|
|
elements: Vec::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-02 02:37:22 +02:00
|
|
|
/// Parse path pattern and create new `Resource` instance with custom prefix
|
2018-05-07 22:50:43 +02:00
|
|
|
pub fn with_prefix(name: &str, path: &str, prefix: &str, for_prefix: bool) -> Self {
|
|
|
|
let (pattern, elements, is_dynamic, len) =
|
|
|
|
Resource::parse(path, prefix, for_prefix);
|
2017-12-08 01:22:26 +01:00
|
|
|
|
2018-02-21 23:31:22 +01:00
|
|
|
let tp = if is_dynamic {
|
|
|
|
let re = match Regex::new(&pattern) {
|
|
|
|
Ok(re) => re,
|
2018-04-14 01:02:01 +02:00
|
|
|
Err(err) => panic!("Wrong path pattern: \"{}\" {}", path, err),
|
2018-02-21 23:31:22 +01:00
|
|
|
};
|
2018-06-22 07:44:38 +02:00
|
|
|
// actix creates one router per thread
|
2018-05-17 21:20:20 +02:00
|
|
|
let names = re
|
|
|
|
.capture_names()
|
2018-06-22 07:44:38 +02:00
|
|
|
.filter_map(|name| {
|
2018-06-22 09:33:32 +02:00
|
|
|
name.map(|name| Atom::from(name))
|
2018-06-22 07:44:38 +02:00
|
|
|
})
|
2018-02-21 23:31:22 +01:00
|
|
|
.collect();
|
2018-05-07 22:50:43 +02:00
|
|
|
PatternType::Dynamic(re, names, len)
|
|
|
|
} else if for_prefix {
|
|
|
|
PatternType::Prefix(pattern.clone())
|
2018-02-21 23:31:22 +01:00
|
|
|
} else {
|
|
|
|
PatternType::Static(pattern.clone())
|
2017-12-08 01:22:26 +01:00
|
|
|
};
|
|
|
|
|
2018-04-02 02:37:22 +02:00
|
|
|
Resource {
|
2018-02-26 23:33:56 +01:00
|
|
|
tp,
|
|
|
|
elements,
|
2017-12-08 01:22:26 +01:00
|
|
|
name: name.into(),
|
2018-04-02 19:27:37 +02:00
|
|
|
rtp: ResourceType::Normal,
|
2018-04-02 02:37:22 +02:00
|
|
|
pattern: path.to_owned(),
|
2017-12-08 01:22:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-02 02:37:22 +02:00
|
|
|
/// Name of the resource
|
2017-12-08 01:22:26 +01:00
|
|
|
pub fn name(&self) -> &str {
|
|
|
|
&self.name
|
|
|
|
}
|
|
|
|
|
2018-04-02 19:27:37 +02:00
|
|
|
/// Resource type
|
|
|
|
pub fn rtype(&self) -> ResourceType {
|
|
|
|
self.rtp
|
|
|
|
}
|
|
|
|
|
2018-04-02 02:37:22 +02:00
|
|
|
/// Path pattern of the resource
|
2017-12-08 01:22:26 +01:00
|
|
|
pub fn pattern(&self) -> &str {
|
|
|
|
&self.pattern
|
|
|
|
}
|
|
|
|
|
2018-06-02 15:51:58 +02:00
|
|
|
/// Is this path a match against this resource?
|
2018-02-21 23:31:22 +01:00
|
|
|
pub fn is_match(&self, path: &str) -> bool {
|
|
|
|
match self.tp {
|
|
|
|
PatternType::Static(ref s) => s == path,
|
2018-05-07 22:50:43 +02:00
|
|
|
PatternType::Dynamic(ref re, _, _) => re.is_match(path),
|
|
|
|
PatternType::Prefix(ref s) => path.starts_with(s),
|
2017-12-08 01:22:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-02 15:51:58 +02:00
|
|
|
/// Are the given path and parameters a match against this resource?
|
2018-06-19 12:45:26 +02:00
|
|
|
pub fn match_with_params<S>(
|
|
|
|
&self, req: &mut HttpRequest<S>, plen: usize, insert: bool,
|
2018-04-14 01:02:01 +02:00
|
|
|
) -> bool {
|
2018-06-19 21:27:41 +02:00
|
|
|
let mut segments: SmallVec<[ParamItem; 5]> = SmallVec::new();
|
2018-06-19 12:45:26 +02:00
|
|
|
|
2018-06-19 21:27:41 +02:00
|
|
|
let names = {
|
2018-06-19 12:45:26 +02:00
|
|
|
let path = &req.path()[plen..];
|
|
|
|
if insert {
|
|
|
|
if path.is_empty() {
|
|
|
|
"/"
|
|
|
|
} else {
|
|
|
|
path
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
path
|
|
|
|
};
|
|
|
|
|
|
|
|
match self.tp {
|
|
|
|
PatternType::Static(ref s) => return s == path,
|
|
|
|
PatternType::Dynamic(ref re, ref names, _) => {
|
|
|
|
if let Some(captures) = re.captures(path) {
|
|
|
|
let mut passed = false;
|
|
|
|
for capture in captures.iter() {
|
|
|
|
if let Some(ref m) = capture {
|
|
|
|
if !passed {
|
|
|
|
passed = true;
|
|
|
|
continue;
|
|
|
|
}
|
2018-06-19 21:27:41 +02:00
|
|
|
segments.push(ParamItem::UrlSegment(
|
2018-06-19 12:45:26 +02:00
|
|
|
(plen + m.start()) as u16,
|
|
|
|
(plen + m.end()) as u16,
|
2018-06-19 21:27:41 +02:00
|
|
|
));
|
2018-02-21 23:31:22 +01:00
|
|
|
}
|
2018-01-03 08:43:17 +01:00
|
|
|
}
|
2018-06-19 21:27:41 +02:00
|
|
|
names
|
2018-06-19 12:45:26 +02:00
|
|
|
} else {
|
|
|
|
return false;
|
2018-01-03 08:43:17 +01:00
|
|
|
}
|
|
|
|
}
|
2018-06-19 12:45:26 +02:00
|
|
|
PatternType::Prefix(ref s) => return path.starts_with(s),
|
2018-02-21 23:31:22 +01:00
|
|
|
}
|
2018-06-19 12:45:26 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
let len = req.path().len();
|
|
|
|
let params = req.match_info_mut();
|
|
|
|
params.set_tail(len as u16);
|
2018-06-22 09:33:32 +02:00
|
|
|
for (idx, segment) in segments.into_iter().enumerate() {
|
|
|
|
params.add(names[idx].clone(), segment);
|
2018-05-07 22:50:43 +02:00
|
|
|
}
|
2018-06-19 12:45:26 +02:00
|
|
|
true
|
2018-05-07 22:50:43 +02:00
|
|
|
}
|
|
|
|
|
2018-06-02 15:51:58 +02:00
|
|
|
/// Is the given path a prefix match and do the parameters match against this resource?
|
2018-06-19 12:45:26 +02:00
|
|
|
pub fn match_prefix_with_params<S>(
|
|
|
|
&self, req: &mut HttpRequest<S>, plen: usize,
|
2018-05-07 22:50:43 +02:00
|
|
|
) -> Option<usize> {
|
2018-06-19 21:27:41 +02:00
|
|
|
let mut segments: SmallVec<[ParamItem; 5]> = SmallVec::new();
|
2018-06-19 12:45:26 +02:00
|
|
|
|
2018-06-19 21:27:41 +02:00
|
|
|
let (names, tail_len) = {
|
2018-06-19 12:45:26 +02:00
|
|
|
let path = &req.path()[plen..];
|
|
|
|
let path = if path.is_empty() { "/" } else { path };
|
|
|
|
|
|
|
|
match self.tp {
|
|
|
|
PatternType::Static(ref s) => if s == path {
|
|
|
|
return Some(s.len());
|
|
|
|
} else {
|
|
|
|
return None;
|
|
|
|
},
|
|
|
|
PatternType::Dynamic(ref re, ref names, len) => {
|
|
|
|
if let Some(captures) = re.captures(path) {
|
|
|
|
let mut pos = 0;
|
|
|
|
let mut passed = false;
|
|
|
|
for capture in captures.iter() {
|
|
|
|
if let Some(ref m) = capture {
|
|
|
|
if !passed {
|
|
|
|
passed = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-06-19 21:27:41 +02:00
|
|
|
segments.push(ParamItem::UrlSegment(
|
2018-06-19 12:45:26 +02:00
|
|
|
(plen + m.start()) as u16,
|
|
|
|
(plen + m.end()) as u16,
|
2018-06-19 21:27:41 +02:00
|
|
|
));
|
2018-06-19 12:45:26 +02:00
|
|
|
pos = m.end();
|
2018-05-07 22:50:43 +02:00
|
|
|
}
|
|
|
|
}
|
2018-06-19 21:27:41 +02:00
|
|
|
(names, pos + len)
|
2018-06-19 12:45:26 +02:00
|
|
|
} else {
|
|
|
|
return None;
|
2018-05-07 22:50:43 +02:00
|
|
|
}
|
|
|
|
}
|
2018-06-19 12:45:26 +02:00
|
|
|
PatternType::Prefix(ref s) => {
|
|
|
|
return if path == s {
|
|
|
|
Some(s.len())
|
|
|
|
} else if path.starts_with(s)
|
|
|
|
&& (s.ends_with('/')
|
|
|
|
|| path.split_at(s.len()).1.starts_with('/'))
|
|
|
|
{
|
|
|
|
if s.ends_with('/') {
|
|
|
|
Some(s.len() - 1)
|
|
|
|
} else {
|
|
|
|
Some(s.len())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2018-05-23 22:21:29 +02:00
|
|
|
}
|
2018-06-19 12:45:26 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let params = req.match_info_mut();
|
|
|
|
params.set_tail(tail_len as u16);
|
2018-06-22 09:33:32 +02:00
|
|
|
for (idx, segment) in segments.into_iter().enumerate() {
|
|
|
|
params.add(names[idx].clone(), segment);
|
2018-01-03 08:43:17 +01:00
|
|
|
}
|
2018-06-19 12:45:26 +02:00
|
|
|
Some(tail_len)
|
2018-01-03 08:43:17 +01:00
|
|
|
}
|
|
|
|
|
2018-05-06 18:07:30 +02:00
|
|
|
/// Build resource path.
|
2018-04-14 01:02:01 +02:00
|
|
|
pub fn resource_path<U, I>(
|
2018-04-29 07:55:47 +02:00
|
|
|
&self, router: &Router, elements: U,
|
2018-04-14 01:02:01 +02:00
|
|
|
) -> Result<String, UrlGenerationError>
|
|
|
|
where
|
|
|
|
U: IntoIterator<Item = I>,
|
|
|
|
I: AsRef<str>,
|
2017-12-08 01:22:26 +01:00
|
|
|
{
|
2018-06-02 20:44:09 +02:00
|
|
|
let mut path = match self.tp {
|
|
|
|
PatternType::Prefix(ref p) => p.to_owned(),
|
|
|
|
PatternType::Static(ref p) => p.to_owned(),
|
|
|
|
PatternType::Dynamic(..) => {
|
|
|
|
let mut path = String::new();
|
|
|
|
let mut iter = elements.into_iter();
|
|
|
|
for el in &self.elements {
|
|
|
|
match *el {
|
|
|
|
PatternElement::Str(ref s) => path.push_str(s),
|
|
|
|
PatternElement::Var(_) => {
|
|
|
|
if let Some(val) = iter.next() {
|
|
|
|
path.push_str(val.as_ref())
|
|
|
|
} else {
|
|
|
|
return Err(UrlGenerationError::NotEnoughElements);
|
|
|
|
}
|
|
|
|
}
|
2017-12-08 01:22:26 +01:00
|
|
|
}
|
|
|
|
}
|
2018-06-02 20:44:09 +02:00
|
|
|
path
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if self.rtp != ResourceType::External {
|
|
|
|
let prefix = router.prefix();
|
|
|
|
if prefix.ends_with('/') {
|
|
|
|
if path.starts_with('/') {
|
|
|
|
path.insert_str(0, &prefix[..prefix.len() - 1]);
|
|
|
|
} else {
|
|
|
|
path.insert_str(0, prefix);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if !path.starts_with('/') {
|
|
|
|
path.insert(0, '/');
|
|
|
|
}
|
|
|
|
path.insert_str(0, prefix);
|
2017-12-08 01:22:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(path)
|
|
|
|
}
|
|
|
|
|
2018-05-07 22:50:43 +02:00
|
|
|
fn parse(
|
|
|
|
pattern: &str, prefix: &str, for_prefix: bool,
|
|
|
|
) -> (String, Vec<PatternElement>, bool, usize) {
|
2017-12-08 01:22:26 +01:00
|
|
|
const DEFAULT_PATTERN: &str = "[^/]+";
|
|
|
|
|
2018-03-03 05:39:22 +01:00
|
|
|
let mut re1 = String::from("^") + prefix;
|
|
|
|
let mut re2 = String::from(prefix);
|
2017-12-08 01:22:26 +01:00
|
|
|
let mut el = String::new();
|
|
|
|
let mut in_param = false;
|
|
|
|
let mut in_param_pattern = false;
|
|
|
|
let mut param_name = String::new();
|
|
|
|
let mut param_pattern = String::from(DEFAULT_PATTERN);
|
2018-02-21 23:31:22 +01:00
|
|
|
let mut is_dynamic = false;
|
2017-12-08 01:22:26 +01:00
|
|
|
let mut elems = Vec::new();
|
2018-05-07 22:50:43 +02:00
|
|
|
let mut len = 0;
|
2017-12-08 01:22:26 +01:00
|
|
|
|
|
|
|
for (index, ch) in pattern.chars().enumerate() {
|
|
|
|
// All routes must have a leading slash so its optional to have one
|
|
|
|
if index == 0 && ch == '/' {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if in_param {
|
|
|
|
// In parameter segment: `{....}`
|
|
|
|
if ch == '}' {
|
|
|
|
elems.push(PatternElement::Var(param_name.clone()));
|
2018-02-22 14:48:18 +01:00
|
|
|
re1.push_str(&format!(r"(?P<{}>{})", ¶m_name, ¶m_pattern));
|
2017-12-08 01:22:26 +01:00
|
|
|
|
|
|
|
param_name.clear();
|
|
|
|
param_pattern = String::from(DEFAULT_PATTERN);
|
|
|
|
|
2018-05-07 22:50:43 +02:00
|
|
|
len = 0;
|
2017-12-08 01:22:26 +01:00
|
|
|
in_param_pattern = false;
|
|
|
|
in_param = false;
|
|
|
|
} else if ch == ':' {
|
|
|
|
// The parameter name has been determined; custom pattern land
|
|
|
|
in_param_pattern = true;
|
|
|
|
param_pattern.clear();
|
|
|
|
} else if in_param_pattern {
|
|
|
|
// Ignore leading whitespace for pattern
|
|
|
|
if !(ch == ' ' && param_pattern.is_empty()) {
|
|
|
|
param_pattern.push(ch);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
param_name.push(ch);
|
|
|
|
}
|
|
|
|
} else if ch == '{' {
|
|
|
|
in_param = true;
|
2018-02-21 23:31:22 +01:00
|
|
|
is_dynamic = true;
|
2017-12-08 01:22:26 +01:00
|
|
|
elems.push(PatternElement::Str(el.clone()));
|
|
|
|
el.clear();
|
|
|
|
} else {
|
2018-02-22 14:48:18 +01:00
|
|
|
re1.push_str(escape(&ch.to_string()).as_str());
|
|
|
|
re2.push(ch);
|
2017-12-08 01:22:26 +01:00
|
|
|
el.push(ch);
|
2018-05-07 22:50:43 +02:00
|
|
|
len += 1;
|
2017-12-08 01:22:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-02 20:44:09 +02:00
|
|
|
if !el.is_empty() {
|
|
|
|
elems.push(PatternElement::Str(el.clone()));
|
|
|
|
}
|
|
|
|
|
2018-02-22 14:48:18 +01:00
|
|
|
let re = if is_dynamic {
|
2018-05-07 22:50:43 +02:00
|
|
|
if !for_prefix {
|
|
|
|
re1.push('$');
|
|
|
|
}
|
2018-02-22 14:48:18 +01:00
|
|
|
re1
|
|
|
|
} else {
|
|
|
|
re2
|
|
|
|
};
|
2018-05-07 22:50:43 +02:00
|
|
|
(re, elems, is_dynamic, len)
|
2017-12-08 01:22:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-02 02:37:22 +02:00
|
|
|
impl PartialEq for Resource {
|
|
|
|
fn eq(&self, other: &Resource) -> bool {
|
2017-12-08 01:22:26 +01:00
|
|
|
self.pattern == other.pattern
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-02 02:37:22 +02:00
|
|
|
impl Eq for Resource {}
|
2017-12-08 01:22:26 +01:00
|
|
|
|
2018-04-02 02:37:22 +02:00
|
|
|
impl Hash for Resource {
|
2017-12-08 01:22:26 +01:00
|
|
|
fn hash<H: Hasher>(&self, state: &mut H) {
|
|
|
|
self.pattern.hash(state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2017-12-29 23:04:13 +01:00
|
|
|
use test::TestRequest;
|
2017-12-08 01:22:26 +01:00
|
|
|
|
|
|
|
#[test]
|
2018-06-02 20:44:09 +02:00
|
|
|
fn test_recognizer10() {
|
2018-02-22 14:48:18 +01:00
|
|
|
let routes = vec![
|
2018-05-17 21:20:20 +02:00
|
|
|
(Resource::new("", "/name"), Some(ResourceHandler::default())),
|
2018-04-29 18:09:08 +02:00
|
|
|
(
|
|
|
|
Resource::new("", "/name/{val}"),
|
|
|
|
Some(ResourceHandler::default()),
|
|
|
|
),
|
2018-04-14 01:02:01 +02:00
|
|
|
(
|
|
|
|
Resource::new("", "/name/{val}/index.html"),
|
|
|
|
Some(ResourceHandler::default()),
|
|
|
|
),
|
2018-04-29 18:09:08 +02:00
|
|
|
(
|
|
|
|
Resource::new("", "/file/{file}.{ext}"),
|
|
|
|
Some(ResourceHandler::default()),
|
|
|
|
),
|
2018-04-14 01:02:01 +02:00
|
|
|
(
|
|
|
|
Resource::new("", "/v{val}/{val2}/index.html"),
|
|
|
|
Some(ResourceHandler::default()),
|
|
|
|
),
|
2018-04-29 18:09:08 +02:00
|
|
|
(
|
|
|
|
Resource::new("", "/v/{tail:.*}"),
|
|
|
|
Some(ResourceHandler::default()),
|
|
|
|
),
|
2018-06-02 20:44:09 +02:00
|
|
|
(
|
|
|
|
Resource::new("", "/test2/{test}.html"),
|
|
|
|
Some(ResourceHandler::default()),
|
|
|
|
),
|
2018-04-29 18:09:08 +02:00
|
|
|
(
|
|
|
|
Resource::new("", "{test}/index.html"),
|
|
|
|
Some(ResourceHandler::default()),
|
|
|
|
),
|
2018-04-14 01:02:01 +02:00
|
|
|
];
|
2017-12-29 20:49:36 +01:00
|
|
|
let (rec, _) = Router::new::<()>("", ServerSettings::default(), routes);
|
2017-12-08 01:22:26 +01:00
|
|
|
|
2017-12-29 23:04:13 +01:00
|
|
|
let mut req = TestRequest::with_uri("/name").finish();
|
2018-02-22 14:48:18 +01:00
|
|
|
assert_eq!(rec.recognize(&mut req), Some(0));
|
2017-12-08 01:22:26 +01:00
|
|
|
assert!(req.match_info().is_empty());
|
|
|
|
|
2017-12-29 23:04:13 +01:00
|
|
|
let mut req = TestRequest::with_uri("/name/value").finish();
|
2018-02-22 14:48:18 +01:00
|
|
|
assert_eq!(rec.recognize(&mut req), Some(1));
|
2017-12-08 01:22:26 +01:00
|
|
|
assert_eq!(req.match_info().get("val").unwrap(), "value");
|
|
|
|
assert_eq!(&req.match_info()["val"], "value");
|
|
|
|
|
2017-12-29 23:04:13 +01:00
|
|
|
let mut req = TestRequest::with_uri("/name/value2/index.html").finish();
|
2018-02-22 14:48:18 +01:00
|
|
|
assert_eq!(rec.recognize(&mut req), Some(2));
|
2017-12-08 01:22:26 +01:00
|
|
|
assert_eq!(req.match_info().get("val").unwrap(), "value2");
|
|
|
|
|
2018-02-19 23:57:57 +01:00
|
|
|
let mut req = TestRequest::with_uri("/file/file.gz").finish();
|
2018-02-22 14:48:18 +01:00
|
|
|
assert_eq!(rec.recognize(&mut req), Some(3));
|
2018-02-19 23:57:57 +01:00
|
|
|
assert_eq!(req.match_info().get("file").unwrap(), "file");
|
|
|
|
assert_eq!(req.match_info().get("ext").unwrap(), "gz");
|
|
|
|
|
2017-12-29 23:04:13 +01:00
|
|
|
let mut req = TestRequest::with_uri("/vtest/ttt/index.html").finish();
|
2018-02-22 14:48:18 +01:00
|
|
|
assert_eq!(rec.recognize(&mut req), Some(4));
|
2017-12-08 01:22:26 +01:00
|
|
|
assert_eq!(req.match_info().get("val").unwrap(), "test");
|
|
|
|
assert_eq!(req.match_info().get("val2").unwrap(), "ttt");
|
|
|
|
|
2017-12-29 23:04:13 +01:00
|
|
|
let mut req = TestRequest::with_uri("/v/blah-blah/index.html").finish();
|
2018-02-22 14:48:18 +01:00
|
|
|
assert_eq!(rec.recognize(&mut req), Some(5));
|
2018-04-29 18:09:08 +02:00
|
|
|
assert_eq!(
|
|
|
|
req.match_info().get("tail").unwrap(),
|
|
|
|
"blah-blah/index.html"
|
|
|
|
);
|
2017-12-08 01:22:26 +01:00
|
|
|
|
2018-06-02 20:44:09 +02:00
|
|
|
let mut req = TestRequest::with_uri("/test2/index.html").finish();
|
2018-02-22 14:48:18 +01:00
|
|
|
assert_eq!(rec.recognize(&mut req), Some(6));
|
2018-06-02 20:44:09 +02:00
|
|
|
assert_eq!(req.match_info().get("test").unwrap(), "index");
|
|
|
|
|
|
|
|
let mut req = TestRequest::with_uri("/bbb/index.html").finish();
|
|
|
|
assert_eq!(rec.recognize(&mut req), Some(7));
|
2017-12-08 01:22:26 +01:00
|
|
|
assert_eq!(req.match_info().get("test").unwrap(), "bbb");
|
|
|
|
}
|
|
|
|
|
2018-02-22 14:48:18 +01:00
|
|
|
#[test]
|
|
|
|
fn test_recognizer_2() {
|
|
|
|
let routes = vec![
|
2018-04-29 18:09:08 +02:00
|
|
|
(
|
|
|
|
Resource::new("", "/index.json"),
|
|
|
|
Some(ResourceHandler::default()),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
Resource::new("", "/{source}.json"),
|
|
|
|
Some(ResourceHandler::default()),
|
|
|
|
),
|
2018-04-14 01:02:01 +02:00
|
|
|
];
|
2018-02-22 14:48:18 +01:00
|
|
|
let (rec, _) = Router::new::<()>("", ServerSettings::default(), routes);
|
|
|
|
|
|
|
|
let mut req = TestRequest::with_uri("/index.json").finish();
|
|
|
|
assert_eq!(rec.recognize(&mut req), Some(0));
|
|
|
|
|
|
|
|
let mut req = TestRequest::with_uri("/test.json").finish();
|
|
|
|
assert_eq!(rec.recognize(&mut req), Some(1));
|
|
|
|
}
|
|
|
|
|
2017-12-29 23:04:13 +01:00
|
|
|
#[test]
|
|
|
|
fn test_recognizer_with_prefix() {
|
2018-02-22 14:48:18 +01:00
|
|
|
let routes = vec![
|
2018-05-17 21:20:20 +02:00
|
|
|
(Resource::new("", "/name"), Some(ResourceHandler::default())),
|
2018-04-29 18:09:08 +02:00
|
|
|
(
|
|
|
|
Resource::new("", "/name/{val}"),
|
|
|
|
Some(ResourceHandler::default()),
|
|
|
|
),
|
2018-04-14 01:02:01 +02:00
|
|
|
];
|
2017-12-29 23:04:13 +01:00
|
|
|
let (rec, _) = Router::new::<()>("/test", ServerSettings::default(), routes);
|
|
|
|
|
|
|
|
let mut req = TestRequest::with_uri("/name").finish();
|
|
|
|
assert!(rec.recognize(&mut req).is_none());
|
|
|
|
|
|
|
|
let mut req = TestRequest::with_uri("/test/name").finish();
|
2018-02-22 14:48:18 +01:00
|
|
|
assert_eq!(rec.recognize(&mut req), Some(0));
|
2017-12-29 23:04:13 +01:00
|
|
|
|
|
|
|
let mut req = TestRequest::with_uri("/test/name/value").finish();
|
2018-02-22 14:48:18 +01:00
|
|
|
assert_eq!(rec.recognize(&mut req), Some(1));
|
2017-12-29 23:04:13 +01:00
|
|
|
assert_eq!(req.match_info().get("val").unwrap(), "value");
|
|
|
|
assert_eq!(&req.match_info()["val"], "value");
|
|
|
|
|
|
|
|
// same patterns
|
2018-02-22 14:48:18 +01:00
|
|
|
let routes = vec![
|
2018-05-17 21:20:20 +02:00
|
|
|
(Resource::new("", "/name"), Some(ResourceHandler::default())),
|
2018-04-29 18:09:08 +02:00
|
|
|
(
|
|
|
|
Resource::new("", "/name/{val}"),
|
|
|
|
Some(ResourceHandler::default()),
|
|
|
|
),
|
2018-04-14 01:02:01 +02:00
|
|
|
];
|
2017-12-29 23:04:13 +01:00
|
|
|
let (rec, _) = Router::new::<()>("/test2", ServerSettings::default(), routes);
|
|
|
|
|
|
|
|
let mut req = TestRequest::with_uri("/name").finish();
|
|
|
|
assert!(rec.recognize(&mut req).is_none());
|
|
|
|
let mut req = TestRequest::with_uri("/test2/name").finish();
|
2018-02-22 14:48:18 +01:00
|
|
|
assert_eq!(rec.recognize(&mut req), Some(0));
|
|
|
|
let mut req = TestRequest::with_uri("/test2/name-test").finish();
|
|
|
|
assert!(rec.recognize(&mut req).is_none());
|
|
|
|
let mut req = TestRequest::with_uri("/test2/name/ttt").finish();
|
|
|
|
assert_eq!(rec.recognize(&mut req), Some(1));
|
|
|
|
assert_eq!(&req.match_info()["val"], "ttt");
|
2017-12-29 23:04:13 +01:00
|
|
|
}
|
|
|
|
|
2017-12-08 01:22:26 +01:00
|
|
|
#[test]
|
|
|
|
fn test_parse_static() {
|
2018-04-02 02:37:22 +02:00
|
|
|
let re = Resource::new("test", "/");
|
2017-12-08 01:22:26 +01:00
|
|
|
assert!(re.is_match("/"));
|
|
|
|
assert!(!re.is_match("/a"));
|
|
|
|
|
2018-04-02 02:37:22 +02:00
|
|
|
let re = Resource::new("test", "/name");
|
2017-12-08 01:22:26 +01:00
|
|
|
assert!(re.is_match("/name"));
|
|
|
|
assert!(!re.is_match("/name1"));
|
|
|
|
assert!(!re.is_match("/name/"));
|
|
|
|
assert!(!re.is_match("/name~"));
|
|
|
|
|
2018-04-02 02:37:22 +02:00
|
|
|
let re = Resource::new("test", "/name/");
|
2017-12-08 01:22:26 +01:00
|
|
|
assert!(re.is_match("/name/"));
|
|
|
|
assert!(!re.is_match("/name"));
|
|
|
|
assert!(!re.is_match("/name/gs"));
|
|
|
|
|
2018-04-02 02:37:22 +02:00
|
|
|
let re = Resource::new("test", "/user/profile");
|
2017-12-08 01:22:26 +01:00
|
|
|
assert!(re.is_match("/user/profile"));
|
|
|
|
assert!(!re.is_match("/user/profile/profile"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parse_param() {
|
2018-04-02 02:37:22 +02:00
|
|
|
let re = Resource::new("test", "/user/{id}");
|
2017-12-08 01:22:26 +01: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"));
|
|
|
|
|
2018-06-19 12:45:26 +02:00
|
|
|
let mut req = TestRequest::with_uri("/user/profile").finish();
|
|
|
|
let url = req.url().clone();
|
|
|
|
req.match_info_mut().set_url(url);
|
|
|
|
assert!(re.match_with_params(&mut req, 0, true));
|
2018-02-21 23:31:22 +01:00
|
|
|
assert_eq!(req.match_info().get("id").unwrap(), "profile");
|
2017-12-08 01:22:26 +01:00
|
|
|
|
2018-06-19 12:45:26 +02:00
|
|
|
let mut req = TestRequest::with_uri("/user/1245125").finish();
|
|
|
|
let url = req.url().clone();
|
|
|
|
req.match_info_mut().set_url(url);
|
|
|
|
assert!(re.match_with_params(&mut req, 0, true));
|
2018-02-21 23:31:22 +01:00
|
|
|
assert_eq!(req.match_info().get("id").unwrap(), "1245125");
|
2017-12-08 01:22:26 +01:00
|
|
|
|
2018-04-02 02:37:22 +02:00
|
|
|
let re = Resource::new("test", "/v{version}/resource/{id}");
|
2017-12-08 01:22:26 +01:00
|
|
|
assert!(re.is_match("/v1/resource/320120"));
|
|
|
|
assert!(!re.is_match("/v/resource/1"));
|
|
|
|
assert!(!re.is_match("/resource"));
|
|
|
|
|
2018-06-19 12:45:26 +02:00
|
|
|
let mut req = TestRequest::with_uri("/v151/resource/adahg32").finish();
|
|
|
|
let url = req.url().clone();
|
|
|
|
req.match_info_mut().set_url(url);
|
|
|
|
assert!(re.match_with_params(&mut req, 0, true));
|
2018-02-21 23:31:22 +01:00
|
|
|
assert_eq!(req.match_info().get("version").unwrap(), "151");
|
|
|
|
assert_eq!(req.match_info().get("id").unwrap(), "adahg32");
|
2017-12-08 01:22:26 +01:00
|
|
|
}
|
2018-04-02 02:37:22 +02:00
|
|
|
|
2018-05-07 22:50:43 +02:00
|
|
|
#[test]
|
|
|
|
fn test_resource_prefix() {
|
|
|
|
let re = Resource::prefix("test", "/name");
|
|
|
|
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 re = Resource::prefix("test", "/name/");
|
|
|
|
assert!(re.is_match("/name/"));
|
|
|
|
assert!(re.is_match("/name/gs"));
|
|
|
|
assert!(!re.is_match("/name"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_reousrce_prefix_dynamic() {
|
|
|
|
let re = Resource::prefix("test", "/{name}/");
|
|
|
|
assert!(re.is_match("/name/"));
|
|
|
|
assert!(re.is_match("/name/gs"));
|
|
|
|
assert!(!re.is_match("/name"));
|
|
|
|
|
|
|
|
let mut req = TestRequest::with_uri("/test2/").finish();
|
2018-06-19 12:45:26 +02:00
|
|
|
let url = req.url().clone();
|
|
|
|
req.match_info_mut().set_url(url);
|
|
|
|
assert!(re.match_with_params(&mut req, 0, true));
|
2018-05-07 22:50:43 +02:00
|
|
|
assert_eq!(&req.match_info()["name"], "test2");
|
|
|
|
|
|
|
|
let mut req =
|
|
|
|
TestRequest::with_uri("/test2/subpath1/subpath2/index.html").finish();
|
2018-06-19 12:45:26 +02:00
|
|
|
let url = req.url().clone();
|
|
|
|
req.match_info_mut().set_url(url);
|
|
|
|
assert!(re.match_with_params(&mut req, 0, true));
|
2018-05-07 22:50:43 +02:00
|
|
|
assert_eq!(&req.match_info()["name"], "test2");
|
|
|
|
}
|
|
|
|
|
2018-04-02 02:37:22 +02:00
|
|
|
#[test]
|
|
|
|
fn test_request_resource() {
|
|
|
|
let routes = vec![
|
2018-04-29 18:09:08 +02:00
|
|
|
(
|
|
|
|
Resource::new("r1", "/index.json"),
|
|
|
|
Some(ResourceHandler::default()),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
Resource::new("r2", "/test.json"),
|
|
|
|
Some(ResourceHandler::default()),
|
|
|
|
),
|
2018-04-14 01:02:01 +02:00
|
|
|
];
|
2018-04-02 02:37:22 +02:00
|
|
|
let (router, _) = Router::new::<()>("", ServerSettings::default(), routes);
|
|
|
|
|
2018-04-14 01:02:01 +02:00
|
|
|
let mut req =
|
|
|
|
TestRequest::with_uri("/index.json").finish_with_router(router.clone());
|
2018-04-02 02:37:22 +02:00
|
|
|
assert_eq!(router.recognize(&mut req), Some(0));
|
|
|
|
let resource = req.resource();
|
|
|
|
assert_eq!(resource.name(), "r1");
|
|
|
|
|
2018-04-14 01:02:01 +02:00
|
|
|
let mut req =
|
|
|
|
TestRequest::with_uri("/test.json").finish_with_router(router.clone());
|
2018-04-02 02:37:22 +02:00
|
|
|
assert_eq!(router.recognize(&mut req), Some(1));
|
|
|
|
let resource = req.resource();
|
|
|
|
assert_eq!(resource.name(), "r2");
|
|
|
|
}
|
2017-12-08 01:22:26 +01:00
|
|
|
}
|