]> git.amazingca.dev Git - autumn/ws/commitdiff
Move to body parsing instead of headers for obvious reasons main
authorAmazingca <autumn@amazingca.dev>
Fri, 3 Jul 2026 04:16:24 +0000 (22:16 -0600)
committerAmazingca <autumn@amazingca.dev>
Fri, 3 Jul 2026 04:16:24 +0000 (22:16 -0600)
src/data.rs
src/main.rs
src/routes.rs
src/server.rs

index 5a34c3c581b31b8cac9471722fa72c5305617340..414003022292407290a714b7ae16ceb0a94c6dbc 100644 (file)
@@ -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<String>) -> 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,
         }
     }
 }
index 9dc8a30591d2af355d2646ffb3afbef88eeeea4a..c3ea372a00c73f9df047aeca044a4d36c2ea64e6 100644 (file)
@@ -53,10 +53,13 @@ fn args_handler(conf: &mut ServerConfig, args: Vec<String>) {
                     Ok(_) => hash,
                     Err(x) => {
                         println!("{}", x);
-                        exit(-1);
+                        "".to_string()
                     }
                 }
             };
+            if hash.chars().count() == 0 {
+                exit(-1);
+            }
             unsafe {
                 env::set_var("token", hash);
             }
index 0c2b295029988e4bba15f15b5bff3365494e2c01..472d0034dd8d3858888c319b8e4bc15a16499753 100644 (file)
@@ -39,14 +39,14 @@ pub fn pull(headers: &Vec<String>) -> (String, String) {
     }
 }
 
-pub fn push(headers: &Vec<String>) -> (String, String) {
+pub fn push(headers: &Vec<String>, 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"),
index d82e3d62f4e2e7d5d4ce29f490af23959a99665c..c1df07fb78befa43c7efda2418376c3a87bd580b 100644 (file)
@@ -24,29 +24,44 @@ pub mod server {
         false
     }
 
-    pub fn get_client_data(headers: &Vec<String>) -> Option<String> {
-        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<String> = headers_raw
+            .split("\r\n")
+            .map(|line| String::from(line))
+            .collect::<Vec<String>>();
+        println!("{headers:#?}");
+        for header in &headers {
+            if header.starts_with("Content-Length: ") {
+                size = header
+                    .replace("Content-Length: ", "")
+                    .trim()
+                    .parse::<usize>()
+                    .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<String>, request_line: &String) -> (String, String) {
+    fn router(headers: &Vec<String>, 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"),