From 6ac53998220e013db45a2b0a84735a2581c5f067 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Mon, 1 Oct 2018 19:23:37 -0600 Subject: [PATCH] WIP if-else refs --- src/main.rs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) 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(); }