頭條地址:https://www.ixigua.com/i680718929763709798...
B站地址:https://www.bilibili.com/video/BV177411m78...
https://github.com/anonymousGiga/web-serve...
此部分內容接前面內容Rust 程式設計小專案:WebServer 03
主要是對handle_client的邏輯進行最佳化:
fn handle_connection(mut stream: TcpStream) {
// --snip--
let (status_line, filename) = if buffer.starts_with(get) {
("HTTP/1.1 200 OK\r\n\r\n", "main.html")
} else {
("HTTP/1.1 404 NOT FOUND\r\n\r\n", "404.html")
};
let contents = fs::read_to_string(filename).unwrap();
let response = format!("{}{}", status_line, contents);
stream.write(response.as_bytes()).unwrap();
stream.flush().unwrap();
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結