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

@@ -19,14 +19,13 @@ HEADERS = Webserv.hpp \
LocationConfig.hpp \ LocationConfig.hpp \
Client.hpp \ Client.hpp \
Server.hpp \ Server.hpp \
MethodType.hpp \
DEPENDENCIES = $(HEADERS:%=$(HEADERS_D)/%) DEPENDENCIES = $(HEADERS:%=$(HEADERS_D)/%)
SRCS = main.cpp \ SRCS = main.cpp \
Webserv.cpp \ Webserv.cpp \
ConfigParser.cpp \ ConfigParser.cpp \
ServerConfig.cpp \
LocationConfig.cpp \
DIR_OBJS = builds DIR_OBJS = builds

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

View File

@@ -14,6 +14,7 @@
# define CONFIGPARSER_HPP # define CONFIGPARSER_HPP
# include "Webserv.hpp" // easier to just do this? # include "Webserv.hpp" // easier to just do this?
# include "ServerConfig.hpp"
// add includes properly // add includes properly
@@ -22,6 +23,8 @@
#define MAX_URI_SIZE 64 #define MAX_URI_SIZE 64
#define BSIZE 1024 #define BSIZE 1024
/*
// this can't be here...
enum MethodType enum MethodType
{ {
GET, GET,
@@ -29,7 +32,7 @@ enum MethodType
DELETE, DELETE,
INVALID, INVALID,
}; };
*/
class ConfigParser { class ConfigParser {
@@ -45,11 +48,14 @@ public:
// ConfigParser & operator=(const ConfigParser& rhs); // ConfigParser & operator=(const ConfigParser& rhs);
// void parse(); // return void cuz throw exceptions. // void parse(); // return void cuz throw exceptions.
//std::vector<Server> * parse(); // const? std::vector<ServerConfig> * parse(); // const?
std::vector<ServerConfig> parse(); // const? // std::vector<ServerConfig> parse(); // const?
// other parses? // other parses?
// i thought if it were an instance of this class you could call
// private member functions from anywhere...
void _print_content() const;
private: private:
std::string _content; std::string _content;
@@ -76,7 +82,6 @@ private:
static MethodType _str_to_method_type(std::string str); static MethodType _str_to_method_type(std::string str);
// just for testing purposes // just for testing purposes
void _print_content() const;
}; };

15
srcs/MethodType.hpp Normal file
View File

@@ -0,0 +1,15 @@
#ifndef METHODTYPE_HPP
# define METHODTYPE_HPP
enum MethodType
{
GET,
POST,
DELETE,
INVALID,
};
#endif

View File

@@ -15,6 +15,8 @@
// add includes properly... // add includes properly...
# include "Webserv.hpp" # include "Webserv.hpp"
# include "MethodType.hpp"
//# include "ConfigParser.hpp"
# include "LocationConfig.hpp" # include "LocationConfig.hpp"
// a class that's all public? just so we have options? // a class that's all public? just so we have options?
@@ -63,13 +65,16 @@ public:
std::cout << "Server_name: " << server_name << '\n'; std::cout << "Server_name: " << server_name << '\n';
std::cout << "root: " << root << '\n'; std::cout << "root: " << root << '\n';
std::cout << "index: "; std::cout << "index: ";
for (int 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 (int i = 0; i < error_pages.size(); i++) for(std::map<int, std::string>::iterator it = error_pages.begin(); \
std::cout << error_pages.first << "--" << error_pages.second << " "; 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 << "\nallow_methods: "; std::cout << "\nallow_methods: ";
for (int i = 0; i < allow_methods.size(); i++) for (size_t i = 0; i < allow_methods.size(); i++)
std::cout << allow_methods[i] << " "; std::cout << allow_methods[i] << " ";
std::cout << "\nskiping Locations for now...\n"; std::cout << "\nskiping Locations for now...\n";
std::cout << "also skiping send_timeout and recv\n"; std::cout << "also skiping send_timeout and recv\n";

View File

@@ -28,6 +28,8 @@ Webserv::Webserv()
} */ } */
// we'll come back to this
/*
Webserv::Webserv(std::vector<ServerConfig>* servers) Webserv::Webserv(std::vector<ServerConfig>* servers)
: _servers(servers) : _servers(servers)
{ {
@@ -42,6 +44,7 @@ Webserv::Webserv(std::vector<ServerConfig>* servers)
throw std::runtime_error("Epoll init"); throw std::runtime_error("Epoll init");
} }
} }
*/
Webserv::~Webserv() Webserv::~Webserv()
{ {

View File

@@ -25,7 +25,15 @@
# include "Client.hpp" # include "Client.hpp"
# include "Server.hpp" # include "Server.hpp"
# include "ConfigParser.hpp"
# include "ServerConfig.hpp"
# include "LocationConfig.hpp"
# include "MethodType.hpp"
# include <csignal> // signal # include <csignal> // signal
# include <unistd.h>
# include <string.h>
# include <stdio.h>
# include <stdlib.h>
# define BUFSIZE 8192 # define BUFSIZE 8192
# define TIMEOUT 3000 # define TIMEOUT 3000
@@ -63,7 +71,7 @@ class Webserv
// Webserv(Webserv const &src); // Webserv(Webserv const &src);
// what should it take as arg, *, &, ? // what should it take as arg, *, &, ?
Webserv(std::vector<ServerConfig>& servers); // Webserv(std::vector<ServerConfig>& servers);
~Webserv(); ~Webserv();
// Webserv &operator=(Webserv const &rhs); // Webserv &operator=(Webserv const &rhs);

View File

@@ -20,10 +20,10 @@ int main(int ac, char **av)
configParser._print_content(); configParser._print_content();
std::vector<ServerConfig> servers = configParser.parse(); // std::vector<ServerConfig>* servers = configParser.parse();
for (int i = 0; i < server.size(); i++) // for (size_t i = 0; i < servers->size(); i++)
servers[i].print_all(); // servers[i]->print_all();
// Webserv serv(configParser.parse()); // Webserv serv(configParser.parse());
// is this better or worse than using // is this better or worse than using