pub struct Response<'res> {
pub response: &'res [u8],
}
pub enum Type {
PATH,
METHOD,
COOKIE,
CONTENT,
}
impl<'res> Response<'res> {
pub fn response(&mut self, types: Type) -> String {
let response = String::from_utf8_lossy(&self.response);
match types {
Type::METHOD => {
let method = response.split(" ").nth(0);
match method {
Some(method) => method.to_string(),
None => "method not found".to_string(),
}
}
Type::PATH => {
let path = response.split(" ").nth(1);
match path {
Some(path) => path.to_string(),
None => "path not found".to_string(),
}
}
Type::CONTENT => {
let content = response.find("\r\n\r\n");
match content {
Some(content) => {
let body = response[content..].trim().trim_end_matches("\0");
body.to_string()
}
None => "content not found".to_string(),
}
}
Type::COOKIE => {
let body = response.find("Cookie: ");
match body {
Some(cookie) => {
let body = &response[cookie..]
.trim()
.trim_start_matches("Cookie: ")
.trim_end_matches("\r\n\r\n");
body.to_string()
}
None => "Cookie not found".to_string(),
}
}
}
}
}