Merge branch 'main' into http-indev

This commit is contained in:
yosyo
2025-04-10 12:23:54 +02:00
committed by GitHub
13 changed files with 711 additions and 4 deletions

132
src/config/Route.cpp Normal file
View File

@ -0,0 +1,132 @@
/* ::: :::::::: */
/* Route.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/21 20:37:02 by adjoly #+# #+# */
/* Updated: 2025/03/26 08:19:25 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "cppeleven.hpp"
#include "log.hpp"
#include "node/default.hpp"
#include <config/default.hpp>
#include <map>
#include <string>
using namespace webserv::config;
std::map<std::string, std::string> *Route::_parseCGI(toml::ANode *table) {
std::map<std::string, std::string> *cgi =
new std::map<std::string, std::string>;
void *val;
for (std::map<std::string, toml::ANode *>::iterator it =
table->getTable()->begin();
it != table->getTable()->end(); it++) {
val = accessValue(it->first, toml::STRING, table, _log);
if (val != not_nullptr) {
if (cgi->find(it->first) != cgi->end())
continue;
else
(*cgi)[it->first] = *static_cast<std::string *>(val);
}
}
return cgi;
}
void Route::_parseMethods(std::vector<toml::ANode *> *table) {
std::string val;
_methods[0] = false;
_methods[1] = false;
_methods[2] = false;
for (std::vector<toml::ANode *>::iterator it = table->begin();
it != table->end(); it++) {
if ((*it)->type() == toml::STRING) {
val = *static_cast<std::string *>((*it)->getValue());
if (val == "GET")
_methods[0] = true;
if (val == "POST")
_methods[1] = true;
if (val == "DELETE")
_methods[2] = true;
}
}
}
Route::Route(toml::ANode *table, Logger *logger)
: _max_body(10485760), _log(logger) {
void *val;
bool found;
_log = logger;
_table = table;
if (_table->type() != toml::TABLE) {
_log->warn("location need to be a table and not a :" +
toml::nodeTypeToStr(_table->type()));
return;
}
val = accessValue("redirect", toml::STRING, _table, _log);
if (val != not_nullptr) {
_root = *static_cast<std::string *>(val);
_redirect = true;
return;
} else
_redirect = false;
val = accessValue("dirlist", toml::BOOL, _table, _log);
if (val != not_nullptr)
_dirlist = *static_cast<bool *>(val);
else
_dirlist = true;
val = accessValue("cookies", toml::BOOL, _table, _log);
if (val != not_nullptr)
_cookies = *static_cast<bool *>(val);
else
_cookies = false;
val = accessValue("upload_path", toml::STRING, _table, _log);
if (val != not_nullptr)
_up_root = *static_cast<std::string *>(val);
else
_up_root = "";
val = accessValue("index", toml::STRING, _table, _log);
if (val != not_nullptr)
_index = *static_cast<std::string *>(val);
else
_index = "index.html";
val = accessValue("root", toml::STRING, _table, _log);
if (val != not_nullptr)
_root = *static_cast<std::string *>(val);
else
#ifdef PKGS
_root = "/var/www/html"
#else
_root = "./html";
#endif
val =
accessValue("client_max_body_size", toml::STRING, _table, _log);
if (val != not_nullptr)
_max_body = _parseSize(*static_cast<std::string *>(val));
std::map<std::string, toml::ANode *>::iterator it =
_table->accessIt("cgi", toml::TABLE, found);
if (found == true && it != _table->getTable()->end())
_cgi = _parseCGI(it->second);
else
_cgi = not_nullptr;
val = accessValue("methods", toml::ARRAY, _table, _log);
if (val != not_nullptr)
_parseMethods(static_cast<std::vector<toml::ANode *> *>(val));
else {
_methods[0] = true;
_methods[1] = false;
_methods[2] = false;
}
}
Route::~Route(void) {
if (_redirect == false)
if (_cgi != not_nullptr)
delete _cgi;
}

145
src/config/Server.cpp Normal file
View File

@ -0,0 +1,145 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Server.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/24 15:10:07 by adjoly #+# #+# */
/* Updated: 2025/03/26 08:47:50 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "config/Server.hpp"
#include "config/Route.hpp"
#include "cppeleven.hpp"
#include "log.hpp"
#include "node/ANode.hpp"
#include "node/Table.hpp"
#include "node/default.hpp"
#include "tomlpp.hpp"
#include <config/default.hpp>
#include <stdexcept>
#include <string>
#include <sys/types.h>
using namespace webserv::config;
toml::ANode *Server::_getServerTable(void) {
toml::ANode *serverT;
std::map<std::string, toml::ANode *>::iterator table =
_table->getTable()->find("server");
if (table == _table->getTable()->end())
throw std::runtime_error(
"could not find any [server] table in config file :(");
else
serverT = table->second;
return serverT;
}
Server::Server(std::string file_name) {
toml::Toml *tomlFile = new toml::Toml(file_name);
try {
tomlFile->parse();
} catch (std::runtime_error &e) {
throw e;
}
bool found;
std::map<std::string, toml::ANode *> *map;
_table = tomlFile->getParsedFile();
void *val = _table->access("log_file", toml::STRING, found);
std::string log_file = "";
if (found == true && val != not_nullptr) {
std::string log_file = *static_cast<std::string *>(val);
}
_log = new Logger(log_file);
_table = _getServerTable();
// host and port parsing
void *host = accessValue("host", toml::STRING, _table, _log);
if (host != not_nullptr) {
_host = *static_cast<std::string *>(host);
} else {
throw std::runtime_error(
"no host specified - please specify one in server.host");
}
void *port = accessValue("port", toml::INT, _table, _log);
if (host != not_nullptr) {
_port = *static_cast<unsigned short *>(port);
} else {
throw std::runtime_error(
"no port specified - please specify one in server.port");
}
// server_names parsing
std::map<std::string, toml::ANode *>::iterator it =
_table->accessIt("server_names", toml::ARRAY, found);
if (found == true && it != _table->getTable()->end()) {
std::vector<toml::ANode *>::iterator vecIt =
it->second->getArray()->begin();
_server_names = new std::vector<std::string>;
for (; vecIt != it->second->getArray()->end(); vecIt++) {
std::string str = *static_cast<std::string *>((*vecIt)->getValue());
_server_names->push_back(str);
}
} else
_log->warn(
"no server_names all request will be accepted from any hostname");
// error_pages parsing
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
it = _table->accessIt("location", toml::TABLE, found);
if (found == true && it != _table->getTable()->end()) {
_routes = new std::map<std::string, 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(it->first) != _routes->end())
continue;
(*_routes)[it->first] = new Route(it->second, _log);
}
}
delete tomlFile->getParsedFile();
delete tomlFile;
}
Server::~Server(void) {
std::map<std::string, Route *>::iterator it = _routes->begin();
for (; it != _routes->end(); it++) {
delete it->second;
}
delete _routes;
delete _err_pages;
delete _server_names;
delete _log; // to see if nessecary
}
std::map<int, std::string> *
Server::_parseErrPages(std::map<std::string, toml::ANode *> *table) {
std::map<int, std::string> *errPages = new std::map<int, std::string>;
void *val;
int nb;
for (std::map<std::string, toml::ANode *>::iterator it = table->begin();
it != table->end(); it++) {
val = accessValue(it->first, toml::STRING, _table, _log);
if (val != not_nullptr) {
nb = std::atoi(it->first.c_str());
if (nb >= 400 && nb <= 599)
(*errPages)[nb] = *static_cast<std::string *>(val);
else
_log->warn("error page - " + it->first + " is not valid :(");
}
}
return errPages;
}

View File

@ -7,11 +7,12 @@
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/03 15:45:07 by mmoussou #+# #+# */
/* Updated: 2025/04/10 12:18:39 by mmoussou ### ########.fr */
/* Updated: 2025/03/25 17:10:29 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include <webserv.hpp>
#include <requests/default.hpp>
#include "config/Server.hpp"
#include <tomlpp.hpp>
#define PORT 8080
#define BUFFER_SIZE 4096