From d5b4dcedd62adea06293bb400a9223af0202510b Mon Sep 17 00:00:00 2001 From: AustrianToast Date: Thu, 27 Mar 2025 09:51:44 +0100 Subject: [PATCH] change signature of Hashmap --- src/main.rs | 80 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 49 insertions(+), 31 deletions(-) diff --git a/src/main.rs b/src/main.rs index 55cf10e..ea7cb5a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -110,7 +110,7 @@ impl StartLine { } fn parse_start_line(input: &str) -> Result> { - let mut response_field_lines: HashMap = HashMap::new(); + let mut response_field_lines: HashMap> = HashMap::new(); let mut response_body: Vec = vec![]; let mut start_line = StartLine::new(); let vec = input.trim().split_ascii_whitespace().collect::>(); @@ -126,9 +126,12 @@ fn parse_start_line(input: &str) -> Result> { response_field_lines.insert( String::from("Content-Length"), - response_body.len().to_string(), + vec![response_body.len().to_string()], + ); + response_field_lines.insert( + String::from("Content-Type"), + vec![String::from("text/plain")], ); - response_field_lines.insert(String::from("Content-Type"), String::from("text/plain")); return Err(response_builder( RequestMethods::Get, @@ -166,9 +169,12 @@ fn parse_start_line(input: &str) -> Result> { response_field_lines.insert( String::from("Content-Length"), - response_body.len().to_string(), + vec![response_body.len().to_string()], + ); + response_field_lines.insert( + String::from("Content-Type"), + vec![String::from("text/plain")], ); - response_field_lines.insert(String::from("Content-Type"), String::from("text/plain")); return Err(response_builder( RequestMethods::Head, @@ -200,10 +206,10 @@ fn parse_start_line(input: &str) -> Result> { fn parse_field_lines( reader: &mut BufReader<&mut TcpStream>, -) -> Result, Vec> { - let mut response_field_lines: HashMap = HashMap::new(); +) -> Result>, Vec> { + let mut response_field_lines: HashMap> = HashMap::new(); let mut response_body: Vec = vec![]; - let mut field_lines: HashMap = HashMap::new(); + let mut field_lines: HashMap> = HashMap::new(); // Read field-lines till I hit an empty line loop { @@ -218,9 +224,12 @@ fn parse_field_lines( response_field_lines.insert( String::from("Content-Length"), - response_body.len().to_string(), + vec![response_body.len().to_string()], + ); + response_field_lines.insert( + String::from("Content-Type"), + vec![String::from("text/plain")], ); - response_field_lines.insert(String::from("Content-Type"), String::from("text/plain")); return Err(response_builder( RequestMethods::Get, @@ -244,10 +253,12 @@ fn parse_field_lines( response_field_lines.insert( String::from("Content-Length"), - response_body.len().to_string(), + vec![response_body.len().to_string()], + ); + response_field_lines.insert( + String::from("Content-Type"), + vec![String::from("text/plain")], ); - response_field_lines - .insert(String::from("Content-Type"), String::from("text/plain")); return Err(response_builder( RequestMethods::Get, @@ -258,7 +269,10 @@ fn parse_field_lines( } }; - field_lines.insert(field_line.0.to_owned(), field_line.1.trim().to_owned()); + field_lines.insert( + field_line.0.to_owned(), + vec![field_line.1.trim().to_owned()], + ); } if !field_lines.contains_key(&String::from("Host")) { @@ -269,9 +283,12 @@ fn parse_field_lines( response_field_lines.insert( String::from("Content-Length"), - response_body.len().to_string(), + vec![response_body.len().to_string()], + ); + response_field_lines.insert( + String::from("Content-Type"), + vec![String::from("text/plain")], ); - response_field_lines.insert(String::from("Content-Type"), String::from("text/plain")); return Err(response_builder( RequestMethods::Get, @@ -287,7 +304,7 @@ fn parse_field_lines( fn response_builder( method: RequestMethods, status_line: &str, - field_lines: Option>, + field_lines: Option>>, body: Option>, ) -> Vec { let mut response: Vec = vec![]; @@ -310,11 +327,9 @@ fn response_builder( response.push(b':'); response.push(b' '); - field_line - .1 - .as_bytes() - .iter() - .for_each(|byte| response.push(*byte)); + for val in field_line.1 { + val.as_bytes().iter().for_each(|byte| response.push(*byte)); + } response.push(b'\r'); response.push(b'\n'); @@ -336,8 +351,8 @@ fn response_builder( response } -fn try_get_file(start_line: &StartLine, _field_lines: &HashMap) -> Vec { - let mut response_field_lines: HashMap = HashMap::new(); +fn try_get_file(start_line: &StartLine, _field_lines: &HashMap>) -> Vec { + let mut response_field_lines: HashMap> = HashMap::new(); let mut response_body: Vec = vec![]; let path: PathBuf = match start_line.target.as_str() { @@ -360,13 +375,13 @@ fn try_get_file(start_line: &StartLine, _field_lines: &HashMap) response_field_lines.insert( String::from("Content-Length"), - response_body.len().to_string(), + vec![response_body.len().to_string()], ); let mime_type = mime_guess::from_path(&path) .first_raw() .expect("Could not guess mime-type from path"); - response_field_lines.insert(String::from("Content-Type"), mime_type.to_string()); + response_field_lines.insert(String::from("Content-Type"), vec![mime_type.to_string()]); response_builder( start_line.method, @@ -382,7 +397,7 @@ fn try_get_file(start_line: &StartLine, _field_lines: &HashMap) fn handle_request(mut stream: TcpStream) -> Result<(), Box> { let mut line = String::new(); let mut reader = BufReader::new(&mut stream); - let mut response_field_lines: HashMap = HashMap::new(); + let mut response_field_lines: HashMap> = HashMap::new(); let mut response_body: Vec = vec![]; // Request can have one or many empty lines preceding the start-line and I will ignore these @@ -400,9 +415,12 @@ fn handle_request(mut stream: TcpStream) -> Result<(), Box> { response_field_lines.insert( String::from("Content-Length"), - response_body.len().to_string(), + vec![response_body.len().to_string()], + ); + response_field_lines.insert( + String::from("Content-Type"), + vec![String::from("text/plain")], ); - response_field_lines.insert(String::from("Content-Type"), String::from("text/plain")); let response = response_builder( RequestMethods::Get, @@ -438,8 +456,8 @@ fn handle_request(mut stream: TcpStream) -> Result<(), Box> { let response = match start_line.target.as_str() { // For docker healtcheck. If the server can properly respond, then it must be healthy. "/server-health" => response_builder(RequestMethods::Head, "HTTP/1.1 200 ", None, None), - "/server-stats" => response_builder(start_line.method, "HTTP/1.1 404 ", None, None), - "/server-info" => response_builder(start_line.method, "HTTP/1.1 404 ", None, None), + // "/server-stats" => response_builder(start_line.method, "HTTP/1.1 404 ", None, None), + // "/server-info" => response_builder(start_line.method, "HTTP/1.1 404 ", None, None), _ => try_get_file(&start_line, &field_lines), }; stream.write_all(&response)?;