recipes/backend/src/utils.rs
2025-01-29 14:37:25 +01:00

41 lines
1 KiB
Rust

use std::net::SocketAddr;
use axum::http::HeaderMap;
use crate::consts;
pub fn get_ip_and_user_agent(headers: &HeaderMap, remote_address: SocketAddr) -> (String, String) {
let ip = match headers.get(consts::REVERSE_PROXY_IP_HTTP_FIELD) {
Some(v) => v.to_str().unwrap_or_default().to_string(),
None => remote_address.to_string(),
};
let user_agent = headers
.get(axum::http::header::USER_AGENT)
.map(|v| v.to_str().unwrap_or_default())
.unwrap_or_default()
.to_string();
(ip, user_agent)
}
pub fn get_url_from_host(host: &str) -> String {
let port: Option<u16> = 'p: {
let split_port: Vec<&str> = host.split(':').collect();
if split_port.len() == 2 {
if let Ok(p) = split_port[1].parse::<u16>() {
break 'p Some(p);
}
}
None
};
format!(
"http{}://{}",
if port.is_some() && port.unwrap() != 443 {
""
} else {
"s"
},
host
)
}