From fb82198417940706f1ba59fcd139654f4feaa914 Mon Sep 17 00:00:00 2001 From: NADAL Jean-Baptiste Date: Wed, 19 Feb 2020 10:19:59 +0100 Subject: [PATCH] Put and Post are now functional --- src/rest/restd.c | 28 +++++++++++++++++++++++----- src/rest/restd.h | 2 +- src/tests/test_rest.c | 21 ++++++++++++++++----- 3 files changed, 40 insertions(+), 11 deletions(-) diff --git a/src/rest/restd.c b/src/rest/restd.c index c09b92b..79b0605 100644 --- a/src/rest/restd.c +++ b/src/rest/restd.c @@ -467,6 +467,22 @@ void restd_http_response(struct evhttp_request *req, int code, const char *conte evhttp_send_reply(req, code, NULL ,resp_buf); } +/*--------------------------------------------------------------------------*/ + +char *restd_http_get_body(struct evhttp_request *req) +{ + char *body = NULL; + struct evbuffer *buf; + + buf = evhttp_request_get_input_buffer(req); + size_t len = evbuffer_get_length(buf); + + body = malloc(len +1 ); + int ret = evbuffer_copyout(buf, body, len); + body[len] = '\0'; + + return body; +} /*--------------------------- LOCAL FUNCTIONS -------------------------------*/ @@ -657,6 +673,7 @@ void rest_request_cb(struct evhttp_request *req, void *arg) const char *cmdtype; restd_server_t *server = (restd_server_t *)arg; +#if 0 printf("request.\n"); switch (evhttp_request_get_command(req)) { @@ -694,6 +711,7 @@ void rest_request_cb(struct evhttp_request *req, void *arg) printf("Received a %s request for %s\nHeaders:\n", cmdtype, evhttp_request_get_uri(req)); +#endif qlist_t *hooks = server->hooks; //int reason = RESTD_ERROR_PATH_NOT_FOUND; @@ -705,12 +723,12 @@ void rest_request_cb(struct evhttp_request *req, void *arg) if (hook->cb) { const char *request_path = evhttp_request_get_uri(req); - printf("==== call_hooks: method: %d - %d \n", hook->method, evhttp_request_get_command(req)); - printf("==== call_hooks: path: %s - %s\n", hook->path, request_path); - printf("==== HOOK FOUND !!!!\n"); + //printf("==== call_hooks: method: %d - %d \n", hook->method, evhttp_request_get_command(req)); + //printf("==== call_hooks: path: %s - %s\n", hook->path, request_path); + //printf("==== HOOK FOUND !!!!\n"); if (hook->method != evhttp_request_get_command(req)) { - printf("==== Hook found but method failed -> next.\n"); + //printf("==== Hook found but method failed -> next.\n"); //reason = RESTD_ERROR_METHOD_NOT_ALLOWED; continue; } @@ -747,7 +765,7 @@ void rest_request_cb(struct evhttp_request *req, void *arg) } } - evhttp_send_reply(req, 200, "OK", NULL); + evhttp_send_reply(req, 500, "Internal Error", NULL); } #if 0 int restd_rest_handler(short event, restd_conn_t *conn) diff --git a/src/rest/restd.h b/src/rest/restd.h index e2228eb..d6e41c6 100644 --- a/src/rest/restd.h +++ b/src/rest/restd.h @@ -58,7 +58,7 @@ extern void restd_server_register_hook_on_path(restd_server_t *server, enum evht restd_callback cb, void *userdata); extern void restd_http_response(struct evhttp_request *req, int code, const char *contenttype, const char *data); - +extern char *restd_http_get_body(struct evhttp_request *req); /*------------------------------- DEFINES ------------------------------------*/ diff --git a/src/tests/test_rest.c b/src/tests/test_rest.c index 8076b75..4ea4f1e 100644 --- a/src/tests/test_rest.c +++ b/src/tests/test_rest.c @@ -28,6 +28,7 @@ #define ksuccess_get_body "{\"status\":\"ok\"}" #define ksuccess_delete_body "{\"status\":\"delete\"}" #define ksuccess_put_body "{\"status\":\"put\"}" +#define ksuccess_post_body "{\"status\":\"post\"}" #define kerror_body "{\"status\":\"error\"}" #define kserver_port "7777" @@ -74,7 +75,10 @@ int my_success_rest_delete_handler(struct evhttp_request *req, void *arg) int my_success_rest_put_handler(struct evhttp_request *req, void *arg) { - restd_http_response(req, 200, "application/json", ksuccess_put_body); + char *body; + body = restd_http_get_body(req); + restd_http_response(req, 200, "application/json", body); + free(body); return RESTD_OK; } @@ -82,7 +86,10 @@ int my_success_rest_put_handler(struct evhttp_request *req, void *arg) int my_success_rest_post_handler(struct evhttp_request *req, void *arg) { - restd_http_response(req, 200, "application/json", ksuccess_put_body); + char *body; + body = restd_http_get_body(req); + restd_http_response(req, 200, "application/json", body); + free(body); return RESTD_OK; } @@ -148,11 +155,14 @@ int exec_request(const char *request, const char *path, int expected_code, const { curl_easy_setopt(curl_handle, CURLOPT_HTTPGET, 0); curl_easy_setopt(curl_handle, CURLOPT_CUSTOMREQUEST, kput_method); + curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDSIZE, strlen(expected_body)); + curl_easy_setopt(curl_handle, CURLOPT_COPYPOSTFIELDS, expected_body); } else if (strcmp(request, kpost_method) == 0) { - printf("method POST\n"); curl_easy_setopt(curl_handle, CURLOPT_POST, 0); + curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDSIZE, strlen(expected_body)); + curl_easy_setopt(curl_handle, CURLOPT_COPYPOSTFIELDS, expected_body); } curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT, 2); @@ -392,7 +402,6 @@ TEST("Rest - create start access http hook free\t") restd_server_set_option(rest_server, "server.port", kserver_port); restd_server_set_option(rest_server, "server.thread", "1"); - // restd_server_register_hook(rest_server, restd_http_handler, NULL); restd_server_register_hook_on_path(rest_server, EVHTTP_REQ_GET, kapi_test_get, my_success_http_handler, NULL); ret = restd_server_start(rest_server); @@ -444,7 +453,9 @@ TEST("Rest - create start access rest hook free\t") ret = exec_request(kput_method, "http://localhost:" kserver_port kapi_test_get, 200, ksuccess_put_body); ASSERT_EQUAL_INT(ret, 0); - // TODO POST + PRINTLN("\n - POST"); + ret = exec_request(kpost_method, "http://localhost:" kserver_port kapi_test_get, 200, ksuccess_post_body); + ASSERT_EQUAL_INT(ret, 0); restd_server_free(rest_server); }