Skip to content

Commit

Permalink
Allow to reset Path instance; export Quoter type
Browse files Browse the repository at this point in the history
  • Loading branch information
fafhrd91 committed Apr 8, 2019
1 parent a60112c commit 51c4dfe
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 9 deletions.
6 changes: 6 additions & 0 deletions router/CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changes

## [0.1.2] - 2019-04-07

* Export `Quoter` type

* Allow to reset `Path` instance

## [0.1.1] - 2019-04-03

* Get dynamic segment by name instead of iterator.
Expand Down
2 changes: 1 addition & 1 deletion router/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "actix-router"
version = "0.1.1"
version = "0.1.2"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Path router"
keywords = ["actix"]
Expand Down
2 changes: 1 addition & 1 deletion router/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl<T: AsRef<[u8]>> ResourcePath for string::String<T> {
mod url;

#[cfg(feature = "http")]
pub use self::url::Url;
pub use self::url::{Quoter, Url};

#[cfg(feature = "http")]
mod http_support {
Expand Down
16 changes: 15 additions & 1 deletion router/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,19 @@ impl<T: ResourcePath> Path<T> {
}
}

#[inline]
/// Get reference to inner path instance
pub fn get_ref(&self) -> &T {
&self.path
}

#[inline]
/// Get mutable reference to inner path instance
pub fn get_mut(&mut self) -> &mut T {
&mut self.path
}

#[inline]
/// Path
pub fn path(&self) -> &str {
let skip = self.skip as usize;
Expand All @@ -72,13 +75,22 @@ impl<T: ResourcePath> Path<T> {
}
}

/// Reset inner path
#[inline]
/// Set new path
pub fn set(&mut self, path: T) {
self.skip = 0;
self.path = path;
self.segments.clear();
}

#[inline]
/// Reset state
pub fn reset(&mut self) {
self.skip = 0;
self.segments.clear();
}

#[inline]
/// Skip first `n` chars in path
pub fn skip(&mut self, n: u16) {
self.skip = self.skip + n;
Expand All @@ -99,11 +111,13 @@ impl<T: ResourcePath> Path<T> {
.push((Rc::new(name.to_string()), PathItem::Static(value)));
}

#[inline]
/// Check if there are any matched patterns
pub fn is_empty(&self) -> bool {
self.segments.is_empty()
}

#[inline]
/// Check number of extracted parameters
pub fn len(&self) -> usize {
self.segments.len()
Expand Down
1 change: 1 addition & 0 deletions router/src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ impl ResourceDef {
&self.pattern
}

#[inline]
/// Check if path matchs this pattern?
pub fn is_match(&self, path: &str) -> bool {
match self.tp {
Expand Down
25 changes: 19 additions & 6 deletions router/src/url.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::rc::Rc;

use crate::ResourcePath;

#[allow(dead_code)]
Expand Down Expand Up @@ -39,7 +37,7 @@ thread_local! {
#[derive(Default, Clone, Debug)]
pub struct Url {
uri: http::Uri,
path: Option<Rc<String>>,
path: Option<String>,
}

impl Url {
Expand All @@ -49,6 +47,13 @@ impl Url {
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 {
&self.uri
}
Expand All @@ -61,19 +66,27 @@ impl Url {
}
}

#[inline]
pub fn update(&mut self, uri: &http::Uri) {
self.uri = uri.clone();
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 {
#[inline]
fn path(&self) -> &str {
self.path()
}
}

pub(crate) struct Quoter {
pub struct Quoter {
safe_table: [u8; 16],
protected_table: [u8; 16],
}
Expand Down Expand Up @@ -108,7 +121,7 @@ impl Quoter {
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 pct = [b'%', 0, 0];
let mut idx = 0;
Expand Down Expand Up @@ -160,7 +173,7 @@ impl Quoter {
if let Some(data) = cloned {
// Unsafe: we get data from http::Uri, which does utf-8 checks already
// 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 {
None
}
Expand Down

0 comments on commit 51c4dfe

Please sign in to comment.