]> gitweb.hhaalo.de Git - sane-kds-s2000w-net.git/commitdiff
change open session with memory stream
authorBastian Dehn <hhaalo@arcor.de>
Tue, 21 May 2024 18:01:07 +0000 (20:01 +0200)
committerBastian Dehn <hhaalo@arcor.de>
Tue, 21 May 2024 18:08:26 +0000 (20:08 +0200)
src/kds_s2000w_client.c

index 4f4a4692d4aa8c92edebfcddf004e01ee29ab610..661be82be7b728887a3f7bb2a97179862350d45a 100644 (file)
@@ -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;
 }