From ed5840afab594ac8ea188aa1a049b8528ff7701d Mon Sep 17 00:00:00 2001 From: Ada Werefox Date: Sat, 25 Mar 2023 21:03:12 +0000 Subject: [PATCH] Completed project. --- 404.html | 11 +++++++ Cargo.lock | 7 +++++ hello.html | 12 +++++++ src/lib.rs | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 41 +++++++++++++++++++++++- 5 files changed, 160 insertions(+), 1 deletion(-) create mode 100644 404.html create mode 100644 Cargo.lock create mode 100644 hello.html create mode 100644 src/lib.rs diff --git a/404.html b/404.html new file mode 100644 index 0000000..191f3f0 --- /dev/null +++ b/404.html @@ -0,0 +1,11 @@ + + + + + Hello! + + +

Oops!

+

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

+ + \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..7ffcc81 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "rust-web-server" +version = "0.1.0" diff --git a/hello.html b/hello.html new file mode 100644 index 0000000..667a078 --- /dev/null +++ b/hello.html @@ -0,0 +1,12 @@ + + + + + Hello! + + + +

Hello!

+

Hi from Rust

+ + diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..f7cda45 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,90 @@ +use std::{ + sync::{mpsc, Arc, Mutex}, + thread, +}; + +pub struct ThreadPool { + workers: Vec, + sender: Option>, +} + +type Job = Box; + +impl ThreadPool { + /// 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. + pub fn new(size: usize) -> ThreadPool { + assert!(size > 0); + + let (sender, receiver) = mpsc::channel(); + + let receiver = Arc::new(Mutex::new(receiver)); + + let mut workers = Vec::with_capacity(size); + + for id in 0..size { + workers.push(Worker::new(id, Arc::clone(&receiver))); + } + + ThreadPool { + workers, + sender: Some(sender), + } + } + + pub fn execute(&self, f: F) + where + F: FnOnce() + Send + 'static, + { + let job = Box::new(f); + + self.sender.as_ref().unwrap().send(job).unwrap(); + } +} + +impl Drop for ThreadPool { + fn drop(&mut self) { + drop(self.sender.take()); + + for worker in &mut self.workers { + println!("Shutting down worker {}", worker.id); + + if let Some(thread) = worker.thread.take() { + thread.join().unwrap(); + } + } + } +} + +struct Worker { + id: usize, + thread: Option>, +} + +impl Worker { + fn new(id: usize, receiver: Arc>>) -> Worker { + let thread = thread::spawn(move || loop { + match receiver.lock().unwrap().recv() { + Ok(job) => { + println!("Worker {id} got a job; executing."); + + job(); + } + Err(_) => { + println!("Worker {id} disconnected; shutting down."); + break; + } + } + }); + + Worker { + id, + thread: Some(thread), + } + } +} diff --git a/src/main.rs b/src/main.rs index e7a11a9..37fef1e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,42 @@ +use rust_web_server::ThreadPool; +use std::{ + fs, + io::{prelude::*, BufReader}, + net::{TcpListener, TcpStream}, + thread, + time::Duration, +}; fn main() { - println!("Hello, world!"); + let listener = TcpListener::bind("127.0.0.1:7878").unwrap(); + let pool = ThreadPool::new(4); + + for stream in listener.incoming().take(2) { + let stream = stream.unwrap(); + + pool.execute(|| { + handle_connection(stream); + }); + + println!("Shutting down."); + } +} + +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(); }