🔨」 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

@ -37,4 +37,5 @@ port = 9090
methods = { "GET", "DELETE" } methods = { "GET", "DELETE" }
root = "/var/lib/docker/volumes/test_data/_data" root = "/var/lib/docker/volumes/test_data/_data"
dirlist = false dirlist = false
cgi.py = "/bin/python" cgi.py = "/bin/python

View File

@ -7,19 +7,30 @@
}; };
}; };
outputs = inputs@{ nixpkgs, ... }: outputs =
inputs@{ nixpkgs, ... }:
let let
supportedSystems = [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ]; supportedSystems = [
forEachSupportedSystem = f: nixpkgs.lib.genAttrs supportedSystems (system: f { "x86_64-linux"
"aarch64-linux"
"x86_64-darwin"
"aarch64-darwin"
];
forEachSupportedSystem =
f:
nixpkgs.lib.genAttrs supportedSystems (
system:
f {
pkgs = import nixpkgs { inherit system; }; pkgs = import nixpkgs { inherit system; };
}); }
in { );
devShells = forEachSupportedSystem ({ pkgs }: { in
default = pkgs.mkShell.override
{}
{ {
devShells = forEachSupportedSystem (
{ pkgs }:
{
default = pkgs.mkShell.override { } {
buildInputs = with pkgs; [ buildInputs = with pkgs; [
]; ];
hardeningDisable = [ "all" ]; hardeningDisable = [ "all" ];
packages = with pkgs; [ packages = with pkgs; [
@ -33,7 +44,7 @@
compiledb compiledb
]; ];
}; };
}); }
);
}; };
} }

View File

@ -6,13 +6,17 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/14 12:20:06 by adjoly #+# #+# */ /* Created: 2025/04/14 12:20:06 by adjoly #+# #+# */
/* Updated: 2025/04/22 15:25:39 by adjoly ### ########.fr */ /* Updated: 2025/05/01 12:48:12 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#pragma once #pragma once
#include "cppeleven.hpp"
#include <config/Server.hpp> #include <config/Server.hpp>
#include <netinet/in.h>
#include <stdexcept>
#include <sys/socket.h>
namespace webserv { namespace webserv {
namespace config { namespace config {
@ -25,7 +29,14 @@ class Config {
Logger *getLogger(void) { return _log; } Logger *getLogger(void) { return _log; }
std::vector<Server *> getServers(void) { return _servers; } std::vector<Server *> getServers(void) { return _servers; }
Server *getServer(const std::string &); Server *getServer(size_t i) {
try {
Server *srv = _servers.at(i);
return srv;
} catch (std::out_of_range &e) {
return not_nullptr;
}
}
private: private:
std::vector<Server *> _servers; std::vector<Server *> _servers;

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/19 14:11:28 by adjoly #+# #+# */ /* Created: 2025/03/19 14:11:28 by adjoly #+# #+# */
/* Updated: 2025/04/22 15:25:58 by adjoly ### ########.fr */ /* Updated: 2025/04/30 17:10:53 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -61,6 +61,7 @@ class Server {
// @brief Can be used to check if a servername is present in this config // @brief Can be used to check if a servername is present in this config
bool isServerName(const std::string &); bool isServerName(const std::string &);
// @brief Can be used to get the route correcponding
Route *whatRoute(const URL &); Route *whatRoute(const URL &);
protected: protected:

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/14 14:14:39 by adjoly #+# #+# */ /* Created: 2025/04/14 14:14:39 by adjoly #+# #+# */
/* Updated: 2025/04/30 14:51:39 by adjoly ### ########.fr */ /* Updated: 2025/05/01 12:51:51 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -24,7 +24,7 @@ namespace server {
class Client { class Client {
public: public:
Client(struct pollfd *, sockaddr_in, config::Config *); Client(struct pollfd *, config::Server *);
Client(const Client &cpy); Client(const Client &cpy);
virtual ~Client(void); virtual ~Client(void);
@ -54,12 +54,10 @@ class Client {
} }
struct pollfd *_pfd; struct pollfd *_pfd;
struct sockaddr_in _client_addr;
http::ARequest *_request; http::ARequest *_request;
// http::Response *_response; // http::Response *_response;
config::Server *_conf; config::Server *_conf;
config::Route *_route; config::Route *_route;
config::Config *_Gconf;
}; };
} // namespace server } // namespace server

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/14 12:53:54 by adjoly #+# #+# */ /* Created: 2025/04/14 12:53:54 by adjoly #+# #+# */
/* Updated: 2025/04/23 17:42:07 by adjoly ### ########.fr */ /* Updated: 2025/05/01 11:21:47 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,6 +15,7 @@
#include "cppeleven.hpp" #include "cppeleven.hpp"
#include "node/ANode.hpp" #include "node/ANode.hpp"
#include <config/default.hpp> #include <config/default.hpp>
#include <netinet/in.h>
#include <stdexcept> #include <stdexcept>
using namespace webserv::config; using namespace webserv::config;
@ -60,12 +61,3 @@ Config::~Config(void) {
delete *it; delete *it;
} }
} }
Server *Config::getServer(const std::string &server_name) {
for (auto it = range(_servers)) {
if ((*it)->isServerName(server_name)) {
return (*it);
}
}
return (not_nullptr);
}

View File

@ -6,13 +6,16 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/24 15:10:07 by adjoly #+# #+# */ /* Created: 2025/03/24 15:10:07 by adjoly #+# #+# */
/* Updated: 2025/04/30 15:37:18 by adjoly ### ########.fr */ /* Updated: 2025/05/01 10:08:15 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "config/URL.hpp" #include "config/URL.hpp"
#include "cppeleven.hpp" #include "cppeleven.hpp"
#include "log.hpp"
#include <config/default.hpp> #include <config/default.hpp>
#include <cstddef>
#include <utility>
#include <webserv.hpp> #include <webserv.hpp>
using namespace webserv::config; using namespace webserv::config;
@ -75,9 +78,10 @@ Server::Server(toml::ANode *node) : _table(node) {
std::map<std::string, toml::ANode *> *location_table = std::map<std::string, toml::ANode *> *location_table =
it->second->getTable(); it->second->getTable();
for (it = location_table->begin(); it != location_table->end(); it++) { for (it = location_table->begin(); it != location_table->end(); it++) {
if (_routes->find(URL(it->first)) != _routes->end()) if (_routes->find(URL(it->first)) != _routes->end()) {
continue; continue;
(*_routes)[URL(it->first)] = new Route(it->second); }
_routes->insert(std::make_pair(URL(it->first), new Route(it->second)));
} }
} }
// delete _table; // delete _table;
@ -129,8 +133,8 @@ Route *Server::whatRoute(const URL &url) {
} }
} }
std::map<URL, Route *>::iterator it = _routes->find(URL("/")); std::map<URL, Route *>::iterator it = _routes->find(URL("/"));
if (it == _routes->end()) { if (it != _routes->end()) {
return not_nullptr;
}
return it->second; return it->second;
} }
return not_nullptr;
}

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */ /* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/17 11:12:41 by mmoussou #+# #+# */ /* 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; using namespace webserv::server;
Client::Client(struct pollfd *pfd, sockaddr_in socket, config::Config *conf) Client::Client(struct pollfd *pfd, config::Server *conf)
: _pfd(pfd), _client_addr(socket), _Gconf(conf) { : _pfd(pfd), _conf(conf) {
_request = not_nullptr; _request = not_nullptr;
log("", "Client", "constructor called"); log("", "Client", "constructor called");
} }
@ -47,18 +47,17 @@ void Client::parse(void) {
_getRequest(received_data); _getRequest(received_data);
this->_conf = this->_Gconf->getServer(this->_request->getHeaders()["Host"]); _route = _conf->whatRoute(URL(this->_request->getTarget()));
this->_route = this->_conf->whatRoute(URL(this->_request->getTarget())); /* std::cout << "_route is " << (_route ? "not null" : "NULL") << std::endl;
std::cout << "_route is " << (_route ? "not null" : "NULL") << std::endl; */
if(!this->_route || this->_route == not_nullptr) if (!this->_route || this->_route == not_nullptr) {
{
this->_request->setMethod("404"); this->_request->setMethod("404");
return; return;
} }
if ((this->_request->getMethod() == "GET" && !_route->getMethods()[0]) if ((this->_request->getMethod() == "GET" && !_route->getMethods()[0]) ||
|| (this->_request->getMethod() == "POST" && !_route->getMethods()[1]) (this->_request->getMethod() == "POST" && !_route->getMethods()[1]) ||
|| (this->_request->getMethod() == "DELETE" && !_route->getMethods()[2])) (this->_request->getMethod() == "DELETE" && !_route->getMethods()[2]))
this->_request->setMethod("405"); this->_request->setMethod("405");
if (received_data.length() > (unsigned long)(_route->getMaxBody())) 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( std::string method = request_str.substr(
0, request_str.substr(0, 4).find_last_not_of(" ") + 1); 0, request_str.substr(0, 4).find_last_not_of(" ") + 1);
if (method == "GET") if (method == "GET") {
{
this->_request = new http::Get(request_str); this->_request = new http::Get(request_str);
_log->info("get request received"); _log->info("get request received");
} } else if (method == "DELETE") {
else if (method == "DELETE")
{
this->_request = new http::Delete(request_str); this->_request = new http::Delete(request_str);
_log->info("delete request received"); _log->info("delete request received");
} } else if (method == "POST") {
else if (method == "POST")
{
this->_request = new http::Post(request_str); this->_request = new http::Post(request_str);
_log->info("post request received"); _log->info("post request received");
} } else {
else
{
this->_request = new http::Get(); this->_request = new http::Get();
this->_request->setMethod("501"); this->_request->setMethod("501");
_log->info("unsupported request received"); _log->info("unsupported request received");
@ -100,7 +92,6 @@ void Client::_getRequest(std::string request_str) {
} }
void Client::answer(void) { void Client::answer(void) {
(void)_client_addr;
std::string response; std::string response;
if (_request == not_nullptr) { if (_request == not_nullptr) {

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/11 16:11:40 by adjoly #+# #+# */ /* 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; size_t i = 0;
for (auto it = range(_fds_server), i++) { for (auto it = range(_fds_server), i++) {
if (_client_fds[i].revents & POLLIN) { if (_client_fds[i].revents & POLLIN) {
_log->info("polliiiiiiiiiiiiiiiinnnnnnnnnnnnnnnn");
struct sockaddr_in client_addr; struct sockaddr_in client_addr;
socklen_t addrlen = sizeof(client_addr); socklen_t addrlen = sizeof(client_addr);
int client_fd = int client_fd =
@ -137,8 +136,15 @@ void Server::_run(void) {
_log->error(str.str()); _log->error(str.str());
continue; continue;
} }
config::Server *conf_srv = _conf->getServer(i);
_log->info("connection accepted"); 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; struct pollfd pfd;
pfd.fd = client_fd; pfd.fd = client_fd;
@ -147,7 +153,7 @@ void Server::_run(void) {
_client_fds.push_back(pfd); _client_fds.push_back(pfd);
struct pollfd *ppfd = struct pollfd *ppfd =
_client_fds.data() + _client_fds.size() - 1; _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) { if (new_client == NULL) {
continue; continue;
} }
@ -195,7 +201,7 @@ void Server::_run(void) {
break; break;
} }
} }
i++; i--;
} }
} }
} }
@ -214,7 +220,7 @@ Server::Server(config::Config *conf) : _conf(conf) {
Server::~Server(void) { Server::~Server(void) {
log("", "Server::Server", "destructor called"); log("", "Server::Server", "destructor called");
for (std::vector<struct pollfd>::iterator it = _client_fds.begin(); for (auto it = range(_client_fds)) {
it != _client_fds.end(); it++)
close(it->fd); close(it->fd);
} }
}