Skip to content
Snippets Groups Projects
Commit 901adb2c authored by codecraft's avatar codecraft :crocodile:
Browse files

Add documentation for `request.mime_from_headers()` and

`extract_cookies_from_vec()`. Update `mime_from_headers()` to take a
mutable reference to self
parent aaceb700
No related branches found
No related tags found
1 merge request!1Initial feature merge
......@@ -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 {
......
......@@ -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
......
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());
}
}
......@@ -2,7 +2,7 @@ pub mod handlers;
pub mod handling;
mod setup;
mod utils;
pub mod utils;
#[cfg(test)]
mod tests {}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment