🔨」 fix: fixed port issue and main loop issue for reasons only gods or kanel knows

This commit is contained in:
2025-05-01 12:54:09 +02:00
parent ea3bf0c4a3
commit e54c5e8323
9 changed files with 107 additions and 92 deletions

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/17 11:12:41 by mmoussou #+# #+# */
/* Updated: 2025/04/30 15:54:43 by mmoussou ### ########.fr */
/* Updated: 2025/05/01 12:53:33 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -18,8 +18,8 @@
using namespace webserv::server;
Client::Client(struct pollfd *pfd, sockaddr_in socket, config::Config *conf)
: _pfd(pfd), _client_addr(socket), _Gconf(conf) {
Client::Client(struct pollfd *pfd, config::Server *conf)
: _pfd(pfd), _conf(conf) {
_request = not_nullptr;
log("", "Client", "constructor called");
}
@ -47,18 +47,17 @@ void Client::parse(void) {
_getRequest(received_data);
this->_conf = this->_Gconf->getServer(this->_request->getHeaders()["Host"]);
this->_route = this->_conf->whatRoute(URL(this->_request->getTarget()));
std::cout << "_route is " << (_route ? "not null" : "NULL") << std::endl;
if(!this->_route || this->_route == not_nullptr)
{
_route = _conf->whatRoute(URL(this->_request->getTarget()));
/* std::cout << "_route is " << (_route ? "not null" : "NULL") << std::endl;
*/
if (!this->_route || this->_route == not_nullptr) {
this->_request->setMethod("404");
return ;
return;
}
if ((this->_request->getMethod() == "GET" && !_route->getMethods()[0])
|| (this->_request->getMethod() == "POST" && !_route->getMethods()[1])
|| (this->_request->getMethod() == "DELETE" && !_route->getMethods()[2]))
if ((this->_request->getMethod() == "GET" && !_route->getMethods()[0]) ||
(this->_request->getMethod() == "POST" && !_route->getMethods()[1]) ||
(this->_request->getMethod() == "DELETE" && !_route->getMethods()[2]))
this->_request->setMethod("405");
if (received_data.length() > (unsigned long)(_route->getMaxBody()))
@ -75,23 +74,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");
@ -100,7 +92,6 @@ void Client::_getRequest(std::string request_str) {
}
void Client::answer(void) {
(void)_client_addr;
std::string response;
if (_request == not_nullptr) {

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/11 16:11:40 by adjoly #+# #+# */
/* Updated: 2025/04/30 15:41:46 by adjoly ### ########.fr */
/* Updated: 2025/05/01 12:52:46 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -121,7 +121,6 @@ void Server::_run(void) {
size_t i = 0;
for (auto it = range(_fds_server), i++) {
if (_client_fds[i].revents & POLLIN) {
_log->info("polliiiiiiiiiiiiiiiinnnnnnnnnnnnnnnn");
struct sockaddr_in client_addr;
socklen_t addrlen = sizeof(client_addr);
int client_fd =
@ -137,8 +136,15 @@ void Server::_run(void) {
_log->error(str.str());
continue;
}
_log->info("connection accepted");
config::Server *conf_srv = _conf->getServer(i);
if (conf_srv == not_nullptr) {
_log->warn(
"where the f does he come from"); // does can't actually
// happen but just in
// case
close(client_fd);
continue;
}
struct pollfd pfd;
pfd.fd = client_fd;
@ -147,7 +153,7 @@ void Server::_run(void) {
_client_fds.push_back(pfd);
struct pollfd *ppfd =
_client_fds.data() + _client_fds.size() - 1;
Client *new_client = new Client(ppfd, client_addr, _conf);
Client *new_client = new Client(ppfd, conf_srv);
if (new_client == NULL) {
continue;
}
@ -195,7 +201,7 @@ void Server::_run(void) {
break;
}
}
i++;
i--;
}
}
}
@ -214,7 +220,7 @@ Server::Server(config::Config *conf) : _conf(conf) {
Server::~Server(void) {
log("", "Server::Server", "destructor called");
for (std::vector<struct pollfd>::iterator it = _client_fds.begin();
it != _client_fds.end(); it++)
for (auto it = range(_client_fds)) {
close(it->fd);
}
}