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 478f8a4f54a17877ad192bf643f94b13191c6640
parent 1ca5bf58f82f9b8284872ef992f6327c5bdae9ef
Author: Erik Agsjö <erik.agsjo@gmail.com>
Date:   Wed,  8 Dec 2021 23:15:57 +0100

Get content-length from body reader

Diffstat:
Mnaett.c | 12++++++++++++
Mnaett.h | 1+
Msrc/naett_core.c | 5+++++
Msrc/naett_linux.c | 5+++++
4 files changed, 23 insertions(+), 0 deletions(-)

diff --git a/naett.c b/naett.c @@ -173,6 +173,11 @@ static void kvSetter(InternalParamPtr param, InternalRequest* req) { static int defaultBodyReader(void* dest, int bufferSize, void* userData) { Buffer* buffer = (Buffer*) userData; + + if (dest == NULL) { + return buffer->size; + } + int bytesToRead = buffer->size - buffer->position; if (bytesToRead > bufferSize) { bytesToRead = bufferSize; @@ -905,9 +910,16 @@ void naettPlatformMakeRequest(InternalResponse* res) { curl_easy_setopt(c, CURLOPT_FOLLOWLOCATION, 1); + int bodySize = res->request->options.bodyReader(NULL, 0, res->request->options.bodyReaderData); + curl_easy_setopt(c, CURLOPT_POSTFIELDSIZE, bodySize); + + curl_easy_setopt(c, CURLOPT_VERBOSE, 1); + setupMethod(c, req->options.method); struct curl_slist* headerList = NULL; + headerList = curl_slist_append(headerList, "User-Agent: Naett/1.0"); + KVLink* header = req->options.headers; size_t bufferSize = 0; char* buffer = NULL; diff --git a/naett.h b/naett.h @@ -22,6 +22,7 @@ void naettInit(naettInitData initThing); typedef struct naettReq naettReq; typedef struct naettRes naettRes; +// If naettReadFunc is called with NULL dest, it must respond with the body size typedef int (*naettReadFunc)(void* dest, int bufferSize, void* userData); typedef int (*naettWriteFunc)(const void* source, int bytes, void* userData); typedef int (*naettHeaderLister)(const char* name, const char* value, void* userData); diff --git a/src/naett_core.c b/src/naett_core.c @@ -65,6 +65,11 @@ static void kvSetter(InternalParamPtr param, InternalRequest* req) { static int defaultBodyReader(void* dest, int bufferSize, void* userData) { Buffer* buffer = (Buffer*) userData; + + if (dest == NULL) { + return buffer->size; + } + int bytesToRead = buffer->size - buffer->position; if (bytesToRead > bufferSize) { bytesToRead = bufferSize; diff --git a/src/naett_linux.c b/src/naett_linux.c @@ -187,9 +187,14 @@ void naettPlatformMakeRequest(InternalResponse* res) { curl_easy_setopt(c, CURLOPT_FOLLOWLOCATION, 1); + int bodySize = res->request->options.bodyReader(NULL, 0, res->request->options.bodyReaderData); + curl_easy_setopt(c, CURLOPT_POSTFIELDSIZE, bodySize); + setupMethod(c, req->options.method); struct curl_slist* headerList = NULL; + headerList = curl_slist_append(headerList, "User-Agent: Naett/1.0"); + KVLink* header = req->options.headers; size_t bufferSize = 0; char* buffer = NULL;