1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-12-21 15:46:48 +00:00

do not use references in ConnectionInfo

This commit is contained in:
Nikolay Kim 2018-06-18 00:01:41 +06:00
parent e1db47d550
commit ea118edf56
2 changed files with 20 additions and 23 deletions

View file

@ -43,13 +43,13 @@ pub struct HttpInnerMessage {
pub params: Params<'static>, pub params: Params<'static>,
pub addr: Option<SocketAddr>, pub addr: Option<SocketAddr>,
pub payload: Option<Payload>, pub payload: Option<Payload>,
pub info: Option<ConnectionInfo<'static>>,
pub prefix: u16, pub prefix: u16,
resource: RouterResource, resource: RouterResource,
} }
struct Query(HashMap<String, String>); struct Query(HashMap<String, String>);
struct Cookies(Vec<Cookie<'static>>); struct Cookies(Vec<Cookie<'static>>);
struct Info(ConnectionInfo);
#[derive(Debug, Copy, Clone, PartialEq)] #[derive(Debug, Copy, Clone, PartialEq)]
enum RouterResource { enum RouterResource {
@ -69,7 +69,6 @@ impl Default for HttpInnerMessage {
addr: None, addr: None,
payload: None, payload: None,
extensions: Extensions::new(), extensions: Extensions::new(),
info: None,
prefix: 0, prefix: 0,
resource: RouterResource::Notset, resource: RouterResource::Notset,
} }
@ -89,7 +88,6 @@ impl HttpInnerMessage {
self.extensions.clear(); self.extensions.clear();
self.params.clear(); self.params.clear();
self.addr = None; self.addr = None;
self.info = None;
self.flags = MessageFlags::empty(); self.flags = MessageFlags::empty();
self.payload = None; self.payload = None;
self.prefix = 0; self.prefix = 0;
@ -122,7 +120,6 @@ impl HttpRequest<()> {
params: Params::new(), params: Params::new(),
extensions: Extensions::new(), extensions: Extensions::new(),
addr: None, addr: None,
info: None,
prefix: 0, prefix: 0,
flags: MessageFlags::empty(), flags: MessageFlags::empty(),
resource: RouterResource::Notset, resource: RouterResource::Notset,
@ -280,12 +277,12 @@ impl<S> HttpRequest<S> {
/// Get *ConnectionInfo* for correct request. /// Get *ConnectionInfo* for correct request.
pub fn connection_info(&self) -> &ConnectionInfo { pub fn connection_info(&self) -> &ConnectionInfo {
if self.as_ref().info.is_none() { if self.extensions().get::<Info>().is_none() {
let info: ConnectionInfo<'static> = self.as_mut()
unsafe { mem::transmute(ConnectionInfo::new(self)) }; .extensions
self.as_mut().info = Some(info); .insert(Info(ConnectionInfo::new(self)));
} }
self.as_ref().info.as_ref().unwrap() &self.extensions().get::<Info>().unwrap().0
} }
/// Generate url for named resource /// Generate url for named resource
@ -380,7 +377,6 @@ impl<S> HttpRequest<S> {
self.as_mut().addr = addr; self.as_mut().addr = addr;
} }
#[doc(hidden)]
/// url query parameters. /// url query parameters.
pub fn query(&self) -> &HashMap<String, String> { pub fn query(&self) -> &HashMap<String, String> {
if self.extensions().get::<Query>().is_none() { if self.extensions().get::<Query>().is_none() {

View file

@ -1,24 +1,25 @@
use std::str::FromStr;
use http::header::{self, HeaderName}; use http::header::{self, HeaderName};
use httpmessage::HttpMessage; use httpmessage::HttpMessage;
use httprequest::HttpRequest; use httprequest::HttpRequest;
use std::str::FromStr;
const X_FORWARDED_FOR: &str = "X-FORWARDED-FOR"; const X_FORWARDED_FOR: &str = "X-FORWARDED-FOR";
const X_FORWARDED_HOST: &str = "X-FORWARDED-HOST"; const X_FORWARDED_HOST: &str = "X-FORWARDED-HOST";
const X_FORWARDED_PROTO: &str = "X-FORWARDED-PROTO"; const X_FORWARDED_PROTO: &str = "X-FORWARDED-PROTO";
/// `HttpRequest` connection information /// `HttpRequest` connection information
pub struct ConnectionInfo<'a> { pub struct ConnectionInfo {
scheme: &'a str, scheme: String,
host: &'a str, host: String,
remote: Option<&'a str>, remote: Option<String>,
peer: Option<String>, peer: Option<String>,
} }
impl<'a> ConnectionInfo<'a> { impl ConnectionInfo {
/// Create *ConnectionInfo* instance for a request. /// Create *ConnectionInfo* instance for a request.
#[cfg_attr(feature = "cargo-clippy", allow(cyclomatic_complexity))] #[cfg_attr(feature = "cargo-clippy", allow(cyclomatic_complexity))]
pub fn new<S>(req: &'a HttpRequest<S>) -> ConnectionInfo<'a> { pub fn new<S>(req: &HttpRequest<S>) -> ConnectionInfo {
let mut host = None; let mut host = None;
let mut scheme = None; let mut scheme = None;
let mut remote = None; let mut remote = None;
@ -115,9 +116,9 @@ impl<'a> ConnectionInfo<'a> {
} }
ConnectionInfo { ConnectionInfo {
scheme: scheme.unwrap_or("http"), scheme: scheme.unwrap_or("http").to_owned(),
host: host.unwrap_or("localhost"), host: host.unwrap_or("localhost").to_owned(),
remote, remote: remote.map(|s| s.to_owned()),
peer, peer,
} }
} }
@ -131,7 +132,7 @@ impl<'a> ConnectionInfo<'a> {
/// - Uri /// - Uri
#[inline] #[inline]
pub fn scheme(&self) -> &str { pub fn scheme(&self) -> &str {
self.scheme &self.scheme
} }
/// Hostname of the request. /// Hostname of the request.
@ -144,7 +145,7 @@ impl<'a> ConnectionInfo<'a> {
/// - Uri /// - Uri
/// - Server hostname /// - Server hostname
pub fn host(&self) -> &str { pub fn host(&self) -> &str {
self.host &self.host
} }
/// Remote IP of client initiated HTTP request. /// Remote IP of client initiated HTTP request.
@ -156,7 +157,7 @@ impl<'a> ConnectionInfo<'a> {
/// - peer name of opened socket /// - peer name of opened socket
#[inline] #[inline]
pub fn remote(&self) -> Option<&str> { pub fn remote(&self) -> Option<&str> {
if let Some(r) = self.remote { if let Some(ref r) = self.remote {
Some(r) Some(r)
} else if let Some(ref peer) = self.peer { } else if let Some(ref peer) = self.peer {
Some(peer) Some(peer)