cleaned things up

This commit is contained in:
Eric LAZO
2022-08-15 19:40:21 +02:00
parent aa03880601
commit 19da59ecf8
7 changed files with 44 additions and 61 deletions

View File

@@ -23,30 +23,25 @@ class ConfigParser {
public: public:
// canonical?
ConfigParser(const char* path); // a string? ConfigParser(const char* path); // a string?
// might not need this either, ask Luke
~ConfigParser(); ~ConfigParser();
// ideally i wouldn't have one cuz it makes no sense, when would i use it?
// ConfigParser & operator=(const ConfigParser& rhs);
std::vector<ServerConfig> * parse(); // const? std::vector<ServerConfig> * parse(); // const?
// std::vector<ServerConfig> parse(); // const?
// i thought if it were an instance of this class you could call // i thought if it were an instance of this class you could call
// private member functions from anywhere... // private member functions from anywhere...
void _print_content() const; void print_content() const;
private: private:
std::string _content; std::string _content;
// explicit? // not sure i even need this...
// what exaclty does explicit do again? ConfigParser();
ConfigParser(); // might need a path as arg?
// should this be in private since it always needs a path?
ServerConfig _parse_server(size_t *start); ServerConfig _parse_server(size_t *start);
@@ -57,6 +52,7 @@ private:
void _set_location_values(LocationConfig *location, 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); const std::string value);
@@ -66,7 +62,7 @@ private:
/* Post Processing */ /* Post Processing */
void _post_processing(std::vector<ServerConfig> *servers); void _post_processing(std::vector<ServerConfig> *servers);
bool _find_root_path_location(std::vector<LocationConfig> locations); // const? bool _find_root_path_location(std::vector<LocationConfig> locations) const;
}; };

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */ /* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/23 16:08:00 by me #+# #+# */ /* Created: 2022/07/23 16:08:00 by me #+# #+# */
/* Updated: 2022/08/14 22:14:06 by erlazo ### ########.fr */ /* Updated: 2022/08/15 19:37:34 by erlazo ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -25,7 +25,6 @@
class LocationConfig class LocationConfig
{ {
public: public:
// canonic stuff?
std::string path; // /path and /path/ are fine std::string path; // /path and /path/ are fine
// i remove trailing / systematically // i remove trailing / systematically
@@ -40,7 +39,7 @@ public:
int redirect_status; // only in location int redirect_status; // only in location
std::string redirect_uri; // only 1 per location std::string redirect_uri; // only 1 per location
void print_all() // const? void print_all() const
{ {
std::cout << "\nPRINTING A LOCATION\n"; std::cout << "\nPRINTING A LOCATION\n";
@@ -58,7 +57,6 @@ public:
std::cout << "------\n"; std::cout << "------\n";
} }
// works a lot better than using a compare function...
bool operator<(const LocationConfig& rhs) const bool operator<(const LocationConfig& rhs) const
{ {
int comp_lhs = 0; int comp_lhs = 0;
@@ -81,7 +79,7 @@ public:
if (rhs.path[rhs.path.find_last_of("/") + 1] != '\0') if (rhs.path[rhs.path.find_last_of("/") + 1] != '\0')
++comp_rhs; ++comp_rhs;
return (comp_lhs < comp_rhs); // right comparison ? not <= ? return (comp_lhs < comp_rhs);
}; };
bool operator==(const LocationConfig& rhs) const bool operator==(const LocationConfig& rhs) const

View File

@@ -14,7 +14,6 @@
class ServerConfig class ServerConfig
{ {
public: public:
// do i need some canonic stuff?
std::vector<std::string> server_name; std::vector<std::string> server_name;
// we could shove default in here if we wanted to... // we could shove default in here if we wanted to...
@@ -35,7 +34,7 @@ public:
std::vector<LocationConfig> locations; std::vector<LocationConfig> locations;
void print_all() // const? void print_all() const
{ {
std::cout << "PRINTING A FULL SERVER CONFIG\n\n"; std::cout << "PRINTING A FULL SERVER CONFIG\n\n";
@@ -46,14 +45,14 @@ public:
for (size_t i = 0; i < index.size(); i++) for (size_t i = 0; i < index.size(); i++)
std::cout << index[i] << " "; std::cout << index[i] << " ";
std::cout << "\nerror_pages: "; std::cout << "\nerror_pages: ";
for(std::map<int, std::string>::iterator it = error_pages.begin(); \ for(std::map<int, std::string>::const_iterator it = error_pages.begin(); \
it != error_pages.end(); it++) it != error_pages.end(); it++)
std::cout << it->first << "--" << it->second << " "; std::cout << it->first << "--" << it->second << " ";
// for (size_t i = 0; i < error_pages.size(); i++) // for (size_t i = 0; i < error_pages.size(); i++)
// std::cout << error_pages->first << "--" << error_pages->second << " "; // std::cout << error_pages->first << "--" << error_pages->second << " ";
// std::cout << "skiping Locations for now...\n"; // std::cout << "skiping Locations for now...\n";
for (std::vector<LocationConfig>::iterator it = locations.begin(); it < locations.end(); it++) for (std::vector<LocationConfig>::const_iterator it = locations.begin(); it < locations.end(); it++)
it->print_all(); it->print_all();
std::cout << "client_body_limit: " << client_body_limit << '\n'; std::cout << "client_body_limit: " << client_body_limit << '\n';

View File

@@ -5,7 +5,7 @@
// should i be sending & references? // should i be sending & references?
// const? // const?
std::string ConfigParser::_pre_set_val_check(const std::string key, \ std::string ConfigParser::_pre_set_val_check(const std::string key,
const std::string value) const std::string value)
{ {
@@ -14,7 +14,7 @@ std::string ConfigParser::_pre_set_val_check(const std::string key, \
// std::cout << "pre check\n"; // std::cout << "pre check\n";
if (key.find_first_of(";") != NPOS) if (key.find_first_of(";") != NPOS)
throw std::invalid_argument("bad config file arguments 2"); throw std::invalid_argument("bad config file arguments");
if (value.find_first_of("\t") != NPOS) if (value.find_first_of("\t") != NPOS)
throw std::invalid_argument("why would you put tabs between values"); throw std::invalid_argument("why would you put tabs between values");
@@ -26,7 +26,7 @@ std::string ConfigParser::_pre_set_val_check(const std::string key, \
// in theory value_find_last_of should find the only ; // in theory value_find_last_of should find the only ;
if (i == NPOS || (value.find_last_not_of(" \n")) != i \ if (i == NPOS || (value.find_last_not_of(" \n")) != i \
|| value.compare(";") == 0) || value.compare(";") == 0)
throw std::invalid_argument("bad config file arguments 4"); throw std::invalid_argument("bad config file arguments");
return (value.substr(0, i)); return (value.substr(0, i));
@@ -63,7 +63,7 @@ std::string ConfigParser::_get_rest_of_line(size_t *curr)
} }
void ConfigParser::_print_content() const void ConfigParser::print_content() const
{ {
std::cout << _content; std::cout << _content;
} }

View File

@@ -2,16 +2,14 @@
#include "ConfigParser.hpp" #include "ConfigParser.hpp"
// Default // Default
// possibly remove...
ConfigParser::ConfigParser() ConfigParser::ConfigParser()
{ {
std::cout << "Default Constructor\n"; std::cout << "Default Constructor\n";
// don't use yet, you have no idea what the defaults are
} }
ConfigParser::ConfigParser(const char* path) ConfigParser::ConfigParser(const char* path)
{ {
// std::cout << "Param Constructor\n";
std::ifstream file; std::ifstream file;
std::string buf; std::string buf;
size_t comment; size_t comment;
@@ -43,21 +41,12 @@ ConfigParser::ConfigParser(const char* path)
throw std::invalid_argument("failed to open config"); throw std::invalid_argument("failed to open config");
} }
// might not need this...
ConfigParser::~ConfigParser() ConfigParser::~ConfigParser()
{ {
// do i need to destroy anything, won't it handle itself? // do i need to destroy anything, won't it handle itself?
} }
/*
ConfigParser & ConfigParser::operator=(const ConfigParser& rhs)
{
if (this == rhs) // * & ?
return (*this); // * ?
// make some stuff equal
return (*this);
}
*/
// const? // const?
std::vector<ServerConfig> * ConfigParser::parse() std::vector<ServerConfig> * ConfigParser::parse()
@@ -78,7 +67,7 @@ std::vector<ServerConfig> * ConfigParser::parse()
throw std::invalid_argument("empty config file"); throw std::invalid_argument("empty config file");
std::string key = _content.substr(start, curr - start); std::string key = _content.substr(start, curr - start);
if (key != "server") if (key != "server")
throw std::invalid_argument("bad config file arguments 1"); throw std::invalid_argument("bad config file arguments");
ret->push_back(_parse_server(&curr)); ret->push_back(_parse_server(&curr));
} }
_post_processing(ret); _post_processing(ret);
@@ -92,7 +81,7 @@ ServerConfig ConfigParser::_parse_server(size_t *start)
ret.client_body_limit = 0; ret.client_body_limit = 0;
if (curr == NPOS || _content[curr] != '{') if (curr == NPOS || _content[curr] != '{')
throw std::invalid_argument("bad config file syntax 1"); throw std::invalid_argument("bad config file syntax");
if ((curr = _content.find_first_of(" \t\n", curr + 1)) == NPOS) if ((curr = _content.find_first_of(" \t\n", curr + 1)) == NPOS)
throw std::invalid_argument("bad config file syntax"); throw std::invalid_argument("bad config file syntax");
@@ -135,13 +124,12 @@ LocationConfig ConfigParser::_parse_location(size_t *start)
ret.path = _get_first_word(&curr); ret.path = _get_first_word(&curr);
if (ret.path[0] != '/') if (ret.path[0] != '/')
throw std::invalid_argument("Location path require a leading /"); throw std::invalid_argument("Location path require a leading /");
// ret.path.insert(0, "/");
// in theory now curr should be right after the "path" // in theory now curr should be right after the "path"
curr = _content.find_first_not_of(" \t\n", curr); curr = _content.find_first_not_of(" \t\n", curr);
if (curr == NPOS || _content[curr] != '{') if (curr == NPOS || _content[curr] != '{')
throw std::invalid_argument("bad config file syntax 2"); throw std::invalid_argument("bad config file syntax");
if ((curr = _content.find_first_of(" \t\n", curr + 1)) == NPOS) if ((curr = _content.find_first_of(" \t\n", curr + 1)) == NPOS)
throw std::invalid_argument("bad config file syntax"); throw std::invalid_argument("bad config file syntax");
@@ -181,15 +169,18 @@ void ConfigParser::_set_server_values(ServerConfig *server, \
throw std::invalid_argument("missing value"); throw std::invalid_argument("missing value");
else if (key == "server_name" && server->server_name.empty()) else if (key == "server_name" && server->server_name.empty())
{ {
for (std::vector<std::string>::iterator it = server->server_name.begin(); \ for (size_t i = 0; i < size; i++)
{
for (std::vector<std::string>::const_iterator it = server->server_name.begin();
it < server->server_name.end(); it++) it < server->server_name.end(); it++)
{ {
if (it->compare(tmp_val[0]) == 0) if (it->compare(tmp_val[i]) == 0)
throw std::invalid_argument("server_name already exists"); throw std::invalid_argument("server_name already exists");
} }
server->server_name.push_back(tmp_val[0]); server->server_name.push_back(tmp_val[i]);
} }
else if (key == "listen" && size == 1 && server->host == "" \ }
else if (key == "listen" && size == 1 && server->host == ""
&& server->port == "") && server->port == "")
{ {
if (tmp_val[0].find_first_of(":") == NPOS) if (tmp_val[0].find_first_of(":") == NPOS)
@@ -225,7 +216,7 @@ void ConfigParser::_set_server_values(ServerConfig *server, \
// tmp_val[0].push_back('/'); // tmp_val[0].push_back('/');
server->root = tmp_val[0]; server->root = tmp_val[0];
} }
else if (key == "client_body_limit" && size == 1 \ else if (key == "client_body_limit" && size == 1
&& server->client_body_limit == 0) && server->client_body_limit == 0)
{ {
if (!::isNumeric(tmp_val[0])) if (!::isNumeric(tmp_val[0]))
@@ -234,13 +225,13 @@ void ConfigParser::_set_server_values(ServerConfig *server, \
} }
else if (key == "index") else if (key == "index")
{ {
for (unsigned long i = 0; i != tmp_val.size(); i++) for (size_t i = 0; i != tmp_val.size(); i++)
server->index.push_back(tmp_val[i]); server->index.push_back(tmp_val[i]);
} }
else if (key == "error_page") else if (key == "error_page")
{ {
std::string path = tmp_val[tmp_val.size() - 1]; std::string path = tmp_val[size - 1];
for (unsigned long i = 0; i != tmp_val.size() - 1; i++) for (size_t i = 0; i < size() - 1; i++)
{ {
if (!(isNumeric_btw(400, 599, tmp_val[i]))) if (!(isNumeric_btw(400, 599, tmp_val[i])))
throw std::invalid_argument("invalid error code"); throw std::invalid_argument("invalid error code");
@@ -279,12 +270,12 @@ void ConfigParser::_set_location_values(LocationConfig *location, \
location->autoindex = (tmp_val[0] == "on" ? true : false); location->autoindex = (tmp_val[0] == "on" ? true : false);
else if (key == "index") else if (key == "index")
{ {
for (unsigned long i = 0; i != tmp_val.size(); i++) for (size_t i = 0; i < size; i++)
location->index.push_back(tmp_val[i]); location->index.push_back(tmp_val[i]);
} }
else if (key == "allow_methods" && location->allow_methods == 0) else if (key == "allow_methods" && location->allow_methods == 0)
{ {
for (unsigned long i = 0; i != tmp_val.size(); i++) for (size_t i = 0; i < size; i++)
{ {
http_method m = ::str_to_http_method(tmp_val[i]); http_method m = ::str_to_http_method(tmp_val[i]);
if (m == UNKNOWN) if (m == UNKNOWN)
@@ -294,23 +285,22 @@ void ConfigParser::_set_location_values(LocationConfig *location, \
} }
else if (key == "cgi_ext") else if (key == "cgi_ext")
{ {
for (size_t i = 0; i < tmp_val.size(); i++) for (size_t i = 0; i < size; i++)
{ {
if (tmp_val[i][0] == '.') if (tmp_val[i][0] == '.')
throw std::invalid_argument("cgi_ext should not have a leading '.'"); throw std::invalid_argument("cgi_ext should not have a leading '.'");
location->cgi_ext.push_back(tmp_val[i]); location->cgi_ext.push_back(tmp_val[i]);
} }
} }
else if (key == "redirect" && location->redirect_status == 0 \ else if (key == "redirect" && location->redirect_status == 0
&& location->redirect_uri == "") && location->redirect_uri == "")
{ {
if (tmp_val.size() != 2) if (size != 2)
throw std::invalid_argument("wrong number of values"); throw std::invalid_argument("wrong number of values");
if (tmp_val[0] != "301" && tmp_val[0] != "302" if (tmp_val[0] != "301" && tmp_val[0] != "302"
&& tmp_val[0] != "303" && tmp_val[0] != "307" && tmp_val[0] != "303" && tmp_val[0] != "307"
&& tmp_val[0] != "308") && tmp_val[0] != "308")
throw std::invalid_argument("bad redirect status"); throw std::invalid_argument("bad redirect status");
// std::cout << tmp_val[1] << '\n';
if (tmp_val[1].compare(0, 7, "http://") if (tmp_val[1].compare(0, 7, "http://")
&& tmp_val[1].compare(0, 8, "https://")) && tmp_val[1].compare(0, 8, "https://"))
throw std::invalid_argument("bad redirect uri"); throw std::invalid_argument("bad redirect uri");

View File

@@ -79,7 +79,7 @@ void ConfigParser::_post_processing(std::vector<ServerConfig> *servers)
} }
// const? // const?
bool ConfigParser::_find_root_path_location(std::vector<LocationConfig> locations) bool ConfigParser::_find_root_path_location(std::vector<LocationConfig> locations) const
{ {
std::vector<LocationConfig>::const_iterator it = locations.begin(); std::vector<LocationConfig>::const_iterator it = locations.begin();

View File

@@ -19,7 +19,7 @@ int main(int ac, char **av)
ConfigParser configParser(config.c_str()); ConfigParser configParser(config.c_str());
configParser._print_content(); configParser.print_content();
// i don't love that servers has to be a pointer... // i don't love that servers has to be a pointer...
std::vector<ServerConfig>* servers = configParser.parse(); std::vector<ServerConfig>* servers = configParser.parse();