From 0cfad785268439a155206335b098a6258bbe8abb Mon Sep 17 00:00:00 2001 From: deadvey Date: Sat, 29 Nov 2025 11:46:45 +0000 Subject: [PATCH] Doesn't really work, but I'm starting to implement request headers --- src/files.rs | 2 +- src/main.rs | 23 ++++++++++++----------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/files.rs b/src/files.rs index c930261..f3126ec 100644 --- a/src/files.rs +++ b/src/files.rs @@ -17,7 +17,7 @@ use crate:: ROOT_DIR, }; -pub fn watch_files(file_list: &Vec) -> u8 +pub fn watch_files(file_list: &[PathBuf]) -> u8 { let (tx, rx) = mpsc::channel::>(); // Use recommended_watcher() to automatically select the best implementation diff --git a/src/main.rs b/src/main.rs index 5f2cdc4..37a0e56 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,7 +20,6 @@ lazy_static! { // TODO, make 'code' an enum // TODO, safely modify bytes in thread. // TODO, Makes paths more consistent. -// TODO, Avoid copying bytes fn main() -> io::Result<()> { #![warn(clippy::pedantic, clippy::perf)] @@ -32,11 +31,6 @@ fn main() -> io::Result<()> let listener = TcpListener::bind(BIND_ADDRESS).unwrap(); println!("Listening at {BIND_ADDRESS}"); -/* - let mut file_list: Vec = vec![ROOT_DIR.join(INDEX_FILE)]; - let (mut bytes, mut new_file_list) = files::read_file(INDEX_FILE, 0); - file_list.append(&mut new_file_list); -*/ // File watch thread thread::spawn( move || { @@ -44,7 +38,6 @@ fn main() -> io::Result<()> let mut file_list: Vec; (bytes, file_list) = files::read_file(INDEX_FILE, 0); file_list.push(ROOT_DIR.join(INDEX_FILE)); - println!("Tracking: {:?}", file_list); tx.send(bytes.clone()).unwrap(); loop @@ -56,7 +49,7 @@ fn main() -> io::Result<()> (bytes, file_list) = files::read_file(INDEX_FILE, 0); file_list.push(ROOT_DIR.join(INDEX_FILE)); - tx.send(bytes.clone()).unwrap(); + tx.send(bytes).unwrap(); } } } @@ -71,9 +64,17 @@ fn main() -> io::Result<()> Ok(()) } -fn handle_connection(mut stream: TcpStream, bytes: &Vec) +fn handle_connection(mut stream: TcpStream, bytes: &[u8]) { let buf_reader = BufReader::new(&stream); - println!("CONNECTION: {buf_reader:#?}"); - stream.write_all(bytes.as_slice()).unwrap(); + let notp_request: Vec<_> = buf_reader + .lines() + .map(|result| result.unwrap()) + .take_while(|line| !line.is_empty()) + .collect(); + println!("REQUEST: {:?}", notp_request); + if let Err(e) = stream.write_all(bytes) + { + eprintln!("Failed to send response: {}", e); + } }