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

FIX: Uri Pretty print

parent 53a054d8
No related branches found
No related tags found
1 merge request!2Project Completed
......@@ -143,11 +143,7 @@ pub async fn handle_connection<T: AsyncRead + AsyncWrite + std::marker::Unpin>(m
if !route.uri.compare_uri(&request.uri) {
continue;
}
handled_response = Some((route.handler)(
request.clone()
,
data.clone(),
));
handled_response = Some((route.handler)(request.clone(), data.clone()));
if let Some(Outcome::Forward(_)) = handled_response {
continue;
......
......@@ -248,18 +248,13 @@ impl Request {
}
#[cfg(test)]
mod test {
use std::str::FromStr;
use crate::{
handling::{
methods::Method,
request::{datatypes::ParseErrors, ParseFormError},
routes::Data,
},
utils::{
mime::Mime::{ApplicationXWwwFormUrlencoded, MultipartFormData},
url_utils::Uri,
},
utils::mime::Mime::{ApplicationXWwwFormUrlencoded, MultipartFormData},
};
use super::Request;
......
......@@ -11,7 +11,7 @@ use tokio_native_tls::{native_tls::{Identity, self}, TlsAcceptor};
use crate::{
handlers::handler::handle_connection,
handling::routes::Route, utils::{url_utils::Uri, vec_utils::remove_n},
handling::routes::Route, utils::url_utils::Uri,
};
#[cfg(feature = "secure")]
use crate::handling::response::{Response, Status};
......@@ -40,22 +40,6 @@ pub struct Config {
tls_acceptor: TlsAcceptor,
}
impl MountPoint {
pub fn compare_with_uri(&self, uri: &mut Uri) -> bool {
let mut uri_iter = uri.parts().iter();
let to_remove_after_finish = self.mountpoint.parts().len();
for part in self.mountpoint.parts().iter() {
let Some(part_uri) = uri_iter.next() else {
return false;
};
if part != part_uri {
return false;
}
}
remove_n(uri.mut_parts(), to_remove_after_finish);
true
}
}
impl<'a> Config {
/// Utility that checks if the given mointpoint is already taken. takes in the uri of the to be
......@@ -78,7 +62,7 @@ impl<'a> Config {
return self;
}
routes.sort_by(|a, b| a.rank.cmp(&b.rank));
let mut mount_message = format!(" >> \x1b[35m{}\x1b[0m\n", mountpoint);
let mut mount_message = format!(" >> \x1b[35m{}\x1b[0m\n", mountpoint.to_pretty_print_string());
for (index, route) in routes.iter().enumerate() {
let indent_sign = match index {
i if i == routes.len() - 1 => "└─",
......@@ -86,11 +70,10 @@ impl<'a> Config {
};
mount_message += &format!(
" \x1b[35m{indent_sign}\x1b[0m \x1b[36m(\x1b[0m{}\x1b[36m)\x1b[0m \x1b[32m{}\x1b[0m \x1b[34;4m{}\x1b[24m{}\x1b[0m\n",
" \x1b[35m{indent_sign}\x1b[0m \x1b[36m(\x1b[0m{}\x1b[36m)\x1b[0m \x1b[32m{}\x1b[0m {}\n",
route.name.unwrap_or(""),
route.method,
mountpoint,
route.uri
route.uri.to_pretty_print_string(&mountpoint)
)
}
......
......@@ -5,6 +5,7 @@ use crate::utils::urlencoded::UrlEncodeData;
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone)]
pub struct Uri {
pub(super) parts: Vec<UrlEncodeData>,
pub raw: String,
}
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone)]
......
use std::str::FromStr;
use crate::utils::{url_utils::datatypes::RawUriElement, urlencoded::UrlEncodeData};
use crate::{
setup::MountPoint,
utils::{url_utils::datatypes::RawUriElement, urlencoded::UrlEncodeData, vec_utils::remove_n},
};
use super::datatypes::{ParseUriError, RawUri, Uri};
impl MountPoint {
pub fn compare_with_uri(&self, uri: &mut Uri) -> bool {
if !uri.raw.starts_with(&self.mountpoint.raw) {
return false;
}
let to_remove_after_finish = self.mountpoint.parts.len();
remove_n(uri.mut_parts(), to_remove_after_finish);
true
}
}
impl Uri {
pub fn new(parts: Vec<&str>) -> Self {
Self {
raw: "/".to_owned() + &parts.join("/"),
parts: parts.into_iter().map(UrlEncodeData::from_raw).collect(),
}
}
......@@ -28,6 +43,18 @@ impl Uri {
}
true
}
pub(crate) fn to_pretty_print_string(&self) -> String {
if self.parts.is_empty() {
"/".to_string()
} else {
let url = self
.parts
.iter()
.map(|part| part.encoded())
.collect::<Vec<_>>();
"/".to_string() + &url.join("/")
}
}
}
impl RawUri {
......@@ -35,7 +62,7 @@ impl RawUri {
let mut result = Self {
infinte_end: false,
parts: Vec::with_capacity(parts.len()),
raw_string: "/".to_owned() + &parts.join("/"),
raw_string: parts.join("/"),
};
for part in parts {
if part.starts_with('<') && part.ends_with("..>") {
......@@ -52,15 +79,15 @@ impl RawUri {
}
result
}
pub fn compare_uri(self, uri: &Uri) -> bool {
pub fn compare_uri(&self, uri: &Uri) -> bool {
let mut iter_comp = uri.parts.iter();
let mut counter = 0;
if uri.parts().len() != self.parts.len() && !self.infinte_end {
return false;
}
for element in self.parts.iter() {
counter += 1;
let Some(compare_element) = iter_comp.next() else {
return false;
};
if *element == RawUriElement::Variable {
continue;
}
......@@ -72,11 +99,19 @@ impl RawUri {
return false;
}
}
if counter > self.parts.len() && !self.infinte_end {
return false;
if uri.parts.len() > self.parts.len() && self.infinte_end {
return true;
}
true
}
pub(crate) fn to_pretty_print_string(&self, is_after: &Uri) -> String {
let is_after = is_after.to_pretty_print_string();
if is_after == "/" {
format!("\x1b[34;4m/\x1b[24m{}\x1b[0m", self)
} else {
format!("\x1b[34;4m{is_after}/\x1b[24m{self}\x1b[0m")
}
}
}
impl std::fmt::Display for RawUri {
......@@ -87,23 +122,22 @@ impl std::fmt::Display for RawUri {
impl std::fmt::Display for Uri {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let url = self
.parts
.iter()
.map(|part| part.encoded())
.collect::<Vec<_>>();
write!(f, "/{}", url.join("/"))
if self.parts.is_empty() {
write!(f, "/")
} else {
let url = self
.parts
.iter()
.map(|part| part.encoded())
.collect::<Vec<_>>();
write!(f, "{}", url.join("/"))
}
}
}
impl FromStr for Uri {
type Err = ParseUriError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if s == "/" {
return Ok(Self {
parts: vec![UrlEncodeData::from_encoded("").unwrap()],
});
}
let split = s.split('/');
let mut result = Vec::new();
for sub in split {
......@@ -116,7 +150,10 @@ impl FromStr for Uri {
UrlEncodeData::from_raw(sub)
});
}
Ok(Self { parts: result })
Ok(Self {
parts: result,
raw: s.to_string(),
})
}
}
......@@ -127,7 +164,7 @@ impl FromStr for RawUri {
let mut result = Self {
infinte_end: false,
parts: Vec::new(),
raw_string: parts.join("/"),
raw_string: s.to_string(),
};
for part in parts {
if part.is_empty() {
......@@ -145,6 +182,7 @@ impl FromStr for RawUri {
.parts
.push(RawUriElement::Name(UrlEncodeData::from_raw(part)))
}
println!("{:?}", result);
Ok(result)
}
}
......
......@@ -5,7 +5,8 @@ use http::handling::{
response::{Response, Outcome, Status},
file_handlers::NamedFile};
fn static_files_handler(request: Request<>, _data: Data) -> Outcome<Response, Status, Data> {
fn static_files_handler(request: Request, _data: Data) -> Outcome<Response, Status, Data> {
println!("{:?} {}", request.uri, request.uri);
let response = static_files(&request.uri.to_string());
let response = match response {
Ok(dat) => Response {
......@@ -56,7 +57,7 @@ async fn main() {
format: None,
handler: static_files_handler,
name: Some("static files"),
uri: "".try_into().unwrap(),
uri: "<file..>".try_into().unwrap(),
method: Method::Get,
rank: 0
};
......
......@@ -8,7 +8,7 @@
<body>
<header>
<nav>
<div>LOGO</div>
<div><img src="/favicon.ico" alt="ICON" width="32" height="32" /></div>
<div>
<div>Dashboard</div>
<div>Books</div>
......
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