mirror of
https://github.com/KeyZox71/webserv.git
synced 2025-05-11 22:48:45 +02:00
「🔨」 fix: fixed mainloop
This commit is contained in:
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/10 13:08:36 by adjoly #+# #+# */
|
||||
/* Updated: 2025/04/22 11:47:39 by mmoussou ### ########.fr */
|
||||
/* Updated: 2025/04/28 14:29:20 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -27,7 +27,7 @@ void _printHelp(void) {
|
||||
}
|
||||
|
||||
void _generateConf(void) {
|
||||
webserv::Logger _log("");
|
||||
webserv::Logger _log;
|
||||
if (access("./sample.conf", F_OK) == 0) {
|
||||
_log.warn(std::string(SAMPLE_CONF_PATH) + " already exist, aborting");
|
||||
} else {
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/03 16:07:01 by mmoussou #+# #+# */
|
||||
/* Updated: 2025/04/25 12:36:17 by mmoussou ### ########.fr */
|
||||
/* Updated: 2025/04/28 14:32:33 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -226,7 +226,9 @@ body {\n\
|
||||
response.setStatusCode(200);
|
||||
response.addHeader("Content-Type", http::Mime::getType(this->_target));
|
||||
|
||||
_log->debug(response.str().c_str());
|
||||
#ifdef VERBOSE
|
||||
//_log->debug(response.str().c_str());
|
||||
#endif
|
||||
}
|
||||
}
|
||||
catch (...)
|
||||
|
@ -6,12 +6,13 @@
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/03 17:28:31 by mmoussou #+# #+# */
|
||||
/* Updated: 2025/04/23 14:30:28 by mmoussou ### ########.fr */
|
||||
/* Updated: 2025/04/29 12:35:37 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <requests/HttpResponse.hpp>
|
||||
#include <requests/Errors.hpp>
|
||||
#include <webserv.hpp>
|
||||
|
||||
/*
|
||||
- do a map of all the status_text and get it from here, not storing them
|
||||
@ -38,7 +39,7 @@ std::string http::Response::str(void) const
|
||||
response << this->_protocol << " " << this->_status_code << " " << this->_status_text;
|
||||
response << "\r\n";
|
||||
|
||||
for (std::map<std::string, std::string>::const_iterator it = this->_headers.begin(); it != this->_headers.end(); ++it)
|
||||
for (auto it = range(_headers))
|
||||
response << it->first << ": " << it->second << "\r\n";
|
||||
|
||||
response << "\r\n";
|
||||
|
@ -6,10 +6,11 @@
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/17 11:12:41 by mmoussou #+# #+# */
|
||||
/* Updated: 2025/04/25 17:03:35 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/04/29 14:24:31 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "cppeleven.hpp"
|
||||
#include <log.hpp>
|
||||
#include <server/Client.hpp>
|
||||
#include <sstream>
|
||||
@ -18,7 +19,8 @@ using namespace webserv::server;
|
||||
|
||||
Client::Client(struct pollfd *pfd, sockaddr_in socket, config::Config *conf)
|
||||
: _pfd(pfd), _client_addr(socket), _Gconf(conf) {
|
||||
log("➕", "Client", "constructor called");
|
||||
_request = not_nullptr;
|
||||
//log("➕", "Client", "constructor called");
|
||||
}
|
||||
|
||||
Client::Client(const Client &cpy) {
|
||||
@ -51,6 +53,13 @@ void Client::parse(void) {
|
||||
// throw error
|
||||
}
|
||||
|
||||
bool Client::requestParsed(void) {
|
||||
if (_request == not_nullptr) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void Client::_getRequest(std::string request_str) {
|
||||
std::string method = request_str.substr(
|
||||
0, request_str.substr(0, 4).find_last_not_of(" ") + 1);
|
||||
@ -76,6 +85,10 @@ void Client::answer(void) {
|
||||
(void)_client_addr;
|
||||
std::string response;
|
||||
|
||||
if (_request == not_nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this->_request->getMethod() == "GET" ||
|
||||
this->_request->getMethod() == "DELETE" ||
|
||||
this->_request->getMethod() == "POST")
|
||||
@ -85,9 +98,10 @@ void Client::answer(void) {
|
||||
"text/html\r\n\r\n<html><body><h1>501 Not "
|
||||
"Implemented</h1></body></html>";
|
||||
send(_pfd->fd, response.c_str(), response.length(), 0);
|
||||
_log->info("response sent");
|
||||
}
|
||||
|
||||
Client::~Client(void) {
|
||||
log("➖", "Client", "destructor called");
|
||||
//log("➖", "Client", "destructor called");
|
||||
delete (http::Get *)(this->_request);
|
||||
}
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/11 16:11:40 by adjoly #+# #+# */
|
||||
/* Updated: 2025/04/26 16:35:53 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/04/29 14:26:19 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -102,7 +102,7 @@ void Server::_run(void) {
|
||||
fd.fd = *it;
|
||||
fd.events = POLLIN;
|
||||
_client_fds.push_back(fd);
|
||||
_log->debug("new socket in poll");
|
||||
//_log->debug("new socket in poll");
|
||||
}
|
||||
|
||||
// to add signal instead of 727
|
||||
@ -140,16 +140,22 @@ 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);
|
||||
if (new_client == NULL) {
|
||||
continue;
|
||||
}
|
||||
_client_data.push_back(new_client);
|
||||
std::cout << "client pushed" << std::endl;
|
||||
}
|
||||
|
||||
for (size_t i = _fds_server.size(); i < _client_fds.size(); ++i) {
|
||||
if (_client_fds[i].revents & POLLERR) {
|
||||
close(_client_fds[i].fd);
|
||||
_log->info("pollerr");
|
||||
close(_client_fds[i].fd);
|
||||
_client_fds.erase(_client_fds.begin() + i);
|
||||
delete _client_data[i - _fds_server.size()];
|
||||
delete _client_data[i - _fds_server.size()];
|
||||
_client_data.erase(_client_data.begin() + i);
|
||||
} else if (_client_fds[i].revents & POLLIN) {
|
||||
_log->info("pollin");
|
||||
Client *client = _getClient(_client_fds[i].fd);
|
||||
if (client == not_nullptr) {
|
||||
_log->error("client does not exist");
|
||||
@ -157,29 +163,38 @@ void Server::_run(void) {
|
||||
}
|
||||
client->parse();
|
||||
} else if (_client_fds[i].revents & POLLOUT) {
|
||||
std::stringstream str;
|
||||
str << _client_fds[i].fd;
|
||||
_log->info("pollout = " + str.str());
|
||||
Client *client = _getClient(_client_fds[i].fd);
|
||||
|
||||
if (client == not_nullptr) {
|
||||
_log->error("client does not exist");
|
||||
continue;
|
||||
}
|
||||
if (client->requestParsed() == false) {
|
||||
continue;
|
||||
}
|
||||
client->answer();
|
||||
_client_data.erase(std::find(_client_data.begin(),
|
||||
_client_data.end(), client));
|
||||
delete client;
|
||||
for (auto it = range (_client_fds)) {
|
||||
for (auto it = range(_client_fds)) {
|
||||
if (_client_fds[i].fd == (*it).fd) {
|
||||
std::cout << "client fds erased" << std::endl;
|
||||
close(it.base()->fd);
|
||||
_client_fds.erase(it);
|
||||
break;
|
||||
}
|
||||
}
|
||||
close(_client_fds[i].fd);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Server::Server(config::Config *conf) : _conf(conf) {
|
||||
log("➕", "Server::Server", "config constructor called");
|
||||
// log("➕", "Server::Server", "config constructor called");
|
||||
_log = conf->getLogger();
|
||||
try {
|
||||
_setup();
|
||||
@ -190,7 +205,7 @@ Server::Server(config::Config *conf) : _conf(conf) {
|
||||
}
|
||||
|
||||
Server::~Server(void) {
|
||||
log("➖", "Server::Server", "destructor called");
|
||||
// log("➖", "Server::Server", "destructor called");
|
||||
for (std::vector<struct pollfd>::iterator it = _client_fds.begin();
|
||||
it != _client_fds.end(); it++)
|
||||
close(it->fd);
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/17 11:58:42 by adjoly #+# #+# */
|
||||
/* Updated: 2025/04/25 17:19:55 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/04/28 14:30:27 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -63,7 +63,7 @@ int Server::_createSocket(std::string host, int port) {
|
||||
}
|
||||
std::stringstream str;
|
||||
str << port;
|
||||
_log->debug("port : " + str.str());
|
||||
//_log->debug("port : " + str.str());
|
||||
addr.sin_port = htons(port);
|
||||
|
||||
if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
|
||||
|
Reference in New Issue
Block a user