From 99ea2cf3ad123ff5dff21bb8019eda4484171a9c Mon Sep 17 00:00:00 2001 From: calcu1on Date: Sun, 20 Apr 2025 23:43:18 -0400 Subject: [PATCH] Rename project and add handling for images and assets. --- Cargo.toml | 2 +- public/assets/main.js | 4 ++-- public/assets/style.css | 8 ++++++++ public/index.html | 6 +++--- src/connection.rs | 23 +++++++++++++++++++---- src/router.rs | 30 +++++++++++------------------- 6 files changed, 44 insertions(+), 29 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e9407fa..2faab33 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "hermes" +name = "batflip" version = "0.1.0" edition = "2021" diff --git a/public/assets/main.js b/public/assets/main.js index 1494664..3650871 100644 --- a/public/assets/main.js +++ b/public/assets/main.js @@ -1,2 +1,2 @@ -let body = document.getElementsByTagName('h1'); -console.log(body); +alert("time to rock"); +console.log("hello"); diff --git a/public/assets/style.css b/public/assets/style.css index 8b307dd..519736d 100644 --- a/public/assets/style.css +++ b/public/assets/style.css @@ -1,4 +1,12 @@ body { color: white; background: purple; + max-width: 95%; + display: block; + margin: auto; } + +img { + max-width: 100%; + display: block; +} diff --git a/public/index.html b/public/index.html index f208a8d..89a50b3 100644 --- a/public/index.html +++ b/public/index.html @@ -2,11 +2,11 @@ Toolshed - - + +

This is my toolshed. This web server is built using Rust :)

- + some image diff --git a/src/connection.rs b/src/connection.rs index 8047a7e..f2c6824 100644 --- a/src/connection.rs +++ b/src/connection.rs @@ -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, needle: &str) -> String { let mut value = String::new(); diff --git a/src/router.rs b/src/router.rs index 17e7254..a141a67 100644 --- a/src/router.rs +++ b/src/router.rs @@ -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() +// }