merge, the whole thing is cleaner

This commit is contained in:
Eric LAZO
2022-08-15 19:54:26 +02:00
29 changed files with 551 additions and 260 deletions

View File

@@ -14,8 +14,6 @@
# include <cstdlib> // strtol, stroul
# include <iostream> // cout, cin
# include <fstream> // ifstream
//# include <unistd.h> // access()
# include <dirent.h> // opendir(), doesn't work...
# include <sys/stat.h> // stat(), replaces opendir() don't bother with ERRNO ?
# include <algorithm> // sort() in Post
@@ -23,43 +21,37 @@ class ConfigParser {
public:
ConfigParser(const char* path); // a string?
// might not need this either, ask Luke
ConfigParser();
~ConfigParser();
ConfigParser(const std::string &config_file);
void read_config(const std::string &config_file);
std::vector<ServerConfig> * parse(); // const?
// i thought if it were an instance of this class you could call
// private member functions from anywhere...
// private member functions from anywhere... // QUESTION : Wut ?
void print_content() const;
private:
std::string _content;
// not sure i even need this...
ConfigParser();
ServerConfig _parse_server(size_t *start);
LocationConfig _parse_location(size_t *start);
void _set_server_values(ServerConfig *server, const std::string key, std::string value);
void _set_location_values(LocationConfig *location, const std::string key, std::string value);
/* Extra */
std::string _pre_set_val_check(const std::string key, \
std::string _pre_set_val_check(const std::string key,
const std::string value);
std::string _get_first_word(size_t *curr); // const?
std::string _get_rest_of_line(size_t *curr); // const?
/* Post Processing */
void _post_processing(std::vector<ServerConfig> *servers);
bool _find_root_path_location(std::vector<LocationConfig> locations) const;

View File

@@ -1,31 +1,14 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* LocationConfig.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/23 16:08:00 by me #+# #+# */
/* Updated: 2022/08/15 19:37:34 by erlazo ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LOCATIONCONFIG_HPP
# define LOCATIONCONFIG_HPP
# include <map>
# include <vector>
# include <string>
# include <iostream>
# include <sys/stat.h> // stat()
# include "utils.hpp"
// again, struct instead?
class LocationConfig
struct LocationConfig
{
public:
std::string path; // /path and /path/ are fine
// i remove trailing / systematically
std::string root;
@@ -91,13 +74,4 @@ public:
};
#endif

View File

@@ -10,13 +10,9 @@
# include <string> // string
# include <iostream> // cout, cin
// a class that's all public? just so we have options?
class ServerConfig
struct ServerConfig
{
public:
std::vector<std::string> server_name;
// we could shove default in here if we wanted to...
std::string host;
std::string port;
@@ -24,9 +20,7 @@ public:
std::string root; // ./www/ or www work www/ and www work
// i do remove trailing / tho
size_t client_body_limit; // set to default max if none set
// 413 (Request Entity Too Large) if exceeded
// default is 1m 1 000 000 ?
size_t client_body_limit;
std::vector<std::string> index;
std::map<int, std::string> error_pages;

View File

@@ -1,6 +1,4 @@
#include "ConfigParser.hpp"
// should i be sending & references?

View File

@@ -1,23 +1,26 @@
#include "ConfigParser.hpp"
// Default
// possibly remove...
ConfigParser::ConfigParser()
ConfigParser::ConfigParser() {};
ConfigParser::~ConfigParser() {};
ConfigParser::ConfigParser(const std::string &config_file)
{
std::cout << "Default Constructor\n";
read_config(config_file);
}
ConfigParser::ConfigParser(const char* path)
void ConfigParser::read_config(const std::string &config_file)
{
std::ifstream file;
std::string buf;
size_t comment;
_content.clear();
file.open(path);
if (file.is_open())
file.open(config_file.c_str());
if (!file)
throw std::invalid_argument("failed to open config");
else
{
_content.clear();
while (!file.eof())
{
getline(file, buf);
@@ -35,19 +38,9 @@ ConfigParser::ConfigParser(const char* path)
_content.append(tmp + '\n');
}
}
file.close();
}
else
throw std::invalid_argument("failed to open config");
}
// might not need this...
ConfigParser::~ConfigParser()
{
// do i need to destroy anything, won't it handle itself?
}
// const?
std::vector<ServerConfig> * ConfigParser::parse()
{
@@ -156,7 +149,7 @@ LocationConfig ConfigParser::_parse_location(size_t *start)
// should i be sending pointers or references?
void ConfigParser::_set_server_values(ServerConfig *server, \
void ConfigParser::_set_server_values(ServerConfig *server,
const std::string key, std::string value)
{
// should i be sending pointers or references?
@@ -181,7 +174,7 @@ void ConfigParser::_set_server_values(ServerConfig *server, \
}
}
else if (key == "listen" && size == 1 && server->host == ""
&& server->port == "")
&& server->port == "") // QUESTION LUKE : C'est quoi cette condition ? Si listen est vide ? Je comprends pas trop.
{
if (tmp_val[0].find_first_of(":") == NPOS)
{
@@ -222,6 +215,9 @@ void ConfigParser::_set_server_values(ServerConfig *server, \
if (!::isNumeric(tmp_val[0]))
throw std::invalid_argument("client_body_limit not a number");
server->client_body_limit = std::strtoul(tmp_val[0].c_str(), NULL, 10);
if (errno == ERANGE || server->client_body_limit > (ULONG_MAX / KB) )
throw std::invalid_argument("client_body_limit too big");
server->client_body_limit = server->client_body_limit * KB;
}
else if (key == "index")
{
@@ -231,7 +227,7 @@ void ConfigParser::_set_server_values(ServerConfig *server, \
else if (key == "error_page")
{
std::string path = tmp_val[size - 1];
for (size_t i = 0; i < size() - 1; i++)
for (size_t i = 0; i < size - 1; i++)
{
if (!(isNumeric_btw(400, 599, tmp_val[i])))
throw std::invalid_argument("invalid error code");
@@ -247,7 +243,7 @@ void ConfigParser::_set_server_values(ServerConfig *server, \
// should i be sending pointers or references?
void ConfigParser::_set_location_values(LocationConfig *location, \
void ConfigParser::_set_location_values(LocationConfig *location,
const std::string key, std::string value)
{
// should i be sending pointers or references?

View File

@@ -22,7 +22,7 @@ void ConfigParser::_post_processing(std::vector<ServerConfig> *servers)
throw std::invalid_argument("Config file needs an Index");
if (it->client_body_limit == 0)
it->client_body_limit = 5000; // what is the recomended size?
it->client_body_limit = 1 * MB;
// if error_pages is left empty, we'll use the defaults which