forked from cool2528/baiduCDP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Http_Request.cpp
402 lines (380 loc) · 10.5 KB
/
Http_Request.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
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
#include "Http_Request.h"
#include <set>
#include <stdexcept>
#ifdef _DEBUG
#pragma comment(lib,"libcurld.lib")
#else
#pragma comment(lib,"libcurl.lib")
#endif
HttpRequest::HttpRequest()
:m_CurlHandle(NULL),
headerList(nullptr),
m_Cookies("")
{
try{
CURLcode rcode = curl_global_init(CURL_GLOBAL_ALL);
if (rcode)
throw rcode;
m_CurlHandle = curl_easy_init();
if (m_CurlHandle)
{
curl_easy_setopt(m_CurlHandle, CURLOPT_SSL_VERIFYPEER, false); // 验证对方的SSL证书
curl_easy_setopt(m_CurlHandle, CURLOPT_SSL_VERIFYHOST, false); //根据主机验证证书的名称
}
}
catch (CURLcode& errCode)
{
//输出日志
throw std::runtime_error("LibCurl initialization environment failed");
}
}
HttpRequest::~HttpRequest()
{
if (headerList)
{
curl_slist_free_all(headerList);
headerList = nullptr;
}
if (m_CurlHandle)
{
curl_easy_cleanup(m_CurlHandle);
m_CurlHandle = nullptr;
}
curl_global_cleanup(); //关闭curl环境
}
void HttpRequest::SetRequestHeader(const std::string strKey, const std::string strValue)
{
std::string strRequestHeader(strKey + ":" + strValue);
headerList = curl_slist_append(headerList, strRequestHeader.c_str());
}
void HttpRequest::SetRequestHeader(RequestHeaderValue& HeaderValue)
{
for (auto &v : HeaderValue)
{
std::string strRequestHeader(v.first + ":" + v.second);
headerList = curl_slist_append(headerList, strRequestHeader.c_str());
}
}
void HttpRequest::Send(RequestType Enumtype, const std::string strUrl,const std::string strPost)
{
CURLcode dwCurlCode;
if (!m_CurlHandle)return;
m_Request.clear();
m_ResponseHeader.clear();
m_Cookies.clear();
switch (Enumtype)
{
case GET:
{
AutoAddHeader();
if (!CheckHeaderExist("Referer:"))
{
headerList = curl_slist_append(headerList, ("Referer:" + strUrl).c_str());
}
//设置URL
curl_easy_setopt(m_CurlHandle, CURLOPT_URL, strUrl.c_str());
curl_easy_setopt(m_CurlHandle, CURLOPT_HTTPHEADER, headerList); //设置自定义协议头头
curl_easy_setopt(m_CurlHandle, CURLOPT_HEADERFUNCTION, header_callback); //设置回调协议头函数
curl_easy_setopt(m_CurlHandle, CURLOPT_HEADERDATA, &m_ResponseHeader);
curl_easy_setopt(m_CurlHandle, CURLOPT_WRITEFUNCTION, read_callback); //设置写的回调函数
curl_easy_setopt(m_CurlHandle, CURLOPT_WRITEDATA, &m_Request); //接收返回的数据
curl_easy_setopt(m_CurlHandle, CURLOPT_NOBODY, 0); //接收返回的数据
dwCurlCode = curl_easy_perform(m_CurlHandle);
if (CURLE_OK != dwCurlCode)
{
//输出日志
}
m_Cookies = GetCookies(GetallResponseHeaders());
}
break;
case POST:
{
AutoAddHeader();
if (!CheckHeaderExist("Referer:"))
{
headerList = curl_slist_append(headerList, ("Referer:" + strUrl).c_str());
}
if (!CheckHeaderExist("Content-Type:"))
{
headerList = curl_slist_append(headerList, ("Content-Type: application/x-www-form-urlencoded"));
}
//设置URL
curl_easy_setopt(m_CurlHandle, CURLOPT_URL, strUrl.c_str());
curl_easy_setopt(m_CurlHandle, CURLOPT_HTTPHEADER, headerList); //设置自定义协议头头
curl_easy_setopt(m_CurlHandle, CURLOPT_POSTFIELDSIZE, (long)strPost.length()); //设置需要发送的数据大小
curl_easy_setopt(m_CurlHandle, CURLOPT_POSTFIELDS, strPost.c_str()); //设置需要发送的数据
curl_easy_setopt(m_CurlHandle, CURLOPT_HEADERFUNCTION, header_callback); //设置回调协议头函数
curl_easy_setopt(m_CurlHandle, CURLOPT_HEADERDATA, &m_ResponseHeader);
curl_easy_setopt(m_CurlHandle, CURLOPT_WRITEFUNCTION, read_callback); //设置写的回调函数
curl_easy_setopt(m_CurlHandle, CURLOPT_WRITEDATA, &m_Request); //接收返回的数据
curl_easy_setopt(m_CurlHandle, CURLOPT_NOBODY, 0); //接收返回的数据
dwCurlCode = curl_easy_perform(m_CurlHandle);
if (CURLE_OK != dwCurlCode)
{
//输出日志
}
m_Cookies = GetCookies(GetallResponseHeaders());
}
break;
case HEAD:
{
AutoAddHeader();
if (!CheckHeaderExist("Referer:"))
{
headerList = curl_slist_append(headerList, ("Referer:" + strUrl).c_str());
}
//设置URL
curl_easy_setopt(m_CurlHandle, CURLOPT_URL, strUrl.c_str());
curl_easy_setopt(m_CurlHandle, CURLOPT_HTTPHEADER, headerList); //设置自定义协议头头
curl_easy_setopt(m_CurlHandle, CURLOPT_HEADERFUNCTION, header_callback); //设置回调协议头函数
curl_easy_setopt(m_CurlHandle, CURLOPT_HEADERDATA, &m_ResponseHeader);
curl_easy_setopt(m_CurlHandle, CURLOPT_WRITEFUNCTION, read_callback); //设置写的回调函数
curl_easy_setopt(m_CurlHandle, CURLOPT_WRITEDATA, &m_Request); //接收返回的数据
curl_easy_setopt(m_CurlHandle, CURLOPT_NOBODY, 1); //接收返回的数据
dwCurlCode = curl_easy_perform(m_CurlHandle);
if (CURLE_OK != dwCurlCode)
{
//输出日志
}
m_Cookies = GetCookies(GetallResponseHeaders());
}
default:
break;
}
if (headerList)
{
curl_slist_free_all(headerList);
headerList = nullptr;
}
}
std::string HttpRequest::GetResponseText()
{
std::string strResultText;
for (auto v: m_Request)
strResultText.append(1, v);
return strResultText;
}
responseData HttpRequest::GetResponseBody() const
{
return m_Request;
}
std::string HttpRequest::GetallResponseHeaders()
{
std::string strResultText;
for (auto v : m_ResponseHeader)
strResultText.append(1, v);
return strResultText;
}
void HttpRequest::SetRequestCookies(const std::string strCookie)
{
if (!m_CurlHandle)return;
curl_easy_setopt(m_CurlHandle, CURLOPT_COOKIE, strCookie.c_str());
}
void HttpRequest::SetHttpRedirect(bool val)
{
if (!m_CurlHandle)return;
curl_easy_setopt(m_CurlHandle, CURLOPT_FOLLOWLOCATION, (long)val);
}
std::string HttpRequest::GetResponCookie() const
{
return m_Cookies;
}
void HttpRequest::AutoAddHeader()
{
if (!headerList || !CheckHeaderExist("Accept:"))
{
headerList = curl_slist_append(headerList, "Accept:*/*");
}
if (!CheckHeaderExist("Accept-Language:"))
{
headerList = curl_slist_append(headerList, "Accept-Language:zh-cn");
}
if (!CheckHeaderExist("User-Agent:"))
{
headerList = curl_slist_append(headerList, std::string(std::string("User-Agent:") + USER_AGENT).c_str());
}
}
std::string HttpRequest::GetCookies(const std::string strHeader)
{
std::string strResult, strTwoResult;
std::vector<std::string> headerList;
std::set<std::string> resultList;
if (!strHeader.empty())
{
std::string split = strHeader + "\r\n";
std::string strsub;
size_t pos = 0;
size_t npos = 0;
npos = split.find("\r\n", pos);
while (npos != std::string::npos)
{
strsub = split.substr(pos, npos - pos);
pos = npos + strlen("\r\n");
npos = split.find("\r\n", pos);
headerList.push_back(strsub);
}
}
for (auto & v : headerList)
{
std::string strTmp;
int nPos = v.find("Set-Cookie:");
if (nPos != std::string::npos)
{
std::string strTmp = v.substr(nPos + lstrlenA("Set-Cookie:"), v.length() - (nPos + lstrlenA("Set-Cookie:")));;
if (strTmp.at(strTmp.length() - 1) != ';')
{
strTmp += ";";
}
strResult += strTmp;
}
}
headerList.clear();
if (!strResult.empty())
{
if (strResult.at(strResult.length() - 1) != ';')
strResult += ";";
std::string split = strResult;
std::string strsub;
size_t pos = 0;
size_t npos = 0;
npos = split.find(";", pos);
while (npos != std::string::npos)
{
strsub = split.substr(pos, npos - pos);
pos = npos + strlen(";");
npos = split.find(";", pos);
resultList.insert(strsub);
}
}
else
{
return strResult;
}
for (auto& s : resultList)
strTwoResult += s + ";";
return strTwoResult;
}
std::vector<std::string> HttpRequest::StrSplit(const std::string &str, const std::string &pattern)
{
std::vector<std::string> resultVec;
std::string split = str;
std::string strsub;
size_t pos = 0;
size_t npos = 0;
npos = split.find(pattern, pos);
while (npos != std::string::npos)
{
strsub = split.substr(pos, npos - pos);
pos = npos + pattern.length();
npos = split.find(pattern, pos);
resultVec.push_back(strsub);
}
return resultVec;
}
RequestHeaderValue HttpRequest::SplitCookie(const std::string strCookie)
{
RequestHeaderValue result;
std::vector<std::string> strCookieArr = StrSplit(strCookie, ";");
for (auto v : strCookieArr)
{
int nPos = std::string::npos;
int nNext = std::string::npos;
nPos = v.find("=");
if (nPos!=std::string::npos)
{
std::string name = v.substr(0, nPos);
std::string value = v.substr(nPos + 1, v.length() - (nPos + 1));
result.insert({ name,value });
}
}
return result;
}
std::string HttpRequest::MergeCookie(const std::string wornCookie, const std::string newCookie)
{
std::string strCookies;
std::string strSource;
std::string strDest;
strSource = wornCookie;
strDest = newCookie;
if (newCookie.empty())
return wornCookie;
if (strSource.at(strSource.length() - 1) != ';')
strSource += ";";
if (strDest.at(strDest.length() - 1) != ';')
strDest += ";";
RequestHeaderValue strResult = SplitCookie(strSource);
RequestHeaderValue strNewCookie = SplitCookie(strDest);
for (auto v : strNewCookie)
{
std::string key = v.first;
auto it = strResult.find(key);
if (it!=strResult.end())
{
strResult.find(key)->second = v.second;
}
else
{
strResult.insert({ v.first,v.second });
}
}
for (auto k: strResult)
strCookies += k.first + "=" + k.second + ";";
#if 0
for (auto v : strCookieArr)
{
int npos = std::string::npos;
int nNextPos = std::string::npos;
RequestHeaderValue item;
item = SplitCookie(v);
if (item.size()==2)
{
std::string strName = item.at("name");
std::string strKey = item.at("key");
npos = strResult.find(strName);
if (npos != std::string::npos)
{
nNextPos = strResult.find(";", npos);
if (nNextPos!=std::string::npos)
{
strResult = strResult.replace(npos + strName.length(), nNextPos, strKey, 0, strKey.length());
}
}
else
{
strResult += v + ";";
}
}
}
#endif
return strCookies;
}
bool HttpRequest::CheckHeaderExist(const std::string strHeaderKey)
{
bool isBresult = false;
struct curl_slist* TmpheaderList = headerList;
while (TmpheaderList)
{
std::string tmpHeaderstr = TmpheaderList->data;
if (tmpHeaderstr.find(strHeaderKey)!=std::string::npos)
{
isBresult = true;
break;
}
TmpheaderList = TmpheaderList->next;
}
return isBresult;
}
size_t HttpRequest::header_callback(char *ptr, size_t size, size_t nmemb, void* userdata)
{
size_t lsize = size * nmemb;
for (size_t i = 0; i < lsize; i++)
((responseData*)userdata)->push_back(ptr[i]);
return lsize;
}
size_t HttpRequest::read_callback(char *ptr, size_t size, size_t nmemb, void* userdata)
{
size_t lsize = size * nmemb;
for (size_t i = 0; i < lsize; i++)
((responseData*)userdata)->push_back(ptr[i]);
return lsize;
}