Check for mime-type and JS fun
This commit is contained in:
168
src/main.rs
168
src/main.rs
@ -10,7 +10,7 @@ use std::{
|
||||
thread,
|
||||
};
|
||||
|
||||
#[derive(PartialEq, Eq, Debug)]
|
||||
#[derive(PartialEq, Eq, Debug, Copy, Clone)]
|
||||
enum RequestMethods {
|
||||
NULL = -1, // This is only to initialise the struct
|
||||
GET,
|
||||
@ -40,11 +40,7 @@ impl RequestLine {
|
||||
}
|
||||
|
||||
// Only GET and HEAD are required, the rest is optional
|
||||
if ["GET", "HEAD"].contains(&method.trim()) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
["GET", "HEAD"].contains(&method.trim())
|
||||
}
|
||||
|
||||
// TODO: make the checks less shit and actually correct
|
||||
@ -350,6 +346,86 @@ fn response_builder(
|
||||
return response;
|
||||
}
|
||||
|
||||
fn act_upon_request(start_line: &RequestLine) -> Result<Vec<u8>, Box<dyn Error>> {
|
||||
let mut response_field_lines: HashMap<String, String> = HashMap::new();
|
||||
let mut response_body: Vec<u8> = vec![];
|
||||
let response: Vec<u8>;
|
||||
|
||||
if start_line.target == "/" {
|
||||
let file = match fs::read("./www/index.html") {
|
||||
Ok(val) => val,
|
||||
Err(_) => {
|
||||
b"The is no index.html, only you and me."
|
||||
.iter()
|
||||
.for_each(|byte| response_body.push(*byte));
|
||||
|
||||
response_field_lines.insert(
|
||||
String::from("Content-Length"),
|
||||
response_body.len().to_string(),
|
||||
);
|
||||
response_field_lines
|
||||
.insert(String::from("Content-Type"), String::from("text/plain"));
|
||||
|
||||
response = response_builder(
|
||||
start_line.method,
|
||||
"HTTP/1.1 200 OK",
|
||||
response_field_lines,
|
||||
Some(response_body),
|
||||
);
|
||||
|
||||
return Ok(response);
|
||||
}
|
||||
};
|
||||
|
||||
file.iter().for_each(|byte| response_body.push(*byte));
|
||||
|
||||
response_field_lines.insert(String::from("Content-Length"), file.len().to_string());
|
||||
response_field_lines.insert(String::from("Content-Type"), String::from("text/html"));
|
||||
|
||||
response = response_builder(
|
||||
start_line.method,
|
||||
"HTTP/1.1 200 OK",
|
||||
response_field_lines,
|
||||
Some(response_body),
|
||||
);
|
||||
} else {
|
||||
let path: PathBuf = PathBuf::from(format!("./www{}", start_line.target));
|
||||
|
||||
match fs::read(&path) {
|
||||
Ok(val) => {
|
||||
val.iter().for_each(|byte| response_body.push(*byte));
|
||||
|
||||
response_field_lines.insert(
|
||||
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(
|
||||
start_line.method,
|
||||
"HTTP/1.1 200 OK",
|
||||
response_field_lines,
|
||||
Some(response_body),
|
||||
);
|
||||
}
|
||||
Err(_) => {
|
||||
response = response_builder(
|
||||
start_line.method,
|
||||
"HTTP/1.1 404 Not Found",
|
||||
response_field_lines,
|
||||
None,
|
||||
);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Ok(response)
|
||||
}
|
||||
|
||||
fn handle_request(mut stream: TcpStream) -> Result<(), Box<dyn Error>> {
|
||||
let mut line = String::new();
|
||||
let mut reader = BufReader::new(&mut stream);
|
||||
@ -384,85 +460,9 @@ fn handle_request(mut stream: TcpStream) -> Result<(), Box<dyn Error>> {
|
||||
// reader.read_to_end(&mut body)?;
|
||||
// dbg!(&body);
|
||||
|
||||
let mut response_field_lines: HashMap<String, String> = HashMap::new();
|
||||
let mut response_body: Vec<u8> = vec![];
|
||||
|
||||
// TODO: Act upon the request
|
||||
if start_line.target == "/" {
|
||||
let file = match fs::read("./www/index.html") {
|
||||
Ok(val) => val,
|
||||
Err(_) => {
|
||||
b"The is no index.html, only you and me."
|
||||
.iter()
|
||||
.for_each(|byte| response_body.push(*byte));
|
||||
|
||||
response_field_lines.insert(
|
||||
String::from("Content-Length"),
|
||||
response_body.len().to_string(),
|
||||
);
|
||||
response_field_lines
|
||||
.insert(String::from("Content-Type"), String::from("text/plain"));
|
||||
|
||||
let response = response_builder(
|
||||
start_line.method,
|
||||
"HTTP/1.1 200 OK",
|
||||
response_field_lines,
|
||||
Some(response_body),
|
||||
);
|
||||
|
||||
stream.write_all(&response)?;
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
|
||||
file.iter().for_each(|byte| response_body.push(*byte));
|
||||
|
||||
response_field_lines.insert(String::from("Content-Length"), file.len().to_string());
|
||||
response_field_lines.insert(String::from("Content-Type"), String::from("text/html"));
|
||||
|
||||
let response = response_builder(
|
||||
start_line.method,
|
||||
"HTTP/1.1 200 OK",
|
||||
response_field_lines,
|
||||
Some(response_body),
|
||||
);
|
||||
|
||||
stream.write_all(&response)?;
|
||||
} else {
|
||||
let path: PathBuf = PathBuf::from(format!("./www{}", start_line.target));
|
||||
|
||||
match fs::read(path) {
|
||||
Ok(val) => {
|
||||
val.iter().for_each(|byte| response_body.push(*byte));
|
||||
|
||||
response_field_lines.insert(
|
||||
String::from("Content-Length"),
|
||||
response_body.len().to_string(),
|
||||
);
|
||||
// TODO: get mime-type of file and use that here
|
||||
response_field_lines.insert(String::from("Content-Type"), String::from("*/*"));
|
||||
|
||||
let response = response_builder(
|
||||
start_line.method,
|
||||
"HTTP/1.1 200 OK",
|
||||
response_field_lines,
|
||||
Some(response_body),
|
||||
);
|
||||
|
||||
stream.write_all(&response)?;
|
||||
}
|
||||
Err(_) => {
|
||||
let response = response_builder(
|
||||
start_line.method,
|
||||
"HTTP/1.1 404 Not Found",
|
||||
response_field_lines,
|
||||
None,
|
||||
);
|
||||
|
||||
stream.write_all(&response)?;
|
||||
}
|
||||
};
|
||||
}
|
||||
let response = act_upon_request(&start_line)?;
|
||||
stream.write_all(&response)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
Reference in New Issue
Block a user