Update ch20-01-single-threaded.md

pull/492/head
Zehua Zou 4 years ago committed by GitHub
parent 50fad7cc40
commit e81004a7ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -87,7 +87,7 @@ fn main() {
} }
fn handle_connection(mut stream: TcpStream) { fn handle_connection(mut stream: TcpStream) {
let mut buffer = [0; 512]; let mut buffer = [0; 1024];
stream.read(&mut buffer).unwrap(); stream.read(&mut buffer).unwrap();
@ -178,7 +178,7 @@ HTTP/1.1 200 OK\r\n\r\n
# use std::io::prelude::*; # use std::io::prelude::*;
# use std::net::TcpStream; # use std::net::TcpStream;
fn handle_connection(mut stream: TcpStream) { fn handle_connection(mut stream: TcpStream) {
let mut buffer = [0; 512]; let mut buffer = [0; 1024];
stream.read(&mut buffer).unwrap(); stream.read(&mut buffer).unwrap();
@ -230,12 +230,16 @@ use std::fs;
// --snip-- // --snip--
fn handle_connection(mut stream: TcpStream) { fn handle_connection(mut stream: TcpStream) {
let mut buffer = [0; 512]; let mut buffer = [0; 1024];
stream.read(&mut buffer).unwrap(); stream.read(&mut buffer).unwrap();
let contents = fs::read_to_string("hello.html").unwrap(); 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!(
"HTTP/1.1 200 OK\r\nContent-Length: {}\r\n\r\n{}",
contents.len(),
contents
);
stream.write(response.as_bytes()).unwrap(); stream.write(response.as_bytes()).unwrap();
stream.flush().unwrap(); stream.flush().unwrap();
@ -265,7 +269,7 @@ fn handle_connection(mut stream: TcpStream) {
// --snip-- // --snip--
fn handle_connection(mut stream: TcpStream) { fn handle_connection(mut stream: TcpStream) {
let mut buffer = [0; 512]; let mut buffer = [0; 1024];
stream.read(&mut buffer).unwrap(); stream.read(&mut buffer).unwrap();
let get = b"GET / HTTP/1.1\r\n"; let get = b"GET / HTTP/1.1\r\n";
@ -273,7 +277,11 @@ fn handle_connection(mut stream: TcpStream) {
if buffer.starts_with(get) { if buffer.starts_with(get) {
let contents = fs::read_to_string("hello.html").unwrap(); 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!(
"HTTP/1.1 200 OK\r\nContent-Length: {}\r\n\r\n{}",
contents.len(),
contents
);
stream.write(response.as_bytes()).unwrap(); stream.write(response.as_bytes()).unwrap();
stream.flush().unwrap(); stream.flush().unwrap();

Loading…
Cancel
Save