From 32a98e0f5f63359c3125c10924984bbdfa139d61 Mon Sep 17 00:00:00 2001 From: Bastian Dehn Date: Thu, 28 Mar 2024 10:51:17 +0100 Subject: [PATCH] add error message from header response --- src/kds_s2000w_client.c | 27 +++++++++++++++++++++++++++ src/kds_s2000w_client.h | 2 ++ src/kds_s2000w_handler.c | 34 +++++++++++++++++++++------------- 3 files changed, 50 insertions(+), 13 deletions(-) diff --git a/src/kds_s2000w_client.c b/src/kds_s2000w_client.c index de706bd..e63fa25 100644 --- a/src/kds_s2000w_client.c +++ b/src/kds_s2000w_client.c @@ -8,6 +8,19 @@ program_config p_config; +void _kds_s2000w_client_read_error_status(CURL* curl, response* resp) +{ + struct curl_header* header = NULL; + curl_easy_header(curl, "x-status", 0, CURLH_HEADER, -1, &header); + + if (header != NULL) { + size_t size = sizeof(char) * strlen(header->value) + 1; + resp->error_status = realloc(resp->error_status, size); + memcpy(resp->error_status, header->value, size); + resp->error_size = size; + } +} + void _kds_s2000w_client_load_config() { const char* config_file = CONFIG_FILE; @@ -45,8 +58,10 @@ response* kds_s2000w_client_response_init() resp = malloc(sizeof(response)); resp->data = malloc(sizeof(char)); + resp->error_status = malloc(sizeof(char)); resp->size = 0; resp->code = 0L; + resp->error_size = 0; return resp; } @@ -57,6 +72,8 @@ void kds_s2000w_client_response_free(response* response) if (response == NULL) return; + free(response->error_status); + response->error_status = NULL; free(response->data); response->data = NULL; free(response); @@ -93,6 +110,7 @@ int kds_s2000w_client_open_session(response* response) curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); result = curl_easy_perform(curl); curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response->code); + _kds_s2000w_client_read_error_status(curl, response); if (result != CURLE_OK) fprintf(stderr, "ERROR: curl_easy_perform: %s\n", curl_easy_strerror(result)); @@ -178,6 +196,7 @@ int kds_s2000w_client_status_session(int64_t sessionid, response* response) curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); result = curl_easy_perform(curl); curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response->code); + _kds_s2000w_client_read_error_status(curl, response); if (result != CURLE_OK) fprintf(stderr, "ERROR: curl_easy_perform: %s\n", curl_easy_strerror(result)); @@ -224,6 +243,7 @@ int kds_s2000w_client_start_scan(int64_t sessionid, response* response) curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); result = curl_easy_perform(curl); curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response->code); + _kds_s2000w_client_read_error_status(curl, response); if (result != CURLE_OK) fprintf(stderr, "ERROR: curl_easy_perform: %s\n", curl_easy_strerror(result)); @@ -269,6 +289,7 @@ int kds_s2000w_client_stop_scan(int64_t sessionid, response* response) curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); result = curl_easy_perform(curl); curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response->code); + _kds_s2000w_client_read_error_status(curl, response); if (result != CURLE_OK) fprintf(stderr, "ERROR: curl_easy_perform: %s\n", curl_easy_strerror(result)); @@ -316,6 +337,7 @@ int kds_s2000w_client_get_metadata(int64_t sessionid, int img_number, response* curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); result = curl_easy_perform(curl); curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response->code); + _kds_s2000w_client_read_error_status(curl, response); if (result != CURLE_OK) fprintf(stderr, "ERROR: curl_easy_perform: %s\n", curl_easy_strerror(result)); @@ -362,6 +384,7 @@ int kds_s2000w_client_get_image(int64_t sessionid, int img_number, response* res curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); result = curl_easy_perform(curl); curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response->code); + _kds_s2000w_client_read_error_status(curl, response); if (result != CURLE_OK) fprintf(stderr, "ERROR: curl_easy_perform: %s\n", curl_easy_strerror(result)); @@ -408,6 +431,7 @@ int kds_s2000w_client_delete_image(int64_t sessionid, int img_number, response* curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); result = curl_easy_perform(curl); curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response->code); + _kds_s2000w_client_read_error_status(curl, response); if (result != CURLE_OK) fprintf(stderr, "ERROR: curl_easy_perform: %s\n", curl_easy_strerror(result)); @@ -447,6 +471,7 @@ int kds_s2000w_client_get_capabilities(response* response) curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); result = curl_easy_perform(curl); curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response->code); + _kds_s2000w_client_read_error_status(curl, response); if (result != CURLE_OK) fprintf(stderr, "ERROR: curl_easy_perform: %s\n", curl_easy_strerror(result)); @@ -491,6 +516,7 @@ int kds_s2000w_client_get_option(int64_t sessionid, response* response) curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); result = curl_easy_perform(curl); curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response->code); + _kds_s2000w_client_read_error_status(curl, response); if (result != CURLE_OK) fprintf(stderr, "ERROR: curl_easy_perform: %s\n", curl_easy_strerror(result)); @@ -535,6 +561,7 @@ int kds_s2000w_client_set_option(int64_t sessionid, response* response) curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); result = curl_easy_perform(curl); curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response->code); + _kds_s2000w_client_read_error_status(curl, response); if (result != CURLE_OK) fprintf(stderr, "ERROR: curl_easy_perform: %s\n", curl_easy_strerror(result)); diff --git a/src/kds_s2000w_client.h b/src/kds_s2000w_client.h index d7e40c8..4b7ee72 100644 --- a/src/kds_s2000w_client.h +++ b/src/kds_s2000w_client.h @@ -4,6 +4,8 @@ typedef struct { char* data; size_t size; long code; + char* error_status; + size_t error_size; } response; response* kds_s2000w_client_response_init(); diff --git a/src/kds_s2000w_handler.c b/src/kds_s2000w_handler.c index b83bcba..a54c234 100644 --- a/src/kds_s2000w_handler.c +++ b/src/kds_s2000w_handler.c @@ -21,8 +21,10 @@ void _get_current_metadata(handler* h) resp = kds_s2000w_client_response_init(); kds_s2000w_client_get_metadata(h->sessionid, h->current_scan_status->current_image_number, resp); + if (resp->error_size > 0) + fprintf(stderr, "%s\n", resp->error_status); + if (resp->code != 200) { - debug_printf_int(ERROR, "metadata response code", resp->code); h->current_metadata->valid = 0; kds_s2000w_client_response_free(resp); resp = NULL; @@ -101,8 +103,8 @@ void _delete_current_image(handler* h) resp = kds_s2000w_client_response_init(); kds_s2000w_client_delete_image(h->sessionid, h->current_scan_status->current_image_number, resp); - if (resp->code != 200) - debug_printf_int(ERROR, "delete current image response code", resp->code); + if (resp->error_size > 0) + fprintf(stderr, "%s\n", resp->error_status); kds_s2000w_client_response_free(resp); resp = NULL; @@ -122,8 +124,8 @@ void _download_current_image(handler* h) resp = kds_s2000w_client_response_init(); kds_s2000w_client_get_image(h->sessionid, h->current_scan_status->current_image_number, resp); - if (resp->code != 200) - debug_printf_int(ERROR, "download current image response code", resp->code); + if (resp->error_size > 0) + fprintf(stderr, "%s\n", resp->error_status); h->jpg_image->size = resp->size; h->jpg_image->data = malloc(sizeof(char) * resp->size); @@ -143,8 +145,8 @@ void _get_current_scan_status(handler* h) resp = kds_s2000w_client_response_init(); kds_s2000w_client_status_session(h->sessionid, resp); - if (resp->code != 200) - debug_printf_int(ERROR, "get current scan status response code", resp->code); + if (resp->error_size > 0) + fprintf(stderr, "%s\n", resp->error_status); status_resp_obj = json_tokener_parse(resp->data); status_obj = json_object_object_get(status_resp_obj, "Status"); @@ -260,6 +262,8 @@ void kds_s2000w_handler_recreate_session(handler* h) resp = kds_s2000w_client_response_init(); kds_s2000w_client_open_session(resp); + if (resp->error_size > 0) + fprintf(stderr, "%s\n", resp->error_status); if (resp->code == 200) { sessionJson = json_tokener_parse(resp->data); @@ -281,8 +285,8 @@ void kds_s2000w_handler_recreate_session(handler* h) resp->data = realloc(resp->data, resp->size); resp->data = memcpy(resp->data, json_string, resp->size); kds_s2000w_client_set_option(h->sessionid, resp); - if (resp->code != 200) - debug_printf_int(ERROR, "set options response code", resp->code); + if (resp->error_size > 0) + fprintf(stderr, "%s\n", resp->error_status); } kds_s2000w_client_response_free(resp); @@ -308,6 +312,8 @@ void kds_s2000w_handler_open(const char* devicename, void** handle) resp = kds_s2000w_client_response_init(); int result = kds_s2000w_client_open_session(resp); + if (resp->error_size > 0) + fprintf(stderr, "%s\n", resp->error_status); if (resp->code == 408) { kds_s2000w_client_response_free(resp); @@ -315,6 +321,8 @@ void kds_s2000w_handler_open(const char* devicename, void** handle) sleep(1); resp = kds_s2000w_client_response_init(); result = kds_s2000w_client_open_session(resp); + if (resp->error_size > 0) + fprintf(stderr, "%s\n", resp->error_status); } if (result != 0 || resp->code == 404) { @@ -363,8 +371,8 @@ void kds_s2000w_handler_start_scan(handler* h) resp = kds_s2000w_client_response_init(); kds_s2000w_client_start_scan(h->sessionid, resp); - if (resp->code != 200) - debug_printf_int(ERROR, "start scan response code", resp->code); + if (resp->error_size > 0) + fprintf(stderr, "%s\n", resp->error_status); kds_s2000w_client_response_free(resp); resp = NULL; @@ -377,8 +385,8 @@ void kds_s2000w_handler_stop_scan(handler* h) resp = kds_s2000w_client_response_init(); kds_s2000w_client_stop_scan(h->sessionid, resp); - if (resp->code != 200) - debug_printf_int(ERROR, "stop scan response code", resp->code); + if (resp->error_size > 0) + fprintf(stderr, "%s\n", resp->error_status); kds_s2000w_client_response_free(resp); resp = NULL; -- 2.39.5