Still trying to get it to compile

This commit is contained in:
Me
2022-07-29 23:29:19 +02:00
parent 3ff62f5ddc
commit 001ad6becf
8 changed files with 209 additions and 147 deletions

View File

@@ -90,9 +90,10 @@ ConfigParser & ConfigParser::operator=(const ConfigParser& rhs)
*/
std::vector<ServerConfig> ConfigParser::parse()
std::vector<ServerConfig> * ConfigParser::parse()
{
std::vector<ServerConfig> ret;
std::vector<ServerConfig> * ret = new std::vector<ServerConfig>();
// std::vector<ServerConfig> ret;
size_t start = 0;
size_t curr = _content.find_first_not_of(" \t\n", 0);
@@ -105,19 +106,19 @@ std::vector<ServerConfig> ConfigParser::parse()
// if not here do i need them elsewhere?
start = _content.find_first_not_of(" \t\n", curr);
curr = _content.find_first_of(" \t\n", start);
std::string key = _conent.substr(start, curr - start);
std::string key = _content.substr(start, curr - start);
if (key != "server")
throw std::invalid_argument("bad config file arguments");
// Server server = parse_server(&curr);
// ret->push_back(server);
// why not this?
ret.push_back(parse_server(&curr);
ret->push_back(_parse_server(&curr));
}
return (ret);
}
// might need new names for Prev and Curr, not super descriptive...
ServerConfig ConfigParser::parse_server(size_t *start)
ServerConfig ConfigParser::_parse_server(size_t *start)
{
ServerConfig ret;
size_t curr = _content.find_first_not_of(" \t\n", *start);
@@ -133,21 +134,24 @@ ServerConfig ConfigParser::parse_server(size_t *start)
// so this moves curr to past the word...
std::string key = _get_first_word(&curr);
// now curr is on space after 1st word.
switch (key)
if (key == "}")
{
case "}":
// why +1 curr is already after it no?
*start = _content.find_first_not_of(" \t\n" curr + 1);
break ;
case "location":
// this does assume we have locations in Server...
// could change the name but it's so clear...
ret.location.push_back(_parse_location(&curr));
default:
std::string values = _get_rest_of_line(&curr);
// curr now should be \n
// checking for ; in _set_value, check key and value
_set_server_values(&ret, key, values); // handles the throws
// why +1 curr is already after it no?
*start = _content.find_first_not_of(" \t\n", curr + 1);
break ;
}
else if (key == "location")
{
// this does assume we have locations in Server...
// could change the name but it's so clear...
ret.locations.push_back(_parse_location(&curr));
}
else
{
std::string values = _get_rest_of_line(&curr);
// curr now should be \n
// checking for ; in _set_value, check key and value
_set_server_values(&ret, key, values); // handles the throws
}
}
return (ret);
@@ -163,7 +167,7 @@ LocationConfig ConfigParser::_parse_location(size_t *start)
if (curr == std::string::npos || _content[curr] != '{')
throw std::invalid_argument("bad config file syntax");
size_t curr = _content.find_first_of(" \t\n", curr + 1);
curr = _content.find_first_of(" \t\n", curr + 1);
// if (curr == std::string::npos) // are there other things to check for?
// throw std::invalid_argument("bad config file syntax");
while (curr != std::string::npos)
@@ -171,17 +175,18 @@ LocationConfig ConfigParser::_parse_location(size_t *start)
// so this moves curr to past the word...
std::string key = _get_first_word(&curr);
// now curr is on space after 1st word.
switch (key)
if (key == "}")
{
case "}":
*start = curr;
break ;
default:
std::string values = _get_rest_of_line(&curr);
// curr now should be \n
// checking for ; in _set_value, check key and value
*start = curr;
break ;
}
else
{
std::string values = _get_rest_of_line(&curr);
// curr now should be \n
// checking for ; in _set_value, check key and value
_set_location_values(&ret, key, values); //handles the throws
_set_location_values(&ret, key, values); //handles the throws
}
}
return (ret);
@@ -223,94 +228,112 @@ void ConfigParser::_set_server_values(ServerConfig *server, \
// like call substr in split?
value = value.substr(0, i - 1);
std::vector<std::string> tmp_val = ::split(value, ' ');
std::vector<std::string> tmp_val = split(value, ' ');
if (tmp_val.size() == 1)
{
switch (key)
if (key == "server_name")
{
case "server_name":
server->server_name = tmp_val[0];
case "listen":
if (tmp_val[0].find_first_of(":") == std::string::npos)
{
// why not store as vector<int> [4] ?
server->host = "0.0.0.0";
server->value = tmp_val[0];
}
else
{
// maybe do this differently?
std::vector<std::string> tmp2 = split(tmp_val[0], ':');
// i might take issue with this, will see
if (server->host != "" && server->host != tmp2[0])
throw std::invalid_argument("bad listen");
server->host = tmp2[0];
server->port = tmp2[1];
}
case "root":
server->root = tmp_val[0];
case "autoindex":
server->autoindex = (tmp_val[0] == "on" ? true : false);
case "client_body_limit":
server->client_body_limit = atoi(tmp_val[0].c_str());
case "recv_timeout":
// what is tv_sec and do i need it?
server->server_name = tmp_val[0];
}
else if (key == "listen")
{
if (tmp_val[0].find_first_of(":") == std::string::npos)
{
// why not store as vector<int> [4] ?
server->host = "0.0.0.0";
server->port = tmp_val[0];
}
else
{
// maybe do this differently?
std::vector<std::string> tmp2 = split(tmp_val[0], ':');
// i might take issue with this, will see
if (server->host != "" && server->host != tmp2[0])
throw std::invalid_argument("bad listen");
server->host = tmp2[0];
server->port = tmp2[1];
}
}
else if (key == "root")
{
server->root = tmp_val[0];
}
else if (key == "autoindex")
{
server->autoindex = (tmp_val[0] == "on" ? true : false);
}
else if (key == "client_body_limit")
{
server->client_body_limit = atoi(tmp_val[0].c_str());
}
else if (key == "recv_timeout")
{
// what is tv_sec and do i need it?
// ok so i don't fully understand this part but ok, keep for now...
server->recv_timeout.tv_sec = atoi(tmp_val[0].c_str());
case "send_timeout":
server->send_timeout.tv_sec = atoi(tmp_val[0].c_str());
default :
throw std::invalid_argument("should only have 1 value");
server->recv_timeout.tv_sec = atoi(tmp_val[0].c_str());
}
else if (key == "send_timeout")
{
server->send_timeout.tv_sec = atoi(tmp_val[0].c_str());
}
else
{
throw std::invalid_argument("should only have 1 value");
// yea ok but it could also be something else like too many
// args
}
}
else if (tmp_val.size() > 1)
{
switch (key)
if (key == "index")
{
case "index":
// could run more tests on value content but meh...
for (unsigned long i = 0; i != tmp_val.size(); i++)
server->index.push_back(tmp_val[i]);
case "allow_methods":
// might do something different here
// like change how methods are stored?
for (unsigned long i = 0; i != tmp_val.size(); i++)
server->allow_methods.push_back(_str_to_method_type(tmp_val[i]));
case "return":
// could run more checks here too
// like tmp_val.size() must be 2
// and tmp_val[0] should be a number and tmp_val[1] a string?
server->redirect_status = atoi(tmp_val[0].c_str());
server->redirect_uri = tmp_val[1];
case "error_page":
// something more complicated?
// like make sure ints then 1 string?
std::string path = tmp_val[tmp_val.size() - 1];
for (unsigned long i = 0; i != tmp_val.size() - 1; i++)
{
int status_code = atoi(tmp_val[i].c_str());
// yea IDK i might not want to store this like that...
if (server->error_pages.find(status_code) != server->error_pages.end())
continue ;
server->error_pages[status_code] = path;
}
default :
throw std::invalid_argument("wrong number of values");
// could run more tests on value content but meh...
for (unsigned long i = 0; i != tmp_val.size(); i++)
server->index.push_back(tmp_val[i]);
}
else if (key == "allow_methods")
{
// might do something different here
// like change how methods are stored?
for (unsigned long i = 0; i != tmp_val.size(); i++)
server->allow_methods.push_back(_str_to_method_type(tmp_val[i]));
}
else if (key == "return")
{
// could run more checks here too
// like tmp_val.size() must be 2
// and tmp_val[0] should be a number and tmp_val[1] a string?
server->redirect_status = atoi(tmp_val[0].c_str());
server->redirect_uri = tmp_val[1];
}
else if (key == "error_page")
{
// something more complicated?
// like make sure ints then 1 string?
std::string path = tmp_val[tmp_val.size() - 1];
for (unsigned long i = 0; i != tmp_val.size() - 1; i++)
{
int status_code = atoi(tmp_val[i].c_str());
// yea IDK i might not want to store this like that...
if (server->error_pages.find(status_code) != server->error_pages.end())
continue ;
server->error_pages[status_code] = path;
}
}
else
{
throw std::invalid_argument("wrong number of values");
}
}
else
throw std::invalid_argument("missing value");
}
// again not sure i want an int ret
int ConfigParser::_set_location_values(LocationConfig *location, \
const std::string key, std::string value)
void ConfigParser::_set_location_values(LocationConfig *location, \
const std::string key, std::string value)
{
// check key for ;
// check values for ; at end and right number of words depending on key
@@ -341,37 +364,44 @@ int ConfigParser::_set_location_values(LocationConfig *location, \
if (tmp_val.size() == 1)
{
switch (key)
if (key == "root")
{
case "root":
location->root = tmp_val[0];
case "client_body_limit":
location->client_body_limit = atoi(tmp_val[0].c_str());
default :
throw std::invalid_argument("should only have 1 argument");
location->root = tmp_val[0];
}
else if (key == "client_body_limit")
{
location->client_body_limit = atoi(tmp_val[0].c_str());
}
else
{
throw std::invalid_argument("should only have 1 argument");
}
}
else if (tmp_val.size() > 1)
{
switch (key)
if (key == "index")
{
for (unsigned long i = 0; i != tmp_val.size(); i++)
location->index.push_back(tmp_val[i]);
}
else if (key == "allow_methods")
{
for (unsigned long i = 0; i != tmp_val.size(); i++)
location->allow_methods.push_back(_str_to_method_type(tmp_val[i]));
}
else if (key == "cgi_info")
{
case "index":
for (unsigned long i = 0; i != tmp_val.size(); i++)
location->index.push_back(tmp_val[i]);
case "allow_methods":
for (unsigned long i = 0; i != tmp_val.size(); i++)
location->allow_methods.push_back(_str_to_methodtype(tmp_val[i]));
case "cgi_info":
// ok wtf is all this even doing, figure that out
unsigned long i = value.find_first_of(" ");
if (i == std::string::npos)
throw std::invalid_argument("bad config file arguments");
// ok why an int now, we gotta be more consistent!
int j = value.find_first_not_of(" ", i);
location->cgi_info[value.substr(0, i)] = value.substr(j, value.length());
default :
unsigned long i = value.find_first_of(" ");
if (i == std::string::npos)
throw std::invalid_argument("bad config file arguments");
// ok why an int now, we gotta be more consistent!
int j = value.find_first_not_of(" ", i);
location->cgi_info[value.substr(0, i)] = value.substr(j, value.length());
}
else
{
throw std::invalid_argument("bad config file arguments");
}
}
else
@@ -412,23 +442,20 @@ std::string ConfigParser::_get_rest_of_line(size_t *curr)
}
MethodType ConfigParser::_str_to_methodtype(std::string str)
MethodType ConfigParser::_str_to_method_type(std::string str)
{
switch (str)
{
case ("GET"):
return GET;
case ("POST"):
return POST;
case ("DELETE"):
return DELETE;
default :
return INVALID;
}
if (str == "GET")
return GET;
else if (str == "POST")
return POST;
else if (str == "DELETE")
return DELETE;
return INVALID;
}
void ConfigParser::_print_content() const
{
std::cout << _content;