forked from awwit/httpserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.h
More file actions
138 lines (100 loc) · 3.58 KB
/
Utils.h
File metadata and controls
138 lines (100 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#pragma once
#include <locale>
#include <vector>
#include <unordered_map>
namespace Utils
{
void toLower(std::string &str) noexcept;
std::string getLowerString(const std::string &str);
void trim(std::string &str);
std::string getTrimmedString(const std::string &str);
std::vector<std::string> explode(const std::string &str, const char sep);
std::string encodeHtmlSymbols(const std::string &str);
std::string binToHexString(const void *bin, const size_t size);
std::string hexStringToBin(const std::string &hexStr);
uint64_t hton64(const uint64_t host64) noexcept;
uint64_t ntoh64(const uint64_t net64) noexcept;
void hton24(void *dest, const uint32_t host32) noexcept;
uint32_t ntoh24(const void *src24) noexcept;
std::string getUniqueName();
size_t getPackNumberSize(const size_t number) noexcept;
size_t getPackStringSize(const std::string &str) noexcept;
template<typename T>
size_t getPackContainerSize(const T &container) noexcept
{
size_t full_size = getPackNumberSize(container.size() );
for (auto const &pair : container) {
full_size += getPackStringSize(pair.first);
full_size += getPackStringSize(pair.second);
}
return full_size;
}
uint8_t *packPointer(uint8_t *dest, void *pointer) noexcept;
uint8_t *packNumber(uint8_t *dest, const size_t number) noexcept;
uint8_t *packString(uint8_t *dest, const std::string &str) noexcept;
template<typename T>
uint8_t *packContainer(void *dest, const T &container) noexcept
{
uint8_t *addr = reinterpret_cast<uint8_t *>(dest);
addr = packNumber(addr, container.size() );
for (auto const &pair : container) {
addr = packString(addr, pair.first);
addr = packString(addr, pair.second);
}
return addr;
}
void packPointer(std::vector<char> &buf, void *pointer);
void packNumber(std::vector<char> &buf, const size_t number);
void packString(std::vector<char> &buf, const std::string &str);
template<typename T>
void packContainer(std::vector<char> &buf, const T &container)
{
packNumber(buf, container.size() );
for (auto const &pair : container) {
packString(buf, pair.first);
packString(buf, pair.second);
}
}
const uint8_t *unpackPointer(void **pointer, const uint8_t *src) noexcept;
const uint8_t *unpackNumber(size_t *number, const uint8_t *src) noexcept;
const uint8_t *unpackString(std::string &str, const uint8_t *src);
template<typename T>
const uint8_t *unpackContainer(T &container, const uint8_t *src)
{
size_t count;
src = unpackNumber(&count, src);
for (size_t i = 0; i < count; ++i) {
std::string key;
src = unpackString(key, src);
std::string value;
src = unpackString(value, src);
container.emplace(std::move(key), std::move(value) );
}
return src;
}
template<typename T>
const uint8_t *unpackVector(T &vector, const uint8_t *src)
{
size_t count;
src = unpackNumber(&count, src);
for (size_t i = 0; i < count; ++i) {
std::string key;
src = unpackString(key, src);
std::string value;
src = unpackString(value, src);
vector.emplace_back(std::move(key), std::move(value) );
}
return src;
}
time_t rfc822DatetimeToTimestamp(const std::string &strTime);
time_t predefinedDatetimeToTimestamp(const char *strTime);
std::string getDatetimeAsString(time_t tTime = ~0, const bool isGmtTime = false);
std::string predefinedDatetimeToRfc822(const char *strTime);
size_t getNumberLength(size_t number) noexcept;
bool parseCookies(
const std::string &cookieHeader,
std::unordered_multimap<std::string, std::string> &cookies
);
std::string urlEncode(const std::string &str);
std::string urlDecode(const std::string &str);
}