simplify `ConnectionInfo` new

This commit is contained in:
ibraheemdev 2021-06-22 18:23:49 -04:00
parent b1148fd735
commit acf3409d2a
1 changed files with 59 additions and 88 deletions

View File

@ -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())
.flat_map(|val| val.split(';'))
.flat_map(|pair| pair.split(','))
{
let mut items = el.trim().splitn(2, '='); let mut items = el.trim().splitn(2, '=');
if let Some(name) = items.next() { if let (Some(name), Some(val)) = (items.next(), items.next()) {
if let Some(val) = items.next() { match name.to_lowercase().as_ref() {
match &name.to_lowercase() as &str {
"for" => { "for" => {
if realip_remote_addr.is_none() { realip_remote_addr.get_or_insert_with(|| val.trim());
realip_remote_addr = Some(val.trim());
}
} }
"proto" => { "proto" => {
if scheme.is_none() { scheme.get_or_insert_with(|| val.trim());
scheme = Some(val.trim());
}
} }
"host" => { "host" => {
if host.is_none() { host.get_or_insert_with(|| val.trim());
host = Some(val.trim());
}
} }
_ => {} _ => {}
} }
} }
} }
}
}
}
}
// scheme let first_header_value = |name| {
if scheme.is_none() { let val = req
if let Some(h) = req
.headers .headers
.get(&HeaderName::from_lowercase(X_FORWARDED_PROTO).unwrap()) .get(name)?
{ .to_str()
if let Ok(h) = h.to_str() { .ok()?
scheme = h.split(',').next().map(|v| v.trim()); .split(',')
} .next()?
} .trim();
if scheme.is_none() { Some(val)
scheme = req.uri.scheme().map(|a| a.as_str()); };
if scheme.is_none() && cfg.secure() {
scheme = Some("https")
}
}
}
// host let scheme = scheme
if host.is_none() { .or_else(|| first_header_value(X_FORWARDED_PROTO))
if let Some(h) = req .or_else(|| req.uri.scheme().map(Scheme::as_str))
.headers .or_else(|| cfg.secure().then(|| "https"))
.get(&HeaderName::from_lowercase(X_FORWARDED_HOST).unwrap()) .unwrap_or("http")
{ .to_owned();
if let Ok(h) = h.to_str() {
host = h.split(',').next().map(|v| v.trim()); let host = host
} .or_else(|| first_header_value(X_FORWARDED_HOST))
} .or_else(|| req.headers.get(&header::HOST)?.to_str().ok())
if host.is_none() { .or_else(|| req.uri.authority().map(Authority::as_str))
if let Some(h) = req.headers.get(&header::HOST) { .unwrap_or(cfg.host())
host = h.to_str().ok(); .to_owned();
}
if host.is_none() { let realip_remote_addr = realip_remote_addr
host = req.uri.authority().map(|a| a.as_str()); .or_else(|| first_header_value(X_FORWARDED_FOR))
if host.is_none() { .map(str::to_owned);
host = Some(cfg.host());
}
}
}
}
// get remote_addraddr from socketaddr
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,
} }
} }