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