From f02f23f992273107cd7f8d848e820164d775dc5a Mon Sep 17 00:00:00 2001 From: Justine Date: Mon, 9 Jan 2023 18:23:49 +0100 Subject: [PATCH] Ok, using args and all --- Cargo.lock | 261 +++++++++++++++++++++++++++++++++++++++++++++++++++- Cargo.toml | 9 +- README.md | 6 +- src/lib.rs | 2 +- src/main.rs | 156 ++++++++++++++++++++++--------- 5 files changed, 383 insertions(+), 51 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 657abb50..4eee3bef 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -45,12 +45,55 @@ dependencies = [ "generic-array", ] +[[package]] +name = "cc" +version = "1.0.78" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a20104e2335ce8a659d6dd92a51a767a0c062599c73b343fd152cb401e828c3d" + [[package]] name = "cfg-if" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "clap" +version = "4.0.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7db700bc935f9e43e88d00b0850dae18a63773cfbec6d8e070fccf7fef89a39" +dependencies = [ + "bitflags", + "clap_derive", + "clap_lex", + "is-terminal", + "once_cell", + "strsim", + "termcolor", +] + +[[package]] +name = "clap_derive" +version = "4.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0177313f9f02afc995627906bbd8967e2be069f5261954222dac78290c2b9014" +dependencies = [ + "heck", + "proc-macro-error", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d4198f73e42b4936b35b5bb248d81d2b595ecb170da0bac7655c54eedfa8da8" +dependencies = [ + "os_str_bytes", +] + [[package]] name = "config" version = "0.13.3" @@ -105,6 +148,27 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0688c2a7f92e427f44895cd63841bff7b29f8d7a1648b9e7e07a4a365b2e1257" +[[package]] +name = "errno" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f639046355ee4f37944e44f60642c6f3a7efa3cf6b78c78a0d989a8ce6c396a1" +dependencies = [ + "errno-dragonfly", + "libc", + "winapi", +] + +[[package]] +name = "errno-dragonfly" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf" +dependencies = [ + "cc", + "libc", +] + [[package]] name = "generic-array" version = "0.14.6" @@ -136,10 +200,40 @@ dependencies = [ ] [[package]] -name = "hello" -version = "0.1.0" +name = "heck" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9" + +[[package]] +name = "hermit-abi" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee512640fe35acbfb4bb779db6f0d80704c2cacfa2e39b601ef3e3f47d1ae4c7" dependencies = [ - "config", + "libc", +] + +[[package]] +name = "io-lifetimes" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "46112a93252b123d31a119a8d1a1ac19deac4fac6e0e8b0df58f0d4e5870e63c" +dependencies = [ + "libc", + "windows-sys", +] + +[[package]] +name = "is-terminal" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28dfb6c8100ccc63462345b67d1bbc3679177c75ee4bf59bf29c8b1d110b8189" +dependencies = [ + "hermit-abi", + "io-lifetimes", + "rustix", + "windows-sys", ] [[package]] @@ -177,6 +271,12 @@ version = "0.5.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f" +[[package]] +name = "linux-raw-sys" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f051f77a7c8e6957c0696eac88f26b0117e54f52d3fc682ab19397a8812846a4" + [[package]] name = "memchr" version = "2.5.0" @@ -215,6 +315,12 @@ dependencies = [ "hashbrown", ] +[[package]] +name = "os_str_bytes" +version = "6.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b7820b9daea5457c9f21c69448905d723fbd21136ccf521748f23fd49e723ee" + [[package]] name = "pathdiff" version = "0.2.1" @@ -265,6 +371,30 @@ dependencies = [ "sha1", ] +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + [[package]] name = "proc-macro2" version = "1.0.49" @@ -304,6 +434,20 @@ dependencies = [ "ordered-multimap", ] +[[package]] +name = "rustix" +version = "0.36.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4feacf7db682c6c329c4ede12649cd36ecab0f3be5b7d74e6a20304725db4549" +dependencies = [ + "bitflags", + "errno", + "io-lifetimes", + "libc", + "linux-raw-sys", + "windows-sys", +] + [[package]] name = "ryu" version = "1.0.12" @@ -352,6 +496,20 @@ dependencies = [ "digest", ] +[[package]] +name = "ssw" +version = "0.1.0" +dependencies = [ + "clap", + "config", +] + +[[package]] +name = "strsim" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" + [[package]] name = "syn" version = "1.0.107" @@ -363,6 +521,15 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "termcolor" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755" +dependencies = [ + "winapi-util", +] + [[package]] name = "thiserror" version = "1.0.38" @@ -422,6 +589,94 @@ version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi", +] + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows-sys" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a3e1820f08b8513f676f7ab6c1f99ff312fb97b553d30ff4dd86f9f15728aa7" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41d2aa71f6f0cbe00ae5167d90ef3cfe66527d6f613ca78ac8024c3ccab9a19e" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd0f252f5a35cac83d6311b2e795981f5ee6e67eb1f9a7f64eb4500fbc4dcdb4" + +[[package]] +name = "windows_i686_gnu" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fbeae19f6716841636c28d695375df17562ca208b2b7d0dc47635a50ae6c5de7" + +[[package]] +name = "windows_i686_msvc" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "84c12f65daa39dd2babe6e442988fc329d6243fdce47d7d2d155b8d874862246" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf7b1b21b5362cbc318f686150e5bcea75ecedc74dd157d874d754a2ca44b0ed" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09d525d2ba30eeb3297665bd434a54297e4170c7f1a44cad4ef58095b4cd2028" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f40009d85759725a34da6d89a94e63d7bdc50a862acf0dbc7c8e488f1edcb6f5" + [[package]] name = "yaml-rust" version = "0.4.5" diff --git a/Cargo.toml b/Cargo.toml index 2c6490dc..205ba5da 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,9 +1,16 @@ [package] -name = "hello" +name = "ssw" version = "0.1.0" edition = "2021" +description = "A pretty basic web server, derived from the final chapter of the Rust book." # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[profile.release] +opt-level = 3 +strip = true +lto = true +codegen-units = 1 [dependencies] +clap = { version = "4.0.32", features = ["derive"] } config = "0.13.3" diff --git a/README.md b/README.md index e0f0e3dc..732b4eae 100644 --- a/README.md +++ b/README.md @@ -4,5 +4,7 @@ This is a simple web server expanding on the example given in the Rust book. At TODO: * Also answer css requests, pictures, etc => OK -* Fully integrate the config path => More or less -* Gather info about the client (IP, User-Agent, etc) to do funny stuff with it +* Fully integrate the config path => Ok +* Gather info about the client (IP, User-Agent, etc) to do funny stuff with it => Just printing client's adress for now +* Use arguments => Done, clap is very very nice ! +* Not satisfied with the way root_folder is handled, being passed on from one function to another. diff --git a/src/lib.rs b/src/lib.rs index 6276a304..5b22c0c8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -71,12 +71,12 @@ struct Worker { impl Worker { fn new(id: usize, receiver: Arc>>) -> Worker { + println!("Worker {id} online"); let thread = thread::spawn(move || loop { let message = receiver.lock().expect("Lock's been poisoned, another thread panicked").recv(); match message { Ok(job) => { - println!("Worker {id} got a job; executing !"); job(); } Err(_) => { diff --git a/src/main.rs b/src/main.rs index 96319029..28c43bb8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -use hello::{ +use ssw::{ ThreadPool, }; use std::{ @@ -6,61 +6,115 @@ use std::{ io::{prelude::*, BufReader}, net::{TcpListener, TcpStream}, collections::HashMap, + fmt, + thread, + env, }; use config::Config; +use clap::Parser; -//CONSTS -static ROOT_FOLDER: &str = "./html"; -static CONF_FILE: &str = "websrv_conf"; +#[derive(Parser, Debug)] +//#[command(author, version, about, long_about = None)] +#[command(name = "ssw")] +#[command(author = None)] +#[command(about = "A simple web server. Pass a configuration file with -c.")] +struct Args { + #[arg(short, long)] + conf_file: String, +} + +///Stores the configuration +struct WebsrvConfig { + nbr_of_threads: usize, + bind_addr: String, + root_folder: String, +} + +impl WebsrvConfig { + ///Loads the config from a config file + ///No need to put the file extension (eg. .toml) + fn from_file(filename: &str) -> WebsrvConfig { + let cfg = Config::builder() + .add_source(config::File::with_name(filename)) + .build() + .unwrap(); + + let cfg = cfg.try_deserialize::>().expect("Could not deserialize configuration"); + + let nbr_of_threads: usize = cfg + .get("threads") + .expect("Threads not found in config") + .parse() + .unwrap(); -fn main() { - //building config - let cfg = Config::builder() - .add_source(config::File::with_name(CONF_FILE)) - .build() - .unwrap(); + let default_bind_addr = String::from("0.0.0.0:7878"); + let bind_addr = cfg + .get("bind_addr") + .unwrap_or(&default_bind_addr) + .to_string(); - let cfg = cfg.try_deserialize::>().expect("Could not deserialize configuration"); - println!("Starting with configuration: "); - for conf in &cfg { - println!("{:?}", conf); + let default_root_folder = String::from("./html"); + let root_folder = cfg + .get("root_folder") + .unwrap_or(&default_root_folder) + .to_string(); + + let ret_value = WebsrvConfig { + nbr_of_threads: nbr_of_threads, + bind_addr: bind_addr, + root_folder: root_folder, + }; + + return ret_value; } +} - let nbr_of_threads: usize = cfg - .get("threads") - .expect("Threads not found in config") - .parse() - .unwrap(); +impl fmt::Display for WebsrvConfig { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "Number_of_threads : {}, bind_addr : {}, root_folder : {}", + self.nbr_of_threads, + self.bind_addr, + self.root_folder) + } +} - let default_bind_addr = String::from("0.0.0.0:7878"); - let bind_addr = cfg - .get("bind_addr") - .unwrap_or(&default_bind_addr) - .to_string(); - let listener = TcpListener::bind(bind_addr).unwrap(); +fn run_server(conf_path: &str) { + //Getting config + let cfg = WebsrvConfig::from_file(conf_path); - let pool = ThreadPool::new(nbr_of_threads); + println!("==== Squi's Tiny Web Server ===="); + println!("Starting with configuration: "); + println!("{}", cfg); + + + //Got the config, start listening + let listener = TcpListener::bind(cfg.bind_addr).unwrap(); + + let pool = ThreadPool::new(cfg.nbr_of_threads); for stream in listener.incoming() { let stream = stream.unwrap(); + let peer_addr = stream.peer_addr().unwrap(); + println!("INFO : Got a connection from {:?}", peer_addr); + + let root_f = cfg.root_folder.clone(); pool.execute(move || { - handle_connection(stream); + handle_connection(stream, root_f.as_str()); }); } println!("Shutting down."); } -fn search_page(page: &String) -> Option> { - println!("Looking for {page} in {ROOT_FOLDER}"); +fn search_page(page: &String, root_folder: &str) -> Option> { //regular page if !page.contains('.') { let possible_pagenames = vec![format!("{page}.html"), format!("{page}.htm")]; for p in possible_pagenames { - match fs::read(format!("{ROOT_FOLDER}/{p}")) { + match fs::read(format!("{root_folder}/{p}")) { Ok(c) => return Some(c), Err(_) => (), } @@ -68,7 +122,7 @@ fn search_page(page: &String) -> Option> { return None; //css, jpg, etc } else { - match fs::read(format!("{ROOT_FOLDER}/{page}")) { + match fs::read(format!("{root_folder}/{page}")) { Ok(c) => return Some(c), Err(_) => return None, }; @@ -77,7 +131,7 @@ fn search_page(page: &String) -> Option> { ///Analyze the request and answer it ///Returns the complete answer, as a vector of bytes -fn analyze_request_line(line: Vec<&str>) -> Vec { +fn analyze_request_line(line: Vec<&str>, root_folder: &str) -> Vec { let _req = String::from(line[0]); let mut page = String::from(line[1]); let proto = String::from(line[2]); @@ -90,7 +144,7 @@ fn analyze_request_line(line: Vec<&str>) -> Vec { page = String::from("index"); } - let mut content = match search_page(&page) { + let mut content = match search_page(&page, root_folder) { Some(c) => { *&mut ret_code = "200 OK"; c @@ -112,9 +166,7 @@ fn analyze_request_line(line: Vec<&str>) -> Vec { let returned_line = returned_line.as_bytes().to_vec(); return returned_line; }, - Err(_e) => { - println!("Got {_e} since we are sending a picture"); - }, + Err(_) => (), }; let img_format = if page.ends_with(".jpg") || page.ends_with(".jpeg") { @@ -132,28 +184,44 @@ fn analyze_request_line(line: Vec<&str>) -> Vec { } -fn handle_connection(mut stream: TcpStream) { +fn handle_connection(mut stream: TcpStream, root_folder: &str) { let buf_reader = BufReader::new(&mut stream); - //for i in buf_reader.lines() { - // println!("{:?}", i); - //} let mut req_lines = buf_reader.lines(); let request = match req_lines.next() { Some(c) => c, - None => Ok(String::from("RIEN")), + None => Ok(String::from("No request")), }; let request = request.unwrap(); - println!("!!{:?}", request); + println!("INFO: Received request {:?}", request); //Some(Ok("GET /crabe HTTP/1.1")) - if &request != "RIEN" { + if &request != "No request" { let request_analyze: Vec<&str> = request.split_whitespace().collect(); - let ret_line = analyze_request_line(request_analyze); + let ret_line = analyze_request_line(request_analyze, root_folder); let payload: &[u8] = &ret_line; stream.write_all(payload).unwrap(); } } +fn get_args() -> Vec { + let args: Vec = env::args().collect(); + return args; +} + +fn main() { + let args = Args::parse(); + println!("Hello {}!", args.conf_file); + dbg!(args); + + thread::spawn(|| { + run_server("websrv_conf"); + }); + loop { + continue + }; + +} +