From a3ccc26bb72280276448f2464057c0c2fb32e356 Mon Sep 17 00:00:00 2001
From: Olzhas <oljas.jarasbaev@demlabs.net>
Date: Tue, 22 Oct 2024 16:40:09 +0700
Subject: [PATCH] [*] refactor

---
 .../rpc_core/include/dap_json_rpc_request.h   |  4 +-
 .../rpc_core/src/dap_json_rpc_request.c       | 73 +++++++++----------
 .../src/dap_json_rpc_request_handler.c        |  9 +--
 net/server/json_rpc/src/dap_json_rpc.c        | 54 +-------------
 4 files changed, 40 insertions(+), 100 deletions(-)

diff --git a/net/server/json_rpc/rpc_core/include/dap_json_rpc_request.h b/net/server/json_rpc/rpc_core/include/dap_json_rpc_request.h
index 539a2efc7..b943fc9f2 100644
--- a/net/server/json_rpc/rpc_core/include/dap_json_rpc_request.h
+++ b/net/server/json_rpc/rpc_core/include/dap_json_rpc_request.h
@@ -86,9 +86,9 @@ void dap_json_rpc_http_request_free(dap_json_rpc_http_request_t *a_http_request)
 char* dap_json_rpc_request_to_http_str(dap_json_rpc_request_t *a_request, size_t*output_data_size);
 
 char * dap_json_rpc_enc_request(dap_client_pvt_t* a_client_internal, char * a_request_data_str, size_t a_request_data_size,
-                                char ** a_path, size_t * a_enc_request_size, char * a_custom_header);
+                                char ** a_path, size_t * a_enc_request_size, char ** a_custom_header);
 
-int dap_json_rpc_request_send(dap_client_pvt_t*  a_client_internal, dap_json_rpc_request_t *a_request, char* a_response);
+int dap_json_rpc_request_send(dap_client_pvt_t*  a_client_internal, dap_json_rpc_request_t *a_request, char** a_response);
 
 #ifdef __cplusplus
 }
diff --git a/net/server/json_rpc/rpc_core/src/dap_json_rpc_request.c b/net/server/json_rpc/rpc_core/src/dap_json_rpc_request.c
index 6051ca6b1..e0385eea3 100644
--- a/net/server/json_rpc/rpc_core/src/dap_json_rpc_request.c
+++ b/net/server/json_rpc/rpc_core/src/dap_json_rpc_request.c
@@ -113,18 +113,18 @@ static void s_exec_cmd_error_handler(int a_error_code, void *a_arg){
 #endif
 }
 
-static int s_exec_cmd_request_get_response(struct exec_cmd_request *a_exec_cmd_request, char **a_response, size_t *a_response_size)
+static int s_exec_cmd_request_get_response(struct exec_cmd_request *a_exec_cmd_request, char **a_response_out, size_t *a_response_out_size)
 {
     int ret = 0;
-    char *l_enc_response = NULL;
-    size_t l_enc_response_size = 0;
+    char *l_response = NULL;
+    size_t l_response_size = 0;
 
 #ifdef DAP_OS_WINDOWS
     EnterCriticalSection(&a_exec_cmd_request->wait_crit_sec);
     if (a_exec_cmd_request->error_code != 0) {
         ret = a_exec_cmd_request->error_code;
     } else {
-        l_enc_response = dap_strdup(a_exec_cmd_request->response);
+        l_enc_response = a_exec_cmd_request->response;
         l_enc_response_size = a_exec_cmd_request->response_size;
         ret = 0;
     }
@@ -134,32 +134,31 @@ static int s_exec_cmd_request_get_response(struct exec_cmd_request *a_exec_cmd_r
     if (a_exec_cmd_request->error_code != 0) {
         ret = a_exec_cmd_request->error_code;
     } else {
-        l_enc_response = dap_strdup(a_exec_cmd_request->response);
-        l_enc_response_size = a_exec_cmd_request->response_size;
+        l_response = a_exec_cmd_request->response;
+        l_response_size = a_exec_cmd_request->response_size;
         ret = 0; 
     }
     pthread_mutex_unlock(&a_exec_cmd_request->wait_mutex);
 #endif
     if (a_exec_cmd_request->error_code) {
         log_it(L_ERROR, "Response error code: %d", ret);
-        ret - 1;
-    } else if (l_enc_response) {
-        dap_client_pvt_t * l_client_pvt = a_exec_cmd_request->client_pvt;
-        l_client_pvt->http_client = NULL;
-        size_t l_response_dec_size_max = l_enc_response_size ? l_enc_response_size * 2 + 16 : 0;
-        *a_response = l_enc_response_size ? DAP_NEW_Z_SIZE(char, l_response_dec_size_max) : NULL;
-        *a_response_size = 0;
-        if(l_enc_response_size)
-            *a_response_size = dap_enc_decode(l_client_pvt->session_key,
-                                    l_enc_response, l_enc_response_size,
-                                    *a_response, l_response_dec_size_max,
-                                    DAP_ENC_DATA_TYPE_RAW);
-        if (l_enc_response) {
-            DAP_DEL_Z(l_enc_response);
-        }
+        ret = - 1;
+    } else if (l_response) {
+            dap_client_pvt_t * l_client_pvt = a_exec_cmd_request->client_pvt;
+            l_client_pvt->http_client = NULL;
+            size_t l_response_dec_size_max = l_response_size ? l_response_size * 2 + 16 : 0;
+            char * l_response_dec = l_response_size ? DAP_NEW_Z_SIZE(char, l_response_dec_size_max) : NULL;
+            size_t l_response_dec_size = 0;
+            if(l_response_size)
+                l_response_dec_size = dap_enc_decode(l_client_pvt->session_key,
+                        l_response, l_response_size,
+                        l_response_dec, l_response_dec_size_max,
+                        DAP_ENC_DATA_TYPE_RAW);
+            *a_response_out = l_response_dec;
+            *a_response_out_size = l_response_dec_size;
     } else {
         log_it(L_ERROR, "Empty response in json-rpc");
-        ret -2;
+        ret = -2;
     }
 
     return ret;
@@ -178,10 +177,8 @@ static int dap_chain_exec_cmd_list_wait(struct exec_cmd_request *a_exec_cmd_requ
     return LeaveCriticalSection(&a_exec_cmd_request->wait_crit_sec), a_exec_cmd_request->response;     
 #else
     pthread_mutex_lock(&a_exec_cmd_request->wait_mutex);
-    if(a_exec_cmd_request->response) {
-        pthread_mutex_unlock(&a_exec_cmd_request->wait_mutex);
-        return EXEC_CMD_OK;
-    }
+    if(a_exec_cmd_request->response)
+        return pthread_mutex_unlock(&a_exec_cmd_request->wait_mutex), EXEC_CMD_OK;
     struct timespec l_cond_timeout;
 #ifdef DAP_OS_DARWIN
     l_cond_timeout = (struct timespec){ .tv_sec = a_timeout_ms / 1000 };
@@ -199,17 +196,17 @@ static int dap_chain_exec_cmd_list_wait(struct exec_cmd_request *a_exec_cmd_requ
         ) {
         case ETIMEDOUT:
             a_exec_cmd_request->response = "ERR_WAIT_TIMEOUT";
+            return EXEC_CMD_ERR_WAIT_TIMEOUT;
         default:
             break;
         }
     }
-    pthread_mutex_unlock(&a_exec_cmd_request->wait_mutex);
-    return  EXEC_CMD_ERR_WAIT_TIMEOUT;
+    return pthread_mutex_unlock(&a_exec_cmd_request->wait_mutex), EXEC_CMD_OK;
 #endif
 }
 
 char * dap_json_rpc_enc_request(dap_client_pvt_t* a_client_internal, char * a_request_data_str, size_t a_request_data_size,
-                                char ** a_path, size_t * a_enc_request_size, char * a_custom_header) {
+                                char ** a_path, size_t * a_enc_request_size, char ** a_custom_header) {
 
     const char * l_sub_url = dap_strdup_printf("channels=%s,enc_type=%d,enc_key_size=%zu,enc_headers=%d",
                                                     a_client_internal->client->active_channels,
@@ -276,11 +273,11 @@ char * dap_json_rpc_enc_request(dap_client_pvt_t* a_client_internal, char * a_re
     }
 
     size_t l_size_required = a_client_internal->session_key_id ? strlen(a_client_internal->session_key_id) + 40 : 40;
-    a_custom_header = DAP_NEW_Z_SIZE(char, l_size_required);
-    size_t l_off = snprintf(a_custom_header, l_size_required, "KeyID: %s\r\n", 
+    *a_custom_header = DAP_NEW_Z_SIZE(char, l_size_required);
+    size_t l_off = snprintf(*a_custom_header, l_size_required, "KeyID: %s\r\n", 
                             a_client_internal->session_key_id ? a_client_internal->session_key_id : "NULL");
     if (a_client_internal->is_close_session)
-        snprintf(a_custom_header + l_off, l_size_required - l_off, "%s\r\n", "SessionCloseAfterRequest: true");
+        snprintf(*a_custom_header + l_off, l_size_required - l_off, "%s\r\n", "SessionCloseAfterRequest: true");
     
     DAP_DEL_Z(l_sub_url_enc);
     DAP_DEL_Z(l_query_enc);
@@ -516,7 +513,7 @@ char* dap_json_rpc_request_to_http_str(dap_json_rpc_request_t *a_request, size_t
     return l_http_str;
 }
 
-int dap_json_rpc_request_send(dap_client_pvt_t*  a_client_internal, dap_json_rpc_request_t *a_request, char* a_response) {
+int dap_json_rpc_request_send(dap_client_pvt_t*  a_client_internal, dap_json_rpc_request_t *a_request, char** a_response) {
     size_t l_request_data_size = 0, l_enc_request_size, l_response_size;
     char* l_custom_header = NULL, *l_path = NULL;
 
@@ -524,7 +521,7 @@ int dap_json_rpc_request_send(dap_client_pvt_t*  a_client_internal, dap_json_rpc
     if (!l_request_data_str) {
         return -1;
     }
-    char * l_enc_request = dap_json_rpc_enc_request(a_client_internal, l_request_data_str, l_request_data_size, &l_path, &l_enc_request_size, l_custom_header);
+    char * l_enc_request = dap_json_rpc_enc_request(a_client_internal, l_request_data_str, l_request_data_size, &l_path, &l_enc_request_size, &l_custom_header);
     DAP_DEL_Z(l_request_data_str);
     if (!l_enc_request || !l_path) {
         DAP_DEL_Z(l_request_data_str);
@@ -554,23 +551,21 @@ int dap_json_rpc_request_send(dap_client_pvt_t*  a_client_internal, dap_json_rpc
     int l_ret = dap_chain_exec_cmd_list_wait(l_exec_cmd_request, 10000);
     switch (l_ret) {
         case EXEC_CMD_OK :{
-            s_exec_cmd_request_get_response(l_exec_cmd_request, &a_response, &l_response_size);
+            s_exec_cmd_request_get_response(l_exec_cmd_request, a_response, &l_response_size);
             log_it(L_DEBUG, "Get response from %s:%d, response size = %lu",
                             a_client_internal->client->link_info.uplink_addr, a_client_internal->client->link_info.uplink_port, l_response_size);
             break;
         }
         case EXEC_CMD_ERR_WAIT_TIMEOUT: {
-            a_response = dap_strdup("Response time run out ");
+            *a_response = dap_strdup("Response time run out ");
             log_it(L_ERROR, "Response time from %s:%d  run out",
                             a_client_internal->client->link_info.uplink_addr, a_client_internal->client->link_info.uplink_port);
-            l_response_size = strlen(a_response);
             break;
         }
         case EXEC_CMD_ERR_UNKNOWN : {
-            a_response = dap_strdup("Unknown error in json-rpc");
+            *a_response = dap_strdup("Unknown error in json-rpc");
             log_it(L_ERROR, "Response from %s:%d has unknown error",
                             a_client_internal->client->link_info.uplink_addr, a_client_internal->client->link_info.uplink_port);
-            l_response_size = strlen(a_response);
             break;
         }
     }
diff --git a/net/server/json_rpc/rpc_core/src/dap_json_rpc_request_handler.c b/net/server/json_rpc/rpc_core/src/dap_json_rpc_request_handler.c
index 9d610e269..65d66f174 100644
--- a/net/server/json_rpc/rpc_core/src/dap_json_rpc_request_handler.c
+++ b/net/server/json_rpc/rpc_core/src/dap_json_rpc_request_handler.c
@@ -65,14 +65,9 @@ char * dap_json_rpc_request_handler(const char * a_request,  size_t a_request_si
         dap_json_rpc_response_t* l_no_rights_res = dap_json_rpc_response_create("You have no rights", TYPE_RESPONSE_STRING, l_http_request->request->id);
         char * l_no_rights_res_str = dap_json_rpc_response_to_string(l_no_rights_res);
         dap_json_rpc_http_request_free(l_http_request);
-        return NULL;
+        return l_no_rights_res_str;
     }
-    const char* l_response = dap_cli_cmd_exec(l_data_str);
-    // size_t res = dap_http_simple_reply(a_http_simple, (void*)l_response, strlen(l_response));
-    // if (!res)
-    //     log_it(L_ERROR, "Error in json-rpc reply");
-    // log_it(L_INFO, "reply for exec_cmd");
+    char* l_response = dap_cli_cmd_exec(l_data_str);
     dap_json_rpc_http_request_free(l_http_request);
-    // DAP_DEL_Z(l_response);
     return l_response;
 }
diff --git a/net/server/json_rpc/src/dap_json_rpc.c b/net/server/json_rpc/src/dap_json_rpc.c
index 64b948a18..da305b201 100644
--- a/net/server/json_rpc/src/dap_json_rpc.c
+++ b/net/server/json_rpc/src/dap_json_rpc.c
@@ -92,10 +92,7 @@ void dap_json_rpc_http_proc(dap_http_simple_t *a_http_simple, void *a_arg)
 {
     log_it(L_DEBUG,"Proc enc http exec_cmd request");
     http_status_code_t *return_code = (http_status_code_t *)a_arg;
-
-   // unsigned int proto_version;
     dap_stream_session_t *l_stream_session = NULL;
-   // unsigned int action_cmd=0;
     bool l_new_session = false;
 
     enc_http_delegate_t *l_dg = enc_http_request_decode(a_http_simple);
@@ -114,24 +111,18 @@ void dap_json_rpc_http_proc(dap_http_simple_t *a_http_simple, void *a_arg)
             char *l_subtok_value = strchr(l_tok, '=');
             if (l_subtok_value && l_subtok_value != l_subtok_name) {
                 *l_subtok_value++ = '\0';
-                //log_it(L_DEBUG, "tok = %s value =%s",l_subtok_name,l_subtok_value);
                 if (strcmp(l_subtok_name,"channels")==0 ){
                     strncpy(l_channels_str,l_subtok_value,sizeof (l_channels_str)-1);
-                    //log_it(L_DEBUG,"Param: channels=%s",l_channels_str);
                 }else if(strcmp(l_subtok_name,"enc_type")==0){
                     l_enc_type = atoi(l_subtok_value);
-                    //log_it(L_DEBUG,"Param: enc_type=%s",dap_enc_get_type_name(l_enc_type));
                     l_is_legacy = false;
                 }else if(strcmp(l_subtok_name,"enc_key_size")==0){
-                    // TODO impliment enc_key_size influence
                     l_enc_key_size = (size_t) atoi(l_subtok_value);
                     if (l_enc_key_size > l_dg->request_size )
                         l_enc_key_size = 32;
-                    //log_it(L_DEBUG,"Param: enc_key_size=%d", l_enc_key_size);
                     l_is_legacy = false;
                 }else if(strcmp(l_subtok_name,"enc_headers")==0){
                     l_enc_headers = atoi(l_subtok_value);
-                    //log_it(L_DEBUG,"Param: enc_headers=%d",l_enc_headers);
                 }
             }
             l_tok = strtok_r(NULL, ",", &l_tok_tmp);
@@ -152,54 +143,13 @@ void dap_json_rpc_http_proc(dap_http_simple_t *a_http_simple, void *a_arg)
         if (l_res_str) {
             enc_http_reply(l_dg, l_res_str, strlen(l_res_str));
         } else {
-            enc_http_reply(l_dg, "Empty reply", strlen("Empty reply"));
+            enc_http_reply(l_dg, "Wrong request", strlen("Wrong request"));
         }
-        // if(l_new_session){
-        //     l_stream_session = dap_stream_session_pure_new();
-        //     strncpy(l_stream_session->active_channels, l_channels_str, l_channels_str_size);
-        //     char *l_key_str = DAP_NEW_Z_SIZE(char, KEX_KEY_STR_SIZE + 1);
-        //     dap_random_string_fill(l_key_str, KEX_KEY_STR_SIZE);
-        //     l_stream_session->key = dap_enc_key_new_generate( l_enc_type, l_key_str, KEX_KEY_STR_SIZE,
-        //                                        NULL, 0, 32);
-        //     dap_http_header_t *l_hdr_key_id = dap_http_header_find(a_http_simple->http_client->in_headers, "KeyID");
-        //     if (l_hdr_key_id) {
-        //         dap_enc_ks_key_t *l_ks_key = dap_enc_ks_find(l_hdr_key_id->value);
-        //         if (!l_ks_key) {
-        //             log_it(L_WARNING, "Key with ID %s not found", l_hdr_key_id->value);
-        //             *return_code = Http_Status_BadRequest;
-        //             return;
-        //         }
-        //         l_stream_session->acl = l_ks_key->acl_list;
-        //         l_stream_session->node = l_ks_key->node_addr;
-        //     }
-            // if (l_is_legacy)
-            //     enc_http_reply_f(l_dg, "%u %s", l_stream_session->id, l_key_str);
-            // else
-            //     enc_http_reply_f(l_dg, "%u %s %u %d %d", l_stream_session->id, l_key_str,
-            //                            DAP_PROTOCOL_VERSION, l_enc_type, l_enc_headers);
-            *return_code = Http_Status_OK;
-
-            // DAP_DELETE(l_key_str);
+        *return_code = Http_Status_OK;
         enc_http_reply_encode(a_http_simple,l_dg);
-        // dap_enc_ks_delete(l_hdr_key_id->value);
         enc_http_delegate_delete(l_dg);
     }else{
         log_it(L_ERROR,"Wrong request");
         *return_code = Http_Status_BadRequest;
     }
-
-    // log_it(L_INFO, "Proc exec_cmd request");
-    // http_status_code_t *l_return_code = (http_status_code_t*)a_arg;
-    // *l_return_code = Http_Status_OK;
-    // strcpy(a_http_simple->reply_mime, "application/json");
-
-    // *l_return_code = Http_Status_OK;
-    // if (!a_http_simple->request){
-    //     *l_return_code = Http_Status_NoContent;
-    //     dap_http_simple_reply_f(a_http_simple, "JSON-RPC request was not formed. "
-    //                                       "JSON-RPC must represent a method containing objects from an object, "
-    //                                       "this is a string the name of the method, id is a number and params "
-    //                                       "is an array that can contain strings, numbers and boolean values.");
-    // }
-    
 }
-- 
GitLab