mirror of https://github.com/fafhrd91/actix-web
simplify `ConnectionInfo` new
This commit is contained in:
parent
b1148fd735
commit
acf3409d2a
147
src/info.rs
147
src/info.rs
|
@ -5,13 +5,16 @@ use derive_more::{Display, Error};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
dev::{AppConfig, Payload, RequestHead},
|
dev::{AppConfig, Payload, RequestHead},
|
||||||
http::header::{self, HeaderName},
|
http::{
|
||||||
|
header,
|
||||||
|
uri::{Authority, Scheme},
|
||||||
|
},
|
||||||
FromRequest, HttpRequest, ResponseError,
|
FromRequest, HttpRequest, ResponseError,
|
||||||
};
|
};
|
||||||
|
|
||||||
const X_FORWARDED_FOR: &[u8] = b"x-forwarded-for";
|
const X_FORWARDED_FOR: &str = "x-forwarded-for";
|
||||||
const X_FORWARDED_HOST: &[u8] = b"x-forwarded-host";
|
const X_FORWARDED_HOST: &str = "x-forwarded-host";
|
||||||
const X_FORWARDED_PROTO: &[u8] = b"x-forwarded-proto";
|
const X_FORWARDED_PROTO: &str = "x-forwarded-proto";
|
||||||
|
|
||||||
/// HTTP connection information.
|
/// HTTP connection information.
|
||||||
///
|
///
|
||||||
|
@ -48,105 +51,73 @@ impl ConnectionInfo {
|
||||||
Ref::map(req.extensions(), |e| e.get().unwrap())
|
Ref::map(req.extensions(), |e| e.get().unwrap())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(clippy::cognitive_complexity, clippy::borrow_interior_mutable_const)]
|
|
||||||
fn new(req: &RequestHead, cfg: &AppConfig) -> ConnectionInfo {
|
fn new(req: &RequestHead, cfg: &AppConfig) -> ConnectionInfo {
|
||||||
let mut host = None;
|
let mut host = None;
|
||||||
let mut scheme = None;
|
let mut scheme = None;
|
||||||
let mut realip_remote_addr = None;
|
let mut realip_remote_addr = None;
|
||||||
|
|
||||||
// load forwarded header
|
for el in req
|
||||||
for hdr in req.headers.get_all(&header::FORWARDED) {
|
.headers
|
||||||
if let Ok(val) = hdr.to_str() {
|
.get_all(&header::FORWARDED)
|
||||||
for pair in val.split(';') {
|
.into_iter()
|
||||||
for el in pair.split(',') {
|
.filter_map(|hdr| hdr.to_str().ok())
|
||||||
let mut items = el.trim().splitn(2, '=');
|
.flat_map(|val| val.split(';'))
|
||||||
if let Some(name) = items.next() {
|
.flat_map(|pair| pair.split(','))
|
||||||
if let Some(val) = items.next() {
|
{
|
||||||
match &name.to_lowercase() as &str {
|
let mut items = el.trim().splitn(2, '=');
|
||||||
"for" => {
|
if let (Some(name), Some(val)) = (items.next(), items.next()) {
|
||||||
if realip_remote_addr.is_none() {
|
match name.to_lowercase().as_ref() {
|
||||||
realip_remote_addr = Some(val.trim());
|
"for" => {
|
||||||
}
|
realip_remote_addr.get_or_insert_with(|| val.trim());
|
||||||
}
|
|
||||||
"proto" => {
|
|
||||||
if scheme.is_none() {
|
|
||||||
scheme = Some(val.trim());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
"host" => {
|
|
||||||
if host.is_none() {
|
|
||||||
host = Some(val.trim());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
"proto" => {
|
||||||
}
|
scheme.get_or_insert_with(|| val.trim());
|
||||||
}
|
|
||||||
|
|
||||||
// scheme
|
|
||||||
if scheme.is_none() {
|
|
||||||
if let Some(h) = req
|
|
||||||
.headers
|
|
||||||
.get(&HeaderName::from_lowercase(X_FORWARDED_PROTO).unwrap())
|
|
||||||
{
|
|
||||||
if let Ok(h) = h.to_str() {
|
|
||||||
scheme = h.split(',').next().map(|v| v.trim());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if scheme.is_none() {
|
|
||||||
scheme = req.uri.scheme().map(|a| a.as_str());
|
|
||||||
if scheme.is_none() && cfg.secure() {
|
|
||||||
scheme = Some("https")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// host
|
|
||||||
if host.is_none() {
|
|
||||||
if let Some(h) = req
|
|
||||||
.headers
|
|
||||||
.get(&HeaderName::from_lowercase(X_FORWARDED_HOST).unwrap())
|
|
||||||
{
|
|
||||||
if let Ok(h) = h.to_str() {
|
|
||||||
host = h.split(',').next().map(|v| v.trim());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if host.is_none() {
|
|
||||||
if let Some(h) = req.headers.get(&header::HOST) {
|
|
||||||
host = h.to_str().ok();
|
|
||||||
}
|
|
||||||
if host.is_none() {
|
|
||||||
host = req.uri.authority().map(|a| a.as_str());
|
|
||||||
if host.is_none() {
|
|
||||||
host = Some(cfg.host());
|
|
||||||
}
|
}
|
||||||
|
"host" => {
|
||||||
|
host.get_or_insert_with(|| val.trim());
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// get remote_addraddr from socketaddr
|
let first_header_value = |name| {
|
||||||
|
let val = req
|
||||||
|
.headers
|
||||||
|
.get(name)?
|
||||||
|
.to_str()
|
||||||
|
.ok()?
|
||||||
|
.split(',')
|
||||||
|
.next()?
|
||||||
|
.trim();
|
||||||
|
Some(val)
|
||||||
|
};
|
||||||
|
|
||||||
|
let scheme = scheme
|
||||||
|
.or_else(|| first_header_value(X_FORWARDED_PROTO))
|
||||||
|
.or_else(|| req.uri.scheme().map(Scheme::as_str))
|
||||||
|
.or_else(|| cfg.secure().then(|| "https"))
|
||||||
|
.unwrap_or("http")
|
||||||
|
.to_owned();
|
||||||
|
|
||||||
|
let host = host
|
||||||
|
.or_else(|| first_header_value(X_FORWARDED_HOST))
|
||||||
|
.or_else(|| req.headers.get(&header::HOST)?.to_str().ok())
|
||||||
|
.or_else(|| req.uri.authority().map(Authority::as_str))
|
||||||
|
.unwrap_or(cfg.host())
|
||||||
|
.to_owned();
|
||||||
|
|
||||||
|
let realip_remote_addr = realip_remote_addr
|
||||||
|
.or_else(|| first_header_value(X_FORWARDED_FOR))
|
||||||
|
.map(str::to_owned);
|
||||||
|
|
||||||
let remote_addr = req.peer_addr.map(|addr| format!("{}", addr));
|
let remote_addr = req.peer_addr.map(|addr| format!("{}", addr));
|
||||||
|
|
||||||
if realip_remote_addr.is_none() {
|
|
||||||
if let Some(h) = req
|
|
||||||
.headers
|
|
||||||
.get(&HeaderName::from_lowercase(X_FORWARDED_FOR).unwrap())
|
|
||||||
{
|
|
||||||
if let Ok(h) = h.to_str() {
|
|
||||||
realip_remote_addr = h.split(',').next().map(|v| v.trim());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ConnectionInfo {
|
ConnectionInfo {
|
||||||
remote_addr,
|
remote_addr,
|
||||||
scheme: scheme.unwrap_or("http").to_owned(),
|
scheme,
|
||||||
host: host.unwrap_or("localhost").to_owned(),
|
host,
|
||||||
realip_remote_addr: realip_remote_addr.map(|s| s.to_owned()),
|
realip_remote_addr,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue