diff --git a/listings/ch20-web-server/listing-20-02/src/main.rs b/listings/ch20-web-server/listing-20-02/src/main.rs index 7dfdc2d8..2e68f2f7 100644 --- a/listings/ch20-web-server/listing-20-02/src/main.rs +++ b/listings/ch20-web-server/listing-20-02/src/main.rs @@ -13,7 +13,7 @@ fn main() { } fn handle_connection(mut stream: TcpStream) { - let mut buffer = [0; 512]; + let mut buffer = [0; 1024]; stream.read(&mut buffer).unwrap(); diff --git a/listings/ch20-web-server/listing-20-03/src/main.rs b/listings/ch20-web-server/listing-20-03/src/main.rs index 33f4fb73..afa579a4 100644 --- a/listings/ch20-web-server/listing-20-03/src/main.rs +++ b/listings/ch20-web-server/listing-20-03/src/main.rs @@ -14,7 +14,7 @@ fn main() { // ANCHOR: here fn handle_connection(mut stream: TcpStream) { - let mut buffer = [0; 512]; + let mut buffer = [0; 1024]; stream.read(&mut buffer).unwrap(); diff --git a/listings/ch20-web-server/listing-20-04/src/main.rs b/listings/ch20-web-server/listing-20-04/src/main.rs index 22e5d1da..818eac97 100644 --- a/listings/ch20-web-server/listing-20-04/src/main.rs +++ b/listings/ch20-web-server/listing-20-04/src/main.rs @@ -13,7 +13,7 @@ fn main() { } fn handle_connection(mut stream: TcpStream) { - let mut buffer = [0; 512]; + let mut buffer = [0; 1024]; stream.read(&mut buffer).unwrap(); diff --git a/listings/ch20-web-server/listing-20-05/src/main.rs b/listings/ch20-web-server/listing-20-05/src/main.rs index 0b3a284a..d20417c4 100644 --- a/listings/ch20-web-server/listing-20-05/src/main.rs +++ b/listings/ch20-web-server/listing-20-05/src/main.rs @@ -19,12 +19,16 @@ fn main() { // ANCHOR: here fn handle_connection(mut stream: TcpStream) { - let mut buffer = [0; 512]; + 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\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.flush().unwrap(); diff --git a/listings/ch20-web-server/listing-20-06/src/main.rs b/listings/ch20-web-server/listing-20-06/src/main.rs index dc192d76..83532442 100644 --- a/listings/ch20-web-server/listing-20-06/src/main.rs +++ b/listings/ch20-web-server/listing-20-06/src/main.rs @@ -17,7 +17,7 @@ fn main() { // --snip-- fn handle_connection(mut stream: TcpStream) { - let mut buffer = [0; 512]; + let mut buffer = [0; 1024]; stream.read(&mut buffer).unwrap(); let get = b"GET / HTTP/1.1\r\n"; @@ -25,7 +25,11 @@ fn handle_connection(mut stream: TcpStream) { if buffer.starts_with(get) { 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.flush().unwrap(); diff --git a/listings/ch20-web-server/listing-20-07/src/main.rs b/listings/ch20-web-server/listing-20-07/src/main.rs index 9fab0c76..4d8e5128 100644 --- a/listings/ch20-web-server/listing-20-07/src/main.rs +++ b/listings/ch20-web-server/listing-20-07/src/main.rs @@ -14,7 +14,7 @@ fn main() { } fn handle_connection(mut stream: TcpStream) { - let mut buffer = [0; 512]; + let mut buffer = [0; 1024]; stream.read(&mut buffer).unwrap(); let get = b"GET / HTTP/1.1\r\n"; @@ -22,7 +22,11 @@ fn handle_connection(mut stream: TcpStream) { if buffer.starts_with(get) { 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.flush().unwrap(); diff --git a/listings/ch20-web-server/listing-20-08/src/main.rs b/listings/ch20-web-server/listing-20-08/src/main.rs index cbcc4c01..ef905fa2 100644 --- a/listings/ch20-web-server/listing-20-08/src/main.rs +++ b/listings/ch20-web-server/listing-20-08/src/main.rs @@ -14,7 +14,7 @@ fn main() { } fn handle_connection(mut stream: TcpStream) { - let mut buffer = [0; 512]; + let mut buffer = [0; 1024]; stream.read(&mut buffer).unwrap(); let get = b"GET / HTTP/1.1\r\n"; @@ -22,7 +22,11 @@ fn handle_connection(mut stream: TcpStream) { if buffer.starts_with(get) { 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.flush().unwrap(); diff --git a/listings/ch20-web-server/listing-20-09/src/main.rs b/listings/ch20-web-server/listing-20-09/src/main.rs index c91bb660..8fdfd076 100644 --- a/listings/ch20-web-server/listing-20-09/src/main.rs +++ b/listings/ch20-web-server/listing-20-09/src/main.rs @@ -20,7 +20,7 @@ fn handle_connection(mut stream: TcpStream) { // --snip-- // ANCHOR_END: here - let mut buffer = [0; 512]; + let mut buffer = [0; 1024]; stream.read(&mut buffer).unwrap(); let get = b"GET / HTTP/1.1\r\n"; diff --git a/listings/ch20-web-server/listing-20-10/src/main.rs b/listings/ch20-web-server/listing-20-10/src/main.rs index 6ea692fc..257d47a7 100644 --- a/listings/ch20-web-server/listing-20-10/src/main.rs +++ b/listings/ch20-web-server/listing-20-10/src/main.rs @@ -23,7 +23,7 @@ fn handle_connection(mut stream: TcpStream) { // --snip-- // ANCHOR_END: here - let mut buffer = [0; 512]; + let mut buffer = [0; 1024]; stream.read(&mut buffer).unwrap(); // ANCHOR: here diff --git a/listings/ch20-web-server/listing-20-11/src/main.rs b/listings/ch20-web-server/listing-20-11/src/main.rs index 7eb3372b..d1087e21 100644 --- a/listings/ch20-web-server/listing-20-11/src/main.rs +++ b/listings/ch20-web-server/listing-20-11/src/main.rs @@ -20,7 +20,7 @@ fn main() { // ANCHOR_END: here fn handle_connection(mut stream: TcpStream) { - let mut buffer = [0; 512]; + let mut buffer = [0; 1024]; stream.read(&mut buffer).unwrap(); let get = b"GET / HTTP/1.1\r\n"; diff --git a/listings/ch20-web-server/listing-20-12/src/main.rs b/listings/ch20-web-server/listing-20-12/src/main.rs index 1381e6d6..72c5b9a8 100644 --- a/listings/ch20-web-server/listing-20-12/src/main.rs +++ b/listings/ch20-web-server/listing-20-12/src/main.rs @@ -21,7 +21,7 @@ fn main() { // ANCHOR_END: here fn handle_connection(mut stream: TcpStream) { - let mut buffer = [0; 512]; + let mut buffer = [0; 1024]; stream.read(&mut buffer).unwrap(); let get = b"GET / HTTP/1.1\r\n"; diff --git a/listings/ch20-web-server/listing-20-13/src/bin/main.rs b/listings/ch20-web-server/listing-20-13/src/bin/main.rs index 452f6822..9d6a9c15 100644 --- a/listings/ch20-web-server/listing-20-13/src/bin/main.rs +++ b/listings/ch20-web-server/listing-20-13/src/bin/main.rs @@ -20,7 +20,7 @@ fn main() { } fn handle_connection(mut stream: TcpStream) { - let mut buffer = [0; 512]; + let mut buffer = [0; 1024]; stream.read(&mut buffer).unwrap(); let get = b"GET / HTTP/1.1\r\n"; diff --git a/listings/ch20-web-server/listing-20-14/src/bin/main.rs b/listings/ch20-web-server/listing-20-14/src/bin/main.rs index 452f6822..9d6a9c15 100644 --- a/listings/ch20-web-server/listing-20-14/src/bin/main.rs +++ b/listings/ch20-web-server/listing-20-14/src/bin/main.rs @@ -20,7 +20,7 @@ fn main() { } fn handle_connection(mut stream: TcpStream) { - let mut buffer = [0; 512]; + let mut buffer = [0; 1024]; stream.read(&mut buffer).unwrap(); let get = b"GET / HTTP/1.1\r\n"; diff --git a/listings/ch20-web-server/listing-20-15/src/bin/main.rs b/listings/ch20-web-server/listing-20-15/src/bin/main.rs index 452f6822..9d6a9c15 100644 --- a/listings/ch20-web-server/listing-20-15/src/bin/main.rs +++ b/listings/ch20-web-server/listing-20-15/src/bin/main.rs @@ -20,7 +20,7 @@ fn main() { } fn handle_connection(mut stream: TcpStream) { - let mut buffer = [0; 512]; + let mut buffer = [0; 1024]; stream.read(&mut buffer).unwrap(); let get = b"GET / HTTP/1.1\r\n"; diff --git a/listings/ch20-web-server/listing-20-16/src/bin/main.rs b/listings/ch20-web-server/listing-20-16/src/bin/main.rs index 452f6822..9d6a9c15 100644 --- a/listings/ch20-web-server/listing-20-16/src/bin/main.rs +++ b/listings/ch20-web-server/listing-20-16/src/bin/main.rs @@ -20,7 +20,7 @@ fn main() { } fn handle_connection(mut stream: TcpStream) { - let mut buffer = [0; 512]; + let mut buffer = [0; 1024]; stream.read(&mut buffer).unwrap(); let get = b"GET / HTTP/1.1\r\n"; diff --git a/listings/ch20-web-server/listing-20-17/src/bin/main.rs b/listings/ch20-web-server/listing-20-17/src/bin/main.rs index 452f6822..9d6a9c15 100644 --- a/listings/ch20-web-server/listing-20-17/src/bin/main.rs +++ b/listings/ch20-web-server/listing-20-17/src/bin/main.rs @@ -20,7 +20,7 @@ fn main() { } fn handle_connection(mut stream: TcpStream) { - let mut buffer = [0; 512]; + let mut buffer = [0; 1024]; stream.read(&mut buffer).unwrap(); let get = b"GET / HTTP/1.1\r\n"; diff --git a/listings/ch20-web-server/listing-20-18/src/bin/main.rs b/listings/ch20-web-server/listing-20-18/src/bin/main.rs index 452f6822..9d6a9c15 100644 --- a/listings/ch20-web-server/listing-20-18/src/bin/main.rs +++ b/listings/ch20-web-server/listing-20-18/src/bin/main.rs @@ -20,7 +20,7 @@ fn main() { } fn handle_connection(mut stream: TcpStream) { - let mut buffer = [0; 512]; + let mut buffer = [0; 1024]; stream.read(&mut buffer).unwrap(); let get = b"GET / HTTP/1.1\r\n"; diff --git a/listings/ch20-web-server/listing-20-19/src/bin/main.rs b/listings/ch20-web-server/listing-20-19/src/bin/main.rs index 452f6822..9d6a9c15 100644 --- a/listings/ch20-web-server/listing-20-19/src/bin/main.rs +++ b/listings/ch20-web-server/listing-20-19/src/bin/main.rs @@ -20,7 +20,7 @@ fn main() { } fn handle_connection(mut stream: TcpStream) { - let mut buffer = [0; 512]; + let mut buffer = [0; 1024]; stream.read(&mut buffer).unwrap(); let get = b"GET / HTTP/1.1\r\n"; diff --git a/listings/ch20-web-server/listing-20-20/src/bin/main.rs b/listings/ch20-web-server/listing-20-20/src/bin/main.rs index 452f6822..9d6a9c15 100644 --- a/listings/ch20-web-server/listing-20-20/src/bin/main.rs +++ b/listings/ch20-web-server/listing-20-20/src/bin/main.rs @@ -20,7 +20,7 @@ fn main() { } fn handle_connection(mut stream: TcpStream) { - let mut buffer = [0; 512]; + let mut buffer = [0; 1024]; stream.read(&mut buffer).unwrap(); let get = b"GET / HTTP/1.1\r\n"; diff --git a/listings/ch20-web-server/listing-20-21/src/bin/main.rs b/listings/ch20-web-server/listing-20-21/src/bin/main.rs index 452f6822..9d6a9c15 100644 --- a/listings/ch20-web-server/listing-20-21/src/bin/main.rs +++ b/listings/ch20-web-server/listing-20-21/src/bin/main.rs @@ -20,7 +20,7 @@ fn main() { } fn handle_connection(mut stream: TcpStream) { - let mut buffer = [0; 512]; + let mut buffer = [0; 1024]; stream.read(&mut buffer).unwrap(); let get = b"GET / HTTP/1.1\r\n"; diff --git a/listings/ch20-web-server/listing-20-22/src/bin/main.rs b/listings/ch20-web-server/listing-20-22/src/bin/main.rs index 452f6822..9d6a9c15 100644 --- a/listings/ch20-web-server/listing-20-22/src/bin/main.rs +++ b/listings/ch20-web-server/listing-20-22/src/bin/main.rs @@ -20,7 +20,7 @@ fn main() { } fn handle_connection(mut stream: TcpStream) { - let mut buffer = [0; 512]; + let mut buffer = [0; 1024]; stream.read(&mut buffer).unwrap(); let get = b"GET / HTTP/1.1\r\n"; diff --git a/listings/ch20-web-server/listing-20-23/src/bin/main.rs b/listings/ch20-web-server/listing-20-23/src/bin/main.rs index 452f6822..9d6a9c15 100644 --- a/listings/ch20-web-server/listing-20-23/src/bin/main.rs +++ b/listings/ch20-web-server/listing-20-23/src/bin/main.rs @@ -20,7 +20,7 @@ fn main() { } fn handle_connection(mut stream: TcpStream) { - let mut buffer = [0; 512]; + let mut buffer = [0; 1024]; stream.read(&mut buffer).unwrap(); let get = b"GET / HTTP/1.1\r\n"; diff --git a/listings/ch20-web-server/listing-20-24/src/bin/main.rs b/listings/ch20-web-server/listing-20-24/src/bin/main.rs index 452f6822..9d6a9c15 100644 --- a/listings/ch20-web-server/listing-20-24/src/bin/main.rs +++ b/listings/ch20-web-server/listing-20-24/src/bin/main.rs @@ -20,7 +20,7 @@ fn main() { } fn handle_connection(mut stream: TcpStream) { - let mut buffer = [0; 512]; + let mut buffer = [0; 1024]; stream.read(&mut buffer).unwrap(); let get = b"GET / HTTP/1.1\r\n"; diff --git a/listings/ch20-web-server/listing-20-25/src/bin/main.rs b/listings/ch20-web-server/listing-20-25/src/bin/main.rs index 8d9310ea..22534d2f 100644 --- a/listings/ch20-web-server/listing-20-25/src/bin/main.rs +++ b/listings/ch20-web-server/listing-20-25/src/bin/main.rs @@ -25,7 +25,7 @@ fn main() { // ANCHOR_END: here fn handle_connection(mut stream: TcpStream) { - let mut buffer = [0; 512]; + let mut buffer = [0; 1024]; stream.read(&mut buffer).unwrap(); let get = b"GET / HTTP/1.1\r\n"; diff --git a/listings/ch20-web-server/no-listing-01-define-threadpool-struct/src/bin/main.rs b/listings/ch20-web-server/no-listing-01-define-threadpool-struct/src/bin/main.rs index d4e0e0c0..8df1b17e 100644 --- a/listings/ch20-web-server/no-listing-01-define-threadpool-struct/src/bin/main.rs +++ b/listings/ch20-web-server/no-listing-01-define-threadpool-struct/src/bin/main.rs @@ -22,7 +22,7 @@ fn main() { } fn handle_connection(mut stream: TcpStream) { - let mut buffer = [0; 512]; + let mut buffer = [0; 1024]; stream.read(&mut buffer).unwrap(); let get = b"GET / HTTP/1.1\r\n"; diff --git a/listings/ch20-web-server/no-listing-02-impl-threadpool-new/src/bin/main.rs b/listings/ch20-web-server/no-listing-02-impl-threadpool-new/src/bin/main.rs index 452f6822..9d6a9c15 100644 --- a/listings/ch20-web-server/no-listing-02-impl-threadpool-new/src/bin/main.rs +++ b/listings/ch20-web-server/no-listing-02-impl-threadpool-new/src/bin/main.rs @@ -20,7 +20,7 @@ fn main() { } fn handle_connection(mut stream: TcpStream) { - let mut buffer = [0; 512]; + let mut buffer = [0; 1024]; stream.read(&mut buffer).unwrap(); let get = b"GET / HTTP/1.1\r\n"; diff --git a/listings/ch20-web-server/no-listing-03-define-execute/src/bin/main.rs b/listings/ch20-web-server/no-listing-03-define-execute/src/bin/main.rs index 452f6822..9d6a9c15 100644 --- a/listings/ch20-web-server/no-listing-03-define-execute/src/bin/main.rs +++ b/listings/ch20-web-server/no-listing-03-define-execute/src/bin/main.rs @@ -20,7 +20,7 @@ fn main() { } fn handle_connection(mut stream: TcpStream) { - let mut buffer = [0; 512]; + let mut buffer = [0; 1024]; stream.read(&mut buffer).unwrap(); let get = b"GET / HTTP/1.1\r\n"; diff --git a/listings/ch20-web-server/no-listing-04-update-worker-definition/src/bin/main.rs b/listings/ch20-web-server/no-listing-04-update-worker-definition/src/bin/main.rs index 452f6822..9d6a9c15 100644 --- a/listings/ch20-web-server/no-listing-04-update-worker-definition/src/bin/main.rs +++ b/listings/ch20-web-server/no-listing-04-update-worker-definition/src/bin/main.rs @@ -20,7 +20,7 @@ fn main() { } fn handle_connection(mut stream: TcpStream) { - let mut buffer = [0; 512]; + let mut buffer = [0; 1024]; stream.read(&mut buffer).unwrap(); let get = b"GET / HTTP/1.1\r\n"; diff --git a/listings/ch20-web-server/no-listing-05-fix-worker-new/src/bin/main.rs b/listings/ch20-web-server/no-listing-05-fix-worker-new/src/bin/main.rs index 452f6822..9d6a9c15 100644 --- a/listings/ch20-web-server/no-listing-05-fix-worker-new/src/bin/main.rs +++ b/listings/ch20-web-server/no-listing-05-fix-worker-new/src/bin/main.rs @@ -20,7 +20,7 @@ fn main() { } fn handle_connection(mut stream: TcpStream) { - let mut buffer = [0; 512]; + let mut buffer = [0; 1024]; stream.read(&mut buffer).unwrap(); let get = b"GET / HTTP/1.1\r\n"; diff --git a/listings/ch20-web-server/no-listing-06-fix-threadpool-drop/src/bin/main.rs b/listings/ch20-web-server/no-listing-06-fix-threadpool-drop/src/bin/main.rs index 452f6822..9d6a9c15 100644 --- a/listings/ch20-web-server/no-listing-06-fix-threadpool-drop/src/bin/main.rs +++ b/listings/ch20-web-server/no-listing-06-fix-threadpool-drop/src/bin/main.rs @@ -20,7 +20,7 @@ fn main() { } fn handle_connection(mut stream: TcpStream) { - let mut buffer = [0; 512]; + let mut buffer = [0; 1024]; stream.read(&mut buffer).unwrap(); let get = b"GET / HTTP/1.1\r\n"; diff --git a/listings/ch20-web-server/no-listing-07-define-message-enum/src/bin/main.rs b/listings/ch20-web-server/no-listing-07-define-message-enum/src/bin/main.rs index 452f6822..9d6a9c15 100644 --- a/listings/ch20-web-server/no-listing-07-define-message-enum/src/bin/main.rs +++ b/listings/ch20-web-server/no-listing-07-define-message-enum/src/bin/main.rs @@ -20,7 +20,7 @@ fn main() { } fn handle_connection(mut stream: TcpStream) { - let mut buffer = [0; 512]; + let mut buffer = [0; 1024]; stream.read(&mut buffer).unwrap(); let get = b"GET / HTTP/1.1\r\n"; diff --git a/src/ch20-01-single-threaded.md b/src/ch20-01-single-threaded.md index b46d39f6..277e1bb8 100644 --- a/src/ch20-01-single-threaded.md +++ b/src/ch20-01-single-threaded.md @@ -325,7 +325,8 @@ familiar; we used it in Chapter 12 when we read the contents of a file for our I/O project in Listing 12-4. Next, we use `format!` to add the file’s contents as the body of the success -response. +response. To ensure a valid HTTP response, we add the `Content-Length` header +which is set to the size of our response body, in this case the size of `hello.html`. Run this code with `cargo run` and load *127.0.0.1:7878* in your browser; you should see your HTML rendered!