From 701f5ee21229a66630ab07969beae0119654c77e Mon Sep 17 00:00:00 2001
From: "alexey.stratulat" <alexey.stratulat@demlabs.net>
Date: Tue, 18 Jun 2024 08:28:42 +0000
Subject: [PATCH] Bugfix 11833 2

---
 .../include => 3rdparty}/http_status_code.h   |  0
 net/client/dap_client_http.c                  | 23 ++++++++++++++++---
 net/client/dap_client_pvt.c                   |  5 ++--
 net/client/include/dap_client_http.h          |  3 ++-
 net/server/enc_server/dap_enc_http.c          |  2 +-
 net/server/json_rpc/include/dap_json_rpc.h    |  2 +-
 .../include/dap_json_rpc_response_handler.h   |  3 ++-
 .../src/dap_json_rpc_response_handler.c       |  5 ++--
 8 files changed, 32 insertions(+), 11 deletions(-)
 rename {net/server/http_server/include => 3rdparty}/http_status_code.h (100%)

diff --git a/net/server/http_server/include/http_status_code.h b/3rdparty/http_status_code.h
similarity index 100%
rename from net/server/http_server/include/http_status_code.h
rename to 3rdparty/http_status_code.h
diff --git a/net/client/dap_client_http.c b/net/client/dap_client_http.c
index 34560bee3..50200f98b 100644
--- a/net/client/dap_client_http.c
+++ b/net/client/dap_client_http.c
@@ -67,6 +67,22 @@ static uint32_t s_max_attempts = 5;
 static WOLFSSL_CTX *s_ctx;
 #endif
 
+http_status_code_t s_extract_http_code(void *a_response, size_t a_response_size) {
+    char l_http_code_str[3] = {'\0'};
+    size_t l_first_space = 0;
+    for (;l_first_space < a_response_size; l_first_space++) {
+        if (((const char*)a_response)[l_first_space] == ' ')
+            break;
+    }
+    if (l_first_space + 3 > a_response_size)
+        return 0;
+    l_http_code_str[0] = ((const char*)a_response)[l_first_space+1];
+    l_http_code_str[1] = ((const char*)a_response)[l_first_space+2];
+    l_http_code_str[2] = ((const char*)a_response)[l_first_space+3];
+    http_status_code_t l_http_code = strtoul(l_http_code_str, NULL, 10);
+    return l_http_code;
+}
+
 /**
  * @brief dap_client_http_init
  * @return
@@ -357,7 +373,8 @@ static void s_http_read(dap_events_socket_t * a_es, void * arg)
                 l_client_http->response_callback(
                         l_client_http->response + l_client_http->header_length,
                         l_client_http->content_length,
-                        l_client_http->callbacks_arg);
+                        l_client_http->callbacks_arg, s_extract_http_code(
+                                l_client_http->response, l_client_http->response_size));
             l_client_http->response_size -= l_client_http->header_length;
             l_client_http->response_size -= l_client_http->content_length;
             l_client_http->header_length = 0;
@@ -442,12 +459,12 @@ static void s_es_delete(dap_events_socket_t * a_es, void * a_arg)
                l_response_size);
 
             //l_client_http->error_callback(-10 , l_client_http->callbacks_arg);
-
+            http_status_code_t l_status_code = s_extract_http_code(l_client_http->response, l_client_http->response_size);
             if(l_client_http->response_callback)
                 l_client_http->response_callback(
                         l_client_http->response + l_client_http->header_length,
                         l_response_size,
-                        l_client_http->callbacks_arg);
+                        l_client_http->callbacks_arg, l_status_code);
             l_client_http->were_callbacks_called = true;
         }else if (l_client_http->response_size){
             log_it(L_INFO, "Remote server disconnected with reply. Body is empty, only headers are in");
diff --git a/net/client/dap_client_pvt.c b/net/client/dap_client_pvt.c
index 70eabe795..8bc48faf8 100644
--- a/net/client/dap_client_pvt.c
+++ b/net/client/dap_client_pvt.c
@@ -79,7 +79,7 @@ static void s_stream_ctl_error(dap_client_t *a_client, void *a_arg, int a_error)
 static void s_stage_stream_streaming(dap_client_t *a_client, void *a_arg);
 
 // STREAM stage callbacks
-static void s_request_response(void *a_response, size_t a_response_size, void * a_obj);
+static void s_request_response(void *a_response, size_t a_response_size, void * a_obj, http_status_code_t http_status);
 static void s_request_error(int a_error_code, void *a_obj);
 
 // stream callbacks
@@ -916,8 +916,9 @@ static void s_request_error(int a_err_code, void * a_obj)
  * @param a_response_size
  * @param a_obj
  */
-static void s_request_response(void * a_response, size_t a_response_size, void * a_obj)
+static void s_request_response(void * a_response, size_t a_response_size, void * a_obj, http_status_code_t a_http_code)
 {
+    (void)a_http_code;
     dap_client_pvt_t * l_client_pvt = (dap_client_pvt_t *) a_obj;
     assert(l_client_pvt);
     l_client_pvt->http_client = NULL;
diff --git a/net/client/include/dap_client_http.h b/net/client/include/dap_client_http.h
index ad98ce7d3..190d52c52 100644
--- a/net/client/include/dap_client_http.h
+++ b/net/client/include/dap_client_http.h
@@ -24,13 +24,14 @@
 #include <stdint.h>
 #include <stddef.h>
 #include "dap_worker.h"
+#include "http_status_code.h"
 #ifdef __cplusplus
 extern "C" {
 #endif
 
 typedef void (*dap_client_http_callback_error_t)(int, void *); // Callback for specific http client operations
 typedef void (*dap_client_http_callback_error_ext_t)(int,int , void *,size_t, void *); // Callback with extended error processing
-typedef void (*dap_client_http_callback_data_t)(void *, size_t, void *); // Callback for specific http client operations
+typedef void (*dap_client_http_callback_data_t)(void *, size_t, void *, http_status_code_t); // Callback for specific http client operations
 
 typedef struct dap_client_http {
     // TODO move unnessassary fields to dap_client_http_pvt privat structure
diff --git a/net/server/enc_server/dap_enc_http.c b/net/server/enc_server/dap_enc_http.c
index ff91ffc6c..73a1073d1 100644
--- a/net/server/enc_server/dap_enc_http.c
+++ b/net/server/enc_server/dap_enc_http.c
@@ -46,7 +46,7 @@
 #include "include/dap_enc_http.h"
 #include "dap_enc_base64.h"
 #include "dap_enc_msrln.h"
-#include "include/http_status_code.h"
+#include "http_status_code.h"
 #include "dap_http_ban_list_client.h"
 #include "json.h"
 #include "dap_http_ban_list_client.h"
diff --git a/net/server/json_rpc/include/dap_json_rpc.h b/net/server/json_rpc/include/dap_json_rpc.h
index 1b0867e5a..f22d90f59 100644
--- a/net/server/json_rpc/include/dap_json_rpc.h
+++ b/net/server/json_rpc/include/dap_json_rpc.h
@@ -25,7 +25,7 @@
 
 #pragma once
 #include "dap_http_simple.h"
-#include "include/http_status_code.h"
+#include "http_status_code.h"
 #include "dap_strfuncs.h"
 #include "dap_json_rpc_request.h"
 #include "dap_json_rpc_request_handler.h"
diff --git a/net/server/json_rpc/rpc_core/include/dap_json_rpc_response_handler.h b/net/server/json_rpc/rpc_core/include/dap_json_rpc_response_handler.h
index 9869db745..05d0232ea 100644
--- a/net/server/json_rpc/rpc_core/include/dap_json_rpc_response_handler.h
+++ b/net/server/json_rpc/rpc_core/include/dap_json_rpc_response_handler.h
@@ -25,6 +25,7 @@
 #pragma once
 
 #include "dap_json_rpc_response.h"
+#include "http_status_code.h"
 #include "uthash.h"
 
 #ifdef __cplusplus
@@ -48,7 +49,7 @@ void dap_json_rpc_response_handler(dap_json_rpc_response_t *a_response);
 
 uint64_t dap_json_rpc_response_get_new_id(void);
 
-void dap_json_rpc_response_accepted(void *a_data, size_t a_size_data, void *a_obj);
+void dap_json_rpc_response_accepted(void *a_data, size_t a_size_data, void *a_obj, http_status_code_t http_code);
 
 #ifdef __cplusplus
 }
diff --git a/net/server/json_rpc/rpc_core/src/dap_json_rpc_response_handler.c b/net/server/json_rpc/rpc_core/src/dap_json_rpc_response_handler.c
index d84c9d4b9..ed80414c1 100644
--- a/net/server/json_rpc/rpc_core/src/dap_json_rpc_response_handler.c
+++ b/net/server/json_rpc/rpc_core/src/dap_json_rpc_response_handler.c
@@ -60,9 +60,10 @@ uint64_t dap_json_rpc_response_get_new_id(void)
     return l_ret;
 }
 
-void dap_json_rpc_response_accepted(void *a_data, size_t a_size_data, void *a_obj)
+void dap_json_rpc_response_accepted(void *a_data, size_t a_size_data, void *a_obj, http_status_code_t http_status)
 {
-    (void) a_obj;
+    (void)http_status;
+    (void)a_obj;
     log_it(L_NOTICE, "Pre handling response");
     char *l_str = DAP_NEW_SIZE(char, a_size_data);
     memcpy(l_str, a_data, a_size_data);
-- 
GitLab