-
Notifications
You must be signed in to change notification settings - Fork 0
/
WinhttpAPI.cpp
166 lines (144 loc) · 6.98 KB
/
WinhttpAPI.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
#include "WinhttpAPI.h"
#include "CodeConvert/CodeCvt.h"
#include "ParamsProcess/ParamsProcess.h"
#include "WinHttpInterface/APIInterface.h"
using std::string;
using std::wstring;
using std::runtime_error;
void printException(std::exception &error, DWORD ec = 0) {
std::cout << std::hex << "[-]error: " << error.what() << " ec: " << ec << '\n';
}
namespace Winhttp {
void WinhttpAPI::Request() {
try {
if (pHttpRequest == nullptr || pHttpResponse == nullptr)
throw runtime_error("unset httprequest or httpresponse");
Request(*pHttpRequest, *pHttpResponse);
} catch (runtime_error &error) {
printException(error, 0);
}
}
void WinhttpAPI::Request(HttpRequestT &HttpRequest, HttpResponseT &HttpResponse) {
DWORD lastError = 0;
try {
// Check
if (HttpRequest.url.empty() || HttpRequest.protocol.empty())
throw runtime_error("unset url or protocol");
// ClearLastResponseData
if (!HttpResponse.Body.empty() || !HttpResponse.Headers.empty()) {
HttpResponse.Body.clear();
HttpResponse.Headers.clear();
}
// Declare
DWORD dwFlag = 0;
WinHttpInterface httpInterface;
// Open
wstring wszUA = CodeCvt::StrToWstr(ParamProcess::GetUA(this->headers), CP_ACP);
wstring wszProxy = CodeCvt::StrToWstr(HttpRequest.proxy, CP_ACP);
wstring wszProxyBypass = CodeCvt::StrToWstr(HttpRequest.proxyBypass, CP_ACP);
if ((lastError = httpInterface.Open(wszUA, wszProxy, wszProxyBypass, 0)) != 0)
throw runtime_error("failed Open");
// Crack
wstring wszUrl = CodeCvt::StrToWstr(HttpRequest.url, CP_ACP);
URL_COMPONENTS UrlComponents = ParamProcess::InitUrlComponents();
if ((lastError = httpInterface.CrackUrl(wszUrl, wszUrl.length(), 0, UrlComponents)) != 0)
throw runtime_error("failed CrackUrl");
// Connect
wstring wszUrlHostName(&UrlComponents.lpszHostName[0],
&UrlComponents.lpszHostName[UrlComponents.dwHostNameLength]);
if ((lastError = httpInterface.Connect(wszUrlHostName, UrlComponents.nPort)) != 0)
throw runtime_error("failed Connect");
// OpenRequest
wstring wszModel = CodeCvt::StrToWstr(HttpRequest.protocol, CP_ACP);
switch (UrlComponents.nScheme) {
case INTERNET_SCHEME_HTTP:
dwFlag = 0;
break;
case INTERNET_SCHEME_HTTPS:
dwFlag = WINHTTP_FLAG_SECURE;
break;
}
if ((lastError = httpInterface.OpenRequest(wszModel, UrlComponents.lpszUrlPath, L"", dwFlag)) != 0)
throw runtime_error("failed OpenRequest");
// SetTimeOut
httpInterface.SetTimeOut(HttpRequest.timeout.resolveTimeout, HttpRequest.timeout.connectTimeout,
HttpRequest.timeout.sendTimeout, HttpRequest.timeout.receiveTimeout);
// SetOption
if (HttpRequest.winhttpOption.dwOption != 0)
httpInterface.SetOption(HttpRequest.winhttpOption.dwOption, HttpRequest.winhttpOption.lpBuffer);
// AddRequestHeaders
for (const auto &ch: this->headers) {
wstring header = (CodeCvt::StrToWstr(ch.first + ": " + ch.second + "\r\n", CP_ACP));
httpInterface.AddRequestHeaders(header, 0);
}
// SendRequest
if ((lastError = httpInterface.SendRequest(L"", HttpRequest.body, HttpRequest.body.length(),
HttpRequest.body.length())) != 0)
throw runtime_error("failed SendRequest");
// ReceiveResponse
if ((lastError = httpInterface.ReceiveResponse()) != 0)
throw runtime_error("failed ReceiveResponse");
// QueryHeaders
DWORD dwBufLen = httpInterface.QueryHeaders(nullptr, 0);
if (dwBufLen > 0) {
std::unique_ptr<wchar_t[]> wBuf = std::make_unique<wchar_t[]>(dwBufLen);
memset(wBuf.get(), 0, dwBufLen);
httpInterface.QueryHeaders(wBuf.get(), dwBufLen);
std::unique_ptr<char[]> cBuf = CodeCvt::WstrToStr(wBuf.get(), CP_ACP);
HttpResponse.Headers.append(cBuf.get(), strlen(cBuf.get()));
}
// QueryDataAvailable & ReadData
if ((lastError = httpInterface.QueryDataAvailable(dwBufLen)) == 0 && dwBufLen > 0) {
std::unique_ptr<std::FILE, std::function<void(std::FILE *)>> pFile([&]() -> std::FILE * {
if (HttpRequest.saveMethod.downloadMethod == HttpRequestT::SaveMethodT::STRING_STREAM)
return nullptr;
return fopen(HttpRequest.saveMethod.downloadPath.c_str(), "wb+");
}(), [](std::FILE *FilePtr) {
if (FilePtr != nullptr) fclose(FilePtr);
});
std::function<void(char *, DWORD)> writeData;
if (HttpRequest.saveMethod.downloadMethod == HttpRequestT::SaveMethodT::STRING_STREAM) {
writeData = [&HttpResponse](char *Buf, DWORD BufLen) {
HttpResponse.Body.append(Buf, BufLen);
};
} else {
writeData = [&pFile](char *Buf, DWORD BufLen) {
fwrite(Buf, BufLen, 1, pFile.get());
};
}
do {
std::unique_ptr<char[]> charBuf = std::make_unique<char[]>(dwBufLen);
httpInterface.ReadData(charBuf.get(), dwBufLen);
writeData(charBuf.get(), dwBufLen);
lastError = httpInterface.QueryDataAvailable(dwBufLen);
} while (lastError == 0 && dwBufLen > 0);
if (lastError != 0)
throw runtime_error("unknow error");
}
} catch (runtime_error &error) {
printException(error, lastError);
}
}
bool WinhttpAPI::SetHeader(const string &Key, const string &Value) {
if (Key.empty())
return false;
if (Value.empty())
this->headers.erase(Key);
else if (this->headers.find(Key) != this->headers.end())
this->headers[Key, Value];
else
this->headers.insert({Key, Value});
return true;
}
bool WinhttpAPI::SetHeaders(const std::map<string, string> &KeyValue) {
for (auto &item: KeyValue)
if (!this->SetHeader(item.first, item.second))
return false;
return true;
}
string WinhttpAPI::GetHeader(const string &Key) {
if (Key.empty() || this->headers.find(Key) == this->headers.end())
return {};
return this->headers[Key];
}
}