From ea118edf5663aec6ff2a1930e3712ad4966d647d Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Mon, 18 Jun 2018 00:01:41 +0600 Subject: [PATCH] do not use references in ConnectionInfo --- src/httprequest.rs | 16 ++++++---------- src/info.rs | 27 ++++++++++++++------------- 2 files changed, 20 insertions(+), 23 deletions(-) diff --git a/src/httprequest.rs b/src/httprequest.rs index 080e44367..501544328 100644 --- a/src/httprequest.rs +++ b/src/httprequest.rs @@ -43,13 +43,13 @@ pub struct HttpInnerMessage { pub params: Params<'static>, pub addr: Option, pub payload: Option, - pub info: Option>, pub prefix: u16, resource: RouterResource, } struct Query(HashMap); struct Cookies(Vec>); +struct Info(ConnectionInfo); #[derive(Debug, Copy, Clone, PartialEq)] enum RouterResource { @@ -69,7 +69,6 @@ impl Default for HttpInnerMessage { addr: None, payload: None, extensions: Extensions::new(), - info: None, prefix: 0, resource: RouterResource::Notset, } @@ -89,7 +88,6 @@ impl HttpInnerMessage { self.extensions.clear(); self.params.clear(); self.addr = None; - self.info = None; self.flags = MessageFlags::empty(); self.payload = None; self.prefix = 0; @@ -122,7 +120,6 @@ impl HttpRequest<()> { params: Params::new(), extensions: Extensions::new(), addr: None, - info: None, prefix: 0, flags: MessageFlags::empty(), resource: RouterResource::Notset, @@ -280,12 +277,12 @@ impl HttpRequest { /// Get *ConnectionInfo* for correct request. pub fn connection_info(&self) -> &ConnectionInfo { - if self.as_ref().info.is_none() { - let info: ConnectionInfo<'static> = - unsafe { mem::transmute(ConnectionInfo::new(self)) }; - self.as_mut().info = Some(info); + if self.extensions().get::().is_none() { + self.as_mut() + .extensions + .insert(Info(ConnectionInfo::new(self))); } - self.as_ref().info.as_ref().unwrap() + &self.extensions().get::().unwrap().0 } /// Generate url for named resource @@ -380,7 +377,6 @@ impl HttpRequest { self.as_mut().addr = addr; } - #[doc(hidden)] /// url query parameters. pub fn query(&self) -> &HashMap { if self.extensions().get::().is_none() { diff --git a/src/info.rs b/src/info.rs index 05d35f470..dad10b646 100644 --- a/src/info.rs +++ b/src/info.rs @@ -1,24 +1,25 @@ +use std::str::FromStr; + use http::header::{self, HeaderName}; use httpmessage::HttpMessage; use httprequest::HttpRequest; -use std::str::FromStr; const X_FORWARDED_FOR: &str = "X-FORWARDED-FOR"; const X_FORWARDED_HOST: &str = "X-FORWARDED-HOST"; const X_FORWARDED_PROTO: &str = "X-FORWARDED-PROTO"; /// `HttpRequest` connection information -pub struct ConnectionInfo<'a> { - scheme: &'a str, - host: &'a str, - remote: Option<&'a str>, +pub struct ConnectionInfo { + scheme: String, + host: String, + remote: Option, peer: Option, } -impl<'a> ConnectionInfo<'a> { +impl ConnectionInfo { /// Create *ConnectionInfo* instance for a request. #[cfg_attr(feature = "cargo-clippy", allow(cyclomatic_complexity))] - pub fn new(req: &'a HttpRequest) -> ConnectionInfo<'a> { + pub fn new(req: &HttpRequest) -> ConnectionInfo { let mut host = None; let mut scheme = None; let mut remote = None; @@ -115,9 +116,9 @@ impl<'a> ConnectionInfo<'a> { } ConnectionInfo { - scheme: scheme.unwrap_or("http"), - host: host.unwrap_or("localhost"), - remote, + scheme: scheme.unwrap_or("http").to_owned(), + host: host.unwrap_or("localhost").to_owned(), + remote: remote.map(|s| s.to_owned()), peer, } } @@ -131,7 +132,7 @@ impl<'a> ConnectionInfo<'a> { /// - Uri #[inline] pub fn scheme(&self) -> &str { - self.scheme + &self.scheme } /// Hostname of the request. @@ -144,7 +145,7 @@ impl<'a> ConnectionInfo<'a> { /// - Uri /// - Server hostname pub fn host(&self) -> &str { - self.host + &self.host } /// Remote IP of client initiated HTTP request. @@ -156,7 +157,7 @@ impl<'a> ConnectionInfo<'a> { /// - peer name of opened socket #[inline] pub fn remote(&self) -> Option<&str> { - if let Some(r) = self.remote { + if let Some(ref r) = self.remote { Some(r) } else if let Some(ref peer) = self.peer { Some(peer)