From 111aeeb7fc950918a7d48a53ec36740d1f597e9c Mon Sep 17 00:00:00 2001 From: eidheim Date: Sat, 10 Jun 2017 11:17:49 +0200 Subject: [PATCH] Renamed case insensitive function and classes, and added test for case_insensitive_equal --- client_http.hpp | 14 +++++++------- server_http.hpp | 22 +++++++++++----------- tests/parse_test.cpp | 13 ++++++++----- 3 files changed, 26 insertions(+), 23 deletions(-) diff --git a/client_http.hpp b/client_http.hpp index e5e2d46..7a64840 100644 --- a/client_http.hpp +++ b/client_http.hpp @@ -30,23 +30,23 @@ namespace SimpleWeb { } #endif -# ifndef CASE_INSENSITIVE_EQUALS_AND_HASH -# define CASE_INSENSITIVE_EQUALS_AND_HASH +# ifndef CASE_INSENSITIVE_EQUAL_AND_HASH +# define CASE_INSENSITIVE_EQUAL_AND_HASH namespace SimpleWeb { - bool iequals(const std::string &str1, const std::string &str2) { + bool case_insensitive_equal(const std::string &str1, const std::string &str2) { return str1.size() == str2.size() && std::equal(str1.begin(), str1.end(), str2.begin(), [](char a, char b) { return tolower(a) == tolower(b); }); } - class case_insensitive_equals { + class CaseInsensitiveEqual { public: bool operator()(const std::string &str1, const std::string &str2) const { - return iequals(str1, str2); + return case_insensitive_equal(str1, str2); } }; // Based on https://stackoverflow.com/questions/2590677/how-do-i-combine-hash-values-in-c0x/2595226#2595226 - class case_insensitive_hash { + class CaseInsensitiveHash { public: size_t operator()(const std::string &str) const { size_t h = 0; @@ -76,7 +76,7 @@ namespace SimpleWeb { std::istream content; - std::unordered_multimap header; + std::unordered_multimap header; private: asio::streambuf content_buffer; diff --git a/server_http.hpp b/server_http.hpp index 3d027ca..ef139c4 100644 --- a/server_http.hpp +++ b/server_http.hpp @@ -28,23 +28,23 @@ namespace SimpleWeb { } #endif -# ifndef CASE_INSENSITIVE_EQUALS_AND_HASH -# define CASE_INSENSITIVE_EQUALS_AND_HASH +# ifndef CASE_INSENSITIVE_EQUAL_AND_HASH +# define CASE_INSENSITIVE_EQUAL_AND_HASH namespace SimpleWeb { - bool iequals(const std::string &str1, const std::string &str2) { + bool case_insensitive_equal(const std::string &str1, const std::string &str2) { return str1.size() == str2.size() && std::equal(str1.begin(), str1.end(), str2.begin(), [](char a, char b) { return tolower(a) == tolower(b); }); } - class case_insensitive_equals { + class CaseInsensitiveEqual { public: bool operator()(const std::string &str1, const std::string &str2) const { - return iequals(str1, str2); + return case_insensitive_equal(str1, str2); } }; // Based on https://stackoverflow.com/questions/2590677/how-do-i-combine-hash-values-in-c0x/2595226#2595226 - class case_insensitive_hash { + class CaseInsensitiveHash { public: size_t operator()(const std::string &str) const { size_t h = 0; @@ -135,7 +135,7 @@ namespace SimpleWeb { Content content; - std::unordered_multimap header; + std::unordered_multimap header; regex::smatch path_match; @@ -143,8 +143,8 @@ namespace SimpleWeb { unsigned short remote_endpoint_port; /// Returns query keys with percent-decoded values. - std::unordered_multimap parse_query_string() { - std::unordered_multimap result; + std::unordered_multimap parse_query_string() { + std::unordered_multimap result; auto qs_start_pos = path.find('?'); if (qs_start_pos != std::string::npos && qs_start_pos + 1 < path.size()) { ++qs_start_pos; @@ -453,9 +453,9 @@ namespace SimpleWeb { auto range=request->header.equal_range("Connection"); for(auto it=range.first;it!=range.second;it++) { - if(iequals(it->second, "close")) { + if(case_insensitive_equal(it->second, "close")) { return; - } else if (iequals(it->second, "keep-alive")) { + } else if (case_insensitive_equal(it->second, "keep-alive")) { this->read_request_and_content(response->socket); return; } diff --git a/tests/parse_test.cpp b/tests/parse_test.cpp index 06f8fe2..692f0d3 100644 --- a/tests/parse_test.cpp +++ b/tests/parse_test.cpp @@ -105,11 +105,14 @@ public: }; int main() { - case_insensitive_equals equals; - assert(equals("Test", "tesT")); - assert(equals("tesT", "test")); - assert(!equals("test", "tset")); - case_insensitive_hash hash; + assert(case_insensitive_equal("Test", "tesT")); + assert(case_insensitive_equal("tesT", "test")); + assert(!case_insensitive_equal("test", "tseT")); + CaseInsensitiveEqual equal; + assert(equal("Test", "tesT")); + assert(equal("tesT", "test")); + assert(!equal("test", "tset")); + CaseInsensitiveHash hash; assert(hash("Test")==hash("tesT")); assert(hash("tesT")==hash("test")); assert(hash("test")!=hash("tset"));