From bcedb87dedd08b48129b6dd87d36b572d6e39834 Mon Sep 17 00:00:00 2001 From: luboslenco Date: Fri, 12 Sep 2025 16:18:18 +0200 Subject: [PATCH] Net fixes --- base/sources/backends/android_net.c | 2 +- base/sources/backends/apple_net.m | 2 +- base/sources/backends/posix_net.c | 1 + base/sources/backends/windows_net.c | 17 +++++------------ base/sources/iron.h | 3 ++- 5 files changed, 10 insertions(+), 15 deletions(-) diff --git a/base/sources/backends/android_net.c b/base/sources/backends/android_net.c index b32e8163..7d93230d 100644 --- a/base/sources/backends/android_net.c +++ b/base/sources/backends/android_net.c @@ -30,7 +30,7 @@ void iron_https_request(const char *url_base, const char *url_path, const char * jbyte *elements = (*env)->GetByteArrayElements(env, bytes_array, NULL); if (elements != NULL) { callback((char *)elements, callbackdata); - // (*env)->ReleaseByteArrayElements(env, bytes_array, elements, JNI_ABORT); + (*env)->ReleaseByteArrayElements(env, bytes_array, elements, JNI_ABORT); } (*vm)->DetachCurrentThread(vm); diff --git a/base/sources/backends/apple_net.m b/base/sources/backends/apple_net.m index 6f3c2147..b6dd6260 100644 --- a/base/sources/backends/apple_net.m +++ b/base/sources/backends/apple_net.m @@ -19,11 +19,11 @@ void iron_https_request(const char *url_base, const char *url_path, const char * urlstring = [urlstring stringByAppendingString:[NSString stringWithUTF8String:url_path]]; NSURL *aUrl = [NSURL URLWithString:urlstring]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:aUrl]; + request.HTTPMethod = method == IRON_HTTP_GET ? @"GET" : @"POST"; if (data != 0) { NSString *datastring = [NSString stringWithUTF8String:data]; request.HTTPBody = [datastring dataUsingEncoding:NSUTF8StringEncoding]; } - request.HTTPMethod = method == IRON_HTTP_GET ? @"GET" : @"POST"; NSURLSessionDataTask *dataTask = [ session dataTaskWithRequest:request diff --git a/base/sources/backends/posix_net.c b/base/sources/backends/posix_net.c index abdadb84..2c2220a7 100644 --- a/base/sources/backends/posix_net.c +++ b/base/sources/backends/posix_net.c @@ -59,6 +59,7 @@ void iron_https_request(const char *url_base, const char *url_path, const char * buf = realloc(buf, buf_len); } } + buf[pos] = '\0'; // Parse const char *body_ptr = ""; diff --git a/base/sources/backends/windows_net.c b/base/sources/backends/windows_net.c index 1afe6c8d..9ca551ec 100644 --- a/base/sources/backends/windows_net.c +++ b/base/sources/backends/windows_net.c @@ -25,20 +25,19 @@ void iron_https_request(const char *url_base, const char *url_path, const char * if (hConnect) { wchar_t wurl_path[4096]; MultiByteToWideChar(CP_UTF8, 0, url_path, -1, wurl_path, 4096); - hRequest = - WinHttpOpenRequest(hConnect, method == IRON_HTTP_GET ? L"GET" : L"POST", wurl_path, NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, WINHTTP_FLAG_SECURE); + hRequest = WinHttpOpenRequest(hConnect, method == IRON_HTTP_GET ? L"GET" : L"POST", wurl_path, NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, WINHTTP_FLAG_SECURE); } BOOL bResults = FALSE; - if (hRequest) { DWORD optionalLength = (data != 0 && strlen(data) > 0) ? (DWORD)strlen(data) : 0; bResults = WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, data == 0 ? WINHTTP_NO_REQUEST_DATA : (LPVOID)data, optionalLength, optionalLength, 0); } - if (bResults) + if (bResults) { bResults = WinHttpReceiveResponse(hRequest, NULL); + } int returnDataIndex = 0; if (bResults) { @@ -50,14 +49,8 @@ void iron_https_request(const char *url_base, const char *url_path, const char * } if ((int)dwSize + 1 > returnDataSize - returnDataIndex) { - int newReturnDataSize = (returnDataIndex + dwSize + 1) * 2; - char *newReturnData = (char *)malloc(newReturnDataSize); - if (newReturnData == 0) { - iron_error("Out of memory\n"); - } - memcpy(newReturnData, returnData, returnDataSize); - returnDataSize = newReturnDataSize; - returnData = newReturnData; + returnDataSize = (returnDataIndex + dwSize + 1) * 2; + returnData = realloc(returnData, returnDataSize); } DWORD dwDownloaded = 0; diff --git a/base/sources/iron.h b/base/sources/iron.h index 73d96780..a71e4469 100644 --- a/base/sources/iron.h +++ b/base/sources/iron.h @@ -1378,7 +1378,8 @@ void _http_callback(const char *body, void *callback_data) { if (body != NULL) { buffer = malloc(sizeof(buffer_t)); buffer->length = cbd->size > 0 ? cbd->size : strlen(body); - buffer->buffer = body; + buffer->buffer = malloc(buffer->length); + memcpy(buffer->buffer, body, buffer->length); } cbd->func(cbd->url, buffer); free(cbd);