Adjustments to handle retrieval of binary files

This commit is contained in:
John Jones 2017-10-04 07:33:29 -05:00
parent 7259028bd0
commit b9c28ceed4
6 changed files with 26 additions and 16 deletions

View file

@ -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 local_node the context
* @param request the request * @param request the request
* @param result the results * @param result the results
* @param result_size the size of the results
* @returns true(1) on success, false(0) on error * @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) if (request == NULL || request->command == NULL)
return 0; return 0;
@ -451,8 +452,10 @@ int ipfs_core_http_request_get(struct IpfsNode* local_node, struct HttpRequest*
res = curl_easy_perform(curl); res = curl_easy_perform(curl);
curl_easy_cleanup(curl); curl_easy_cleanup(curl);
if (res == CURLE_OK) { 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 = s.ptr;
*result_size = s.len;
}
else else
res = -1; res = -1;
} else { } else {

View file

@ -290,9 +290,10 @@ int ipfs_exporter_object_cat(struct CliArguments* args) {
request->sub_command = "get"; request->sub_command = "get";
request->arguments = libp2p_utils_vector_new(1); request->arguments = libp2p_utils_vector_new(1);
libp2p_utils_vector_add(request->arguments, hash); libp2p_utils_vector_add(request->arguments, hash);
int retVal = ipfs_core_http_request_get(local_node, request, &response); size_t response_size = 0;
if (response != NULL && strlen(response) > 0) { int retVal = ipfs_core_http_request_get(local_node, request, &response, &response_size);
fprintf(stdout, "%s", response); if (response != NULL && response_size > 0) {
fprintf(stdout, response);
free(response); free(response);
} else { } else {
retVal = 0; retVal = 0;

View file

@ -71,9 +71,10 @@ int ipfs_core_http_request_process(struct IpfsNode* local_node, struct HttpReque
* @param local_node the context * @param local_node the context
* @param request the request * @param request the request
* @param result the results * @param result the results
* @param result_size the size of the results
* @returns true(1) on success, false(0) on error * @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 * Do an HTTP Post to the local API

View file

@ -20,9 +20,10 @@ int ipfs_name_publish(struct IpfsNode* local_node, char* name) {
request->command = "name"; request->command = "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); size_t response_size = 0;
if (response != NULL) { int retVal = ipfs_core_http_request_get(local_node, request, &response, &response_size);
fprintf(stdout, "%s", response); if (response != NULL && response_size > 0) {
fprintf(stdout, response);
free(response); free(response);
} }
ipfs_core_http_request_free(request); ipfs_core_http_request_free(request);
@ -38,9 +39,10 @@ int ipfs_name_resolve(struct IpfsNode* local_node, char* name) {
request->command = "name"; request->command = "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); size_t response_size = 0;
if (response != NULL) { int retVal = ipfs_core_http_request_get(local_node, request, &response, &response_size);
fprintf(stdout, "%s", response); if (response != NULL && response_size > 0) {
fprintf(stdout, response);
free(response); free(response);
} }
ipfs_core_http_request_free(request); ipfs_core_http_request_free(request);

View file

@ -68,13 +68,14 @@ int ipfs_routing_generic_get_value (ipfs_routing* routing, const unsigned char *
req->sub_command = "get"; req->sub_command = "get";
req->arguments = libp2p_utils_vector_new(1); req->arguments = libp2p_utils_vector_new(1);
libp2p_utils_vector_add(req->arguments, buffer); 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"); libp2p_logger_error("offline", "Unable to call API for dht get.\n");
ipfs_core_http_request_free(req); ipfs_core_http_request_free(req);
return 0; return 0;
} }
ipfs_core_http_request_free(req); ipfs_core_http_request_free(req);
*vlen = strlen(response); *vlen = response_size;
if (*vlen > 0) { if (*vlen > 0) {
*val = malloc(*vlen + 1); *val = malloc(*vlen + 1);
uint8_t* ptr = (uint8_t*)*val; 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->command = "dht";
request->sub_command = "provide"; request->sub_command = "provide";
libp2p_utils_vector_add(request->arguments, buffer); 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"); libp2p_logger_error("offline", "Unable to call API for dht publish.\n");
ipfs_core_http_request_free(request); ipfs_core_http_request_free(request);
return 0; return 0;
} }
ipfs_core_http_request_free(request); ipfs_core_http_request_free(request);
fprintf(stdout, "%s", response); fprintf(stdout, response);
return 1; return 1;
} else { } else {
libp2p_logger_debug("offline", "Unable to announce that I can provide the hash, as API not available.\n"); libp2p_logger_debug("offline", "Unable to announce that I can provide the hash, as API not available.\n");

View file

@ -178,6 +178,7 @@ int test_core_api_object_cat_binary() {
libp2p_logger_add_class("bitswap_engine"); libp2p_logger_add_class("bitswap_engine");
libp2p_logger_add_class("bitswap_network"); libp2p_logger_add_class("bitswap_network");
libp2p_logger_add_class("exporter"); libp2p_logger_add_class("exporter");
libp2p_logger_add_class("api");
// build repo // build repo
if (!drop_build_open_repo(ipfs_path1, &fs_repo, config_file1)) { if (!drop_build_open_repo(ipfs_path1, &fs_repo, config_file1)) {