Repository navigation
Expand file tree
/
Copy pathutils.hpp
More file actions
519 lines (451 loc) · 14.4 KB
/
Copy pathutils.hpp
File metadata and controls
519 lines (451 loc) · 14.4 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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
//
// Created by qiyu on 12/19/17.
//
#ifndef CINATRA_UTILS_HPP
#define CINATRA_UTILS_HPP
#pragma once
#include <string>
#include <sstream>
#include <iomanip>
#include <cstddef> //std::byte
#include <string_view>
#include <cstdlib>
#include <cctype>
#include <type_traits>
#include <algorithm>
#include <iostream>
#include "define.h"
namespace cinatra {
struct ci_less
{
// case-independent (ci) compare_less binary function
struct nocase_compare
{
bool operator() (const unsigned char& c1, const unsigned char& c2) const {
return tolower(c1) < tolower(c2);
}
};
bool operator() (const std::string & s1, const std::string & s2) const {
return std::lexicographical_compare
(s1.begin(), s1.end(), // source range
s2.begin(), s2.end(), // dest range
nocase_compare()); // comparison
}
bool operator() (std::string_view s1, std::string_view s2) const {
return std::lexicographical_compare
(s1.begin(), s1.end(), // source range
s2.begin(), s2.end(), // dest range
nocase_compare()); // comparison
}
};
class noncopyable {
public:
noncopyable() = default;
~noncopyable() = default;
private:
noncopyable(const noncopyable&) = delete;
noncopyable& operator=(const noncopyable&) = delete;
};
using namespace std::string_view_literals;
template <class T>
struct sv_char_trait : std::char_traits<T> {
using base_t = std::char_traits<T>;
using char_type = typename base_t::char_type;
static constexpr int compare(std::string_view s1, std::string_view s2) noexcept {
if (s1.length() != s2.length())
return -1;
size_t n = s1.length();
for (size_t i = 0; i < n; ++i) {
if (!base_t::eq(s1[i], s2[i])) {
return base_t::eq(s1[i], s2[i]) ? -1 : 1;
}
}
return 0;
}
static constexpr size_t find(std::string_view str, const char_type& a) noexcept {
auto s = str.data();
for (size_t i = 0; i < str.length(); ++i) {
if (base_t::eq(s[i], a)) {
return i;
}
}
return std::string_view::npos;
}
};
inline std::string_view trim_left(std::string_view v) {
v.remove_prefix((std::min)(v.find_first_not_of(" "), v.size()));
return v;
}
inline std::string_view trim_right(std::string_view v) {
v.remove_suffix((std::min)(v.size() - v.find_last_not_of(" ") - 1, v.size()));
return v;
}
inline std::string_view trim(std::string_view v) {
v.remove_prefix((std::min)(v.find_first_not_of(" "), v.size()));
v.remove_suffix((std::min)(v.size() - v.find_last_not_of(" ") - 1, v.size()));
return v;
}
template<typename T>
constexpr bool is_int64_v = std::is_same_v<T, std::int64_t> || std::is_same_v<T, std::uint64_t>;
enum class http_method {
DEL,
GET,
HEAD,
POST,
PUT,
CONNECT,
OPTIONS,
TRACE
};
constexpr inline auto GET = http_method::GET;
constexpr inline auto POST = http_method::POST;
constexpr inline auto DEL = http_method::DEL;
constexpr inline auto HEAD = http_method::HEAD;
constexpr inline auto PUT = http_method::PUT;
constexpr inline auto CONNECT = http_method::CONNECT;
constexpr inline auto TRACE = http_method::TRACE;
constexpr inline auto OPTIONS = http_method::OPTIONS;
inline constexpr std::string_view method_name(http_method mthd) {
switch (mthd)
{
case cinatra::http_method::DEL:
return "DELETE"sv;
break;
case cinatra::http_method::GET:
return "GET"sv;
break;
case cinatra::http_method::HEAD:
return "HEAD"sv;
break;
case cinatra::http_method::POST:
return "POST"sv;
break;
case cinatra::http_method::PUT:
return "PUT"sv;
break;
case cinatra::http_method::CONNECT:
return "CONNECT"sv;
break;
case cinatra::http_method::OPTIONS:
return "OPTIONS"sv;
break;
case cinatra::http_method::TRACE:
return "TRACE"sv;
break;
default:
return "UNKONWN"sv;
break;
}
}
constexpr auto type_to_name(std::integral_constant<http_method, http_method::DEL>) noexcept { return "DELETE"sv; }
constexpr auto type_to_name(std::integral_constant<http_method, http_method::GET>) noexcept { return "GET"sv; }
constexpr auto type_to_name(std::integral_constant<http_method, http_method::HEAD>) noexcept { return "HEAD"sv; }
constexpr auto type_to_name(std::integral_constant<http_method, http_method::POST>) noexcept { return "POST"sv; }
constexpr auto type_to_name(std::integral_constant<http_method, http_method::PUT>) noexcept { return "PUT"sv; }
constexpr auto type_to_name(std::integral_constant<http_method, http_method::CONNECT>) noexcept { return "CONNECT"sv; }
constexpr auto type_to_name(std::integral_constant<http_method, http_method::OPTIONS>) noexcept { return "OPTIONS"sv; }
constexpr auto type_to_name(std::integral_constant<http_method, http_method::TRACE>) noexcept { return "TRACE"sv; }
inline bool iequal(const char *s, size_t l, const char *t) {
if (strlen(t) != l)
return false;
for (size_t i = 0; i < l; i++) {
if (std::tolower(s[i]) != std::tolower(t[i]))
return false;
}
return true;
}
inline std::vector<std::string_view> split(std::string_view s, std::string_view delimiter) {
size_t start = 0;
size_t end = s.find_first_of(delimiter);
std::vector<std::string_view> output;
while (end <= std::string_view::npos)
{
output.emplace_back(s.substr(start, end - start));
if (end == std::string_view::npos)
break;
start = end + 1;
end = s.find_first_of(delimiter, start);
}
return output;
}
inline void remove_char(std::string &str, const char ch) {
str.erase(std::remove(str.begin(), str.end(), ch), str.end());
}
// var bools = [];
// var valid_chr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_.-';
// for(var i = 0; i <= 127; ++ i) {
// var contain = valid_chr.indexOf(String.fromCharCode(i)) == -1;
// bools.push(contain?false:true);
// }
// console.log(JSON.stringify(bools))
// 浏览器运行 js 生成 valid_chr 数组
inline const constexpr bool valid_chr[128] =
{
false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,true,true,false,true,true,true,true,true,true,true,true,true,true,false,false,false,false,false,false,false,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,false,false,false,false,true,false,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,false,false,false,false,false
};
inline std::ostringstream& quote_impl(std::ostringstream& os, std::string_view str, std::string_view safe) {
os << std::setiosflags(std::ios::right) << std::setfill('0');
auto begin = reinterpret_cast<const std::byte*>(str.data());
auto end = begin + sizeof(char) * str.size();
std::for_each(begin, end, [&os, &safe] (auto& chr)
{
char chrval = (char) chr;
unsigned int intval = (unsigned int) chr;
if ((intval > 127 || !valid_chr[intval]) && safe.find(chrval) == std::string_view::npos)
os << '%' << std::setw(2) << std::hex << std::uppercase << intval;
else
os << chrval;
});
return os;
}
inline const std::string quote(std::string_view str)
{
std::ostringstream os;
return quote_impl(os, str, "/").str();
}
inline const std::string quote_plus(std::string_view str)
{
if (str.find(' ') == std::string_view::npos) return quote(str);
std::ostringstream os;
auto strval = quote_impl(os, str, " ").str();
std::replace(strval.begin(), strval.end(), ' ', '+');
return strval;
}
std::string form_urldecode(const std::string &src) {
std::string ret;
char ch;
int i, ii;
for (i = 0; i<src.length(); i++) {
if (int(src[i]) == 37) {
sscanf(src.substr(i + 1, 2).c_str(), "%x", &ii);
ch = static_cast<char>(ii);
ret += ch;
i = i + 2;
}
else {
ret += src[i];
}
}
return ret;
}
inline bool is_form_url_encode(std::string_view str) {
return str.find("%") != std::string_view::npos || str.find("+") != std::string_view::npos;
}
inline std::string_view get_extension(std::string_view name) {
size_t pos = name.rfind('.');
if (pos == std::string_view::npos) {
return {};
}
return name.substr(pos);
}
inline std::string to_hex_string(std::size_t value) {
std::ostringstream stream;
stream << std::hex << value;
return stream.str();
}
inline const char *MAP = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
inline const char *MAP_URL_ENCODED = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789-_";
// from h2o
inline size_t base64_encode(char *_dst, const void *_src, size_t len, int url_encoded) {
char *dst = _dst;
const uint8_t *src = reinterpret_cast<const uint8_t *>(_src);
const char *map = url_encoded ? MAP_URL_ENCODED : MAP;
uint32_t quad;
for (; len >= 3; src += 3, len -= 3) {
quad = ((uint32_t)src[0] << 16) | ((uint32_t)src[1] << 8) | src[2];
*dst++ = map[quad >> 18];
*dst++ = map[(quad >> 12) & 63];
*dst++ = map[(quad >> 6) & 63];
*dst++ = map[quad & 63];
}
if (len != 0) {
quad = (uint32_t)src[0] << 16;
*dst++ = map[quad >> 18];
if (len == 2) {
quad |= (uint32_t)src[1] << 8;
*dst++ = map[(quad >> 12) & 63];
*dst++ = map[(quad >> 6) & 63];
if (!url_encoded)
*dst++ = '=';
}
else {
*dst++ = map[(quad >> 12) & 63];
if (!url_encoded) {
*dst++ = '=';
*dst++ = '=';
}
}
}
*dst = '\0';
return dst - _dst;
}
inline bool is_valid_utf8(unsigned char *s, size_t length)
{
for (unsigned char *e = s + length; s != e; )
{
if (s + 4 <= e && ((*(uint32_t *)s) & 0x80808080) == 0)
{
s += 4;
}
else
{
while (!(*s & 0x80))
{
if (++s == e)
{
return true;
}
}
if ((s[0] & 0x60) == 0x40)
{
if (s + 1 >= e || (s[1] & 0xc0) != 0x80 || (s[0] & 0xfe) == 0xc0)
{
return false;
}
s += 2;
}
else if ((s[0] & 0xf0) == 0xe0)
{
if (s + 2 >= e || (s[1] & 0xc0) != 0x80 || (s[2] & 0xc0) != 0x80 ||
(s[0] == 0xe0 && (s[1] & 0xe0) == 0x80) || (s[0] == 0xed && (s[1] & 0xe0) == 0xa0))
{
return false;
}
s += 3;
}
else if ((s[0] & 0xf8) == 0xf0)
{
if (s + 3 >= e || (s[1] & 0xc0) != 0x80 || (s[2] & 0xc0) != 0x80 || (s[3] & 0xc0) != 0x80 ||
(s[0] == 0xf0 && (s[1] & 0xf0) == 0x80) || (s[0] == 0xf4 && s[1] > 0x8f) || s[0] > 0xf4)
{
return false;
}
s += 4;
}
else
{
return false;
}
}
}
return true;
}
template<typename T>
inline std::string to_str(T&& value) {
using U = std::remove_const_t<std::remove_reference_t<T>>;
if constexpr(std::is_integral_v<U> && !is_int64_v<U>) {
std::vector<char> temp(20, '\0');
itoa_fwd(value, temp.data());
return std::string(temp.data());
}
else if constexpr (is_int64_v<U>) {
std::vector<char> temp(65, '\0');
xtoa(value, temp.data(), 10, std::is_signed_v<U>);
return std::string(temp.data());
}
else if constexpr (std::is_floating_point_v<U>) {
std::vector<char> temp(20, '\0');
sprintf(temp.data(), "%f", value);
return std::string(temp.data());
}
else if constexpr(std::is_same_v<std::string, U> || std::is_same_v<const char*, U>) {
return value;
}
else {
std::cout << "this type has not supported yet" << std::endl;
}
}
#define HAS_MEMBER(member)\
template<typename T, typename... Args>\
struct has_##member\
{\
private:\
template<typename U> static auto Check(int) -> decltype(std::declval<U>().member(std::declval<Args>()...), std::true_type()); \
template<typename U> static std::false_type Check(...);\
public:\
enum{value = std::is_same<decltype(Check<T>(0)), std::true_type>::value};\
};
HAS_MEMBER(before)
HAS_MEMBER(after)
template <typename... Args, typename F, std::size_t... Idx>
constexpr void for_each_l(std::tuple<Args...>& t, F&& f, std::index_sequence<Idx...>) {
(std::forward<F>(f)(std::get<Idx>(t)), ...);
}
template <typename... Args, typename F, std::size_t... Idx>
constexpr void for_each_r(std::tuple<Args...>& t, F&& f, std::index_sequence<Idx...>) {
constexpr auto size = sizeof...(Idx);
(std::forward<F>(f)(std::get<size - Idx - 1>(t)), ...);
}
template<http_method N>
constexpr void get_str(std::string& s, std::string_view name) {
s = type_to_name(std::integral_constant<http_method, N>{}).data();
s += std::string(name.data(), name.length());
}
template<http_method... Is>
constexpr auto get_arr(std::string_view name) {
std::array<std::string, sizeof...(Is)> arr = {};
size_t index = 0;
(get_str<Is>(arr[index++], name), ...);
return arr;
}
inline std::string get_time_str(std::time_t t) {
std::stringstream ss;
ss << std::put_time(std::localtime(&t), "%Y-%m-%d %H:%M:%S");
return ss.str();
}
inline std::string get_gmt_time_str(std::time_t t)
{
struct tm* GMTime = gmtime(&t);
char buff[512]={0};
strftime(buff,sizeof(buff),"%a, %d %b %Y %H:%M:%S %Z",GMTime);
return buff;
}
inline std::string get_cur_time_str() {
return get_time_str(std::time(nullptr));
}
const std::map<std::string_view, std::string_view> get_cookies_map(std::string_view cookies_str)
{
std::map<std::string_view, std::string_view> cookies;
auto cookies_vec = split(cookies_str,"; ");
for(auto iter:cookies_vec)
{
auto cookie_key_vlaue = split(iter,"=");
if(cookie_key_vlaue.size()==2)
{
cookies[cookie_key_vlaue[0]] = cookie_key_vlaue[1];
}
}
return cookies;
};
template <typename T, typename Tuple>
struct has_type;
template <typename T, typename... Us>
struct has_type<T, std::tuple<Us...>> : std::disjunction<std::is_same<T, Us>...> {};
template< typename T>
struct filter_helper
{
static constexpr auto func()
{
return std::tuple<>();
}
template< class... Args >
static constexpr auto func(T&&, Args&&...args)
{
return filter_helper::func(std::forward<Args>(args)...);
}
template< class X, class... Args >
static constexpr auto func(X&&x, Args&&...args)
{
return std::tuple_cat(std::make_tuple(std::forward<X>(x)), filter_helper::func(std::forward<Args>(args)...));
}
};
template<typename T, typename... Args>
inline auto filter(Args&&... args) {
return filter_helper<T>::func(std::forward<Args>(args)...);
}
}
#endif //CINATRA_UTILS_HPP