Chapter 20. Listing 20-25 is inconsistent with the rest of the code in the chapter
skoocda opened this issue · comments
-
I have searched open and closed issues and pull requests for duplicates, using these search terms:
mut buffer
20-25 -
I have checked the latest
main
branch to see if this has already been fixed, in this file:
Checked, it has not been fixed. -
URL to the section(s) of the book with this problem:
https://github.com/rust-lang/book/blob/main/listings/ch20-web-server/listing-20-25/src/main.rs -
Description of the problem:
The current web-server configuration, as of listing 20-24 uses the following TCP stream handling logic:
fn handle_connection(mut stream: TcpStream) {
let buf_reader = BufReader::new(&mut stream);
let request_line = buf_reader.lines().next().unwrap().unwrap();
let (status_line, filename) = match &request_line[..] {
This changes without comment in the next listing, 20-25, which uses the following logic:
fn handle_connection(mut stream: TcpStream) {
let mut buffer = [0; 1024];
stream.read(&mut buffer).unwrap();
let get = b"GET / HTTP/1.1\r\n";
let sleep = b"GET /sleep HTTP/1.1\r\n";
let (status_line, filename) = if buffer.starts_with(get) {
This logic changes silently between 20-24 and 20-25, although it is not evident until the final code is displayed on https://doc.rust-lang.org/book/ch20-03-graceful-shutdown-and-cleanup.html
This leads to a final codebase that is inconsistent with the reader's code developed via the step-by-step process.
- Suggested fix:
Keep the prior style for consistency (e.g. the buf_reader with match case), or provide reasoning as to why the buffer style improves on the design. The latter case will require additional writing.
Indeed! See #3857 for an open PR that should resolve this – thanks for filing it!