Skip to content

Commit

Permalink
Merge pull request #2246 from Adeleet/master
Browse files Browse the repository at this point in the history
Increase HTTP buffer size and add 'Content-Length' header
  • Loading branch information
steveklabnik authored Apr 15, 2020
2 parents f5db319 + a641809 commit f688ff2
Show file tree
Hide file tree
Showing 32 changed files with 53 additions and 36 deletions.
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

0 comments on commit f688ff2

Please sign in to comment.