Browse Source

changed split to use string delimiter

master
nicolas-arnaud 2 years ago
parent
commit
d9f6f1d49c
  1. 2
      includes/webserv.hpp
  2. 1
      srcs/load/Env.cpp
  3. 4
      srcs/load/Route.cpp
  4. 4
      srcs/load/Server.cpp
  5. 8
      srcs/sock/Client.cpp
  6. 13
      srcs/sock/Master.cpp
  7. 13
      srcs/tools.cpp

2
includes/webserv.hpp

@ -51,7 +51,7 @@ typedef std::vector< JSONNode * > JSONList;
void *ft_memset(void *b, int c, size_t len); void *ft_memset(void *b, int c, size_t len);
bool isInt(string str); 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 listen);
listen_t get_listen_t(string ip, int port); listen_t get_listen_t(string ip, int port);
string getMime(string path); string getMime(string path);

1
srcs/load/Env.cpp

@ -66,7 +66,6 @@ void Env::cycle(void) {
void Env::set_fds(void) { void Env::set_fds(void) {
for (std::vector< Master * >::iterator it = _masters.begin(); for (std::vector< Master * >::iterator it = _masters.begin();
it < _masters.end(); it++) { it < _masters.end(); it++) {
if (*it)
(*it)->set_fds(); (*it)->set_fds();
} }
} }

4
srcs/load/Route.cpp

@ -104,8 +104,8 @@ string Route::correctUri(string uri) {
std::vector< string >::iterator it2; std::vector< string >::iterator it2;
ret << "./" << _root; ret << "./" << _root;
std::vector< string > loc_split = split(_location, '/'); std::vector< string > loc_split = split(_location, "/");
std::vector< string > uri_split = split(uri, '/'); std::vector< string > uri_split = split(uri, "/");
it2 = uri_split.begin(); it2 = uri_split.begin();
for (it = loc_split.begin(); it < loc_split.end(); it++) { for (it = loc_split.begin(); it < loc_split.end(); it++) {
while (it2 < uri_split.end() && *it2 == "") while (it2 < uri_split.end() && *it2 == "")

4
srcs/load/Server.cpp

@ -91,11 +91,11 @@ std::vector< Master * > Server::get_sockets(JSONNode *server) {
*/ */
Route *Server::choose_route(string uri) { Route *Server::choose_route(string uri) {
std::vector< string > req = split(uri, '/'); std::vector< string > req = split(uri, "/");
std::vector< string > root; std::vector< string > root;
for (std::map< string, Route * >::iterator rit = _routes.begin(); for (std::map< string, Route * >::iterator rit = _routes.begin();
rit != _routes.end(); rit++) { rit != _routes.end(); rit++) {
root = split((*rit).first, '/'); root = split((*rit).first, "/");
std::vector< string >::iterator root_it = root.begin(); std::vector< string >::iterator root_it = root.begin();
for (std::vector< string >::iterator it = req.begin(); it < req.end(); for (std::vector< string >::iterator it = req.begin(); it < req.end();
it++) { it++) {

8
srcs/sock/Client.cpp

@ -15,7 +15,7 @@ bool Client::getRequest(string paquet) {
cout << "|===|paquet|===>" << paquet << "|===||\n"; cout << "|===|paquet|===>" << paquet << "|===||\n";
if (paquet.length() < 1) // HTTPS? if (paquet.length() < 1) // HTTPS?
return false; return false;
std::vector< string > lines = split(paquet, '\n'); std::vector< string > lines = split(paquet, "\n");
long chunk_len = long chunk_len =
(_content.length() > 0 && _request["Transfer-Encoding:"].size() && (_content.length() > 0 && _request["Transfer-Encoding:"].size() &&
_request["Transfer-Encoding:"].at(0) == "chunked") _request["Transfer-Encoding:"].at(0) == "chunked")
@ -48,17 +48,17 @@ bool Client::getRequest(string paquet) {
} }
bool Client::parseHeader() { bool Client::parseHeader() {
std::vector< string > lines = split(_header, '\n'); std::vector< string > lines = split(_header, "\r\n");
std::vector< string > line; std::vector< string > line;
if (lines.size() > 0) { if (lines.size() > 0) {
for (std::vector< string >::iterator it = lines.begin() + 1; for (std::vector< string >::iterator it = lines.begin() + 1;
it < lines.end(); it++) { it < lines.end(); it++) {
line = split(*it, ' '); line = split(*it, " ");
_request[line.at(0)] = _request[line.at(0)] =
std::vector< string >(line.begin() + 1, line.end()); 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" && if (method.at(0) == "POST" &&
_request.find("Content-Length:") == _request.end() && _request.find("Content-Length:") == _request.end() &&
_request.find("Transfer-Encoding:") == _request.end()) _request.find("Transfer-Encoding:") == _request.end())

13
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_listen;
std::vector< string > ip_required; 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(); for (std::vector< Server * >::iterator sit = env->_servers.begin();
sit < env->_servers.end(); sit++) { sit < env->_servers.end(); sit++) {
@ -122,7 +121,7 @@ Server *Master::choose_server(Env *env, string host) {
continue; continue;
} }
bool is_inrange = true; bool is_inrange = true;
ip_listen = split((*it).ip, '.'); ip_listen = split((*it).ip, ".");
std::vector< string >::iterator r = ip_required.begin(); std::vector< string >::iterator r = ip_required.begin();
for (std::vector< string >::iterator l = ip_listen.end(); for (std::vector< string >::iterator l = ip_listen.end();
l >= ip_listen.begin(); --l) { l >= ip_listen.begin(); --l) {
@ -134,20 +133,16 @@ Server *Master::choose_server(Env *env, string host) {
} }
} }
if (exact.size() == 0) { if (exact.size() == 0) {
cout << "Inrange: ";
for (std::vector< Server * >::iterator sit = inrange.begin(); for (std::vector< Server * >::iterator sit = inrange.begin();
sit < inrange.end(); sit++) { sit < inrange.end(); sit++) {
cout << "- " << (*sit)->getName() << "\n"; if (host == (*sit)->getName())
if (host == ((*sit)->getName() + "\r"))
return *sit; return *sit;
} }
return inrange.front(); return inrange.front();
} else { } else {
cout << "Exact: \n";
for (std::vector< Server * >::iterator sit = exact.begin(); for (std::vector< Server * >::iterator sit = exact.begin();
sit < exact.end(); sit++) { sit < exact.end(); sit++) {
cout << "- " << (*sit)->getName() << "\n"; if (host == (*sit)->getName())
if (host == ((*sit)->getName() + "\r"))
return *sit; return *sit;
} }
return exact.front(); return exact.front();

13
srcs/tools.cpp

@ -18,13 +18,18 @@ bool isInt(string str) {
return true; return true;
} }
std::vector< string > split(string str, char delim) { std::vector< string > split(string str, string delim) {
std::vector< std::string > tokens; string temp(str);
string token; 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); tokens.push_back(token);
temp.erase(0, pos + delim.length());
}
tokens.push_back(temp);
return tokens; return tokens;
} }

Loading…
Cancel
Save