forked from awwit/httpserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.cpp
More file actions
459 lines (359 loc) · 8.93 KB
/
Utils.cpp
File metadata and controls
459 lines (359 loc) · 8.93 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
#include "Utils.h"
#include <array>
#include <chrono>
#include <cstring>
#include <sstream>
#include <iomanip>
#include <cstdint>
#include <cctype>
namespace Utils
{
void toLower(std::string &str, const std::locale &loc)
{
for (auto &c : str)
{
c = std::tolower(c, loc);
}
}
void trim(std::string &str)
{
static const std::array<char, 7> whitespace { " \t\n\v\f\r" };
const size_t last = str.find_last_not_of(whitespace.data() );
if (std::string::npos == last)
{
return str.clear();
}
str.assign(str.cbegin() + str.find_first_not_of(whitespace.data() ), str.cbegin() + last + 1);
}
std::vector<std::string> explode(const std::string &str, const char sep)
{
std::vector<std::string> values;
for (size_t pos = 0; std::string::npos != pos;)
{
size_t delimiter = str.find(sep, pos);
std::string value = str.substr(pos, delimiter);
trim(value);
values.emplace_back(std::move(value) );
pos = delimiter;
if (std::string::npos != pos)
{
++pos;
}
}
return values;
}
std::string encodeHtmlSymbols(const std::string &str)
{
std::string buf;
buf.reserve(str.length() );
for (size_t pos = 0; pos < str.length(); ++pos)
{
switch (str[pos])
{
case '&': buf.append("&"); break;
case '\"': buf.append("""); break;
case '\'': buf.append("'"); break;
case '<': buf.append("<"); break;
case '>': buf.append(">"); break;
default: buf.push_back(str[pos]); break;
}
}
return buf;
}
std::string binToHexString(const void *binData, const size_t dataSize)
{
std::string str(dataSize * 2, 0);
const uint8_t *bin = reinterpret_cast<const uint8_t *>(binData);
static const std::array<char, 17> hexDigits { "0123456789abcdef" };
for (size_t i = dataSize - 1; std::numeric_limits<size_t>::max() != i; --i)
{
str[i * 2 + 0] = hexDigits[bin[i] >> 4];
str[i * 2 + 1] = hexDigits[bin[i] & 0x0F];
}
return str;
}
static unsigned char hexStringToBinEncodeSymbol(const char c)
{
if (c >= '0' && c <= '9')
{
return c - 0x30;
}
else if (c >= 'a' && c <= 'f')
{
return c - 0x57;
}
else if (c >= 'A' && c <= 'F')
{
return c - 0x37;
}
return 0;
}
std::string hexStringToBin(const std::string &hexStr)
{
std::string bin(hexStr.length() / 2, 0);
for (size_t i = 0; i < bin.length(); ++i)
{
const char a = hexStr[i * 2 + 0];
const char b = hexStr[i * 2 + 1];
bin[i] = (
(hexStringToBinEncodeSymbol(a) << 4) | hexStringToBinEncodeSymbol(b)
);
}
return bin;
}
unsigned long long htonll(const unsigned long long src)
{
enum Endianness {
INIT = 0,
LITE = 1,
BIGE = 2
};
static int endian = Endianness::INIT;
union {
unsigned long long ull;
unsigned char c[8];
} x;
if (endian == Endianness::INIT)
{
x.ull = 0x01;
endian = (x.c[7] == 0x01ULL) ? Endianness::BIGE : Endianness::LITE;
}
if (endian == Endianness::BIGE)
{
return src;
}
x.ull = src;
unsigned char c;
c = x.c[0]; x.c[0] = x.c[7]; x.c[7] = c;
c = x.c[1]; x.c[1] = x.c[6]; x.c[6] = c;
c = x.c[2]; x.c[2] = x.c[5]; x.c[5] = c;
c = x.c[3]; x.c[3] = x.c[4]; x.c[4] = c;
return x.ull;
}
std::string getUniqueName()
{
size_t time = std::chrono::high_resolution_clock::now().time_since_epoch().count();
time = htonll(time);
return binToHexString(&time, sizeof(time) );
}
char *stlStringToPChar(const std::string &str)
{
const size_t length = str.length();
char *s = nullptr;
if (length)
{
s = new char[length + 1];
#ifdef WIN32
::strcpy_s(s, length + 1, str.c_str() );
#elif POSIX
::strcpy(s, str.c_str() );
s[length] = '\0';
#else
#error "Undefine platform"
#endif
}
return s;
}
void filesIncomingToRawFilesInfo(Utils::raw_fileinfo *raw[], const std::unordered_multimap<std::string, HttpServer::FileIncoming> &map)
{
if (raw && map.size() )
{
raw_fileinfo *arr = new raw_fileinfo[map.size()];
*raw = arr;
size_t i = 0;
for (auto it = map.cbegin(); map.cend() != it; ++it, ++i)
{
arr[i].key = stlStringToPChar(it->first);
const HttpServer::FileIncoming &file = it->second;
arr[i].file_name = stlStringToPChar(file.getName() );
arr[i].file_type = stlStringToPChar(file.getType() );
arr[i].file_size = file.getSize();
}
}
}
void rawFilesInfoToFilesIncoming(std::unordered_multimap<std::string, HttpServer::FileIncoming> &map, const Utils::raw_fileinfo raw[], const size_t count)
{
for (size_t i = 0; i < count; ++i)
{
map.emplace(raw[i].key ? raw[i].key : "", HttpServer::FileIncoming(raw[i].file_name, raw[i].file_type, raw[i].file_size) );
}
}
void destroyRawPairs(Utils::raw_pair raw[], const size_t count)
{
if (raw)
{
for (size_t i = 0; i < count; ++i)
{
raw_pair &cur = raw[i];
delete[] cur.key;
delete[] cur.value;
}
delete[] raw;
}
}
void destroyRawFilesInfo(Utils::raw_fileinfo raw[], const size_t count)
{
if (raw)
{
for (size_t i = 0; i < count; ++i)
{
raw_fileinfo &cur = raw[i];
delete[] cur.key;
delete[] cur.file_name;
delete[] cur.file_type;
}
delete[] raw;
}
}
time_t stringTimeToTimestamp(const std::string &strTime)
{
static const std::unordered_map<std::string, int> map_months {
{"Jan", 0}, {"Feb", 1}, {"Mar", 2}, {"Apr", 3}, {"May", 4}, {"Jun", 5}, {"Jul", 6}, {"Aug", 7}, {"Sep", 8}, {"Oct", 9}, {"Nov", 10}, {"Dec", 11}
};
if (strTime.length() > 64)
{
return ~0;
}
const size_t str_mon_length = 64;
std::vector<char> s_mon(str_mon_length);
struct ::tm tc = {};
// Parse RFC 822
#ifdef WIN32
if (~0 != ::sscanf_s(strTime.c_str(), "%*s %d %3s %d %d:%d:%d", &tc.tm_mday, s_mon.data(), static_cast<unsigned int>(s_mon.size() ), &tc.tm_year, &tc.tm_hour, &tc.tm_min, &tc.tm_sec) )
#else
if (~0 != ::sscanf(strTime.c_str(), "%*s %d %3s %d %d:%d:%d", &tc.tm_mday, s_mon.data(), &tc.tm_year, &tc.tm_hour, &tc.tm_min, &tc.tm_sec) )
#endif
{
tc.tm_year -= 1900;
auto const it_mon = map_months.find(s_mon.data() );
if (map_months.cend() != it_mon)
{
tc.tm_mon = it_mon->second;
}
}
tc.tm_isdst = -1;
return ::mktime(&tc);
}
std::string getDatetimeAsString(const ::time_t tTime, const bool isGmtTime)
{
std::array<char, 64> buf;
::time_t cur_time = tTime;
if (tTime == ~0)
{
::time(&cur_time);
}
#ifdef WIN32
struct ::tm stm = {};
isGmtTime ?
::localtime_s(&stm, &cur_time) :
::gmtime_s(&stm, &cur_time);
// RFC 822
::strftime(buf.data(), buf.size(), "%a, %d %b %Y %H:%M:%S GMT", &stm);
#else
struct ::tm stm = {};
isGmtTime ?
::localtime_r(&cur_time, &stm) :
::gmtime_r(&cur_time, &stm);
// RFC 822
::strftime(buf.data(), buf.size(), "%a, %d %b %G %H:%M:%S GMT", &stm);
#endif
return std::string(buf.data() );
}
size_t getNumberLength(const size_t number)
{
size_t length = 0;
size_t n = number;
do
{
++length;
n /= 10;
}
while (n);
return length;
}
bool parseCookies(const std::string &cookieHeader, std::unordered_multimap<std::string, std::string> &cookies)
{
if (cookieHeader.empty() )
{
return true;
}
for (size_t cur_pos = 0, next_value; std::string::npos != cur_pos; cur_pos = next_value)
{
next_value = cookieHeader.find(';', cur_pos);
size_t delimiter = cookieHeader.find('=', cur_pos);
if (std::string::npos == delimiter || delimiter > next_value)
{
return false;
}
std::string key = cookieHeader.substr(cur_pos, delimiter - cur_pos);
trim(key);
key = urlDecode(key);
++delimiter;
std::string value = cookieHeader.substr(delimiter, std::string::npos != next_value ? next_value - delimiter : next_value);
trim(value);
value = urlDecode(value);
cookies.emplace(std::move(key), std::move(value) );
if (std::string::npos != next_value)
{
++next_value;
}
}
return true;
}
static inline bool isCharUrlAllowed(const char c)
{
return c == '-' || c == '_' || c == '.' || c == '~';
}
std::string urlEncode(const std::string &str)
{
std::string encoded;
static const std::array<char, 17> hexDigits { "0123456789ABCDEF" };
for (size_t i = 0; i < str.length(); ++i)
{
const unsigned char c = str[i];
if (std::isalnum(c) || isCharUrlAllowed(c) )
{
encoded.push_back(c);
}
else if (' ' == c)
{
encoded.push_back('+');
}
else
{
const uint8_t a = c >> 4;
const uint8_t b = c & 0x0F;
encoded.push_back('%');
encoded.push_back(hexDigits[a]);
encoded.push_back(hexDigits[b]);
}
}
return encoded;
}
std::string urlDecode(const std::string &str)
{
std::string decoded;
for (size_t i = 0; i < str.length(); ++i)
{
unsigned char c = str[i];
if ('%' == c)
{
if (i + 2 < str.length() )
{
const char a = str[++i];
const char b = str[++i];
c = (
(hexStringToBinEncodeSymbol(a) << 4) | hexStringToBinEncodeSymbol(b)
);
}
}
else if ('+' == c)
{
c = ' ';
}
decoded.push_back(c);
}
return decoded;
}
};