🔨」 fix(Client): added empty constructor and parse function

This commit is contained in:
y-syo
2025-04-25 12:34:11 +02:00
parent c41545b230
commit a77f44579f
2 changed files with 47 additions and 7 deletions

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/14 14:14:39 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 { class Client {
public: public:
Client();
Client(int, sockaddr_in, config::Config *); Client(int, sockaddr_in, config::Config *);
void parse(int, sockaddr_in, config::Config *);
virtual ~Client(void); virtual ~Client(void);
void answer(void); void answer(void);

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */ /* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/17 11:12:41 by mmoussou #+# #+# */ /* Created: 2025/04/17 11:12:41 by mmoussou #+# #+# */
/* Updated: 2025/04/23 14:40:06 by mmoussou ### ########.fr */ /* Updated: 2025/04/25 12:33:38 by mmoussou ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,8 +15,11 @@
using namespace server; using namespace server;
Client::Client(void) {}
Client::Client(int fd, sockaddr_in socket, config::Config *conf) Client::Client(int fd, sockaddr_in socket, config::Config *conf)
: _fd(fd), _client_addr(socket) { : _fd(fd), _client_addr(socket)
{
std::string received_data; std::string received_data;
char buffer[BUFFER_SIZE]; char buffer[BUFFER_SIZE];
ssize_t bytes_received; ssize_t bytes_received;
@ -34,6 +37,33 @@ Client::Client(int fd, sockaddr_in socket, config::Config *conf)
this->_conf = conf->getServer(this->_request->getHeaders()["Host"]); this->_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) ) // if (received_data.length > (get max_body_size from Route corresponding) )
// throw error // throw error
} }
@ -42,20 +72,28 @@ void Client::_getRequest(std::string request_str) {
std::string method = request_str.substr( std::string method = request_str.substr(
0, request_str.substr(0, 4).find_last_not_of(" ") + 1); 0, request_str.substr(0, 4).find_last_not_of(" ") + 1);
if (method == "GET") { if (method == "GET")
{
_log->info("get request received"); _log->info("get request received");
this->_request = new http::Get(request_str); this->_request = new http::Get(request_str);
} else if (method == "DELETE") { }
else if (method == "DELETE")
{
_log->info("delete request received"); _log->info("delete request received");
this->_request = new http::Delete(request_str); this->_request = new http::Delete(request_str);
} else if (method == "POST") { }
else if (method == "POST")
{
_log->info("post request received"); _log->info("post request received");
this->_request = new http::Post(request_str); this->_request = new http::Post(request_str);
} else { }
else
{
_log->info("unsupported request received"); _log->info("unsupported request received");
this->_request = new http::Get(); this->_request = new http::Get();
this->_request->setMethod("501"); this->_request->setMethod("501");
} }
// set target to correct target with the conf
} }
void Client::answer(void) { void Client::answer(void) {