diff --git a/src/main.rs b/src/main.rs index c96f99c..f2dfdac 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,13 +18,22 @@ fn handle_connection(mut stream: TcpStream) { stream.read(&mut buffer).unwrap(); + let get = b"GET / HTTP/1.1\r\n"; + 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 (headers, response) = if buffer.starts_with(get) { + let bytes = fs::read("public/index.html").unwrap(); + ("HTTP/1.1 200 OK\r\n\r\n".as_bytes(), bytes[..]) + + } else { + + ("HTTP/1.1 404 NOT FOUND\r\n\r\n".as_bytes(), "Couldn't find {}".as_bytes()) + + }; + + stream.write(headers).unwrap(); stream.write(response).unwrap(); stream.flush().unwrap(); }