cleaned things up
This commit is contained in:
@@ -23,30 +23,25 @@ class ConfigParser {
|
||||
|
||||
public:
|
||||
|
||||
// canonical?
|
||||
|
||||
ConfigParser(const char* path); // a string?
|
||||
|
||||
// might not need this either, ask Luke
|
||||
~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?
|
||||
|
||||
|
||||
// i thought if it were an instance of this class you could call
|
||||
// private member functions from anywhere...
|
||||
void _print_content() const;
|
||||
void print_content() const;
|
||||
|
||||
|
||||
|
||||
private:
|
||||
std::string _content;
|
||||
|
||||
// explicit?
|
||||
// what exaclty does explicit do again?
|
||||
ConfigParser(); // might need a path as arg?
|
||||
// should this be in private since it always needs a path?
|
||||
// not sure i even need this...
|
||||
ConfigParser();
|
||||
|
||||
|
||||
ServerConfig _parse_server(size_t *start);
|
||||
@@ -57,6 +52,7 @@ private:
|
||||
void _set_location_values(LocationConfig *location, const std::string key, std::string value);
|
||||
|
||||
|
||||
/* Extra */
|
||||
std::string _pre_set_val_check(const std::string key, \
|
||||
const std::string value);
|
||||
|
||||
@@ -66,8 +62,8 @@ private:
|
||||
|
||||
/* Post Processing */
|
||||
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;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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
|
||||
{
|
||||
public:
|
||||
// canonic stuff?
|
||||
|
||||
std::string path; // /path and /path/ are fine
|
||||
// i remove trailing / systematically
|
||||
@@ -40,7 +39,7 @@ public:
|
||||
int redirect_status; // only in location
|
||||
std::string redirect_uri; // only 1 per location
|
||||
|
||||
void print_all() // const?
|
||||
void print_all() const
|
||||
{
|
||||
std::cout << "\nPRINTING A LOCATION\n";
|
||||
|
||||
@@ -58,7 +57,6 @@ public:
|
||||
std::cout << "------\n";
|
||||
}
|
||||
|
||||
// works a lot better than using a compare function...
|
||||
bool operator<(const LocationConfig& rhs) const
|
||||
{
|
||||
int comp_lhs = 0;
|
||||
@@ -81,7 +79,7 @@ public:
|
||||
if (rhs.path[rhs.path.find_last_of("/") + 1] != '\0')
|
||||
++comp_rhs;
|
||||
|
||||
return (comp_lhs < comp_rhs); // right comparison ? not <= ?
|
||||
return (comp_lhs < comp_rhs);
|
||||
};
|
||||
|
||||
bool operator==(const LocationConfig& rhs) const
|
||||
|
||||
@@ -14,7 +14,6 @@
|
||||
class ServerConfig
|
||||
{
|
||||
public:
|
||||
// do i need some canonic stuff?
|
||||
|
||||
std::vector<std::string> server_name;
|
||||
// we could shove default in here if we wanted to...
|
||||
@@ -35,7 +34,7 @@ public:
|
||||
std::vector<LocationConfig> locations;
|
||||
|
||||
|
||||
void print_all() // const?
|
||||
void print_all() const
|
||||
{
|
||||
std::cout << "PRINTING A FULL SERVER CONFIG\n\n";
|
||||
|
||||
@@ -46,14 +45,14 @@ public:
|
||||
for (size_t i = 0; i < index.size(); i++)
|
||||
std::cout << index[i] << " ";
|
||||
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++)
|
||||
std::cout << it->first << "--" << it->second << " ";
|
||||
// for (size_t i = 0; i < error_pages.size(); i++)
|
||||
// std::cout << error_pages->first << "--" << error_pages->second << " ";
|
||||
|
||||
// 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();
|
||||
|
||||
std::cout << "client_body_limit: " << client_body_limit << '\n';
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
|
||||
// should i be sending & references?
|
||||
// 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)
|
||||
{
|
||||
|
||||
@@ -14,7 +14,7 @@ std::string ConfigParser::_pre_set_val_check(const std::string key, \
|
||||
|
||||
// std::cout << "pre check\n";
|
||||
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)
|
||||
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 ;
|
||||
if (i == NPOS || (value.find_last_not_of(" \n")) != i \
|
||||
|| 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));
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -2,16 +2,14 @@
|
||||
#include "ConfigParser.hpp"
|
||||
|
||||
// Default
|
||||
// possibly remove...
|
||||
ConfigParser::ConfigParser()
|
||||
{
|
||||
std::cout << "Default Constructor\n";
|
||||
// don't use yet, you have no idea what the defaults are
|
||||
}
|
||||
|
||||
ConfigParser::ConfigParser(const char* path)
|
||||
{
|
||||
// std::cout << "Param Constructor\n";
|
||||
|
||||
std::ifstream file;
|
||||
std::string buf;
|
||||
size_t comment;
|
||||
@@ -43,21 +41,12 @@ ConfigParser::ConfigParser(const char* path)
|
||||
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?
|
||||
}
|
||||
|
||||
/*
|
||||
ConfigParser & ConfigParser::operator=(const ConfigParser& rhs)
|
||||
{
|
||||
if (this == rhs) // * & ?
|
||||
return (*this); // * ?
|
||||
|
||||
// make some stuff equal
|
||||
return (*this);
|
||||
}
|
||||
*/
|
||||
|
||||
// const?
|
||||
std::vector<ServerConfig> * ConfigParser::parse()
|
||||
@@ -78,7 +67,7 @@ std::vector<ServerConfig> * ConfigParser::parse()
|
||||
throw std::invalid_argument("empty config file");
|
||||
std::string key = _content.substr(start, curr - start);
|
||||
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));
|
||||
}
|
||||
_post_processing(ret);
|
||||
@@ -92,7 +81,7 @@ ServerConfig ConfigParser::_parse_server(size_t *start)
|
||||
|
||||
ret.client_body_limit = 0;
|
||||
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)
|
||||
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);
|
||||
if (ret.path[0] != '/')
|
||||
throw std::invalid_argument("Location path require a leading /");
|
||||
// ret.path.insert(0, "/");
|
||||
// in theory now curr should be right after the "path"
|
||||
|
||||
curr = _content.find_first_not_of(" \t\n", 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)
|
||||
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");
|
||||
else if (key == "server_name" && server->server_name.empty())
|
||||
{
|
||||
for (std::vector<std::string>::iterator it = server->server_name.begin(); \
|
||||
it < server->server_name.end(); it++)
|
||||
for (size_t i = 0; i < size; i++)
|
||||
{
|
||||
if (it->compare(tmp_val[0]) == 0)
|
||||
throw std::invalid_argument("server_name already exists");
|
||||
for (std::vector<std::string>::const_iterator it = server->server_name.begin();
|
||||
it < server->server_name.end(); it++)
|
||||
{
|
||||
if (it->compare(tmp_val[i]) == 0)
|
||||
throw std::invalid_argument("server_name already exists");
|
||||
}
|
||||
server->server_name.push_back(tmp_val[i]);
|
||||
}
|
||||
server->server_name.push_back(tmp_val[0]);
|
||||
}
|
||||
else if (key == "listen" && size == 1 && server->host == "" \
|
||||
else if (key == "listen" && size == 1 && server->host == ""
|
||||
&& server->port == "")
|
||||
{
|
||||
if (tmp_val[0].find_first_of(":") == NPOS)
|
||||
@@ -225,7 +216,7 @@ void ConfigParser::_set_server_values(ServerConfig *server, \
|
||||
// tmp_val[0].push_back('/');
|
||||
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)
|
||||
{
|
||||
if (!::isNumeric(tmp_val[0]))
|
||||
@@ -234,13 +225,13 @@ void ConfigParser::_set_server_values(ServerConfig *server, \
|
||||
}
|
||||
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]);
|
||||
}
|
||||
else if (key == "error_page")
|
||||
{
|
||||
std::string path = tmp_val[tmp_val.size() - 1];
|
||||
for (unsigned long i = 0; i != tmp_val.size() - 1; i++)
|
||||
std::string path = tmp_val[size - 1];
|
||||
for (size_t i = 0; i < size() - 1; i++)
|
||||
{
|
||||
if (!(isNumeric_btw(400, 599, tmp_val[i])))
|
||||
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);
|
||||
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]);
|
||||
}
|
||||
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]);
|
||||
if (m == UNKNOWN)
|
||||
@@ -294,23 +285,22 @@ void ConfigParser::_set_location_values(LocationConfig *location, \
|
||||
}
|
||||
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] == '.')
|
||||
throw std::invalid_argument("cgi_ext should not have a leading '.'");
|
||||
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 == "")
|
||||
{
|
||||
if (tmp_val.size() != 2)
|
||||
if (size != 2)
|
||||
throw std::invalid_argument("wrong number of values");
|
||||
if (tmp_val[0] != "301" && tmp_val[0] != "302"
|
||||
&& tmp_val[0] != "303" && tmp_val[0] != "307"
|
||||
&& tmp_val[0] != "308")
|
||||
throw std::invalid_argument("bad redirect status");
|
||||
// std::cout << tmp_val[1] << '\n';
|
||||
if (tmp_val[1].compare(0, 7, "http://")
|
||||
&& tmp_val[1].compare(0, 8, "https://"))
|
||||
throw std::invalid_argument("bad redirect uri");
|
||||
|
||||
@@ -79,7 +79,7 @@ void ConfigParser::_post_processing(std::vector<ServerConfig> *servers)
|
||||
}
|
||||
|
||||
// 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();
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ int main(int ac, char **av)
|
||||
|
||||
ConfigParser configParser(config.c_str());
|
||||
|
||||
configParser._print_content();
|
||||
configParser.print_content();
|
||||
|
||||
// i don't love that servers has to be a pointer...
|
||||
std::vector<ServerConfig>* servers = configParser.parse();
|
||||
|
||||
Reference in New Issue
Block a user