diff --git a/dap-sdk/net/server/http_server/dap_http.c b/dap-sdk/net/server/http_server/dap_http.c index 8c3c495c38cca9f4d74fbb5232617fddad3c3ea4..6b27d5449f1b4c5f7647bb15a08a0725ac705df6 100644 --- a/dap-sdk/net/server/http_server/dap_http.c +++ b/dap-sdk/net/server/http_server/dap_http.c @@ -137,7 +137,7 @@ void dap_http_delete( dap_server_t *a_server, void * a_arg ) * @param a_write_callback Callback for write in DATA state * @param a_error_callback Callback for error processing */ -void dap_http_add_proc(dap_http_t *a_http, const char *a_url_path, void *a_inheritor +dap_http_url_proc_t * dap_http_add_proc(dap_http_t *a_http, const char *a_url_path, void *a_inheritor ,dap_http_client_callback_t a_new_callback ,dap_http_client_callback_t a_delete_callback ,dap_http_client_callback_t a_headers_read_callback 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 f0369850927fc2d11d6417e1aeed901a7d695593..27a9ae8d55b1ed7a399f4b71ed98d1fad28a45b4 100644 --- a/dap-sdk/net/server/http_server/dap_http_simple.c +++ b/dap-sdk/net/server/http_server/dap_http_simple.c @@ -119,21 +119,20 @@ void dap_http_simple_module_deinit( void ) * @param a_reply_size_max Maximum reply size * @param a_callback Callback for data processing */ -struct dap_http_simple_url_proc * dap_http_simple_proc_add( dap_http_t *a_http, const char *a_url_path, size_t a_reply_size_max, dap_http_simple_callback_t a_callback ) +struct dap_http_url_proc * dap_http_simple_proc_add( dap_http_t *a_http, const char *a_url_path, size_t a_reply_size_max, dap_http_simple_callback_t a_callback ) { dap_http_simple_url_proc_t *l_url_proc = DAP_NEW_Z( dap_http_simple_url_proc_t ); l_url_proc->proc_callback = a_callback; l_url_proc->reply_size_max = a_reply_size_max; - dap_http_add_proc( a_http, a_url_path, + return dap_http_add_proc( a_http, a_url_path, l_url_proc, // Internal structure s_http_client_new, // Contrustor - NULL, // Destructor + s_http_client_delete, // Destructor s_http_client_headers_read, NULL, // Headers read, write s_http_client_data_read, s_http_client_data_write, // Data read, write NULL); // errror - return l_url_proc; } static void _free_user_agents_list() @@ -324,7 +323,6 @@ bool s_proc_queue_callback(dap_proc_thread_t * a_thread, void * a_arg ) s_set_writable_flags( l_http_simple); dap_proc_thread_assign_on_worker_inter(a_thread, l_http_simple->worker, l_http_simple->esocket); - s_http_simple_delete( l_http_simple ); return true; } /** @@ -337,6 +335,7 @@ static void s_http_client_new( dap_http_client_t *a_http_client, void *a_arg ) (void) a_arg; a_http_client->_inheritor = DAP_NEW_Z( dap_http_simple_t ); dap_http_simple_t * l_http_simple = DAP_HTTP_SIMPLE(a_http_client); + dap_http_simple_url_proc_t * l_http_simple_url_proc = DAP_HTTP_SIMPLE_URL_PROC( a_http_client->proc ); // log_it(L_DEBUG,"dap_http_simple_headers_read"); // Sleep(300); @@ -372,7 +371,7 @@ static void s_http_client_headers_read( dap_http_client_t *a_http_client, void * { (void) a_arg; dap_http_simple_t * l_http_simple = DAP_HTTP_SIMPLE(a_http_client); - + assert(l_http_simple); if( a_http_client->in_content_length ) { // dbg if( a_http_client->in_content_length < 3){ if( a_http_client->in_content_length > 0){ @@ -387,7 +386,6 @@ static void s_http_client_headers_read( dap_http_client_t *a_http_client, void * log_it(L_ERROR, "Not defined content-length %u in request", a_http_client->in_content_length); } else { log_it( L_DEBUG, "No data section, execution proc callback" ); - a_http_client->_inheritor = NULL; dap_events_socket_remove_from_worker_unsafe(l_http_simple->esocket ,l_http_simple->worker); dap_proc_queue_add_callback_inter( l_http_simple->worker->proc_queue_input, s_proc_queue_callback, l_http_simple); @@ -455,8 +453,6 @@ void s_http_client_data_read( dap_http_client_t *a_http_client, void * a_arg ) // bool isOK=true; log_it( L_INFO,"Data for http_simple_request collected" ); dap_events_socket_remove_from_worker_unsafe(a_http_client->esocket,a_http_client->esocket->worker); - a_http_client->_inheritor = NULL; - l_http_simple->http_client_uuid = a_http_client->esocket->uuid; dap_proc_queue_add_callback_inter( l_http_simple->worker->proc_queue_input , s_proc_queue_callback, l_http_simple); } } 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 f04aad7483a21a53cc463527b287baf2d0d67370..56ca3f4f94ff5a6799fcf24c7597361908221a8f 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,27 +87,38 @@ 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; - 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; + 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); } - pthread_rwlock_unlock(&l_http_client->http->url_proc->cache_rwlock); return; } @@ -135,7 +146,6 @@ void dap_http_client_delete( dap_events_socket_t * a_esocket, void *a_arg ) } } DAP_DEL_Z(l_http_client->_inheritor) - } diff --git a/dap-sdk/net/server/http_server/include/dap_http.h b/dap-sdk/net/server/http_server/include/dap_http.h index 3d516531eb91a8cd2d39e8ccec1e79a2d926e666..e138e88a452e75d676dcaf0c6d0f1e3796aa01ea 100644 --- a/dap-sdk/net/server/http_server/include/dap_http.h +++ b/dap-sdk/net/server/http_server/include/dap_http.h @@ -72,7 +72,7 @@ void dap_http_deinit( ); // Deinit module int dap_http_new( dap_server_t *a_server, const char *a_server_name ); // Create dap_http structure in the internal data field of dap_server_t instance void dap_http_delete( dap_server_t *a_server, void *a_arg ); // Clear dap_http structure in the internal data field of dap_server_t instance -void dap_http_add_proc(dap_http_t *sh, const char *url_path, void *internal +dap_http_url_proc_t * dap_http_add_proc(dap_http_t *sh, const char *url_path, void *internal ,dap_http_client_callback_t new_callback ,dap_http_client_callback_t delete_callback ,dap_http_client_callback_t headers_read_callback diff --git a/dap-sdk/net/server/http_server/include/dap_http_simple.h b/dap-sdk/net/server/http_server/include/dap_http_simple.h index 1c616f0848c0303e17b16076cc476eb531d4bcf7..a2a40243ff297e0af80a7b5d14316eaa75622be0 100644 --- a/dap-sdk/net/server/http_server/include/dap_http_simple.h +++ b/dap-sdk/net/server/http_server/include/dap_http_simple.h @@ -65,7 +65,7 @@ typedef struct dap_http_simple { #define DAP_HTTP_SIMPLE(a) ((dap_http_simple_t*) (a)->_inheritor ) -struct dap_http_simple_url_proc * dap_http_simple_proc_add( dap_http_t *sh, const char *url_path, size_t reply_size_max, dap_http_simple_callback_t cb ); // Add simple processor +struct dap_http_url_proc * dap_http_simple_proc_add( dap_http_t *sh, const char *url_path, size_t reply_size_max, dap_http_simple_callback_t cb ); // Add simple processor int dap_http_simple_module_init( void ); void dap_http_simple_module_deinit(void);