Merge pull request #4 from calcu1on/issue-2

API Key Handling and some more dynamic functions.
This commit is contained in:
calcu1on 2024-12-22 21:42:27 -05:00 committed by GitHub
commit 4b019c85d1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 32 additions and 27 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 # 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. # option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/ #.idea/
keys.txt
# Added by cargo # Added by cargo

View File

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

View File

@ -12,7 +12,7 @@ struct IncomingRequest {
// Listen on port and return response. // Listen on port and return response.
fn main() { fn main() {
let run_local = false; let run_local = true;
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 {
@ -44,15 +44,15 @@ fn handle_connection(mut stream: TcpStream) {
let mut response = String::new(); let mut response = String::new();
// If not a GET request, or missing API key, throw 404. // If not a GET request, or missing API key, throw 404.
if inc_request.req_type != "GET" || api_key.is_empty() { if inc_request.req_type != "GET" || api_key.is_empty() {
response = return_404(); response = return_response("404");
} }
else { else {
// Check that the API key is in fact fact valuid. let mut keyfile: Vec<String> = fs::read_to_string("./keys.txt").into_iter().collect();
// Not really checking right now. keyfile[0].pop().expect("Not a string").to_string();
response = return_200(); if api_key == keyfile[0] {
response = return_response("200");
}
} }
// If not present, return 404.
// Perhaps move both of those conditions to a new function.
stream.write_all(response.as_bytes()).unwrap(); stream.write_all(response.as_bytes()).unwrap();
} }
@ -72,26 +72,28 @@ fn get_api_key(headers: Vec<String>) -> String {
api_key api_key
} }
// Return a 404. #[allow(unused_assignments)]
fn return_404() -> String { fn return_response(code: &str) -> String {
let status_line = "HTTP/1.1 404 Not Found"; let mut response = String::new();
let contents = fs::read_to_string("./public/404.json").unwrap(); match code {
let length = contents.len(); "404" => {
let status_line = "HTTP/1.1 404 Not Found";
let contents = fs::read_to_string("./public/404.json").unwrap();
let length = contents.len();
let response = response =
format!("{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}"); format!("{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}");
},
"200" => {
let status_line = "HTTP/1.1 200 OK";
let contents = fs::read_to_string("./public/200.json").unwrap();
let length = contents.len();
response response =
} format!("{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}");
},
// Return a 200 with the json. &_ => todo!()
fn return_200() -> String { }
let status_line = "HTTP/1.1 200 OK";
let contents = fs::read_to_string("./public/200.json").unwrap(); response
let length = contents.len();
let response =
format!("{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}");
response
} }