diff --git a/Cargo.lock b/Cargo.lock index 5d0b4f4..40a4e04 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -23,14 +23,6 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" -[[package]] -name = "core" -version = "0.1.0" -dependencies = [ - "ctrlc", - "regex", -] - [[package]] name = "ctrlc" version = "3.4.0" @@ -41,6 +33,14 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "http_core" +version = "0.1.0" +dependencies = [ + "ctrlc", + "regex", +] + [[package]] name = "libc" version = "0.2.147" diff --git a/Cargo.toml b/Cargo.toml index 1b3b5a5..4dea5e3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,16 +1,16 @@ [package] -name = "core" +name = "http_core" version = "0.1.0" edition = "2021" -description = "A lightweight server core for Rust" +description = "A lightweight http server core for Rust" categories = ["http-server"] keywords = ["core", "server", "http", "minimalist", "lightweight"] authors = ["Ando "] license = "MIT" -repository = "https://github.com/exprust/core" +repository = "https://github.com/rusticore/http-core" [lib] -name = "core" +name = "http_core" path = "src/lib.rs" [dependencies] diff --git a/README.md b/README.md index e09964f..00d7060 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,15 @@

Exprust

-![build](https://github.com/exprust/core/workflows/build/badge.svg) -![license](https://img.shields.io/github/license/exprust/core?color=success) +![build](https://github.com/rusticore/http-core/workflows/build/badge.svg) +![license](https://img.shields.io/github/license/rusticore/http-core?color=success) -A lightweight server core for Rust +A lightweight http server core for Rust ## License diff --git a/examples/hello_lib.rs b/examples/hello_lib.rs index f03b0b9..3865c20 100644 --- a/examples/hello_lib.rs +++ b/examples/hello_lib.rs @@ -1,4 +1,4 @@ -use core::server::Server; +use http_core::server::Server; const HELLO_WORLD_HTML: &str = r#" diff --git a/src/handler.rs b/src/handler.rs index 7fce382..4576a7c 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -14,16 +14,13 @@ impl Handler { pub fn handle_connection(mut tcp_stream: TcpStream, router: &Box) { loop { let mut buffer = [0; 1028]; - match tcp_stream.read(&mut buffer) { Ok(stream_count) => { if stream_count == 0 { break; } - let request = &buffer[0..stream_count]; router(request); - println!("{}", Handler::get_request_path(request)); } Err(e) => panic!("{}", e), @@ -35,7 +32,6 @@ impl Handler { if let Err(e) = ctrlc::set_handler(move || { exit.store(true, Ordering::SeqCst); println!("Ctrl + C pressed. Exiting gracefully..."); - process::exit(0); }) { panic!("{}", e); @@ -45,7 +41,6 @@ impl Handler { pub fn get_request_path(request: &[u8]) -> String { let request_str = String::from_utf8_lossy(request); let request_lines: Vec<&str> = request_str.lines().collect(); - let request_path = request_lines .first() .unwrap_or(&"") diff --git a/src/params.rs b/src/params.rs index f75f6a2..18bad07 100644 --- a/src/params.rs +++ b/src/params.rs @@ -4,7 +4,6 @@ use regex::Regex; pub fn is_empty(host: &str, port: u32) -> bool { let is_host_empty = host.is_empty(); let is_port_empty = port == 0; - match (is_host_empty, is_port_empty) { (true, true) => true, _ => false, @@ -13,7 +12,6 @@ pub fn is_empty(host: &str, port: u32) -> bool { pub fn is_valid_local_host(host: &str) -> bool { let pattern = constants::HOST_REGEX; - match Regex::new(pattern) { Ok(re) => re.is_match(host), Err(_) => false, @@ -22,7 +20,6 @@ pub fn is_valid_local_host(host: &str) -> bool { pub fn is_valid_port(port: u32) -> bool { let pattern = constants::PORT_REGEX; - match Regex::new(pattern) { Ok(re) => re.is_match(&port.to_string()), Err(_) => false, diff --git a/src/server.rs b/src/server.rs index 604ef5e..878eee2 100644 --- a/src/server.rs +++ b/src/server.rs @@ -34,7 +34,6 @@ impl Server { F: Fn(&[u8]) + Send + Sync + 'static, { self.initialize(); - self .routes .lock() @@ -44,36 +43,32 @@ impl Server { pub fn listen(&self, host: &str, port: u32) { self.initialize(); - validator::validate_host_and_port(host, port); - let addr = format!("{}:{}", host, port); - let term = Arc::new(AtomicBool::new(false)); self::Handler::handle_exit_process(Arc::clone(&term).clone()); - println!("App is listening on {}", &addr); - - let listener = TcpListener::bind(&addr).expect("An error occured while listening app"); + let listener = TcpListener::bind(&addr).unwrap(); + match listener.accept() { + Ok((_socket, _addr)) => println!("App is listening on {}", &addr), + Err(e) => println!("An error occured while listening app with error, {}", e), + } for stream in listener.incoming() { if term.load(Ordering::SeqCst) { println!("Exiting thread..."); - break; } match stream { Ok(tcp_stream) => { let routes_clone = Arc::clone(&self.routes); - thread::spawn(move || { let routes_lock = routes_clone.lock().unwrap(); let router = routes_lock.get("/").unwrap_or_else(|| { panic!("An error occurred, no handler found for default route"); }); - Handler::handle_connection(tcp_stream, router); }); } diff --git a/tests/lib_spec.rs b/tests/lib_spec.rs index 6cb8c66..0ba4cd6 100644 --- a/tests/lib_spec.rs +++ b/tests/lib_spec.rs @@ -1,6 +1,6 @@ #[cfg(test)] mod params { - use core::params; + use http_core::params; #[test] fn is_empty() {