diff --git a/src/main.rs b/src/main.rs
index e1fef7e..17eb936 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -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"
Hello World!Ahojky
Jou jou jou
".to_vec())
+ .with_data(b"Hello World!HTTP server testing
Lorem ipsum
".to_vec())
.with_header(ResponseHeader::ContentType(Content::html_utf8())).with_header(ResponseHeader::Connection(Connection::KeepAlive)),
};
diff --git a/src/request.rs b/src/request.rs
index a08301d..e1afcde 100644
--- a/src/request.rs
+++ b/src/request.rs
@@ -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::, 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,
+ pub path: Box<[Box]>,
pub query: Option, Box)]>>,
}
+impl ServerPath {
+ pub fn to_matchable(&self) -> Vec<&str> {
+ self.path.iter().map(|s| s.as_ref()).collect::>()
+ }
+}
+
impl FromStr for ServerPath {
type Err = io::Error;
fn from_str(s: &str) -> Result {
+ let get_path = |path: &&str| {
+ path.split('/')
+ .filter(|s| !s.is_empty() && !s.starts_with('.'))
+ .map(|s| s.into())
+ .collect::>>()
+ .into_boxed_slice()
+ };
+
match s.split("?").collect::>().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()),
})
}
diff --git a/src/response.rs b/src/response.rs
index 77cc58a..10d999a 100644
--- a/src/response.rs
+++ b/src/response.rs
@@ -40,12 +40,12 @@ impl Response {
.collect::>()
.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 {
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(),
}
}
}
diff --git a/src/shared_enums.rs b/src/shared_enums.rs
index aa58948..12e7ccc 100644
--- a/src/shared_enums.rs
+++ b/src/shared_enums.rs
@@ -11,12 +11,10 @@ pub enum ContentType {
impl ContentType {
fn to_str(&self) -> Box {
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 {
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::>()
.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,