better path
This commit is contained in:
parent
a9ccdaf3f5
commit
772cb421cb
4 changed files with 39 additions and 43 deletions
|
|
@ -29,12 +29,12 @@ fn main() -> std::io::Result<()> {
|
||||||
|
|
||||||
println!("{req:?}");
|
println!("{req:?}");
|
||||||
|
|
||||||
let response = match req.path.path.to_string().as_str(){
|
let response = match req.path.to_matchable().as_slice(){
|
||||||
"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)))),
|
["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()
|
_ => Response::new()
|
||||||
.with_code(ResponseCode::Ok)
|
.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)),
|
.with_header(ResponseHeader::ContentType(Content::html_utf8())).with_header(ResponseHeader::Connection(Connection::KeepAlive)),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -89,7 +89,7 @@ impl FromStr for RequestHeader {
|
||||||
"close" => Connection::Close,
|
"close" => Connection::Close,
|
||||||
"keep-alive" => Connection::KeepAlive,
|
"keep-alive" => Connection::KeepAlive,
|
||||||
"Upgrade" => Connection::Upgrade,
|
"Upgrade" => Connection::Upgrade,
|
||||||
other => Connection::Other(other.to_string().into_boxed_str()),
|
other => Connection::Other(other.into()),
|
||||||
})),
|
})),
|
||||||
"Upgrade" => Ok(RequestHeader::Upgrade(
|
"Upgrade" => Ok(RequestHeader::Upgrade(
|
||||||
value
|
value
|
||||||
|
|
@ -98,8 +98,8 @@ impl FromStr for RequestHeader {
|
||||||
.collect::<Result<Vec<Upgrade>, io::Error>>()?,
|
.collect::<Result<Vec<Upgrade>, io::Error>>()?,
|
||||||
)),
|
)),
|
||||||
_ => Ok(RequestHeader::Other {
|
_ => Ok(RequestHeader::Other {
|
||||||
name: header_type.to_string().into_boxed_str(),
|
name: (*header_type).into(),
|
||||||
value: value.to_string().into_boxed_str(),
|
value: (*value).into(),
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
_ => Err(io::Error::new(
|
_ => Err(io::Error::new(
|
||||||
|
|
@ -148,7 +148,7 @@ impl Request {
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
method: parsed_first_line.0,
|
method: parsed_first_line.0,
|
||||||
path: parsed_first_line.1,
|
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(),
|
headers: headers.into_boxed_slice(),
|
||||||
body: None,
|
body: None,
|
||||||
})
|
})
|
||||||
|
|
@ -188,21 +188,31 @@ impl Request {
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct ServerPath {
|
pub struct ServerPath {
|
||||||
pub path: Box<str>,
|
pub path: Box<[Box<str>]>,
|
||||||
pub query: Option<Box<[(Box<str>, 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 {
|
impl FromStr for ServerPath {
|
||||||
type Err = io::Error;
|
type Err = io::Error;
|
||||||
|
|
||||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
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() {
|
match s.split("?").collect::<Vec<&str>>().as_slice() {
|
||||||
[path] => Ok(Self {
|
[path] => Ok(Self {
|
||||||
path: path
|
path: get_path(path),
|
||||||
.split('/')
|
|
||||||
.filter(|s| !s.is_empty() && !s.starts_with('.'))
|
|
||||||
.map(|s| s.to_string())
|
|
||||||
.collect(),
|
|
||||||
query: None,
|
query: None,
|
||||||
}),
|
}),
|
||||||
[path, query] => {
|
[path, query] => {
|
||||||
|
|
@ -217,10 +227,7 @@ impl FromStr for ServerPath {
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
query_hashset.insert(parameter.to_string());
|
query_hashset.insert(parameter.to_string());
|
||||||
Some((
|
Some(((*parameter).into(), (*value).into()))
|
||||||
parameter.to_string().into_boxed_str(),
|
|
||||||
value.to_string().into_boxed_str(),
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => None,
|
_ => None,
|
||||||
|
|
@ -228,11 +235,7 @@ impl FromStr for ServerPath {
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
path: path
|
path: get_path(path),
|
||||||
.split('/')
|
|
||||||
.filter(|s| !s.is_empty() && !s.starts_with('.'))
|
|
||||||
.map(|s| s.to_string())
|
|
||||||
.collect(),
|
|
||||||
query: Some(query_parsed.into_boxed_slice()),
|
query: Some(query_parsed.into_boxed_slice()),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -40,12 +40,12 @@ impl Response {
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
.join("\r\n")
|
.join("\r\n")
|
||||||
)
|
)
|
||||||
.into_boxed_str()
|
.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
http_version: "HTTP/1.1".to_owned().into_boxed_str(),
|
http_version: "HTTP/1.1".to_owned().into(),
|
||||||
code: ResponseCode::Ok,
|
code: ResponseCode::Ok,
|
||||||
headers: vec![],
|
headers: vec![],
|
||||||
data: vec![],
|
data: vec![],
|
||||||
|
|
@ -225,11 +225,9 @@ impl ResponseHeader {
|
||||||
fn to_str(&self) -> Box<str> {
|
fn to_str(&self) -> Box<str> {
|
||||||
type R = ResponseHeader;
|
type R = ResponseHeader;
|
||||||
match self {
|
match self {
|
||||||
R::ContentType(content) => {
|
R::ContentType(content) => format!("Content-Type: {}", content.to_str()).into(),
|
||||||
format!("Content-Type: {}", content.to_str()).into_boxed_str()
|
R::CacheControl(c) => format!("Cache-Control: {}", c.to_str()).into(),
|
||||||
}
|
R::Connection(c) => format!("Connection: {}", c.to_str()).into(),
|
||||||
R::CacheControl(c) => format!("Cache-Control: {}", c.to_str()).into_boxed_str(),
|
|
||||||
R::Connection(c) => format!("Connection: {}", c.to_str()).into_boxed_str(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,12 +11,10 @@ pub enum ContentType {
|
||||||
impl ContentType {
|
impl ContentType {
|
||||||
fn to_str(&self) -> Box<str> {
|
fn to_str(&self) -> Box<str> {
|
||||||
match self {
|
match self {
|
||||||
ContentType::Text(text) => format!("text/{}", text.to_str()).into_boxed_str(),
|
ContentType::Text(text) => format!("text/{}", text.to_str()).into(),
|
||||||
ContentType::Aplication(app) => {
|
ContentType::Aplication(app) => format!("application/{}", app.to_str()).into(),
|
||||||
format!("application/{}", app.to_str()).into_boxed_str()
|
ContentType::Image(img) => format!("image/{}", img.to_str()).into(),
|
||||||
}
|
ContentType::Any => "*/*".into(),
|
||||||
ContentType::Image(img) => format!("image/{}", img.to_str()).into_boxed_str(),
|
|
||||||
ContentType::Any => "*/*".to_string().into_boxed_str(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -31,9 +29,9 @@ pub enum Parameter {
|
||||||
impl Parameter {
|
impl Parameter {
|
||||||
fn to_str(&self) -> Box<str> {
|
fn to_str(&self) -> Box<str> {
|
||||||
match &self {
|
match &self {
|
||||||
Parameter::Preference(val) => format!("q={val}").into_boxed_str(),
|
Parameter::Preference(val) => format!("q={val}").into(),
|
||||||
Parameter::Charset(ch) => format!("charset={}", ch.to_str()).into_boxed_str(),
|
Parameter::Charset(ch) => format!("charset={}", ch.to_str()).into(),
|
||||||
Parameter::Other(p, v) => format!("{p}={v}").into_boxed_str(),
|
Parameter::Other(p, v) => format!("{p}={v}").into(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -68,7 +66,7 @@ impl Content {
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
.join("; ")
|
.join("; ")
|
||||||
)
|
)
|
||||||
.into_boxed_str(),
|
.into(),
|
||||||
None => self.content_type.to_str(),
|
None => self.content_type.to_str(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -200,10 +198,7 @@ impl FromStr for Parameter {
|
||||||
|
|
||||||
["charset", "utf-8"] => Ok(Parameter::Charset(Charset::UTF8)),
|
["charset", "utf-8"] => Ok(Parameter::Charset(Charset::UTF8)),
|
||||||
|
|
||||||
[t, v] => Ok(Parameter::Other(
|
[t, v] => Ok(Parameter::Other((*t).into(), (*v).into())),
|
||||||
t.to_string().into_boxed_str(),
|
|
||||||
v.to_string().into_boxed_str(),
|
|
||||||
)),
|
|
||||||
|
|
||||||
_ => Err(io::Error::new(
|
_ => Err(io::Error::new(
|
||||||
io::ErrorKind::InvalidData,
|
io::ErrorKind::InvalidData,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue