diff --git a/core/http/src/handlers/handler.rs b/core/http/src/handlers/handler.rs index 99f830084fd0faaf15f58b4b1a196c1d3aed0627..9596f5281d61b9f685b4e1f582d7202223586c2c 100644 --- a/core/http/src/handlers/handler.rs +++ b/core/http/src/handlers/handler.rs @@ -90,7 +90,7 @@ pub async fn handle_connection(mut stream: TcpStream, mountpoints: Vec<MountPoin 0 }; if length != 0 { - request.mime_type = Request::extract_mime_from_vec(&request.headers); + request.mime_from_headers(); let mut buffer = vec![0u8; MAX_HTTP_MESSAGE_SIZE.into()]; let read = buf_reader.read(&mut buffer).await.unwrap(); if read != length { diff --git a/core/http/src/handling/request/cookies.rs b/core/http/src/handling/request/cookies.rs index d078c7659fbd9ae1ee62565395e2051eeecbab2a..b386086032d10783a597643e4eef2cac914f5a62 100644 --- a/core/http/src/handling/request/cookies.rs +++ b/core/http/src/handling/request/cookies.rs @@ -3,6 +3,41 @@ use std::collections::HashMap; use super::Request; impl Request<'_> { + /// Extracts the cookies from a Vector and gives back an optional HashMap of Strings + /// + /// Returns none if there are no cookies or there is a problem with the cookies, for example + /// missing a value. + /// + /// Removes the `Cookie: ` Header from the Vector of headers + /// + /// # Examples + /// ``` + /// use http::handling::request::Request; + /// + /// + /// let mut request_vec = vec![ + /// "GET / HTTP/1.1".to_string(), + /// "Accept: sdf".to_string(), + /// "Cookie: io=23; f=as".to_string(), + /// "Format: gzip".to_string(), + /// ]; + /// let right_finished_vec = vec![ + /// "GET / HTTP/1.1".to_string(), + /// "Accept: sdf".to_string(), + /// "Format: gzip".to_string(), + /// ]; + /// + /// let mut wrong = vec!["GET / HTTP/1.1".to_string()]; + /// + /// assert_eq!(None, Request::extract_cookies_from_vec(&mut wrong)); + /// let cookies = Request::extract_cookies_from_vec(&mut request_vec); + /// assert_eq!(right_finished_vec, request_vec); + /// assert_eq!("23", cookies.clone().unwrap().get("io").unwrap()); + /// assert_eq!("as", cookies.unwrap().get("f").unwrap()); + /// ``` + /// + /// # Panics + /// No Panics pub fn extract_cookies_from_vec(headers: &mut Vec<String>) -> Option<HashMap<String, String>> { let mut cookies: HashMap<String, String> = HashMap::new(); let mut cookies_string = if let Some(index) = headers diff --git a/core/http/src/handling/request/request_mime.rs b/core/http/src/handling/request/request_mime.rs index 94ebc4c0662909413329639f94ffaeb64965da9d..03ac55a3557c495fb4e59f5b223059ba77d731f7 100644 --- a/core/http/src/handling/request/request_mime.rs +++ b/core/http/src/handling/request/request_mime.rs @@ -1,58 +1,101 @@ -use crate::utils::mime::mime_enum::Mime; - use super::datatypes::Request; impl Request<'_> { - pub fn extract_mime_from_vec(headers: &[String]) -> Option<Mime> { - let Some(content_type_header) = headers + /// Sets the `mime_type` of this [`Request`] from the headers. + /// + /// The mime_type can remain none if there isn't a `Content-Type: ` header + /// + /// # Example + /// ``` + /// use http::{ + /// handling::{methods::Method, request::Request}, + /// utils::mime::mime_enum::Mime, + /// }; + /// + /// let mut request = Request { + /// uri: "thing", + /// headers: vec![ + /// "GET / 23".to_string(), + /// "SDF:LKJSD:F".to_string(), + /// "Content-Type: text/plain".to_string(), + /// "SDF".to_string(), + /// ], + /// method: Method::Get, + /// cookies: None, + /// mime_type: None, + /// }; + /// let mut wrong = Request { + /// uri: "thing", + /// headers: vec![ + /// "GET / 23".to_string(), + /// "SDF:LKJSD:F".to_string(), + /// "SDF".to_string(), + /// ], + /// method: Method::Get, + /// cookies: None, + /// mime_type: None, + /// }; + /// request.mime_from_headers(); + /// wrong.mime_from_headers(); + /// assert_eq!(None, wrong.mime_type); + /// assert_eq!(Mime::TextPlain, request.mime_type.unwrap()); + /// ``` + /// # Panics + /// No Panics + pub fn mime_from_headers(&mut self) { + let Some(content_type_header) = self.headers .iter() .find(|header| header.starts_with("Content-Type: ")) else { - return None; + return; }; let content_type_string = content_type_header .strip_prefix("Content-Type: ") .unwrap() .to_string(); - let mime = match content_type_string.split_once(';') { - Some(sub) => { - if let Ok(a) = sub.0.trim().parse() { - a - } else { - return None; - } - } - None => { - if let Ok(a) = content_type_string.trim().parse() { - a - } else { - return None; - } - } + self.mime_type = match content_type_string.split_once(';') { + Some(sub) => sub.0.trim().parse().ok(), + None => content_type_string.trim().parse().ok(), }; - Some(mime) } } #[cfg(test)] mod test { - use crate::{handling::request::Request, utils::mime::mime_enum::Mime}; + use crate::{ + handling::{methods::Method, request::Request}, + utils::mime::mime_enum::Mime, + }; #[test] pub fn test_mime_parse_from_header_vec() { - let mut right = vec![ - "GET / 23".to_string(), - "SDF:LKJSD:F".to_string(), - "Content-Type: text/plain".to_string(), - "SDF".to_string(), - ]; - let mut wrong = vec!["SDF:LKJSD:F".to_string(), "SDF".to_string()]; + let mut request = Request { + uri: "thing", + headers: vec![ + "GET / 23".to_string(), + "SDF:LKJSD:F".to_string(), + "Content-Type: text/plain".to_string(), + "SDF".to_string(), + ], + method: Method::Get, + cookies: None, + mime_type: None, + }; - assert_eq!(None, Request::extract_mime_from_vec(&mut wrong)); - assert_eq!( - Mime::TextPlain, - Request::extract_mime_from_vec(&mut right).unwrap() - ); - assert_eq!(None, Request::extract_mime_from_vec(&mut right)); + let mut wrong = Request { + uri: "thing", + headers: vec![ + "GET / 23".to_string(), + "SDF:LKJSD:F".to_string(), + "SDF".to_string(), + ], + method: Method::Get, + cookies: None, + mime_type: None, + }; + request.mime_from_headers(); + wrong.mime_from_headers(); + assert_eq!(None, wrong.mime_type); + assert_eq!(Mime::TextPlain, request.mime_type.unwrap()); } } diff --git a/core/http/src/lib.rs b/core/http/src/lib.rs index a51c899e8bba0326d49c41a31083d8e24c0a2775..1d6353a848ca924e164ae17aa09e4861f6d30cdc 100644 --- a/core/http/src/lib.rs +++ b/core/http/src/lib.rs @@ -2,7 +2,7 @@ pub mod handlers; pub mod handling; mod setup; -mod utils; +pub mod utils; #[cfg(test)] mod tests {}