Skip to content
Snippets Groups Projects
Commit d903515d authored by Dmitriy A. Gerasimov's avatar Dmitriy A. Gerasimov
Browse files

[*] Fixed caching mechanism

parent cbceec45
No related branches found
No related tags found
No related merge requests found
Pipeline #7215 passed with stage
in 11 seconds
...@@ -446,10 +446,15 @@ size_t dap_http_simple_reply(dap_http_simple_t *a_http_simple, void *a_data, siz ...@@ -446,10 +446,15 @@ size_t dap_http_simple_reply(dap_http_simple_t *a_http_simple, void *a_data, siz
*/ */
dap_http_cache_t * dap_http_simple_make_cache_from_reply(dap_http_simple_t * a_http_simple, time_t a_ts_expire ) dap_http_cache_t * dap_http_simple_make_cache_from_reply(dap_http_simple_t * a_http_simple, time_t a_ts_expire )
{ {
// Because we call it from callback, we have no headers ready for output
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->http->url_proc,
a_http_simple->reply_byte, a_http_simple->reply_byte,
a_http_simple->reply_size, a_http_simple->reply_size,
a_http_simple->http_client->out_headers, NULL,200, a_ts_expire); a_http_simple->http_client->out_headers,NULL,
200, a_ts_expire);
} }
/** /**
......
...@@ -88,12 +88,23 @@ void dap_http_client_new( dap_events_socket_t *a_esocket, void *a_arg ) ...@@ -88,12 +88,23 @@ void dap_http_client_new( dap_events_socket_t *a_esocket, void *a_arg )
l_http_client->state_write = DAP_HTTP_CLIENT_STATE_NONE; l_http_client->state_write = DAP_HTTP_CLIENT_STATE_NONE;
pthread_rwlock_rdlock(&l_http_client->http->url_proc->cache_rwlock); pthread_rwlock_rdlock(&l_http_client->http->url_proc->cache_rwlock);
if(l_http_client->http->url_proc->cache){ dap_http_cache_t * l_http_cache = l_http_client->http->url_proc->cache;
if ( ! l_http_client->http->url_proc->cache->ts_expire || l_http_client->http->url_proc->cache->ts_expire >= time(NULL) ){ if(l_http_cache){
l_http_client->out_headers = dap_http_headers_dup(l_http_client->http->url_proc->cache->headers); 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) if(s_debug_http)
log_it(L_DEBUG,"%d Out: prepare cached headers", l_http_client->esocket->socket); 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;
} }
} }
pthread_rwlock_unlock(&l_http_client->http->url_proc->cache_rwlock); pthread_rwlock_unlock(&l_http_client->http->url_proc->cache_rwlock);
...@@ -594,7 +605,7 @@ void dap_http_client_write( dap_events_socket_t * a_esocket, void *a_arg ) ...@@ -594,7 +605,7 @@ void dap_http_client_write( dap_events_socket_t * a_esocket, void *a_arg )
if(l_sent){ if(l_sent){
if ( l_http_client->out_cache_position + l_sent >= l_http_client->proc->cache->body_size ){ // All is sent if ( l_http_client->out_cache_position + l_sent >= l_http_client->proc->cache->body_size ){ // All is sent
if(s_debug_http) if(s_debug_http)
log_it(L_DEBUG,"%d Out: All cached data over, signal to close connection", l_http_client->esocket->socket); log_it(L_DEBUG,"Out %d: All cached data over, signal to close connection", l_http_client->esocket->socket);
l_http_client->esocket->flags |= DAP_SOCK_SIGNAL_CLOSE; l_http_client->esocket->flags |= DAP_SOCK_SIGNAL_CLOSE;
l_http_client->state_write = DAP_HTTP_CLIENT_STATE_NONE; l_http_client->state_write = DAP_HTTP_CLIENT_STATE_NONE;
dap_events_socket_set_writable_unsafe( a_esocket, false ); dap_events_socket_set_writable_unsafe( a_esocket, false );
...@@ -615,33 +626,37 @@ void dap_http_client_write( dap_events_socket_t * a_esocket, void *a_arg ) ...@@ -615,33 +626,37 @@ void dap_http_client_write( dap_events_socket_t * a_esocket, void *a_arg )
* @brief dap_http_client_out_header_generate Produce general headers * @brief dap_http_client_out_header_generate Produce general headers
* @param cl_ht HTTP client instance * @param cl_ht HTTP client instance
*/ */
void dap_http_client_out_header_generate(dap_http_client_t *cl_ht) void dap_http_client_out_header_generate(dap_http_client_t *a_http_client)
{ {
char buf[1024]; char buf[1024];
if ( cl_ht->reply_status_code == 200 ) { if ( a_http_client->reply_status_code == 200 ) {
if (s_debug_http)
if ( cl_ht->out_last_modified ) { log_it(L_DEBUG, "Out headers generate for sock %d", a_http_client->esocket->socket);
dap_time_to_str_rfc822( buf, sizeof(buf), cl_ht->out_last_modified ); if ( a_http_client->out_last_modified ) {
dap_http_header_add( &cl_ht->out_headers, "Last-Modified", buf ); dap_time_to_str_rfc822( buf, sizeof(buf), a_http_client->out_last_modified );
} dap_http_header_add( &a_http_client->out_headers, "Last-Modified", buf );
if ( cl_ht->out_content_type[0] ) { }
dap_http_header_add(&cl_ht->out_headers,"Content-Type",cl_ht->out_content_type); if ( a_http_client->out_content_type[0] ) {
log_it(L_DEBUG,"output: Content-Type = '%s'",cl_ht->out_content_type); dap_http_header_add(&a_http_client->out_headers,"Content-Type",a_http_client->out_content_type);
} log_it(L_DEBUG,"output: Content-Type = '%s'",a_http_client->out_content_type);
if ( cl_ht->out_content_length ) { }
dap_snprintf(buf,sizeof(buf),"%llu",(unsigned long long)cl_ht->out_content_length); if ( a_http_client->out_content_length ) {
dap_http_header_add(&cl_ht->out_headers,"Content-Length",buf); dap_snprintf(buf,sizeof(buf),"%llu",(unsigned long long)a_http_client->out_content_length);
log_it(L_DEBUG,"output: Content-Length = %llu",cl_ht->out_content_length); dap_http_header_add(&a_http_client->out_headers,"Content-Length",buf);
} log_it(L_DEBUG,"output: Content-Length = %llu",a_http_client->out_content_length);
} }
}else
if (s_debug_http)
log_it(L_WARNING, "Out headers: nothing generate for sock %d, http code %d", a_http_client->esocket->socket,
a_http_client->reply_status_code);
if ( cl_ht->out_connection_close || !cl_ht->keep_alive ) if ( a_http_client->out_connection_close || !a_http_client->keep_alive )
dap_http_header_add( &cl_ht->out_headers, "Connection","Close" ); dap_http_header_add( &a_http_client->out_headers, "Connection","Close" );
dap_http_header_add( &cl_ht->out_headers, "Server-Name", cl_ht->http->server_name ); dap_http_header_add( &a_http_client->out_headers, "Server-Name", a_http_client->http->server_name );
log_it( L_DEBUG,"Output: Headers generated" ); log_it( L_DEBUG,"Output: Headers generated" );
} }
/** /**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment