mirror of
https://github.com/KeyZox71/webserv.git
synced 2025-05-11 00:08:46 +02:00
「✨」 feat: Finished parsing (should be working)
This commit is contained in:
4
Makefile
4
Makefile
@ -6,7 +6,7 @@
|
|||||||
# By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ #
|
# By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ #
|
||||||
# +#+#+#+#+#+ +#+ #
|
# +#+#+#+#+#+ +#+ #
|
||||||
# Created: 2024/10/25 16:09:27 by adjoly #+# #+# #
|
# Created: 2024/10/25 16:09:27 by adjoly #+# #+# #
|
||||||
# Updated: 2025/03/19 14:01:12 by adjoly ### ########.fr #
|
# Updated: 2025/03/25 18:13:53 by adjoly ### ########.fr #
|
||||||
# #
|
# #
|
||||||
# **************************************************************************** #
|
# **************************************************************************** #
|
||||||
|
|
||||||
@ -24,7 +24,7 @@ SRCS = $(shell find . -name '*.cpp')
|
|||||||
|
|
||||||
OBJS = $(addprefix $(OBJSDIR), $(SRCS:.cpp=.o))
|
OBJS = $(addprefix $(OBJSDIR), $(SRCS:.cpp=.o))
|
||||||
|
|
||||||
FLAGS = -Wall -Werror -Wextra -std=c++98 -MMD -MP
|
FLAGS = -Wall -Werror -Wextra -std=c++98 -MMD -MP -g
|
||||||
|
|
||||||
RED = \033[0;31m
|
RED = \033[0;31m
|
||||||
GREEN = \033[0;32m
|
GREEN = \033[0;32m
|
||||||
|
@ -3,17 +3,6 @@ server_names = { "localhost", "2B5.local" }
|
|||||||
host = "localhost"
|
host = "localhost"
|
||||||
port = 8080
|
port = 8080
|
||||||
|
|
||||||
root = "/var/www/html"
|
|
||||||
methods = { "GET", "POST", "DELETE" }
|
|
||||||
|
|
||||||
dirlist = false
|
|
||||||
uploads = false
|
|
||||||
cookies = false
|
|
||||||
|
|
||||||
cgi.py = "/usr/bin/python3"
|
|
||||||
|
|
||||||
client_max_body_size = "10M"
|
|
||||||
|
|
||||||
[server.error_pages]
|
[server.error_pages]
|
||||||
404 = "not_found.html"
|
404 = "not_found.html"
|
||||||
401 = "unauthorized.html"
|
401 = "unauthorized.html"
|
||||||
@ -25,12 +14,8 @@ root = "/var/www/html"
|
|||||||
dirlist = true
|
dirlist = true
|
||||||
client_max_body_size = "10M"
|
client_max_body_size = "10M"
|
||||||
|
|
||||||
[server.location./.error_pages]
|
|
||||||
500 = "uwu.html"
|
|
||||||
|
|
||||||
[server.location./api]
|
[server.location./api]
|
||||||
methods = { "GET", "POST" }
|
methods = { "GET", "POST" }
|
||||||
uploads = true
|
|
||||||
root = "/var/www/api"
|
root = "/var/www/api"
|
||||||
upload_path = "/etc/webserv/up"
|
upload_path = "/etc/webserv/up"
|
||||||
cgi.go = "/bin/go"
|
cgi.go = "/bin/go"
|
||||||
|
@ -1,19 +1,23 @@
|
|||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
/* */
|
/* */
|
||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* Config.hpp :+: :+: :+: */
|
/* cgi.hpp :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/03/23 11:36:01 by adjoly #+# #+# */
|
/* Created: 2025/03/24 14:17:34 by adjoly #+# #+# */
|
||||||
/* Updated: 2025/03/23 12:09:10 by adjoly ### ########.fr */
|
/* Updated: 2025/03/24 14:20:00 by adjoly ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <stdexcept>
|
#include <sstream>
|
||||||
|
class cgi {
|
||||||
namespace webserv {
|
public:
|
||||||
namespace config {};
|
cgi();
|
||||||
}; // namespace webserv
|
~cgi(void);
|
||||||
|
protected:
|
||||||
|
private:
|
||||||
|
std::string _request;
|
||||||
|
};
|
@ -6,7 +6,7 @@
|
|||||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/03/19 14:59:41 by adjoly #+# #+# */
|
/* 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:
|
private:
|
||||||
bool _dirlist;
|
bool _dirlist;
|
||||||
bool _cookies;
|
bool _cookies;
|
||||||
bool _uploads;
|
|
||||||
bool _redirect;
|
bool _redirect;
|
||||||
|
|
||||||
int32_t _max_body;
|
int32_t _max_body;
|
||||||
|
|
||||||
std::string _root;
|
std::string _root;
|
||||||
std::string _upRoot;
|
std::string _up_root;
|
||||||
|
std::string _index;
|
||||||
std::map<std::string, std::string> *_cgi;
|
std::map<std::string, std::string> *_cgi;
|
||||||
|
|
||||||
Logger *_log;
|
Logger *_log;
|
||||||
|
|
||||||
bool _methods[3]; ///> A methods boolean array which correspond to - 0: GET, 1: POST, 2: DELETE
|
bool _methods[3]; ///> A methods boolean array which correspond to - 0: GET,
|
||||||
std::map<int, std::string> *_err_pages; ///> An error pages map to map error specified in the config file
|
///1: POST, 2: DELETE
|
||||||
toml::ANode *_table;
|
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
|
* @brief Can be used to parse a table of cgi
|
||||||
@ -66,7 +55,8 @@ class Route {
|
|||||||
*
|
*
|
||||||
* @return A pointer to a map of cgi
|
* @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
|
* @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
|
* @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
|
* @brief Can be used to parse a array of methods
|
||||||
*
|
*
|
||||||
* @param The table to get the methods from
|
* @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
|
* @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)
|
* @brief Can be used to parse a string of a number with a size (ex. 10M)
|
||||||
|
@ -6,25 +6,86 @@
|
|||||||
/* 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/03/19 23:09:56 by adjoly ### ########.fr */
|
/* Updated: 2025/03/25 17:56:34 by adjoly ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "Route.hpp"
|
#include "config/default.hpp"
|
||||||
|
#include "cppeleven.hpp"
|
||||||
|
#include "node/ANode.hpp"
|
||||||
|
|
||||||
namespace webserv {
|
namespace webserv {
|
||||||
namespace config {
|
namespace config {
|
||||||
|
|
||||||
class Server {
|
class Server {
|
||||||
public:
|
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:
|
protected:
|
||||||
private:
|
private:
|
||||||
Route *_default;
|
std::map<std::string, Route *>
|
||||||
std::vector<Route *> *_routes;
|
*_routes; ///> A map of all the route present in the config file
|
||||||
std::string host;
|
std::map<int, std::string> *_err_pages; ///> An error pages map to map error
|
||||||
std::vector<std::string> server_names;
|
/// 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
|
} // namespace config
|
||||||
|
@ -6,11 +6,53 @@
|
|||||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/03/19 14:15:51 by adjoly #+# #+# */
|
/* 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
|
#pragma once
|
||||||
|
|
||||||
|
#include "Route.hpp"
|
||||||
|
#include "Server.hpp"
|
||||||
|
#include "cppeleven.hpp"
|
||||||
|
#include "node/Table.hpp"
|
||||||
|
#include "node/default.hpp"
|
||||||
#include <tomlpp.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
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/03/20 09:28:27 by adjoly #+# #+# */
|
/* 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 {
|
namespace webserv {
|
||||||
class Logger {
|
class Logger {
|
||||||
public:
|
public:
|
||||||
Logger(std::string fileName) {
|
Logger(const std::string &fileName) : _fileName(fileName) {
|
||||||
if (fileName.empty())
|
if (fileName.empty())
|
||||||
_ttyOnly = true;
|
_ttyOnly = true;
|
||||||
else {
|
else {
|
||||||
_file.open(fileName.c_str(), std::ios::app);
|
_file.open(fileName.c_str(), std::ios::app);
|
||||||
_ttyOnly = false;
|
_ttyOnly = false;
|
||||||
}
|
}
|
||||||
if (!_file.is_open()) {
|
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
|
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) {
|
// Copy assignment operator
|
||||||
std::stringstream ss = printPogitMsg("✏️", "webserv", "info", msg);
|
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;
|
std::cerr << ss << std::endl;
|
||||||
if (!_ttyOnly) {
|
if (!_ttyOnly) {
|
||||||
_file << ss << std::endl;
|
_file << ss << std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void warn(std::string msg) {
|
void warn(const std::string &msg) {
|
||||||
std::stringstream ss = printPogitMsg("🔨", "webserv", "warning", msg);
|
std::string ss = printPogitMsg("🔨", "webserv", "warning", msg);
|
||||||
std::cerr << ss << std::endl;
|
std::cerr << ss << std::endl;
|
||||||
if (!_ttyOnly) {
|
if (!_ttyOnly) {
|
||||||
_file << ss << std::endl;
|
_file << ss << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
void error(std::string msg) {
|
void error(const std::string &msg) {
|
||||||
std::stringstream ss = printPogitMsg("🚧", "webserv", "error", msg);
|
std::string ss = printPogitMsg("🚧", "webserv", "error", msg);
|
||||||
std::cerr << ss << std::endl;
|
std::cerr << ss << std::endl;
|
||||||
if (!_ttyOnly) {
|
if (!_ttyOnly) {
|
||||||
_file << ss << std::endl;
|
_file << ss << std::endl;
|
||||||
@ -60,7 +92,10 @@ class Logger {
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
private:
|
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;
|
std::stringstream os;
|
||||||
#ifdef tty
|
#ifdef tty
|
||||||
if (what.empty())
|
if (what.empty())
|
||||||
@ -73,8 +108,10 @@ class Logger {
|
|||||||
else
|
else
|
||||||
os << "「" << emoji << "」" << type << "(" << what << "):" << msg;
|
os << "「" << emoji << "」" << type << "(" << what << "):" << msg;
|
||||||
#endif
|
#endif
|
||||||
return os;
|
return os.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string _fileName;
|
||||||
bool _ttyOnly;
|
bool _ttyOnly;
|
||||||
std::ofstream _file;
|
std::ofstream _file;
|
||||||
};
|
};
|
||||||
|
Submodule lib/tomlpp updated: 3131ed3ac4...d9e507093a
@ -1,61 +1,42 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* Route.cpp :+: :+: :+: */
|
/* Route.cpp :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/03/21 20:37:02 by adjoly #+# #+# */
|
/* Created: 2025/03/21 20:37:02 by adjoly #+# #+# */
|
||||||
/* Updated: 2025/03/24 10:51:29 by adjoly ### ########.fr */
|
/* Updated: 2025/03/26 08:19:25 by adjoly ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "cppeleven.hpp"
|
#include "cppeleven.hpp"
|
||||||
#include "log.hpp"
|
#include "log.hpp"
|
||||||
#include "node/ANode.hpp"
|
|
||||||
#include "node/default.hpp"
|
#include "node/default.hpp"
|
||||||
#include <config/Route.hpp>
|
#include <config/default.hpp>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
using namespace webserv::config;
|
using namespace webserv::config;
|
||||||
|
|
||||||
std::map<std::string, std::string> *
|
std::map<std::string, std::string> *Route::_parseCGI(toml::ANode *table) {
|
||||||
Route::_parseCGI(std::map<std::string, toml::ANode *> *table) {
|
|
||||||
std::map<std::string, std::string> *cgi =
|
std::map<std::string, std::string> *cgi =
|
||||||
new std::map<std::string, std::string>;
|
new std::map<std::string, std::string>;
|
||||||
void *val;
|
void *val;
|
||||||
|
|
||||||
for (std::map<std::string, toml::ANode *>::iterator it = table->begin();
|
for (std::map<std::string, toml::ANode *>::iterator it =
|
||||||
it != table->end(); it++) {
|
table->getTable()->begin();
|
||||||
val = accessValue(it->first, toml::STRING);
|
it != table->getTable()->end(); it++) {
|
||||||
if (val != not_nullptr)
|
val = accessValue(it->first, toml::STRING, table, _log);
|
||||||
cgi->insert(it->first, *static_cast<std::string *>(val));
|
if (val != not_nullptr) {
|
||||||
|
if (cgi->find(it->first) != cgi->end())
|
||||||
|
continue;
|
||||||
|
else
|
||||||
|
(*cgi)[it->first] = *static_cast<std::string *>(val);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return cgi;
|
return cgi;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::map<int, std::string> *
|
|
||||||
Route::_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);
|
|
||||||
if (val != not_nullptr) {
|
|
||||||
nb = std::atoi(it->first.c_str());
|
|
||||||
if (nb != 0 && (nb >= 400 && nb <= 599))
|
|
||||||
(*errPages)[nb] = *static_cast<std::string *>(val);
|
|
||||||
else
|
|
||||||
_log->warn("error page - " + it->first + " is not valid :(");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return errPages;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Route::_parseMethods(std::vector<toml::ANode *> *table) {
|
void Route::_parseMethods(std::vector<toml::ANode *> *table) {
|
||||||
std::string val;
|
std::string val;
|
||||||
|
|
||||||
@ -76,40 +57,46 @@ void Route::_parseMethods(std::vector<toml::ANode *> *table) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Route::_defaultErrPages(void) {
|
|
||||||
_err_pages = new std::map<int, std::string>;
|
|
||||||
|
|
||||||
(*_err_pages)[400] = _root + "/400.html";
|
|
||||||
(*_err_pages)[403] = _root + "/403.html";
|
|
||||||
(*_err_pages)[404] = _root + "/404.html";
|
|
||||||
}
|
|
||||||
|
|
||||||
Route::Route(toml::ANode *table, Logger *logger)
|
Route::Route(toml::ANode *table, Logger *logger)
|
||||||
: _max_body(10485760), _log(logger) {
|
: _max_body(10485760), _log(logger) {
|
||||||
void *val;
|
void *val;
|
||||||
|
bool found;
|
||||||
|
|
||||||
_table = table;
|
|
||||||
_log = logger;
|
_log = logger;
|
||||||
val = accessValue("redirect", toml::STRING);
|
_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) {
|
if (val != not_nullptr) {
|
||||||
_root = *static_cast<std::string *>(val);
|
_root = *static_cast<std::string *>(val);
|
||||||
_redirect = true;
|
_redirect = true;
|
||||||
return;
|
return;
|
||||||
}
|
} else
|
||||||
val = accessValue("dirlist", toml::BOOL);
|
_redirect = false;
|
||||||
|
val = accessValue("dirlist", toml::BOOL, _table, _log);
|
||||||
if (val != not_nullptr)
|
if (val != not_nullptr)
|
||||||
_dirlist = *static_cast<bool *>(val);
|
_dirlist = *static_cast<bool *>(val);
|
||||||
else
|
else
|
||||||
_dirlist = true;
|
_dirlist = true;
|
||||||
val = accessValue("cookies", toml::BOOL);
|
val = accessValue("cookies", toml::BOOL, _table, _log);
|
||||||
if (val != not_nullptr)
|
if (val != not_nullptr)
|
||||||
_cookies = *static_cast<bool *>(val);
|
_cookies = *static_cast<bool *>(val);
|
||||||
else
|
else
|
||||||
_cookies = false;
|
_cookies = false;
|
||||||
val = accessValue("uploads", toml::BOOL);
|
val = accessValue("upload_path", toml::STRING, _table, _log);
|
||||||
if (val != not_nullptr)
|
if (val != not_nullptr)
|
||||||
_uploads = *static_cast<bool *>(val);
|
_up_root = *static_cast<std::string *>(val);
|
||||||
val = accessValue("root", toml::STRING);
|
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)
|
if (val != not_nullptr)
|
||||||
_root = *static_cast<std::string *>(val);
|
_root = *static_cast<std::string *>(val);
|
||||||
else
|
else
|
||||||
@ -118,25 +105,17 @@ Route::Route(toml::ANode *table, Logger *logger)
|
|||||||
#else
|
#else
|
||||||
_root = "./html";
|
_root = "./html";
|
||||||
#endif
|
#endif
|
||||||
val = accessValue("upload_path", toml::STRING);
|
val =
|
||||||
if (val != not_nullptr)
|
accessValue("client_max_body_size", toml::STRING, _table, _log);
|
||||||
_upRoot = *static_cast<std::string *>(val);
|
|
||||||
val = accessValue("client_max_body_size", toml::STRING);
|
|
||||||
if (val != not_nullptr)
|
if (val != not_nullptr)
|
||||||
_max_body = _parseSize(*static_cast<std::string *>(val));
|
_max_body = _parseSize(*static_cast<std::string *>(val));
|
||||||
val = accessValue("cgi", toml::TABLE);
|
std::map<std::string, toml::ANode *>::iterator it =
|
||||||
if (val != not_nullptr)
|
_table->accessIt("cgi", toml::TABLE, found);
|
||||||
_cgi =
|
if (found == true && it != _table->getTable()->end())
|
||||||
_parseCGI(static_cast<std::map<std::string, toml::ANode *> *>(val));
|
_cgi = _parseCGI(it->second);
|
||||||
else
|
else
|
||||||
_cgi = not_nullptr;
|
_cgi = not_nullptr;
|
||||||
val = accessValue("error_pages", toml::TABLE);
|
val = accessValue("methods", toml::ARRAY, _table, _log);
|
||||||
if (val != not_nullptr)
|
|
||||||
_err_pages = _parseErrPages(
|
|
||||||
static_cast<std::map<std::string, toml::ANode *> *>(val));
|
|
||||||
else
|
|
||||||
_err_pages = _defaultErrPages();
|
|
||||||
val = accessValue("methods", toml::ARRAY);
|
|
||||||
if (val != not_nullptr)
|
if (val != not_nullptr)
|
||||||
_parseMethods(static_cast<std::vector<toml::ANode *> *>(val));
|
_parseMethods(static_cast<std::vector<toml::ANode *> *>(val));
|
||||||
else {
|
else {
|
||||||
@ -146,20 +125,8 @@ Route::Route(toml::ANode *table, Logger *logger)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void *Route::accessValue(std::string name, toml::nodeType type) {
|
Route::~Route(void) {
|
||||||
void *val;
|
if (_redirect == false)
|
||||||
bool found;
|
if (_cgi != not_nullptr)
|
||||||
|
delete _cgi;
|
||||||
val = _table->access(name, type, found);
|
|
||||||
if (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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
145
src/config/Server.cpp
Normal file
145
src/config/Server.cpp
Normal 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;
|
||||||
|
}
|
@ -6,12 +6,14 @@
|
|||||||
/* 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/03/19 16:02:30 by adjoly ### ########.fr */
|
/* Updated: 2025/03/25 17:10:29 by adjoly ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "config/Server.hpp"
|
||||||
#include <tomlpp.hpp>
|
#include <tomlpp.hpp>
|
||||||
|
|
||||||
int main(int, char **) {
|
int main(int, char **) {
|
||||||
|
webserv::config::Server serverConf("exemples/test.toml");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user