From 901adb2c57f7cfa256a59e53457db5d958450318 Mon Sep 17 00:00:00 2001
From: Darius Auding <Darius.auding@gmx.de>
Date: Sun, 25 Jun 2023 21:53:57 +0200
Subject: [PATCH] Add documentation for `request.mime_from_headers()` and
 `extract_cookies_from_vec()`. Update `mime_from_headers()` to take a mutable
 reference to self

---
 core/http/src/handlers/handler.rs             |   2 +-
 core/http/src/handling/request/cookies.rs     |  35 ++++++
 .../http/src/handling/request/request_mime.rs | 113 ++++++++++++------
 core/http/src/lib.rs                          |   2 +-
 4 files changed, 115 insertions(+), 37 deletions(-)

diff --git a/core/http/src/handlers/handler.rs b/core/http/src/handlers/handler.rs
index 99f8300..9596f52 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 d078c76..b386086 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 94ebc4c..03ac55a 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 a51c899..1d6353a 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 {}
-- 
GitLab