more updates.
This commit is contained in:
parent
3c66a6b6bc
commit
f47ebb8633
15
public/404.html
Normal file
15
public/404.html
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
<!Doctype html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Page Not Found</title>
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
background: orange;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>Page cannot be found</h1>
|
||||||
|
<h3>Sorry, this page cannot be found.</h3>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@ -10,6 +10,7 @@ use crate::router;
|
|||||||
struct IncomingRequest {
|
struct IncomingRequest {
|
||||||
req_type: String,
|
req_type: String,
|
||||||
path: String
|
path: String
|
||||||
|
// headers: Vec<String>
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(unused_assignments)]
|
#[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();
|
let request_line: String = http_request.first().expect("Not a string").to_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.
|
// 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();
|
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() {
|
||||||
@ -71,6 +73,18 @@ fn get_api_key(headers: Vec<String>) -> String {
|
|||||||
api_key
|
api_key
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 {
|
fn return_response(path_or_code: &str) -> String {
|
||||||
let status_line: String;
|
let status_line: String;
|
||||||
let contents: String;
|
let contents: String;
|
||||||
@ -78,7 +92,7 @@ fn return_response(path_or_code: &str) -> String {
|
|||||||
match path_or_code {
|
match path_or_code {
|
||||||
"404" => {
|
"404" => {
|
||||||
status_line = "HTTP/1.1 404 Not Found".to_string();
|
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" => {
|
"200" => {
|
||||||
status_line = "HTTP/1.1 200 OK".to_string();
|
status_line = "HTTP/1.1 200 OK".to_string();
|
||||||
@ -93,7 +107,7 @@ fn return_response(path_or_code: &str) -> String {
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
status_line = "HTTP/1.1 404 Not Found".to_string();
|
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user