Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 2 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"]
30 changes: 0 additions & 30 deletions src/main.rs

This file was deleted.

6 changes: 6 additions & 0 deletions ws_client/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "ws_client"
version = "0.1.0"
edition = "2024"

[dependencies]
3 changes: 3 additions & 0 deletions ws_client/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}
6 changes: 6 additions & 0 deletions ws_core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "ws_core"
version = "0.1.0"
edition = "2024"

[dependencies]
2 changes: 2 additions & 0 deletions ws_core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod read;
pub mod write;
2 changes: 1 addition & 1 deletion src/read_header.rs → ws_core/src/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub fn read_header(stream: &mut TcpStream) -> Result<FrameHeader> {
} 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
Expand Down
File renamed without changes.
9 changes: 9 additions & 0 deletions ws_server/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
31 changes: 27 additions & 4 deletions src/handle_client.rs → ws_server/src/main.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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();
}
}

Expand Down Expand Up @@ -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(())
}