-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.cpp
84 lines (78 loc) · 1.93 KB
/
Utils.cpp
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
#include "Utils.h"
#include <stdio.h>
using std::string;
string Utils::llToString(long long i)
{
string s;
char str[32] = {0};
size_t strLen = snprintf(str, 31, "%lli", i);
s.append(str, strLen);
return s;
}
string Utils::UriEscape(const string& in)
{
string out;
for (size_t i = 0; i < in.size(); i++)
{
char current = in[i];
if (
((current >= 65) && (current <= 90)) || // A-Z
((current >= 97) && (current <= 122)) || // a-z
((current >= 48) && (current <= 57)) || // 0-9
(current == '$') ||
(current == '&') ||
(current == '+') ||
(current == ',') ||
(current == '/') ||
(current == ':') ||
(current == ';') ||
(current == '=') ||
(current == '?') ||
(current == '@') ||
(current == '-') ||
(current == '_') ||
(current == '.') ||
(current == '+') ||
(current == '!') ||
(current == '*') ||
(current == '(') ||
(current == ')') ||
(current == '\'')
)
{
out += current;
}
else
{
char str[4] = {0};
snprintf(str, 4, "%%%02hhx", current);
out += str;
}
}
return out;
}
time_t Utils::parseHttpDate(const string& dateStr)
{
// strptime onmy ubuntu box ignores the timezone parameter at the end of the format string.
// This isn't the case on OS X, so we keep it for that platform.
#ifdef __linux__
#define REQ_INPUT_LEN 26
#else
#define REQ_INPUT_LEN 29
#endif
struct tm date;
const char* in = dateStr.c_str();
char* parsed = strptime(in, "%a, %d %b %Y %T %Z", &date);
if (parsed != (in + REQ_INPUT_LEN))
{
return 0;
}
return timegm(&date);
}
string Utils::getHttpDate(time_t secondsSinceEpoch)
{
char buffer[30] = {0};
struct tm* date = gmtime(&secondsSinceEpoch);
strftime(buffer, 30, "%a, %d %b %Y %T %Z", date);
return buffer;
}