diff --git a/core/http/src/handlers/handler.rs b/core/http/src/handlers/handler.rs index f1847b293016a975dd8f7b6802619994c63e9d95..fa5f75a6942fe178681ec868ecdabe3b641a67fa 100644 --- a/core/http/src/handlers/handler.rs +++ b/core/http/src/handlers/handler.rs @@ -1,8 +1,8 @@ use std::{io, path::PathBuf}; use tokio::{ - io::{AsyncBufReadExt, AsyncReadExt, BufReader, AsyncWriteExt}, - net::TcpStream, stream, + io::{AsyncBufReadExt, AsyncReadExt, BufReader}, + net::TcpStream, }; use tokio_native_tls::{TlsStream, TlsAcceptor}; @@ -215,14 +215,14 @@ fn error_occured_when_writing(e: io::Error) { eprintln!("\x1b[31mError {e} occured when trying to write answer to TCP-Stream for Client, aborting\x1b[0m"); } -async fn redirect_to_secure(mut stream: TcpStream) { - let resp = Response { - cookies: None, - status: Some(Status::MovedPermanently), - headers: vec!["Location: https://127.0.0.1".to_string()], - body: Box::new("") - }.build(None); - if let Err(e) = stream.write_all(&resp).await { - error_occured_when_writing(e); - } -} +// async fn redirect_to_secure(mut stream: TcpStream) { +// let resp = Response { +// cookies: None, +// status: Some(Status::MovedPermanently), +// headers: vec!["Location: https://127.0.0.1".to_string()], +// body: Box::new("") +// }.build(None); +// if let Err(e) = stream.write_all(&resp).await { +// error_occured_when_writing(e); +// } +// } diff --git a/core/http/src/handling/request/form_utils.rs b/core/http/src/handling/request/form_utils.rs index 67ada474f0407637a80d4932c5bcc747927c1765..cc8999d7cc5eef4d0fa667c9c93d0dff0442572c 100644 --- a/core/http/src/handling/request/form_utils.rs +++ b/core/http/src/handling/request/form_utils.rs @@ -143,7 +143,7 @@ impl Request { let Some(thing) = keymap.get_mut(&key) else { continue; }; - *thing = Ok(value.into()); + *thing = Ok(value); } } Mime::MultipartFormData => { diff --git a/core/http/src/handling/response/response.rs b/core/http/src/handling/response/build_and_write.rs similarity index 100% rename from core/http/src/handling/response/response.rs rename to core/http/src/handling/response/build_and_write.rs diff --git a/core/http/src/handling/response/cookie_management/cookie.rs b/core/http/src/handling/response/cookie_management/cookie.rs index 8234ab46ee914dd92e9666d390d7c44ac5b54384..6175266c4539d5465d00c13596074dcab72ad4d0 100644 --- a/core/http/src/handling/response/cookie_management/cookie.rs +++ b/core/http/src/handling/response/cookie_management/cookie.rs @@ -102,7 +102,7 @@ impl std::fmt::Display for Cookie { if let Some(same_site) = &self.same_site { appendix += &format!("; {}", same_site); if !self.secure && *same_site == SameSite::None { - appendix += &format!("; Secure"); + appendix += "; Secure"; } } if let Some(expires) = &self.expires { diff --git a/core/http/src/handling/response/mod.rs b/core/http/src/handling/response/mod.rs index 535eaa812a2192d9b897d3c506f1e0f48dacfd88..9233b38f814a27a9825c6e6b48a7c70807ef333b 100644 --- a/core/http/src/handling/response/mod.rs +++ b/core/http/src/handling/response/mod.rs @@ -1,6 +1,6 @@ +mod build_and_write; mod cookie_management; mod datatypes; -mod response; mod status; mod traits; diff --git a/core/http/src/utils/url_utils/uri.rs b/core/http/src/utils/url_utils/uri.rs index b9e6939e183f22df30d13df4d6d2834e24d1373b..6b8146be14f1297f202e4fc59bcaa93928a5fbd5 100644 --- a/core/http/src/utils/url_utils/uri.rs +++ b/core/http/src/utils/url_utils/uri.rs @@ -7,10 +7,7 @@ use super::datatypes::{ParseUriError, RawUri, Uri, UriError}; impl Uri { pub fn new(parts: Vec<&str>) -> Self { Self { - parts: parts - .into_iter() - .map(|part| UrlEncodeData::from_raw(part)) - .collect(), + parts: parts.into_iter().map(UrlEncodeData::from_raw).collect(), } } } @@ -23,11 +20,11 @@ impl RawUri { raw_string: "/".to_owned() + &parts.join("/"), }; for part in parts { - if part.starts_with("<") && part.ends_with("..>") { + if part.starts_with('<') && part.ends_with("..>") { result.infinte_end = true; break; } - if part.starts_with("<") && part.ends_with(">") { + if part.starts_with('<') && part.ends_with('>') { result.parts.push(RawUriElement::Variable); continue; } @@ -114,11 +111,11 @@ impl FromStr for RawUri { if part.is_empty() { continue; } - if part.starts_with("<") && part.ends_with("..>") { + if part.starts_with('<') && part.ends_with("..>") { result.infinte_end = true; break; } - if part.starts_with("<") && part.ends_with(">") { + if part.starts_with('<') && part.ends_with('>') { result.parts.push(RawUriElement::Variable); continue; } diff --git a/core/http/src/utils/urlencoded/datatypes.rs b/core/http/src/utils/urlencoded/datatypes.rs index d48e02be890d6ef0362c6f3040006136b2a46346..32079556861b204ba03c414b1fe1c0832faa2029 100644 --- a/core/http/src/utils/urlencoded/datatypes.rs +++ b/core/http/src/utils/urlencoded/datatypes.rs @@ -13,6 +13,24 @@ pub struct UrlEncodeData { pub(crate) raw_string: Option<String>, } +#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)] +pub enum UrlEncodeError { + NonHexAfterPercent, +} + +#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)] +pub struct ParseUrlEncodeError { + pub inner: UrlEncodeError, +} + +impl std::fmt::Display for ParseUrlEncodeError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{:?}", self) + } +} + +impl std::error::Error for ParseUrlEncodeError {} + impl UrlEncodeData { /// Generates a [UrlEncodeData] from any raw data that can be a slice of [u8] pub fn from_raw<T: AsRef<[u8]>>(raw: T) -> Self { @@ -27,7 +45,7 @@ impl UrlEncodeData { /// # Errors /// /// errors if the encoded data is wrongly encoded -> %<invalid_character> - pub fn from_encoded(encoded: &str) -> Result<Self, ()> { + pub fn from_encoded(encoded: &str) -> super::UriParseResult<Self> { Ok(Self { encoded: encoded.to_owned(), raw: encoded.decode()?, diff --git a/core/http/src/utils/urlencoded/endecode.rs b/core/http/src/utils/urlencoded/endecode.rs index 8dd45d666a285cea508fe91e698e073e3148825c..8a77253972bde4185914391294ab4670c35f4e75 100644 --- a/core/http/src/utils/urlencoded/endecode.rs +++ b/core/http/src/utils/urlencoded/endecode.rs @@ -1,3 +1,5 @@ +use super::{datatypes::ParseUrlEncodeError, UriParseResult}; + /// Base of the HexaDecimal Number system static BASE16_HEXA_DECIMAL: u8 = 0x10; /// Highest possible Value per digit @@ -15,7 +17,7 @@ pub trait DeCodable { /// /// # Errors /// Errors if the encoding isn't right - fn decode(&self) -> Result<Vec<u8>, ()>; + fn decode(&self) -> UriParseResult<Vec<u8>>; } impl<T: AsRef<[u8]>> EnCodable for T { @@ -44,7 +46,7 @@ impl EnCodable for [u8] { } } impl DeCodable for &str { - fn decode(&self) -> Result<Vec<u8>, ()> { + fn decode(&self) -> UriParseResult<Vec<u8>> { let mut first = true; let mut result = Vec::with_capacity(self.len()); @@ -55,7 +57,7 @@ impl DeCodable for &str { continue; } let Ok(char) = u8::from_str_radix(i[0..=1].as_ref(), BASE16_HEXA_DECIMAL.into()) else { - return Err(()); + return Err(ParseUrlEncodeError { inner: super::datatypes::UrlEncodeError::NonHexAfterPercent }); }; result.push(char); result.extend_from_slice(i[2..].as_bytes()); @@ -97,7 +99,9 @@ mod test { .unwrap() ); assert_eq!( - Err(()), + Err(crate::utils::urlencoded::datatypes::ParseUrlEncodeError { + inner: crate::utils::urlencoded::datatypes::UrlEncodeError::NonHexAfterPercent + }), "Darius%2iis%20the%20biggest%20genius%2FGenie%2FHuman%20extraordin%C3%A4ire".decode() ); assert_eq!( diff --git a/core/http/src/utils/urlencoded/mod.rs b/core/http/src/utils/urlencoded/mod.rs index 3145200e62492c82c37385fa9aa0e1d69c48e5eb..bf5f2929e3402acbbe07cce909947008a7d15a96 100644 --- a/core/http/src/utils/urlencoded/mod.rs +++ b/core/http/src/utils/urlencoded/mod.rs @@ -1,4 +1,7 @@ mod datatypes; mod endecode; +use std::result; + +type UriParseResult<T> = result::Result<T, datatypes::ParseUrlEncodeError>; pub use datatypes::UrlEncodeData; pub use endecode::{DeCodable, EnCodable}; diff --git a/site/src/main.rs b/site/src/main.rs index 22e790001eaafc456015137a3ff01d618e516614..014d33a424b0b0339e6f5ec4055574a5ea8f1dd7 100644 --- a/site/src/main.rs +++ b/site/src/main.rs @@ -105,7 +105,7 @@ async fn main() { rank: 0, }; - http::build("127.0.0.1:8000") + http::build("127.0.0.1:8080") .await .mount("/", vec![fileserver, post_test, static_hi]) .mount("/post/", vec![post_test])