-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathHttpClient.cpp
300 lines (255 loc) · 8.61 KB
/
HttpClient.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
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
// HttpClient.cpp
#include "HttpClient.h"
#include <string.h>
#include <string>
using std::string;
#define SPACE_CHARS " \t\r\n"
static string trim(const string& str) {
string::size_type pos1 = str.find_first_not_of(SPACE_CHARS);
if (pos1 == string::npos) return "";
string::size_type pos2 = str.find_last_not_of(SPACE_CHARS);
return str.substr(pos1, pos2 - pos1 + 1);
}
static inline bool is_unambiguous(char c) {
return (c >= '0' && c <= '9') ||
(c >= 'A' && c <= 'Z') ||
(c >= 'a' && c <= 'z') ||
c == '-' ||
c == '_' ||
c == '.' ||
c == '~';
}
static string escape(const string& istr) {
string ostr;
const char* p = istr.c_str();
int len = istr.size();
char szHex[4] = { 0 };
for (int i = 0; i < len; ++i) {
if (is_unambiguous(p[i])) {
ostr += p[i];
}
else {
sprintf_s(szHex, "%%%02X", p[i]);
ostr += szHex;
}
}
return ostr;
}
#define DEFAULT_TIMEOUT 10
HttpClient::HttpClient() {
m_timeout = DEFAULT_TIMEOUT;
}
HttpClient::~HttpClient() {
}
int HttpClient::Send(const HttpRequest& req, HttpResponse* res) {
return curl(req, res);
}
static size_t s_formget_cb(void* arg, const char* buf, size_t len) {
return len;
}
static size_t s_header_cb(char* buf, size_t size, size_t cnt, void* userdata) {
if (buf == NULL || userdata == NULL) return 0;
HttpResponse* res = (HttpResponse*)userdata;
string str(buf);
string::size_type pos = str.find_first_of(':');
if (pos == string::npos) {
if (res->version.empty() && strncmp(buf, "HTTP", 4) == 0) {
// status line
// HTTP/1.1 200 OK\r\n
char* space1 = strchr(buf, ' ');
char* space2 = strchr(space1 + 1, ' ');
if (space1 && space2) {
*space1 = '\0';
*space2 = '\0';
res->version = buf;
res->status_code = atoi(space1 + 1);
res->status_message = trim(space2 + 1);
}
}
}
else {
// headers
string key = trim(str.substr(0, pos));
string value = trim(str.substr(pos + 1));
res->headers[key] = value;
}
return size * cnt;
}
static size_t s_body_cb(char* buf, size_t size, size_t cnt, void* userdata) {
if (buf == NULL || userdata == NULL) return 0;
HttpResponse* res = (HttpResponse*)userdata;
res->body.append(buf, size * cnt);
return size * cnt;
}
int HttpClient::curl(const HttpRequest& req, HttpResponse* res) {
CURL* handle = curl_easy_init();
// SSL
curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, 0);
curl_easy_setopt(handle, CURLOPT_SSL_VERIFYHOST, 0);
// method
curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, req.method.c_str());
// url
curl_easy_setopt(handle, CURLOPT_URL, req.url.c_str());
// header
struct curl_slist* headers = NULL;
if (m_headers.size() != 0) {
for (auto& pair : m_headers) {
string header = pair.first;
header += ": ";
header += pair.second;
headers = curl_slist_append(headers, header.c_str());
}
}
const char* psz = "text/plain";
switch (req.content_type) {
#define CASE_CONTENT_TYPE(id, str) \
case id: psz = str; break;
FOREACH_CONTENT_TYPE(CASE_CONTENT_TYPE)
#undef CASE_CONTENT_TYPE
}
string strContentType("Content-type: ");
strContentType += psz;
headers = curl_slist_append(headers, strContentType.c_str());
curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers);
//hlogd("%s %s", req.method.c_str(), req.url.c_str());
//hlogd("%s", strContentType.c_str());
// body or params
struct curl_httppost* httppost = NULL;
struct curl_httppost* lastpost = NULL;
switch (req.content_type) {
case FORM_DATA:
{
for (auto& pair : req.form) {
CURLformoption opt = pair.second.type == FormData::FILENAME ? CURLFORM_FILE : CURLFORM_COPYCONTENTS;
curl_formadd(&httppost, &lastpost,
CURLFORM_COPYNAME, pair.first.c_str(),
opt, pair.second.data.c_str(),
CURLFORM_END);
}
if (httppost) {
curl_easy_setopt(handle, CURLOPT_HTTPPOST, httppost);
curl_formget(httppost, NULL, s_formget_cb);
}
}
break;
case QUERY_STRING:
case X_WWW_FORM_URLENCODED:
{
string params;
auto iter = req.kvs.begin();
while (iter != req.kvs.end()) {
if (iter != req.kvs.begin()) {
params += '&';
}
params += escape(iter->first);
params += '=';
params += escape(iter->second);
iter++;
}
if (req.content_type == QUERY_STRING) {
string url_with_params(req.url);
url_with_params += '?';
url_with_params += params;
curl_easy_setopt(handle, CURLOPT_URL, url_with_params.c_str());
//hlogd("%s", url_with_params.c_str());
}
else {
curl_easy_setopt(handle, CURLOPT_POSTFIELDS, params.c_str());
//hlogd("%s", params.c_str());
}
}
break;
default:
{
if (req.text.size() != 0) {
curl_easy_setopt(handle, CURLOPT_POSTFIELDS, req.text.c_str());
//hlogd("%s", req.text.c_str());
}
}
break;
}
if (m_timeout != 0) {
curl_easy_setopt(handle, CURLOPT_TIMEOUT, m_timeout);
}
curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, s_body_cb);
curl_easy_setopt(handle, CURLOPT_WRITEDATA, res);
curl_easy_setopt(handle, CURLOPT_HEADER, 0);
curl_easy_setopt(handle, CURLOPT_HEADERFUNCTION, s_header_cb);
curl_easy_setopt(handle, CURLOPT_HEADERDATA, res);
int ret = curl_easy_perform(handle);
if (ret != 0) {
//hloge("%d: %s", ret, curl_easy_strerror((CURLcode)ret));
}
//if (res->body.length() != 0) {
//hlogd("Response:%s", res->body.c_str());
//}
//double total_time, name_time, conn_time, pre_time;
//curl_easy_getinfo(handle, CURLINFO_TOTAL_TIME, &total_time);
//curl_easy_getinfo(handle, CURLINFO_NAMELOOKUP_TIME, &name_time);
//curl_easy_getinfo(handle, CURLINFO_CONNECT_TIME, &conn_time);
//curl_easy_getinfo(handle, CURLINFO_PRETRANSFER_TIME, &pre_time);
//hlogd("TIME_INFO: %lf,%lf,%lf,%lf", total_time, name_time, conn_time, pre_time);
if (headers) {
curl_slist_free_all(headers);
}
if (httppost) {
curl_formfree(httppost);
}
curl_easy_cleanup(handle);
return ret;
}
const char* HttpClient::strerror(int errcode) {
return curl_easy_strerror((CURLcode)errcode);
}
//下载文件数据接收函数
size_t dl_req_reply(void* buffer, size_t size, size_t nmemb, void* user_p)
{
FILE* fp = (FILE*)user_p;
size_t return_size = fwrite(buffer, size, nmemb, fp);
//cout << (char *)buffer << endl;
return return_size;
}
//http GET请求文件下载
CURLcode HttpClient::dl_curl_get_req(const std::string& url, std::string filename)
{
const char* file_name = filename.c_str();
char* pc = new char[1024];//足够长
strcpy_s(pc, strlen(file_name) + 1, file_name);
FILE* fp;
fopen_s(&fp, pc, "wb");
//curl初始化
CURL* curl = curl_easy_init();
// curl返回值
CURLcode res;
if (curl)
{
//设置curl的请求头
struct curl_slist* header_list = NULL;
header_list = curl_slist_append(header_list, "User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header_list);
//不接收响应头数据0代表不接收 1代表接收
curl_easy_setopt(curl, CURLOPT_HEADER, 0);
//设置请求的URL地址
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
//设置ssl验证
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
//CURLOPT_VERBOSE的值为1时,会显示详细的调试信息
curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);
curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
//设置数据接收函数
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &dl_req_reply);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
//设置超时时间
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 6); // set transport and time out time
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 6);
// 开启请求
res = curl_easy_perform(curl);
}
// 释放curl
curl_easy_cleanup(curl);
//释放文件资源
fclose(fp);
return res;
}