🔨」 fix: fixed issue with mainloop where it received empty request

This commit is contained in:
2025-05-28 11:06:19 +02:00
parent adde9e0362
commit 570ade97b4
11 changed files with 79 additions and 50 deletions

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/17 11:12:41 by mmoussou #+# #+# */
/* Updated: 2025/05/27 22:26:09 by adjoly ### ########.fr */
/* Updated: 2025/05/28 10:58:11 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -51,9 +51,13 @@ void Client::parse(void) {
_getRequest(received_data);
if (_request == not_nullptr)
return;
_route = _conf->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 {

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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();
}

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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(),