handle non-/ with 404

This commit is contained in:
AJ ONeal 2018-10-01 19:43:49 -06:00
parent 9bef4073a8
commit 8c1f52f39d
2 changed files with 21 additions and 4 deletions

11
public/404.html Normal file
View File

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Not Found!</title>
</head>
<body>
<h1>U2!?</h1>
<p>And you still haven't found what you're lookin' for...</p>
</body>
</html>

View File

@ -20,11 +20,17 @@ fn handle_connection(mut stream: TcpStream) {
println!("Request: {}", String::from_utf8_lossy(&buffer[..]));
let headers = "HTTP/1.1 200 OK\r\n\r\n".as_bytes();
stream.write(headers).unwrap();
let response = fs::read("public/index.html").unwrap();
let response = &response[..];
let get = b"GET / HTTP/1.1\r\n";
let (headers, response) = if buffer.starts_with(get) {
("HTTP/1.1 200 OK\r\n\r\n", "index.html")
} else {
("HTTP/1.1 404 Not Found\r\n\r\n", "404.html")
};
let response = fs::read(format!("public/{}", response)).unwrap();
let response = &response[..];
stream.write(headers.as_bytes()).unwrap();
stream.write(response).unwrap();
stream.flush().unwrap();
}