WIP if-else refs

This commit is contained in:
AJ ONeal 2018-10-01 19:23:37 -06:00
parent 9bef4073a8
commit 6ac5399822
1 changed files with 13 additions and 4 deletions

View File

@ -18,13 +18,22 @@ fn handle_connection(mut stream: TcpStream) {
stream.read(&mut buffer).unwrap(); stream.read(&mut buffer).unwrap();
let get = b"GET / HTTP/1.1\r\n";
println!("Request: {}", String::from_utf8_lossy(&buffer[..])); println!("Request: {}", String::from_utf8_lossy(&buffer[..]));
let headers = "HTTP/1.1 200 OK\r\n\r\n".as_bytes(); let (headers, response) = if buffer.starts_with(get) {
stream.write(headers).unwrap();
let response = fs::read("public/index.html").unwrap();
let response = &response[..];
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.write(response).unwrap();
stream.flush().unwrap(); stream.flush().unwrap();
} }