better path

This commit is contained in:
maxstrb 2025-10-18 20:42:33 +02:00
parent a9ccdaf3f5
commit 772cb421cb
4 changed files with 39 additions and 43 deletions

View file

@ -29,12 +29,12 @@ fn main() -> std::io::Result<()> {
println!("{req:?}");
let response = match req.path.path.to_string().as_str(){
"css.css" => Response::new().with_code(ResponseCode::Ok).with_data(b"body{background-color: #ff0000;}".to_vec()).with_header(ResponseHeader::ContentType(Content::new(ContentType::Text(shared_enums::TextType::Css)))),
let response = match req.path.to_matchable().as_slice(){
["css.css"] => Response::new().with_code(ResponseCode::Ok).with_data(b"body{background-color: #121212;} h1{color: #ffffff;} p{color: #025da9;}".to_vec()).with_header(ResponseHeader::ContentType(Content::new(ContentType::Text(shared_enums::TextType::Css)))),
_ => Response::new()
.with_code(ResponseCode::Ok)
.with_data(b"<!doctype html><html lang=\"en\"><head><link rel=\"stylesheet\" href=\"css.css\"><meta charset=\"UTF-8\"/><title>Hello World!</title></head><body><h1>Ahojky</h1><p>Jou jou jou</p></body></html>".to_vec())
.with_data(b"<!doctype html><html lang=\"en\"><head><link rel=\"stylesheet\" href=\"css.css\"><meta charset=\"UTF-8\"/><title>Hello World!</title></head><body><h1>HTTP server testing</h1><p>Lorem ipsum</p></body></html>".to_vec())
.with_header(ResponseHeader::ContentType(Content::html_utf8())).with_header(ResponseHeader::Connection(Connection::KeepAlive)),
};

View file

@ -89,7 +89,7 @@ impl FromStr for RequestHeader {
"close" => Connection::Close,
"keep-alive" => Connection::KeepAlive,
"Upgrade" => Connection::Upgrade,
other => Connection::Other(other.to_string().into_boxed_str()),
other => Connection::Other(other.into()),
})),
"Upgrade" => Ok(RequestHeader::Upgrade(
value
@ -98,8 +98,8 @@ impl FromStr for RequestHeader {
.collect::<Result<Vec<Upgrade>, io::Error>>()?,
)),
_ => Ok(RequestHeader::Other {
name: header_type.to_string().into_boxed_str(),
value: value.to_string().into_boxed_str(),
name: (*header_type).into(),
value: (*value).into(),
}),
},
_ => Err(io::Error::new(
@ -148,7 +148,7 @@ impl Request {
Ok(Self {
method: parsed_first_line.0,
path: parsed_first_line.1,
http_version: parsed_first_line.2.into_boxed_str(),
http_version: parsed_first_line.2.into(),
headers: headers.into_boxed_slice(),
body: None,
})
@ -188,21 +188,31 @@ impl Request {
#[derive(Debug)]
pub struct ServerPath {
pub path: Box<str>,
pub path: Box<[Box<str>]>,
pub query: Option<Box<[(Box<str>, Box<str>)]>>,
}
impl ServerPath {
pub fn to_matchable(&self) -> Vec<&str> {
self.path.iter().map(|s| s.as_ref()).collect::<Vec<&str>>()
}
}
impl FromStr for ServerPath {
type Err = io::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let get_path = |path: &&str| {
path.split('/')
.filter(|s| !s.is_empty() && !s.starts_with('.'))
.map(|s| s.into())
.collect::<Vec<Box<str>>>()
.into_boxed_slice()
};
match s.split("?").collect::<Vec<&str>>().as_slice() {
[path] => Ok(Self {
path: path
.split('/')
.filter(|s| !s.is_empty() && !s.starts_with('.'))
.map(|s| s.to_string())
.collect(),
path: get_path(path),
query: None,
}),
[path, query] => {
@ -217,10 +227,7 @@ impl FromStr for ServerPath {
None
} else {
query_hashset.insert(parameter.to_string());
Some((
parameter.to_string().into_boxed_str(),
value.to_string().into_boxed_str(),
))
Some(((*parameter).into(), (*value).into()))
}
}
_ => None,
@ -228,11 +235,7 @@ impl FromStr for ServerPath {
.collect();
Ok(Self {
path: path
.split('/')
.filter(|s| !s.is_empty() && !s.starts_with('.'))
.map(|s| s.to_string())
.collect(),
path: get_path(path),
query: Some(query_parsed.into_boxed_slice()),
})
}

View file

@ -40,12 +40,12 @@ impl Response {
.collect::<Vec<_>>()
.join("\r\n")
)
.into_boxed_str()
.into()
}
pub fn new() -> Self {
Self {
http_version: "HTTP/1.1".to_owned().into_boxed_str(),
http_version: "HTTP/1.1".to_owned().into(),
code: ResponseCode::Ok,
headers: vec![],
data: vec![],
@ -225,11 +225,9 @@ impl ResponseHeader {
fn to_str(&self) -> Box<str> {
type R = ResponseHeader;
match self {
R::ContentType(content) => {
format!("Content-Type: {}", content.to_str()).into_boxed_str()
}
R::CacheControl(c) => format!("Cache-Control: {}", c.to_str()).into_boxed_str(),
R::Connection(c) => format!("Connection: {}", c.to_str()).into_boxed_str(),
R::ContentType(content) => format!("Content-Type: {}", content.to_str()).into(),
R::CacheControl(c) => format!("Cache-Control: {}", c.to_str()).into(),
R::Connection(c) => format!("Connection: {}", c.to_str()).into(),
}
}
}

View file

@ -11,12 +11,10 @@ pub enum ContentType {
impl ContentType {
fn to_str(&self) -> Box<str> {
match self {
ContentType::Text(text) => format!("text/{}", text.to_str()).into_boxed_str(),
ContentType::Aplication(app) => {
format!("application/{}", app.to_str()).into_boxed_str()
}
ContentType::Image(img) => format!("image/{}", img.to_str()).into_boxed_str(),
ContentType::Any => "*/*".to_string().into_boxed_str(),
ContentType::Text(text) => format!("text/{}", text.to_str()).into(),
ContentType::Aplication(app) => format!("application/{}", app.to_str()).into(),
ContentType::Image(img) => format!("image/{}", img.to_str()).into(),
ContentType::Any => "*/*".into(),
}
}
}
@ -31,9 +29,9 @@ pub enum Parameter {
impl Parameter {
fn to_str(&self) -> Box<str> {
match &self {
Parameter::Preference(val) => format!("q={val}").into_boxed_str(),
Parameter::Charset(ch) => format!("charset={}", ch.to_str()).into_boxed_str(),
Parameter::Other(p, v) => format!("{p}={v}").into_boxed_str(),
Parameter::Preference(val) => format!("q={val}").into(),
Parameter::Charset(ch) => format!("charset={}", ch.to_str()).into(),
Parameter::Other(p, v) => format!("{p}={v}").into(),
}
}
}
@ -68,7 +66,7 @@ impl Content {
.collect::<Vec<_>>()
.join("; ")
)
.into_boxed_str(),
.into(),
None => self.content_type.to_str(),
}
}
@ -200,10 +198,7 @@ impl FromStr for Parameter {
["charset", "utf-8"] => Ok(Parameter::Charset(Charset::UTF8)),
[t, v] => Ok(Parameter::Other(
t.to_string().into_boxed_str(),
v.to_string().into_boxed_str(),
)),
[t, v] => Ok(Parameter::Other((*t).into(), (*v).into())),
_ => Err(io::Error::new(
io::ErrorKind::InvalidData,