From e20969c28df3fc62d16ae1d4694fb4baf5929f67 Mon Sep 17 00:00:00 2001 From: yggverse Date: Sun, 23 Feb 2025 05:52:10 +0200 Subject: [PATCH] add comments, update debug messages --- src/main.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 8a493db..7e64bf2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -56,17 +56,22 @@ fn handle( connection: Result, HandshakeError>, ) { use titanite::*; - //println!("[{}] [info] [{peer}] New connection", now()); + println!("[{}] [info] [{peer}] New peer connected..", now()); match connection { Ok(mut stream) => { + // server should work with large files without memory overload, + // because of that incoming data read partially, using chunks; + // collect header bytes first to route the request let mut header = Vec::with_capacity(HEADER_MAX_LEN); loop { let mut buffer = vec![0]; match stream.read(&mut buffer) { - Ok(0) => println!("[{}] [warning] [{peer}] Peer closed connection", now()), + Ok(0) => println!("[{}] [warning] [{peer}] Peer closed connection.", now()), Ok(_) => { header.push(buffer[0]); + // header bytes collected if header.len() > HEADER_MAX_LEN || buffer[0] == b'\n' { + // detect controller for the request by parse its header bytes return match Request::from_bytes(&header) { Ok(request) => match request { Request::Gemini(this) => { @@ -136,9 +141,10 @@ fn gemini( // println!("[{}] [info] [{peer}] Chunk sent: {l} ({read} total)", now()); // EOF if l == 0 { + println!("[{}] [info] [{peer}] Response: {read} bytes", now()); stream.flush().unwrap(); stream.shutdown().unwrap(); - println!("[{}] [info] [{peer}] Response: {read} bytes", now()); + println!("[{}] [info] [{peer}] Connection closed by server.", now()); break; } }, @@ -194,7 +200,7 @@ fn titan( let mut input = vec![0; argument.chunk]; match stream.read(&mut input) { Ok(0) => { - println!("[{}] [warning] [{peer}] Peer closed connection", now()); + println!("[{}] [warning] [{peer}] Peer closed connection.", now()); if let Err(e) = tmp.delete() { println!("[{}] [error] [{peer}] {e}", now()); }