log user agents

This commit is contained in:
Crispy 2024-04-27 21:20:10 +02:00
parent 0ba58d98e2
commit 6f0484f26b
2 changed files with 5 additions and 0 deletions

View file

@ -3,6 +3,7 @@ pub struct Request {
pub method: Method, pub method: Method,
pub path: String, pub path: String,
pub host: String, pub host: String,
pub user_agent: String,
pub real_ip: Option<String>, pub real_ip: Option<String>,
pub range: Option<RequestRange>, pub range: Option<RequestRange>,
} }
@ -54,6 +55,7 @@ impl Request {
let mut host = None; let mut host = None;
let mut range = None; let mut range = None;
let mut real_ip = None; let mut real_ip = None;
let mut user_agent = String::new();
for line in lines { for line in lines {
if line.is_empty() { if line.is_empty() {
break; break;
@ -64,6 +66,7 @@ impl Request {
"host" => host = Some(value.to_owned()), "host" => host = Some(value.to_owned()),
"range" => range = RequestRange::parse(value), "range" => range = RequestRange::parse(value),
"x-real-ip" => real_ip = Some(value.to_owned()), "x-real-ip" => real_ip = Some(value.to_owned()),
"user-agent" => user_agent = value.to_owned(),
_ => (), _ => (),
} }
} }
@ -88,6 +91,7 @@ impl Request {
host, host,
real_ip, real_ip,
range, range,
user_agent,
}) })
} }
} }

View file

@ -105,6 +105,7 @@ fn handle_request(request: &str, stream: &mut TcpStream) -> bool {
if let Some(request) = request { if let Some(request) = request {
let client_ip = format!("{socket_addr}"); let client_ip = format!("{socket_addr}");
let client_ip = request.real_ip.as_ref().unwrap_or(&client_ip); let client_ip = request.real_ip.as_ref().unwrap_or(&client_ip);
println!("[{client_ip}] {}", request.user_agent);
println!("[{client_ip}] {} {}", request.method, request.path); println!("[{client_ip}] {} {}", request.method, request.path);
let head_only = request.method == Method::Head; let head_only = request.method == Method::Head;
let path = request.path.clone(); let path = request.path.clone();