From 8c1f52f39d665e465a2b3e555bb4e0aa7eb6d208 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Mon, 1 Oct 2018 19:43:49 -0600 Subject: [PATCH] handle non-/ with 404 --- public/404.html | 11 +++++++++++ src/main.rs | 14 ++++++++++---- 2 files changed, 21 insertions(+), 4 deletions(-) create mode 100644 public/404.html diff --git a/public/404.html b/public/404.html new file mode 100644 index 0000000..3b38b01 --- /dev/null +++ b/public/404.html @@ -0,0 +1,11 @@ + + + + + Not Found! + + +

U2!?

+

And you still haven't found what you're lookin' for...

+ + diff --git a/src/main.rs b/src/main.rs index c96f99c..08a760a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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(); }