From cd28674bb41a1f982e6c9973e641a59187b521fa Mon Sep 17 00:00:00 2001 From: Bastian Dehn Date: Tue, 21 May 2024 20:01:07 +0200 Subject: [PATCH] change open session with memory stream --- src/kds_s2000w_client.c | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/kds_s2000w_client.c b/src/kds_s2000w_client.c index 4f4a469..661be82 100644 --- a/src/kds_s2000w_client.c +++ b/src/kds_s2000w_client.c @@ -51,6 +51,17 @@ size_t _kds_s2000w_client_callback(char *ptr, size_t size, size_t nmemb, void *u return fullsize; } +void _kds_s2000w_client_stream_to_response(FILE* stream, response* resp, size_t* size) +{ + debug_printf(ALL, "kds_s2000w_client_stream_to_response"); + + resp->data = realloc(resp->data, sizeof(char) * *size + 1); + fseek(stream, 0, SEEK_SET); + fread(resp->data, sizeof(char), *size, stream); + resp->data[*size] = '\0'; + resp->size = *size; +} + response* kds_s2000w_client_response_init() { debug_printf(ALL, "kds_s2000w_client_response_init"); @@ -91,6 +102,9 @@ int kds_s2000w_client_open_session(response* response) char* url = NULL; CURLcode result = 0; char body[50] = {0}; + FILE* stream = NULL; + char* buffer = NULL; + size_t* sizeloc = NULL; curl = curl_easy_init(); if(!curl) @@ -105,8 +119,10 @@ int kds_s2000w_client_open_session(response* response) curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_POST, 1L); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, body); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, _kds_s2000w_client_callback); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*) response); + buffer = malloc(sizeof(char)); + sizeloc = malloc(sizeof(size_t)); + stream = open_memstream(&buffer, sizeloc); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, stream); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); result = curl_easy_perform(curl); @@ -116,12 +132,20 @@ int kds_s2000w_client_open_session(response* response) if (result != CURLE_OK) fprintf(stderr, "ERROR: curl_easy_perform: %s\n", curl_easy_strerror(result)); + fflush(stream); + _kds_s2000w_client_stream_to_response(stream, response, sizeloc); + fclose(stream); + curl_url_cleanup(url_handler); url_handler = NULL; curl_free(url); url = NULL; curl_easy_cleanup(curl); curl = NULL; + free(buffer); + buffer = NULL; + free(sizeloc); + sizeloc = NULL; return result; } -- 2.39.5