🏗️」 wip: change mainloop to work has expected

This commit is contained in:
2025-04-25 13:27:46 +02:00
parent d15e9061d3
commit 8d4ef4095a
7 changed files with 49 additions and 77 deletions

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/14 14:14:39 by adjoly #+# #+# */
/* Updated: 2025/04/25 12:43:54 by mmoussou ### ########.fr */
/* Updated: 2025/04/25 13:20:13 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -24,8 +24,8 @@ namespace server {
class Client {
public:
Client();
Client(struct pollfd, sockaddr_in, config::Config *);
void parse(struct pollfd, sockaddr_in, config::Config *);
Client(struct pollfd *, sockaddr_in, config::Config *);
void parse(void);
virtual ~Client(void);
void answer(void);
@ -33,11 +33,12 @@ class Client {
private:
void _getRequest(std::string);
struct pollfd _fd;
struct pollfd *_pfd;
struct sockaddr_in _client_addr;
http::IRequest *_request;
//http::Response *_response;
config::Server *_conf;
// http::Response *_response;
config::Server *_conf;
config::Config *_Gconf;
};
} // namespace server

View File

@ -6,26 +6,24 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/11 17:45:43 by adjoly #+# #+# */
/* Updated: 2025/04/22 11:51:27 by mmoussou ### ########.fr */
/* Updated: 2025/04/25 13:23:08 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#include <log.hpp>
#include <config/default.hpp>
#include <fcntl.h>
#include <log.hpp>
#include <netinet/in.h>
#include <stdexcept>
#include <sys/poll.h>
#include <vector>
namespace webserv {
namespace server {
struct client_data {
sockaddr_in sock_data;
pollfd poll_fd;
};
class Client;
class Server {
public:
@ -48,7 +46,7 @@ class Server {
*
* @param The fd of the client
*/
bool _handle_client(struct pollfd &, sockaddr_in *);
bool _handle_client(Client *);
/**
* @brief Can be used to fill the vector passed as parameters with all the
@ -85,8 +83,8 @@ class Server {
Logger *_log; // Pointer to the log class
std::vector<int> _fds_server; // The fds of the sockets
std::vector<struct pollfd> _client_fds; // A vector of all the poll fd
std::vector<sockaddr_in *>
_client_data; // vector of all the client sockaddr_in
std::vector<Client *> _client_data; // vector of all the client sockaddr_in
};
}; // namespace server
}; // namespace webserv

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/03 15:45:07 by mmoussou #+# #+# */
/* Updated: 2025/04/22 15:43:21 by adjoly ### ########.fr */
/* Updated: 2025/04/25 13:23:33 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -62,7 +62,7 @@ int main(int ac, char **av) {
return EXIT_FAILURE;
}
webserv::Server *serv = new webserv::Server(conf);
webserv::server::Server *serv = new webserv::server::Server(conf);
delete serv;
delete _log;

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/24 13:46:34 by adjoly #+# #+# */
/* Updated: 2025/04/24 14:38:20 by adjoly ### ########.fr */
/* Updated: 2025/04/25 13:22:00 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -20,7 +20,7 @@ Cgi::Cgi(http::IRequest *req, config::Server *conf) : _conf(conf), _request(req)
}
void Cgi::_initEnvp(void) {
_envp[] = "";
//_envp[] = "";
}
std::string Cgi::getEnv(std::string &key) {

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/17 11:12:41 by mmoussou #+# #+# */
/* Updated: 2025/04/25 12:44:12 by mmoussou ### ########.fr */
/* Updated: 2025/04/25 13:21:42 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,41 +17,18 @@ using namespace server;
Client::Client(void) {}
Client::Client(struct pollfd fd, sockaddr_in socket, config::Config *conf)
: _fd(fd), _client_addr(socket)
{
std::string received_data;
char buffer[BUFFER_SIZE];
ssize_t bytes_received;
do {
std::memset(buffer, 0, BUFFER_SIZE);
bytes_received = recv(this->_fd.fd, buffer, BUFFER_SIZE - 1, 0);
if (bytes_received == -1) {
_log->error("failed to receive request");
throw std::runtime_error("failed to receive request");
}
received_data += std::string(buffer, bytes_received);
} while (buffer[bytes_received]);
this->_getRequest(received_data);
this->_conf = conf->getServer(this->_request->getHeaders()["Host"]);
// if (received_data.length > (get max_body_size from Route corresponding) )
// throw error
Client::Client(struct pollfd *pfd, sockaddr_in socket, config::Config *conf)
: _pfd(pfd), _client_addr(socket), _Gconf(conf) {
log("", "Client", "constructor called");
}
void Client::parse(struct pollfd fd, sockaddr_in socket, config::Config *conf)
{
this->_fd = fd;
this->_client_addr = socket;
void Client::parse(void) {
std::string received_data;
char buffer[BUFFER_SIZE];
ssize_t bytes_received;
do {
std::memset(buffer, 0, BUFFER_SIZE);
bytes_received = recv(this->_fd.fd, buffer, BUFFER_SIZE - 1, 0);
bytes_received = recv(_pfd->fd, buffer, BUFFER_SIZE - 1, 0);
if (bytes_received == -1) {
_log->error("failed to receive request");
throw std::runtime_error("failed to receive request");
@ -61,8 +38,7 @@ void Client::parse(struct pollfd fd, sockaddr_in socket, config::Config *conf)
this->_getRequest(received_data);
this->_conf = conf->getServer(this->_request->getHeaders()["Host"]);
this->_conf = _Gconf->getServer(this->_request->getHeaders()["Host"]);
// if (received_data.length > (get max_body_size from Route corresponding) )
// throw error
@ -72,23 +48,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");
@ -97,7 +66,7 @@ void Client::_getRequest(std::string request_str) {
}
void Client::answer(void) {
(void) _client_addr;
(void)_client_addr;
std::string response;
if (this->_request->getMethod() == "GET" ||
@ -105,10 +74,13 @@ void Client::answer(void) {
this->_request->getMethod() == "POST")
response = this->_request->execute().str();
else
response = "HTTP/1.1 501 Not Implemented\r\nContent-Type: text/html\r\n\r\n<html><body><h1>501 Not Implemented</h1></body></html>";
send(this->_fd.fd, response.c_str(), response.length(), 0);
response = "HTTP/1.1 501 Not Implemented\r\nContent-Type: "
"text/html\r\n\r\n<html><body><h1>501 Not "
"Implemented</h1></body></html>";
send(_pfd->fd, response.c_str(), response.length(), 0);
}
Client::~Client(void) {
log("", "Client", "destructor called");
delete (http::Get *)(this->_request);
}

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/11 16:11:40 by adjoly #+# #+# */
/* Updated: 2025/04/23 16:22:22 by adjoly ### ########.fr */
/* Updated: 2025/04/25 13:09:33 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -28,7 +28,7 @@
#include <sys/socket.h>
#include <webserv.hpp>
using namespace webserv;
using namespace webserv::server;
extern int _sig;
@ -127,22 +127,22 @@ void Server::_run(void) {
pfd.events = POLLIN | POLLOUT;
pfd.revents = 0;
_client_fds.push_back(pfd);
struct sockaddr_in *new_client_sock = new sockaddr_in();
std::memmove(new_client_sock, &client_addr,
sizeof(struct sockaddr_in));
_client_data.push_back(new_client_sock);
auto itpfd = _client_fds.end() - 1;
Client *new_client = new Client(itpfd, client_addr, _conf);
_client_data.push_back(new_client);
}
for (size_t i = _fds_server.size(); i < _client_fds.size(); ++i) {
if (_client_fds[i].revents & POLLIN) {
if (_handle_client(_client_fds[i], _client_data[i])) {
if (_client_fds[i].revents & POLLERR) {
if (_handle_client(_client_fds[i])) {
close(_client_fds[i].fd);
_client_fds.erase(_client_fds.begin() + i);
delete _client_data[i];
_client_data.erase(_client_data.begin() + i);
i--;
}
}
} else if (_client_fds[i].revents & POLLIN) {}
else if()
}
}
}

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/17 11:58:42 by adjoly #+# #+# */
/* Updated: 2025/04/23 16:00:14 by adjoly ### ########.fr */
/* Updated: 2025/04/25 13:23:13 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -80,10 +80,11 @@ int Server::_createSocket(std::string host, int port) {
return (fd);
}
bool Server::_handle_client(struct pollfd &pollfd, sockaddr_in *sock_data) {
bool Server::_handle_client(Client *client) {
try {
Client client(pollfd.fd, *sock_data, _conf);
client.answer();
client->parse();
client->answer();
} catch (std::runtime_error &e) {
_log->error(e.what());
return false;