LunaHook-mirror/LunaHost/GUI/http.hpp

78 lines
2.6 KiB
C++
Raw Normal View History

2024-04-10 03:17:53 +08:00
#include <winhttp.h>
using InternetHandle = AutoHandle<Functor<WinHttpCloseHandle>>;
struct HttpRequest
{
HttpRequest(
2024-07-21 19:28:47 +08:00
const wchar_t *agentName,
const wchar_t *serverName,
const wchar_t *action,
const wchar_t *objectName,
2024-04-10 03:17:53 +08:00
std::string body = "",
2024-07-21 19:28:47 +08:00
const wchar_t *headers = NULL,
2024-04-10 03:17:53 +08:00
DWORD port = INTERNET_DEFAULT_PORT,
2024-07-21 19:28:47 +08:00
const wchar_t *referrer = NULL,
2024-04-10 03:17:53 +08:00
DWORD requestFlags = WINHTTP_FLAG_SECURE | WINHTTP_FLAG_ESCAPE_DISABLE,
2024-07-21 19:28:47 +08:00
const wchar_t *httpVersion = NULL,
const wchar_t **acceptTypes = NULL);
2024-04-10 03:17:53 +08:00
operator bool() { return errorCode == ERROR_SUCCESS; }
std::wstring response;
std::wstring headers;
InternetHandle connection = NULL;
InternetHandle request = NULL;
DWORD errorCode = ERROR_SUCCESS;
};
HttpRequest::HttpRequest(
2024-07-21 19:28:47 +08:00
const wchar_t *agentName,
const wchar_t *serverName,
const wchar_t *action,
const wchar_t *objectName,
2024-04-10 03:17:53 +08:00
std::string body,
2024-07-21 19:28:47 +08:00
const wchar_t *headers,
2024-04-10 03:17:53 +08:00
DWORD port,
2024-07-21 19:28:47 +08:00
const wchar_t *referrer,
2024-04-10 03:17:53 +08:00
DWORD requestFlags,
2024-07-21 19:28:47 +08:00
const wchar_t *httpVersion,
const wchar_t **acceptTypes)
2024-04-10 03:17:53 +08:00
{
static std::atomic<HINTERNET> internet = NULL;
2024-07-21 19:28:47 +08:00
if (!internet)
internet = WinHttpOpen(agentName, WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, NULL, NULL, 0);
2024-04-10 03:17:53 +08:00
if (internet)
if (InternetHandle connection = WinHttpConnect(internet, serverName, port, 0))
if (InternetHandle request = WinHttpOpenRequest(connection, action, objectName, httpVersion, referrer, acceptTypes, requestFlags))
if (WinHttpSendRequest(request, headers, -1UL, body.empty() ? NULL : body.data(), body.size(), body.size(), NULL))
{
WinHttpReceiveResponse(request, NULL);
2024-07-21 19:28:47 +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);
2024-04-10 03:17:53 +08:00
std::string data;
DWORD availableSize, downloadedSize;
do
{
availableSize = 0;
WinHttpQueryDataAvailable(request, &availableSize);
2024-07-21 19:28:47 +08:00
if (!availableSize)
break;
2024-04-10 03:17:53 +08:00
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);
}
2024-07-21 19:28:47 +08:00
else
errorCode = GetLastError();
else
errorCode = GetLastError();
else
errorCode = GetLastError();
else
errorCode = GetLastError();
2024-04-10 03:17:53 +08:00
}