From a77f44579f83457850d3729e8b579312d473c2b5 Mon Sep 17 00:00:00 2001 From: y-syo Date: Fri, 25 Apr 2025 12:34:11 +0200 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=F0=9F=94=A8=E3=80=8D=20fix(Client):?= =?UTF-8?q?=20added=20empty=20constructor=20and=20parse=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/server/Client.hpp | 4 ++- src/server/Client.cpp | 50 +++++++++++++++++++++++++++++++++----- 2 files changed, 47 insertions(+), 7 deletions(-) diff --git a/includes/server/Client.hpp b/includes/server/Client.hpp index 4d1ca6f..4463e12 100644 --- a/includes/server/Client.hpp +++ b/includes/server/Client.hpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/14 14:14:39 by adjoly #+# #+# */ -/* Updated: 2025/04/23 14:39:16 by mmoussou ### ########.fr */ +/* Updated: 2025/04/25 12:32:29 by mmoussou ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,7 +23,9 @@ namespace server { class Client { public: + Client(); Client(int, sockaddr_in, config::Config *); + void parse(int, sockaddr_in, config::Config *); virtual ~Client(void); void answer(void); diff --git a/src/server/Client.cpp b/src/server/Client.cpp index cf1b71b..c6eb21d 100644 --- a/src/server/Client.cpp +++ b/src/server/Client.cpp @@ -6,7 +6,7 @@ /* By: mmoussou _conf = conf->getServer(this->_request->getHeaders()["Host"]); + + // if (received_data.length > (get max_body_size from Route corresponding) ) + // throw error +} + +void Client::parse(int fd, sockaddr_in socket, config::Config *conf) +{ + this->_fd = fd; + this->_client_addr = socket; + std::string received_data; + char buffer[BUFFER_SIZE]; + ssize_t bytes_received; + do { + std::memset(buffer, 0, BUFFER_SIZE); + bytes_received = recv(fd, buffer, BUFFER_SIZE - 1, 0); + if (bytes_received == -1) { + _log->error("failed to receive request"); + throw std::runtime_error("failed to receive request"); + } + received_data += std::string(buffer, bytes_received); + } while (buffer[bytes_received]); + + this->_getRequest(received_data); + + this->_conf = conf->getServer(this->_request->getHeaders()["Host"]); + + // if (received_data.length > (get max_body_size from Route corresponding) ) // throw error } @@ -42,20 +72,28 @@ void Client::_getRequest(std::string request_str) { std::string method = request_str.substr( 0, request_str.substr(0, 4).find_last_not_of(" ") + 1); - if (method == "GET") { + if (method == "GET") + { _log->info("get request received"); this->_request = new http::Get(request_str); - } else if (method == "DELETE") { + } + else if (method == "DELETE") + { _log->info("delete request received"); this->_request = new http::Delete(request_str); - } else if (method == "POST") { + } + else if (method == "POST") + { _log->info("post request received"); this->_request = new http::Post(request_str); - } else { + } + else + { _log->info("unsupported request received"); this->_request = new http::Get(); this->_request->setMethod("501"); } + // set target to correct target with the conf } void Client::answer(void) {