diff --git a/core/http_request.c b/core/http_request.c index 61944c8..e5229d9 100644 --- a/core/http_request.c +++ b/core/http_request.c @@ -413,9 +413,10 @@ size_t curl_cb(void* ptr, size_t size, size_t nmemb, struct curl_string* str) { * @param local_node the context * @param request the request * @param result the results + * @param result_size the size of the results * @returns true(1) on success, false(0) on error */ -int ipfs_core_http_request_get(struct IpfsNode* local_node, struct HttpRequest* request, char** result) { +int ipfs_core_http_request_get(struct IpfsNode* local_node, struct HttpRequest* request, char** result, size_t *result_size) { if (request == NULL || request->command == NULL) return 0; @@ -451,8 +452,10 @@ int ipfs_core_http_request_get(struct IpfsNode* local_node, struct HttpRequest* res = curl_easy_perform(curl); curl_easy_cleanup(curl); if (res == CURLE_OK) { - if (strcmp(s.ptr, "404 page not found") != 0) + if (strcmp(s.ptr, "404 page not found") != 0) { *result = s.ptr; + *result_size = s.len; + } else res = -1; } else { diff --git a/importer/exporter.c b/importer/exporter.c index b1f9892..245aa11 100644 --- a/importer/exporter.c +++ b/importer/exporter.c @@ -290,9 +290,10 @@ int ipfs_exporter_object_cat(struct CliArguments* args) { request->sub_command = "get"; request->arguments = libp2p_utils_vector_new(1); libp2p_utils_vector_add(request->arguments, hash); - int retVal = ipfs_core_http_request_get(local_node, request, &response); - if (response != NULL && strlen(response) > 0) { - fprintf(stdout, "%s", response); + size_t response_size = 0; + int retVal = ipfs_core_http_request_get(local_node, request, &response, &response_size); + if (response != NULL && response_size > 0) { + fprintf(stdout, response); free(response); } else { retVal = 0; diff --git a/include/ipfs/core/http_request.h b/include/ipfs/core/http_request.h index 5534f28..6838b27 100644 --- a/include/ipfs/core/http_request.h +++ b/include/ipfs/core/http_request.h @@ -71,9 +71,10 @@ int ipfs_core_http_request_process(struct IpfsNode* local_node, struct HttpReque * @param local_node the context * @param request the request * @param result the results + * @param result_size the size of the results * @returns true(1) on success, false(0) on error */ -int ipfs_core_http_request_get(struct IpfsNode* local_node, struct HttpRequest* request, char** result); +int ipfs_core_http_request_get(struct IpfsNode* local_node, struct HttpRequest* request, char** result, size_t* result_size); /** * Do an HTTP Post to the local API diff --git a/namesys/name.c b/namesys/name.c index e5d450f..c79bb17 100644 --- a/namesys/name.c +++ b/namesys/name.c @@ -20,9 +20,10 @@ int ipfs_name_publish(struct IpfsNode* local_node, char* name) { request->command = "name"; request->sub_command = "publish"; libp2p_utils_vector_add(request->arguments, name); - int retVal = ipfs_core_http_request_get(local_node, request, &response); - if (response != NULL) { - fprintf(stdout, "%s", response); + size_t response_size = 0; + int retVal = ipfs_core_http_request_get(local_node, request, &response, &response_size); + if (response != NULL && response_size > 0) { + fprintf(stdout, response); free(response); } ipfs_core_http_request_free(request); @@ -38,9 +39,10 @@ int ipfs_name_resolve(struct IpfsNode* local_node, char* name) { request->command = "name"; request->sub_command = "resolve"; libp2p_utils_vector_add(request->arguments, name); - int retVal = ipfs_core_http_request_get(local_node, request, &response); - if (response != NULL) { - fprintf(stdout, "%s", response); + size_t response_size = 0; + int retVal = ipfs_core_http_request_get(local_node, request, &response, &response_size); + if (response != NULL && response_size > 0) { + fprintf(stdout, response); free(response); } ipfs_core_http_request_free(request); diff --git a/routing/offline.c b/routing/offline.c index c8d0028..023ae2d 100644 --- a/routing/offline.c +++ b/routing/offline.c @@ -68,13 +68,14 @@ int ipfs_routing_generic_get_value (ipfs_routing* routing, const unsigned char * req->sub_command = "get"; req->arguments = libp2p_utils_vector_new(1); libp2p_utils_vector_add(req->arguments, buffer); - if (!ipfs_core_http_request_get(routing->local_node, req, &response)) { + size_t response_size = 0; + if (!ipfs_core_http_request_get(routing->local_node, req, &response, &response_size)) { libp2p_logger_error("offline", "Unable to call API for dht get.\n"); ipfs_core_http_request_free(req); return 0; } ipfs_core_http_request_free(req); - *vlen = strlen(response); + *vlen = response_size; if (*vlen > 0) { *val = malloc(*vlen + 1); uint8_t* ptr = (uint8_t*)*val; @@ -145,13 +146,14 @@ int ipfs_routing_offline_provide (ipfs_routing* offlineRouting, const unsigned c request->command = "dht"; request->sub_command = "provide"; libp2p_utils_vector_add(request->arguments, buffer); - if (!ipfs_core_http_request_get(offlineRouting->local_node, request, &response)) { + size_t response_size = 0; + if (!ipfs_core_http_request_get(offlineRouting->local_node, request, &response, &response_size)) { libp2p_logger_error("offline", "Unable to call API for dht publish.\n"); ipfs_core_http_request_free(request); return 0; } ipfs_core_http_request_free(request); - fprintf(stdout, "%s", response); + fprintf(stdout, response); return 1; } else { libp2p_logger_debug("offline", "Unable to announce that I can provide the hash, as API not available.\n"); diff --git a/test/core/test_api.h b/test/core/test_api.h index 8135d5d..a960840 100644 --- a/test/core/test_api.h +++ b/test/core/test_api.h @@ -178,6 +178,7 @@ int test_core_api_object_cat_binary() { libp2p_logger_add_class("bitswap_engine"); libp2p_logger_add_class("bitswap_network"); libp2p_logger_add_class("exporter"); + libp2p_logger_add_class("api"); // build repo if (!drop_build_open_repo(ipfs_path1, &fs_repo, config_file1)) {