」 feat: Finished parsing (should be working)

This commit is contained in:
2025-03-26 08:48:33 +01:00
parent dbba3f0668
commit 1f18956cb6
11 changed files with 384 additions and 150 deletions

View File

@ -1,19 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Config.hpp :+: :+: :+: */
/* cgi.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/23 11:36:01 by adjoly #+# #+# */
/* Updated: 2025/03/23 12:09:10 by adjoly ### ########.fr */
/* Created: 2025/03/24 14:17:34 by adjoly #+# #+# */
/* Updated: 2025/03/24 14:20:00 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#include <stdexcept>
namespace webserv {
namespace config {};
}; // namespace webserv
#include <sstream>
class cgi {
public:
cgi();
~cgi(void);
protected:
private:
std::string _request;
};

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/19 14:59:41 by adjoly #+# #+# */
/* Updated: 2025/03/24 10:48:37 by adjoly ### ########.fr */
/* Updated: 2025/03/26 08:31:41 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -33,31 +33,20 @@ class Route {
private:
bool _dirlist;
bool _cookies;
bool _uploads;
bool _redirect;
int32_t _max_body;
std::string _root;
std::string _upRoot;
std::string _up_root;
std::string _index;
std::map<std::string, std::string> *_cgi;
Logger *_log;
bool _methods[3]; ///> A methods boolean array which correspond to - 0: GET, 1: POST, 2: DELETE
std::map<int, std::string> *_err_pages; ///> An error pages map to map error specified in the config file
toml::ANode *_table;
/**
* @brief Can be used to access a value in the _table(ANode *) of a specific type
*
* @param The name of the value to get
* @param The type of the value to get
*
* @return The value got or not_nullptr
*/
void *accessValue(std::string, toml::nodeType);
bool _methods[3]; ///> A methods boolean array which correspond to - 0: GET,
///1: POST, 2: DELETE
toml::ANode *_table;
/**
* @brief Can be used to parse a table of cgi
@ -66,7 +55,8 @@ class Route {
*
* @return A pointer to a map of cgi
*/
std::map<std::string, std::string> *_parseCGI(std::map<std::string, toml::ANode *> *);
std::map<std::string, std::string> *
_parseCGI(toml::ANode *);
/**
* @brief Can be used to parse a table of error pages
@ -75,19 +65,20 @@ class Route {
*
* @return A pointer to a map of error pages
*/
std::map<int, std::string> *_parseErrPages(std::map<std::string, toml::ANode *> *);
std::map<int, std::string> *
_parseErrPages(std::map<std::string, toml::ANode *> *);
/**
* @brief Can be used to parse a array of methods
*
* @param The table to get the methods from
*/
void _parseMethods(std::vector<toml::ANode *> *);
void _parseMethods(std::vector<toml::ANode *> *);
/**
* @brief Can be used to sed err pages to the default error pages
*/
void _defaultErrPages(void);
void _defaultErrPages(void);
/**
* @brief Can be used to parse a string of a number with a size (ex. 10M)

View File

@ -6,25 +6,86 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/19 14:11:28 by adjoly #+# #+# */
/* Updated: 2025/03/19 23:09:56 by adjoly ### ########.fr */
/* Updated: 2025/03/25 17:56:34 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#include "Route.hpp"
#include "config/default.hpp"
#include "cppeleven.hpp"
#include "node/ANode.hpp"
namespace webserv {
namespace config {
class Server {
public:
Server(std::string);
~Server();
/**
* @brief Can be used to get the path of a specific error page or return
* an empty string
*
* @param The http error code for the page
*/
std::string getErrorPage(int page) {
if (_err_pages->find(page) != _err_pages->end())
return (*_err_pages)[page];
else
return "";
}
/**
* @brief Can be used to get a pointer to a specific route (or not_nullptr
* if not found)
*/
Route *getRoute(std::string route) {
if (_routes->find(route) != _routes->end())
return (*_routes)[route];
else
return not_nullptr;
}
/**
* @brief Can be used to get the Logger pointer
*/
Logger *getLogger(void) { return _log; }
// @brief Can be used to get a server name
std::vector<std::string> *getServerNames(void) { return _server_names; }
// @brief Can be used to get the host specified in the config file
std::string getHost(void) { return _host; }
// @brief Can be used to get the port specified in the config file
int getPort(void) { return _port; }
protected:
private:
Route *_default;
std::vector<Route *> *_routes;
std::string host;
std::vector<std::string> server_names;
std::map<std::string, Route *>
*_routes; ///> A map of all the route present in the config file
std::map<int, std::string> *_err_pages; ///> An error pages map to map error
/// specified in the config file
std::vector<std::string>
*_server_names; ///> A vector with all the server names
std::string _host; ///> The host on which the server will be exposed
unsigned short _port; ///> The port on which the server will be exposed
toml::ANode *_table; ///> The table used for the parsing (is deleted at the
/// end of the constructor)
Logger *_log; ///> A pointer to the logger class
std::map<int, std::string> *
_parseErrPages(std::map<std::string, toml::ANode *> *table);
/**
* @brief Can be used to get the [server] table in _table
*
* @return A pointer to the [server] table as an ANode
*/
toml::ANode *_getServerTable(void);
};
} // namespace config

View File

@ -6,11 +6,53 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/19 14:15:51 by adjoly #+# #+# */
/* Updated: 2025/03/19 14:16:39 by adjoly ### ########.fr */
/* Updated: 2025/03/26 08:39:08 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#include "Route.hpp"
#include "Server.hpp"
#include "cppeleven.hpp"
#include "node/Table.hpp"
#include "node/default.hpp"
#include <tomlpp.hpp>
#include "Config.hpp"
namespace webserv {
namespace config {
/**
* @brief Can be used to access a value in the _table(ANode *) of a specific
*type
*
* @param The name of the value to get
* @param The type of the value to get
* @param The table to search in
* @param A Logger class
*
* @return The value got or not_nullptr
*/
static inline void *accessValue(const std::string &name, toml::nodeType type,
toml::ANode *table, Logger *log) {
void *val;
bool found = false;
if (table == not_nullptr)
return not_nullptr;
val = dynamic_cast<toml::Table *>(table)->access(name, type, found);
if (found == true && val != not_nullptr) {
return val;
} else {
if (found == false) {
return not_nullptr;
} else {
log->warn("found - " + name + " but is not " +
toml::nodeTypeToStr(type) + ", skipping...");
return not_nullptr;
}
}
}
}; // namespace config
}; // namespace webserv

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/20 09:28:27 by adjoly #+# #+# */
/* Updated: 2025/03/20 14:55:09 by adjoly ### ########.fr */
/* Updated: 2025/03/25 17:50:45 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -21,37 +21,69 @@
namespace webserv {
class Logger {
public:
Logger(std::string fileName) {
Logger(const std::string &fileName) : _fileName(fileName) {
if (fileName.empty())
_ttyOnly = true;
else {
_file.open(fileName.c_str(), std::ios::app);
_ttyOnly = false;
}
if (!_file.is_open()) {
throw std::runtime_error("could not open fileeee"); // TODO change that shit but i dont know what to put other than a htrow
if (!_file.is_open() && !_ttyOnly) {
throw std::runtime_error(
"could not open fileeee"); // TODO change that shit but i dont
// know what to put other than a
// htrow
}
}
~Logger(void) { _file.close(); }
Logger(const Logger &other) : _ttyOnly(other._ttyOnly) {
if (!other._ttyOnly) {
_file.open(other._fileName.c_str(), std::ios::app);
if (!_file.is_open()) {
throw std::runtime_error("Could not open file: " +
other._fileName);
}
}
}
void info(std::string msg) {
std::stringstream ss = printPogitMsg("✏️", "webserv", "info", msg);
// Copy assignment operator
Logger &operator=(const Logger &other) {
if (this != &other) {
if (_file.is_open()) {
_file.close();
}
_ttyOnly = other._ttyOnly;
if (!other._ttyOnly) {
_file.open(other._fileName.c_str(), std::ios::app);
if (!_file.is_open()) {
throw std::runtime_error("Could not open file: " +
other._fileName);
}
}
}
return *this;
}
~Logger(void) {
if (_file.is_open())
_file.close();
}
void info(const std::string &msg) {
std::string ss = printPogitMsg("✏️", "webserv", "info", msg);
std::cerr << ss << std::endl;
if (!_ttyOnly) {
_file << ss << std::endl;
}
}
void warn(std::string msg) {
std::stringstream ss = printPogitMsg("🔨", "webserv", "warning", msg);
void warn(const std::string &msg) {
std::string ss = printPogitMsg("🔨", "webserv", "warning", msg);
std::cerr << ss << std::endl;
if (!_ttyOnly) {
_file << ss << std::endl;
}
}
void error(std::string msg) {
std::stringstream ss = printPogitMsg("🚧", "webserv", "error", msg);
void error(const std::string &msg) {
std::string ss = printPogitMsg("🚧", "webserv", "error", msg);
std::cerr << ss << std::endl;
if (!_ttyOnly) {
_file << ss << std::endl;
@ -60,7 +92,10 @@ class Logger {
protected:
private:
std::stringstream printPogitMsg(std::string emoji, std::string type, std::string what, std::string msg) {
std::string printPogitMsg(const std::string &emoji,
const std::string &type,
const std::string &what,
const std::string &msg) {
std::stringstream os;
#ifdef tty
if (what.empty())
@ -73,8 +108,10 @@ class Logger {
else
os << "" << emoji << "" << type << "(" << what << "):" << msg;
#endif
return os;
return os.str();
}
std::string _fileName;
bool _ttyOnly;
std::ofstream _file;
};