🔨」 fix: fixed error with config and handled properly exception

This commit is contained in:
2025-05-01 16:32:44 +02:00
parent 040043694e
commit 9a28e15af0
8 changed files with 105 additions and 32 deletions

View File

@ -25,8 +25,7 @@ cgi.go = "/bin/go"
[server.location./redir] [server.location./redir]
redirect = "https://kanel.ovh" redirect = "https://kanel.ovh"
[serverr] [default]
server_names = { "ptnnnn.local"}
host = "127.0.0.1" host = "127.0.0.1"
port = 9090 port = 9090
@ -37,5 +36,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

@ -6,7 +6,7 @@
/* 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/05/01 12:48:12 by adjoly ### ########.fr */ /* Updated: 2025/05/01 15:29:53 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -28,18 +28,21 @@ class Config {
Logger *getLogger(void) { return _log; } Logger *getLogger(void) { return _log; }
Server *getDefaultServer(void) const { return _default; }
std::vector<Server *> getServers(void) { return _servers; } std::vector<Server *> getServers(void) { return _servers; }
Server *getServer(size_t i) { Server *getServer(size_t i) {
try { try {
Server *srv = _servers.at(i); Server *srv = _servers.at(i);
return srv; return srv;
} catch (std::out_of_range &e) { } catch (std::out_of_range &e) {
return not_nullptr; return not_nullptr;
} }
} }
private: private:
std::vector<Server *> _servers; std::vector<Server *> _servers;
Server *_default;
}; };
}; // namespace config }; // namespace config

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/30 17:10:53 by adjoly ### ########.fr */ /* Updated: 2025/05/01 15:32:31 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -25,6 +25,7 @@ namespace config {
class Server { class Server {
public: public:
Server(toml::ANode *); Server(toml::ANode *);
Server(toml::ANode *, void *);
~Server(); ~Server();
/** /**

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/05/01 13:22:48 by adjoly ### ########.fr */ /* Updated: 2025/05/01 15:25:45 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -45,7 +45,7 @@ class Client {
void _getRequest(std::string); void _getRequest(std::string);
std::string _sanitizeStr(std::string &str) { std::string _sanitizeStr(std::string &str) {
std::string newStr = str; std::string newStr = str;
if (str[str.size() - 1] == '\r') { if (str[str.size() - 1] == '\r') {
newStr.erase(str.size() - 1); newStr.erase(str.size() - 1);
@ -53,9 +53,9 @@ class Client {
return newStr; return newStr;
} }
struct pollfd *_pfd; struct pollfd *_pfd;
http::ARequest *_request; http::ARequest *_request;
http::Response _response; http::Response _response;
config::Server *_conf; config::Server *_conf;
config::Route *_route; config::Route *_route;
}; };

View File

@ -6,10 +6,11 @@
/* 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/05/01 11:21:47 by adjoly ### ########.fr */ /* Updated: 2025/05/01 16:31:45 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "log.hpp"
#include "node/default.hpp" #include "node/default.hpp"
#include "webserv.hpp" #include "webserv.hpp"
#include "cppeleven.hpp" #include "cppeleven.hpp"
@ -44,11 +45,24 @@ Config::Config(std::string &filename) {
if (it->second->type() == toml::TABLE) { if (it->second->type() == toml::TABLE) {
_log->info("taking server from table : " + it->first); _log->info("taking server from table : " + it->first);
try { try {
Server *srv = new Server(it->second); if (it->first == "default") {
_servers.push_back(srv); _default = new Server(it->second, not_nullptr);
} else {
Server *srv = new Server(it->second);
_servers.push_back(srv);
}
} catch (std::runtime_error &e) { } catch (std::runtime_error &e) {
_log->error(e.what()); _log->error(e.what());
break; if (!_servers.empty()) {
for (auto it = range(_servers))
delete (*it);
}
if (_default != not_nullptr) {
delete _default;
}
delete table;
delete file;
throw e;
} }
} }
} }

View File

@ -6,7 +6,7 @@
/* 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/05/01 10:08:15 by adjoly ### ########.fr */ /* Updated: 2025/05/01 16:30:28 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -20,7 +20,7 @@
using namespace webserv::config; using namespace webserv::config;
Server::Server(toml::ANode *node) : _table(node) { Server::Server(toml::ANode *node) : _routes(not_nullptr), _server_names(not_nullptr), _table(node) {
bool found; bool found;
if (_table == not_nullptr) if (_table == not_nullptr)
@ -31,7 +31,7 @@ Server::Server(toml::ANode *node) : _table(node) {
if (host != not_nullptr) { if (host != not_nullptr) {
_host = *static_cast<std::string *>(host); _host = *static_cast<std::string *>(host);
} else { } else {
delete _table; //delete _table;
throw std::runtime_error( throw std::runtime_error(
"no host specified - please specify one in server.host"); "no host specified - please specify one in server.host");
} }
@ -88,10 +88,11 @@ Server::Server(toml::ANode *node) : _table(node) {
} }
Server::~Server(void) { Server::~Server(void) {
for (auto it = prange(_routes)) { if (_routes != not_nullptr && !_routes->empty()) {
delete it->second; for (auto it = prange(_routes))
delete it->second;
delete _routes;
} }
delete _routes;
delete _err_pages; delete _err_pages;
if (_server_names != not_nullptr) if (_server_names != not_nullptr)
delete _server_names; delete _server_names;
@ -138,3 +139,53 @@ Route *Server::whatRoute(const URL &url) {
} }
return not_nullptr; return not_nullptr;
} }
Server::Server(toml::ANode *node, void *) : _routes(not_nullptr), _server_names(not_nullptr), _table(node) {
bool found;
if (_table == not_nullptr)
return;
// host parsing
void *host = accessValue("host", toml::STRING, _table, _log);
if (host != not_nullptr) {
_host = *static_cast<std::string *>(host);
} else {
delete _table;
throw std::runtime_error(
"no host specified - please specify one");
}
// port parsing
void *port = accessValue("port", toml::INT, _table, _log);
if (port != not_nullptr) {
_port = *static_cast<unsigned short *>(port);
} else {
delete _table;
throw std::runtime_error(
"no port specified - please specify one");
}
// error_pages parsing
std::map<std::string, toml::ANode *> *map =
static_cast<std::map<std::string, toml::ANode *> *>(
accessValue("error_pages", toml::TABLE, _table, _log));
if (map != not_nullptr) {
_err_pages = _parseErrPages(map);
} else
_err_pages = not_nullptr;
// location parsing
auto it = _table->accessIt("location", toml::TABLE, found);
if (found == true && it != _table->getTable()->end()) {
_routes = new std::map<URL, Route *>;
std::map<std::string, toml::ANode *> *location_table =
it->second->getTable();
for (it = location_table->begin(); it != location_table->end(); it++) {
if (_routes->find(URL(it->first)) != _routes->end()) {
continue;
}
_routes->insert(std::make_pair(URL(it->first), new Route(it->second)));
}
}
// delete _table;
}

View File

@ -6,10 +6,11 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */ /* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/03 15:45:07 by mmoussou #+# #+# */ /* Created: 2025/02/03 15:45:07 by mmoussou #+# #+# */
/* Updated: 2025/04/25 13:23:33 by adjoly ### ########.fr */ /* Updated: 2025/05/01 16:32:06 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "cppeleven.hpp"
#include <config/default.hpp> #include <config/default.hpp>
#include <csignal> #include <csignal>
#include <server/default.hpp> #include <server/default.hpp>
@ -39,6 +40,7 @@ void ft_sig(int sig) {
} }
int main(int ac, char **av) { int main(int ac, char **av) {
_log = not_nullptr;
if (help(ac, av)) { if (help(ac, av)) {
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
@ -53,8 +55,11 @@ int main(int ac, char **av) {
try { try {
std::string str = av[1]; std::string str = av[1];
conf = new config::Config(str); conf = new config::Config(str);
} catch (std::exception &e) { } catch (std::exception &) {
std::cout << e.what() << std::endl; //std::cout << e.what() << std::endl;
//delete conf;
if (_log != not_nullptr)
delete _log;
return 1; return 1;
} }
if (signal(SIGINT, &ft_sig) == SIG_ERR) { if (signal(SIGINT, &ft_sig) == SIG_ERR) {

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/05/01 13:22:55 by adjoly ### ########.fr */ /* Updated: 2025/05/01 15:27:00 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -120,7 +120,7 @@ void Client::answer(void) {
std::stringstream str; std::stringstream str;
str << "response sent, for page : "; str << "response sent, for page : ";
str << _request->getTarget(); str << _request->getTarget();
str << " with code : "; str << " with response code : ";
str << _response.getStatusCode(); str << _response.getStatusCode();
_log->info(str.str()); _log->info(str.str());
} }