Put and Post are now functional
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
NADAL Jean-Baptiste
2020-02-19 10:19:59 +01:00
parent 7d4d941c57
commit fb82198417
3 changed files with 40 additions and 11 deletions

View File

@@ -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); 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 -------------------------------*/ /*--------------------------- LOCAL FUNCTIONS -------------------------------*/
@@ -657,6 +673,7 @@ void rest_request_cb(struct evhttp_request *req, void *arg)
const char *cmdtype; const char *cmdtype;
restd_server_t *server = (restd_server_t *)arg; restd_server_t *server = (restd_server_t *)arg;
#if 0
printf("request.\n"); printf("request.\n");
switch (evhttp_request_get_command(req)) 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", printf("Received a %s request for %s\nHeaders:\n",
cmdtype, evhttp_request_get_uri(req)); cmdtype, evhttp_request_get_uri(req));
#endif
qlist_t *hooks = server->hooks; qlist_t *hooks = server->hooks;
//int reason = RESTD_ERROR_PATH_NOT_FOUND; //int reason = RESTD_ERROR_PATH_NOT_FOUND;
@@ -705,12 +723,12 @@ void rest_request_cb(struct evhttp_request *req, void *arg)
if (hook->cb) if (hook->cb)
{ {
const char *request_path = evhttp_request_get_uri(req); 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: method: %d - %d \n", hook->method, evhttp_request_get_command(req));
printf("==== call_hooks: path: %s - %s\n", hook->path, request_path); //printf("==== call_hooks: path: %s - %s\n", hook->path, request_path);
printf("==== HOOK FOUND !!!!\n"); //printf("==== HOOK FOUND !!!!\n");
if (hook->method != evhttp_request_get_command(req)) 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; //reason = RESTD_ERROR_METHOD_NOT_ALLOWED;
continue; 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 #if 0
int restd_rest_handler(short event, restd_conn_t *conn) int restd_rest_handler(short event, restd_conn_t *conn)

View File

@@ -58,7 +58,7 @@ extern void restd_server_register_hook_on_path(restd_server_t *server, enum evht
restd_callback cb, void *userdata); restd_callback cb, void *userdata);
extern void restd_http_response(struct evhttp_request *req, int code, const char *contenttype, const char *data); 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 ------------------------------------*/ /*------------------------------- DEFINES ------------------------------------*/

View File

@@ -28,6 +28,7 @@
#define ksuccess_get_body "{\"status\":\"ok\"}" #define ksuccess_get_body "{\"status\":\"ok\"}"
#define ksuccess_delete_body "{\"status\":\"delete\"}" #define ksuccess_delete_body "{\"status\":\"delete\"}"
#define ksuccess_put_body "{\"status\":\"put\"}" #define ksuccess_put_body "{\"status\":\"put\"}"
#define ksuccess_post_body "{\"status\":\"post\"}"
#define kerror_body "{\"status\":\"error\"}" #define kerror_body "{\"status\":\"error\"}"
#define kserver_port "7777" #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) 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; 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) 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; 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_HTTPGET, 0);
curl_easy_setopt(curl_handle, CURLOPT_CUSTOMREQUEST, kput_method); 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) 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_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); 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.port", kserver_port);
restd_server_set_option(rest_server, "server.thread", "1"); 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); 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); 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); ret = exec_request(kput_method, "http://localhost:" kserver_port kapi_test_get, 200, ksuccess_put_body);
ASSERT_EQUAL_INT(ret, 0); 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); restd_server_free(rest_server);
} }