Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increase HTTP buffer size and add 'Content-Length' header #2246

Merged
merged 1 commit into from
Apr 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion listings/ch20-web-server/listing-20-02/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion listings/ch20-web-server/listing-20-03/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion listings/ch20-web-server/listing-20-04/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
8 changes: 6 additions & 2 deletions listings/ch20-web-server/listing-20-05/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
8 changes: 6 additions & 2 deletions listings/ch20-web-server/listing-20-06/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,19 @@ 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";

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();
Expand Down
8 changes: 6 additions & 2 deletions listings/ch20-web-server/listing-20-07/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@ 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";

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();
Expand Down
8 changes: 6 additions & 2 deletions listings/ch20-web-server/listing-20-08/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@ 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";

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();
Expand Down
2 changes: 1 addition & 1 deletion listings/ch20-web-server/listing-20-09/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion listings/ch20-web-server/listing-20-10/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion listings/ch20-web-server/listing-20-11/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion listings/ch20-web-server/listing-20-12/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion listings/ch20-web-server/listing-20-13/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion listings/ch20-web-server/listing-20-14/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion listings/ch20-web-server/listing-20-15/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion listings/ch20-web-server/listing-20-16/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion listings/ch20-web-server/listing-20-17/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion listings/ch20-web-server/listing-20-18/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion listings/ch20-web-server/listing-20-19/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion listings/ch20-web-server/listing-20-20/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion listings/ch20-web-server/listing-20-21/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion listings/ch20-web-server/listing-20-22/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion listings/ch20-web-server/listing-20-23/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion listings/ch20-web-server/listing-20-24/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion listings/ch20-web-server/listing-20-25/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
3 changes: 2 additions & 1 deletion src/ch20-01-single-threaded.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down