diff --git a/configs/server.conf b/configs/server.conf index a7c8f66..1c23f4f 100644 --- a/configs/server.conf +++ b/configs/server.conf @@ -3,6 +3,8 @@ server { listen 8080 80 9090; host 0.0.0.0 host localhost; + port 80 83 + port 81 82 error_page 404 /auto_error.html; autoindex on; root html_pages/; @@ -23,3 +25,12 @@ server { root icons/; } } + +server { + location *.ico { + method SOPHIE; + root icons/; + } + +} + diff --git a/configs/server2.conf b/configs/server2.conf new file mode 100644 index 0000000..400da30 --- /dev/null +++ b/configs/server2.conf @@ -0,0 +1,17 @@ +server { + port 11 12 + port 13 +} + + + +server { + port 24 25 +} + +server { + port 36 + port 37 + something + port 38 +} diff --git a/networking/Servers/parser_conf.cpp b/networking/Servers/parser_conf.cpp index 69e8e8d..e6debcd 100644 --- a/networking/Servers/parser_conf.cpp +++ b/networking/Servers/parser_conf.cpp @@ -4,28 +4,49 @@ #include "parser_conf.hpp" +using namespace std; + +void set_port(string &value, vector &port) +{ + vector elem = split_stoi(value, ' '); + for (vector::iterator it = elem.begin(); it != elem.end(); ++it) { + port.push_back(*it); + } +} + void parse_conf::set_values_server(std::string s, t_server &server) { // this probably needs a better name than 'key' - std::string key = s.substr(0, s.find(' ')); - if (key == "server_name") - server._server_name = s.substr(s.find(' ') + 1, s.size()); - if (key == "listen") - server._port = ft_stoi((s.substr(s.find(' ') + 1, s.size()))); - if (key == "host") - server._host = s.substr(s.find(' ') + 1, s.size()); - if (key == "error_page") - server._error_page = split(s.substr(s.find(' ') + 1, s.size()), ' '); - if (key == "auto_index") - server._auto_index = ft_stoi(s.substr(s.find(' ') + 1, s.size())); - if (key == "root") - server._root = s.substr(s.find(' ') + 1, s.size()); - if (key == "index") - server._index = s.substr(s.find(' ') + 1, s.size()); - if (key == "key") - server._key = s.substr(s.find(' ') + 1, s.size()); - if (key == "value") - server._value = s.substr(s.find(' ') + 1, s.size()); + //server._port = split_stoi(value, ' '); + try + { + std::string key = s.substr(0, s.find(' ')); + std::string value = s.substr(s.find(' ') + 1, s.find(';') - s.find(' ') - 1); + if (key == "server_name") + server._server_name = split(value, ' '); + else if (key == "port") + set_port(value, server._port); + else if (key == "host") + server._host = value; + else if (key == "error_page") + server._error_page = split(value, ' '); + else if (key == "autoindex") + server._autoindex = (value == "on") ? true : false; + else if (key == "root") + server._root = value; + else if (key == "index") + server._index = value; + else if (key == "key") + server._key = value; + else if (key == "value") + server._value = s.substr(s.find(' ') + 1, s.size()); + else + throw (s); + } + catch (string &n) + { + std::cout << "Unknown setting: " << n << std::endl; + } } // This function gets the whole line and a struct (t_location) @@ -41,7 +62,7 @@ void parse_conf::set_values_location(std::string s, t_location &location) if (key == "cgi") location._cgi = value; if (key == "autoindex") - location._autoindex= value; + location._autoindex= (value == "on"); if (key == "client_body_size") location._client_body_size = ft_stoi(value); } @@ -57,12 +78,17 @@ void parse_conf::set_values_location(std::string s, t_location &location) bool is_acc = false; int server_count = 0; std::string map_key; - while(std::getline(file, line, '\t')) + while(std::getline(file, line, '\n')) { - std::string key = line.substr(0, line.find(" ")); - line = line.substr(0, line.find('\n')); - if (line.empty()) + line = trim_whitespace_front(line); + if (line.empty()) { + std::cout << "empty line" << std::endl; continue; + } + std::string key = line.substr(0, line.find(' ')); + //line = line.substr(0, line.find('\n')); + //std::cout << "l|" << line << "|"<< std::endl; + //std::cout << "k|" << key << "|"<< std::endl; if (key == "server") { server_count++; _server.resize(server_count); @@ -78,8 +104,10 @@ void parse_conf::set_values_location(std::string s, t_location &location) } std::cout << line << std::endl; - if (!is_acc) + if (!is_acc) { + cout << "setting server, sc: " << server_count << endl; set_values_server(line, _server[server_count - 1]); + } if (is_acc) { // send the map with the appropriate key set_values_location(line, _server[server_count - 1]._location_map[map_key]); // for some reason it is not working yet @@ -94,7 +122,7 @@ const std::vector &parse_conf::get_server() const } // gaat dit werken? -int parse_conf::get_server_port(const t_server &server) { +const std::vector &parse_conf::get_server_port(const t_server &server) { return server._port; } diff --git a/networking/Servers/parser_conf.hpp b/networking/Servers/parser_conf.hpp index 719d88e..6770ee0 100644 --- a/networking/Servers/parser_conf.hpp +++ b/networking/Servers/parser_conf.hpp @@ -8,34 +8,37 @@ #include "../../all_libs.hpp" #include "../../http.hpp" -typedef struct s_location { - std::string _address; //this one is replaced by the map-key - std::string _method; - std::string _root; - std::string _cgi; - std::string _autoindex; - int _client_body_size; -} t_location; - -typedef struct s_server { - std::string _server_name; - int _port; - std::string _host; - std::vector _error_page; - int _auto_index; - std::string _root; - std::string _index; - std::string _key; - std::string _value; +using namespace std; +typedef struct s_location { + string _address; //this one is replaced by the map-key + string _method; + string _root; + string _cgi; + bool _autoindex; + int _client_body_size; +} t_location; - std::map _location_map; -} t_server; +typedef struct s_server { + vector _server_name; + vector _port; + string _host; + vector _error_page; + bool _autoindex; + string _root; + string _index; + string _key; + string _value; + map _location_map; +} t_server; class parse_conf { private: +<<<<<<< HEAD + vector _server; +======= /* std::string _server_name; int _port; @@ -52,15 +55,16 @@ class parse_conf //private std::vector _server; +>>>>>>> sophie_request - void set_values_server(std::string s, t_server &server); - void set_values_location(std::string s, t_location &location); + void set_values_server(string s, t_server &server); + void set_values_location(string s, t_location &location); public: - parse_conf(std::ifstream &file); + parse_conf(ifstream &file); /* GETTERS */ - int get_server_port(const t_server &server); - const std::vector &get_server() const; + const vector &get_server_port(const t_server &server); + const vector &get_server() const; /* const std::string &get_server_name() const; int get_port() const; diff --git a/networking/Servers/test.cpp b/networking/Servers/test.cpp index f659c38..c4d7acd 100644 --- a/networking/Servers/test.cpp +++ b/networking/Servers/test.cpp @@ -6,43 +6,28 @@ int main() { std::cout<< RED<< "MAIN" << RESET << std::endl; std::ifstream file; - const char *path = "configs/server.conf"; + const char *path = "configs/server2.conf"; file.open(path); if(file.is_open()){ std::cout<< RED<< "###########CONFIG OPEN##########" << RESET << std::endl; - parse_conf ex(file); + parse_conf conf(file); - - - std::cout << "..." << ex.get_server_port(ex.get_server()[0])<< std::endl; - std::cout << "..." << ex.get_server().size() << std::endl; - - /* - std::cout << "EX.GETservname(): |" << ex.get_server_name() << std::endl; - std::cout << BLUE << "----------" << RESET << std::endl; - std::cout << "sn " << ex.get_server_name() < map_locations = ex.get_location(); - - std::cout << "loc_map[\"/\"]._method: " << map_locations["/"]._method << std::endl; - std::cout << "loc_map[*.error_image.png]._method: " << map_locations["*.error_image.png"]._method << std::endl; - std::cout << "loc_map[*.ico]._method: " << map_locations["*.ico"]._method << std::endl; - */ + for (int i = 0; i < conf.get_server().size(); ++i) { + vector p = conf.get_server_port(conf.get_server()[i]); + std::cout << "Server: " << i << std::endl; + for (vector::iterator it = p.begin(); it != p.end(); ++it) + std::cout << "port: " << *it << std::endl; + } + vector a = conf.get_server(); + std::cout<< a[0]._server_name[0] << std::endl; } else std::cout << RED << " ERROR no configfile" << RESET << std::endl; file.close(); + + //HDE::parser_config_open r(); - HTTP::select_server t; + HTTP::select_server t; } diff --git a/networking/utils/utils.cpp b/networking/utils/utils.cpp index eae4cc5..e06a1ce 100644 --- a/networking/utils/utils.cpp +++ b/networking/utils/utils.cpp @@ -29,6 +29,18 @@ std::vector split(const std::string &s, char delim) return elems; } +std::vector split_stoi(std::string s, char delim) +{ + std::vector elems; + std::stringstream ss(s); + std::string item; + while (getline(ss, item, delim)) + { + elems.push_back(ft_stoi(item)); + } + return elems; +} + int ft_stoi(std::string s) { int i; @@ -36,3 +48,11 @@ int ft_stoi(std::string s) return i; } +std::string trim_whitespace_front(const std::string &s) +{ + int i = 0; + int len = s.length(); + while(s[i] == ' ' || s[i] == '\t') + i++; + return (s.substr(i, len - i)); +} diff --git a/networking/utils/utils.hpp b/networking/utils/utils.hpp index 40f6379..0b40f09 100644 --- a/networking/utils/utils.hpp +++ b/networking/utils/utils.hpp @@ -9,4 +9,6 @@ int ft_stoi(std::string s); void db(std::string s); void dbe(std::string s); std::vector split(const std::string &s, char delim); +std::vector split_stoi(std::string s, char delim); +std::string trim_whitespace_front(const std::string &s); #endif //INC_13JUL_UTILS_HPP