Implementation of http_request

yamux
jmjatlanta 2017-09-21 15:27:16 -05:00
parent 9425e2fee3
commit a1b887ba76
6 changed files with 233 additions and 2 deletions

View File

@ -7,7 +7,7 @@ endif
LFLAGS =
DEPS = builder.h ipfs_node.h
OBJS = builder.o daemon.o null.o ping.o bootstrap.o ipfs_node.o api.o client_api.o
OBJS = builder.o daemon.o null.o ping.o bootstrap.o ipfs_node.o api.o client_api.o http_request.o
%.o: %.c $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS)

View File

@ -18,6 +18,7 @@
#include "libp2p/utils/logger.h"
#include "ipfs/core/api.h"
#include "ipfs/importer/exporter.h"
#include "ipfs/core/http_request.h"
pthread_mutex_t conns_lock;
int conns_count;
@ -419,6 +420,7 @@ void *api_connection_thread (void *ptr)
goto quit;
}
struct HttpRequest* http_request = ipfs_core_http_request_new();
if (strcmp(buf + req.method, "GET")==0) {
char *path;
unsigned char *obj;
@ -440,6 +442,7 @@ void *api_connection_thread (void *ptr)
write_cstr (s, HTTP_500);
}
} else {
//TODO: build http_request here
path = req.buf + req.path;
if (get_object(params->this_node, path, &obj, &size)) {
@ -454,6 +457,7 @@ void *api_connection_thread (void *ptr)
}
} else if (strcmp(buf + req.method, "POST")==0) {
// TODO: Handle gzip/json POST requests.
// TODO: build http_request here
p = header_value_cmp(&req, "Content-Type:", "multipart/form-data;");
if (p) {
@ -499,12 +503,18 @@ void *api_connection_thread (void *ptr)
req.buf+req.method, req.buf+req.path, req.buf+req.http_ver,
req.buf+req.header, req.body_size);
char* response_text = NULL;
if (!ipfs_core_http_request_process(http_request, &response_text)) {
libp2p_logger_error("api", "ipfs_core_http_request_process returned false.\n");
// TODO: Handle this condition
}
snprintf(resp, sizeof(resp), "%s 200 OK\r\n" \
"Content-Type: application/json\r\n"
"Server: c-ipfs/0.0.0-dev\r\n"
"X-Chunked-Output: 1\r\n"
"Connection: close\r\n"
"Transfer-Encoding: chunked\r\n\r\n", req.buf + req.http_ver);
"Transfer-Encoding: chunked\r\n\r\n%s", req.buf + req.http_ver, response_text);
write_str (s, resp);
libp2p_logger_error("api", "resp = {\n%s\n}\n", resp);
} else {

86
core/http_request.c Normal file
View File

@ -0,0 +1,86 @@
#include <stdlib.h>
#include "libp2p/utils/vector.h"
#include "ipfs/core/http_request.h"
/**
* Handles HttpRequest and HttpParam
*/
/***
* Build a new HttpRequest
* @returns the newly allocated HttpRequest struct
*/
struct HttpRequest* ipfs_core_http_request_new() {
struct HttpRequest* result = (struct HttpRequest*) malloc(sizeof(struct HttpRequest));
if (result != NULL) {
result->command = NULL;
result->sub_command = NULL;
result->params = libp2p_utils_vector_new(1);
result->arguments = libp2p_utils_vector_new(1);
}
return result;
}
/***
* Clean up resources of a HttpRequest struct
* @param request the struct to destroy
*/
void ipfs_core_http_request_free(struct HttpRequest* request) {
if (request != NULL) {
if (request->command != NULL)
free(request->command);
if (request->sub_command != NULL)
free(request->sub_command);
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);
ipfs_core_http_param_free(curr_param);
}
libp2p_utils_vector_free(request->params);
}
if (request->arguments != NULL) {
for(int i = 0; i < request->arguments->total; i++) {
free((char*)libp2p_utils_vector_get(request->arguments, i));
}
libp2p_utils_vector_free(request->arguments);
}
free(request);
}
}
/***
* Build a new HttpParam
* @returns a newly allocated HttpParam struct
*/
struct HttpParam* ipfs_core_http_param_new() {
struct HttpParam* param = (struct HttpParam*) malloc(sizeof(struct HttpParam));
if (param != NULL) {
param->name = NULL;
param->value = NULL;
}
return param;
}
/***
* Clean up resources allocated by a HttpParam struct
* @param param the struct to destroy
*/
void ipfs_core_http_param_free(struct HttpParam* param) {
if (param != NULL) {
if (param->name != NULL)
free(param->name);
if (param->value != NULL)
free(param->value);
free(param);
}
}
/***
* Process the parameters passed in from an http request
* @param request the request
* @param response the response
* @returns true(1) on success, false(0) otherwise.
*/
int ipfs_core_http_request_process(struct HttpRequest* request, char** response) {
return 0;
}

View File

@ -0,0 +1,51 @@
#pragma once
/***
* A name/value pair of http parameters
*/
struct HttpParam {
char* name; // the name of the parameter
char* value; // the value of the parameter
};
/**
* A struct to help with incoming http requests
*/
struct HttpRequest {
char* command; // the command
char* sub_command; // the sub command
struct Libp2pVector* params; // a collection of HttpParam structs
struct Libp2pVector* arguments; // a collection of chars that are arguments
};
/***
* Build a new HttpRequest
* @returns the newly allocated HttpRequest struct
*/
struct HttpRequest* ipfs_core_http_request_new();
/***
* Clean up resources of a HttpRequest struct
* @param request the struct to destroy
*/
void ipfs_core_http_request_free(struct HttpRequest* request);
/***
* Build a new HttpParam
* @returns a newly allocated HttpParam struct
*/
struct HttpParam* ipfs_core_http_param_new();
/***
* Clean up resources allocated by a HttpParam struct
* @param param the struct to destroy
*/
void ipfs_core_http_param_free(struct HttpParam* param);
/***
* Process the parameters passed in from an http request
* @param request the request
* @param response the response
* @returns true(1) on success, false(0) otherwise.
*/
int ipfs_core_http_request_process(struct HttpRequest* request, char** response);

View File

@ -5,6 +5,7 @@ DEPS = cmd/ipfs/test_init.h repo/test_repo_bootstrap_peers.h repo/test_repo_conf
OBJS = testit.o test_helper.o \
../blocks/block.o ../blocks/blockstore.o \
../cid/cid.o \
../cmd/cli.o \
../cmd/ipfs/init.o \
../commands/argument.o ../commands/command_option.o ../commands/command.o ../commands/cli/parse.o \
../core/*.o \
@ -18,6 +19,7 @@ OBJS = testit.o test_helper.o \
../namesys/pb.o \
../namesys/publisher.o \
../namesys/resolver.o \
../namesys/name.o \
../repo/init.o \
../repo/fsrepo/*.o \
../repo/config/*.o \

View File

@ -6,6 +6,7 @@
#include "ipfs/core/daemon.h"
#include "ipfs/importer/exporter.h"
#include "ipfs/importer/importer.h"
#include "ipfs/namesys/name.h"
int test_core_api_startup_shutdown() {
char* repo_path = "/tmp/ipfs_1/.ipfs";
@ -127,3 +128,84 @@ int test_core_api_object_cat() {
pthread_join(daemon_thread2, NULL);
return retVal;
}
int test_core_api_name_resolve() {
int retVal = 0;
pthread_t daemon_thread1;
pthread_t daemon_thread2;
int thread_started1 = 0;
int thread_started2 = 0;
char* ipfs_path1 = "/tmp/ipfs_1";
char* config_file1 = "config.test1";
char* ipfs_path2 = "/tmp/ipfs_2";
char* config_file2 = "config.test2";
struct FSRepo* fs_repo = NULL;
char hash[256] = "";
char peer_id1[256] = "";
char* publish_args[] = {"ipfs", "--config", ipfs_path1, "name", "publish", hash };
char* resolve_args[] = {"ipfs", "--config", ipfs_path2, "name", "resolve", peer_id1 };
struct CliArguments* args = NULL;
// build 2 repos... repo 1
if (!drop_build_open_repo(ipfs_path1, &fs_repo, config_file1)) {
ipfs_repo_fsrepo_free(fs_repo);
libp2p_logger_error("test_api", "Unable to drop and build repository at %s\n", ipfs_path1);
goto exit;
}
sprintf(peer_id1, "/ipns/%s", fs_repo->config->identity->peer->id);
ipfs_repo_fsrepo_free(fs_repo);
// build 2 repos... repo 2
if (!drop_build_open_repo(ipfs_path2, &fs_repo, config_file2)) {
ipfs_repo_fsrepo_free(fs_repo);
libp2p_logger_error("test_api", "Unable to drop and build repository at %s\n", ipfs_path2);
goto exit;
}
libp2p_logger_debug("test_api", "Changed the server id to %s.\n", fs_repo->config->identity->peer->id);
ipfs_repo_fsrepo_free(fs_repo);
// add a file to the first repo
uint8_t *bytes = (unsigned char*)"hello, world!\n";
char* filename = "test1.txt";
create_file(filename, bytes, strlen((char*)bytes));
struct HashtableNode* node;
size_t bytes_written;
struct IpfsNode *local_node = NULL;
pthread_t api_pth = 0;
ipfs_node_offline_new(ipfs_path1, &local_node);
ipfs_import_file(NULL, filename, &node, local_node, &bytes_written, 0);
memset(hash, 0, 256);
ipfs_cid_hash_to_base58(node->hash, node->hash_size, (unsigned char*)hash, 256);
ipfs_node_free(&api_pth, local_node);
ipfs_hashtable_node_free(node);
// publish name on server 1
args = cli_arguments_new(6, publish_args);
ipfs_name(args);
cli_arguments_free(args);
args = NULL;
libp2p_logger_debug("test_api", "*** Firing up daemons ***\n");
pthread_create(&daemon_thread1, NULL, test_daemon_start, (void*)ipfs_path1);
thread_started1 = 1;
pthread_create(&daemon_thread2, NULL, test_daemon_start, (void*)ipfs_path2);
thread_started2 = 1;
sleep(3);
// use a client of server2 to to ask for the "name resolve" on server 1
args = cli_arguments_new(6, resolve_args);
if (ipfs_name(args) == 0) {
goto exit;
}
retVal = 1;
exit:
cli_arguments_free(args);
ipfs_daemon_stop();
if (thread_started1)
pthread_join(daemon_thread1, NULL);
if (thread_started2)
pthread_join(daemon_thread2, NULL);
return retVal;
}