-
Notifications
You must be signed in to change notification settings - Fork 6
/
http.cpp
382 lines (324 loc) · 10.5 KB
/
http.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
#include "http.h"
CWinHttp::CWinHttp()
{
}
CWinHttp::~CWinHttp()
{
if (m_hRequest) WinHttpCloseHandle(m_hRequest);
if (m_hConnect) WinHttpCloseHandle(m_hConnect);
if (m_hSession) WinHttpCloseHandle(m_hSession);
}
BOOL CWinHttp::OpenSession(const std::string &userAgent, int dwAccessType, const std::string &proxyName, const std::string &proxyBypass, int dwFlags)
{
LPWSTR pwszUserAgent = NULL;
std::wstring wstrUserAgent;
if (!userAgent.empty()) {
wstrUserAgent = CEncoder::A2W_(userAgent);
pwszUserAgent = const_cast<LPWSTR>(wstrUserAgent.c_str());
}
LPWSTR pwszProxyName = NULL;
std::wstring wstrProxyName;
if (!proxyName.empty()) {
wstrProxyName = CEncoder::A2W_(proxyName);
pwszProxyName = const_cast<LPWSTR>(wstrProxyName.c_str());
}
LPWSTR pwszProxyBypass = NULL;
std::wstring wstrProxyBypass;
if (!proxyBypass.empty()) {
wstrProxyBypass = CEncoder::U2W_(proxyBypass);
pwszProxyBypass = const_cast<LPWSTR>(wstrProxyBypass.c_str());
}
if (dwAccessType == WINHTTP_ACCESS_TYPE_NO_PROXY)
m_hSession = WinHttpOpen(pwszUserAgent, dwAccessType, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, dwFlags); //不使用代理IP
else
m_hSession = WinHttpOpen(pwszUserAgent, dwAccessType, pwszProxyName, pwszProxyBypass, dwFlags); //使用代理IP
return m_hSession != NULL;
}
BOOL CWinHttp::OpenConnect(const std::string &url)
{
if (m_hSession)
{
if (WinHttpCheckPlatform())
{
URL_COMPONENTS urlComp;
ZeroMemory(&urlComp, sizeof(urlComp));
urlComp.dwStructSize = sizeof(urlComp);
WCHAR wszScheme[64] = { 0 };
urlComp.lpszScheme = wszScheme;
urlComp.dwSchemeLength = ARRAYSIZE(wszScheme);
WCHAR wszHostName[1024] = { 0 };
urlComp.lpszHostName = wszHostName;
urlComp.dwHostNameLength = ARRAYSIZE(wszHostName);
WCHAR wszUrlPath[1024] = { 0 };
urlComp.lpszUrlPath = wszUrlPath;
urlComp.dwUrlPathLength = ARRAYSIZE(wszUrlPath);
WCHAR wszExtraInfo[1024] = { 0 };
urlComp.lpszExtraInfo = wszExtraInfo;
urlComp.dwExtraInfoLength = ARRAYSIZE(wszExtraInfo);
std::wstring wstrUrl = CEncoder::U2W_(url.c_str());
if (WinHttpCrackUrl(wstrUrl.c_str(), wstrUrl.length(), ICU_ESCAPE, &urlComp))
{
m_strHost = urlComp.lpszHostName;
m_strPath = urlComp.lpszUrlPath;
m_strExt = urlComp.lpszExtraInfo;
m_nPort = urlComp.nPort;
m_nScheme = urlComp.nScheme;
m_hConnect = WinHttpConnect(m_hSession, m_strHost.c_str(), m_nPort, 0);
return m_hConnect != NULL;
}
}
}
return FALSE;
}
BOOL CWinHttp::OpenRequest(bool bPost, bool bInitContentType)
{
if (m_hConnect)
{
const wchar_t* pwszVerb = bPost ? L"POST" : L"GET";
DWORD dwFlags = m_nScheme == INTERNET_SCHEME_HTTPS ? WINHTTP_FLAG_SECURE : 0;
m_hRequest = WinHttpOpenRequest(m_hConnect, pwszVerb, m_strPath.c_str(), NULL,
WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, dwFlags);
if (bPost && bInitContentType)
SetRequestHeader("Content-Type", "application/x-www-form-urlencoded");
return m_hRequest != NULL;
}
return FALSE;
}
BOOL CWinHttp::Send(LPVOID lpbuffer, DWORD dwsize)
{
BOOL bResults = FALSE;
if (m_hRequest) {
if (lpbuffer != NULL && dwsize > 0)
bResults = WinHttpSendRequest(m_hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, lpbuffer, dwsize, dwsize, 0);
else
bResults = WinHttpSendRequest(m_hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0);
}
return bResults;
}
BOOL CWinHttp::Write(LPCVOID lpbuffer, DWORD dwsize)
{
if (m_hRequest)
{
return WinHttpWriteData(m_hRequest, lpbuffer, dwsize, NULL);
}
return FALSE;
}
std::vector<BYTE> CWinHttp::GetResponseBody()
{
DWORD dwSize = 0;
DWORD dwDownloaded = 0;
std::vector<BYTE> list;
if (WinHttpReceiveResponse(m_hRequest, NULL))
{
do
{
// Check for available data.
dwSize = 0;
if (!WinHttpQueryDataAvailable(m_hRequest, &dwSize))
{
printf("Error %u in WinHttpQueryDataAvailable.\n", GetLastError());
break;
}
// No more available data.
if (!dwSize)
break;
// Allocate space for the buffer.
//pszOutBuffer = new char[dwSize + 1];
BYTE* lpReceivedData = new BYTE[dwSize];
if (!lpReceivedData)
{
printf("Out of memory\n");
break;
}
else
{
// Read the Data.
ZeroMemory(lpReceivedData, dwSize);
if (!WinHttpReadData(m_hRequest, lpReceivedData, dwSize, &dwDownloaded)) {
printf("Error %u in WinHttpReadData.\n", GetLastError());
}
else {
for (size_t i = 0; i < dwSize; i++)
{
list.push_back(lpReceivedData[i]);
}
}
// Free the memory allocated to the buffer.
delete[] lpReceivedData;
// This condition should never be reached since WinHttpQueryDataAvailable
// reported that there are bits to read.
if (!dwDownloaded)
break;
}
} while (dwSize > 0);
}
return list;
}
std::string CWinHttp::GetResponseHeaderValue(int dwInfoLevel, DWORD dwIndex)
{
DWORD dwSize = 0;
LPVOID lpOutBuffer = NULL;
BOOL bResults = FALSE;
bResults = WinHttpQueryHeaders(m_hRequest, dwInfoLevel, WINHTTP_HEADER_NAME_BY_INDEX, NULL, &dwSize, &dwIndex);
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
{
lpOutBuffer = new WCHAR[dwSize / sizeof(WCHAR)];
bResults = WinHttpQueryHeaders(m_hRequest, dwInfoLevel, WINHTTP_HEADER_NAME_BY_INDEX, lpOutBuffer, &dwSize, &dwIndex);
return CEncoder::W2A_((wchar_t*)lpOutBuffer);
}
return "";
}
std::string CWinHttp::GetResponseHeaderValue(const std::string &name)
{
DWORD dwSize = 0;
DWORD dwIndex = 0;
LPVOID lpOutBuffer = NULL;
BOOL bResults = FALSE;
std::wstring strHeaderName;
strHeaderName = CEncoder::A2W_(name);
bResults = WinHttpQueryHeaders(m_hRequest, WINHTTP_QUERY_CUSTOM, strHeaderName.c_str(), NULL, &dwSize, &dwIndex);
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
{
lpOutBuffer = new WCHAR[dwSize / sizeof(WCHAR)];
bResults = WinHttpQueryHeaders(m_hRequest, WINHTTP_QUERY_CUSTOM, strHeaderName.c_str(), lpOutBuffer, &dwSize, &dwIndex);
return CEncoder::W2A_((wchar_t*)lpOutBuffer);
}
return "";
}
std::string CWinHttp::GetResponseHeaders()
{
return GetResponseHeaderValue(WINHTTP_QUERY_RAW_HEADERS_CRLF);
}
BOOL CWinHttp::SetRequestHeader(const std::string &name, const std::string &value)
{
if (name.empty() || value.empty())
return FALSE;
BOOL bResults = FALSE;
if (m_hRequest)
{
std::string strHeader(name + ": " + value);
LPWSTR pwszHeaders = NULL;
std::wstring wstrHeaders;
if (!strHeader.empty()) {
wstrHeaders = CEncoder::A2W_(strHeader);
pwszHeaders = const_cast<LPWSTR>(wstrHeaders.c_str());
bResults = WinHttpAddRequestHeaders(m_hRequest, pwszHeaders, (ULONG)-1L, WINHTTP_ADDREQ_FLAG_ADD_IF_NEW | WINHTTP_ADDREQ_FLAG_REPLACE);
}
}
return bResults;
}
BOOL CWinHttp::SetReferer(const std::string &referer)
{
return(SetRequestHeader("Referer", referer));
}
BOOL CWinHttp::SetTimeout(int nResolveTimeout, int nConnectTimeout, int nSendTimeout, int nReceiveTimeout)
{
if (m_hSession)
return WinHttpSetTimeouts(m_hSession, nResolveTimeout, nConnectTimeout, nSendTimeout, nReceiveTimeout);
return FALSE;
}
BOOL CWinHttp::SetOption(int Option, int value)
{
if (m_hRequest)
return WinHttpSetOption(m_hRequest, Option, (LPVOID)&value, 4);
return FALSE;
}
BOOL CWinHttp::SetLocal(bool Is)
{
return SetOption(WINHTTP_OPTION_REDIRECT_POLICY, Is ? WINHTTP_OPTION_REDIRECT_POLICY_ALWAYS : WINHTTP_OPTION_REDIRECT_POLICY_NEVER);
}
std::string CWinHttp::GetLocal()
{
return GetResponseHeaderValue(WINHTTP_QUERY_LOCATION);
}
BOOL CWinHttp::SetCookie(const std::string &cookies)
{
return(SetRequestHeader("cookie", cookies));
}
std::string CWinHttp::GetCookie()
{
std::map<std::string, std::string> map_cookies;
std::map<std::string, std::string>::iterator iter;
std::string strHeaders;
strHeaders = GetResponseHeaders();
strHeaders = str_replace(strHeaders, "Set-Cookie: ", "Set-Cookie:");
std::vector<std::string> list = str_between_array(strHeaders, "Set-Cookie:", "\r\n", true);
std::vector<std::string> array_list;
for (size_t i = 0; i < list.size(); ++i) {
std::vector<std::string> temp_list = str_split(list[i], ";", true);
array_list.insert(array_list.end(), temp_list.begin(), temp_list.end());
}
for (size_t i = 0; i < array_list.size(); ++i) {
std::string name = str_left(array_list[i], "=");
std::string value = str_right(array_list[i], "=");
name = str_trim(name);
//value = str_trim(value);
if (!name.empty() && !value.empty())
{
iter = map_cookies.find(name);
if (iter != map_cookies.end())
iter->second = value;
else
map_cookies.insert(make_pair(name, value));
}
}
std::string cookies;
for (iter = map_cookies.begin(); iter != map_cookies.end(); ++iter)
{
if (iter->second == "-" || iter->second == "''")
continue;
if (cookies.empty())
cookies.append(iter->first).append("=").append(iter->second);
else
cookies.append("; ").append(iter->first).append("=").append(iter->second);
}
return cookies;
//DWORD dwSize = 0;
//DWORD dwIndex = 0;
//LPVOID lpOutBuffer = NULL;
//std::wstring result;
//do
//{
// dwSize = 0;
// WinHttpQueryHeaders(m_hRequest, WINHTTP_QUERY_SET_COOKIE, WINHTTP_HEADER_NAME_BY_INDEX, NULL, &dwSize, &dwIndex);
// lpOutBuffer = new WCHAR[dwSize / sizeof(WCHAR)];
// WinHttpQueryHeaders(m_hRequest, WINHTTP_QUERY_SET_COOKIE, WINHTTP_HEADER_NAME_BY_INDEX, lpOutBuffer, &dwSize, &dwIndex);
// if (dwSize > 0)
// result.append((wchar_t *)lpOutBuffer).append(L"\n");
// delete lpOutBuffer;
//} while (dwSize > 0);
//return CEncoder::W2A_(result);
}
std::string CWinHttp::MergeCookie(const std::string &oldCookies, const std::string &nowCookies)
{
std::map<std::string, std::string> map_cookies;
std::map<std::string, std::string>::iterator iter;
std::vector<std::string> array_list = str_split(oldCookies, ";", true);
std::vector<std::string> now_array_list = str_split(nowCookies, ";", true);
array_list.insert(array_list.end(), now_array_list.begin(), now_array_list.end());
for (size_t i = 0; i < array_list.size(); ++i) {
std::string name = str_left(array_list[i], "=");
std::string value = str_right(array_list[i], "=");
name = str_trim(name);
//value = str_trim(value);
if (!name.empty() && !value.empty())
{
iter = map_cookies.find(name);
if (iter != map_cookies.end())
iter->second = value;
else
map_cookies.insert(make_pair(name, value));
}
}
std::string cookies;
for (iter = map_cookies.begin(); iter != map_cookies.end(); ++iter)
{
if (iter->second == "-" || iter->second == "''")
continue;
if (cookies.empty())
cookies.append(iter->first).append("=").append(iter->second);
else
cookies.append("; ").append(iter->first).append("=").append(iter->second);
}
return cookies;
}