From fae8641138f7b1f3f1002cc551c94e12c4a35cbf Mon Sep 17 00:00:00 2001 From: nicolas-arnaud Date: Mon, 7 Nov 2022 02:01:58 +0100 Subject: [PATCH] improved route finding and uri correction --- includes/webserv.hpp | 3 +++ srcs/load/Route.cpp | 6 +++++- srcs/load/Server.cpp | 22 ++++++++++++++++++---- srcs/load/Socket.cpp | 5 ++++- srcs/tools.cpp | 10 ++++++++++ 5 files changed, 40 insertions(+), 6 deletions(-) diff --git a/includes/webserv.hpp b/includes/webserv.hpp index 24ccbdf..9bad44a 100644 --- a/includes/webserv.hpp +++ b/includes/webserv.hpp @@ -11,6 +11,7 @@ #include #include +#include #include #include #include @@ -19,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -41,6 +43,7 @@ typedef std::vector JSONList; void *ft_memset(void *b, int c, size_t len); bool isInt(string str); +std::vector split(string str, char delim); #include "Nodes.hpp" #include "Token.hpp" diff --git a/srcs/load/Route.cpp b/srcs/load/Route.cpp index 5bdad8c..d72d930 100644 --- a/srcs/load/Route.cpp +++ b/srcs/load/Route.cpp @@ -56,7 +56,11 @@ string Route::correctUri(string uri) { string root = _root; cout << "Correcting request: " << uri - << "with root: " << root << "\n"; + << " with root: " << root << "\n"; + int i = 0; + while (uri.at(i) == '/') + i++; + uri.erase(0, i); while ((slash_pos = root.find('/')) > 0) { ret << root.substr(0, slash_pos); root.erase(0, slash_pos); diff --git a/srcs/load/Server.cpp b/srcs/load/Server.cpp index 02ac698..8bcd6d4 100644 --- a/srcs/load/Server.cpp +++ b/srcs/load/Server.cpp @@ -42,10 +42,24 @@ void Server::refresh(void) { } Route *Server::get_route(string uri) { - for (std::map::iterator it = _routes.begin(); - it != _routes.end(); it++) { - if (uri == (*it).first) - return (*it).second; + cout << uri << "\n"; + std::vector req = split(uri, '/'); + std::vector root; + for (std::map::iterator rit = _routes.begin(); + rit != _routes.end(); rit++) { + root = split((*rit).first, '/'); + std::vector::iterator root_it = root.begin(); + + for (std::vector::iterator it = req.begin(); + it < req.end(); it++) { + if (*it == "") + continue ; + cout << *it << " - " << *root_it << "\n"; + if (*it != *(root_it++)) + break; + if (root_it == root.end()) + return ((*rit).second); + } } return this; } diff --git a/srcs/load/Socket.cpp b/srcs/load/Socket.cpp index 1fd0828..1a844a8 100644 --- a/srcs/load/Socket.cpp +++ b/srcs/load/Socket.cpp @@ -108,10 +108,13 @@ void Socket::refresh() { } void Socket::answer(int fd, string request) { + string uri = split(request, ' ').at(1); + cout << uri << "\n"; + cout << request << "\n|===|===|===|\n"; std::stringstream answer; answer << "HTTP/1.1 200 OK\nContent-Type: text/plain\nContent-Length: "; - string uri = "docs/"; + Route *route = _server->get_route(uri); answer << route->getAutoindex(uri); cout << answer.str() << "\n|===|===|===|\n"; diff --git a/srcs/tools.cpp b/srcs/tools.cpp index bb74698..9bd58c2 100644 --- a/srcs/tools.cpp +++ b/srcs/tools.cpp @@ -17,3 +17,13 @@ bool isInt(string str) { return false; return true; } + +std::vector split(string str, char delim) { + std::vector tokens; + std::string token; + std::stringstream ss(str); + while (getline(ss, token, delim)){ + tokens.push_back(token); + } + return tokens; +}