From 4fadff63f4b1eec0dd3646df4beb69adb52c5204 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Sat, 23 Jun 2018 09:57:03 +0600 Subject: [PATCH] Use Box::leak for dynamic param names --- Cargo.toml | 1 - src/lib.rs | 1 - src/middleware/csrf.rs | 4 ++-- src/param.rs | 13 ++++++------- src/router.rs | 14 ++++++++------ 5 files changed, 16 insertions(+), 17 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index aa523e17a..19f3ebdde 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -102,7 +102,6 @@ tokio-tls = { version="0.1", optional = true } # openssl openssl = { version="0.10", optional = true } tokio-openssl = { version="0.2", optional = true } -string_cache = "0.7.3" [dev-dependencies] env_logger = "0.5" diff --git a/src/lib.rs b/src/lib.rs index 22016c117..92ff13197 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -113,7 +113,6 @@ extern crate net2; extern crate parking_lot; extern crate rand; extern crate slab; -extern crate string_cache; extern crate tokio; extern crate tokio_io; extern crate tokio_reactor; diff --git a/src/middleware/csrf.rs b/src/middleware/csrf.rs index 670ec1c1f..faa763e25 100644 --- a/src/middleware/csrf.rs +++ b/src/middleware/csrf.rs @@ -15,7 +15,7 @@ //! the allowed origins. //! //! Use [`CsrfFilter::allow_xhr()`](struct.CsrfFilter.html#method.allow_xhr) -//! if you want to allow requests with unsafe methods via +//! if you want to allow requests with unprotected methods via //! [CORS](../cors/struct.Cors.html). //! //! # Example @@ -175,7 +175,7 @@ impl CsrfFilter { /// /// The filter is conservative by default, but it should be safe to allow /// missing `Origin` headers because a cross-site attacker cannot prevent - /// the browser from sending `Origin` on unsafe requests. + /// the browser from sending `Origin` on unprotected requests. pub fn allow_missing_origin(mut self) -> CsrfFilter { self.allow_missing_origin = true; self diff --git a/src/param.rs b/src/param.rs index 54325ee13..1003c6428 100644 --- a/src/param.rs +++ b/src/param.rs @@ -5,7 +5,6 @@ use std::str::FromStr; use http::StatusCode; use smallvec::SmallVec; -use string_cache::DefaultAtom as Atom; use error::{InternalError, ResponseError, UriSegmentError}; use uri::Url; @@ -22,7 +21,7 @@ pub trait FromParam: Sized { #[derive(Debug, Clone)] pub(crate) enum ParamItem { - Static(Atom), + Static(&'static str), UrlSegment(u16, u16), } @@ -33,7 +32,7 @@ pub(crate) enum ParamItem { pub struct Params { url: Url, pub(crate) tail: u16, - segments: SmallVec<[(Atom, ParamItem); 3]>, + segments: SmallVec<[(&'static str, ParamItem); 3]>, } impl Params { @@ -57,12 +56,12 @@ impl Params { self.tail = tail; } - pub(crate) fn add(&mut self, name: Atom, value: ParamItem) { + pub(crate) fn add(&mut self, name: &'static str, value: ParamItem) { self.segments.push((name, value)); } - pub(crate) fn add_static(&mut self, name: &str, value: &'static str) { - self.segments.push((Atom::from(name), ParamItem::Static(Atom::from(value)))); + pub(crate) fn add_static(&mut self, name: &'static str, value: &'static str) { + self.segments.push((name, ParamItem::Static(value))); } /// Check if there are any matched patterns @@ -78,7 +77,7 @@ impl Params { /// Get matched parameter by name without type conversion pub fn get(&self, key: &str) -> Option<&str> { for item in self.segments.iter() { - if key == &item.0 { + if key == item.0 { return match item.1 { ParamItem::Static(ref s) => Some(&s), ParamItem::UrlSegment(s, e) => { diff --git a/src/router.rs b/src/router.rs index 03e299e15..e37e46b41 100644 --- a/src/router.rs +++ b/src/router.rs @@ -11,8 +11,6 @@ use param::ParamItem; use resource::ResourceHandler; use server::ServerSettings; -use string_cache::DefaultAtom as Atom; - /// Interface for application router. pub struct Router(Rc); @@ -145,7 +143,7 @@ enum PatternElement { enum PatternType { Static(String), Prefix(String), - Dynamic(Regex, Vec, usize), + Dynamic(Regex, Vec<&'static str>, usize), } #[derive(Debug, Copy, Clone, PartialEq)] @@ -222,7 +220,11 @@ impl Resource { let names = re .capture_names() .filter_map(|name| { - name.map(|name| Atom::from(name)) + name.map(|name| { + let s: &'static str = + Box::leak(name.to_owned().into_boxed_str()); + s + }) }) .collect(); PatternType::Dynamic(re, names, len) @@ -313,7 +315,7 @@ impl Resource { let params = req.match_info_mut(); params.set_tail(len as u16); for (idx, segment) in segments.into_iter().enumerate() { - params.add(names[idx].clone(), segment); + params.add(names[idx], segment); } true } @@ -379,7 +381,7 @@ impl Resource { let params = req.match_info_mut(); params.set_tail(tail_len as u16); for (idx, segment) in segments.into_iter().enumerate() { - params.add(names[idx].clone(), segment); + params.add(names[idx], segment); } Some(tail_len) }