Rename project and add handling for images and assets.
This commit is contained in:
@@ -25,24 +25,39 @@ pub fn handle_connection(mut stream: TcpStream) {
|
||||
.collect();
|
||||
// Get the first line of the stream which contains request data.
|
||||
let request_line: String = http_request.first().expect("No request line found").to_string();
|
||||
let response: String;
|
||||
let mut response;
|
||||
// Split the request data on spaces into a vector.
|
||||
let req_vec: Vec<&str> = request_line.split(" ").collect();
|
||||
let inc_request = IncomingRequest {
|
||||
req_type: req_vec[0].to_string(),
|
||||
path: req_vec[1].to_string(),
|
||||
};
|
||||
|
||||
// Handle 404.
|
||||
if inc_request.path.is_empty() {
|
||||
response = return_response("404");
|
||||
}
|
||||
|
||||
// Handle images.
|
||||
if inc_request.path.contains("images") {
|
||||
let mut public_route: String = "./public".to_string();
|
||||
public_route.push_str(&inc_request.path);
|
||||
let image_data = fs::read(&public_route).unwrap();
|
||||
let status_line = "HTTP/1.1 200 OK".to_string();
|
||||
let response = format!(
|
||||
"{}\r\nContent-Length: {}\r\n\r\n",
|
||||
status_line,
|
||||
image_data.len()
|
||||
);
|
||||
stream.write(response.as_bytes()).unwrap();
|
||||
stream.write(&image_data).unwrap();
|
||||
}
|
||||
else {
|
||||
response = return_response(&inc_request.path);
|
||||
stream.write(response.as_bytes()).unwrap();
|
||||
}
|
||||
|
||||
stream.write_all(response.as_bytes()).unwrap();
|
||||
}
|
||||
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn get_header(headers: Vec<String>, needle: &str) -> String {
|
||||
let mut value = String::new();
|
||||
|
||||
@@ -1,32 +1,24 @@
|
||||
use std::fs;
|
||||
|
||||
pub fn get_route_data(route: &str) -> String {
|
||||
let route_data: String;
|
||||
match route {
|
||||
"/" => {
|
||||
route_data = fs::read_to_string("./public/index.html").unwrap();
|
||||
return fs::read_to_string("./public/index.html").unwrap();
|
||||
},
|
||||
"/about" => {
|
||||
route_data = fs::read_to_string("./public/pages/about.html").unwrap();
|
||||
return fs::read_to_string("./public/pages/about.html").unwrap();
|
||||
}
|
||||
_ if route.contains("assets") => {
|
||||
let mut public_route: String = "./public".to_string();
|
||||
public_route.push_str(&route);
|
||||
return fs::read_to_string(&public_route).unwrap()
|
||||
},
|
||||
&_ => {
|
||||
let mut public_path: String = "./public".to_string();
|
||||
if path_exists(&public_path) {
|
||||
if public_path.contains("images") {
|
||||
route_data = fs::read(public_path).unwrap();
|
||||
}
|
||||
else {
|
||||
route_data = fs::read_to_string(public_path).unwrap();
|
||||
}
|
||||
}
|
||||
else {
|
||||
route_data = fs::read_to_string("./public/404.html").unwrap();
|
||||
}
|
||||
return fs::read_to_string("./public/404.html").unwrap();
|
||||
}
|
||||
}
|
||||
route_data
|
||||
}
|
||||
|
||||
pub fn path_exists(path: &str) -> bool {
|
||||
fs::metadata(path).is_ok()
|
||||
}
|
||||
// pub fn path_exists(path: &str) -> bool {
|
||||
// fs::metadata(path).is_ok()
|
||||
// }
|
||||
|
||||
Reference in New Issue
Block a user