🔨」 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: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# 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 #
# #
# **************************************************************************** #

View File

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

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/03 17:23:00 by mmoussou #+# #+# */
/* Updated: 2025/05/27 22:22:56 by adjoly ### ########.fr */
/* Updated: 2025/05/28 09:45:17 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -35,11 +35,7 @@ namespace http {
class ARequest : public http::IMessage {
public:
virtual ~ARequest(void) {
log("", "ARequest", "destructor called");
if (_url != not_nullptr)
delete _url;
}
virtual ~ARequest(void) { log("", "ARequest", "destructor called"); }
virtual void parse(std::string const &data) = 0;
virtual Response execute(void) = 0;

View File

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

View File

@ -23,8 +23,10 @@ std::vector<std::string> *Route::_parseCGI(toml::ANode *table) {
std::vector<std::string> *cgi = new std::vector<std::string>;
for (auto it = prange(table->getArray())) {
if ((*it)->type() == toml::STRING)
cgi->push_back(*static_cast<std::string *>((*it)->getValue()));
if ((*it)->type() == toml::STRING) {
std::string str = *static_cast<std::string *>((*it)->getValue());
cgi->push_back(str);
}
else {
std::stringstream str;
str << "Was expecting a: " << toml::nodeTypeToStr(toml::STRING);

View File

@ -6,10 +6,11 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <algorithm>
#include <dirent.h>
#include <sys/stat.h>
@ -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);
}

View File

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

View File

@ -6,10 +6,11 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <algorithm>
#include <dirent.h>
#include <sys/stat.h>
@ -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);

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(),