From fc064efb61813f9d77afcff868ed3f35034ed79e Mon Sep 17 00:00:00 2001 From: ari melody Date: Mon, 16 Jun 2025 19:50:48 +0100 Subject: [PATCH] ignore case on header keys --- src/http.rs | 9 ++++----- src/main.rs | 4 ++-- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/http.rs b/src/http.rs index 27bbdc4..131c96e 100644 --- a/src/http.rs +++ b/src/http.rs @@ -48,7 +48,7 @@ pub struct Request<'a> { path: &'a str, method: &'a str, version: &'a str, - headers: HashMap<&'a str, &'a str>, + headers: HashMap, query: HashMap<&'a str, &'a str>, body: Option, real_address: IpAddr, @@ -82,7 +82,7 @@ impl<'a> Request<'a> { None => {}, } - let mut headers: HashMap<&'a str, &'a str> = HashMap::new(); + let mut headers: HashMap = HashMap::new(); if lines.len() > 1 { let mut i: usize = 1; loop { @@ -90,7 +90,7 @@ impl<'a> Request<'a> { let line = &lines[i]; if line.len() == 0 || !line.contains(":") { break; } let (name, value) = line.split_once(":").unwrap(); - headers.insert(name, value.trim()); + headers.insert(name.to_lowercase(), value.trim()); i += 1; } } @@ -149,7 +149,7 @@ impl<'a> Request<'a> { pub fn body(&self) -> &Option { &self.body } - pub fn headers(&self) -> &HashMap<&'a str, &'a str> { + pub fn headers(&self) -> &HashMap { &self.headers } pub fn query(&self) -> &HashMap<&'a str, &'a str> { @@ -282,7 +282,6 @@ impl HttpServer { Ok(status) => { let end_date = Local::now(); - println!( "[{}] {} {} {} - {} {} - {}ms - {} ({})", start_date.format("%Y-%m-%d %H:%M:%S"), diff --git a/src/main.rs b/src/main.rs index 75e4e5e..664036b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -81,7 +81,7 @@ env!("CARGO_PKG_VERSION")); } if request.path() == "/" { - if request.headers().get("Accept").is_some_and( + if request.headers().get("accept").is_some_and( |accept| accept.contains("text/html") ) { // HTML response @@ -136,7 +136,7 @@ env!("CARGO_PKG_VERSION")); let response_content = content .replace("{{response}}", &query_response) - .replace("{{host}}", match request.headers().get("Host") { + .replace("{{host}}", match request.headers().get("host") { Some(host) => { host } None => { "mcq.bliss.town" } });