forked from skyfireitdiy/sflib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsf_http_utils.hpp
176 lines (158 loc) · 5.55 KB
/
sf_http_utils.hpp
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
/**
* @version 1.0.0
* @author skyfire
* @mail [email protected]
* @see http://github.com/skyfireitdiy/sflib
* @file sf_http_utils.hpp
* sflib第一版本发布
* 版本号1.0.0
* 发布日期:2018-10-22
*/
#pragma once
#include "sf_http_utils.h"
#include "sf_tcp_utils.hpp"
#include "sf_type.hpp"
#include "sf_http_request_line.h"
#include "sf_http_multipart.hpp"
namespace skyfire
{
inline unsigned char sf_to_hex(unsigned char x)
{
return static_cast<unsigned char>(x > 9 ? x + 55 : x + 48);
}
inline unsigned char sf_from_hex(unsigned char x)
{
unsigned char y = 0;
if (x >= 'A' && x <= 'Z') y = static_cast<unsigned char>(x - 'A' + 10);
else if (x >= 'a' && x <= 'z') y = static_cast<unsigned char>(x - 'a' + 10);
else if (x >= '0' && x <= '9') y = x - '0';
return y;
}
inline std::string sf_url_encode(const std::string &str)
{
std::string strTemp = "";
size_t length = str.length();
for (size_t i = 0; i < length; i++)
{
if (isalnum((unsigned char)str[i]) ||
(str[i] == '-') ||
(str[i] == '_') ||
(str[i] == '.') ||
(str[i] == '~'))
strTemp += str[i];
else if (str[i] == ' ')
strTemp += "+";
else
{
strTemp += '%';
strTemp += sf_to_hex((unsigned char)str[i] >> 4);
strTemp += sf_to_hex((unsigned char)str[i] % 16);
}
}
return strTemp;
}
inline std::string sf_url_decode(const std::string &str)
{
std::string strTemp = "";
size_t length = str.length();
for (size_t i = 0; i < length; i++)
{
if (str[i] == '+') strTemp += ' ';
else if (str[i] == '%')
{
unsigned char high = sf_from_hex((unsigned char)str[++i]);
unsigned char low = sf_from_hex((unsigned char)str[++i]);
strTemp += high*16 + low;
}
else strTemp += str[i];
}
return strTemp;
}
inline std::map<std::string, std::string> sf_parse_param(std::string param_str) {
std::map<std::string, std::string> param;
unsigned long url_pos;
while((url_pos = param_str.find('&')) != std::string::npos)
{
auto tmp_param = std::string(param_str.begin(),param_str.begin()+url_pos);
param_str = std::string(param_str.begin()+url_pos+1,param_str.end());
if(tmp_param.empty())
continue;
if((url_pos = tmp_param.find('=')) == std::string::npos)
continue;
auto key = sf_url_decode(std::string(tmp_param.begin(), tmp_param.begin() + url_pos));
auto value = sf_url_decode(std::string(tmp_param.begin() + url_pos + 1, tmp_param.end()));
param[key] = value;
}
if(param_str.empty())
return param;
if((url_pos = param_str.find('=')) == std::string::npos)
return param;
auto key = sf_url_decode(std::string(param_str.begin(), param_str.begin() + url_pos));
auto value = sf_url_decode(std::string(param_str.begin() + url_pos + 1, param_str.end()));
param[key] = value;
return param;
}
inline void sf_parse_url(const std::string &raw_url, std::string &url, std::map<std::string,std::string>& param,
std::string frame)
{
auto frame_pos = raw_url.find('#');
std::string raw_url_without_frame;
if(frame_pos == std::string::npos) {
raw_url_without_frame = raw_url;
frame = "";
}
else{
raw_url_without_frame = std::string(raw_url.begin(),raw_url.begin()+frame_pos);
frame = std::string(raw_url.begin()+frame_pos+1,raw_url.end());
}
auto url_pos = raw_url_without_frame.find('?');
if(url_pos == std::string::npos){
url = raw_url_without_frame;
return;
}
url = std::string(raw_url_without_frame.begin(),raw_url_without_frame.begin()+url_pos);
auto param_str = std::string(raw_url_without_frame.begin()+url_pos+1,raw_url_without_frame.end());
param = sf_parse_param(param_str);
}
inline std::string sf_to_header_key_format(std::string key) {
bool flag = false;
for (auto &k:key) {
if (0 != isalnum(k)) {
if (!flag)
k = static_cast<char>(toupper(k));
flag = true;
} else {
flag = false;
}
}
return key;
}
inline std::string sf_make_http_time_str(const std::chrono::system_clock::time_point &tp)
{
auto raw_time = std::chrono::system_clock::to_time_t(tp);
std::tm *time_info = std::localtime(&raw_time);
std::string ret(128,'\0');
strftime(ret.data(),128,"%a, %d %b %Y %T GMT",time_info);
ret.resize(strlen(ret.c_str()));
return ret;
}
inline byte_array read_file(const std::string &filename, size_t max_size)
{
byte_array data;
std::ifstream fi(filename, std::ios::in | std::ios::binary);
if(!fi){
return data;
}
fi.seekg(0,std::ios::end);
auto file_size = fi.tellg();
if(file_size>max_size)
{
file_size = max_size;
}
fi.seekg(0,std::ios::beg);
data.resize(file_size);
fi.read(data.data(),file_size);
fi.close();
return data;
}
}