handle non-/ with 404
This commit is contained in:
parent
9bef4073a8
commit
8c1f52f39d
|
@ -0,0 +1,11 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Not Found!</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>U2!?</h1>
|
||||
<p>And you still haven't found what you're lookin' for...</p>
|
||||
</body>
|
||||
</html>
|
14
src/main.rs
14
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();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue