From db210a2c4e8bb59f9ac6d15b6a4f2665b59dcd54 Mon Sep 17 00:00:00 2001 From: dqnid Date: Fri, 17 Jan 2025 00:59:13 +0100 Subject: [PATCH] feat: dynamic buffer read with max size --- src/http/server.rs | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/src/http/server.rs b/src/http/server.rs index b56f436..386c912 100644 --- a/src/http/server.rs +++ b/src/http/server.rs @@ -33,9 +33,34 @@ impl HttpApp<'_> { } pub fn process_petition(&self, stream: &mut TcpStream) -> ProcessedResponse { - let mut buffer = [0; 1024]; // TODO: manage this size - let _amount = stream.read(&mut buffer); - let petition = String::from_utf8_lossy(&buffer[..]); + let mut petition = String::new(); + const BUFFER_SIZE: usize = 1024; + + loop { + let mut buffer = [0; BUFFER_SIZE]; + let amount = stream.read(&mut buffer); + match amount { + Ok(size_read) => { + if size_read < 1 { + break; + } + + let buffer_string = String::from_utf8_lossy(&buffer[..]); + petition.push_str(&buffer_string); + + if size_read < BUFFER_SIZE { + break; + } + } + Err(_e) => { + break; + } + } + if petition.bytes().len() > self.config.max_buffer_size_bytes { + break; + } + } + let petition = parse_request(&petition); match petition {