1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-24 07:53:00 +01:00

Use Box::leak for dynamic param names

This commit is contained in:
Nikolay Kim 2018-06-23 09:57:03 +06:00
parent 7bc7b4839b
commit 4fadff63f4
5 changed files with 16 additions and 17 deletions

View File

@ -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"

View File

@ -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;

View File

@ -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

View File

@ -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) => {

View File

@ -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<Inner>);
@ -145,7 +143,7 @@ enum PatternElement {
enum PatternType {
Static(String),
Prefix(String),
Dynamic(Regex, Vec<Atom>, 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)
}