Browse Source

Add 404

master
Noelle 4 years ago
parent
commit
a8a34cdbc6
2 changed files with 23 additions and 4 deletions
  1. 13
    0
      404.html
  2. 10
    4
      src/main.rs

+ 13
- 0
404.html View File

@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>
Hello!
</title>
</head>
<body>
<h1>Oops!</h1>
<p>Sorry, I don't know what you're asking for.</p>
</body>
</html>

+ 10
- 4
src/main.rs View File

@@ -16,17 +16,23 @@ fn handle_connecton(mut stream: TcpStream) {
let mut buffer = [0; 512];
stream.read(&mut buffer).unwrap();

let get = "GET / HTTP/1.1\r\n";
let get = b"GET / HTTP/1.1\r\n";

if buffer.starts_with(get) {
// It's the request we want
let status_line = "HTTP/1.1 200 OK\r\n\r\n";
let contents = fs::read_to_string("hello.html").unwrap();
let response = format!("HTTP/1.1 200 OK\r\n\r\n{}", contents);
let response = format!("{}{}", status_line, contents);

stream.write(response.as_bytes()).unwrap();
stream.flush().unwrap();
} else {
// a different request so who cares
let status_line = "HTTP/1.1 404 NOT FOUND\r\n\r\n";
let contents = fs::read_to_string("404.html").unwrap();
let response = format!("{}{}", status_line, contents);

stream.write(response.as_bytes()).unwrap();
stream.flush().unwrap();
}



Loading…
Cancel
Save