Skip to content

Commit 82c8262

Browse files
committed
server/http: move string related code to string_utils
1 parent 3a52512 commit 82c8262

File tree

4 files changed

+62
-60
lines changed

4 files changed

+62
-60
lines changed

cpp/server/http.hpp

Lines changed: 2 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
#include "defines.hpp"
44
#include "string_utils.hpp"
55

6-
#include <unordered_map>
7-
#include <boost/functional/hash.hpp>
8-
96
namespace msrv {
107

8+
using HttpKeyValueMap = AsciiLowerCaseMap<std::string>;
9+
1110
enum class HttpMethod
1211
{
1312
UNDEFINED,
@@ -49,49 +48,6 @@ struct HttpHeader
4948
static const char LOCATION[];
5049
};
5150

52-
inline char asciiToLower(char ch)
53-
{
54-
return ch >= 'A' && ch <= 'Z' ? static_cast<char>(ch - 'A' + 'a') : ch;
55-
}
56-
57-
struct HttpKeyHash
58-
{
59-
size_t operator()(std::string const& str) const
60-
{
61-
size_t h = 0;
62-
63-
for (char ch : str)
64-
{
65-
boost::hash_combine(h, asciiToLower(ch));
66-
}
67-
68-
return h;
69-
}
70-
};
71-
72-
struct HttpKeyEqual
73-
{
74-
bool operator()(std::string const& s1, std::string const& s2) const
75-
{
76-
if (s1.size() != s2.size())
77-
{
78-
return false;
79-
}
80-
81-
for (size_t i = 0; i < s1.size(); i++)
82-
{
83-
if (asciiToLower(s1[i]) != asciiToLower(s2[i]))
84-
{
85-
return false;
86-
}
87-
}
88-
89-
return true;
90-
}
91-
};
92-
93-
using HttpKeyValueMap = std::unordered_map<std::string, std::string, HttpKeyHash, HttpKeyEqual>;
94-
9551
std::string toString(HttpMethod method);
9652
std::string toString(HttpStatus status);
9753

cpp/server/string_utils.hpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
#include <string>
66
#include <sstream>
7+
#include <unordered_map>
78

9+
#include <boost/functional/hash.hpp>
810
#include <boost/utility/string_view.hpp>
911

1012
namespace msrv {
@@ -45,4 +47,48 @@ StringView trimWhitespace(StringView str);
4547

4648
void formatText(char* data, size_t maxWidth);
4749

50+
inline char asciiToLower(char ch)
51+
{
52+
return ch >= 'A' && ch <= 'Z' ? static_cast<char>(ch - 'A' + 'a') : ch;
53+
}
54+
55+
struct AsciiLowerCaseHash
56+
{
57+
size_t operator()(std::string const& str) const
58+
{
59+
size_t h = 0;
60+
61+
for (char ch : str)
62+
{
63+
boost::hash_combine(h, asciiToLower(ch));
64+
}
65+
66+
return h;
67+
}
68+
};
69+
70+
struct AsciiLowerCaseEqual
71+
{
72+
bool operator()(std::string const& s1, std::string const& s2) const
73+
{
74+
if (s1.size() != s2.size())
75+
{
76+
return false;
77+
}
78+
79+
for (size_t i = 0; i < s1.size(); i++)
80+
{
81+
if (asciiToLower(s1[i]) != asciiToLower(s2[i]))
82+
{
83+
return false;
84+
}
85+
}
86+
87+
return true;
88+
}
89+
};
90+
91+
template<typename T>
92+
using AsciiLowerCaseMap = std::unordered_map<std::string, T, AsciiLowerCaseHash, AsciiLowerCaseEqual>;
93+
4894
}

cpp/server/tests/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ set(
77
test_main.hpp
88
base64_tests.cpp
99
fnv_hash_tests.cpp
10-
http_tests.cpp
1110
parsing_tests.cpp
1211
router_tests.cpp
1312
server_tests.cpp
13+
string_utils_tests.cpp
1414
timers_tests.cpp
1515
)
1616

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,50 @@
1-
#include "../http.hpp"
1+
#include "../string_utils.hpp"
22

33
#include <catch.hpp>
44

5-
namespace msrv::http_tests {
5+
namespace msrv::string_utils_tests {
66

7-
TEST_CASE("HttpKeyEqual")
7+
TEST_CASE("AsciiLowerCaseEqual")
88
{
99
SECTION("works")
1010
{
11-
HttpKeyEqual equal;
11+
AsciiLowerCaseEqual equal;
1212
REQUIRE(equal("hello", "hello") == true);
1313
REQUIRE(equal("hello", "Hello") == true);
1414
REQUIRE(equal("hello", "hallo") == false);
1515
REQUIRE(equal("hello", "h") == false);
1616
}
1717
}
1818

19-
TEST_CASE("HttpKeyHash")
19+
TEST_CASE("AsciiLowerCaseHash")
2020
{
2121
SECTION("works")
2222
{
23-
HttpKeyHash hash;
23+
AsciiLowerCaseHash hash;
2424
REQUIRE(hash("hello") == hash("hello"));
2525
REQUIRE(hash("hello") == hash("Hello"));
2626
REQUIRE(hash("hello") != hash("hallo"));
2727
REQUIRE(hash("hello") != hash("h"));
2828
}
2929
}
3030

31-
TEST_CASE("HttpKeyValueMap")
31+
TEST_CASE("AsciiLowerCaseMap")
3232
{
3333
SECTION("works")
3434
{
35-
HttpKeyValueMap map;
35+
AsciiLowerCaseMap<int> map;
3636

37-
map.emplace("test", "1");
37+
map.emplace("test", 1);
3838
auto it = map.find("TEST");
3939
REQUIRE(it != map.end());
40-
REQUIRE(it->second == "1");
40+
REQUIRE(it->second == 1);
4141

42-
map["Test"] = "2";
42+
map["Test"] = 2;
4343
it = map.find("TEST");
4444
REQUIRE(it != map.end());
45-
REQUIRE(it->second == "2");
45+
REQUIRE(it->second == 2);
4646

47-
auto ret = map.try_emplace("tEst", "3");
47+
auto ret = map.try_emplace("tEst", 3);
4848
REQUIRE(ret.second == false);
4949
}
5050
}

0 commit comments

Comments
 (0)