1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-30 18:44:35 +01: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 addr: Option<SocketAddr>,
pub payload: Option<Payload>,
pub info: Option<ConnectionInfo<'static>>,
pub prefix: u16,
resource: RouterResource,
}
struct Query(HashMap<String, String>);
struct Cookies(Vec<Cookie<'static>>);
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<S> HttpRequest<S> {
/// 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::<Info>().is_none() {
self.as_mut()
.extensions
.insert(Info(ConnectionInfo::new(self)));
}
self.as_ref().info.as_ref().unwrap()
&self.extensions().get::<Info>().unwrap().0
}
/// Generate url for named resource
@ -380,7 +377,6 @@ impl<S> HttpRequest<S> {
self.as_mut().addr = addr;
}
#[doc(hidden)]
/// url query parameters.
pub fn query(&self) -> &HashMap<String, String> {
if self.extensions().get::<Query>().is_none() {

View File

@ -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<String>,
peer: Option<String>,
}
impl<'a> ConnectionInfo<'a> {
impl ConnectionInfo {
/// Create *ConnectionInfo* instance for a request.
#[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 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)