refactoring and the beginnings of special paths
This commit is contained in:
54
src/main.rs
54
src/main.rs
@ -315,34 +315,24 @@ fn response_builder(
|
||||
return response;
|
||||
}
|
||||
|
||||
fn act_upon_request(
|
||||
start_line: StartLine,
|
||||
_field_lines: HashMap<String, String>,
|
||||
_request_body: Vec<u8>,
|
||||
) -> Result<Vec<u8>, Box<dyn Error>> {
|
||||
fn try_get_file(start_line: &StartLine, _field_lines: &HashMap<String, String>) -> Vec<u8> {
|
||||
let mut response_field_lines: HashMap<String, String> = HashMap::new();
|
||||
let mut response_body: Vec<u8> = vec![];
|
||||
let response: Vec<u8>;
|
||||
let special_paths = ["/server-health", "/server-stats", "/server-info"];
|
||||
|
||||
if special_paths.contains(&start_line.target.as_str()) {
|
||||
match start_line.target.as_str() {
|
||||
"/server-health" => {
|
||||
response = response_builder(start_line.method, "HTTP/1.1 200 ", None, None);
|
||||
}
|
||||
_ => {
|
||||
response = response_builder(start_line.method, "HTTP/1.1 404 ", None, None);
|
||||
}
|
||||
}
|
||||
|
||||
return Ok(response);
|
||||
}
|
||||
|
||||
let path: PathBuf = match start_line.target.as_str() {
|
||||
"/" => PathBuf::from("/www/index.html"),
|
||||
_ => PathBuf::from(format!("/www{}", start_line.target)),
|
||||
};
|
||||
|
||||
// TODO: Check if wanted file is contained in the optional Accept field-line
|
||||
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Accept for reference
|
||||
// let mime_type = match MimeGuess::from_path(&path).first_raw() {
|
||||
// Some(val) => val,
|
||||
// _ => {
|
||||
// return response_builder(RequestMethods::HEAD, "HTTP/1.1 500 ", None, None);
|
||||
// }
|
||||
// }
|
||||
|
||||
match fs::read(&path) {
|
||||
Ok(val) => {
|
||||
val.iter().for_each(|byte| response_body.push(*byte));
|
||||
@ -351,25 +341,21 @@ fn act_upon_request(
|
||||
String::from("Content-Length"),
|
||||
response_body.len().to_string(),
|
||||
);
|
||||
// TODO: get mime-type of file and use that here
|
||||
|
||||
let mime_type = mime_guess::from_path(&path)
|
||||
.first_raw()
|
||||
.expect("Could not guess mime-type from path");
|
||||
response_field_lines.insert(String::from("Content-Type"), mime_type.to_string());
|
||||
|
||||
response = response_builder(
|
||||
response_builder(
|
||||
start_line.method,
|
||||
"HTTP/1.1 200 ",
|
||||
Some(response_field_lines),
|
||||
Some(response_body),
|
||||
);
|
||||
)
|
||||
}
|
||||
Err(_) => {
|
||||
response = response_builder(start_line.method, "HTTP/1.1 404 ", None, None);
|
||||
Err(_) => response_builder(start_line.method, "HTTP/1.1 404 ", None, None),
|
||||
}
|
||||
};
|
||||
|
||||
Ok(response)
|
||||
}
|
||||
|
||||
fn handle_request(mut stream: TcpStream) -> Result<(), Box<dyn Error>> {
|
||||
@ -423,10 +409,16 @@ fn handle_request(mut stream: TcpStream) -> Result<(), Box<dyn Error>> {
|
||||
}
|
||||
};
|
||||
|
||||
let mut body: Vec<u8> = vec![];
|
||||
reader.read_to_end(&mut body)?;
|
||||
let mut request_body: Vec<u8> = vec![];
|
||||
reader.read_to_end(&mut request_body)?;
|
||||
|
||||
let response = act_upon_request(start_line, field_lines, body)?;
|
||||
let response = match start_line.target.as_str() {
|
||||
// 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-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),
|
||||
_ => try_get_file(&start_line, &field_lines),
|
||||
};
|
||||
stream.write_all(&response)?;
|
||||
|
||||
Ok(())
|
||||
|
Reference in New Issue
Block a user