Fixes for http receiving

This commit is contained in:
John Jones 2017-09-25 12:07:16 -05:00
parent 5fc40e51ee
commit 88baee62a2
4 changed files with 16 additions and 15 deletions

View file

@ -572,9 +572,13 @@ void *api_connection_thread (void *ptr)
snprintf(resp, sizeof(resp), "%s 200 OK\r\n" \ snprintf(resp, sizeof(resp), "%s 200 OK\r\n" \
"Content-Type: application/json\r\n" "Content-Type: application/json\r\n"
"Server: c-ipfs/0.0.0-dev\r\n" "Server: c-ipfs/0.0.0-dev\r\n"
"X-Chunked-Output: 1\r\n" //"X-Chunked-Output: 1\r\n"
"Connection: close\r\n" "Connection: close\r\n"
"Transfer-Encoding: chunked\r\n\r\n%s", req.buf + req.http_ver, response_text); //"Transfer-Encoding: chunked\r\n"
"\r\n"
"%s\r\n"
//"0\r\n"
,req.buf + req.http_ver, response_text);
if (response_text != NULL) if (response_text != NULL)
free(response_text); free(response_text);
write_str (s, resp); write_str (s, resp);
@ -595,15 +599,12 @@ quit:
if (inet_ntop(AF_INET, &( params->this_node->api_context->conns[params->index]->ipv4), client, INET_ADDRSTRLEN) == NULL) if (inet_ntop(AF_INET, &( params->this_node->api_context->conns[params->index]->ipv4), client, INET_ADDRSTRLEN) == NULL)
strcpy(client, "UNKNOW"); strcpy(client, "UNKNOW");
libp2p_logger_debug("api", "Closing client connection %s:%d (%d).\n", client, params->this_node->api_context->conns[params->index]->port, params->index+1); libp2p_logger_debug("api", "Closing client connection %s:%d (%d).\n", client, params->this_node->api_context->conns[params->index]->port, params->index+1);
libp2p_logger_debug("api", "api_connection_thread: Attempting lock of mutex.\n");
pthread_mutex_lock(&params->this_node->api_context->conns_lock); pthread_mutex_lock(&params->this_node->api_context->conns_lock);
libp2p_logger_debug("api", "api_connection_thread: Lock successful.\n");
close(s); close(s);
free ( params->this_node->api_context->conns[params->index]); free ( params->this_node->api_context->conns[params->index]);
params->this_node->api_context->conns[params->index] = NULL; params->this_node->api_context->conns[params->index] = NULL;
params->this_node->api_context->conns_count--; params->this_node->api_context->conns_count--;
pthread_mutex_unlock(&params->this_node->api_context->conns_lock); pthread_mutex_unlock(&params->this_node->api_context->conns_lock);
libp2p_logger_debug("api", "api_connection_thread: Unlock successful.\n");
free(params); free(params);
return NULL; return NULL;
} }
@ -615,9 +616,7 @@ void api_connections_cleanup (struct IpfsNode* local_node)
{ {
int i; int i;
libp2p_logger_debug("api", "api_connections_cleanup: Attempting lock.\n");
pthread_mutex_lock(&local_node->api_context->conns_lock); pthread_mutex_lock(&local_node->api_context->conns_lock);
libp2p_logger_debug("api", "api_connections_cleanup: Lock successful.\n");
if (local_node->api_context->conns_count > 0 && local_node->api_context->conns) { if (local_node->api_context->conns_count > 0 && local_node->api_context->conns) {
for (i = 0 ; i < local_node->api_context->max_conns ; i++) { for (i = 0 ; i < local_node->api_context->max_conns ; i++) {
if (local_node->api_context->conns[i]->pthread) { if (local_node->api_context->conns[i]->pthread) {
@ -634,7 +633,6 @@ void api_connections_cleanup (struct IpfsNode* local_node)
local_node->api_context->conns = NULL; local_node->api_context->conns = NULL;
} }
pthread_mutex_unlock(&local_node->api_context->conns_lock); pthread_mutex_unlock(&local_node->api_context->conns_lock);
libp2p_logger_debug("api", "api_connections_cleanup: Unlock successful\n");
} }
/** /**
@ -664,15 +662,12 @@ void *api_listen_thread (void *ptr)
continue; continue;
} }
libp2p_logger_debug("api", "api_listen_thread: Lock\n");
pthread_mutex_lock(&local_node->api_context->conns_lock); pthread_mutex_lock(&local_node->api_context->conns_lock);
libp2p_logger_debug("api", "api_listen_thread: Lock Complete\n");
for (i = 0 ; i < local_node->api_context->max_conns && local_node->api_context->conns[i] ; i++); for (i = 0 ; i < local_node->api_context->max_conns && local_node->api_context->conns[i] ; i++);
local_node->api_context->conns[i] = malloc (sizeof (struct s_conns)); local_node->api_context->conns[i] = malloc (sizeof (struct s_conns));
if (!local_node->api_context->conns[i]) { if (!local_node->api_context->conns[i]) {
libp2p_logger_error("api", "Fail to allocate memory to accept connection.\n"); libp2p_logger_error("api", "Fail to allocate memory to accept connection.\n");
pthread_mutex_unlock(&local_node->api_context->conns_lock); pthread_mutex_unlock(&local_node->api_context->conns_lock);
libp2p_logger_debug("api", "api_listen_thread: memory failure, unlock successful.\n");
close (s); close (s);
continue; continue;
} }
@ -686,7 +681,6 @@ void *api_listen_thread (void *ptr)
if (connection_param == NULL) { if (connection_param == NULL) {
libp2p_logger_error("api", "api_listen_thread: Unable to allocate memory.\n"); libp2p_logger_error("api", "api_listen_thread: Unable to allocate memory.\n");
pthread_mutex_unlock(&local_node->api_context->conns_lock); pthread_mutex_unlock(&local_node->api_context->conns_lock);
libp2p_logger_debug("api", "api_listen_thread: memory failure 2 unlock successful.\n");
close (s); close (s);
continue; continue;
} }
@ -703,7 +697,6 @@ void *api_listen_thread (void *ptr)
} }
libp2p_logger_debug("api", "API for %s: Accept connection %s:%d (%d/%d), pthread %d.\n", local_node->identity->peer->id, client, port, local_node->api_context->conns_count, local_node->api_context->max_conns, i+1); libp2p_logger_debug("api", "API for %s: Accept connection %s:%d (%d/%d), pthread %d.\n", local_node->identity->peer->id, client, port, local_node->api_context->conns_count, local_node->api_context->max_conns, i+1);
pthread_mutex_unlock(&local_node->api_context->conns_lock); pthread_mutex_unlock(&local_node->api_context->conns_lock);
libp2p_logger_debug("api", "api_listen_thread:Api connection cleanup unlock successful.\n");
} }
api_connections_cleanup (local_node); api_connections_cleanup (local_node);
return NULL; return NULL;

View file

@ -251,6 +251,7 @@ size_t curl_cb(void* ptr, size_t size, size_t nmemb, struct curl_string* str) {
str->ptr[new_len] = '\0'; str->ptr[new_len] = '\0';
str->len = new_len; str->len = new_len;
} }
libp2p_logger_debug("http_request", "curl_cb received %s.\n", str->ptr);
return size + nmemb; return size + nmemb;
} }
@ -298,6 +299,8 @@ int ipfs_core_http_request_get(struct IpfsNode* local_node, struct HttpRequest*
curl_easy_cleanup(curl); curl_easy_cleanup(curl);
if (res == CURLE_OK) { if (res == CURLE_OK) {
*result = s.ptr; *result = s.ptr;
} else {
libp2p_logger_error("http_request", "Results of [%s] returned failure. Return value: %d.\n", url, res);
} }
return res == CURLE_OK; return res == CURLE_OK;
} }

View file

@ -21,8 +21,10 @@ int ipfs_name_publish(struct IpfsNode* local_node, char* name) {
request->sub_command = "publish"; request->sub_command = "publish";
libp2p_utils_vector_add(request->arguments, name); libp2p_utils_vector_add(request->arguments, name);
int retVal = ipfs_core_http_request_get(local_node, request, &response); int retVal = ipfs_core_http_request_get(local_node, request, &response);
if (response != NULL) if (response != NULL) {
fprintf(stdout, "%s", response);
free(response); free(response);
}
ipfs_core_http_request_free(request); ipfs_core_http_request_free(request);
return retVal; return retVal;
} }
@ -37,8 +39,10 @@ int ipfs_name_resolve(struct IpfsNode* local_node, char* name) {
request->sub_command = "resolve"; request->sub_command = "resolve";
libp2p_utils_vector_add(request->arguments, name); libp2p_utils_vector_add(request->arguments, name);
int retVal = ipfs_core_http_request_get(local_node, request, &response); int retVal = ipfs_core_http_request_get(local_node, request, &response);
if (response != NULL) if (response != NULL) {
fprintf(stdout, "%s", response);
free(response); free(response);
}
ipfs_core_http_request_free(request); ipfs_core_http_request_free(request);
return retVal; return retVal;
} }

View file

@ -333,6 +333,7 @@ int test_core_api_name_resolve_3()
libp2p_logger_add_class("api"); libp2p_logger_add_class("api");
libp2p_logger_add_class("test_api"); libp2p_logger_add_class("test_api");
libp2p_logger_add_class("http_request");
// publish name on server 1 // publish name on server 1
args = cli_arguments_new(6, publish_args); args = cli_arguments_new(6, publish_args);