diff --git a/CHANGES.md b/CHANGES.md index 8909f3e29..00518764d 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,12 +1,14 @@ # Changes -## [1.0.0-alpha.7] - 2019-04-xx +## [1.0.0-beta.1] - 2019-04-xx ### Added -* Added helper functions for reading test response body, +* Add helper functions for reading test response body, `test::read_response()` and test::read_response_json()` +* Add `.peer_addr()` #744 + ### Changed * Rename `RouterConfig` to `ServiceConfig` diff --git a/src/info.rs b/src/info.rs index ece17bf04..e9b375875 100644 --- a/src/info.rs +++ b/src/info.rs @@ -30,7 +30,7 @@ impl ConnectionInfo { let mut host = None; let mut scheme = None; let mut remote = None; - let peer = None; + let mut peer = None; // load forwarded header for hdr in req.headers.get_all(&header::FORWARDED) { @@ -116,10 +116,10 @@ impl ConnectionInfo { remote = h.split(',').next().map(|v| v.trim()); } } - // if remote.is_none() { - // get peeraddr from socketaddr - // peer = req.peer_addr().map(|addr| format!("{}", addr)); - // } + if remote.is_none() { + // get peeraddr from socketaddr + peer = req.peer_addr.map(|addr| format!("{}", addr)); + } } ConnectionInfo { diff --git a/src/middleware/logger.rs b/src/middleware/logger.rs index d5fca526b..43893bc0f 100644 --- a/src/middleware/logger.rs +++ b/src/middleware/logger.rs @@ -363,13 +363,6 @@ impl FormatText { let rt = (rt.num_nanoseconds().unwrap_or(0) as f64) / 1_000_000.0; fmt.write_fmt(format_args!("{:.6}", rt)) } - // FormatText::RemoteAddr => { - // if let Some(remote) = req.connection_info().remote() { - // return remote.fmt(fmt); - // } else { - // "-".fmt(fmt) - // } - // } FormatText::EnvironHeader(ref name) => { if let Ok(val) = env::var(name) { fmt.write_fmt(format_args!("{}", val)) @@ -441,6 +434,14 @@ impl FormatText { }; *self = FormatText::Str(s.to_string()); } + FormatText::RemoteAddr => { + let s = if let Some(remote) = req.connection_info().remote() { + FormatText::Str(remote.to_string()) + } else { + FormatText::Str("-".to_string()) + }; + *self = s; + } _ => (), } } diff --git a/src/request.rs b/src/request.rs index 5823c08ca..ad5b2488f 100644 --- a/src/request.rs +++ b/src/request.rs @@ -1,6 +1,6 @@ use std::cell::{Ref, RefCell, RefMut}; -use std::fmt; use std::rc::Rc; +use std::{fmt, net}; use actix_http::http::{HeaderMap, Method, Uri, Version}; use actix_http::{Error, Extensions, HttpMessage, Message, Payload, RequestHead}; @@ -170,6 +170,17 @@ impl HttpRequest { self.url_for(name, &NO_PARAMS) } + /// Peer socket address + /// + /// Peer address is actual socket address, if proxy is used in front of + /// actix http server, then peer address would be address of this proxy. + /// + /// To get client connection information `.connection_info()` should be used. + #[inline] + pub fn peer_addr(&self) -> Option { + self.head().peer_addr + } + /// Get *ConnectionInfo* for the current request. #[inline] pub fn connection_info(&self) -> Ref { diff --git a/src/service.rs b/src/service.rs index 8bc2ff9a5..5303436ce 100644 --- a/src/service.rs +++ b/src/service.rs @@ -1,5 +1,5 @@ use std::cell::{Ref, RefMut}; -use std::fmt; +use std::{fmt, net}; use actix_http::body::{Body, MessageBody, ResponseBody}; use actix_http::http::{HeaderMap, Method, StatusCode, Uri, Version}; @@ -12,6 +12,7 @@ use futures::future::{ok, FutureResult, IntoFuture}; use crate::config::{AppConfig, AppService}; use crate::data::Data; +use crate::info::ConnectionInfo; use crate::request::HttpRequest; pub trait HttpServiceFactory { @@ -134,6 +135,23 @@ impl ServiceRequest { } } + /// Peer socket address + /// + /// Peer address is actual socket address, if proxy is used in front of + /// actix http server, then peer address would be address of this proxy. + /// + /// To get client connection information `ConnectionInfo` should be used. + #[inline] + pub fn peer_addr(&self) -> Option { + self.head().peer_addr + } + + /// Get *ConnectionInfo* for the current request. + #[inline] + pub fn connection_info(&self) -> Ref { + ConnectionInfo::get(self.head(), &*self.app_config()) + } + /// Get a reference to the Path parameters. /// /// Params is a container for url parameters.