diff --git a/Cargo.lock b/Cargo.lock index 12ca07b..9344348 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -64,9 +64,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.179" +version = "0.2.180" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c5a2d376baa530d1238d133232d15e239abad80d05838b4b59354e5268af431f" +checksum = "bcc35a38544a891a5f7c865aca548a982ccb3b8650a5b06d0fd33a10283c56fc" [[package]] name = "sha1" @@ -92,9 +92,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" [[package]] -name = "websocket" +name = "ws_client" +version = "0.1.0" + +[[package]] +name = "ws_core" +version = "0.1.0" + +[[package]] +name = "ws_server" version = "0.1.0" dependencies = [ "base64", "sha1", + "ws_core", ] diff --git a/Cargo.toml b/Cargo.toml index f887dd5..85385ce 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,8 +1,2 @@ -[package] -name = "websocket" -version = "0.1.0" -edition = "2024" - -[dependencies] -sha1="0.10.6" -base64 ="0.22" +[workspace] +members = ["ws_core", "ws_client", "ws_server"] diff --git a/src/main.rs b/src/main.rs deleted file mode 100644 index 4b7d9dd..0000000 --- a/src/main.rs +++ /dev/null @@ -1,30 +0,0 @@ -use std::io::Result; -use std::net::TcpListener; -use std::thread; - -mod read_header; -mod send_message; -mod handle_client; - -use handle_client::handle_client; - -pub fn main() -> Result<()> { - let listener = TcpListener::bind("127.0.0.1:8080")?; - - for stream in listener.incoming() { - match stream { - Ok(stream) => { - println!("Connection Established"); - - thread::spawn(move || { - let _ = handle_client(stream); - }); - } - Err(e) => { - println!("Connection failed : {}", e); - } - } - } - - Ok(()) -} diff --git a/ws_client/Cargo.toml b/ws_client/Cargo.toml new file mode 100644 index 0000000..51eea51 --- /dev/null +++ b/ws_client/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "ws_client" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/ws_client/src/main.rs b/ws_client/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/ws_client/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/ws_core/Cargo.toml b/ws_core/Cargo.toml new file mode 100644 index 0000000..648221c --- /dev/null +++ b/ws_core/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "ws_core" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/ws_core/src/lib.rs b/ws_core/src/lib.rs new file mode 100644 index 0000000..d3abbc9 --- /dev/null +++ b/ws_core/src/lib.rs @@ -0,0 +1,2 @@ +pub mod read; +pub mod write; \ No newline at end of file diff --git a/src/read_header.rs b/ws_core/src/read.rs similarity index 97% rename from src/read_header.rs rename to ws_core/src/read.rs index d6cc056..96b1646 100644 --- a/src/read_header.rs +++ b/ws_core/src/read.rs @@ -40,7 +40,7 @@ pub fn read_header(stream: &mut TcpStream) -> Result { } else if payload_len == 127 { let mut next_eight = [0u8; 8]; let _ = stream.read_exact(&mut next_eight); - payload_len = u64::from_be_bytes(next_eight) as u64; + payload_len = u64::from_be_bytes(next_eight); } //now we will get the masking key if we get mask as 1 means the payload data is masked diff --git a/src/send_message.rs b/ws_core/src/write.rs similarity index 100% rename from src/send_message.rs rename to ws_core/src/write.rs diff --git a/ws_server/Cargo.toml b/ws_server/Cargo.toml new file mode 100644 index 0000000..9ba1631 --- /dev/null +++ b/ws_server/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "ws_server" +version = "0.1.0" +edition = "2024" + +[dependencies] +ws_core = { path = "../ws_core" } +sha1="0.10.6" +base64 ="0.22.1" \ No newline at end of file diff --git a/src/handle_client.rs b/ws_server/src/main.rs similarity index 86% rename from src/handle_client.rs rename to ws_server/src/main.rs index 37c8b74..08c2f0f 100644 --- a/src/handle_client.rs +++ b/ws_server/src/main.rs @@ -1,10 +1,12 @@ use base64::{Engine as _, engine::general_purpose::STANDARD}; use sha1::{Digest, Sha1}; use std::io::{Read, Result, Write}; -use std::net::TcpStream; +use std::net::{TcpListener, TcpStream}; +use std::thread; + +use ws_core::read::read_header; +use ws_core::write::send_message; -use crate::read_header::read_header; -use crate::send_message::send_message; pub fn handle_client(mut stream: TcpStream) -> Result<()> { // these variable are here to read all the buffer sent from the client @@ -40,7 +42,7 @@ pub fn handle_client(mut stream: TcpStream) -> Result<()> { //get the key to upgrade the protocol for line in request.lines() { if line.starts_with("Sec-WebSocket-Key:") { - websocket_key = Some(line.split(":").nth(1).unwrap().trim().to_string()).unwrap(); + websocket_key = line.split(":").nth(1).unwrap().trim().to_string(); } } @@ -131,3 +133,24 @@ pub fn handle_client(mut stream: TcpStream) -> Result<()> { } Ok(()) } + +pub fn main() -> Result<()> { + let listener = TcpListener::bind("127.0.0.1:8080")?; + + for stream in listener.incoming() { + match stream { + Ok(stream) => { + println!("Connection Established"); + + thread::spawn(move || { + let _ = handle_client(stream); + }); + } + Err(e) => { + println!("Connection failed : {}", e); + } + } + } + + Ok(()) +} \ No newline at end of file