diff --git a/includes/config/Server.hpp b/includes/config/Server.hpp index ab4a6f7..ed6561a 100644 --- a/includes/config/Server.hpp +++ b/includes/config/Server.hpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/19 14:11:28 by adjoly #+# #+# */ -/* Updated: 2025/06/23 20:10:48 by adjoly ### ########.fr */ +/* Updated: 2025/07/05 16:39:51 by mmoussou ### ########.fr */ /* */ /* ************************************************************************** */ @@ -65,7 +65,8 @@ class Server { bool isServerName(const std::string &); // @brief Can be used to get the route correcponding - Route *whatRoute(const URL &); + Route *whatRoute(URL url); + Route *whichRoute(std::string &target); protected: private: diff --git a/includes/config/URL.hpp b/includes/config/URL.hpp index 07eddbc..4a6e3bd 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/05/27 19:46:54 by adjoly ### ########.fr */ +/* Updated: 2025/07/05 17:54:18 by mmoussou ### ########.fr */ /* */ /* ************************************************************************** */ @@ -48,6 +48,42 @@ class URL { std::string getQueryString(void) const { return _query_string; } std::string getPort(void) const { return _port; } + URL truncate() const { + std::vector segments = _path_segments; + if (!segments.empty()) + segments.erase(segments.begin()); + + std::string truncatedPath = "/"; + for (size_t i = 0; i < segments.size(); ++i) { + truncatedPath += segments[i]; + if (i != segments.size() - 1) + truncatedPath += "/"; + } + + // Reconstruct full URL with scheme, port, etc. + size_t scheme_pos = _full_url.find("://"); + std::string prefix = ""; + + if (scheme_pos != std::string::npos) { + prefix = _full_url.substr(0, scheme_pos + 3); + size_t host_end = _full_url.find('/', scheme_pos + 3); + if (host_end != std::string::npos) + prefix += _full_url.substr(scheme_pos + 3, host_end - (scheme_pos + 3)); + else + prefix += _full_url.substr(scheme_pos + 3); + } + + if (!_port.empty()) + prefix += ":" + _port; + + if (!_query_string.empty()) + truncatedPath += "?" + _query_string; + + return URL(prefix + truncatedPath); + } + + + private: void _parse() { size_t scheme_pos = _full_url.find("://"); diff --git a/includes/requests/ARequest.hpp b/includes/requests/ARequest.hpp index c479a25..de668b0 100644 --- a/includes/requests/ARequest.hpp +++ b/includes/requests/ARequest.hpp @@ -6,7 +6,7 @@ /* By: mmoussou +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/24 15:10:07 by adjoly #+# #+# */ -/* Updated: 2025/06/17 19:14:04 by adjoly ### ########.fr */ +/* Updated: 2025/07/05 17:56:40 by mmoussou ### ########.fr */ /* */ /* ************************************************************************** */ @@ -132,7 +132,7 @@ bool Server::isServerName(const std::string &server_name) { return false; } -Route *Server::whatRoute(const URL &url) { +Route *Server::whatRoute(URL url) { std::map::iterator ret = _routes->end(); int i = 0; @@ -152,6 +152,31 @@ Route *Server::whatRoute(const URL &url) { return ret->second; } +Route *Server::whichRoute(std::string &target) { + URL url(target); + std::map::iterator ret = _routes->end(); + + int i = 0; + + if (_routes == not_nullptr) + return not_nullptr; + + for (auto it = prange(_routes)) { + if (i < it->first.countMatchingSegments(url)) { + i = it->first.countMatchingSegments(url); + ret = it; + } + } + + if (ret == _routes->end()) + return _routes->find(URL("/"))->second; + + for (; i > 0; i--) + url = url.truncate(); + target = url.getFullUrl(); + return ret->second; +} + Server::Server(toml::ANode *node, void *) : _routes(not_nullptr), _server_names(not_nullptr), _table(node) { bool found; diff --git a/src/requests_handling/requestImplementation/Delete.cpp b/src/requests_handling/requestImplementation/Delete.cpp index 1aab39a..6cddfb6 100644 --- a/src/requests_handling/requestImplementation/Delete.cpp +++ b/src/requests_handling/requestImplementation/Delete.cpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/30 09:42:18 by adjoly #+# #+# */ -/* Updated: 2025/07/02 11:57:06 by adjoly ### ########.fr */ +/* Updated: 2025/07/07 19:06:38 by mmoussou ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,7 +36,6 @@ void Delete::parse(std::string const &data) { _method = _sanitizeStr(_method); _target = _sanitizeStr(_target); _protocol = _sanitizeStr(_protocol); - // this->_target.insert(this->_target.begin(), '.'); } while (std::getline(stream, line) && line != "\r") { diff --git a/src/server/Client.cpp b/src/server/Client.cpp index 0cd49d5..df67e14 100644 --- a/src/server/Client.cpp +++ b/src/server/Client.cpp @@ -6,7 +6,7 @@ /* By: mmoussou +#include #include #include #include @@ -55,7 +56,9 @@ void Client::parse(void) { if (_request == not_nullptr) return; - _route = _conf->whatRoute(URL(this->_request->getTarget())); + std::cout << "before: " << this->_request->_target << std::endl; + _route = _conf->whichRoute(this->_request->_target); + std::cout << "after: " << this->_request->_target << std::endl; if (_request->getMethod() != "501" && _conf->getServerNames() != not_nullptr) { @@ -78,7 +81,7 @@ void Client::parse(void) { this->_request->setMethod("405"); if (received_data.length() > (unsigned long)(_route->getMaxBody())) - _request->setMethod("413"); + this->_request->setMethod("413"); } bool Client::requestParsed(void) { @@ -101,8 +104,9 @@ void Client::_getRequest(std::string request_str) { _response_done = true; return; } - std::string method = request_str.substr( - 0, request_str.substr(0, 4).find_last_not_of(" ") + 1); + std::istringstream strm(request_str); + std::string method; + strm >> method; if (method == "GET") { _request = new http::Get(request_str, _conf); @@ -131,22 +135,31 @@ void Client::answer(void) { return; } - if (_route != not_nullptr && _route->getRedirect() == true) { + if (_route != not_nullptr && _route->getRedirect() == true) + { http::Redirect redir(_route->getRootDir()); _response = redir; _response_str = _response.str(); _bytes_sent = 0; - } else if (_response_str.empty()) { + } + else if (_response_str.empty()) + { if (this->_request->getMethod() == "GET" || this->_request->getMethod() == "DELETE" || - this->_request->getMethod() == "POST") { + this->_request->getMethod() == "POST") + { _response = this->_request->execute(); _response_str = _response.str(); - } else { - this->_response.setStatusCode(501); - _response_str = "HTTP/1.1 501 Not Implemented\r\nContent-Type: " - "text/html\r\n\r\n

501 Not " - "Implemented

"; + } + else + { + this->_response.setProtocol(this->_request->getProtocol()); + this->_response.setStatusCode(std::atoi(this->_request->getMethod().c_str())); + this->_response.addHeader("Content-Type", "text/html"); + this->_response.setBody(http::Errors::getResponseBody( + this->_response.getStatusCode(), + this->_conf->getErrorPage(this->_response.getStatusCode()))); + this->_response_str = this->_response.str(); } _bytes_sent = 0; }