diff --git a/includes/webserv.hpp b/includes/webserv.hpp index e4be719..5c963a0 100644 --- a/includes/webserv.hpp +++ b/includes/webserv.hpp @@ -51,7 +51,7 @@ typedef std::vector< JSONNode * > JSONList; void *ft_memset(void *b, int c, size_t len); bool isInt(string str); -std::vector< string > split(string str, char delim); +std::vector< string > split(string str, string delim); listen_t get_listen_t(string listen); listen_t get_listen_t(string ip, int port); string getMime(string path); diff --git a/srcs/load/Env.cpp b/srcs/load/Env.cpp index e70023c..8a17d8e 100644 --- a/srcs/load/Env.cpp +++ b/srcs/load/Env.cpp @@ -66,8 +66,7 @@ void Env::cycle(void) { void Env::set_fds(void) { for (std::vector< Master * >::iterator it = _masters.begin(); it < _masters.end(); it++) { - if (*it) - (*it)->set_fds(); + (*it)->set_fds(); } } /*|==========| diff --git a/srcs/load/Route.cpp b/srcs/load/Route.cpp index c07f005..6529e31 100644 --- a/srcs/load/Route.cpp +++ b/srcs/load/Route.cpp @@ -104,8 +104,8 @@ string Route::correctUri(string uri) { std::vector< string >::iterator it2; ret << "./" << _root; - std::vector< string > loc_split = split(_location, '/'); - std::vector< string > uri_split = split(uri, '/'); + std::vector< string > loc_split = split(_location, "/"); + std::vector< string > uri_split = split(uri, "/"); it2 = uri_split.begin(); for (it = loc_split.begin(); it < loc_split.end(); it++) { while (it2 < uri_split.end() && *it2 == "") diff --git a/srcs/load/Server.cpp b/srcs/load/Server.cpp index 22a76b2..df7aed9 100644 --- a/srcs/load/Server.cpp +++ b/srcs/load/Server.cpp @@ -91,11 +91,11 @@ std::vector< Master * > Server::get_sockets(JSONNode *server) { */ Route *Server::choose_route(string uri) { - std::vector< string > req = split(uri, '/'); + std::vector< string > req = split(uri, "/"); std::vector< string > root; for (std::map< string, Route * >::iterator rit = _routes.begin(); rit != _routes.end(); rit++) { - root = split((*rit).first, '/'); + root = split((*rit).first, "/"); std::vector< string >::iterator root_it = root.begin(); for (std::vector< string >::iterator it = req.begin(); it < req.end(); it++) { diff --git a/srcs/sock/Client.cpp b/srcs/sock/Client.cpp index bf3ced6..9e7117c 100644 --- a/srcs/sock/Client.cpp +++ b/srcs/sock/Client.cpp @@ -15,7 +15,7 @@ bool Client::getRequest(string paquet) { cout << "|===|paquet|===>" << paquet << "|===||\n"; if (paquet.length() < 1) // HTTPS? return false; - std::vector< string > lines = split(paquet, '\n'); + std::vector< string > lines = split(paquet, "\n"); long chunk_len = (_content.length() > 0 && _request["Transfer-Encoding:"].size() && _request["Transfer-Encoding:"].at(0) == "chunked") @@ -48,17 +48,17 @@ bool Client::getRequest(string paquet) { } bool Client::parseHeader() { - std::vector< string > lines = split(_header, '\n'); + std::vector< string > lines = split(_header, "\r\n"); std::vector< string > line; if (lines.size() > 0) { for (std::vector< string >::iterator it = lines.begin() + 1; it < lines.end(); it++) { - line = split(*it, ' '); + line = split(*it, " "); _request[line.at(0)] = std::vector< string >(line.begin() + 1, line.end()); } } - std::vector< string > method = split(lines.at(0), ' '); + std::vector< string > method = split(lines.at(0), " "); if (method.at(0) == "POST" && _request.find("Content-Length:") == _request.end() && _request.find("Transfer-Encoding:") == _request.end()) diff --git a/srcs/sock/Master.cpp b/srcs/sock/Master.cpp index 72b6684..b79925c 100644 --- a/srcs/sock/Master.cpp +++ b/srcs/sock/Master.cpp @@ -106,8 +106,7 @@ Server *Master::choose_server(Env *env, string host) { std::vector< string > ip_listen; std::vector< string > ip_required; - cout << "Requested: " << _listen.ip << ":" << _listen.port << "\n"; - ip_required = split(_listen.ip, '.'); + ip_required = split(_listen.ip, "."); for (std::vector< Server * >::iterator sit = env->_servers.begin(); sit < env->_servers.end(); sit++) { @@ -122,7 +121,7 @@ Server *Master::choose_server(Env *env, string host) { continue; } bool is_inrange = true; - ip_listen = split((*it).ip, '.'); + ip_listen = split((*it).ip, "."); std::vector< string >::iterator r = ip_required.begin(); for (std::vector< string >::iterator l = ip_listen.end(); l >= ip_listen.begin(); --l) { @@ -134,20 +133,16 @@ Server *Master::choose_server(Env *env, string host) { } } if (exact.size() == 0) { - cout << "Inrange: "; for (std::vector< Server * >::iterator sit = inrange.begin(); sit < inrange.end(); sit++) { - cout << "- " << (*sit)->getName() << "\n"; - if (host == ((*sit)->getName() + "\r")) + if (host == (*sit)->getName()) return *sit; } return inrange.front(); } else { - cout << "Exact: \n"; for (std::vector< Server * >::iterator sit = exact.begin(); sit < exact.end(); sit++) { - cout << "- " << (*sit)->getName() << "\n"; - if (host == ((*sit)->getName() + "\r")) + if (host == (*sit)->getName()) return *sit; } return exact.front(); diff --git a/srcs/tools.cpp b/srcs/tools.cpp index 422018a..43eddd0 100644 --- a/srcs/tools.cpp +++ b/srcs/tools.cpp @@ -18,13 +18,18 @@ bool isInt(string str) { return true; } -std::vector< string > split(string str, char delim) { - std::vector< std::string > tokens; +std::vector< string > split(string str, string delim) { + string temp(str); string token; - std::stringstream ss(str); + size_t pos; + std::vector< std::string > tokens; - while (getline(ss, token, delim)) + while ((pos = temp.find(delim)) != string::npos) { + token = temp.substr(0, pos); tokens.push_back(token); + temp.erase(0, pos + delim.length()); + } + tokens.push_back(temp); return tokens; }