-
Notifications
You must be signed in to change notification settings - Fork 0
/
HttpRequest.cpp
141 lines (136 loc) · 4.2 KB
/
HttpRequest.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
#include "HttpRequest.h"
#include <algorithm>
#ifndef __GNUC__
#pragma comment(lib, "winhttp")
#endif
CHttpRequest::COptions::COptions() :
method(L"GET"), proxyList({}),
userAgent(L"Mozilla/5.0 (Windows NT x.y; Win64; x64; rv:10.0) Gecko/20100101 Firefox/10.0"),
headers({}), data({}), insecure(false) {
}
CHttpRequest::CUrl CHttpRequest::ParseUrl(std::wstring url) {
CHttpRequest::CUrl httpRequestUrl = {};
size_t index = url.find(L"://");
if (index != std::wstring::npos) {
std::wstring protocol = url.substr(0, index);
if (protocol == L"https") {
httpRequestUrl.winHttpFlags |= WINHTTP_FLAG_SECURE;
httpRequestUrl.port = 443;
} else if (protocol == L"http") {
if ((httpRequestUrl.winHttpFlags & WINHTTP_FLAG_SECURE) == WINHTTP_FLAG_SECURE) {
httpRequestUrl.winHttpFlags ^= WINHTTP_FLAG_SECURE;
}
httpRequestUrl.port = 80;
}
url = url.substr(index + 3);
index = url.find(L"/");
if (index == std::wstring::npos) {
httpRequestUrl.host = url;
httpRequestUrl.path = L"/";
} else {
httpRequestUrl.host = url.substr(0, index);
httpRequestUrl.path = url.substr(index);
}
index = httpRequestUrl.host.find(L":");
if (index != std::wstring::npos) {
httpRequestUrl.port = std::wcstol(httpRequestUrl.host.substr(index + 1).c_str(), nullptr, 10);
httpRequestUrl.host = httpRequestUrl.host.substr(0, index);
}
}
return httpRequestUrl;
}
std::vector<uint8_t> CHttpRequest::Request(const std::wstring& url, CHttpRequest::COptions options, uint32_t& statusCode) {
std::vector<uint8_t> result = {};
CHttpRequest::CUrl httpRequestUrl = ParseUrl(url);
DWORD statusCodeLen = sizeof(statusCode);
bool useProxies = options.proxyList.length() > 0;
HINTERNET hSession = WinHttpOpen(
options.userAgent.c_str(),
useProxies ? WINHTTP_ACCESS_TYPE_DEFAULT_PROXY
: WINHTTP_ACCESS_TYPE_NO_PROXY,
WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0
);
if (hSession) {
bool ok = true;
if (useProxies) {
WINHTTP_PROXY_INFO proxy = {};
proxy.dwAccessType = WINHTTP_ACCESS_TYPE_NAMED_PROXY;
proxy.lpszProxy = &options.proxyList[0];
ok = WinHttpSetOption(
hSession, WINHTTP_OPTION_PROXY, &proxy, sizeof(proxy)
);
}
HINTERNET hConn = WinHttpConnect(
hSession, httpRequestUrl.host.c_str(), httpRequestUrl.port, 0
);
if (hConn) {
HINTERNET hReq = WinHttpOpenRequest(
hConn, options.method.c_str(), httpRequestUrl.path.c_str(),
NULL, WINHTTP_NO_REFERER,
WINHTTP_DEFAULT_ACCEPT_TYPES, httpRequestUrl.winHttpFlags
);
if (hReq) {
if (options.insecure) {
DWORD dwFlags = SECURITY_FLAG_IGNORE_UNKNOWN_CA |
SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE |
SECURITY_FLAG_IGNORE_CERT_CN_INVALID |
SECURITY_FLAG_IGNORE_CERT_DATE_INVALID;
ok = WinHttpSetOption(
hReq, WINHTTP_OPTION_SECURITY_FLAGS,
&dwFlags, sizeof(dwFlags)
);
}
if (ok) {
LPVOID data = WINHTTP_NO_REQUEST_DATA;
DWORD dataLen = options.data.size();
if (dataLen > 0) {
data = (LPVOID)options.data.data();
}
for (const auto& header : options.headers) {
WinHttpAddRequestHeaders(
hReq,
(header.first + L": " + header.second).c_str(),
(ULONG)-1L,
WINHTTP_ADDREQ_FLAG_ADD
);
}
ok = WinHttpSendRequest(
hReq, WINHTTP_NO_ADDITIONAL_HEADERS, 0,
data, dataLen, dataLen, 0
);
if (ok) {
ok = WinHttpReceiveResponse(hReq, NULL);
if (ok) {
ok = WinHttpQueryHeaders(
hReq,
WINHTTP_QUERY_STATUS_CODE |
WINHTTP_QUERY_FLAG_NUMBER,
NULL,
&statusCode,
&statusCodeLen,
0
);
if (ok) {
CHAR buff[8192];
std::fill_n(buff, sizeof(buff), 0);
DWORD read = 0;
do {
ok = WinHttpReadData(hReq, buff, sizeof(buff), &read);
if (!ok) {
break;
}
result.insert(result.end(), buff, buff + read);
std::fill_n(buff, read, 0);
} while (read > 0);
}
}
}
}
WinHttpCloseHandle(hReq);
}
WinHttpCloseHandle(hConn);
}
WinHttpCloseHandle(hSession);
}
return result;
}