From 4d832d5e2371ba8984e97f24e0fdf3b5cdc26afd Mon Sep 17 00:00:00 2001 From: Amazingca Date: Thu, 2 Jul 2026 22:16:24 -0600 Subject: [PATCH] Move to body parsing instead of headers for obvious reasons --- src/data.rs | 12 ++++-------- src/main.rs | 5 ++++- src/routes.rs | 4 ++-- src/server.rs | 51 +++++++++++++++++++++++++++++++++------------------ 4 files changed, 43 insertions(+), 29 deletions(-) diff --git a/src/data.rs b/src/data.rs index 5a34c3c..4140030 100644 --- a/src/data.rs +++ b/src/data.rs @@ -1,6 +1,5 @@ pub mod data { - use crate::server::server::get_client_data; use std::{ fs::{read_to_string, write}, path::Path, @@ -18,13 +17,10 @@ pub mod data { } } - pub fn push_data(headers: &Vec) -> bool { - match get_client_data(headers) { - Some(x) => match write("./data", x.as_str()) { - Ok(_) => true, - Err(_) => false, - }, - None => false, + pub fn push_data(body: String) -> bool { + match write("./data", body.as_str()) { + Ok(_) => true, + Err(_) => false, } } } diff --git a/src/main.rs b/src/main.rs index 9dc8a30..c3ea372 100644 --- a/src/main.rs +++ b/src/main.rs @@ -53,10 +53,13 @@ fn args_handler(conf: &mut ServerConfig, args: Vec) { Ok(_) => hash, Err(x) => { println!("{}", x); - exit(-1); + "".to_string() } } }; + if hash.chars().count() == 0 { + exit(-1); + } unsafe { env::set_var("token", hash); } diff --git a/src/routes.rs b/src/routes.rs index 0c2b295..472d003 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -39,14 +39,14 @@ pub fn pull(headers: &Vec) -> (String, String) { } } -pub fn push(headers: &Vec) -> (String, String) { +pub fn push(headers: &Vec, body: String) -> (String, String) { if !get_authorization(headers) { ( String::from("403 FORBIDDEN"), String::from("You are not authorized to view this endpoint"), ) } else { - if data::push_data(headers) { + if data::push_data(body) { ( String::from("200 OK"), String::from("Data successfully synced to server"), diff --git a/src/server.rs b/src/server.rs index d82e3d6..c1df07f 100644 --- a/src/server.rs +++ b/src/server.rs @@ -24,29 +24,44 @@ pub mod server { false } - pub fn get_client_data(headers: &Vec) -> Option { - for header in headers { - if header.starts_with("Data: ") { - return Some(header.replace("Data: ", "")); + pub fn handle_connection(mut stream: TcpStream) { + // Source - https://stackoverflow.com/a/72019105 + let mut reader = BufReader::new(&stream); + let mut headers_raw = String::new(); + loop { + let r = reader.read_line(&mut headers_raw).unwrap(); + if r == 2 { + //detect empty line + break; } } - None - } + let mut size = 0; + let headers: Vec = headers_raw + .split("\r\n") + .map(|line| String::from(line)) + .collect::>(); + println!("{headers:#?}"); + for header in &headers { + if header.starts_with("Content-Length: ") { + size = header + .replace("Content-Length: ", "") + .trim() + .parse::() + .unwrap(); + } + } - pub fn handle_connection(mut stream: TcpStream) { - let buf_reader = BufReader::new(&stream); - let request_data: Vec<_> = buf_reader - .lines() - .map(|result| result.unwrap()) - .take_while(|line| !line.is_empty()) - .collect(); + let mut buffer = vec![0; size]; //New Vector with size of Content + reader.read_exact(&mut buffer).unwrap(); //Get the Body Content. + + let body = String::from_utf8(buffer).unwrap(); - println!("{request_data:#?}"); + println!("{}", body); - let request_line = &request_data[0]; + let request_line = &headers[0]; - let (status, response) = router(&request_data, &request_line); + let (status, response) = router(&headers, &request_line, body); let status_line = format!("HTTP/1.1 {}", status); let length = response.len(); @@ -55,12 +70,12 @@ pub mod server { stream.write_all(response.as_bytes()).unwrap(); } - fn router(headers: &Vec, request_line: &String) -> (String, String) { + fn router(headers: &Vec, request_line: &String, body: String) -> (String, String) { match request_line.as_str() { "GET / HTTP/1.1" => routes::home(), "GET /auth HTTP/1.1" => routes::auth(headers), "GET /pull HTTP/1.1" => routes::pull(headers), - "POST /push HTTP/1.1" => routes::push(headers), + "POST /push HTTP/1.1" => routes::push(headers, body), _ => ( String::from("404 NOT FOUND"), String::from("The provided endpoint was not found"), -- 2.54.0