Compare commits

...

2 Commits

Author SHA1 Message Date
cf48dc3e1c new but basic parse_field_line function 2025-03-27 12:23:11 +01:00
d5b4dcedd6 change signature of Hashmap 2025-03-27 09:51:44 +01:00

View File

@ -110,7 +110,7 @@ impl StartLine {
} }
fn parse_start_line(input: &str) -> Result<StartLine, Vec<u8>> { fn parse_start_line(input: &str) -> Result<StartLine, Vec<u8>> {
let mut response_field_lines: HashMap<String, String> = HashMap::new(); let mut response_field_lines: HashMap<String, Vec<String>> = HashMap::new();
let mut response_body: Vec<u8> = vec![]; let mut response_body: Vec<u8> = vec![];
let mut start_line = StartLine::new(); let mut start_line = StartLine::new();
let vec = input.trim().split_ascii_whitespace().collect::<Vec<&str>>(); let vec = input.trim().split_ascii_whitespace().collect::<Vec<&str>>();
@ -126,9 +126,12 @@ fn parse_start_line(input: &str) -> Result<StartLine, Vec<u8>> {
response_field_lines.insert( response_field_lines.insert(
String::from("Content-Length"), String::from("Content-Length"),
response_body.len().to_string(), vec![response_body.len().to_string()],
);
response_field_lines.insert(
String::from("Content-Type"),
vec![String::from("text/plain")],
); );
response_field_lines.insert(String::from("Content-Type"), String::from("text/plain"));
return Err(response_builder( return Err(response_builder(
RequestMethods::Get, RequestMethods::Get,
@ -166,9 +169,12 @@ fn parse_start_line(input: &str) -> Result<StartLine, Vec<u8>> {
response_field_lines.insert( response_field_lines.insert(
String::from("Content-Length"), String::from("Content-Length"),
response_body.len().to_string(), vec![response_body.len().to_string()],
);
response_field_lines.insert(
String::from("Content-Type"),
vec![String::from("text/plain")],
); );
response_field_lines.insert(String::from("Content-Type"), String::from("text/plain"));
return Err(response_builder( return Err(response_builder(
RequestMethods::Head, RequestMethods::Head,
@ -198,12 +204,79 @@ fn parse_start_line(input: &str) -> Result<StartLine, Vec<u8>> {
Ok(start_line) Ok(start_line)
} }
//TODO: Be able to parse this
// Accept: text/html, application/xhtml+xml, application/xml;q=0.9, */*;q=0.8
// Be careful of optional whitespace (OWS)
// q=x.yyy is basically a which I want most value the closer to 1 the better. If no q=x.yyy was specified, then q=1 is implied.
// This basically mean I should sort the vector by qvalue. So that the one it wants most, is at the beginning.
fn parse_field_line(field_line: (&str, &str)) -> (String, Vec<String>) {
let field_name = field_line.0.to_owned();
let mut fuck: HashMap<String, Vec<String>> = HashMap::new();
if !field_line.1.contains(",") {
return (field_name, vec![field_line.1.trim().to_owned()]);
}
let temp_field_values = field_line.1.split(",").collect::<Vec<&str>>();
for field_value in temp_field_values {
if field_value.contains(";") {
let temp_field_value = field_value.split(";").collect::<Vec<&str>>();
if temp_field_value.len() != 2 {
// TODO: return with an http_response
continue;
}
if !temp_field_value[1].starts_with("q=") && !temp_field_value[1].starts_with("Q=") {
// TODO: return with an http_response
continue;
}
let qvalue = temp_field_value[1]
.trim()
.trim_start_matches("q=")
.trim_start_matches("Q=");
if qvalue == "0" {
continue;
}
if let Some(val) = fuck.get_mut(qvalue) {
val.push(temp_field_value[0].trim().to_owned());
continue;
}
fuck.insert(
qvalue.to_owned(),
vec![temp_field_value[0].trim().to_owned()],
);
} else {
if let Some(val) = fuck.get_mut("1") {
val.push(field_value.trim().to_owned());
continue;
}
fuck.insert("1".to_owned(), vec![field_value.trim().to_owned()]);
}
}
let fuck_keys = fuck.keys();
let mut keys = fuck_keys.collect::<Vec<&String>>();
keys.sort_by(|a, b| b.cmp(a));
let mut field_values: Vec<String> = vec![];
for key in keys {
if let Some(value) = fuck.get(key) {
value.iter().for_each(|val| field_values.push(val.clone()));
}
}
(field_name, field_values)
}
fn parse_field_lines( fn parse_field_lines(
reader: &mut BufReader<&mut TcpStream>, reader: &mut BufReader<&mut TcpStream>,
) -> Result<HashMap<String, String>, Vec<u8>> { ) -> Result<HashMap<String, Vec<String>>, Vec<u8>> {
let mut response_field_lines: HashMap<String, String> = HashMap::new(); let mut response_field_lines: HashMap<String, Vec<String>> = HashMap::new();
let mut response_body: Vec<u8> = vec![]; let mut response_body: Vec<u8> = vec![];
let mut field_lines: HashMap<String, String> = HashMap::new(); let mut field_lines: HashMap<String, Vec<String>> = HashMap::new();
// Read field-lines till I hit an empty line // Read field-lines till I hit an empty line
loop { loop {
@ -218,9 +291,12 @@ fn parse_field_lines(
response_field_lines.insert( response_field_lines.insert(
String::from("Content-Length"), String::from("Content-Length"),
response_body.len().to_string(), vec![response_body.len().to_string()],
);
response_field_lines.insert(
String::from("Content-Type"),
vec![String::from("text/plain")],
); );
response_field_lines.insert(String::from("Content-Type"), String::from("text/plain"));
return Err(response_builder( return Err(response_builder(
RequestMethods::Get, RequestMethods::Get,
@ -234,8 +310,8 @@ fn parse_field_lines(
break; break;
} }
let field_line = match line.split_once(":") { let field_line: (String, Vec<String>) = match line.split_once(":") {
Some(val) => val, Some(val) => parse_field_line(val),
None => { None => {
"Invalid field-line" "Invalid field-line"
.as_bytes() .as_bytes()
@ -244,10 +320,12 @@ fn parse_field_lines(
response_field_lines.insert( response_field_lines.insert(
String::from("Content-Length"), String::from("Content-Length"),
response_body.len().to_string(), vec![response_body.len().to_string()],
);
response_field_lines.insert(
String::from("Content-Type"),
vec![String::from("text/plain")],
); );
response_field_lines
.insert(String::from("Content-Type"), String::from("text/plain"));
return Err(response_builder( return Err(response_builder(
RequestMethods::Get, RequestMethods::Get,
@ -258,7 +336,7 @@ fn parse_field_lines(
} }
}; };
field_lines.insert(field_line.0.to_owned(), field_line.1.trim().to_owned()); field_lines.insert(field_line.0, field_line.1);
} }
if !field_lines.contains_key(&String::from("Host")) { if !field_lines.contains_key(&String::from("Host")) {
@ -269,9 +347,12 @@ fn parse_field_lines(
response_field_lines.insert( response_field_lines.insert(
String::from("Content-Length"), String::from("Content-Length"),
response_body.len().to_string(), vec![response_body.len().to_string()],
);
response_field_lines.insert(
String::from("Content-Type"),
vec![String::from("text/plain")],
); );
response_field_lines.insert(String::from("Content-Type"), String::from("text/plain"));
return Err(response_builder( return Err(response_builder(
RequestMethods::Get, RequestMethods::Get,
@ -287,7 +368,7 @@ fn parse_field_lines(
fn response_builder( fn response_builder(
method: RequestMethods, method: RequestMethods,
status_line: &str, status_line: &str,
field_lines: Option<HashMap<String, String>>, field_lines: Option<HashMap<String, Vec<String>>>,
body: Option<Vec<u8>>, body: Option<Vec<u8>>,
) -> Vec<u8> { ) -> Vec<u8> {
let mut response: Vec<u8> = vec![]; let mut response: Vec<u8> = vec![];
@ -310,11 +391,9 @@ fn response_builder(
response.push(b':'); response.push(b':');
response.push(b' '); response.push(b' ');
field_line for val in field_line.1 {
.1 val.as_bytes().iter().for_each(|byte| response.push(*byte));
.as_bytes() }
.iter()
.for_each(|byte| response.push(*byte));
response.push(b'\r'); response.push(b'\r');
response.push(b'\n'); response.push(b'\n');
@ -336,8 +415,8 @@ fn response_builder(
response response
} }
fn try_get_file(start_line: &StartLine, _field_lines: &HashMap<String, String>) -> Vec<u8> { fn try_get_file(start_line: &StartLine, _field_lines: &HashMap<String, Vec<String>>) -> Vec<u8> {
let mut response_field_lines: HashMap<String, String> = HashMap::new(); let mut response_field_lines: HashMap<String, Vec<String>> = HashMap::new();
let mut response_body: Vec<u8> = vec![]; let mut response_body: Vec<u8> = vec![];
let path: PathBuf = match start_line.target.as_str() { let path: PathBuf = match start_line.target.as_str() {
@ -360,13 +439,13 @@ fn try_get_file(start_line: &StartLine, _field_lines: &HashMap<String, String>)
response_field_lines.insert( response_field_lines.insert(
String::from("Content-Length"), String::from("Content-Length"),
response_body.len().to_string(), vec![response_body.len().to_string()],
); );
let mime_type = mime_guess::from_path(&path) let mime_type = mime_guess::from_path(&path)
.first_raw() .first_raw()
.expect("Could not guess mime-type from path"); .expect("Could not guess mime-type from path");
response_field_lines.insert(String::from("Content-Type"), mime_type.to_string()); response_field_lines.insert(String::from("Content-Type"), vec![mime_type.to_string()]);
response_builder( response_builder(
start_line.method, start_line.method,
@ -382,7 +461,7 @@ fn try_get_file(start_line: &StartLine, _field_lines: &HashMap<String, String>)
fn handle_request(mut stream: TcpStream) -> Result<(), Box<dyn Error>> { fn handle_request(mut stream: TcpStream) -> Result<(), Box<dyn Error>> {
let mut line = String::new(); let mut line = String::new();
let mut reader = BufReader::new(&mut stream); let mut reader = BufReader::new(&mut stream);
let mut response_field_lines: HashMap<String, String> = HashMap::new(); let mut response_field_lines: HashMap<String, Vec<String>> = HashMap::new();
let mut response_body: Vec<u8> = vec![]; let mut response_body: Vec<u8> = vec![];
// Request can have one or many empty lines preceding the start-line and I will ignore these // Request can have one or many empty lines preceding the start-line and I will ignore these
@ -400,9 +479,12 @@ fn handle_request(mut stream: TcpStream) -> Result<(), Box<dyn Error>> {
response_field_lines.insert( response_field_lines.insert(
String::from("Content-Length"), String::from("Content-Length"),
response_body.len().to_string(), vec![response_body.len().to_string()],
);
response_field_lines.insert(
String::from("Content-Type"),
vec![String::from("text/plain")],
); );
response_field_lines.insert(String::from("Content-Type"), String::from("text/plain"));
let response = response_builder( let response = response_builder(
RequestMethods::Get, RequestMethods::Get,
@ -438,8 +520,8 @@ fn handle_request(mut stream: TcpStream) -> Result<(), Box<dyn Error>> {
let response = match start_line.target.as_str() { let response = match start_line.target.as_str() {
// For docker healtcheck. If the server can properly respond, then it must be healthy. // For docker healtcheck. If the server can properly respond, then it must be healthy.
"/server-health" => response_builder(RequestMethods::Head, "HTTP/1.1 200 ", None, None), "/server-health" => response_builder(RequestMethods::Head, "HTTP/1.1 200 ", None, None),
"/server-stats" => response_builder(start_line.method, "HTTP/1.1 404 ", None, None), // "/server-stats" => response_builder(start_line.method, "HTTP/1.1 404 ", None, None),
"/server-info" => response_builder(start_line.method, "HTTP/1.1 404 ", None, None), // "/server-info" => response_builder(start_line.method, "HTTP/1.1 404 ", None, None),
_ => try_get_file(&start_line, &field_lines), _ => try_get_file(&start_line, &field_lines),
}; };
stream.write_all(&response)?; stream.write_all(&response)?;