clean comment and stuff

This commit is contained in:
lperrey
2022-08-18 12:44:09 +02:00
parent 9b0fcc1520
commit 469eca8aa9
27 changed files with 100 additions and 325 deletions

View File

@@ -14,46 +14,41 @@
# include <cstdlib> // strtol, stroul
# include <iostream> // cout, cin
# include <fstream> // ifstream
# include <sys/stat.h> // stat(), replaces opendir() don't bother with ERRNO ?
# include <algorithm> // sort() in Post
class ConfigParser {
public:
public:
ConfigParser();
~ConfigParser();
ConfigParser(const std::string &config_file);
ConfigParser();
~ConfigParser();
ConfigParser(const std::string &config_file);
void read_config(const std::string &config_file);
void read_config(const std::string &config_file);
std::vector<ServerConfig> * parse(); // const?
std::vector<ServerConfig> *parse() const;
void print_content() const;
// i thought if it were an instance of this class you could call
// private member functions from anywhere... // QUESTION : Wut ?
void print_content() const;
private:
std::string _content;
ServerConfig _parse_server(size_t *start) const;
LocationConfig _parse_location(size_t *start) const;
private:
std::string _content;
void _set_server_values(ServerConfig *server, const std::string &key, std::string value) const;
void _set_location_values(LocationConfig *location, const std::string &key, std::string value) const;
ServerConfig _parse_server(size_t *start);
LocationConfig _parse_location(size_t *start);
/* Extra */
std::string _pre_set_val_check(const std::string &key,
const std::string &value) const;
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);
std::string _get_first_word(size_t *curr) const;
std::string _get_rest_of_line(size_t *curr) const;
/* Extra */
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;
/* Post Processing */
void _post_processing(std::vector<ServerConfig> *servers) const;
bool _find_root_path_location(std::vector<LocationConfig> locations) const;
};

View File

@@ -1,16 +1,13 @@
#include "ConfigParser.hpp"
// should i be sending & references?
// const?
std::string ConfigParser::_pre_set_val_check(const std::string key,
const std::string value)
std::string ConfigParser::_pre_set_val_check(const std::string &key,
const std::string &value) const
{
// check key for ;
// check values for ; at end and right number of words depending on key
// std::cout << "pre check\n";
if (key.find_first_of(";") != NPOS)
throw std::invalid_argument("bad config file arguments");
@@ -30,9 +27,8 @@ std::string ConfigParser::_pre_set_val_check(const std::string key,
return (value.substr(0, i));
}
// const?
// assumes curr is on a space or \t or \n
std::string ConfigParser::_get_first_word(size_t *curr)
std::string ConfigParser::_get_first_word(size_t *curr) const
{
size_t start;
@@ -45,9 +41,8 @@ std::string ConfigParser::_get_first_word(size_t *curr)
return (key);
}
// const?
// also assumes curr is on a space \t or \n
std::string ConfigParser::_get_rest_of_line(size_t *curr)
std::string ConfigParser::_get_rest_of_line(size_t *curr) const
{
size_t start;

View File

@@ -41,8 +41,7 @@ void ConfigParser::read_config(const std::string &config_file)
}
}
// const?
std::vector<ServerConfig> * ConfigParser::parse()
std::vector<ServerConfig> * ConfigParser::parse() const
{
std::vector<ServerConfig> * ret = new std::vector<ServerConfig>();
@@ -67,7 +66,7 @@ std::vector<ServerConfig> * ConfigParser::parse()
return (ret);
}
ServerConfig ConfigParser::_parse_server(size_t *start)
ServerConfig ConfigParser::_parse_server(size_t *start) const
{
ServerConfig ret;
size_t curr = _content.find_first_not_of(" \t\n", *start);
@@ -78,7 +77,6 @@ ServerConfig ConfigParser::_parse_server(size_t *start)
if ((curr = _content.find_first_of(" \t\n", curr + 1)) == NPOS)
throw std::invalid_argument("bad config file syntax");
// are there other things to check for?
while (curr != NPOS) // here curr == { + 1
{
// so this moves curr to past the word...
@@ -104,7 +102,7 @@ ServerConfig ConfigParser::_parse_server(size_t *start)
LocationConfig ConfigParser::_parse_location(size_t *start)
LocationConfig ConfigParser::_parse_location(size_t *start) const
{
LocationConfig ret;
size_t curr = *start;
@@ -148,11 +146,9 @@ LocationConfig ConfigParser::_parse_location(size_t *start)
// should i be sending pointers or references?
void ConfigParser::_set_server_values(ServerConfig *server,
const std::string key, std::string value)
const std::string &key, std::string value) const
{
// should i be sending pointers or references?
value = _pre_set_val_check(key, value);
std::vector<std::string> tmp_val = ::split(value, ' ');
@@ -174,11 +170,11 @@ void ConfigParser::_set_server_values(ServerConfig *server,
}
}
else if (key == "listen" && size == 1 && server->host == ""
&& server->port == "") // QUESTION LUKE : C'est quoi cette condition ? Si listen est vide ? Je comprends pas trop.
&& server->port == "")
{
if (tmp_val[0].find_first_of(":") == NPOS)
{
if (!::isNumeric_btw(0, 65535, tmp_val[0]))
if (!::is_numeric_btw(0, 65535, tmp_val[0]))
throw std::invalid_argument("bad port number");
server->host = "0.0.0.0";
server->port = tmp_val[0];
@@ -192,10 +188,10 @@ void ConfigParser::_set_server_values(ServerConfig *server,
throw std::invalid_argument("bad host ip");
for (size_t i = 0; i < ip.size(); i++)
{
if (!::isNumeric_btw(0, 255, ip[i]))
if (!::is_numeric_btw(0, 255, ip[i]))
throw std::invalid_argument("bad host ip");
}
if (!::isNumeric_btw(0, 65535, tmp2[1]))
if (!::is_numeric_btw(0, 65535, tmp2[1]))
throw std::invalid_argument("bad port number");
server->host = tmp2[0];
server->port = tmp2[1];
@@ -206,13 +202,12 @@ void ConfigParser::_set_server_values(ServerConfig *server,
// remove trailing /
if (tmp_val[0][tmp_val[0].size() - 1] == '/')
tmp_val[0].erase(tmp_val[0].size() - 1, 1);
// tmp_val[0].push_back('/');
server->root = tmp_val[0];
}
else if (key == "client_body_limit" && size == 1
&& server->client_body_limit == 0)
{
if (!::isNumeric(tmp_val[0]))
if (!::is_numeric(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) )
@@ -229,7 +224,7 @@ void ConfigParser::_set_server_values(ServerConfig *server,
std::string path = tmp_val[size - 1];
for (size_t i = 0; i < size - 1; i++)
{
if (!(isNumeric_btw(400, 599, tmp_val[i])))
if (!(is_numeric_btw(400, 599, tmp_val[i])))
throw std::invalid_argument("invalid error code");
int status_code = std::strtoul(tmp_val[i].c_str(), NULL, 10);
if (server->error_pages.find(status_code) != server->error_pages.end())
@@ -242,11 +237,9 @@ void ConfigParser::_set_server_values(ServerConfig *server,
}
// should i be sending pointers or references?
void ConfigParser::_set_location_values(LocationConfig *location,
const std::string key, std::string value)
const std::string &key, std::string value) const
{
// should i be sending pointers or references?
value = _pre_set_val_check(key, value);
std::vector<std::string> tmp_val = ::split(value, ' ');
@@ -259,7 +252,6 @@ void ConfigParser::_set_location_values(LocationConfig *location,
// remove trailing /
if (tmp_val[0][tmp_val[0].size() - 1] == '/')
tmp_val[0].erase(tmp_val[0].size() - 1, 1);
// tmp_val[0].push_back('/');
location->root = tmp_val[0];
}
else if (key == "autoindex" && size == 1)
@@ -306,7 +298,6 @@ void ConfigParser::_set_location_values(LocationConfig *location,
}
else if (key == "upload_dir" && size == 1 && location->upload_dir == "")
{
// what checks to do?
// add trailing /
if (tmp_val[0][tmp_val[0].size() - 1] != '/')
tmp_val[0].push_back('/');

View File

@@ -3,21 +3,18 @@
#include "ConfigParser.hpp"
void ConfigParser::_post_processing(std::vector<ServerConfig> *servers)
void ConfigParser::_post_processing(std::vector<ServerConfig> *servers) const
{
std::vector<ServerConfig>::iterator it = servers->begin();
while (it != servers->end())
{
// host and port are Mandatory
if (it->host == "")
throw std::invalid_argument("Config file needs a host and port");
// root is mandatory
if (it->root == "")
throw std::invalid_argument("Config file needs a root");
// index is mandatory in Server
if (it->index.empty())
throw std::invalid_argument("Config file needs an Index");