Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • cellframe/cellframe-sdk
  • MIKA83/cellframe-sdk
2 results
Show changes
Commits on Source (5)
......@@ -26,8 +26,20 @@
#define LOG_TAG "http_cache"
dap_http_cache_t * dap_http_cache_update(struct dap_http_url_proc * a_url_proc, byte_t * a_body, size_t a_body_size,
dap_http_header_t * a_headers, time_t a_ts_expire )
/**
* @brief dap_http_cache_update
* @param a_url_proc
* @param a_body
* @param a_body_size
* @param a_headers
* @param a_response_phrase
* @param a_respoonse_code
* @param ts_expire
* @return
*/
dap_http_cache_t * dap_http_cache_update(struct dap_http_url_proc * a_url_proc, const byte_t * a_body, size_t a_body_size,
dap_http_header_t * a_headers, const char * a_response_phrase, int a_respoonse_code,
time_t a_ts_expire )
{
dap_http_cache_t * l_ret = DAP_NEW_Z(dap_http_cache_t);
if(a_body_size){
......@@ -36,8 +48,21 @@ dap_http_cache_t * dap_http_cache_update(struct dap_http_url_proc * a_url_proc,
l_ret->body_size = a_body_size;
}
l_ret->headers = dap_http_headers_dup( a_headers);
l_ret->ts_expire = a_ts_expire;
l_ret->url_proc = a_url_proc;
if(a_response_phrase)
l_ret->response_phrase = strdup(a_response_phrase);
l_ret->response_code = a_respoonse_code;
//Here we cut off 'Date' header because we add it new on each cached request
dap_http_header_t * l_hdr_date= dap_http_header_find(l_ret->headers,"Date");
if(l_hdr_date)
dap_http_header_remove(&l_ret->headers,l_hdr_date);
// Reset current cache for url_proc and replace with our own
pthread_rwlock_wrlock(&a_url_proc->cache_rwlock);
dap_http_cache_delete(a_url_proc->cache);
a_url_proc->cache = l_ret;
......
......@@ -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 )
{
// 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,
a_http_simple->reply_byte,
a_http_simple->reply_size,
a_http_simple->http_client->out_headers, 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 )
l_http_client->state_write = DAP_HTTP_CLIENT_STATE_NONE;
pthread_rwlock_rdlock(&l_http_client->http->url_proc->cache_rwlock);
if(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) ){
l_http_client->out_headers = dap_http_headers_dup(l_http_client->http->url_proc->cache->headers);
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;
}
}
pthread_rwlock_unlock(&l_http_client->http->url_proc->cache_rwlock);
......@@ -456,27 +467,39 @@ void dap_http_client_read( dap_events_socket_t *a_esocket, void *a_arg )
}
}
if ( l_http_client->proc->headers_read_callback ) {
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);
l_http_client->proc->headers_read_callback( l_http_client, NULL );
}
}else
pthread_rwlock_unlock(&l_http_client->http->url_proc->cache_rwlock);
// If no headers callback we go to the DATA processing
if( l_http_client->in_content_length ) {
//log_it( L_DEBUG, "headers -> DAP_HTTP_CLIENT_STATE_DATA" );
if(s_debug_http)
log_it( L_DEBUG, "headers -> DAP_HTTP_CLIENT_STATE_DATA" );
l_http_client->state_read = DAP_HTTP_CLIENT_STATE_DATA;
}else{ // No data, its over
l_http_client->state_write=DAP_HTTP_CLIENT_STATE_START;
dap_events_socket_set_writable_unsafe(a_esocket, true);
}
}
dap_events_socket_shrink_buf_in( a_esocket, l_eol_pos + 1 );
} break;
case DAP_HTTP_CLIENT_STATE_DATA:{
size_t read_bytes = 0;
//log_it(L_DEBUG, "dap_http_client_read: DAP_HTTP_CLIENT_STATE_DATA");
if ( l_http_client->proc->data_read_callback ) {
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);
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);
l_http_client->proc->data_read_callback( l_http_client, &read_bytes );
dap_events_socket_shrink_buf_in( a_esocket, read_bytes );
} else {
log_it( L_WARNING, "data_read callback is NULL in DAP_HTTP_CLIENT_STATE_DATA" );
pthread_rwlock_unlock(&l_http_client->http->url_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);
}
} break;
case DAP_HTTP_CLIENT_STATE_NONE: {
......@@ -557,7 +580,7 @@ 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->http->url_proc->cache->ts_expire < time(NULL) &&
l_http_client->out_cache_position == NULL // Check if we haven't started
// to share cached data
)
......@@ -569,7 +592,9 @@ void dap_http_client_write( dap_events_socket_t * a_esocket, void *a_arg )
l_http_client->http->url_proc->cache = NULL;
if(s_debug_http)
log_it(L_NOTICE,"Cache expired and dropped out");
}
}else if (s_debug_http)
log_it(L_DEBUG, "No cache so we call write callback");
pthread_rwlock_unlock(&l_http_client->proc->cache_rwlock);
l_http_client->proc->data_write_callback( l_http_client, NULL );
}else if(l_http_client->proc->cache) {
......@@ -580,7 +605,7 @@ void dap_http_client_write( dap_events_socket_t * a_esocket, void *a_arg )
if(l_sent){
if ( l_http_client->out_cache_position + l_sent >= l_http_client->proc->cache->body_size ){ // All is sent
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->state_write = DAP_HTTP_CLIENT_STATE_NONE;
dap_events_socket_set_writable_unsafe( a_esocket, false );
......@@ -601,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
* @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];
if ( cl_ht->reply_status_code == 200 ) {
if ( cl_ht->out_last_modified ) {
dap_time_to_str_rfc822( buf, sizeof(buf), cl_ht->out_last_modified );
dap_http_header_add( &cl_ht->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);
log_it(L_DEBUG,"output: Content-Type = '%s'",cl_ht->out_content_type);
}
if ( cl_ht->out_content_length ) {
dap_snprintf(buf,sizeof(buf),"%llu",(unsigned long long)cl_ht->out_content_length);
dap_http_header_add(&cl_ht->out_headers,"Content-Length",buf);
log_it(L_DEBUG,"output: Content-Length = %llu",cl_ht->out_content_length);
}
}
char buf[1024];
if ( a_http_client->reply_status_code == 200 ) {
if (s_debug_http)
log_it(L_DEBUG, "Out headers generate for sock %d", a_http_client->esocket->socket);
if ( a_http_client->out_last_modified ) {
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 ( a_http_client->out_content_type[0] ) {
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 ( a_http_client->out_content_length ) {
dap_snprintf(buf,sizeof(buf),"%llu",(unsigned long long)a_http_client->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 )
dap_http_header_add( &cl_ht->out_headers, "Connection","Close" );
if ( a_http_client->out_connection_close || !a_http_client->keep_alive )
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" );
}
/**
......
......@@ -31,10 +31,12 @@ typedef struct dap_http_cache
byte_t *body;
size_t body_size;
dap_http_header_t * headers;
char * response_phrase;
int response_code;
time_t ts_expire;
} dap_http_cache_t;
dap_http_cache_t * dap_http_cache_update(struct dap_http_url_proc * a_url_proc, byte_t * a_body, size_t a_body_size,
dap_http_header_t * a_headers, time_t ts_expire );
dap_http_cache_t * dap_http_cache_update(struct dap_http_url_proc * a_url_proc, const byte_t * a_body, size_t a_body_size,
dap_http_header_t * a_headers, const char * a_response_phrase, int a_respoonse_code,
time_t a_ts_expire );
void dap_http_cache_delete(dap_http_cache_t * a_http_cache);
......@@ -187,7 +187,7 @@ static bool s_stream_ch_delete_in_proc(dap_proc_thread_t * a_thread, void * a_ar
{
(void) a_thread;
dap_stream_ch_chain_t * l_ch_chain=(dap_stream_ch_chain_t*) a_arg;
dap_stream_ch_chain_hash_item_t * l_item, *l_tmp;
dap_stream_ch_chain_hash_item_t * l_item = NULL, *l_tmp = NULL;
// Clear remote atoms
HASH_ITER(hh, l_ch_chain->remote_atoms, l_item, l_tmp){
......@@ -307,7 +307,7 @@ static bool s_sync_out_chains_proc_callback(dap_proc_thread_t *a_thread, void *a
if (l_iter && l_first_size) {
// first packet
if (!dap_hash_fast_is_blank(&l_sync_request->request.hash_from)) {
l_iter = l_chain->callback_atom_find_by_hash(l_sync_request->chain.request_atom_iter,
(void ) l_chain->callback_atom_find_by_hash(l_sync_request->chain.request_atom_iter,
&l_sync_request->request.hash_from, &l_first_size);
}
......
......@@ -25,9 +25,12 @@
#pragma once
#include "dap_http.h"
#include "dap_enc_http.h"
#include "dap_config.h"
#define DAP_CHAIN_NET_SRV_VPN_CDB_GDB_PREFIX "srv.vpn"
extern dap_config_t * g_dap_config_cdb;
int dap_chain_net_srv_vpn_cdb_init(dap_http_t * a_http);
void dap_chain_net_srv_vpn_cdb_deinit();
void dap_chain_net_srv_vpn_cdb_auth_after(enc_http_delegate_t* a_delegate, const char * a_login, const char * a_pkey_b64 );