1
0
mirror of https://github.com/fafhrd91/actix-net synced 2025-01-19 03:44:40 +01:00

Allow to reset Path instance; export Quoter type

This commit is contained in:
Nikolay Kim 2019-04-07 22:48:18 -07:00
parent a60112c71e
commit 51c4dfe5cb
6 changed files with 43 additions and 9 deletions

View File

@ -1,5 +1,11 @@
# Changes # Changes
## [0.1.2] - 2019-04-07
* Export `Quoter` type
* Allow to reset `Path` instance
## [0.1.1] - 2019-04-03 ## [0.1.1] - 2019-04-03
* Get dynamic segment by name instead of iterator. * Get dynamic segment by name instead of iterator.

View File

@ -1,6 +1,6 @@
[package] [package]
name = "actix-router" name = "actix-router"
version = "0.1.1" version = "0.1.2"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"] authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Path router" description = "Path router"
keywords = ["actix"] keywords = ["actix"]

View File

@ -39,7 +39,7 @@ impl<T: AsRef<[u8]>> ResourcePath for string::String<T> {
mod url; mod url;
#[cfg(feature = "http")] #[cfg(feature = "http")]
pub use self::url::Url; pub use self::url::{Quoter, Url};
#[cfg(feature = "http")] #[cfg(feature = "http")]
mod http_support { mod http_support {

View File

@ -51,16 +51,19 @@ impl<T: ResourcePath> Path<T> {
} }
} }
#[inline]
/// Get reference to inner path instance /// Get reference to inner path instance
pub fn get_ref(&self) -> &T { pub fn get_ref(&self) -> &T {
&self.path &self.path
} }
#[inline]
/// Get mutable reference to inner path instance /// Get mutable reference to inner path instance
pub fn get_mut(&mut self) -> &mut T { pub fn get_mut(&mut self) -> &mut T {
&mut self.path &mut self.path
} }
#[inline]
/// Path /// Path
pub fn path(&self) -> &str { pub fn path(&self) -> &str {
let skip = self.skip as usize; let skip = self.skip as usize;
@ -72,13 +75,22 @@ impl<T: ResourcePath> Path<T> {
} }
} }
/// Reset inner path #[inline]
/// Set new path
pub fn set(&mut self, path: T) { pub fn set(&mut self, path: T) {
self.skip = 0; self.skip = 0;
self.path = path; self.path = path;
self.segments.clear(); self.segments.clear();
} }
#[inline]
/// Reset state
pub fn reset(&mut self) {
self.skip = 0;
self.segments.clear();
}
#[inline]
/// Skip first `n` chars in path /// Skip first `n` chars in path
pub fn skip(&mut self, n: u16) { pub fn skip(&mut self, n: u16) {
self.skip = self.skip + n; self.skip = self.skip + n;
@ -99,11 +111,13 @@ impl<T: ResourcePath> Path<T> {
.push((Rc::new(name.to_string()), PathItem::Static(value))); .push((Rc::new(name.to_string()), PathItem::Static(value)));
} }
#[inline]
/// Check if there are any matched patterns /// Check if there are any matched patterns
pub fn is_empty(&self) -> bool { pub fn is_empty(&self) -> bool {
self.segments.is_empty() self.segments.is_empty()
} }
#[inline]
/// Check number of extracted parameters /// Check number of extracted parameters
pub fn len(&self) -> usize { pub fn len(&self) -> usize {
self.segments.len() self.segments.len()

View File

@ -118,6 +118,7 @@ impl ResourceDef {
&self.pattern &self.pattern
} }
#[inline]
/// Check if path matchs this pattern? /// Check if path matchs this pattern?
pub fn is_match(&self, path: &str) -> bool { pub fn is_match(&self, path: &str) -> bool {
match self.tp { match self.tp {

View File

@ -1,5 +1,3 @@
use std::rc::Rc;
use crate::ResourcePath; use crate::ResourcePath;
#[allow(dead_code)] #[allow(dead_code)]
@ -39,7 +37,7 @@ thread_local! {
#[derive(Default, Clone, Debug)] #[derive(Default, Clone, Debug)]
pub struct Url { pub struct Url {
uri: http::Uri, uri: http::Uri,
path: Option<Rc<String>>, path: Option<String>,
} }
impl Url { impl Url {
@ -49,6 +47,13 @@ impl Url {
Url { uri, path } Url { uri, path }
} }
pub fn with_quoter(uri: http::Uri, quoter: &Quoter) -> Url {
Url {
path: quoter.requote(uri.path().as_bytes()),
uri,
}
}
pub fn uri(&self) -> &http::Uri { pub fn uri(&self) -> &http::Uri {
&self.uri &self.uri
} }
@ -61,19 +66,27 @@ impl Url {
} }
} }
#[inline]
pub fn update(&mut self, uri: &http::Uri) { pub fn update(&mut self, uri: &http::Uri) {
self.uri = uri.clone(); self.uri = uri.clone();
self.path = DEFAULT_QUOTER.with(|q| q.requote(uri.path().as_bytes())); self.path = DEFAULT_QUOTER.with(|q| q.requote(uri.path().as_bytes()));
} }
#[inline]
pub fn update_with_quoter(&mut self, uri: &http::Uri, quoter: &Quoter) {
self.uri = uri.clone();
self.path = quoter.requote(uri.path().as_bytes());
}
} }
impl ResourcePath for Url { impl ResourcePath for Url {
#[inline]
fn path(&self) -> &str { fn path(&self) -> &str {
self.path() self.path()
} }
} }
pub(crate) struct Quoter { pub struct Quoter {
safe_table: [u8; 16], safe_table: [u8; 16],
protected_table: [u8; 16], protected_table: [u8; 16],
} }
@ -108,7 +121,7 @@ impl Quoter {
q q
} }
pub fn requote(&self, val: &[u8]) -> Option<Rc<String>> { pub fn requote(&self, val: &[u8]) -> Option<String> {
let mut has_pct = 0; let mut has_pct = 0;
let mut pct = [b'%', 0, 0]; let mut pct = [b'%', 0, 0];
let mut idx = 0; let mut idx = 0;
@ -160,7 +173,7 @@ impl Quoter {
if let Some(data) = cloned { if let Some(data) = cloned {
// Unsafe: we get data from http::Uri, which does utf-8 checks already // Unsafe: we get data from http::Uri, which does utf-8 checks already
// this code only decodes valid pct encoded values // this code only decodes valid pct encoded values
Some(Rc::new(unsafe { String::from_utf8_unchecked(data) })) Some(unsafe { String::from_utf8_unchecked(data) })
} else { } else {
None None
} }