Compare commits

...

2 Commits

Author SHA1 Message Date
AJ ONeal 00ba212da5 WIP use lambda 2018-10-01 19:32:25 -06:00
AJ ONeal 6ac5399822 WIP if-else refs 2018-10-01 19:23:37 -06:00
1 changed files with 17 additions and 6 deletions

View File

@ -18,13 +18,24 @@ 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 mut respond = |headers, response| {
stream.write(headers).unwrap(); stream.write(headers).unwrap();
let response = fs::read("public/index.html").unwrap(); stream.write(response).unwrap();
let response = &response[..]; stream.flush().unwrap();
};
stream.write(response).unwrap(); if buffer.starts_with(get) {
stream.flush().unwrap();
let bytes = fs::read_to_string("public/index.html").unwrap();
respond("HTTP/1.1 200 OK\r\n\r\n".as_bytes(), bytes.as_bytes())
} else {
respond("HTTP/1.1 404 NOT FOUND\r\n\r\n".as_bytes(), "Couldn't find {}".as_bytes())
}
} }