1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-24 00:21:08 +01:00

Add .peer_addr() #744

This commit is contained in:
Nikolay Kim 2019-04-16 10:11:38 -07:00
parent eb4f6b74fb
commit 420d3064c5
5 changed files with 48 additions and 16 deletions

View File

@ -1,12 +1,14 @@
# Changes # Changes
## [1.0.0-alpha.7] - 2019-04-xx ## [1.0.0-beta.1] - 2019-04-xx
### Added ### 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()` `test::read_response()` and test::read_response_json()`
* Add `.peer_addr()` #744
### Changed ### Changed
* Rename `RouterConfig` to `ServiceConfig` * Rename `RouterConfig` to `ServiceConfig`

View File

@ -30,7 +30,7 @@ impl 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;
let peer = None; let mut peer = None;
// load forwarded header // load forwarded header
for hdr in req.headers.get_all(&header::FORWARDED) { for hdr in req.headers.get_all(&header::FORWARDED) {
@ -116,10 +116,10 @@ impl ConnectionInfo {
remote = h.split(',').next().map(|v| v.trim()); remote = h.split(',').next().map(|v| v.trim());
} }
} }
// if remote.is_none() { if remote.is_none() {
// get peeraddr from socketaddr // get peeraddr from socketaddr
// peer = req.peer_addr().map(|addr| format!("{}", addr)); peer = req.peer_addr.map(|addr| format!("{}", addr));
// } }
} }
ConnectionInfo { ConnectionInfo {

View File

@ -363,13 +363,6 @@ impl FormatText {
let rt = (rt.num_nanoseconds().unwrap_or(0) as f64) / 1_000_000.0; let rt = (rt.num_nanoseconds().unwrap_or(0) as f64) / 1_000_000.0;
fmt.write_fmt(format_args!("{:.6}", rt)) 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) => { FormatText::EnvironHeader(ref name) => {
if let Ok(val) = env::var(name) { if let Ok(val) = env::var(name) {
fmt.write_fmt(format_args!("{}", val)) fmt.write_fmt(format_args!("{}", val))
@ -441,6 +434,14 @@ impl FormatText {
}; };
*self = FormatText::Str(s.to_string()); *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;
}
_ => (), _ => (),
} }
} }

View File

@ -1,6 +1,6 @@
use std::cell::{Ref, RefCell, RefMut}; use std::cell::{Ref, RefCell, RefMut};
use std::fmt;
use std::rc::Rc; use std::rc::Rc;
use std::{fmt, net};
use actix_http::http::{HeaderMap, Method, Uri, Version}; use actix_http::http::{HeaderMap, Method, Uri, Version};
use actix_http::{Error, Extensions, HttpMessage, Message, Payload, RequestHead}; use actix_http::{Error, Extensions, HttpMessage, Message, Payload, RequestHead};
@ -170,6 +170,17 @@ impl HttpRequest {
self.url_for(name, &NO_PARAMS) 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<net::SocketAddr> {
self.head().peer_addr
}
/// Get *ConnectionInfo* for the current request. /// Get *ConnectionInfo* for the current request.
#[inline] #[inline]
pub fn connection_info(&self) -> Ref<ConnectionInfo> { pub fn connection_info(&self) -> Ref<ConnectionInfo> {

View File

@ -1,5 +1,5 @@
use std::cell::{Ref, RefMut}; use std::cell::{Ref, RefMut};
use std::fmt; use std::{fmt, net};
use actix_http::body::{Body, MessageBody, ResponseBody}; use actix_http::body::{Body, MessageBody, ResponseBody};
use actix_http::http::{HeaderMap, Method, StatusCode, Uri, Version}; 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::config::{AppConfig, AppService};
use crate::data::Data; use crate::data::Data;
use crate::info::ConnectionInfo;
use crate::request::HttpRequest; use crate::request::HttpRequest;
pub trait HttpServiceFactory { 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<net::SocketAddr> {
self.head().peer_addr
}
/// Get *ConnectionInfo* for the current request.
#[inline]
pub fn connection_info(&self) -> Ref<ConnectionInfo> {
ConnectionInfo::get(self.head(), &*self.app_config())
}
/// Get a reference to the Path parameters. /// Get a reference to the Path parameters.
/// ///
/// Params is a container for url parameters. /// Params is a container for url parameters.