diff --git a/includes/server/Client.hpp b/includes/server/Client.hpp index 9b01183..4155ff1 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/25 12:43:54 by mmoussou ### ########.fr */ +/* Updated: 2025/04/25 13:20:13 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,8 +24,8 @@ namespace server { class Client { public: Client(); - Client(struct pollfd, sockaddr_in, config::Config *); - void parse(struct pollfd, sockaddr_in, config::Config *); + Client(struct pollfd *, sockaddr_in, config::Config *); + void parse(void); virtual ~Client(void); void answer(void); @@ -33,11 +33,12 @@ class Client { private: void _getRequest(std::string); - struct pollfd _fd; + struct pollfd *_pfd; struct sockaddr_in _client_addr; http::IRequest *_request; - //http::Response *_response; - config::Server *_conf; + // http::Response *_response; + config::Server *_conf; + config::Config *_Gconf; }; } // namespace server diff --git a/includes/server/Server.hpp b/includes/server/Server.hpp index 2d13f61..a6ccb96 100644 --- a/includes/server/Server.hpp +++ b/includes/server/Server.hpp @@ -6,26 +6,24 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/11 17:45:43 by adjoly #+# #+# */ -/* Updated: 2025/04/22 11:51:27 by mmoussou ### ########.fr */ +/* Updated: 2025/04/25 13:23:08 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #pragma once -#include #include #include +#include #include #include #include #include namespace webserv { +namespace server { -struct client_data { - sockaddr_in sock_data; - pollfd poll_fd; -}; +class Client; class Server { public: @@ -48,7 +46,7 @@ class Server { * * @param The fd of the client */ - bool _handle_client(struct pollfd &, sockaddr_in *); + bool _handle_client(Client *); /** * @brief Can be used to fill the vector passed as parameters with all the @@ -85,8 +83,8 @@ class Server { Logger *_log; // Pointer to the log class std::vector _fds_server; // The fds of the sockets std::vector _client_fds; // A vector of all the poll fd - std::vector - _client_data; // vector of all the client sockaddr_in + std::vector _client_data; // vector of all the client sockaddr_in }; +}; // namespace server }; // namespace webserv diff --git a/src/main.cpp b/src/main.cpp index 37142c5..838f211 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,7 +6,7 @@ /* By: mmoussou +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/24 13:46:34 by adjoly #+# #+# */ -/* Updated: 2025/04/24 14:38:20 by adjoly ### ########.fr */ +/* Updated: 2025/04/25 13:22:00 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,7 +20,7 @@ Cgi::Cgi(http::IRequest *req, config::Server *conf) : _conf(conf), _request(req) } void Cgi::_initEnvp(void) { - _envp[] = ""; + //_envp[] = ""; } std::string Cgi::getEnv(std::string &key) { diff --git a/src/server/Client.cpp b/src/server/Client.cpp index 60290aa..1bbb2b5 100644 --- a/src/server/Client.cpp +++ b/src/server/Client.cpp @@ -6,7 +6,7 @@ /* By: mmoussou _fd.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 +Client::Client(struct pollfd *pfd, sockaddr_in socket, config::Config *conf) + : _pfd(pfd), _client_addr(socket), _Gconf(conf) { + log("➕", "Client", "constructor called"); } -void Client::parse(struct pollfd fd, sockaddr_in socket, config::Config *conf) -{ - this->_fd = fd; - this->_client_addr = socket; +void Client::parse(void) { std::string received_data; char buffer[BUFFER_SIZE]; ssize_t bytes_received; do { std::memset(buffer, 0, BUFFER_SIZE); - bytes_received = recv(this->_fd.fd, buffer, BUFFER_SIZE - 1, 0); + bytes_received = recv(_pfd->fd, buffer, BUFFER_SIZE - 1, 0); if (bytes_received == -1) { _log->error("failed to receive request"); throw std::runtime_error("failed to receive request"); @@ -61,8 +38,7 @@ void Client::parse(struct pollfd fd, sockaddr_in socket, config::Config *conf) this->_getRequest(received_data); - this->_conf = conf->getServer(this->_request->getHeaders()["Host"]); - + this->_conf = _Gconf->getServer(this->_request->getHeaders()["Host"]); // if (received_data.length > (get max_body_size from Route corresponding) ) // throw error @@ -72,23 +48,16 @@ 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") { this->_request = new http::Get(request_str); _log->info("get request received"); - } - else if (method == "DELETE") - { + } else if (method == "DELETE") { this->_request = new http::Delete(request_str); _log->info("delete request received"); - } - else if (method == "POST") - { + } else if (method == "POST") { this->_request = new http::Post(request_str); _log->info("post request received"); - } - else - { + } else { this->_request = new http::Get(); this->_request->setMethod("501"); _log->info("unsupported request received"); @@ -97,7 +66,7 @@ void Client::_getRequest(std::string request_str) { } void Client::answer(void) { - (void) _client_addr; + (void)_client_addr; std::string response; if (this->_request->getMethod() == "GET" || @@ -105,10 +74,13 @@ void Client::answer(void) { this->_request->getMethod() == "POST") response = this->_request->execute().str(); else - response = "HTTP/1.1 501 Not Implemented\r\nContent-Type: text/html\r\n\r\n

501 Not Implemented

"; - send(this->_fd.fd, response.c_str(), response.length(), 0); + response = "HTTP/1.1 501 Not Implemented\r\nContent-Type: " + "text/html\r\n\r\n

501 Not " + "Implemented

"; + send(_pfd->fd, response.c_str(), response.length(), 0); } Client::~Client(void) { + log("➖", "Client", "destructor called"); delete (http::Get *)(this->_request); } diff --git a/src/server/Server.cpp b/src/server/Server.cpp index ec8f0cd..0d0658a 100644 --- a/src/server/Server.cpp +++ b/src/server/Server.cpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/11 16:11:40 by adjoly #+# #+# */ -/* Updated: 2025/04/23 16:22:22 by adjoly ### ########.fr */ +/* Updated: 2025/04/25 13:09:33 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,7 +28,7 @@ #include #include -using namespace webserv; +using namespace webserv::server; extern int _sig; @@ -127,22 +127,22 @@ void Server::_run(void) { pfd.events = POLLIN | POLLOUT; pfd.revents = 0; _client_fds.push_back(pfd); - struct sockaddr_in *new_client_sock = new sockaddr_in(); - std::memmove(new_client_sock, &client_addr, - sizeof(struct sockaddr_in)); - _client_data.push_back(new_client_sock); + auto itpfd = _client_fds.end() - 1; + Client *new_client = new Client(itpfd, client_addr, _conf); + _client_data.push_back(new_client); } for (size_t i = _fds_server.size(); i < _client_fds.size(); ++i) { - if (_client_fds[i].revents & POLLIN) { - if (_handle_client(_client_fds[i], _client_data[i])) { + if (_client_fds[i].revents & POLLERR) { + if (_handle_client(_client_fds[i])) { close(_client_fds[i].fd); _client_fds.erase(_client_fds.begin() + i); delete _client_data[i]; _client_data.erase(_client_data.begin() + i); i--; } - } + } else if (_client_fds[i].revents & POLLIN) {} + else if() } } } diff --git a/src/server/ServerUtils.cpp b/src/server/ServerUtils.cpp index e2becb6..28e6849 100644 --- a/src/server/ServerUtils.cpp +++ b/src/server/ServerUtils.cpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/17 11:58:42 by adjoly #+# #+# */ -/* Updated: 2025/04/23 16:00:14 by adjoly ### ########.fr */ +/* Updated: 2025/04/25 13:23:13 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -80,10 +80,11 @@ int Server::_createSocket(std::string host, int port) { return (fd); } -bool Server::_handle_client(struct pollfd &pollfd, sockaddr_in *sock_data) { +bool Server::_handle_client(Client *client) { try { - Client client(pollfd.fd, *sock_data, _conf); - client.answer(); + client->parse(); + client->answer(); + } catch (std::runtime_error &e) { _log->error(e.what()); return false;