Files
armorpaint/base/sources/backends/windows_net.c
T

86 lines
2.5 KiB
C
Raw Normal View History

2025-02-26 13:24:16 +01:00
#include <iron_system.h>
2025-02-26 08:47:09 +01:00
#include <iron_net.h>
2024-11-10 22:11:29 +01:00
#include <stdio.h>
#include <stdlib.h>
2025-03-07 11:30:51 +01:00
#include <Windows.h>
2024-11-10 22:11:29 +01:00
#include <winhttp.h>
static char *returnData = NULL;
static int returnDataSize = 0;
2025-09-12 15:29:02 +02:00
void iron_https_request(const char *url_base, const char *url_path, const char *data, int port, int method,
iron_http_callback_t callback, void *callbackdata) {
2024-11-10 22:11:29 +01:00
// based on https://docs.microsoft.com/en-us/windows/desktop/winhttp/winhttp-sessions-overview
2025-03-09 17:02:12 +01:00
HINTERNET hSession = WinHttpOpen(L"WinHTTP via Iron/1.0", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
2024-11-10 22:11:29 +01:00
HINTERNET hConnect = NULL;
if (hSession) {
wchar_t wurl[4096];
2025-09-12 15:29:02 +02:00
MultiByteToWideChar(CP_UTF8, 0, url_base, -1, wurl, 4096);
2024-11-10 22:11:29 +01:00
hConnect = WinHttpConnect(hSession, wurl, port, 0);
}
HINTERNET hRequest = NULL;
if (hConnect) {
2025-09-12 15:29:02 +02:00
wchar_t wurl_path[4096];
MultiByteToWideChar(CP_UTF8, 0, url_path, -1, wurl_path, 4096);
2025-09-12 16:18:18 +02:00
hRequest = WinHttpOpenRequest(hConnect, method == IRON_HTTP_GET ? L"GET" : L"POST", wurl_path, NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, WINHTTP_FLAG_SECURE);
2024-11-10 22:11:29 +01:00
}
BOOL bResults = FALSE;
if (hRequest) {
DWORD optionalLength = (data != 0 && strlen(data) > 0) ? (DWORD)strlen(data) : 0;
2025-09-12 15:29:02 +02:00
bResults = WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0,
2024-11-10 22:11:29 +01:00
data == 0 ? WINHTTP_NO_REQUEST_DATA : (LPVOID)data, optionalLength, optionalLength, 0);
}
2025-09-12 16:18:18 +02:00
if (bResults) {
2024-11-10 22:11:29 +01:00
bResults = WinHttpReceiveResponse(hRequest, NULL);
2025-09-12 16:18:18 +02:00
}
2024-11-10 22:11:29 +01:00
int returnDataIndex = 0;
if (bResults) {
DWORD dwSize;
do {
dwSize = 0;
if (!WinHttpQueryDataAvailable(hRequest, &dwSize)) {
2025-03-09 17:02:12 +01:00
iron_error("Error %d in WinHttpQueryDataAvailable.\n", GetLastError());
2024-11-10 22:11:29 +01:00
}
if ((int)dwSize + 1 > returnDataSize - returnDataIndex) {
2025-09-12 16:18:18 +02:00
returnDataSize = (returnDataIndex + dwSize + 1) * 2;
returnData = realloc(returnData, returnDataSize);
2024-11-10 22:11:29 +01:00
}
DWORD dwDownloaded = 0;
if (!WinHttpReadData(hRequest, (LPVOID)(&returnData[returnDataIndex]), dwSize, &dwDownloaded)) {
2025-03-09 17:02:12 +01:00
iron_error("Error %d in WinHttpReadData.\n", GetLastError());
2024-11-10 22:11:29 +01:00
}
returnDataIndex += dwSize;
} while (dwSize > 0);
}
else {
2025-09-12 15:29:02 +02:00
callback(NULL, callbackdata);
2024-11-10 22:11:29 +01:00
return;
}
returnData[returnDataIndex] = 0;
if (!bResults) {
2025-03-09 17:02:12 +01:00
iron_error("Error %d has occurred.\n", GetLastError());
2024-11-10 22:11:29 +01:00
}
if (hRequest) {
WinHttpCloseHandle(hRequest);
}
if (hConnect) {
WinHttpCloseHandle(hConnect);
}
if (hSession) {
WinHttpCloseHandle(hSession);
}
2025-09-12 15:29:02 +02:00
callback(returnData, callbackdata);
2024-11-10 22:11:29 +01:00
}