From d368d820514a51b5cef26ec34bead83824c02120 Mon Sep 17 00:00:00 2001 From: ruben Date: Sat, 17 Jul 2021 17:26:29 +0200 Subject: [PATCH 1/4] more parse --- configs/server.conf | 1 + networking/Servers/parser_conf.cpp | 25 ++++++----- networking/Servers/parser_conf.hpp | 68 ++++++++++++------------------ networking/Servers/test.cpp | 3 +- networking/utils/utils.cpp | 12 ++++++ networking/utils/utils.hpp | 1 + 6 files changed, 57 insertions(+), 53 deletions(-) diff --git a/configs/server.conf b/configs/server.conf index a7c8f66..6fb0a17 100644 --- a/configs/server.conf +++ b/configs/server.conf @@ -3,6 +3,7 @@ server { listen 8080 80 9090; host 0.0.0.0 host localhost; + port 80 81 error_page 404 /auto_error.html; autoindex on; root html_pages/; diff --git a/networking/Servers/parser_conf.cpp b/networking/Servers/parser_conf.cpp index ca9b05f..ef561f9 100644 --- a/networking/Servers/parser_conf.cpp +++ b/networking/Servers/parser_conf.cpp @@ -4,26 +4,29 @@ #include "parser_conf.hpp" +using namespace std; + 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(' ')); + std::string value = s.substr(s.find(' ') + 1,s.find(';') - s.find(' ') - 1); 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()))); + server._server_name = split(value, ' '); + if (key == "port") + server._port = split_stoi(value, ' '); if (key == "host") - server._host = s.substr(s.find(' ') + 1, s.size()); + server._host = value; if (key == "error_page") - server._error_page = split(s.substr(s.find(' ') + 1, s.size()), ' '); + server._error_page = split(value, ' '); if (key == "auto_index") - server._auto_index = ft_stoi(s.substr(s.find(' ') + 1, s.size())); + server._auto_index = (value == "on") ? true : false; if (key == "root") - server._root = s.substr(s.find(' ') + 1, s.size()); + server._root = value; if (key == "index") - server._index = s.substr(s.find(' ') + 1, s.size()); + server._index = value; if (key == "key") - server._key = s.substr(s.find(' ') + 1, s.size()); + server._key = value; if (key == "value") server._value = s.substr(s.find(' ') + 1, s.size()); } @@ -41,7 +44,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._auto_index= (value == "on"); if (key == "client_body_size") location._client_body_size = ft_stoi(value); } @@ -93,7 +96,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 986dd2c..b3f6679 100644 --- a/networking/Servers/parser_conf.hpp +++ b/networking/Servers/parser_conf.hpp @@ -8,58 +8,44 @@ #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; +using namespace std; +typedef struct s_location { + string _address; //this one is replaced by the map-key + string _method; + string _root; + string _cgi; + bool _auto_index; + 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; - - std::map _location_map; -} t_server; +typedef struct s_server { + vector _server_name; + vector _port; + string _host; + vector _error_page; + bool _auto_index; + string _root; + string _index; + string _key; + string _value; + map _location_map; +} t_server; class parse_conf { private: - /* - 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; - - std::map _location_map; - */ - - std::vector _server; + vector _server; - 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..3a7d4d6 100644 --- a/networking/Servers/test.cpp +++ b/networking/Servers/test.cpp @@ -14,7 +14,8 @@ int main() - std::cout << "..." << ex.get_server_port(ex.get_server()[0])<< std::endl; + std::cout << "..." << ex.get_server_port(ex.get_server()[0])[0] << std::endl; + std::cout << "..." << ex.get_server_port(ex.get_server()[0])[1] << std::endl; std::cout << "..." << ex.get_server().size() << std::endl; /* diff --git a/networking/utils/utils.cpp b/networking/utils/utils.cpp index eae4cc5..8ed43d5 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; diff --git a/networking/utils/utils.hpp b/networking/utils/utils.hpp index 40f6379..d785b9d 100644 --- a/networking/utils/utils.hpp +++ b/networking/utils/utils.hpp @@ -9,4 +9,5 @@ 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); #endif //INC_13JUL_UTILS_HPP From f0693f920ca1271b34f32753b40ba6f17da68023 Mon Sep 17 00:00:00 2001 From: Ruben Date: Sat, 17 Jul 2021 20:34:24 +0200 Subject: [PATCH 2/4] line feed now by \n instead \t --- configs/server.conf | 4 +- configs/server2.conf | 17 +++++++ networking/Servers/parser_conf.cpp | 77 ++++++++++++++++++++---------- networking/Servers/parser_conf.hpp | 4 +- networking/Servers/test.cpp | 14 ++++-- networking/utils/utils.cpp | 8 ++++ networking/utils/utils.hpp | 1 + 7 files changed, 92 insertions(+), 33 deletions(-) create mode 100644 configs/server2.conf diff --git a/configs/server.conf b/configs/server.conf index 6fb0a17..1c40841 100644 --- a/configs/server.conf +++ b/configs/server.conf @@ -3,7 +3,8 @@ server { listen 8080 80 9090; host 0.0.0.0 host localhost; - port 80 81 + port 80 83 + port 81 82 error_page 404 /auto_error.html; autoindex on; root html_pages/; @@ -24,3 +25,4 @@ server { 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 ef561f9..af21a46 100644 --- a/networking/Servers/parser_conf.cpp +++ b/networking/Servers/parser_conf.cpp @@ -6,29 +6,47 @@ using namespace std; +void set_port(string &value, vector &port) +{ + vector elem = split_stoi(value, ' '); + for (vector::iterator it = begin(elem); it != end(elem); ++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(' ')); - std::string value = s.substr(s.find(' ') + 1,s.find(';') - s.find(' ') - 1); - if (key == "server_name") - server._server_name = split(value, ' '); - if (key == "port") - server._port = split_stoi(value, ' '); - if (key == "host") - server._host = value; - if (key == "error_page") - server._error_page = split(value, ' '); - if (key == "auto_index") - server._auto_index = (value == "on") ? true : false; - if (key == "root") - server._root = value; - if (key == "index") - server._index = value; - if (key == "key") - server._key = value; - 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) @@ -44,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._auto_index= (value == "on"); + location._autoindex= (value == "on"); if (key == "client_body_size") location._client_body_size = ft_stoi(value); } @@ -60,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); @@ -81,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 diff --git a/networking/Servers/parser_conf.hpp b/networking/Servers/parser_conf.hpp index b3f6679..e994029 100644 --- a/networking/Servers/parser_conf.hpp +++ b/networking/Servers/parser_conf.hpp @@ -14,7 +14,7 @@ typedef struct s_location { string _method; string _root; string _cgi; - bool _auto_index; + bool _autoindex; int _client_body_size; } t_location; @@ -23,7 +23,7 @@ typedef struct s_server { vector _port; string _host; vector _error_page; - bool _auto_index; + bool _autoindex; string _root; string _index; string _key; diff --git a/networking/Servers/test.cpp b/networking/Servers/test.cpp index 3a7d4d6..13e434d 100644 --- a/networking/Servers/test.cpp +++ b/networking/Servers/test.cpp @@ -6,17 +6,23 @@ 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); + for (int i = 0; i < ex.get_server().size(); ++i) { + vector p = ex.get_server_port(ex.get_server()[i]); + std::cout << "Server: " << i << std::endl; + for (vector::iterator it = begin(p); it != end(p); ++it) + std::cout << "port: " << *it << std::endl; + } - std::cout << "..." << ex.get_server_port(ex.get_server()[0])[0] << std::endl; - std::cout << "..." << ex.get_server_port(ex.get_server()[0])[1] << std::endl; - std::cout << "..." << ex.get_server().size() << std::endl; + + // std::cout << "..." << ex.get_server_port(ex.get_server()[0])[0] << std::endl; + // std::cout << "..." << ex.get_server_port(ex.get_server()[0])[1] << std::endl; /* std::cout << "EX.GETservname(): |" << ex.get_server_name() << std::endl; diff --git a/networking/utils/utils.cpp b/networking/utils/utils.cpp index 8ed43d5..e06a1ce 100644 --- a/networking/utils/utils.cpp +++ b/networking/utils/utils.cpp @@ -48,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 d785b9d..0b40f09 100644 --- a/networking/utils/utils.hpp +++ b/networking/utils/utils.hpp @@ -10,4 +10,5 @@ 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 From 2971cc2e8c735f4752e3b8465d644287c17ff4ea Mon Sep 17 00:00:00 2001 From: ruben Date: Mon, 19 Jul 2021 13:17:27 +0200 Subject: [PATCH 3/4] refactor for c++98 std --- networking/Servers/parser_conf.cpp | 4 +-- networking/Servers/test.cpp | 39 ++++++------------------------ 2 files changed, 9 insertions(+), 34 deletions(-) diff --git a/networking/Servers/parser_conf.cpp b/networking/Servers/parser_conf.cpp index af21a46..463f679 100644 --- a/networking/Servers/parser_conf.cpp +++ b/networking/Servers/parser_conf.cpp @@ -9,7 +9,7 @@ using namespace std; void set_port(string &value, vector &port) { vector elem = split_stoi(value, ' '); - for (vector::iterator it = begin(elem); it != end(elem); ++it) { + for (vector::iterator it = elem.begin(); it != elem.end(); ++it) { port.push_back(*it); } } @@ -43,7 +43,7 @@ void parse_conf::set_values_server(std::string s, t_server &server) else throw (s); } - catch (string n) + catch (string &n) { std::cout << "Unknown setting: " << n << std::endl; } diff --git a/networking/Servers/test.cpp b/networking/Servers/test.cpp index 13e434d..13b173e 100644 --- a/networking/Servers/test.cpp +++ b/networking/Servers/test.cpp @@ -10,46 +10,21 @@ int main() file.open(path); if(file.is_open()){ std::cout<< RED<< "###########CONFIG OPEN##########" << RESET << std::endl; - parse_conf ex(file); + parse_conf conf(file); - - for (int i = 0; i < ex.get_server().size(); ++i) { - vector p = ex.get_server_port(ex.get_server()[i]); + 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 = begin(p); it != end(p); ++it) + for (vector::iterator it = p.begin(); it != p.end(); ++it) std::cout << "port: " << *it << std::endl; } - - - // std::cout << "..." << ex.get_server_port(ex.get_server()[0])[0] << std::endl; - // std::cout << "..." << ex.get_server_port(ex.get_server()[0])[1] << 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; - */ - } 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; } From b8e5ef813cf3f84a8079ff5b610240b1f59810af Mon Sep 17 00:00:00 2001 From: sophielouisefeith <57356744+sophielouisefeith@users.noreply.github.com> Date: Thu, 22 Jul 2021 14:27:14 +0200 Subject: [PATCH 4/4] config parse --- configs/server.conf | 8 ++++++++ networking/Servers/test.cpp | 3 +++ 2 files changed, 11 insertions(+) diff --git a/configs/server.conf b/configs/server.conf index 1c40841..1c23f4f 100644 --- a/configs/server.conf +++ b/configs/server.conf @@ -26,3 +26,11 @@ server { } } +server { + location *.ico { + method SOPHIE; + root icons/; + } + +} + diff --git a/networking/Servers/test.cpp b/networking/Servers/test.cpp index 13b173e..c4d7acd 100644 --- a/networking/Servers/test.cpp +++ b/networking/Servers/test.cpp @@ -18,6 +18,9 @@ int main() 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;