Compare commits

...

10 Commits

Author SHA1 Message Date
calcu1on
92075763db Commiting changes 2025-04-11 16:39:17 -04:00
calcu1on
4e3306c5dd
Merge pull request #7 from calcu1on/issue-6
Initial start of the route handling for DAM assets.
2025-01-05 19:53:56 -05:00
calcu1on
f47ebb8633 more updates. 2025-01-05 19:56:42 -05:00
calcu1on
3c66a6b6bc Initial start of the route handling for DAM assets. 2025-01-05 11:26:21 -05:00
calcu1on
d1ac420503
Merge pull request #5 from calcu1on/better-404
better handlign of something.
2024-12-22 21:57:49 -05:00
calcu1on
c5e38f29a1 better handlign of something. 2024-12-22 21:59:40 -05:00
calcu1on
56cca06199 API Key Handling and some more dynamic functions. 2024-12-22 21:43:52 -05:00
calcu1on
4b019c85d1
Merge pull request #4 from calcu1on/issue-2
API Key Handling and some more dynamic functions.
2024-12-22 21:42:27 -05:00
calcu1on
7c58f9db51 Switch to json 404. 2024-12-22 20:37:04 -05:00
calcu1on
411efe5276
Merge pull request #2 from calcu1on/issue-1
Switch to json 404.
2024-12-22 20:35:22 -05:00
14 changed files with 170 additions and 35086 deletions

1
.gitignore vendored
View File

@ -19,6 +19,7 @@ Cargo.lock
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
keys.txt
# Added by cargo

View File

@ -4,3 +4,6 @@ version = "0.1.0"
edition = "2021"
[dependencies]
base64 = "0.22.1"
rand = "0.8"
sqlite = "0.36.1"

BIN
main Executable file

Binary file not shown.

BIN
public/.DS_Store vendored Normal file

Binary file not shown.

File diff suppressed because it is too large Load Diff

View File

@ -1,9 +1,15 @@
<!Doctype html>
<html>
<head>
<title>Page Not Found | Hermes</title>
<title>Page Not Found</title>
<style>
body {
background: orange;
}
</style>
</head>
<body>
<p>The requested page could not be found.</p>
<h1>Page cannot be found</h1>
<h3>Sorry, this page cannot be found.</h3>
</body>
</html>

2
public/assets/main.js Normal file
View File

@ -0,0 +1,2 @@
let body = document.getElementsByTagName('h1');
console.log(body);

4
public/assets/style.css Normal file
View File

@ -0,0 +1,4 @@
body {
color: white;
background: purple;
}

BIN
public/images/6.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 256 KiB

12
public/index.html Normal file
View File

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

16
public/pages/about.html Normal file
View File

@ -0,0 +1,16 @@
<!Doctype html>
<html>
<head>
<title>About</title>
<style>
body {
background: purple;
padding: 20px;
color: white;
}
</style>
</head>
<body>
<h1>This is the about page!</h1>
</body>
</html>

82
src/connection.rs Normal file
View File

@ -0,0 +1,82 @@
use std::{
fs,
io::{prelude::*, BufReader},
net::TcpStream,
};
use crate::router;
#[allow(dead_code)]
struct IncomingRequest {
req_type: String,
path: String
// headers: Vec<String>
}
#[allow(unused_assignments)]
pub fn handle_connection(mut stream: TcpStream) {
// Create a new buffer reader with the stream data.
let buf_reader = BufReader::new(&stream);
// Store each line in a vector.
let http_request: Vec<_> = buf_reader
.lines()
.map(|result| result.unwrap())
.take_while(|line| !line.is_empty())
.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;
// 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(),
};
if inc_request.path.is_empty() {
response = return_response("404");
}
else {
response = return_response(&inc_request.path);
}
stream.write_all(response.as_bytes()).unwrap();
}
#[allow(dead_code)]
fn get_header(headers: Vec<String>, needle: &str) -> String {
let mut value = String::new();
for header in headers {
let split_header: Vec<&str> = header.split(":").collect();
let header_key: String = split_header.first().expect("Nothing here").to_string();
if header_key == needle {
value = split_header[1].to_string();
}
}
value
}
fn return_response(path_or_code: &str) -> String {
let status_line: String;
let contents: String;
match path_or_code {
"404" => {
status_line = "HTTP/1.1 404 Not Found".to_string();
contents = fs::read_to_string("./public/404.html").unwrap();
},
"200" => {
status_line = "HTTP/1.1 200 OK".to_string();
contents = fs::read_to_string("./public/index.html").unwrap();
},
&_ => {
// Need to get the proper response based on the given path.
status_line = "HTTP/1.1 200 OK".to_string();
contents = router::get_route_data(path_or_code);
// status_line = "HTTP/1.1 404 Not Found".to_string();
}
}
let length = contents.len();
format!("{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}")
}

View File

@ -1,92 +1,20 @@
use std::{
fs,
io::{prelude::*, BufReader},
net::{TcpListener, TcpStream},
};
use std::net::TcpListener;
#[allow(dead_code)]
struct IncomingRequest {
req_type: String,
path: String
}
pub mod connection;
mod router;
// Listen on port and return response.
fn main() {
let listener = TcpListener::bind("164.92.85.112:7878").unwrap();
let run_local = true;
let mut listener = TcpListener::bind("127.0.0.1:6942").unwrap();
if !run_local {
listener = TcpListener::bind("0.0.0.0:6942").unwrap();
}
for stream in listener.incoming() {
let stream = stream.unwrap();
handle_connection(stream);
connection::handle_connection(stream);
}
}
#[allow(unused_assignments)]
fn handle_connection(mut stream: TcpStream) {
let buf_reader = BufReader::new(&stream);
let http_request: Vec<_> = buf_reader
.lines()
.map(|result| result.unwrap())
.take_while(|line| !line.is_empty())
.collect();
let request_line: String = http_request.first().expect("Not a string").to_string();
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(),
};
let api_key: String = get_api_key(http_request);
let mut response = String::new();
// If not a GET request, or missing API key, throw 404.
if inc_request.req_type != "GET" || api_key.is_empty() {
response = return_404();
}
else {
// Check that the API key is in fact fact valuid.
// Not really checking right now.
response = return_200();
}
// If not present, return 404.
// Perhaps move both of those conditions to a new function.
stream.write_all(response.as_bytes()).unwrap();
}
// get the apy key from the headers.
fn get_api_key(headers: Vec<String>) -> String {
let mut api_key = String::new();
for header in headers {
// split header, strip whitespace, check if exists.
let split_header: Vec<&str> = header.split(":").collect();
let header_key: String = split_header.first().expect("Nothing here").to_string();
if header_key == "x-api-key" {
api_key = split_header[1].to_string();
}
}
api_key
}
// Return a 404.
fn return_404() -> String {
let status_line = "HTTP/1.1 404 Not Found";
let contents = fs::read_to_string("./public/404.html").unwrap();
let length = contents.len();
let response =
format!("{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}");
response
}
// Return a 200 with the json.
fn return_200() -> String {
let status_line = "HTTP/1.1 200 OK";
let contents = fs::read_to_string("./public/200.json").unwrap();
let length = contents.len();
let response =
format!("{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}");
response
}

32
src/router.rs Normal file
View File

@ -0,0 +1,32 @@
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();
},
"/about" => {
route_data = fs::read_to_string("./public/pages/about.html").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();
}
}
}
route_data
}
pub fn path_exists(path: &str) -> bool {
fs::metadata(path).is_ok()
}