naett.c

Tiny cross-platform HTTP / HTTPS client library in C.
git clone git://git.finwo.net/lib/naett.c
Log | Files | Refs | README | LICENSE

commit 72e026e64e2f5f94c58fdfe0265cfc016a2956a3
parent e44a499155c16b91e068b005d23aa83ecb497de7
Author: Erik Agsjö <erik.agsjo@gmail.com>
Date:   Wed, 24 May 2023 23:21:09 +0200

Bugfixes from GrahamAsher

Diffstat:
Mnaett.c | 12+++++++++---
Msrc/naett_core.c | 2++
Msrc/naett_internal.h | 1+
Msrc/naett_win.c | 7+++++--
4 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/naett.c b/naett.c @@ -8,6 +8,7 @@ #ifdef _MSC_VER #define strcasecmp _stricmp #define min(a,b) (((a)<(b))?(a):(b)) + #define strdup _strdup #endif #ifdef _WIN32 @@ -479,6 +480,7 @@ void naettFree(naettReq* request) { naettPlatformFreeRequest(req); KVLink* node = req->options.headers; freeKVList(node); + free((void*)req->options.method); free((void*)req->url); free(request); } @@ -491,6 +493,7 @@ void naettClose(naettRes* response) { naettPlatformCloseResponse(res); KVLink* node = res->headers; freeKVList(node); + free(res->body.data); free(response); } // End of inlined naett_core.c // @@ -1013,7 +1016,7 @@ static LPWSTR winFromUTF8(const char* source) { } static LPWSTR wcsndup(LPCWSTR str, size_t len) { - LPWSTR result = (LPWSTR)calloc(1, sizeof(WCHAR) * (len + 1)); + LPWSTR result = calloc(1, sizeof(WCHAR) * (len + 1)); wcsncpy(result, str, len); return result; } @@ -1195,7 +1198,7 @@ int naettPlatformInitRequest(InternalRequest* req) { } req->host = wcsndup(components.lpszHostName, components.dwHostNameLength); - req->resource = wcsndup(components.lpszUrlPath, components.dwUrlPathLength); + req->resource = wcsndup(components.lpszUrlPath, components.dwUrlPathLength + components.dwExtraInfoLength); free(url); req->session = WinHttpOpen( @@ -1207,6 +1210,9 @@ int naettPlatformInitRequest(InternalRequest* req) { WinHttpSetStatusCallback(req->session, callback, WINHTTP_CALLBACK_FLAG_ALL_COMPLETIONS, 0); + // Set the connect timeout. Leave the other three timeouts at their default values. + WinHttpSetTimeouts(req->session, 0, req->options.timeoutMS, 30000, 30000); + req->connection = WinHttpConnect(req->session, req->host, components.nPort, 0); if (!req->connection) { naettPlatformFreeRequest(req); @@ -1249,7 +1255,7 @@ void naettPlatformMakeRequest(InternalResponse* res) { int contentLength = req->options.bodyReader(NULL, 0, req->options.bodyReaderData); if (contentLength > 0) { - wsprintfW(contentLengthHeader, L"Content-Length: %d", contentLength); + swprintf(contentLengthHeader, 64, L"Content-Length: %d", contentLength); extraHeaders = contentLengthHeader; } diff --git a/src/naett_core.c b/src/naett_core.c @@ -366,6 +366,7 @@ void naettFree(naettReq* request) { naettPlatformFreeRequest(req); KVLink* node = req->options.headers; freeKVList(node); + free((void*)req->options.method); free((void*)req->url); free(request); } @@ -378,5 +379,6 @@ void naettClose(naettRes* response) { naettPlatformCloseResponse(res); KVLink* node = res->headers; freeKVList(node); + free(res->body.data); free(response); } diff --git a/src/naett_internal.h b/src/naett_internal.h @@ -4,6 +4,7 @@ #ifdef _MSC_VER #define strcasecmp _stricmp #define min(a,b) (((a)<(b))?(a):(b)) + #define strdup _strdup #endif #ifdef _WIN32 diff --git a/src/naett_win.c b/src/naett_win.c @@ -223,7 +223,7 @@ int naettPlatformInitRequest(InternalRequest* req) { } req->host = wcsndup(components.lpszHostName, components.dwHostNameLength); - req->resource = wcsndup(components.lpszUrlPath, components.dwUrlPathLength); + req->resource = wcsndup(components.lpszUrlPath, components.dwUrlPathLength + components.dwExtraInfoLength); free(url); req->session = WinHttpOpen( @@ -235,6 +235,9 @@ int naettPlatformInitRequest(InternalRequest* req) { WinHttpSetStatusCallback(req->session, callback, WINHTTP_CALLBACK_FLAG_ALL_COMPLETIONS, 0); + // Set the connect timeout. Leave the other three timeouts at their default values. + WinHttpSetTimeouts(req->session, 0, req->options.timeoutMS, 30000, 30000); + req->connection = WinHttpConnect(req->session, req->host, components.nPort, 0); if (!req->connection) { naettPlatformFreeRequest(req); @@ -277,7 +280,7 @@ void naettPlatformMakeRequest(InternalResponse* res) { int contentLength = req->options.bodyReader(NULL, 0, req->options.bodyReaderData); if (contentLength > 0) { - wsprintfW(contentLengthHeader, L"Content-Length: %d", contentLength); + swprintf(contentLengthHeader, 64, L"Content-Length: %d", contentLength); extraHeaders = contentLengthHeader; }