mirror of https://github.com/fafhrd91/actix-web
never return port in realip_remote_addr
This commit is contained in:
parent
96a4dc9dec
commit
42689dbc55
|
@ -11,7 +11,11 @@
|
||||||
- Some guards now return `impl Guard` and their concrete types are made private: `guard::{Header}` and all the method guards. [#2552]
|
- Some guards now return `impl Guard` and their concrete types are made private: `guard::{Header}` and all the method guards. [#2552]
|
||||||
- The `Not` guard is now generic over the type of guard it wraps. [#2552]
|
- The `Not` guard is now generic over the type of guard it wraps. [#2552]
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
- `ConnectionInfo::realip_remote_addr` will not return the port number if sourcing the IP from the peer's socket address. [#2554]
|
||||||
|
|
||||||
[#2552]: https://github.com/actix/actix-web/pull/2552
|
[#2552]: https://github.com/actix/actix-web/pull/2552
|
||||||
|
[#2554]: https://github.com/actix/actix-web/pull/2554
|
||||||
|
|
||||||
|
|
||||||
## 4.0.0-beta.16 - 2021-12-27
|
## 4.0.0-beta.16 - 2021-12-27
|
||||||
|
|
20
src/info.rs
20
src/info.rs
|
@ -134,7 +134,7 @@ impl ConnectionInfo {
|
||||||
.or_else(|| first_header_value(req, &*X_FORWARDED_FOR))
|
.or_else(|| first_header_value(req, &*X_FORWARDED_FOR))
|
||||||
.map(str::to_owned);
|
.map(str::to_owned);
|
||||||
|
|
||||||
let remote_addr = req.peer_addr.map(|addr| addr.to_string());
|
let remote_addr = req.peer_addr.map(|addr| addr.ip().to_string());
|
||||||
|
|
||||||
ConnectionInfo {
|
ConnectionInfo {
|
||||||
host,
|
host,
|
||||||
|
@ -165,6 +165,7 @@ impl ConnectionInfo {
|
||||||
/// - Host
|
/// - Host
|
||||||
/// - Uri
|
/// - Uri
|
||||||
/// - Server hostname
|
/// - Server hostname
|
||||||
|
#[inline]
|
||||||
pub fn host(&self) -> &str {
|
pub fn host(&self) -> &str {
|
||||||
&self.host
|
&self.host
|
||||||
}
|
}
|
||||||
|
@ -172,6 +173,7 @@ impl ConnectionInfo {
|
||||||
/// Remote address of the connection.
|
/// Remote address of the connection.
|
||||||
///
|
///
|
||||||
/// Get remote_addr address from socket address.
|
/// Get remote_addr address from socket address.
|
||||||
|
#[inline]
|
||||||
pub fn remote_addr(&self) -> Option<&str> {
|
pub fn remote_addr(&self) -> Option<&str> {
|
||||||
self.remote_addr.as_deref()
|
self.remote_addr.as_deref()
|
||||||
}
|
}
|
||||||
|
@ -432,13 +434,25 @@ mod tests {
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn peer_addr_extract() {
|
async fn peer_addr_extract() {
|
||||||
|
let req = TestRequest::default().to_http_request();
|
||||||
|
let res = PeerAddr::extract(&req).await;
|
||||||
|
assert!(res.is_err());
|
||||||
|
|
||||||
let addr = "127.0.0.1:8080".parse().unwrap();
|
let addr = "127.0.0.1:8080".parse().unwrap();
|
||||||
let req = TestRequest::default().peer_addr(addr).to_http_request();
|
let req = TestRequest::default().peer_addr(addr).to_http_request();
|
||||||
let peer_addr = PeerAddr::extract(&req).await.unwrap();
|
let peer_addr = PeerAddr::extract(&req).await.unwrap();
|
||||||
assert_eq!(peer_addr, PeerAddr(addr));
|
assert_eq!(peer_addr, PeerAddr(addr));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn real_ip_from_socket_addr() {
|
||||||
let req = TestRequest::default().to_http_request();
|
let req = TestRequest::default().to_http_request();
|
||||||
let res = PeerAddr::extract(&req).await;
|
let res = ConnectionInfo::extract(&req).await.unwrap();
|
||||||
assert!(res.is_err());
|
assert!(res.realip_remote_addr().is_none());
|
||||||
|
|
||||||
|
let addr = "127.0.0.1:8080".parse().unwrap();
|
||||||
|
let req = TestRequest::default().peer_addr(addr).to_http_request();
|
||||||
|
let conn_info = ConnectionInfo::extract(&req).await.unwrap();
|
||||||
|
assert_eq!(conn_info.realip_remote_addr().unwrap(), "127.0.0.1");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue