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) {