From 8289aa3db4b9413aba487732877b161b2e79d6d7 Mon Sep 17 00:00:00 2001 From: nekobit Date: Thu, 29 Sep 2022 03:07:09 +0000 Subject: [PATCH] call_if_match implementation (untested) FossilOrigin-Name: 1edbe4f7555dce7630f91ba8e8ad23739686550757a9a7707ac1aaab86d8ef40 --- src/http/request.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++++ src/http/request.h | 7 +++++-- 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/src/http/request.cpp b/src/http/request.cpp index 183e5f6..41c6217 100644 --- a/src/http/request.cpp +++ b/src/http/request.cpp @@ -16,6 +16,7 @@ * along with this program. If not, see . */ +#include #include "request.h" using namespace HTTP; @@ -28,5 +29,50 @@ Request::Request(Type type, const std::string url, RequestCallback_t func) bool Request::call_if_match(Type type, const std::string& string) { + // Before doing anything, check if the type (POST, GET...) are the same + if (type != this->type) return false; + + std::vector args{}; + // Current point of `string' + size_t curr = std::string::npos; + size_t n, n_last = std::string::npos; + bool match = true; + while((n = url.find(':', n)) != std::string::npos) + { + // Compare range from start to found point + if (!std::equal(url.begin() + n_last, + string.begin() + n_last, + string.begin() + (n-1))) + return false; + + // Peek next character to know where to stop (often a '/') + size_t next_char_dist = url.find(n+1, n); + if (next_char_dist != std::string::npos) + { + // Push string + args.push_back(string.substr(n, next_char_dist)); + // Seek n past next_char + n += next_char_dist; + } + else { + // Out of bounds, last argument + args.push_back(string.substr(n)); + } + + n_last = n; + } + + // If previous loop couldn't find anything, check if strings aren't fully equal + if (n == std::string::npos && + n_last == std::string::npos && + url != string) + { + // ... if so, this doesn't match + return false; + } + + callback(args); + + return true; } diff --git a/src/http/request.h b/src/http/request.h index 912ae43..a3d1665 100644 --- a/src/http/request.h +++ b/src/http/request.h @@ -29,11 +29,14 @@ namespace HTTP * @brief A HTTP Request structure * * A very simple format is used for the URL to match paremeters. - * If a ':' character is in place, then it will read until the next '/' character + * If a ':' character is in place, then it will read until the next character (often '/') * or until the next end of line. * * Example: /api/v1/user/:/haters - * /api/v1/user/:/dislike_user/: + * + * Example 2: /api/v1/user/:/dislike_user/: + * + * Example 3: /lets/get/:_fails/or/wins_: */ class Request {