Calling the API from the client for "name resolve" and "name publish"

This commit is contained in:
jmjatlanta 2017-09-21 18:03:42 -05:00
parent c54cf989c0
commit 90262ef657
4 changed files with 99 additions and 8 deletions

View file

@ -1,5 +1,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <curl/curl.h>
#include "libp2p/utils/vector.h" #include "libp2p/utils/vector.h"
#include "libp2p/utils/logger.h" #include "libp2p/utils/logger.h"
#include "ipfs/core/http_request.h" #include "ipfs/core/http_request.h"
@ -196,6 +197,62 @@ int ipfs_core_http_request_add_commands(struct HttpRequest* request, char** url)
return 1; return 1;
} }
/***
* Add parameters and arguments to the URL
* @param request the request
* @param url the url to continue to build
*/
int ipfs_core_http_request_add_parameters(struct HttpRequest* request, char** url) {
// params
if (request->params != NULL) {
for (int i = 0; i < request->params->total; i++) {
struct HttpParam* curr_param = (struct HttpParam*) libp2p_utils_vector_get(request->params, i);
int len = strlen(curr_param->name) + strlen(curr_param->value) + 2;
char* str = (char*) malloc(strlen(*url) + len);
if (str == NULL) {
return 0;
}
sprintf(str, "%s/%s=%s", *url, curr_param->name, curr_param->value);
free(*url);
*url = str;
}
}
// args
if (request->arguments != NULL) {
int isFirst = 1;
for(int i = 0; i < request->arguments->total; i++) {
char* arg = (char*) libp2p_utils_vector_get(request->arguments, i);
int len = strlen(arg) + strlen(*url) + 6;
char* str = (char*) malloc(len);
if (str == NULL)
return 0;
if (isFirst)
sprintf(str, "%s?arg=%s", *url, arg);
else
sprintf(str, "%s&arg=%s", *url, arg);
free(*url);
*url = str;
}
}
return 1;
}
struct curl_string {
char* ptr;
size_t len;
};
size_t curl_cb(void* ptr, size_t size, size_t nmemb, struct curl_string* str) {
size_t new_len = str->len + size * nmemb;
str->ptr = realloc(str->ptr, new_len + 1);
if (str->ptr != NULL) {
memcpy(str->ptr + str->len, ptr, size*nmemb);
str->ptr[new_len] = '\0';
str->len = new_len;
}
return size + nmemb;
}
/** /**
* Do an HTTP Get to the local API * Do an HTTP Get to the local API
* @param local_node the context * @param local_node the context
@ -216,10 +273,31 @@ int ipfs_core_http_request_get(struct IpfsNode* local_node, struct HttpRequest*
return 0; return 0;
} }
int request_length = 10 + strlen(request->command); if (!ipfs_core_http_request_add_parameters(request, &url)) {
if (request->sub_command != NULL) free(url);
request_length += 1 + strlen(request->sub_command);
//TODO: continue building out this method
return 0; return 0;
}
// do the GET using libcurl
CURL *curl;
CURLcode res;
struct curl_string s;
s.len = 0;
s.ptr = malloc(1);
s.ptr[0] = '\0';
curl = curl_easy_init();
if (!curl) {
return 0;
}
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_cb);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if (res == CURLE_OK) {
*result = s.ptr;
}
return res == CURLE_OK;
} }

View file

@ -1,6 +1,6 @@
CC = gcc CC = gcc
CFLAGS = -O0 -I../include -I../../c-libp2p/include -I../../c-multihash/include -I../../c-multiaddr/include -I../../c-protobuf -g3 -Wall -std=c99 CFLAGS = -O0 -I../include -I../../c-libp2p/include -I../../c-multihash/include -I../../c-multiaddr/include -I../../c-protobuf -g3 -Wall -std=c99
LFLAGS = -L../../c-libp2p -L../../c-multihash -L../../c-multiaddr -lp2p -lm -lmultihash -lmultiaddr -lpthread -lresolv LFLAGS = -L../../c-libp2p -L../../c-multihash -L../../c-multiaddr -lp2p -lm -lmultihash -lmultiaddr -lpthread -lresolv -lcurl
DEPS = cmd/ipfs/test_init.h repo/test_repo_bootstrap_peers.h repo/test_repo_config.h repo/test_repo_identity.h cid/test_cid.h DEPS = cmd/ipfs/test_init.h repo/test_repo_bootstrap_peers.h repo/test_repo_config.h repo/test_repo_identity.h cid/test_cid.h
OBJS = main.o \ OBJS = main.o \
../blocks/block.o ../blocks/blockstore.o \ ../blocks/block.o ../blocks/blockstore.o \

View file

@ -3,6 +3,7 @@
*/ */
#include <stdlib.h> #include <stdlib.h>
#include "libp2p/utils/logger.h" #include "libp2p/utils/logger.h"
#include "libp2p/utils/vector.h"
#include "ipfs/core/ipfs_node.h" #include "ipfs/core/ipfs_node.h"
#include "ipfs/cmd/cli.h" #include "ipfs/cmd/cli.h"
#include "ipfs/core/http_request.h" #include "ipfs/core/http_request.h"
@ -18,6 +19,7 @@ int ipfs_name_publish(struct IpfsNode* local_node, char* name) {
return 0; return 0;
request->command = "name"; request->command = "name";
request->sub_command = "publish"; request->sub_command = "publish";
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)
free(response); free(response);
@ -27,7 +29,18 @@ int ipfs_name_publish(struct IpfsNode* local_node, char* name) {
int ipfs_name_resolve(struct IpfsNode* local_node, char* name) { int ipfs_name_resolve(struct IpfsNode* local_node, char* name) {
// ask api // ask api
char* response = NULL;
struct HttpRequest* request = ipfs_core_http_request_new();
if (request == NULL)
return 0; return 0;
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)
free(response);
ipfs_core_http_request_free(request);
return retVal;
} }
/** /**

View file

@ -1,6 +1,6 @@
CC = gcc CC = gcc
CFLAGS = -O0 -I../include -I../../c-libp2p/include -I../../c-multihash/include -I../../c-multiaddr/include -I../../c-protobuf -I../../lmdb/libraries/liblmdb -g3 -Wall -std=c99 CFLAGS = -O0 -I../include -I../../c-libp2p/include -I../../c-multihash/include -I../../c-multiaddr/include -I../../c-protobuf -I../../lmdb/libraries/liblmdb -g3 -Wall -std=c99
LFLAGS = -L../../c-libp2p -L../../c-multihash -L../../c-multiaddr -lp2p -lm -lmultihash -lmultiaddr -lpthread LFLAGS = -L../../c-libp2p -L../../c-multihash -L../../c-multiaddr -lp2p -lm -lmultihash -lmultiaddr -lpthread -lcurl
DEPS = cmd/ipfs/test_init.h repo/test_repo_bootstrap_peers.h repo/test_repo_config.h repo/test_repo_identity.h cid/test_cid.h DEPS = cmd/ipfs/test_init.h repo/test_repo_bootstrap_peers.h repo/test_repo_config.h repo/test_repo_identity.h cid/test_cid.h
OBJS = testit.o test_helper.o \ OBJS = testit.o test_helper.o \
../blocks/block.o ../blocks/blockstore.o \ ../blocks/block.o ../blocks/blockstore.o \