summaryrefslogtreecommitdiffstats
path: root/src/doc/book/listings/ch20-web-server/listing-20-14
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
commit698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch)
tree173a775858bd501c378080a10dca74132f05bc50 /src/doc/book/listings/ch20-web-server/listing-20-14
parentInitial commit. (diff)
downloadrustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz
rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/doc/book/listings/ch20-web-server/listing-20-14')
-rw-r--r--src/doc/book/listings/ch20-web-server/listing-20-14/404.html11
-rw-r--r--src/doc/book/listings/ch20-web-server/listing-20-14/Cargo.lock6
-rw-r--r--src/doc/book/listings/ch20-web-server/listing-20-14/Cargo.toml6
-rw-r--r--src/doc/book/listings/ch20-web-server/listing-20-14/hello.html11
-rw-r--r--src/doc/book/listings/ch20-web-server/listing-20-14/src/lib.rs40
-rw-r--r--src/doc/book/listings/ch20-web-server/listing-20-14/src/main.rs43
6 files changed, 117 insertions, 0 deletions
diff --git a/src/doc/book/listings/ch20-web-server/listing-20-14/404.html b/src/doc/book/listings/ch20-web-server/listing-20-14/404.html
new file mode 100644
index 000000000..88d8e9152
--- /dev/null
+++ b/src/doc/book/listings/ch20-web-server/listing-20-14/404.html
@@ -0,0 +1,11 @@
+<!DOCTYPE html>
+<html lang="en">
+ <head>
+ <meta charset="utf-8">
+ <title>Hello!</title>
+ </head>
+ <body>
+ <h1>Oops!</h1>
+ <p>Sorry, I don't know what you're asking for.</p>
+ </body>
+</html>
diff --git a/src/doc/book/listings/ch20-web-server/listing-20-14/Cargo.lock b/src/doc/book/listings/ch20-web-server/listing-20-14/Cargo.lock
new file mode 100644
index 000000000..f2d069f46
--- /dev/null
+++ b/src/doc/book/listings/ch20-web-server/listing-20-14/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-14/Cargo.toml b/src/doc/book/listings/ch20-web-server/listing-20-14/Cargo.toml
new file mode 100644
index 000000000..fe619478a
--- /dev/null
+++ b/src/doc/book/listings/ch20-web-server/listing-20-14/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-14/hello.html b/src/doc/book/listings/ch20-web-server/listing-20-14/hello.html
new file mode 100644
index 000000000..fe442d6b9
--- /dev/null
+++ b/src/doc/book/listings/ch20-web-server/listing-20-14/hello.html
@@ -0,0 +1,11 @@
+<!DOCTYPE html>
+<html lang="en">
+ <head>
+ <meta charset="utf-8">
+ <title>Hello!</title>
+ </head>
+ <body>
+ <h1>Hello!</h1>
+ <p>Hi from Rust</p>
+ </body>
+</html>
diff --git a/src/doc/book/listings/ch20-web-server/listing-20-14/src/lib.rs b/src/doc/book/listings/ch20-web-server/listing-20-14/src/lib.rs
new file mode 100644
index 000000000..c1fa1828b
--- /dev/null
+++ b/src/doc/book/listings/ch20-web-server/listing-20-14/src/lib.rs
@@ -0,0 +1,40 @@
+// ANCHOR: here
+use std::thread;
+
+pub struct ThreadPool {
+ threads: Vec<thread::JoinHandle<()>>,
+}
+
+impl ThreadPool {
+ // --snip--
+ // ANCHOR_END: here
+ /// Create a new ThreadPool.
+ ///
+ /// The size is the number of threads in the pool.
+ ///
+ /// # Panics
+ ///
+ /// The `new` function will panic if the size is zero.
+ // ANCHOR: here
+ pub fn new(size: usize) -> ThreadPool {
+ assert!(size > 0);
+
+ let mut threads = Vec::with_capacity(size);
+
+ for _ in 0..size {
+ // create some threads and store them in the vector
+ }
+
+ ThreadPool { threads }
+ }
+ // --snip--
+ // ANCHOR_END: here
+
+ pub fn execute<F>(&self, f: F)
+ where
+ F: FnOnce() + Send + 'static,
+ {
+ }
+ // ANCHOR: here
+}
+// ANCHOR_END: here
diff --git a/src/doc/book/listings/ch20-web-server/listing-20-14/src/main.rs b/src/doc/book/listings/ch20-web-server/listing-20-14/src/main.rs
new file mode 100644
index 000000000..79efb28a2
--- /dev/null
+++ b/src/doc/book/listings/ch20-web-server/listing-20-14/src/main.rs
@@ -0,0 +1,43 @@
+use hello::ThreadPool;
+use std::{
+ fs,
+ io::{prelude::*, BufReader},
+ net::{TcpListener, TcpStream},
+ thread,
+ time::Duration,
+};
+
+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);
+ });
+ }
+}
+
+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();
+}