Rename project and add handling for images and assets.

This commit is contained in:
calcu1on 2025-04-20 23:43:18 -04:00
parent 92075763db
commit 99ea2cf3ad
6 changed files with 44 additions and 29 deletions

View File

@ -1,5 +1,5 @@
[package]
name = "hermes"
name = "batflip"
version = "0.1.0"
edition = "2021"

View File

@ -1,2 +1,2 @@
let body = document.getElementsByTagName('h1');
console.log(body);
alert("time to rock");
console.log("hello");

View File

@ -1,4 +1,12 @@
body {
color: white;
background: purple;
max-width: 95%;
display: block;
margin: auto;
}
img {
max-width: 100%;
display: block;
}

View File

@ -2,11 +2,11 @@
<html>
<head>
<title>Toolshed</title>
<link rel="stylesheet" href="/assets/style.css">
<script src="/assets/main.js" defer></script>
<link rel="stylesheet" href="/assets/style.css" />
<script src="/assets/main.js"></script>
</head>
<body>
<h1>This is my toolshed. This web server is built using Rust :)</h1>
<img src="/images/6.jpg"/>
<img src="/images/6.jpg" alt="some image" />
</body>
</html>

View File

@ -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();

View File

@ -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()
// }