Strip slashes and fix request check
FossilOrigin-Name: 8323e57ec5ebfc8c0e9f16b7977deb9d51d5607158791c5426bd2c27ae95c52c
This commit is contained in:
parent
1c8f6fdd6f
commit
5b93813094
4 changed files with 29 additions and 7 deletions
|
@ -46,7 +46,7 @@ void Server::map_routes(std::initializer_list<RequestCallbackPair_t> requests)
|
|||
}
|
||||
}
|
||||
|
||||
std::optional<HTTP::Response const> Server::handle_request(const HTTP::Request &request)
|
||||
std::optional<HTTP::Response const> Server::handle_request(HTTP::Request &request)
|
||||
{
|
||||
for (auto& reqs: req_map)
|
||||
{
|
||||
|
|
|
@ -65,7 +65,7 @@ namespace HTTP
|
|||
* @param request The request to check
|
||||
* @return An HTTP response which contains the information
|
||||
*/
|
||||
std::optional<HTTP::Response const> handle_request(const HTTP::Request& request);
|
||||
std::optional<HTTP::Response const> handle_request(HTTP::Request& request);
|
||||
|
||||
inline uint16_t get_port() const noexcept { return port; }
|
||||
protected:
|
||||
|
|
|
@ -31,8 +31,10 @@ bool Request::operator==(const Request& other) const
|
|||
return get_url() == other.get_url() && get_type() == other.get_type();
|
||||
}
|
||||
|
||||
std::optional<RequestArgs_t> Request::match_get_args(const Request& other)
|
||||
std::optional<RequestArgs_t> 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<RequestArgs_t> 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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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<RequestArgs_t> match_get_args(const Request& request);
|
||||
std::optional<RequestArgs_t> 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;
|
||||
};
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue