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(); }