From 7d6643032408720bbcb4355cf3bc453e8724c272 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Sun, 18 Nov 2018 20:08:43 -0800 Subject: [PATCH] move url module to different crate --- Cargo.toml | 5 +- src/h1/decoder.rs | 1 - src/lib.rs | 3 - src/message.rs | 5 -- src/request.rs | 6 +- src/test.rs | 3 +- src/uri.rs | 169 ---------------------------------------------- 7 files changed, 3 insertions(+), 189 deletions(-) delete mode 100644 src/uri.rs diff --git a/Cargo.toml b/Cargo.toml index 50bacddb..b2c7c084 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -52,7 +52,7 @@ base64 = "0.9" bitflags = "1.0" http = "0.1.8" httparse = "1.3" -failure = "0.1.2" +failure = "0.1.3" indexmap = "1.0" log = "0.4" mime = "0.3" @@ -62,7 +62,6 @@ serde_json = "1.0" sha1 = "0.6" time = "0.1" encoding = "0.2" -lazy_static = "1.0" serde_urlencoded = "0.5.3" cookie = { version="0.11", features=["percent-encode"] } @@ -93,8 +92,6 @@ openssl = { version="0.10", optional = true } # rustls rustls = { version = "^0.14", optional = true } -backtrace="*" - [dev-dependencies] actix-web = "0.7" env_logger = "0.5" diff --git a/src/h1/decoder.rs b/src/h1/decoder.rs index 12dfe165..de0df367 100644 --- a/src/h1/decoder.rs +++ b/src/h1/decoder.rs @@ -234,7 +234,6 @@ impl MessageTypeDecoder for Request { { let inner = msg.inner_mut(); - inner.url.update(&uri); inner.head.uri = uri; inner.head.method = method; inner.head.version = ver; diff --git a/src/lib.rs b/src/lib.rs index 3024b753..bee172f0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -76,8 +76,6 @@ extern crate bitflags; #[macro_use] extern crate failure; #[macro_use] -extern crate lazy_static; -#[macro_use] extern crate futures; extern crate cookie; extern crate encoding; @@ -124,7 +122,6 @@ mod payload; mod request; mod response; mod service; -pub mod uri; pub mod error; pub mod h1; diff --git a/src/message.rs b/src/message.rs index 03e18f08..e1059fa4 100644 --- a/src/message.rs +++ b/src/message.rs @@ -6,7 +6,6 @@ use http::{HeaderMap, Method, StatusCode, Uri, Version}; use extensions::Extensions; use payload::Payload; -use uri::Url; /// Represents various types of connection #[derive(Copy, Clone, PartialEq, Debug)] @@ -147,8 +146,6 @@ impl ResponseHead { pub struct Message { pub head: T, - pub url: Url, - pub status: StatusCode, pub extensions: RefCell, pub payload: RefCell>, pub(crate) pool: &'static MessagePool, @@ -168,9 +165,7 @@ impl Default for Message { fn default() -> Self { Message { pool: T::pool(), - url: Url::default(), head: T::default(), - status: StatusCode::OK, payload: RefCell::new(None), extensions: RefCell::new(Extensions::new()), } diff --git a/src/request.rs b/src/request.rs index 1ee47edb..d529c09f 100644 --- a/src/request.rs +++ b/src/request.rs @@ -94,11 +94,7 @@ impl Request { /// The target path of this Request. #[inline] pub fn path(&self) -> &str { - if let Some(path) = self.inner().url.path() { - path - } else { - self.inner().head.uri.path() - } + self.inner().head.uri.path() } #[inline] diff --git a/src/test.rs b/src/test.rs index c6f258a7..5442a414 100644 --- a/src/test.rs +++ b/src/test.rs @@ -15,7 +15,6 @@ use tokio::runtime::current_thread::Runtime; use header::{Header, IntoHeaderValue}; use payload::Payload; use request::Request; -use uri::Url as InnerUrl; // use ws; /// The `TestServer` type. @@ -390,8 +389,8 @@ impl TestRequest { let mut req = Request::new(); { let inner = req.inner_mut(); + inner.head.uri = uri; inner.head.method = method; - inner.url = InnerUrl::new(&uri); inner.head.version = version; inner.head.headers = headers; *inner.payload.borrow_mut() = payload; diff --git a/src/uri.rs b/src/uri.rs deleted file mode 100644 index 89f6d3b1..00000000 --- a/src/uri.rs +++ /dev/null @@ -1,169 +0,0 @@ -use http::Uri; -use std::rc::Rc; - -#[allow(dead_code)] -const GEN_DELIMS: &[u8] = b":/?#[]@"; -#[allow(dead_code)] -const SUB_DELIMS_WITHOUT_QS: &[u8] = b"!$'()*,"; -#[allow(dead_code)] -const SUB_DELIMS: &[u8] = b"!$'()*,+?=;"; -#[allow(dead_code)] -const RESERVED: &[u8] = b":/?#[]@!$'()*,+?=;"; -#[allow(dead_code)] -const UNRESERVED: &[u8] = b"abcdefghijklmnopqrstuvwxyz - ABCDEFGHIJKLMNOPQRSTUVWXYZ - 1234567890 - -._~"; -const ALLOWED: &[u8] = b"abcdefghijklmnopqrstuvwxyz - ABCDEFGHIJKLMNOPQRSTUVWXYZ - 1234567890 - -._~ - !$'()*,"; -const QS: &[u8] = b"+&=;b"; - -#[inline] -fn bit_at(array: &[u8], ch: u8) -> bool { - array[(ch >> 3) as usize] & (1 << (ch & 7)) != 0 -} - -#[inline] -fn set_bit(array: &mut [u8], ch: u8) { - array[(ch >> 3) as usize] |= 1 << (ch & 7) -} - -lazy_static! { - pub static ref DEFAULT_QUOTER: Quoter = { Quoter::new(b"@:", b"/+") }; -} - -#[derive(Default, Clone, Debug)] -pub struct Url { - path: Option>, -} - -impl Url { - pub fn new(uri: &Uri) -> Url { - let path = DEFAULT_QUOTER.requote(uri.path().as_bytes()); - - Url { path } - } - - pub(crate) fn update(&mut self, uri: &Uri) { - self.path = DEFAULT_QUOTER.requote(uri.path().as_bytes()); - } - - pub fn path(&self) -> Option<&str> { - self.path.as_ref().map(|s| s.as_str()) - } -} - -pub struct Quoter { - safe_table: [u8; 16], - protected_table: [u8; 16], -} - -impl Quoter { - pub fn new(safe: &[u8], protected: &[u8]) -> Quoter { - let mut q = Quoter { - safe_table: [0; 16], - protected_table: [0; 16], - }; - - // prepare safe table - for i in 0..128 { - if ALLOWED.contains(&i) { - set_bit(&mut q.safe_table, i); - } - if QS.contains(&i) { - set_bit(&mut q.safe_table, i); - } - } - - for ch in safe { - set_bit(&mut q.safe_table, *ch) - } - - // prepare protected table - for ch in protected { - set_bit(&mut q.safe_table, *ch); - set_bit(&mut q.protected_table, *ch); - } - - q - } - - pub fn requote(&self, val: &[u8]) -> Option> { - let mut has_pct = 0; - let mut pct = [b'%', 0, 0]; - let mut idx = 0; - let mut cloned: Option> = None; - - let len = val.len(); - while idx < len { - let ch = val[idx]; - - if has_pct != 0 { - pct[has_pct] = val[idx]; - has_pct += 1; - if has_pct == 3 { - has_pct = 0; - let buf = cloned.as_mut().unwrap(); - - if let Some(ch) = restore_ch(pct[1], pct[2]) { - if ch < 128 { - if bit_at(&self.protected_table, ch) { - buf.extend_from_slice(&pct); - idx += 1; - continue; - } - - if bit_at(&self.safe_table, ch) { - buf.push(ch); - idx += 1; - continue; - } - } - buf.push(ch); - } else { - buf.extend_from_slice(&pct[..]); - } - } - } else if ch == b'%' { - has_pct = 1; - if cloned.is_none() { - let mut c = Vec::with_capacity(len); - c.extend_from_slice(&val[..idx]); - cloned = Some(c); - } - } else if let Some(ref mut cloned) = cloned { - cloned.push(ch) - } - idx += 1; - } - - 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) })) - } else { - None - } - } -} - -#[inline] -fn from_hex(v: u8) -> Option { - if v >= b'0' && v <= b'9' { - Some(v - 0x30) // ord('0') == 0x30 - } else if v >= b'A' && v <= b'F' { - Some(v - 0x41 + 10) // ord('A') == 0x41 - } else if v > b'a' && v <= b'f' { - Some(v - 0x61 + 10) // ord('a') == 0x61 - } else { - None - } -} - -#[inline] -fn restore_ch(d1: u8, d2: u8) -> Option { - from_hex(d1).and_then(|d1| from_hex(d2).and_then(move |d2| Some(d1 << 4 | d2))) -}