From 698f8c2f01ea549d77d7dc3338a12e04c11057b9 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:02:58 +0200 Subject: Adding upstream version 1.64.0+dfsg1. Signed-off-by: Daniel Baumann --- .../ch20-web-server/listing-20-12/404.html | 11 ++++++ .../ch20-web-server/listing-20-12/Cargo.lock | 6 +++ .../ch20-web-server/listing-20-12/Cargo.toml | 6 +++ .../ch20-web-server/listing-20-12/hello.html | 11 ++++++ .../ch20-web-server/listing-20-12/output.txt | 10 +++++ .../ch20-web-server/listing-20-12/src/main.rs | 44 ++++++++++++++++++++++ 6 files changed, 88 insertions(+) create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-12/404.html create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-12/Cargo.lock create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-12/Cargo.toml create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-12/hello.html create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-12/output.txt create mode 100644 src/doc/book/listings/ch20-web-server/listing-20-12/src/main.rs (limited to 'src/doc/book/listings/ch20-web-server/listing-20-12') diff --git a/src/doc/book/listings/ch20-web-server/listing-20-12/404.html b/src/doc/book/listings/ch20-web-server/listing-20-12/404.html new file mode 100644 index 000000000..88d8e9152 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-12/404.html @@ -0,0 +1,11 @@ + + + + + Hello! + + +

Oops!

+

Sorry, I don't know what you're asking for.

+ + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-12/Cargo.lock b/src/doc/book/listings/ch20-web-server/listing-20-12/Cargo.lock new file mode 100644 index 000000000..f2d069f46 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-12/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "hello" +version = "0.1.0" + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-12/Cargo.toml b/src/doc/book/listings/ch20-web-server/listing-20-12/Cargo.toml new file mode 100644 index 000000000..fe619478a --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-12/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "hello" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/doc/book/listings/ch20-web-server/listing-20-12/hello.html b/src/doc/book/listings/ch20-web-server/listing-20-12/hello.html new file mode 100644 index 000000000..fe442d6b9 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-12/hello.html @@ -0,0 +1,11 @@ + + + + + Hello! + + +

Hello!

+

Hi from Rust

+ + diff --git a/src/doc/book/listings/ch20-web-server/listing-20-12/output.txt b/src/doc/book/listings/ch20-web-server/listing-20-12/output.txt new file mode 100644 index 000000000..57a58b960 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-12/output.txt @@ -0,0 +1,10 @@ +$ cargo check + Checking hello v0.1.0 (file:///projects/hello) +error[E0433]: failed to resolve: use of undeclared type `ThreadPool` + --> src/main.rs:11:16 + | +11 | let pool = ThreadPool::new(4); + | ^^^^^^^^^^ use of undeclared type `ThreadPool` + +For more information about this error, try `rustc --explain E0433`. +error: could not compile `hello` due to previous error diff --git a/src/doc/book/listings/ch20-web-server/listing-20-12/src/main.rs b/src/doc/book/listings/ch20-web-server/listing-20-12/src/main.rs new file mode 100644 index 000000000..21b9a80f1 --- /dev/null +++ b/src/doc/book/listings/ch20-web-server/listing-20-12/src/main.rs @@ -0,0 +1,44 @@ +use std::{ + fs, + io::{prelude::*, BufReader}, + net::{TcpListener, TcpStream}, + thread, + time::Duration, +}; + +// ANCHOR: here +fn main() { + let listener = TcpListener::bind("127.0.0.1:7878").unwrap(); + let pool = ThreadPool::new(4); + + for stream in listener.incoming() { + let stream = stream.unwrap(); + + pool.execute(|| { + handle_connection(stream); + }); + } +} +// ANCHOR_END: here + +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[..] { + "GET / HTTP/1.1" => ("HTTP/1.1 200 OK", "hello.html"), + "GET /sleep HTTP/1.1" => { + thread::sleep(Duration::from_secs(5)); + ("HTTP/1.1 200 OK", "hello.html") + } + _ => ("HTTP/1.1 404 NOT FOUND", "404.html"), + }; + + let contents = fs::read_to_string(filename).unwrap(); + let length = contents.len(); + + let response = + format!("{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}"); + + stream.write_all(response.as_bytes()).unwrap(); +} -- cgit v1.2.3