add comments, update debug messages

This commit is contained in:
yggverse 2025-02-23 05:52:10 +02:00
parent 8ee71e0166
commit e20969c28d

View file

@ -56,17 +56,22 @@ fn handle(
connection: Result<TlsStream<TcpStream>, HandshakeError<TcpStream>>, connection: Result<TlsStream<TcpStream>, HandshakeError<TcpStream>>,
) { ) {
use titanite::*; use titanite::*;
//println!("[{}] [info] [{peer}] New connection", now()); println!("[{}] [info] [{peer}] New peer connected..", now());
match connection { match connection {
Ok(mut stream) => { 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); let mut header = Vec::with_capacity(HEADER_MAX_LEN);
loop { loop {
let mut buffer = vec![0]; let mut buffer = vec![0];
match stream.read(&mut buffer) { match stream.read(&mut buffer) {
Ok(0) => println!("[{}] [warning] [{peer}] Peer closed connection", now()), Ok(0) => println!("[{}] [warning] [{peer}] Peer closed connection.", now()),
Ok(_) => { Ok(_) => {
header.push(buffer[0]); header.push(buffer[0]);
// header bytes collected
if header.len() > HEADER_MAX_LEN || buffer[0] == b'\n' { 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) { return match Request::from_bytes(&header) {
Ok(request) => match request { Ok(request) => match request {
Request::Gemini(this) => { Request::Gemini(this) => {
@ -136,9 +141,10 @@ fn gemini(
// println!("[{}] [info] [{peer}] Chunk sent: {l} ({read} total)", now()); // println!("[{}] [info] [{peer}] Chunk sent: {l} ({read} total)", now());
// EOF // EOF
if l == 0 { if l == 0 {
println!("[{}] [info] [{peer}] Response: {read} bytes", now());
stream.flush().unwrap(); stream.flush().unwrap();
stream.shutdown().unwrap(); stream.shutdown().unwrap();
println!("[{}] [info] [{peer}] Response: {read} bytes", now()); println!("[{}] [info] [{peer}] Connection closed by server.", now());
break; break;
} }
}, },
@ -194,7 +200,7 @@ fn titan(
let mut input = vec![0; argument.chunk]; let mut input = vec![0; argument.chunk];
match stream.read(&mut input) { match stream.read(&mut input) {
Ok(0) => { Ok(0) => {
println!("[{}] [warning] [{peer}] Peer closed connection", now()); println!("[{}] [warning] [{peer}] Peer closed connection.", now());
if let Err(e) = tmp.delete() { if let Err(e) = tmp.delete() {
println!("[{}] [error] [{peer}] {e}", now()); println!("[{}] [error] [{peer}] {e}", now());
} }