2019-06-13 16:01:29 +08:00
|
|
|
#include "network.h"
|
|
|
|
|
|
|
|
HttpRequest::HttpRequest(
|
|
|
|
const wchar_t* agentName,
|
|
|
|
const wchar_t* serverName,
|
|
|
|
const wchar_t* action,
|
|
|
|
const wchar_t* objectName,
|
2020-03-30 10:55:12 +08:00
|
|
|
std::string body,
|
|
|
|
const wchar_t* headers,
|
2021-01-15 21:07:23 +08:00
|
|
|
DWORD port,
|
2020-03-30 10:55:12 +08:00
|
|
|
const wchar_t* referrer,
|
2019-06-13 16:01:29 +08:00
|
|
|
DWORD requestFlags,
|
|
|
|
const wchar_t* httpVersion,
|
2021-01-15 21:07:23 +08:00
|
|
|
const wchar_t** acceptTypes
|
2019-06-13 16:01:29 +08:00
|
|
|
)
|
|
|
|
{
|
|
|
|
static std::atomic<HINTERNET> internet = NULL;
|
|
|
|
if (!internet) internet = WinHttpOpen(agentName, WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, NULL, NULL, 0);
|
|
|
|
if (internet)
|
2020-10-14 08:37:03 +08:00
|
|
|
if (InternetHandle connection = WinHttpConnect(internet, serverName, port, 0))
|
2019-06-13 16:01:29 +08:00
|
|
|
if (InternetHandle request = WinHttpOpenRequest(connection, action, objectName, httpVersion, referrer, acceptTypes, requestFlags))
|
2020-03-30 10:55:12 +08:00
|
|
|
if (WinHttpSendRequest(request, headers, -1UL, body.empty() ? NULL : body.data(), body.size(), body.size(), NULL))
|
2019-06-13 16:01:29 +08:00
|
|
|
{
|
2020-04-02 17:07:32 +08:00
|
|
|
WinHttpReceiveResponse(request, NULL);
|
2020-08-12 15:42:24 +08:00
|
|
|
|
2020-03-30 10:55:12 +08:00
|
|
|
//DWORD size = 0;
|
|
|
|
//WinHttpQueryHeaders(request, WINHTTP_QUERY_RAW_HEADERS_CRLF, WINHTTP_HEADER_NAME_BY_INDEX, NULL, &size, WINHTTP_NO_HEADER_INDEX);
|
|
|
|
//this->headers.resize(size);
|
|
|
|
//WinHttpQueryHeaders(request, WINHTTP_QUERY_RAW_HEADERS_CRLF, WINHTTP_HEADER_NAME_BY_INDEX, this->headers.data(), &size, WINHTTP_NO_HEADER_INDEX);
|
2019-06-13 16:01:29 +08:00
|
|
|
std::string data;
|
|
|
|
DWORD availableSize, downloadedSize;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
availableSize = 0;
|
|
|
|
WinHttpQueryDataAvailable(request, &availableSize);
|
|
|
|
if (!availableSize) break;
|
|
|
|
std::vector<char> buffer(availableSize);
|
|
|
|
WinHttpReadData(request, buffer.data(), availableSize, &downloadedSize);
|
|
|
|
data.append(buffer.data(), downloadedSize);
|
|
|
|
} while (availableSize > 0);
|
|
|
|
response = StringToWideString(data);
|
|
|
|
this->connection = std::move(connection);
|
|
|
|
this->request = std::move(request);
|
|
|
|
}
|
|
|
|
else errorCode = GetLastError();
|
|
|
|
else errorCode = GetLastError();
|
|
|
|
else errorCode = GetLastError();
|
|
|
|
else errorCode = GetLastError();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::wstring Escape(const std::wstring& text)
|
|
|
|
{
|
|
|
|
std::wstring escaped;
|
|
|
|
for (unsigned char ch : WideStringToString(text)) escaped += FormatString(L"%%%02X", (int)ch);
|
|
|
|
return escaped;
|
|
|
|
}
|
|
|
|
|
2020-12-14 21:26:01 +08:00
|
|
|
std::string Escape(const std::string& text)
|
2019-06-13 16:01:29 +08:00
|
|
|
{
|
2020-12-14 21:26:01 +08:00
|
|
|
std::string escaped;
|
|
|
|
for (unsigned char ch : text) escaped += FormatString("%%%02X", (int)ch);
|
|
|
|
return escaped;
|
2019-06-13 16:01:29 +08:00
|
|
|
}
|
2020-12-14 21:26:01 +08:00
|
|
|
|
2021-03-13 15:51:36 +08:00
|
|
|
TEST(assert(JSON::Parse<wchar_t>(LR"([{"string":"hello world","boolean":false,"number":1.67e+4,"null":null,"array":[]},"hello world"])")));
|