From ac6f8cc01b8cac681d543aebee194349e2a19f6c Mon Sep 17 00:00:00 2001
From: "Dmitriy A. Gerasimov" <dmitriy.gerasimov@demlabs.net>
Date: Wed, 17 Feb 2021 00:17:19 +0700
Subject: [PATCH] [*] Fix

---
 .../net/server/http_server/dap_http_simple.c  | 18 +---
 .../http_server/http_client/dap_http_client.c | 86 +++++++++----------
 2 files changed, 45 insertions(+), 59 deletions(-)

diff --git a/dap-sdk/net/server/http_server/dap_http_simple.c b/dap-sdk/net/server/http_server/dap_http_simple.c
index fb724ce2f7..f3ed0bcb1f 100644
--- a/dap-sdk/net/server/http_server/dap_http_simple.c
+++ b/dap-sdk/net/server/http_server/dap_http_simple.c
@@ -330,22 +330,10 @@ static void s_http_client_delete( dap_http_client_t *a_http_client, void *arg )
 {
     dap_http_simple_t * l_http_simple = DAP_HTTP_SIMPLE(a_http_client);
     if (l_http_simple){
-        if(l_http_simple->reply_byte)
-            DAP_DELETE(l_http_simple->reply_byte);
+        DAP_DEL_Z(l_http_simple->reply_byte);
     }
 }
 
-static void s_http_simple_delete( dap_http_simple_t *a_http_simple)
-{
-    if (a_http_simple){
-        if(a_http_simple->reply_byte)
-            DAP_DELETE(a_http_simple->reply_byte);
-        DAP_DELETE(a_http_simple);
-    }
-}
-
-
-
 static void s_http_client_headers_read( dap_http_client_t *a_http_client, void *a_arg )
 {
     (void) a_arg;
@@ -404,7 +392,7 @@ static void s_http_client_data_write( dap_http_client_t * a_http_client, void *a
         //cl_ht->client->signal_close=cl_ht->keep_alive;
         a_http_client->esocket->flags |= DAP_SOCK_SIGNAL_CLOSE;
         //dap_client_ready_to_write(cl_ht->client,false);
-        DAP_DELETE(l_http_simple->reply );
+        //DAP_DELETE(l_http_simple->reply );
     }
 }
 
@@ -474,7 +462,7 @@ dap_http_cache_t * dap_http_simple_make_cache_from_reply(dap_http_simple_t * a_h
     a_http_simple->http_client->out_content_length = a_http_simple->reply_size;
     a_http_simple->http_client->reply_status_code = 200;
     dap_http_client_out_header_generate(a_http_simple->http_client);
-    return dap_http_cache_update(a_http_simple->http_client->http->url_proc,
+    return dap_http_cache_update(a_http_simple->http_client->proc,
                                  a_http_simple->reply_byte,
                                  a_http_simple->reply_size,
                                  a_http_simple->http_client->out_headers,NULL,
diff --git a/dap-sdk/net/server/http_server/http_client/dap_http_client.c b/dap-sdk/net/server/http_server/http_client/dap_http_client.c
index 374bb106a8..87cd6e909b 100644
--- a/dap-sdk/net/server/http_server/http_client/dap_http_client.c
+++ b/dap-sdk/net/server/http_server/http_client/dap_http_client.c
@@ -87,38 +87,6 @@ void dap_http_client_new( dap_events_socket_t *a_esocket, void *a_arg )
     l_http_client->state_read = DAP_HTTP_CLIENT_STATE_START;
     l_http_client->state_write = DAP_HTTP_CLIENT_STATE_NONE;
 
-    if(l_http_client->http &&  l_http_client->http->url_proc){
-        // Check if present cache
-        pthread_rwlock_rdlock(&l_http_client->http->url_proc->cache_rwlock);
-        dap_http_cache_t * l_http_cache = l_http_client->http->url_proc->cache;
-        if(l_http_cache){
-            if ( ! l_http_cache->ts_expire || l_http_cache->ts_expire >= time(NULL) ){
-                l_http_client->out_headers = dap_http_headers_dup(l_http_cache->headers);
-                l_http_client->out_content_length = l_http_cache->body_size;
-                l_http_client->reply_status_code = l_http_cache->response_code;
-                if(l_http_cache->response_phrase)
-                    strncpy(l_http_client->reply_reason_phrase,l_http_cache->response_phrase,sizeof (l_http_client->reply_reason_phrase)-1);
-
-                if(s_debug_http)
-                    log_it(L_DEBUG,"%d Out: prepare cached headers", l_http_client->esocket->socket);
-
-            }else if (l_http_cache){
-                pthread_rwlock_unlock(&l_http_client->http->url_proc->cache_rwlock);
-                pthread_rwlock_wrlock(&l_http_client->http->url_proc->cache_rwlock);
-                dap_http_cache_delete(l_http_cache);
-                l_http_client->http->url_proc->cache = NULL;
-                l_http_cache == NULL;
-            }
-        }
-        if (l_http_cache == NULL){
-            pthread_rwlock_unlock(&l_http_client->http->url_proc->cache_rwlock);
-            // Call client constructor if we're not in cache mode
-            if(l_http_client->http->url_proc->new_callback)
-                l_http_client->http->url_proc->new_callback(l_http_client, NULL);
-
-        }else
-            pthread_rwlock_unlock(&l_http_client->http->url_proc->cache_rwlock);
-    }
     return;
 }
 
@@ -438,6 +406,36 @@ void dap_http_client_read( dap_events_socket_t *a_esocket, void *a_arg )
 
                 if ( url_proc ) {
                     l_http_client->state_read = DAP_HTTP_CLIENT_STATE_HEADERS;
+                    // Check if present cache
+                    pthread_rwlock_rdlock(&l_http_client->proc->cache_rwlock);
+                    dap_http_cache_t * l_http_cache = l_http_client->proc->cache;
+                    if(l_http_cache){
+                        if ( ! l_http_cache->ts_expire || l_http_cache->ts_expire >= time(NULL) ){
+                            l_http_client->out_headers = dap_http_headers_dup(l_http_cache->headers);
+                            l_http_client->out_content_length = l_http_cache->body_size;
+                            l_http_client->reply_status_code = l_http_cache->response_code;
+                            if(l_http_cache->response_phrase)
+                                strncpy(l_http_client->reply_reason_phrase,l_http_cache->response_phrase,sizeof (l_http_client->reply_reason_phrase)-1);
+
+                            if(s_debug_http)
+                                log_it(L_DEBUG,"%d Out: prepare cached headers", l_http_client->esocket->socket);
+
+                        }else if (l_http_cache){
+                            pthread_rwlock_unlock(&l_http_client->proc->cache_rwlock);
+                            pthread_rwlock_wrlock(&l_http_client->proc->cache_rwlock);
+                            dap_http_cache_delete(l_http_cache);
+                            l_http_client->proc->cache = NULL;
+                            l_http_cache == NULL;
+                        }
+                    }
+                    if (l_http_cache == NULL){
+                        pthread_rwlock_unlock(&l_http_client->proc->cache_rwlock);
+                        // Call client constructor
+                        if(l_http_client->proc->new_callback)
+                            l_http_client->proc->new_callback(l_http_client, NULL);
+                    }else
+                        pthread_rwlock_unlock(&l_http_client->proc->cache_rwlock);
+
                 } else {
                     log_it( L_WARNING, "Input: unprocessed URL request %s is rejected", l_http_client->url_path );
                     s_report_error_and_restart( a_esocket, l_http_client );
@@ -477,12 +475,12 @@ void dap_http_client_read( dap_events_socket_t *a_esocket, void *a_arg )
                         }
                     }
 
-                    pthread_rwlock_rdlock(&l_http_client->http->url_proc->cache_rwlock);
-                    if ( l_http_client->http->url_proc->cache == NULL &&  l_http_client->proc->headers_read_callback ) {
-                        pthread_rwlock_unlock(&l_http_client->http->url_proc->cache_rwlock);
+                    pthread_rwlock_rdlock(&l_http_client->proc->cache_rwlock);
+                    if ( l_http_client->proc->cache == NULL &&  l_http_client->proc->headers_read_callback ) {
+                        pthread_rwlock_unlock(&l_http_client->proc->cache_rwlock);
                         l_http_client->proc->headers_read_callback( l_http_client, NULL );
                     }else{
-                        pthread_rwlock_unlock(&l_http_client->http->url_proc->cache_rwlock);
+                        pthread_rwlock_unlock(&l_http_client->proc->cache_rwlock);
                         if(s_debug_http)
                             log_it(L_DEBUG, "Cache is present, don't call underlaying callbacks");
                     }
@@ -502,13 +500,13 @@ void dap_http_client_read( dap_events_socket_t *a_esocket, void *a_arg )
                 size_t read_bytes = 0;
                 if(s_debug_http)
                     log_it(L_DEBUG, "dap_http_client_read: DAP_HTTP_CLIENT_STATE_DATA");
-                pthread_rwlock_rdlock(&l_http_client->http->url_proc->cache_rwlock);
+                pthread_rwlock_rdlock(&l_http_client->proc->cache_rwlock);
                 if ( l_http_client->proc->cache == NULL && l_http_client->proc->data_read_callback ) {
-                    pthread_rwlock_unlock(&l_http_client->http->url_proc->cache_rwlock);
+                    pthread_rwlock_unlock(&l_http_client->proc->cache_rwlock);
                     l_http_client->proc->data_read_callback( l_http_client, &read_bytes );
                     dap_events_socket_shrink_buf_in( a_esocket, read_bytes );
                 } else {
-                    pthread_rwlock_unlock(&l_http_client->http->url_proc->cache_rwlock);
+                    pthread_rwlock_unlock(&l_http_client->proc->cache_rwlock);
                     a_esocket->buf_in_size = 0;
                     l_http_client->state_write=DAP_HTTP_CLIENT_STATE_START;
                     dap_events_socket_set_writable_unsafe(a_esocket, true);
@@ -592,16 +590,16 @@ void dap_http_client_write( dap_events_socket_t * a_esocket, void *a_arg )
                 if  ( ( l_http_client->proc->cache == NULL &&
                         l_http_client->proc->data_write_callback ) ||
                       ( l_http_client->proc->data_write_callback &&
-                        l_http_client->http->url_proc->cache->ts_expire < time(NULL) &&
+                        l_http_client->proc->cache->ts_expire < time(NULL) &&
                         l_http_client->out_cache_position == NULL // Check if we haven't started
                                                                   // to share cached data
                         )
                     ){
                     if (l_http_client->proc->cache){
-                        pthread_rwlock_unlock(&l_http_client->http->url_proc->cache_rwlock);
-                        pthread_rwlock_wrlock(&l_http_client->http->url_proc->cache_rwlock);
-                        dap_http_cache_delete(l_http_client->http->url_proc->cache);
-                        l_http_client->http->url_proc->cache = NULL;
+                        pthread_rwlock_unlock(&l_http_client->proc->cache_rwlock);
+                        pthread_rwlock_wrlock(&l_http_client->proc->cache_rwlock);
+                        dap_http_cache_delete(l_http_client->proc->cache);
+                        l_http_client->proc->cache = NULL;
                         if(s_debug_http)
                             log_it(L_NOTICE,"Cache expired and dropped out");
                     }else if (s_debug_http)
-- 
GitLab