mirror of
https://github.com/KaiserY/trpl-zh-cn
synced 2025-02-25 05:42:18 +08:00
37 lines
773 B
Rust
Executable File
37 lines
773 B
Rust
Executable File
// ANCHOR: here
|
|
use std::fs;
|
|
// --snip--
|
|
|
|
// ANCHOR_END: here
|
|
use std::io::prelude::*;
|
|
use std::net::TcpListener;
|
|
use std::net::TcpStream;
|
|
|
|
fn main() {
|
|
let listener = TcpListener::bind("127.0.0.1:7878").unwrap();
|
|
|
|
for stream in listener.incoming() {
|
|
let stream = stream.unwrap();
|
|
|
|
handle_connection(stream);
|
|
}
|
|
}
|
|
|
|
// ANCHOR: here
|
|
fn handle_connection(mut stream: TcpStream) {
|
|
let mut buffer = [0; 1024];
|
|
stream.read(&mut buffer).unwrap();
|
|
|
|
let contents = fs::read_to_string("hello.html").unwrap();
|
|
|
|
let response = format!(
|
|
"HTTP/1.1 200 OK\r\nContent-Length: {}\r\n\r\n{}",
|
|
contents.len(),
|
|
contents
|
|
);
|
|
|
|
stream.write(response.as_bytes()).unwrap();
|
|
stream.flush().unwrap();
|
|
}
|
|
// ANCHOR_END: here
|