]> gitweb.hhaalo.de Git - sane-kds-s2000w-net.git/commitdiff
add method for session body
authorBastian Dehn <hhaalo@arcor.de>
Wed, 15 Oct 2025 06:40:09 +0000 (08:40 +0200)
committerBastian Dehn <hhaalo@arcor.de>
Wed, 15 Oct 2025 06:40:35 +0000 (08:40 +0200)
src/kds_s2000w_client.c
src/kds_s2000w_client.h

index 52cd30e0832af4a7759ba5f034ffd95dfee41ea3..b7902cd411d6bd22066e7f8a86d408ab54d4a885 100644 (file)
@@ -123,6 +123,8 @@ char* _kds_s2000w_client_errbuf_init()
  curl_param_t* _kds_s2000w_client_param_init()
  {
        curl_param_t* param = malloc(sizeof(curl_param_t));
+       if (param == NULL)
+               return NULL;
 
        param->curl = curl_easy_init();
        param->url_handler = curl_url();
@@ -150,7 +152,26 @@ char* _kds_s2000w_client_errbuf_init()
        param->errbuf = NULL;
  }
 
-void kds_s2000w_client_init(client_config_t* config)
+ char* _kds_s2000w_client_get_username_body()
+ {
+       char* body = malloc(sizeof(char) * MAX_STR_BUFFER_LENGTH);
+       if (body == NULL)
+               return NULL;
+
+       memset(body, 0, MAX_STR_BUFFER_LENGTH);
+       sprintf(body, "{\"OCPUserName\": \"%s\"}", client_config->username);
+       char* tmp_body = realloc(body, sizeof(char*) * (strlen(body) + 1));
+       if (tmp_body == NULL) {
+               free(body);
+               body = NULL;
+               return NULL;
+       }
+       
+       body = tmp_body;
+       return body;
+ }
+
+void kds_s2000w_client_init(const client_config_t* config)
 {
        kds_s2000w_debug_printf(ALL, "kds_s2000w_client_init");
 
@@ -208,6 +229,8 @@ uint8_t kds_s2000w_client_open_session(response* resp)
        kds_s2000w_debug_printf(ALL, "kds_s2000w_client_open_session");
 
        curl_param_t* param = _kds_s2000w_client_param_init();
+       if (param == NULL)
+               return 1;
 
        curl_url_set(param->url_handler, CURLUPART_URL, client_config->scanner_url, 0);
        curl_url_set(param->url_handler, CURLUPART_PATH, SESSION_PATH, 0);
@@ -218,15 +241,13 @@ uint8_t kds_s2000w_client_open_session(response* resp)
        param->headers = curl_slist_append(param->headers, ACCEPT_JSON_HEADER);
        curl_easy_setopt(param->curl, CURLOPT_HTTPHEADER, param->headers);
 
-       char* body = malloc(sizeof(char) * MAX_STR_BUFFER_LENGTH);
+       char* body = _kds_s2000w_client_get_username_body();
        if (body == NULL) {
                _kds_s2000w_client_param_free(param);
                param = NULL;
                return 1;
        }
 
-       memset(body, 0, MAX_STR_BUFFER_LENGTH);
-       sprintf(body, "{\"OCPUserName\": \"%s\"}", client_config->username);
        curl_easy_setopt(param->curl, CURLOPT_POST, 1L);
        curl_easy_setopt(param->curl, CURLOPT_POSTFIELDS, body);
        _kds_s2000w_client_set_ssl_verification_off(param->curl);
@@ -243,6 +264,8 @@ uint8_t kds_s2000w_client_open_session(response* resp)
 
        _kds_s2000w_client_param_free(param);
        param = NULL;
+       free(body);
+       body = NULL;
 
        return result;
 }
index 5972c9032d4bf309df44d82f9fb0843dd10a64dd..f3d94876ecb570ae89bc46b72d160795cca5f679 100644 (file)
@@ -17,7 +17,7 @@ typedef struct {
        uint8_t heartbeat;
 } client_config_t;
 
-void kds_s2000w_client_init(client_config_t* config);
+void kds_s2000w_client_init(const client_config_t* config);
 void kds_s2000w_client_free();
 
 response* kds_s2000w_client_response_init();