diff --git a/Makefile b/Makefile index a0d5c6c..6e32193 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: adjoly +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2024/10/25 16:09:27 by adjoly #+# #+# # -# Updated: 2025/04/28 14:57:54 by adjoly ### ########.fr # +# Updated: 2025/05/28 09:42:17 by adjoly ### ########.fr # # # # **************************************************************************** # diff --git a/includes/config/Route.hpp b/includes/config/Route.hpp index 880e011..f28560b 100644 --- a/includes/config/Route.hpp +++ b/includes/config/Route.hpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/19 14:59:41 by adjoly #+# #+# */ -/* Updated: 2025/05/27 20:05:20 by adjoly ### ########.fr */ +/* Updated: 2025/05/28 09:52:21 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/includes/requests/ARequest.hpp b/includes/requests/ARequest.hpp index 5221459..e05f7be 100644 --- a/includes/requests/ARequest.hpp +++ b/includes/requests/ARequest.hpp @@ -6,7 +6,7 @@ /* By: mmoussou +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/30 09:30:15 by adjoly #+# #+# */ -/* Updated: 2025/05/27 22:23:15 by adjoly ### ########.fr */ +/* Updated: 2025/05/28 09:55:33 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,6 +24,7 @@ class Get : public ARequest { public: Get(void) {} Get(std::string &data, config::Server *srv); + ~Get(void); void parse(std::string const &data); @@ -35,11 +36,9 @@ class Get : public ARequest { server::Cgi *_cgi; }; -// TODO: pass _srv to other class Post : public ARequest { public: - Post(void) {} - Post(std::string &data); + Post(std::string &data, config::Server *srv); void parse(std::string const &data); @@ -49,8 +48,10 @@ class Post : public ARequest { Response execute(void); - // private: - // server::Cgi *_cgi; + server::Cgi *getCgi() const { return _cgi; } + + private: + server::Cgi *_cgi; }; class Delete : public ARequest { diff --git a/src/config/Route.cpp b/src/config/Route.cpp index fc05fb5..f7de719 100644 --- a/src/config/Route.cpp +++ b/src/config/Route.cpp @@ -23,8 +23,10 @@ std::vector *Route::_parseCGI(toml::ANode *table) { std::vector *cgi = new std::vector; for (auto it = prange(table->getArray())) { - if ((*it)->type() == toml::STRING) - cgi->push_back(*static_cast((*it)->getValue())); + if ((*it)->type() == toml::STRING) { + std::string str = *static_cast((*it)->getValue()); + cgi->push_back(str); + } else { std::stringstream str; str << "Was expecting a: " << toml::nodeTypeToStr(toml::STRING); diff --git a/src/requests_handling/requestImplementation/Delete.cpp b/src/requests_handling/requestImplementation/Delete.cpp index c0e6280..2e25981 100644 --- a/src/requests_handling/requestImplementation/Delete.cpp +++ b/src/requests_handling/requestImplementation/Delete.cpp @@ -6,10 +6,11 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/30 09:42:18 by adjoly #+# #+# */ -/* Updated: 2025/05/27 22:23:22 by adjoly ### ########.fr */ +/* Updated: 2025/05/28 09:55:54 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ +#include "cppeleven.hpp" #include #include #include @@ -19,7 +20,11 @@ using namespace webserv::http; -Delete::Delete(std::string &data) { this->parse(data); } +Delete::Delete(std::string &data) { + _url = not_nullptr; + _srv = not_nullptr; + this->parse(data); +} void Delete::parse(std::string const &data) { std::istringstream stream(data); @@ -91,4 +96,3 @@ Response Delete::execute(void) { return (response); } - diff --git a/src/requests_handling/requestImplementation/Get.cpp b/src/requests_handling/requestImplementation/Get.cpp index 6aa3467..647d874 100644 --- a/src/requests_handling/requestImplementation/Get.cpp +++ b/src/requests_handling/requestImplementation/Get.cpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/30 09:40:16 by adjoly #+# #+# */ -/* Updated: 2025/05/27 22:32:40 by adjoly ### ########.fr */ +/* Updated: 2025/05/28 10:00:45 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -34,6 +34,11 @@ Get::Get(std::string &data, config::Server *srv) { this->parse(data); } +Get::~Get(void) { + // if (_url != not_nullptr) + // delete _url; +} + void Get::parse(std::string const &data) { std::istringstream stream(data); std::string line; @@ -276,5 +281,6 @@ body {\n\ http::Errors::getResponseBody(response.getStatusCode())); } + delete _url; return (response); } diff --git a/src/requests_handling/requestImplementation/Post.cpp b/src/requests_handling/requestImplementation/Post.cpp index 8da7358..0068b32 100644 --- a/src/requests_handling/requestImplementation/Post.cpp +++ b/src/requests_handling/requestImplementation/Post.cpp @@ -6,10 +6,11 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/30 09:50:20 by adjoly #+# #+# */ -/* Updated: 2025/05/27 22:23:36 by adjoly ### ########.fr */ +/* Updated: 2025/05/28 09:55:37 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ +#include "cppeleven.hpp" #include #include #include @@ -21,7 +22,12 @@ using namespace webserv::http; -Post::Post(std::string &data) { this->parse(data); } +Post::Post(std::string &data, config::Server *srv) { + _url = not_nullptr; + _srv = srv; + _cgi = not_nullptr; + this->parse(data); +} void Post::parse(std::string const &data) { std::istringstream stream(data); diff --git a/src/server/Client.cpp b/src/server/Client.cpp index 3ba9fa5..eb3a859 100644 --- a/src/server/Client.cpp +++ b/src/server/Client.cpp @@ -6,7 +6,7 @@ /* By: mmoussou whatRoute(URL(this->_request->getTarget())); - if (_conf->getServerNames() != not_nullptr) { + if (_request->getMethod() != "501" && + _conf->getServerNames() != not_nullptr) { std::string host = _request->getHeader("Host"); bool ret = _conf->isServerName(host.substr(0, host.find(':'))); if (ret == false) { @@ -61,38 +65,40 @@ void Client::parse(void) { } } - if (!this->_route || this->_route == not_nullptr) { - this->_request->setMethod("404"); + if (!_route || _route == not_nullptr) { + _request->setMethod("404"); return; } if (_route->getRedirect() == true) { - } else if ((this->_request->getMethod() == "GET" && - !_route->getMethods()[0]) || - (this->_request->getMethod() == "POST" && - !_route->getMethods()[1]) || - (this->_request->getMethod() == "DELETE" && - !_route->getMethods()[2])) + } else if ((_request->getMethod() == "GET" && !_route->getMethods()[0]) || + (_request->getMethod() == "POST" && !_route->getMethods()[1]) || + (_request->getMethod() == "DELETE" && !_route->getMethods()[2])) this->_request->setMethod("405"); if (received_data.length() > (unsigned long)(_route->getMaxBody())) - this->_request->setMethod("413"); + _request->setMethod("413"); } bool Client::requestParsed(void) { - if (_request->getCgi() != not_nullptr && !_request->getCgi()->isProcessed()) - return false; if (_request == not_nullptr) return false; + if (_request->getCgi() != not_nullptr) + if (!_request->getCgi()->isProcessed()) + return false; return true; } void Client::_getRequest(std::string request_str) { + if (request_str == "") { + _response_done = true; + return; + } std::string method = request_str.substr( 0, request_str.substr(0, 4).find_last_not_of(" ") + 1); if (method == "GET") { - this->_request = new http::Get(request_str, _conf); + _request = new http::Get(request_str, _conf); std::stringstream str; str << "get request received on port : "; str << _conf->getPort(); @@ -100,14 +106,14 @@ void Client::_getRequest(std::string request_str) { str << _request->getTarget(); _log->info(str.str()); } else if (method == "DELETE") { - this->_request = new http::Delete(request_str); + _request = new http::Delete(request_str); _log->info("delete request received"); } else if (method == "POST") { - this->_request = new http::Post(request_str); + _request = new http::Post(request_str, _conf); _log->info("post request received"); } else { - this->_request = new http::Get(); - this->_request->setMethod("501"); + _request = new http::Get(); + _request->setMethod("501"); _log->info("unsupported request received"); } // set target to correct target with the conf @@ -160,18 +166,12 @@ void Client::answer(void) { str << _response.getStatusCode(); _log->info(str.str()); } - - /*std::stringstream str; - str << "response sent, for page : "; - str << _request->getTarget(); - str << " with response code : "; - str << _response.getStatusCode(); - _log->info(str.str());*/ } Client::~Client(void) { log("➖", "Client", "destructor called"); - delete (http::Get *)(this->_request); + if (_request != not_nullptr) + delete _request; } bool Client::isReadyToClose() const { diff --git a/src/server/Server.cpp b/src/server/Server.cpp index 35827cd..08dc16a 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/05/27 21:19:49 by adjoly ### ########.fr */ +/* Updated: 2025/05/28 10:59:27 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -136,6 +136,8 @@ void Server::_run(void) { case RES: _handle_resource(i); break; + default: + break; } } } @@ -153,5 +155,8 @@ Server::Server() { Server::~Server(void) { log("➖", "Server::Server", "destructor called"); + for (auto it = range(_client_data)) { + delete *it; + } PfdManager::clear(); } diff --git a/src/server/ServerHandle.cpp b/src/server/ServerHandle.cpp index 1f327a0..6373c4c 100644 --- a/src/server/ServerHandle.cpp +++ b/src/server/ServerHandle.cpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/05/27 18:22:48 by adjoly #+# #+# */ -/* Updated: 2025/05/27 22:24:35 by adjoly ### ########.fr */ +/* Updated: 2025/05/28 10:49:47 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -75,6 +75,15 @@ void Server::_handle_client(size_t *i) { } try { client->parse(); + if (client->isReadyToClose()) { + _client_data.erase(std::find(_client_data.begin(), + _client_data.end(), client)); + delete client; + close(PfdManager::at(*i).fd); + PfdManager::remove(PfdManager::at(*i).fd); + _log->debug("client removed"); + i--; + } } catch (std::exception &e) { _log->error(e.what()); _client_data.erase(std::find(_client_data.begin(),