Merge branch 'master' into yamux

# Conflicts:
#	core/http_request.c
This commit is contained in:
John Jones 2017-11-29 06:21:49 -05:00
commit 9bdfed92f1
6 changed files with 15 additions and 75 deletions

View file

@ -1,6 +1,7 @@
MIT License
Copyright (c) 2017 BitShares Munich IVS
Copyright (c) 2017 Agorise, IBC.
Contains works from BitShares Munich IVS
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View file

@ -462,9 +462,6 @@ void *api_connection_thread (void *ptr)
goto quit;
}
// once we leave the building of the req struct, do we need to do more? This flag will tell us.
int further_processing_necessary = 0;
if (strncmp(req.buf + req.method, "GET", 3)==0) {
if (strcmp (req.buf + req.path, "/")==0 ||
strcmp (req.buf + req.path, "/webui")==0 ||
@ -481,10 +478,7 @@ void *api_connection_thread (void *ptr)
} else {
write_cstr (s, HTTP_500);
}
} else if (cstrstart(req.buf + req.path, API_V0_START)) {
req.request = req.path + sizeof(API_V0_START) - 1;
further_processing_necessary = 1;
} else {
} else if (!cstrstart(req.buf + req.path, API_V0_START)) {
// TODO: handle download file here.
// move out of the if to do further processing
}
@ -540,10 +534,10 @@ void *api_connection_thread (void *ptr)
// Unexpected???
libp2p_logger_error("api", "fail unexpected '%s'.\n", req.buf + req.method);
write_cstr (s, HTTP_500);
further_processing_necessary = 0;
}
if (further_processing_necessary) {
if (cstrstart(req.buf + req.path, API_V0_START)) {
req.request = req.path + sizeof(API_V0_START) - 1;
// now do something with the request we have built
struct HttpRequest* http_request = api_build_http_request(&req);
if (http_request != NULL) {

View file

@ -164,7 +164,11 @@ int ipfs_core_http_process_object(struct IpfsNode* local_node, struct HttpReques
if (request->arguments->total == 1) {
char* hash = (char*)libp2p_utils_vector_get(request->arguments, 0);
struct Cid* cid = NULL;
ipfs_cid_decode_hash_from_base58((unsigned char*)hash, strlen(hash), &cid);
if (!ipfs_cid_decode_hash_from_base58((unsigned char*)hash, strlen(hash), &cid)) {
ipfs_cid_free(cid);
cid = NULL;
return 0;
}
*response = ipfs_core_http_response_new();
struct HttpResponse* res = *response;
res->content_type = "application/json";
@ -291,63 +295,6 @@ int ipfs_core_http_process_dht(struct IpfsNode* local_node, struct HttpRequest*
return retVal;
}
int ipfs_core_http_process_swarm_connect(struct IpfsNode* local_node, struct HttpRequest* request, struct HttpResponse** resp) {
// get the address
if (request->arguments == NULL || request->arguments->total < 0)
return 0;
const char* address = (char*) libp2p_utils_vector_get(request->arguments, 0);
if (address == NULL)
return 0;
// TODO: see if we are already connected, or at least already have this peer in our peerstore
// attempt to connect
struct MultiAddress* ma = multiaddress_new_from_string(address);
if (ma == NULL) {
libp2p_logger_error("http_request", "swarm_connect: Unable to convert %s to a MultiAddress.\n", address);
return 0;
}
struct Libp2pPeer* new_peer = libp2p_peer_new_from_multiaddress(ma);
if (!libp2p_peer_connect(local_node->dialer, new_peer, local_node->peerstore, local_node->repo->config->datastore, 30)) {
libp2p_logger_error("http_request", "swarm_connect: Unable to connect to peer %s.\n", libp2p_peer_id_to_string(new_peer));
libp2p_peer_free(new_peer);
multiaddress_free(ma);
return 0;
}
*resp = ipfs_core_http_response_new();
struct HttpResponse* response = *resp;
if (response == NULL) {
libp2p_logger_error("http_response", "swarm_connect: Unable to allocate memory for the response.\n");
libp2p_peer_free(new_peer);
multiaddress_free(ma);
return 0;
}
response->content_type = "application/json";
char* json = "{ \"Strings\": [ \"%s\"] }";
response->bytes_size = strlen(json) + strlen(address) + 1;
response->bytes = (uint8_t*) malloc(response->bytes_size);
if (response->bytes == NULL) {
response->bytes_size = 0;
response->content_type = NULL;
libp2p_peer_free(new_peer);
multiaddress_free(ma);
return 0;
}
sprintf((char*)response->bytes, json, address);
// getting rid of the peer here will close the connection. That's not what we want
//libp2p_peer_free(new_peer);
multiaddress_free(ma);
return 1;
}
int ipfs_core_http_process_swarm(struct IpfsNode* local_node, struct HttpRequest* request, struct HttpResponse** response) {
int retVal = 0;
if (strcmp(request->sub_command, "connect") == 0) {
// connect to a peer
retVal = ipfs_core_http_process_swarm_connect(local_node, request, response);
}
return retVal;
}
/***
* Process the parameters passed in from an http request
* @param local_node the context
@ -366,8 +313,6 @@ int ipfs_core_http_request_process(struct IpfsNode* local_node, struct HttpReque
retVal = ipfs_core_http_process_object(local_node, request, response);
} else if (strcmp(request->command, "dht") == 0) {
retVal = ipfs_core_http_process_dht(local_node, request, response);
} else if (strcmp(request->command, "swarm") == 0) {
retVal = ipfs_core_http_process_swarm(local_node, request, response);
}
return retVal;
}

View file

@ -295,7 +295,7 @@ int ipfs_exporter_object_cat(struct CliArguments* args, FILE* output_file) {
request->arguments = libp2p_utils_vector_new(1);
libp2p_utils_vector_add(request->arguments, hash);
size_t response_size = 0;
int retVal = ipfs_core_http_request_get(local_node, request, &response, &response_size);
int retVal = ipfs_core_http_request_post(local_node, request, &response, &response_size, "", 0);
if (response != NULL && response_size > 0) {
fwrite(response, 1, response_size, output_file);
free(response);

View file

@ -21,7 +21,7 @@ int ipfs_name_publish(struct IpfsNode* local_node, char* name) {
request->sub_command = "publish";
libp2p_utils_vector_add(request->arguments, name);
size_t response_size = 0;
int retVal = ipfs_core_http_request_get(local_node, request, &response, &response_size);
int retVal = ipfs_core_http_request_post(local_node, request, &response, &response_size, "", 0);
if (response != NULL && response_size > 0) {
fwrite(response, 1, response_size, stdout);
free(response);
@ -40,7 +40,7 @@ int ipfs_name_resolve(struct IpfsNode* local_node, char* name) {
request->sub_command = "resolve";
libp2p_utils_vector_add(request->arguments, name);
size_t response_size = 0;
int retVal = ipfs_core_http_request_get(local_node, request, &response, &response_size);
int retVal = ipfs_core_http_request_post(local_node, request, &response, &response_size, "", 0);
if (response != NULL && response_size > 0) {
fwrite(response, 1, response_size, stdout);
free(response);

View file

@ -69,7 +69,7 @@ int ipfs_routing_generic_get_value (ipfs_routing* routing, const unsigned char *
req->arguments = libp2p_utils_vector_new(1);
libp2p_utils_vector_add(req->arguments, buffer);
size_t response_size = 0;
if (!ipfs_core_http_request_get(routing->local_node, req, &response, &response_size)) {
if (!ipfs_core_http_request_post(routing->local_node, req, &response, &response_size, "", 0)) {
libp2p_logger_error("offline", "Unable to call API for dht get.\n");
ipfs_core_http_request_free(req);
return 0;
@ -181,7 +181,7 @@ int ipfs_routing_offline_provide (ipfs_routing* offlineRouting, const unsigned c
request->sub_command = "provide";
libp2p_utils_vector_add(request->arguments, buffer);
size_t response_size = 0;
if (!ipfs_core_http_request_get(offlineRouting->local_node, request, &response, &response_size)) {
if (!ipfs_core_http_request_post(offlineRouting->local_node, request, &response, &response_size, "", 0)) {
libp2p_logger_error("offline", "Unable to call API for dht publish.\n");
ipfs_core_http_request_free(request);
return 0;