Commiting changes
This commit is contained in:
parent
4e3306c5dd
commit
92075763db
BIN
public/.DS_Store
vendored
Normal file
BIN
public/.DS_Store
vendored
Normal file
Binary file not shown.
35002
public/200.json
35002
public/200.json
File diff suppressed because it is too large
Load Diff
@ -1,3 +0,0 @@
|
|||||||
{
|
|
||||||
"detail": "Route not found, or request malformed...thats all im saying."
|
|
||||||
}
|
|
||||||
2
public/assets/main.js
Normal file
2
public/assets/main.js
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
let body = document.getElementsByTagName('h1');
|
||||||
|
console.log(body);
|
||||||
4
public/assets/style.css
Normal file
4
public/assets/style.css
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
body {
|
||||||
|
color: white;
|
||||||
|
background: purple;
|
||||||
|
}
|
||||||
BIN
public/images/6.jpg
Normal file
BIN
public/images/6.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 256 KiB |
12
public/index.html
Normal file
12
public/index.html
Normal 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
16
public/pages/about.html
Normal 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>
|
||||||
@ -24,55 +24,26 @@ pub fn handle_connection(mut stream: TcpStream) {
|
|||||||
.take_while(|line| !line.is_empty())
|
.take_while(|line| !line.is_empty())
|
||||||
.collect();
|
.collect();
|
||||||
// Get the first line of the stream which contains request data.
|
// Get the first line of the stream which contains request data.
|
||||||
let request_line: String = http_request.first().expect("Not a string").to_string();
|
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.
|
// Split the request data on spaces into a vector.
|
||||||
let req_vec: Vec<&str> = request_line.split(" ").collect();
|
let req_vec: Vec<&str> = request_line.split(" ").collect();
|
||||||
println!("{:?}", http_request);
|
|
||||||
let inc_request = IncomingRequest {
|
let inc_request = IncomingRequest {
|
||||||
req_type: req_vec[0].to_string(),
|
req_type: req_vec[0].to_string(),
|
||||||
path: req_vec[1].to_string(),
|
path: req_vec[1].to_string(),
|
||||||
};
|
};
|
||||||
// Retrieve the api key from the request.
|
if inc_request.path.is_empty() {
|
||||||
let api_key: String = get_header(http_request, "x-api-key");
|
|
||||||
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_response("404");
|
response = return_response("404");
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// Get allowed api key from text file.
|
response = return_response(&inc_request.path);
|
||||||
let mut keyfile: Vec<String> = fs::read_to_string("./keys.txt").into_iter().collect();
|
|
||||||
// Get the first item, for now. @todo make this more dynamic.
|
|
||||||
keyfile[0].pop().expect("Keyfile not found").to_string();
|
|
||||||
if api_key == keyfile[0] {
|
|
||||||
// Here we need to look up the route by path.
|
|
||||||
if inc_request.path.is_empty() {
|
|
||||||
response = return_response("404");
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
response = return_response(&inc_request.path);
|
|
||||||
}
|
|
||||||
// response = return_response("200");
|
|
||||||
} else {
|
|
||||||
response = return_response("404");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
stream.write_all(response.as_bytes()).unwrap();
|
stream.write_all(response.as_bytes()).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
fn get_header(headers: Vec<String>, needle: &str) -> String {
|
fn get_header(headers: Vec<String>, needle: &str) -> String {
|
||||||
let mut value = String::new();
|
let mut value = String::new();
|
||||||
for header in headers {
|
for header in headers {
|
||||||
@ -96,19 +67,13 @@ fn return_response(path_or_code: &str) -> String {
|
|||||||
},
|
},
|
||||||
"200" => {
|
"200" => {
|
||||||
status_line = "HTTP/1.1 200 OK".to_string();
|
status_line = "HTTP/1.1 200 OK".to_string();
|
||||||
contents = fs::read_to_string("./public/200.json").unwrap();
|
contents = fs::read_to_string("./public/index.html").unwrap();
|
||||||
},
|
},
|
||||||
&_ => {
|
&_ => {
|
||||||
// Need to get the proper response based on the given path.
|
// Need to get the proper response based on the given path.
|
||||||
let has_route = router::route_exists(path_or_code);
|
status_line = "HTTP/1.1 200 OK".to_string();
|
||||||
if has_route {
|
contents = router::get_route_data(path_or_code);
|
||||||
status_line = "HTTP/1.1 200 OK".to_string();
|
// status_line = "HTTP/1.1 404 Not Found".to_string();
|
||||||
contents = router::get_route_data(path_or_code);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
status_line = "HTTP/1.1 404 Not Found".to_string();
|
|
||||||
contents = fs::read_to_string("./public/404.html").unwrap();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -9,7 +9,7 @@ fn main() {
|
|||||||
let mut listener = TcpListener::bind("127.0.0.1:6942").unwrap();
|
let mut listener = TcpListener::bind("127.0.0.1:6942").unwrap();
|
||||||
|
|
||||||
if !run_local {
|
if !run_local {
|
||||||
listener = TcpListener::bind("164.92.85.112:7878").unwrap();
|
listener = TcpListener::bind("0.0.0.0:6942").unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
for stream in listener.incoming() {
|
for stream in listener.incoming() {
|
||||||
|
|||||||
@ -1,38 +1,32 @@
|
|||||||
use std::collections::HashMap;
|
use std::fs;
|
||||||
|
|
||||||
pub fn get_route_data(route: &str) -> String {
|
pub fn get_route_data(route: &str) -> String {
|
||||||
let route_data: String;
|
let route_data: String;
|
||||||
match route {
|
match route {
|
||||||
"/fetch" => {
|
"/" => {
|
||||||
route_data = fetch_asset();
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
&_ => todo!(),
|
|
||||||
}
|
}
|
||||||
route_data
|
route_data
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn route_exists(route: &str) -> bool {
|
pub fn path_exists(path: &str) -> bool {
|
||||||
let existing_routes = get_routes();
|
fs::metadata(path).is_ok()
|
||||||
if existing_routes.contains_key(route) {
|
|
||||||
true
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_routes() -> HashMap<String, String> {
|
|
||||||
let mut routes = HashMap::new();
|
|
||||||
routes.insert("/fetch".to_string(), "fetch_asset".to_string());
|
|
||||||
routes
|
|
||||||
}
|
|
||||||
|
|
||||||
fn fetch_asset() -> String {
|
|
||||||
let asset = r#"{
|
|
||||||
"title": "this is a title",
|
|
||||||
"id": 2,
|
|
||||||
"url": "https://www.espn.com
|
|
||||||
}"#;
|
|
||||||
|
|
||||||
asset.to_string()
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user