mirror of
https://github.com/KeyZox71/webserv.git
synced 2025-05-11 00:28:45 +02:00
Merge branch 'main' into http-indev
This commit is contained in:
23
includes/cgi.hpp
Normal file
23
includes/cgi.hpp
Normal file
@ -0,0 +1,23 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* cgi.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/03/24 14:17:34 by adjoly #+# #+# */
|
||||
/* Updated: 2025/03/24 14:20:00 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <sstream>
|
||||
class cgi {
|
||||
public:
|
||||
cgi();
|
||||
~cgi(void);
|
||||
protected:
|
||||
private:
|
||||
std::string _request;
|
||||
};
|
102
includes/config/Route.hpp
Normal file
102
includes/config/Route.hpp
Normal file
@ -0,0 +1,102 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Route.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/03/19 14:59:41 by adjoly #+# #+# */
|
||||
/* Updated: 2025/03/26 08:31:41 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "cppeleven.hpp"
|
||||
#include "log.hpp"
|
||||
#include "node/default.hpp"
|
||||
#include <cctype>
|
||||
#include <cstdlib>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <tomlpp.hpp>
|
||||
|
||||
namespace webserv {
|
||||
namespace config {
|
||||
|
||||
class Route {
|
||||
public:
|
||||
Route(toml::ANode *, Logger *);
|
||||
~Route(void);
|
||||
|
||||
protected:
|
||||
private:
|
||||
bool _dirlist;
|
||||
bool _cookies;
|
||||
bool _redirect;
|
||||
|
||||
int32_t _max_body;
|
||||
|
||||
std::string _root;
|
||||
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
|
||||
toml::ANode *_table;
|
||||
|
||||
/**
|
||||
* @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(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
|
||||
} // namespace webserv
|
93
includes/config/Server.hpp
Normal file
93
includes/config/Server.hpp
Normal file
@ -0,0 +1,93 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Server.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/03/19 14:11:28 by adjoly #+# #+# */
|
||||
/* Updated: 2025/03/25 17:56:34 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#pragma once
|
||||
|
||||
#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:
|
||||
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
|
||||
|
||||
} // namespace webserv
|
58
includes/config/default.hpp
Normal file
58
includes/config/default.hpp
Normal file
@ -0,0 +1,58 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* default.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/03/19 14:15:51 by adjoly #+# #+# */
|
||||
/* 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>
|
||||
|
||||
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
|
119
includes/log.hpp
Normal file
119
includes/log.hpp
Normal file
@ -0,0 +1,119 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* log.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/03/20 09:28:27 by adjoly #+# #+# */
|
||||
/* Updated: 2025/03/25 17:50:45 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
namespace webserv {
|
||||
class Logger {
|
||||
public:
|
||||
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() && !_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(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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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(const std::string &msg) {
|
||||
std::string ss = printPogitMsg("🔨", "webserv", "warning", msg);
|
||||
std::cerr << ss << std::endl;
|
||||
if (!_ttyOnly) {
|
||||
_file << ss << std::endl;
|
||||
}
|
||||
}
|
||||
void error(const std::string &msg) {
|
||||
std::string ss = printPogitMsg("🚧", "webserv", "error", msg);
|
||||
std::cerr << ss << std::endl;
|
||||
if (!_ttyOnly) {
|
||||
_file << ss << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
private:
|
||||
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())
|
||||
os << type << ":" << msg;
|
||||
else
|
||||
os << type << "(" << what << "):" << msg;
|
||||
#else
|
||||
if (what.empty())
|
||||
os << "「" << emoji << "」" << type << ":" << msg;
|
||||
else
|
||||
os << "「" << emoji << "」" << type << "(" << what << "):" << msg;
|
||||
#endif
|
||||
return os.str();
|
||||
}
|
||||
|
||||
std::string _fileName;
|
||||
bool _ttyOnly;
|
||||
std::ofstream _file;
|
||||
};
|
||||
|
||||
}; // namespace webserv
|
Reference in New Issue
Block a user