diff --git a/includes/config/URL.hpp b/includes/config/URL.hpp index 245b669..5af2cc8 100644 --- a/includes/config/URL.hpp +++ b/includes/config/URL.hpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/22 12:17:48 by adjoly #+# #+# */ -/* Updated: 2025/04/30 14:38:03 by adjoly ### ########.fr */ +/* Updated: 2025/05/04 12:21:03 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,18 +18,29 @@ #include #include +#include + class URL { public: - URL(const std::string &url) : _full_url(url) { parse(); } - - bool operator==(const URL &other) const { - return comparePathSegments(other); - } + URL(const std::string &url) : _full_url(url) { _parse(); } bool operator<(const URL &other) const { return _full_url < other._full_url; } + int countMatchingSegments(const URL &url) const { + if (_path_segments.size() == 0 || url._path_segments.size() == 0) + return 0; + int i = 0; + + auto u = url._path_segments; + for (auto it = u.begin(); it != u.end() && *it == _path_segments[i]; + it++, i++) + ; + + return i; + } + std::vector getSegments(void) const { return _path_segments; } std::string getFullUrl(void) const { return _full_url; } @@ -37,7 +48,7 @@ class URL { std::string getPort(void) const { return _port; } private: - void parse() { + void _parse() { size_t scheme_pos = _full_url.find("://"); size_t path_start = 0; size_t query_start = _full_url.find('?'); @@ -55,12 +66,12 @@ class URL { if (path_start != std::string::npos) { std::string path = _full_url.substr(path_start, query_start - path_start); - splitPath(path, _path_segments); + _splitPath(path, _path_segments); } } else { if (path_start != std::string::npos) { std::string path = _full_url.substr(path_start); - splitPath(path, _path_segments); + _splitPath(path, _path_segments); } } @@ -69,8 +80,8 @@ class URL { } } - void splitPath(const std::string &path, - std::vector &segments) const { + void _splitPath(const std::string &path, + std::vector &segments) const { std::stringstream ss(path); std::string segment; while (std::getline(ss, segment, '/')) { @@ -80,17 +91,6 @@ class URL { } } - bool comparePathSegments(const URL &other) const { - size_t min_size = - std::min(_path_segments.size(), other._path_segments.size()); - for (size_t i = 0; i < min_size; ++i) { - if (_path_segments[i] != other._path_segments[i]) { - return false; - } - } - return true; - } - std::string _full_url; std::vector _path_segments; std::string _query_string; diff --git a/src/config/Server.cpp b/src/config/Server.cpp index 0171082..2f40823 100644 --- a/src/config/Server.cpp +++ b/src/config/Server.cpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/24 15:10:07 by adjoly #+# #+# */ -/* Updated: 2025/05/01 16:30:28 by adjoly ### ########.fr */ +/* Updated: 2025/05/04 12:48:10 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,7 +20,8 @@ using namespace webserv::config; -Server::Server(toml::ANode *node) : _routes(not_nullptr), _server_names(not_nullptr), _table(node) { +Server::Server(toml::ANode *node) + : _routes(not_nullptr), _server_names(not_nullptr), _table(node) { bool found; if (_table == not_nullptr) @@ -31,7 +32,7 @@ Server::Server(toml::ANode *node) : _routes(not_nullptr), _server_names(not_null if (host != not_nullptr) { _host = *static_cast(host); } else { - //delete _table; + // delete _table; throw std::runtime_error( "no host specified - please specify one in server.host"); } @@ -81,7 +82,8 @@ Server::Server(toml::ANode *node) : _routes(not_nullptr), _server_names(not_null if (_routes->find(URL(it->first)) != _routes->end()) { continue; } - _routes->insert(std::make_pair(URL(it->first), new Route(it->second))); + _routes->insert( + std::make_pair(URL(it->first), new Route(it->second))); } } // delete _table; @@ -128,19 +130,24 @@ bool Server::isServerName(const std::string &server_name) { } Route *Server::whatRoute(const URL &url) { + std::map::iterator ret = _routes->end(); + + int i = 0; + for (auto it = prange(_routes)) { - if (it->first == url) { - return it->second; + if (i < it->first.countMatchingSegments(url)) { + ret = it; } } - std::map::iterator it = _routes->find(URL("/")); - if (it != _routes->end()) { - return it->second; - } - return not_nullptr; + + if (ret == _routes->end()) + return _routes->find(URL("/"))->second; + + return ret->second; } -Server::Server(toml::ANode *node, void *) : _routes(not_nullptr), _server_names(not_nullptr), _table(node) { +Server::Server(toml::ANode *node, void *) + : _routes(not_nullptr), _server_names(not_nullptr), _table(node) { bool found; if (_table == not_nullptr) @@ -152,8 +159,7 @@ Server::Server(toml::ANode *node, void *) : _routes(not_nullptr), _server_names( _host = *static_cast(host); } else { delete _table; - throw std::runtime_error( - "no host specified - please specify one"); + throw std::runtime_error("no host specified - please specify one"); } // port parsing void *port = accessValue("port", toml::INT, _table, _log); @@ -161,8 +167,7 @@ Server::Server(toml::ANode *node, void *) : _routes(not_nullptr), _server_names( _port = *static_cast(port); } else { delete _table; - throw std::runtime_error( - "no port specified - please specify one"); + throw std::runtime_error("no port specified - please specify one"); } // error_pages parsing @@ -184,7 +189,8 @@ Server::Server(toml::ANode *node, void *) : _routes(not_nullptr), _server_names( if (_routes->find(URL(it->first)) != _routes->end()) { continue; } - _routes->insert(std::make_pair(URL(it->first), new Route(it->second))); + _routes->insert( + std::make_pair(URL(it->first), new Route(it->second))); } } // delete _table; diff --git a/src/requests_handling/requestImplementation/Get.cpp b/src/requests_handling/requestImplementation/Get.cpp index 99aed32..21ba46d 100644 --- a/src/requests_handling/requestImplementation/Get.cpp +++ b/src/requests_handling/requestImplementation/Get.cpp @@ -6,15 +6,15 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/30 09:40:16 by adjoly #+# #+# */ -/* Updated: 2025/05/02 14:52:24 by mmoussou ### ########.fr */ +/* Updated: 2025/05/04 12:25:43 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ -#include -#include -#include #include +#include #include +#include +#include #include @@ -91,35 +91,33 @@ Response Get::execute(void) { this->_target = this->_route->getRootDir() + this->_target; try { - if (isDirectory(this->_target)) - { - if (!access((this->_target + this->_route->getIndex()).c_str(), R_OK)) - { + if (isDirectory(this->_target)) { + if (!access((this->_target + this->_route->getIndex()).c_str(), + R_OK)) { this->_target = this->_target + this->_route->getIndex(); - std::ifstream file(this->_target.c_str(), std::ios::binary); - std::streampos file_start = file.tellg(); - response.setBody(std::string((std::istreambuf_iterator(file)), - std::istreambuf_iterator())); - std::stringstream length; - length << (file.tellg() - file_start); - response.addHeader("Content-Length", length.str()); + std::ifstream file(this->_target.c_str(), std::ios::binary); + std::streampos file_start = file.tellg(); + response.setBody( + std::string((std::istreambuf_iterator(file)), + std::istreambuf_iterator())); + std::stringstream length; + length << (file.tellg() - file_start); + response.addHeader("Content-Length", length.str()); - response.setProtocol(this->_protocol); - response.setStatusCode(200); - response.addHeader("Content-Type", - http::Mime::getType(this->_target)); - } - else if (this->_route->getDirList()) - { + response.setProtocol(this->_protocol); + response.setStatusCode(200); + response.addHeader("Content-Type", + http::Mime::getType(this->_target)); + } else if (this->_route->getDirList()) { DIR *dir; struct dirent *entry; struct stat file_stat; - std::vector files; + std::vector files; if ((dir = opendir(this->_target.c_str())) == NULL) throw; - while ((entry = readdir(dir)) != NULL) { + while ((entry = readdir(dir)) != NULL) { std::string file_name = entry->d_name; if (file_name == ".") continue; @@ -172,12 +170,9 @@ body {\n\ response.addHeader("Content-Length", length.str()); response.addHeader("Content-Type", "text/html"); response.setBody(body); - } - else + } else throw std::runtime_error("dir but no dirlist"); - } - else - { + } else { std::ifstream file(this->_target.c_str(), std::ios::binary); std::streampos file_start = file.tellg(); response.setBody(std::string((std::istreambuf_iterator(file)), @@ -189,7 +184,7 @@ body {\n\ response.setProtocol(this->_protocol); response.setStatusCode(200); response.addHeader("Content-Type", - http::Mime::getType(this->_target)); + http::Mime::getType(this->_target)); #ifdef VERBOSE //_log->debug(response.str().c_str());