ignore case on header keys

This commit is contained in:
ari melody 2025-06-16 19:50:48 +01:00
parent d2fb78108e
commit fc064efb61
Signed by: ari
GPG key ID: CF99829C92678188
2 changed files with 6 additions and 7 deletions

View file

@ -48,7 +48,7 @@ pub struct Request<'a> {
path: &'a str, path: &'a str,
method: &'a str, method: &'a str,
version: &'a str, version: &'a str,
headers: HashMap<&'a str, &'a str>, headers: HashMap<String, &'a str>,
query: HashMap<&'a str, &'a str>, query: HashMap<&'a str, &'a str>,
body: Option<String>, body: Option<String>,
real_address: IpAddr, real_address: IpAddr,
@ -82,7 +82,7 @@ impl<'a> Request<'a> {
None => {}, None => {},
} }
let mut headers: HashMap<&'a str, &'a str> = HashMap::new(); let mut headers: HashMap<String, &'a str> = HashMap::new();
if lines.len() > 1 { if lines.len() > 1 {
let mut i: usize = 1; let mut i: usize = 1;
loop { loop {
@ -90,7 +90,7 @@ impl<'a> Request<'a> {
let line = &lines[i]; let line = &lines[i];
if line.len() == 0 || !line.contains(":") { break; } if line.len() == 0 || !line.contains(":") { break; }
let (name, value) = line.split_once(":").unwrap(); let (name, value) = line.split_once(":").unwrap();
headers.insert(name, value.trim()); headers.insert(name.to_lowercase(), value.trim());
i += 1; i += 1;
} }
} }
@ -149,7 +149,7 @@ impl<'a> Request<'a> {
pub fn body(&self) -> &Option<String> { pub fn body(&self) -> &Option<String> {
&self.body &self.body
} }
pub fn headers(&self) -> &HashMap<&'a str, &'a str> { pub fn headers(&self) -> &HashMap<String, &'a str> {
&self.headers &self.headers
} }
pub fn query(&self) -> &HashMap<&'a str, &'a str> { pub fn query(&self) -> &HashMap<&'a str, &'a str> {
@ -282,7 +282,6 @@ impl HttpServer {
Ok(status) => { Ok(status) => {
let end_date = Local::now(); let end_date = Local::now();
println!( println!(
"[{}] {} {} {} - {} {} - {}ms - {} ({})", "[{}] {} {} {} - {} {} - {}ms - {} ({})",
start_date.format("%Y-%m-%d %H:%M:%S"), start_date.format("%Y-%m-%d %H:%M:%S"),

View file

@ -81,7 +81,7 @@ env!("CARGO_PKG_VERSION"));
} }
if request.path() == "/" { if request.path() == "/" {
if request.headers().get("Accept").is_some_and( if request.headers().get("accept").is_some_and(
|accept| accept.contains("text/html") |accept| accept.contains("text/html")
) { ) {
// HTML response // HTML response
@ -136,7 +136,7 @@ env!("CARGO_PKG_VERSION"));
let response_content = content let response_content = content
.replace("{{response}}", &query_response) .replace("{{response}}", &query_response)
.replace("{{host}}", match request.headers().get("Host") { .replace("{{host}}", match request.headers().get("host") {
Some(host) => { host } Some(host) => { host }
None => { "mcq.bliss.town" } None => { "mcq.bliss.town" }
}); });