浏览代码

Add 404

master
Noelle 4 年前
父节点
当前提交
a8a34cdbc6
共有 2 个文件被更改,包括 23 次插入4 次删除
  1. 13
    0
      404.html
  2. 10
    4
      src/main.rs

+ 13
- 0
404.html 查看文件

@@ -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 查看文件

@@ -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();
}



正在加载...
取消
保存