diff --git a/src/http/httpserver.cpp b/src/http/httpserver.cpp index cf65ef4..14af19e 100644 --- a/src/http/httpserver.cpp +++ b/src/http/httpserver.cpp @@ -46,7 +46,7 @@ void Server::map_routes(std::initializer_list requests) } } -std::optional Server::handle_request(const HTTP::Request &request) +std::optional Server::handle_request(HTTP::Request &request) { for (auto& reqs: req_map) { diff --git a/src/http/httpserver.h b/src/http/httpserver.h index 83278ad..5a88820 100644 --- a/src/http/httpserver.h +++ b/src/http/httpserver.h @@ -65,7 +65,7 @@ namespace HTTP * @param request The request to check * @return An HTTP response which contains the information */ - std::optional handle_request(const HTTP::Request& request); + std::optional handle_request(HTTP::Request& request); inline uint16_t get_port() const noexcept { return port; } protected: diff --git a/src/http/request.cpp b/src/http/request.cpp index 28ee9d8..0078a8c 100644 --- a/src/http/request.cpp +++ b/src/http/request.cpp @@ -31,8 +31,10 @@ bool Request::operator==(const Request& other) const return get_url() == other.get_url() && get_type() == other.get_type(); } -std::optional Request::match_get_args(const Request& other) +std::optional Request::match_get_args(Request& other) { + other.try_strip_slash(); + // Before doing anything, check if the type (POST, GET...) are the same if (other.get_type() != this->type) return std::nullopt; @@ -84,10 +86,24 @@ std::optional Request::match_get_args(const Request& other) } // Compare rest of n - if (!std::equal(url.begin() + n_last, + if (url.length() == n && + !std::equal(url.begin() + n_last, url.begin() + url.length(), other_url.begin() + curr)) return std::nullopt; return args; } + +bool Request::try_strip_slash() +{ + bool ret = false; + // While loop because the user can just pass /url///////////... + while (url.back() == '/') + { + url.pop_back(); + ret = true; + } + return ret; +} + diff --git a/src/http/request.h b/src/http/request.h index 2cbb5b6..94a81c9 100644 --- a/src/http/request.h +++ b/src/http/request.h @@ -87,12 +87,18 @@ namespace HTTP * Compares a request with another requests, and expands ':' to arguments * when comparing. If no expansion required, equivalent to Request::operator==, but returns an empty std::vector * - * @remark The values _must_ * @param request Request to compare (and expand against) * @return Vector of strings to arguments, may be empty if no expansions * @return std::nullopt if the match fails entirely */ - std::optional match_get_args(const Request& request); + std::optional match_get_args(Request& request); + + /** + * @brief Strips ending slash + * @remark Strips multiple slashes if any + * @return True unless any slashes were stripped + */ + bool try_strip_slash(); // Type shouldn't be modifyable inline Type get_type() const noexcept { return type; } @@ -100,7 +106,7 @@ namespace HTTP inline const std::string& get_url() const noexcept { return url; } private: Type type; - const std::string url; + std::string url; }; }