🏗️」 wip: Nearly finished config file parsing

This commit is contained in:
2025-03-24 10:52:02 +01:00
parent 3024aa3985
commit dbba3f0668
4 changed files with 250 additions and 24 deletions

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/19 14:59:41 by adjoly #+# #+# */
/* Updated: 2025/03/21 11:09:38 by adjoly ### ########.fr */
/* Updated: 2025/03/24 10:48:37 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,37 +14,20 @@
#include "cppeleven.hpp"
#include "log.hpp"
#include "node/ANode.hpp"
#include "node/Array.hpp"
#include <algorithm>
#include "node/default.hpp"
#include <cctype>
#include <cstdlib>
#include <map>
#include <stdexcept>
#include <string>
#include <tomlpp.hpp>
#include <vector>
namespace webserv {
namespace config {
int32_t parseSize(std::string size) {
if (size[size.size()] == 'M')
return std::atoi(size.c_str()) * 1024 * 1024;
if (size[size.size()] == 'K')
return std::atoi(size.c_str()) * 1024;
if (isalpha(size[size.size()]))
return std::atoi(size.c_str());
return -1;
}
class Route {
public:
Route(std::map<std::string, toml::ANode *> *node) {
if (node == not_nullptr)
throw std::runtime_error("location table does not exist");
}
~Route(void) {}
Route(toml::ANode *, Logger *);
~Route(void);
protected:
private:
@ -61,8 +44,67 @@ class Route {
Logger *_log;
bool _methods[3]; // 1: GET, 2: POST, 3: DELETE
std::map<int, std::string> *_err_pages;
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);
/**
* @brief Can be used to parse a table of cgi
*
* @param The table to get cgi from
*
* @return A pointer to a map of cgi
*/
std::map<std::string, std::string> *_parseCGI(std::map<std::string, toml::ANode *> *);
/**
* @brief Can be used to parse a table of error pages
*
* @param The table to get the error pages from
*
* @return A pointer to a map of error pages
*/
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 *> *);
/**
* @brief Can be used to sed err pages to the default error pages
*/
void _defaultErrPages(void);
/**
* @brief Can be used to parse a string of a number with a size (ex. 10M)
*
* @param The input string
*
* @return The number in bytes
*/
int32_t _parseSize(std::string size) {
if (size[size.size()] == 'M')
return std::atoi(size.c_str()) * 1024 * 1024;
if (size[size.size()] == 'K')
return std::atoi(size.c_str()) * 1024;
if (isalpha(size[size.size()]))
return std::atoi(size.c_str());
return -1;
}
};
} // namespace config