🏗️」 wip: loop nearly working

This commit is contained in:
2025-04-23 12:43:38 +02:00
parent cc2dc23963
commit 7cd221524f
4 changed files with 49 additions and 57 deletions

View File

@ -2,7 +2,7 @@ log_file = "test.log"
[server]
server_names = { "localhost", "2B5.local" }
host = "localhost"
host = "0.0.0.0"
port = 8080
[server.error_pages]

View File

@ -6,85 +6,74 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/17 11:12:41 by mmoussou #+# #+# */
/* Updated: 2025/04/22 12:01:16 by mmoussou ### ########.fr */
/* Updated: 2025/04/23 12:42:41 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include <server/Client.hpp>
#include <log.hpp>
#include <server/Client.hpp>
using namespace server;
Client::Client(int fd, sockaddr_in socket, config::Config *conf): _fd(fd), _client_addr(socket)
{
Client::Client(int 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
{
char buffer[BUFFER_SIZE];
ssize_t bytes_received;
do {
std::memset(buffer, 0, BUFFER_SIZE);
bytes_received = recv(fd, buffer, BUFFER_SIZE - 1, 0);
if (bytes_received == -1)
{
if (bytes_received == -1) {
_log->error("failed to receive request");
continue;
}
received_data += std::string(buffer, bytes_received);
}
while (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) )
// if (received_data.length > (get max_body_size from Route corresponding) )
// throw error
}
void Client::_getRequest(std::string request_str)
{
std::string method = request_str.substr(0, request_str.substr(0, 4).find_last_not_of(" ") + 1);
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") {
_log->info("get request received");
this->_request = new http::Get(request_str);
}
else if (method == "DELETE")
{
} else if (method == "DELETE") {
_log->info("delete request received");
this->_request = new http::Delete(request_str);
}
else if (method == "POST")
{
} else if (method == "POST") {
_log->info("post request received");
this->_request = new http::Post(request_str);
}
else
{
} else {
_log->info("unsupported request received");
this->_request = new http::Get();
this->_request->setMethod("501");
}
}
void Client::answer(void)
{
void Client::answer(void) {
std::string response;
(void) _client_addr;
if (this->_request->getMethod() == "GET" || this->_request->getMethod() == "DELETE" || this->_request->getMethod() == "POST")
(void)_client_addr;
if (this->_request->getMethod() == "GET" ||
this->_request->getMethod() == "DELETE" ||
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>";
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, response.c_str(), response.length(), 0);
}
Client::~Client(void)
{
Client::~Client(void) {
delete (http::Get *)(this->_request);
delete this->_response;
}

View File

@ -6,12 +6,13 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/11 16:11:40 by adjoly #+# #+# */
/* Updated: 2025/04/22 16:37:31 by adjoly ### ########.fr */
/* Updated: 2025/04/23 12:38:15 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include <cerrno>
#include <cmath>
#include <cstring>
#include <exception>
#include <fcntl.h>
#include <iterator>
@ -71,12 +72,8 @@ void Server::_setup(void) {
auto itH = hosts.begin();
for (auto it = range(ports), itH++) {
try {
int fd = _createSocket(*itH, *it);
_fds_server.push_back(fd);
} catch (std::exception &e) {
throw e;
}
int fd = _createSocket(*itH, *it);
_fds_server.push_back(fd);
}
}
@ -126,11 +123,18 @@ void Server::_run(void) {
pfd.fd = client_fd;
pfd.events = POLLIN | POLLOUT;
_client_fds.push_back(pfd);
_client_data.push_back(new sockaddr_in(client_addr));
struct sockaddr_in* new_client_sock = new sockaddr_in();
std::memmove(new_client_sock, &client_addr, sizeof(struct sockaddr_in));
std::cout << "tamere ==== " << new_client_sock << std::endl;
_client_data.push_back(new_client_sock);
}
for (size_t i = 0; i < _client_fds.size(); ++i) {
for (size_t i = _fds_server.size(); i < _client_fds.size(); ++i) {
std::cout << i << std::endl;
if (_client_fds[i].revents & POLLIN) {
std::cout << _client_fds.size() << " " << _client_data.size() << std::endl;
std::cout << "tamere ==== " << _client_data[i] << std::endl;
std::cout << i << std::endl;
if (!_handle_client(_client_fds[i], _client_data[i])) {
close(_client_fds[i].fd);
_client_fds.erase(_client_fds.begin() + i);
@ -149,7 +153,7 @@ Server::Server(config::Config *conf) : _conf(conf) {
try {
_setup();
_run();
} catch (std::runtime_error &e) {
} catch (std::exception &e) {
_log->error(e.what());
}
}

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/17 11:58:42 by adjoly #+# #+# */
/* Updated: 2025/04/22 16:32:45 by adjoly ### ########.fr */
/* Updated: 2025/04/23 12:33:47 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,6 +14,8 @@
#include <netinet/in.h>
#include <server/default.hpp>
#include <sstream>
#include <stdexcept>
#include <string>
#include <sys/socket.h>
using namespace webserv::server;
@ -84,17 +86,14 @@ int Server::_createSocket(std::string host, int port) {
}
bool Server::_handle_client(struct pollfd &pollfd, sockaddr_in *sock_data) {
Client *client;
try {
client = new Client(pollfd.fd, *sock_data, _conf);
client->answer();
} catch (std::exception &e) {
std::cout << "tamere ==== " << sock_data << std::endl;
Client client(pollfd.fd, *sock_data, _conf);
client.answer();
} catch (std::runtime_error &e) {
_log->error(e.what());
return false;
}
delete client;
return true;
}