From 395c1b8c2ed69d6b67bb77c4b6532eb15fab79ea Mon Sep 17 00:00:00 2001 From: apogeeoak <59737221+apogeeoak@users.noreply.github.com> Date: Tue, 26 Jan 2021 16:30:20 -0500 Subject: [PATCH 1/2] Added Content-Length header to Ch 20-01 listings. --- listings/ch20-web-server/listing-20-07/src/main.rs | 9 +++++++-- listings/ch20-web-server/listing-20-09/src/main.rs | 11 ++++++++--- src/ch20-01-single-threaded.md | 8 ++++---- 3 files changed, 19 insertions(+), 9 deletions(-) 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 4d8e51286d..4d19e04a51 100644 --- a/listings/ch20-web-server/listing-20-07/src/main.rs +++ b/listings/ch20-web-server/listing-20-07/src/main.rs @@ -33,10 +33,15 @@ fn handle_connection(mut stream: TcpStream) { // ANCHOR: here // --snip-- } else { - let status_line = "HTTP/1.1 404 NOT FOUND\r\n\r\n"; + let status_line = "HTTP/1.1 404 NOT FOUND"; let contents = fs::read_to_string("404.html").unwrap(); - let response = format!("{}{}", status_line, contents); + let response = format!( + "{}\r\nContent-Length: {}\r\n\r\n{}", + status_line, + 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 8fdfd076ae..e2f2f9f606 100644 --- a/listings/ch20-web-server/listing-20-09/src/main.rs +++ b/listings/ch20-web-server/listing-20-09/src/main.rs @@ -27,14 +27,19 @@ fn handle_connection(mut stream: TcpStream) { // ANCHOR: here let (status_line, filename) = if buffer.starts_with(get) { - ("HTTP/1.1 200 OK\r\n\r\n", "hello.html") + ("HTTP/1.1 200 OK", "hello.html") } else { - ("HTTP/1.1 404 NOT FOUND\r\n\r\n", "404.html") + ("HTTP/1.1 404 NOT FOUND", "404.html") }; let contents = fs::read_to_string(filename).unwrap(); - let response = format!("{}{}", status_line, contents); + let response = format!( + "{}\r\nContent-Length: {}\r\n\r\n{}", + status_line, + contents.len(), + contents + ); stream.write(response.as_bytes()).unwrap(); stream.flush().unwrap(); diff --git a/src/ch20-01-single-threaded.md b/src/ch20-01-single-threaded.md index 0eae4a06b1..d3c0b1ff79 100644 --- a/src/ch20-01-single-threaded.md +++ b/src/ch20-01-single-threaded.md @@ -390,10 +390,10 @@ indicating the response to the end user. error page if anything other than */* was requested Here, our response has a status line with status code 404 and the reason -phrase `NOT FOUND`. We’re still not returning headers, and the body of the -response will be the HTML in the file *404.html*. You’ll need to create a -*404.html* file next to *hello.html* for the error page; again feel free to use -any HTML you want or use the example HTML in Listing 20-8. +phrase `NOT FOUND`. The body of the response will be the HTML in the file +*404.html*. You’ll need to create a *404.html* file next to *hello.html* for +the error page; again feel free to use any HTML you want or use the example +HTML in Listing 20-8. Filename: 404.html From 98c15e36ec4bca5e69caa3ee76e2cddc69d6c90e Mon Sep 17 00:00:00 2001 From: apogeeoak <59737221+apogeeoak@users.noreply.github.com> Date: Tue, 26 Jan 2021 17:45:36 -0500 Subject: [PATCH 2/2] Updated listings in Ch 20-02 and 20-03 with Content-Length header. --- listings/ch20-web-server/listing-20-08/src/main.rs | 9 +++++++-- listings/ch20-web-server/listing-20-10/src/main.rs | 13 +++++++++---- listings/ch20-web-server/listing-20-11/src/main.rs | 13 +++++++++---- listings/ch20-web-server/listing-20-12/src/main.rs | 13 +++++++++---- .../ch20-web-server/listing-20-13/src/bin/main.rs | 13 +++++++++---- .../ch20-web-server/listing-20-14/src/bin/main.rs | 13 +++++++++---- .../ch20-web-server/listing-20-15/src/bin/main.rs | 13 +++++++++---- .../ch20-web-server/listing-20-16/src/bin/main.rs | 13 +++++++++---- .../ch20-web-server/listing-20-17/src/bin/main.rs | 13 +++++++++---- .../ch20-web-server/listing-20-18/src/bin/main.rs | 13 +++++++++---- .../ch20-web-server/listing-20-19/src/bin/main.rs | 13 +++++++++---- .../ch20-web-server/listing-20-20/src/bin/main.rs | 13 +++++++++---- .../ch20-web-server/listing-20-21/src/bin/main.rs | 13 +++++++++---- .../ch20-web-server/listing-20-22/src/bin/main.rs | 13 +++++++++---- .../ch20-web-server/listing-20-23/src/bin/main.rs | 13 +++++++++---- .../ch20-web-server/listing-20-24/src/bin/main.rs | 13 +++++++++---- .../ch20-web-server/listing-20-25/src/bin/main.rs | 13 +++++++++---- .../src/bin/main.rs | 13 +++++++++---- .../src/bin/main.rs | 13 +++++++++---- .../no-listing-03-define-execute/src/bin/main.rs | 13 +++++++++---- .../src/bin/main.rs | 13 +++++++++---- .../no-listing-05-fix-worker-new/src/bin/main.rs | 13 +++++++++---- .../src/bin/main.rs | 13 +++++++++---- .../src/bin/main.rs | 13 +++++++++---- .../no-listing-08-final-code/src/bin/main.rs | 13 +++++++++---- 25 files changed, 223 insertions(+), 98 deletions(-) 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 ef905fa270..6dfaa97f24 100644 --- a/listings/ch20-web-server/listing-20-08/src/main.rs +++ b/listings/ch20-web-server/listing-20-08/src/main.rs @@ -31,10 +31,15 @@ fn handle_connection(mut stream: TcpStream) { stream.write(response.as_bytes()).unwrap(); stream.flush().unwrap(); } else { - let status_line = "HTTP/1.1 404 NOT FOUND\r\n\r\n"; + let status_line = "HTTP/1.1 404 NOT FOUND"; let contents = fs::read_to_string("404.html").unwrap(); - let response = format!("{}{}", status_line, contents); + let response = format!( + "{}\r\nContent-Length: {}\r\n\r\n{}", + status_line, + contents.len(), + contents + ); stream.write(response.as_bytes()).unwrap(); stream.flush().unwrap(); 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 257d47a795..c4361e58a2 100644 --- a/listings/ch20-web-server/listing-20-10/src/main.rs +++ b/listings/ch20-web-server/listing-20-10/src/main.rs @@ -31,12 +31,12 @@ fn handle_connection(mut stream: TcpStream) { let sleep = b"GET /sleep HTTP/1.1\r\n"; let (status_line, filename) = if buffer.starts_with(get) { - ("HTTP/1.1 200 OK\r\n\r\n", "hello.html") + ("HTTP/1.1 200 OK", "hello.html") } else if buffer.starts_with(sleep) { thread::sleep(Duration::from_secs(5)); - ("HTTP/1.1 200 OK\r\n\r\n", "hello.html") + ("HTTP/1.1 200 OK", "hello.html") } else { - ("HTTP/1.1 404 NOT FOUND\r\n\r\n", "404.html") + ("HTTP/1.1 404 NOT FOUND", "404.html") }; // --snip-- @@ -44,7 +44,12 @@ fn handle_connection(mut stream: TcpStream) { let contents = fs::read_to_string(filename).unwrap(); - let response = format!("{}{}", status_line, contents); + let response = format!( + "{}\r\nContent-Length: {}\r\n\r\n{}", + status_line, + contents.len(), + contents + ); stream.write(response.as_bytes()).unwrap(); stream.flush().unwrap(); 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 d1087e2127..bf88f949ce 100644 --- a/listings/ch20-web-server/listing-20-11/src/main.rs +++ b/listings/ch20-web-server/listing-20-11/src/main.rs @@ -27,17 +27,22 @@ fn handle_connection(mut stream: TcpStream) { let sleep = b"GET /sleep HTTP/1.1\r\n"; let (status_line, filename) = if buffer.starts_with(get) { - ("HTTP/1.1 200 OK\r\n\r\n", "hello.html") + ("HTTP/1.1 200 OK", "hello.html") } else if buffer.starts_with(sleep) { thread::sleep(Duration::from_secs(5)); - ("HTTP/1.1 200 OK\r\n\r\n", "hello.html") + ("HTTP/1.1 200 OK", "hello.html") } else { - ("HTTP/1.1 404 NOT FOUND\r\n\r\n", "404.html") + ("HTTP/1.1 404 NOT FOUND", "404.html") }; let contents = fs::read_to_string(filename).unwrap(); - let response = format!("{}{}", status_line, contents); + let response = format!( + "{}\r\nContent-Length: {}\r\n\r\n{}", + status_line, + contents.len(), + contents + ); stream.write(response.as_bytes()).unwrap(); stream.flush().unwrap(); 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 72c5b9a83c..ff0fd45396 100644 --- a/listings/ch20-web-server/listing-20-12/src/main.rs +++ b/listings/ch20-web-server/listing-20-12/src/main.rs @@ -28,17 +28,22 @@ fn handle_connection(mut stream: TcpStream) { let sleep = b"GET /sleep HTTP/1.1\r\n"; let (status_line, filename) = if buffer.starts_with(get) { - ("HTTP/1.1 200 OK\r\n\r\n", "hello.html") + ("HTTP/1.1 200 OK", "hello.html") } else if buffer.starts_with(sleep) { thread::sleep(Duration::from_secs(5)); - ("HTTP/1.1 200 OK\r\n\r\n", "hello.html") + ("HTTP/1.1 200 OK", "hello.html") } else { - ("HTTP/1.1 404 NOT FOUND\r\n\r\n", "404.html") + ("HTTP/1.1 404 NOT FOUND", "404.html") }; let contents = fs::read_to_string(filename).unwrap(); - let response = format!("{}{}", status_line, contents); + let response = format!( + "{}\r\nContent-Length: {}\r\n\r\n{}", + status_line, + contents.len(), + contents + ); stream.write(response.as_bytes()).unwrap(); stream.flush().unwrap(); 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 9d6a9c1546..2078ca9f11 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 @@ -27,17 +27,22 @@ fn handle_connection(mut stream: TcpStream) { let sleep = b"GET /sleep HTTP/1.1\r\n"; let (status_line, filename) = if buffer.starts_with(get) { - ("HTTP/1.1 200 OK\r\n\r\n", "hello.html") + ("HTTP/1.1 200 OK", "hello.html") } else if buffer.starts_with(sleep) { thread::sleep(Duration::from_secs(5)); - ("HTTP/1.1 200 OK\r\n\r\n", "hello.html") + ("HTTP/1.1 200 OK", "hello.html") } else { - ("HTTP/1.1 404 NOT FOUND\r\n\r\n", "404.html") + ("HTTP/1.1 404 NOT FOUND", "404.html") }; let contents = fs::read_to_string(filename).unwrap(); - let response = format!("{}{}", status_line, contents); + let response = format!( + "{}\r\nContent-Length: {}\r\n\r\n{}", + status_line, + contents.len(), + contents + ); stream.write(response.as_bytes()).unwrap(); stream.flush().unwrap(); 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 9d6a9c1546..2078ca9f11 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 @@ -27,17 +27,22 @@ fn handle_connection(mut stream: TcpStream) { let sleep = b"GET /sleep HTTP/1.1\r\n"; let (status_line, filename) = if buffer.starts_with(get) { - ("HTTP/1.1 200 OK\r\n\r\n", "hello.html") + ("HTTP/1.1 200 OK", "hello.html") } else if buffer.starts_with(sleep) { thread::sleep(Duration::from_secs(5)); - ("HTTP/1.1 200 OK\r\n\r\n", "hello.html") + ("HTTP/1.1 200 OK", "hello.html") } else { - ("HTTP/1.1 404 NOT FOUND\r\n\r\n", "404.html") + ("HTTP/1.1 404 NOT FOUND", "404.html") }; let contents = fs::read_to_string(filename).unwrap(); - let response = format!("{}{}", status_line, contents); + let response = format!( + "{}\r\nContent-Length: {}\r\n\r\n{}", + status_line, + contents.len(), + contents + ); stream.write(response.as_bytes()).unwrap(); stream.flush().unwrap(); 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 9d6a9c1546..2078ca9f11 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 @@ -27,17 +27,22 @@ fn handle_connection(mut stream: TcpStream) { let sleep = b"GET /sleep HTTP/1.1\r\n"; let (status_line, filename) = if buffer.starts_with(get) { - ("HTTP/1.1 200 OK\r\n\r\n", "hello.html") + ("HTTP/1.1 200 OK", "hello.html") } else if buffer.starts_with(sleep) { thread::sleep(Duration::from_secs(5)); - ("HTTP/1.1 200 OK\r\n\r\n", "hello.html") + ("HTTP/1.1 200 OK", "hello.html") } else { - ("HTTP/1.1 404 NOT FOUND\r\n\r\n", "404.html") + ("HTTP/1.1 404 NOT FOUND", "404.html") }; let contents = fs::read_to_string(filename).unwrap(); - let response = format!("{}{}", status_line, contents); + let response = format!( + "{}\r\nContent-Length: {}\r\n\r\n{}", + status_line, + contents.len(), + contents + ); stream.write(response.as_bytes()).unwrap(); stream.flush().unwrap(); 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 9d6a9c1546..2078ca9f11 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 @@ -27,17 +27,22 @@ fn handle_connection(mut stream: TcpStream) { let sleep = b"GET /sleep HTTP/1.1\r\n"; let (status_line, filename) = if buffer.starts_with(get) { - ("HTTP/1.1 200 OK\r\n\r\n", "hello.html") + ("HTTP/1.1 200 OK", "hello.html") } else if buffer.starts_with(sleep) { thread::sleep(Duration::from_secs(5)); - ("HTTP/1.1 200 OK\r\n\r\n", "hello.html") + ("HTTP/1.1 200 OK", "hello.html") } else { - ("HTTP/1.1 404 NOT FOUND\r\n\r\n", "404.html") + ("HTTP/1.1 404 NOT FOUND", "404.html") }; let contents = fs::read_to_string(filename).unwrap(); - let response = format!("{}{}", status_line, contents); + let response = format!( + "{}\r\nContent-Length: {}\r\n\r\n{}", + status_line, + contents.len(), + contents + ); stream.write(response.as_bytes()).unwrap(); stream.flush().unwrap(); 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 9d6a9c1546..2078ca9f11 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 @@ -27,17 +27,22 @@ fn handle_connection(mut stream: TcpStream) { let sleep = b"GET /sleep HTTP/1.1\r\n"; let (status_line, filename) = if buffer.starts_with(get) { - ("HTTP/1.1 200 OK\r\n\r\n", "hello.html") + ("HTTP/1.1 200 OK", "hello.html") } else if buffer.starts_with(sleep) { thread::sleep(Duration::from_secs(5)); - ("HTTP/1.1 200 OK\r\n\r\n", "hello.html") + ("HTTP/1.1 200 OK", "hello.html") } else { - ("HTTP/1.1 404 NOT FOUND\r\n\r\n", "404.html") + ("HTTP/1.1 404 NOT FOUND", "404.html") }; let contents = fs::read_to_string(filename).unwrap(); - let response = format!("{}{}", status_line, contents); + let response = format!( + "{}\r\nContent-Length: {}\r\n\r\n{}", + status_line, + contents.len(), + contents + ); stream.write(response.as_bytes()).unwrap(); stream.flush().unwrap(); 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 9d6a9c1546..2078ca9f11 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 @@ -27,17 +27,22 @@ fn handle_connection(mut stream: TcpStream) { let sleep = b"GET /sleep HTTP/1.1\r\n"; let (status_line, filename) = if buffer.starts_with(get) { - ("HTTP/1.1 200 OK\r\n\r\n", "hello.html") + ("HTTP/1.1 200 OK", "hello.html") } else if buffer.starts_with(sleep) { thread::sleep(Duration::from_secs(5)); - ("HTTP/1.1 200 OK\r\n\r\n", "hello.html") + ("HTTP/1.1 200 OK", "hello.html") } else { - ("HTTP/1.1 404 NOT FOUND\r\n\r\n", "404.html") + ("HTTP/1.1 404 NOT FOUND", "404.html") }; let contents = fs::read_to_string(filename).unwrap(); - let response = format!("{}{}", status_line, contents); + let response = format!( + "{}\r\nContent-Length: {}\r\n\r\n{}", + status_line, + contents.len(), + contents + ); stream.write(response.as_bytes()).unwrap(); stream.flush().unwrap(); 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 9d6a9c1546..2078ca9f11 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 @@ -27,17 +27,22 @@ fn handle_connection(mut stream: TcpStream) { let sleep = b"GET /sleep HTTP/1.1\r\n"; let (status_line, filename) = if buffer.starts_with(get) { - ("HTTP/1.1 200 OK\r\n\r\n", "hello.html") + ("HTTP/1.1 200 OK", "hello.html") } else if buffer.starts_with(sleep) { thread::sleep(Duration::from_secs(5)); - ("HTTP/1.1 200 OK\r\n\r\n", "hello.html") + ("HTTP/1.1 200 OK", "hello.html") } else { - ("HTTP/1.1 404 NOT FOUND\r\n\r\n", "404.html") + ("HTTP/1.1 404 NOT FOUND", "404.html") }; let contents = fs::read_to_string(filename).unwrap(); - let response = format!("{}{}", status_line, contents); + let response = format!( + "{}\r\nContent-Length: {}\r\n\r\n{}", + status_line, + contents.len(), + contents + ); stream.write(response.as_bytes()).unwrap(); stream.flush().unwrap(); 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 9d6a9c1546..2078ca9f11 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 @@ -27,17 +27,22 @@ fn handle_connection(mut stream: TcpStream) { let sleep = b"GET /sleep HTTP/1.1\r\n"; let (status_line, filename) = if buffer.starts_with(get) { - ("HTTP/1.1 200 OK\r\n\r\n", "hello.html") + ("HTTP/1.1 200 OK", "hello.html") } else if buffer.starts_with(sleep) { thread::sleep(Duration::from_secs(5)); - ("HTTP/1.1 200 OK\r\n\r\n", "hello.html") + ("HTTP/1.1 200 OK", "hello.html") } else { - ("HTTP/1.1 404 NOT FOUND\r\n\r\n", "404.html") + ("HTTP/1.1 404 NOT FOUND", "404.html") }; let contents = fs::read_to_string(filename).unwrap(); - let response = format!("{}{}", status_line, contents); + let response = format!( + "{}\r\nContent-Length: {}\r\n\r\n{}", + status_line, + contents.len(), + contents + ); stream.write(response.as_bytes()).unwrap(); stream.flush().unwrap(); 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 9d6a9c1546..2078ca9f11 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 @@ -27,17 +27,22 @@ fn handle_connection(mut stream: TcpStream) { let sleep = b"GET /sleep HTTP/1.1\r\n"; let (status_line, filename) = if buffer.starts_with(get) { - ("HTTP/1.1 200 OK\r\n\r\n", "hello.html") + ("HTTP/1.1 200 OK", "hello.html") } else if buffer.starts_with(sleep) { thread::sleep(Duration::from_secs(5)); - ("HTTP/1.1 200 OK\r\n\r\n", "hello.html") + ("HTTP/1.1 200 OK", "hello.html") } else { - ("HTTP/1.1 404 NOT FOUND\r\n\r\n", "404.html") + ("HTTP/1.1 404 NOT FOUND", "404.html") }; let contents = fs::read_to_string(filename).unwrap(); - let response = format!("{}{}", status_line, contents); + let response = format!( + "{}\r\nContent-Length: {}\r\n\r\n{}", + status_line, + contents.len(), + contents + ); stream.write(response.as_bytes()).unwrap(); stream.flush().unwrap(); 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 9d6a9c1546..2078ca9f11 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 @@ -27,17 +27,22 @@ fn handle_connection(mut stream: TcpStream) { let sleep = b"GET /sleep HTTP/1.1\r\n"; let (status_line, filename) = if buffer.starts_with(get) { - ("HTTP/1.1 200 OK\r\n\r\n", "hello.html") + ("HTTP/1.1 200 OK", "hello.html") } else if buffer.starts_with(sleep) { thread::sleep(Duration::from_secs(5)); - ("HTTP/1.1 200 OK\r\n\r\n", "hello.html") + ("HTTP/1.1 200 OK", "hello.html") } else { - ("HTTP/1.1 404 NOT FOUND\r\n\r\n", "404.html") + ("HTTP/1.1 404 NOT FOUND", "404.html") }; let contents = fs::read_to_string(filename).unwrap(); - let response = format!("{}{}", status_line, contents); + let response = format!( + "{}\r\nContent-Length: {}\r\n\r\n{}", + status_line, + contents.len(), + contents + ); stream.write(response.as_bytes()).unwrap(); stream.flush().unwrap(); 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 9d6a9c1546..2078ca9f11 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 @@ -27,17 +27,22 @@ fn handle_connection(mut stream: TcpStream) { let sleep = b"GET /sleep HTTP/1.1\r\n"; let (status_line, filename) = if buffer.starts_with(get) { - ("HTTP/1.1 200 OK\r\n\r\n", "hello.html") + ("HTTP/1.1 200 OK", "hello.html") } else if buffer.starts_with(sleep) { thread::sleep(Duration::from_secs(5)); - ("HTTP/1.1 200 OK\r\n\r\n", "hello.html") + ("HTTP/1.1 200 OK", "hello.html") } else { - ("HTTP/1.1 404 NOT FOUND\r\n\r\n", "404.html") + ("HTTP/1.1 404 NOT FOUND", "404.html") }; let contents = fs::read_to_string(filename).unwrap(); - let response = format!("{}{}", status_line, contents); + let response = format!( + "{}\r\nContent-Length: {}\r\n\r\n{}", + status_line, + contents.len(), + contents + ); stream.write(response.as_bytes()).unwrap(); stream.flush().unwrap(); 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 9d6a9c1546..2078ca9f11 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 @@ -27,17 +27,22 @@ fn handle_connection(mut stream: TcpStream) { let sleep = b"GET /sleep HTTP/1.1\r\n"; let (status_line, filename) = if buffer.starts_with(get) { - ("HTTP/1.1 200 OK\r\n\r\n", "hello.html") + ("HTTP/1.1 200 OK", "hello.html") } else if buffer.starts_with(sleep) { thread::sleep(Duration::from_secs(5)); - ("HTTP/1.1 200 OK\r\n\r\n", "hello.html") + ("HTTP/1.1 200 OK", "hello.html") } else { - ("HTTP/1.1 404 NOT FOUND\r\n\r\n", "404.html") + ("HTTP/1.1 404 NOT FOUND", "404.html") }; let contents = fs::read_to_string(filename).unwrap(); - let response = format!("{}{}", status_line, contents); + let response = format!( + "{}\r\nContent-Length: {}\r\n\r\n{}", + status_line, + contents.len(), + contents + ); stream.write(response.as_bytes()).unwrap(); stream.flush().unwrap(); 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 694df159ab..3b314bc65b 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 @@ -31,17 +31,22 @@ fn handle_connection(mut stream: TcpStream) { let sleep = b"GET /sleep HTTP/1.1\r\n"; let (status_line, filename) = if buffer.starts_with(get) { - ("HTTP/1.1 200 OK\r\n\r\n", "hello.html") + ("HTTP/1.1 200 OK", "hello.html") } else if buffer.starts_with(sleep) { thread::sleep(Duration::from_secs(5)); - ("HTTP/1.1 200 OK\r\n\r\n", "hello.html") + ("HTTP/1.1 200 OK", "hello.html") } else { - ("HTTP/1.1 404 NOT FOUND\r\n\r\n", "404.html") + ("HTTP/1.1 404 NOT FOUND", "404.html") }; let contents = fs::read_to_string(filename).unwrap(); - let response = format!("{}{}", status_line, contents); + let response = format!( + "{}\r\nContent-Length: {}\r\n\r\n{}", + status_line, + contents.len(), + contents + ); stream.write(response.as_bytes()).unwrap(); stream.flush().unwrap(); 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 8df1b17ee8..d2b8c889d0 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 @@ -29,17 +29,22 @@ fn handle_connection(mut stream: TcpStream) { let sleep = b"GET /sleep HTTP/1.1\r\n"; let (status_line, filename) = if buffer.starts_with(get) { - ("HTTP/1.1 200 OK\r\n\r\n", "hello.html") + ("HTTP/1.1 200 OK", "hello.html") } else if buffer.starts_with(sleep) { thread::sleep(Duration::from_secs(5)); - ("HTTP/1.1 200 OK\r\n\r\n", "hello.html") + ("HTTP/1.1 200 OK", "hello.html") } else { - ("HTTP/1.1 404 NOT FOUND\r\n\r\n", "404.html") + ("HTTP/1.1 404 NOT FOUND", "404.html") }; let contents = fs::read_to_string(filename).unwrap(); - let response = format!("{}{}", status_line, contents); + let response = format!( + "{}\r\nContent-Length: {}\r\n\r\n{}", + status_line, + contents.len(), + contents + ); stream.write(response.as_bytes()).unwrap(); stream.flush().unwrap(); 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 9d6a9c1546..2078ca9f11 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 @@ -27,17 +27,22 @@ fn handle_connection(mut stream: TcpStream) { let sleep = b"GET /sleep HTTP/1.1\r\n"; let (status_line, filename) = if buffer.starts_with(get) { - ("HTTP/1.1 200 OK\r\n\r\n", "hello.html") + ("HTTP/1.1 200 OK", "hello.html") } else if buffer.starts_with(sleep) { thread::sleep(Duration::from_secs(5)); - ("HTTP/1.1 200 OK\r\n\r\n", "hello.html") + ("HTTP/1.1 200 OK", "hello.html") } else { - ("HTTP/1.1 404 NOT FOUND\r\n\r\n", "404.html") + ("HTTP/1.1 404 NOT FOUND", "404.html") }; let contents = fs::read_to_string(filename).unwrap(); - let response = format!("{}{}", status_line, contents); + let response = format!( + "{}\r\nContent-Length: {}\r\n\r\n{}", + status_line, + contents.len(), + contents + ); stream.write(response.as_bytes()).unwrap(); stream.flush().unwrap(); 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 9d6a9c1546..2078ca9f11 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 @@ -27,17 +27,22 @@ fn handle_connection(mut stream: TcpStream) { let sleep = b"GET /sleep HTTP/1.1\r\n"; let (status_line, filename) = if buffer.starts_with(get) { - ("HTTP/1.1 200 OK\r\n\r\n", "hello.html") + ("HTTP/1.1 200 OK", "hello.html") } else if buffer.starts_with(sleep) { thread::sleep(Duration::from_secs(5)); - ("HTTP/1.1 200 OK\r\n\r\n", "hello.html") + ("HTTP/1.1 200 OK", "hello.html") } else { - ("HTTP/1.1 404 NOT FOUND\r\n\r\n", "404.html") + ("HTTP/1.1 404 NOT FOUND", "404.html") }; let contents = fs::read_to_string(filename).unwrap(); - let response = format!("{}{}", status_line, contents); + let response = format!( + "{}\r\nContent-Length: {}\r\n\r\n{}", + status_line, + contents.len(), + contents + ); stream.write(response.as_bytes()).unwrap(); stream.flush().unwrap(); 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 9d6a9c1546..2078ca9f11 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 @@ -27,17 +27,22 @@ fn handle_connection(mut stream: TcpStream) { let sleep = b"GET /sleep HTTP/1.1\r\n"; let (status_line, filename) = if buffer.starts_with(get) { - ("HTTP/1.1 200 OK\r\n\r\n", "hello.html") + ("HTTP/1.1 200 OK", "hello.html") } else if buffer.starts_with(sleep) { thread::sleep(Duration::from_secs(5)); - ("HTTP/1.1 200 OK\r\n\r\n", "hello.html") + ("HTTP/1.1 200 OK", "hello.html") } else { - ("HTTP/1.1 404 NOT FOUND\r\n\r\n", "404.html") + ("HTTP/1.1 404 NOT FOUND", "404.html") }; let contents = fs::read_to_string(filename).unwrap(); - let response = format!("{}{}", status_line, contents); + let response = format!( + "{}\r\nContent-Length: {}\r\n\r\n{}", + status_line, + contents.len(), + contents + ); stream.write(response.as_bytes()).unwrap(); stream.flush().unwrap(); 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 9d6a9c1546..2078ca9f11 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 @@ -27,17 +27,22 @@ fn handle_connection(mut stream: TcpStream) { let sleep = b"GET /sleep HTTP/1.1\r\n"; let (status_line, filename) = if buffer.starts_with(get) { - ("HTTP/1.1 200 OK\r\n\r\n", "hello.html") + ("HTTP/1.1 200 OK", "hello.html") } else if buffer.starts_with(sleep) { thread::sleep(Duration::from_secs(5)); - ("HTTP/1.1 200 OK\r\n\r\n", "hello.html") + ("HTTP/1.1 200 OK", "hello.html") } else { - ("HTTP/1.1 404 NOT FOUND\r\n\r\n", "404.html") + ("HTTP/1.1 404 NOT FOUND", "404.html") }; let contents = fs::read_to_string(filename).unwrap(); - let response = format!("{}{}", status_line, contents); + let response = format!( + "{}\r\nContent-Length: {}\r\n\r\n{}", + status_line, + contents.len(), + contents + ); stream.write(response.as_bytes()).unwrap(); stream.flush().unwrap(); 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 9d6a9c1546..2078ca9f11 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 @@ -27,17 +27,22 @@ fn handle_connection(mut stream: TcpStream) { let sleep = b"GET /sleep HTTP/1.1\r\n"; let (status_line, filename) = if buffer.starts_with(get) { - ("HTTP/1.1 200 OK\r\n\r\n", "hello.html") + ("HTTP/1.1 200 OK", "hello.html") } else if buffer.starts_with(sleep) { thread::sleep(Duration::from_secs(5)); - ("HTTP/1.1 200 OK\r\n\r\n", "hello.html") + ("HTTP/1.1 200 OK", "hello.html") } else { - ("HTTP/1.1 404 NOT FOUND\r\n\r\n", "404.html") + ("HTTP/1.1 404 NOT FOUND", "404.html") }; let contents = fs::read_to_string(filename).unwrap(); - let response = format!("{}{}", status_line, contents); + let response = format!( + "{}\r\nContent-Length: {}\r\n\r\n{}", + status_line, + contents.len(), + contents + ); stream.write(response.as_bytes()).unwrap(); stream.flush().unwrap(); 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 9d6a9c1546..2078ca9f11 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 @@ -27,17 +27,22 @@ fn handle_connection(mut stream: TcpStream) { let sleep = b"GET /sleep HTTP/1.1\r\n"; let (status_line, filename) = if buffer.starts_with(get) { - ("HTTP/1.1 200 OK\r\n\r\n", "hello.html") + ("HTTP/1.1 200 OK", "hello.html") } else if buffer.starts_with(sleep) { thread::sleep(Duration::from_secs(5)); - ("HTTP/1.1 200 OK\r\n\r\n", "hello.html") + ("HTTP/1.1 200 OK", "hello.html") } else { - ("HTTP/1.1 404 NOT FOUND\r\n\r\n", "404.html") + ("HTTP/1.1 404 NOT FOUND", "404.html") }; let contents = fs::read_to_string(filename).unwrap(); - let response = format!("{}{}", status_line, contents); + let response = format!( + "{}\r\nContent-Length: {}\r\n\r\n{}", + status_line, + contents.len(), + contents + ); stream.write(response.as_bytes()).unwrap(); stream.flush().unwrap(); diff --git a/listings/ch20-web-server/no-listing-08-final-code/src/bin/main.rs b/listings/ch20-web-server/no-listing-08-final-code/src/bin/main.rs index 7e2508e2a5..d3f8754f63 100644 --- a/listings/ch20-web-server/no-listing-08-final-code/src/bin/main.rs +++ b/listings/ch20-web-server/no-listing-08-final-code/src/bin/main.rs @@ -29,17 +29,22 @@ fn handle_connection(mut stream: TcpStream) { let sleep = b"GET /sleep HTTP/1.1\r\n"; let (status_line, filename) = if buffer.starts_with(get) { - ("HTTP/1.1 200 OK\r\n\r\n", "hello.html") + ("HTTP/1.1 200 OK", "hello.html") } else if buffer.starts_with(sleep) { thread::sleep(Duration::from_secs(5)); - ("HTTP/1.1 200 OK\r\n\r\n", "hello.html") + ("HTTP/1.1 200 OK", "hello.html") } else { - ("HTTP/1.1 404 NOT FOUND\r\n\r\n", "404.html") + ("HTTP/1.1 404 NOT FOUND", "404.html") }; let contents = fs::read_to_string(filename).unwrap(); - let response = format!("{}{}", status_line, contents); + let response = format!( + "{}\r\nContent-Length: {}\r\n\r\n{}", + status_line, + contents.len(), + contents + ); stream.write(response.as_bytes()).unwrap(); stream.flush().unwrap();