rust-lang-zh_CN/projects/hello/src/main.rs
2023-04-11 17:17:17 +08:00

47 lines
1.1 KiB
Rust

use hello::ThreadPool;
use std::{
fs,
thread,
io::{prelude::*, BufReader},
net::{TcpListener, TcpStream},
time::Duration,
};
fn main() {
let listener = TcpListener::bind("127.0.0.1:7878").unwrap();
let pool = ThreadPool::new(4);
for stream in listener.incoming().take(2) {
let stream = stream.unwrap();
pool.execute(|| {
handle_conn(stream);
});
}
println! ("关闭中。");
}
fn handle_conn(mut stream: TcpStream) {
let buf_reader = BufReader::new(&mut stream);
let req_line = buf_reader.lines().next().unwrap().unwrap();
let (status_line, filename) = match &req_line[..] {
"GET / HTTP/1.1" => ( "HTTP/1.1 200 OK", "hello.html"),
"GET /sleep HTTP/1.1" => {
thread::sleep(Duration::from_secs(10));
("HTTP/1.1 200 0K", "hello.html")
}
_ => ("HTTP/1.1 404 NOT FOUND", "404.html"),
};
let contents = fs::read_to_string(filename).unwrap();
let length = contents.len();
let resp =
format! ("{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}");
stream.write_all(resp.as_bytes()).unwrap();
}