From f47ebb8633d67ab4e6accb9d5180887974a47c51 Mon Sep 17 00:00:00 2001 From: calcu1on Date: Sun, 5 Jan 2025 19:56:42 -0500 Subject: [PATCH] more updates. --- public/404.html | 15 +++++++++++++++ src/connection.rs | 20 +++++++++++++++++--- 2 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 public/404.html diff --git a/public/404.html b/public/404.html new file mode 100644 index 0000000..d42bc82 --- /dev/null +++ b/public/404.html @@ -0,0 +1,15 @@ + + + + Page Not Found + + + +

Page cannot be found

+

Sorry, this page cannot be found.

+ + diff --git a/src/connection.rs b/src/connection.rs index d20a3b3..93d41a0 100644 --- a/src/connection.rs +++ b/src/connection.rs @@ -10,6 +10,7 @@ use crate::router; struct IncomingRequest { req_type: String, path: String + // headers: Vec } #[allow(unused_assignments)] @@ -26,12 +27,13 @@ pub fn handle_connection(mut stream: TcpStream) { let request_line: String = http_request.first().expect("Not a string").to_string(); // Split the request data on spaces into a vector. let req_vec: Vec<&str> = request_line.split(" ").collect(); + println!("{:?}", http_request); let inc_request = IncomingRequest { req_type: req_vec[0].to_string(), path: req_vec[1].to_string(), }; // Retrieve the api key from the request. - let api_key: String = get_api_key(http_request); + 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() { @@ -71,6 +73,18 @@ fn get_api_key(headers: Vec) -> String { api_key } +fn get_header(headers: Vec, 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; @@ -78,7 +92,7 @@ fn return_response(path_or_code: &str) -> String { match path_or_code { "404" => { status_line = "HTTP/1.1 404 Not Found".to_string(); - contents = fs::read_to_string("./public/404.json").unwrap(); + contents = fs::read_to_string("./public/404.html").unwrap(); }, "200" => { status_line = "HTTP/1.1 200 OK".to_string(); @@ -93,7 +107,7 @@ fn return_response(path_or_code: &str) -> String { } else { status_line = "HTTP/1.1 404 Not Found".to_string(); - contents = fs::read_to_string("./public/404.json").unwrap(); + contents = fs::read_to_string("./public/404.html").unwrap(); } } }