WIP to test with params
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
NADAL Jean-Baptiste
2020-02-19 11:18:07 +01:00
parent fb82198417
commit 6db405df1f
2 changed files with 187 additions and 186 deletions

View File

@@ -26,6 +26,7 @@
/*------------------------------- INCLUDES ----------------------------------*/
#include <string.h>
#include <fcntl.h>
#include <stdbool.h>
#include <arpa/inet.h>
@@ -37,6 +38,7 @@
#include <event2/event.h>
#include <event2/http.h>
#include <event2/listener.h>
#include <event2/keyvalq_struct.h>
#ifdef __linux__
#include <sys/eventfd.h>
@@ -174,6 +176,8 @@ static void libevent_log_cb(int severity, const char *msg);
static int notify_loopexit(restd_server_t *server);
static void notify_cb(struct bufferevent *buffer, void *userdata);
void rest_request_cb(struct evhttp_request *req, void *arg);
void print_request_info(struct evhttp_request *req);
void restd_http_response_from_file(struct evhttp_request *req, int code, int fd, const char *content_type);
/*--------------------------- PUBLIC FUNCTIONS -------------------------------*/
@@ -459,12 +463,12 @@ void restd_http_response(struct evhttp_request *req, int code, const char *conte
if (data != NULL)
{
evbuffer_add_printf(resp_buf, "%s", data);
evbuffer_add(resp_buf, data, strlen(data));
}
evhttp_add_header(resp_headers, "Content-Type", contenttype);
evhttp_send_reply(req, code, NULL ,resp_buf);
evhttp_send_reply(req, code, NULL, resp_buf);
}
/*--------------------------------------------------------------------------*/
@@ -474,10 +478,10 @@ char *restd_http_get_body(struct evhttp_request *req)
char *body = NULL;
struct evbuffer *buf;
buf = evhttp_request_get_input_buffer(req);
buf = evhttp_request_get_input_buffer(req);
size_t len = evbuffer_get_length(buf);
body = malloc(len +1 );
body = malloc(len + 1);
int ret = evbuffer_copyout(buf, body, len);
body[len] = '\0';
@@ -670,10 +674,102 @@ static bool contain(const char *src, const char *dest, int len)
void rest_request_cb(struct evhttp_request *req, void *arg)
{
const char *cmdtype;
restd_server_t *server = (restd_server_t *)arg;
char *root_path;
#if 0
print_request_info(req);
#endif
const char *request_path = evhttp_request_get_uri(req);
qlist_t *hooks = server->hooks;
//int reason = RESTD_ERROR_PATH_NOT_FOUND;
qlist_obj_t obj;
bzero((void *)&obj, sizeof(qlist_obj_t));
while (hooks->getnext(hooks, &obj, false) == true)
{
restd_hook_t *hook = (restd_hook_t *)obj.data;
if (hook->cb)
{
//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");
//reason = RESTD_ERROR_METHOD_NOT_ALLOWED;
continue;
}
if ((hook->path != NULL) && (request_path != NULL))
{
int i = 0;
int pos = -1;
while (hook->path[i])
{
if (hook->path[i] == ':')
pos = i;
i++;
}
if (pos != -1 && contain(hook->path, request_path, pos))
{
const char *buffer = &request_path[pos];
// printf("buffer: <%s>\n", buffer);
// TODO conn->id = atoi(buffer);
hook->cb(req, hook->userdata);
return;
}
else
{
int rett = strcmp(hook->path, request_path);
if (rett == 0)
{
hook->cb(req, hook->userdata);
return;
}
}
}
}
}
// No Hook Found check if it's a real file into document root.
root_path = restd_server_get_option(server, "server.root_path");
if ((root_path != NULL) && (strlen(root_path) != 0))
{
int fd;
char buf[1024] = "";
qstrcatf(buf, "%s%s", root_path, request_path);
fd = open(buf, 0);
if (fd != -1)
{
restd_http_response_from_file(req, 200, fd, file_mime_lookup(buf));
return;
}
else
{
// TODO 404
}
}
#if 0 // TODO
if (conn->server->error_handler != NULL)
{
return conn->server->error_handler(reason, conn, NULL);
}
else
#endif
{
evhttp_send_reply(req, 500, "Internal Error", NULL);
}
}
/*--------------------------------------------------------------------------*/
void print_request_info(struct evhttp_request *req)
{
const char *cmdtype;
struct evkeyvalq *headers;
struct evkeyval *header;
printf("request.\n");
switch (evhttp_request_get_command(req))
{
@@ -711,168 +807,31 @@ 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;
qlist_obj_t obj;
bzero((void *)&obj, sizeof(qlist_obj_t));
while (hooks->getnext(hooks, &obj, false) == true)
headers = evhttp_request_get_input_headers(req);
for (header = headers->tqh_first; header;
header = header->next.tqe_next)
{
restd_hook_t *hook = (restd_hook_t *)obj.data;
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");
if (hook->method != evhttp_request_get_command(req))
{
//printf("==== Hook found but method failed -> next.\n");
//reason = RESTD_ERROR_METHOD_NOT_ALLOWED;
continue;
}
if ((hook->path != NULL) && (request_path != NULL))
{
int i = 0;
int pos = -1;
while (hook->path[i])
{
if (hook->path[i] == ':')
pos = i;
i++;
}
if (pos != -1 && contain(hook->path, request_path, pos))
{
const char *buffer = &request_path[pos];
// printf("buffer: <%s>\n", buffer);
// TODO conn->id = atoi(buffer);
hook->cb(req, hook->userdata);
return;
}
else
{
int rett = strcmp(hook->path, request_path);
if (rett == 0)
{
hook->cb(req, hook->userdata);
return;
}
}
}
}
printf(" %s: %s\n", header->key, header->value);
}
evhttp_send_reply(req, 500, "Internal Error", NULL);
}
#if 0
int restd_rest_handler(short event, restd_conn_t *conn)
/*--------------------------------------------------------------------------*/
void restd_http_response_from_file(struct evhttp_request *req, int code, int fd, const char *content_type)
{
if (event & RESTD_EVENT_INIT)
{
DEBUG("==> HTTP INIT");
restd_http_t *http = http_new(conn->out);
if (http == NULL)
return RESTD_CLOSE;
restd_conn_set_extra(conn, http, http_free_cb);
return RESTD_OK;
}
else if (event & RESTD_EVENT_CLOSE)
{
DEBUG("==> HTTP CLOSE=%x (TIMEOUT=%d, SHUTDOWN=%d)",
event, event & RESTD_EVENT_TIMEOUT, event & RESTD_EVENT_SHUTDOWN);
return RESTD_OK;
}
else if ((event & RESTD_EVENT_READ) || (event & RESTD_EVENT_WRITE))
{
restd_http_t *http = (restd_http_t *)restd_conn_get_extra(conn);
int status = http_parser(http, conn->in);
if (conn->method == NULL && http->request.method != NULL)
{
restd_conn_set_method(conn, http->request.method);
}
DEBUG("==> HTTP READ || HTTP WRITE");
int reason = RESTD_ERROR_PATH_NOT_FOUND;
DEBUG("********restd_rest_handler: event 0x%x", event);
char *root_path;
qlist_t *hooks = conn->server->hooks;
struct evbuffer *resp_buf;
struct evkeyvalq *resp_headers;
ev_off_t len;
qlist_obj_t obj;
bzero((void *)&obj, sizeof(qlist_obj_t));
while (hooks->getnext(hooks, &obj, false) == true)
{
restd_hook_t *hook = (restd_hook_t *)obj.data;
if (hook->cb)
{
printf("==== call_hooks: method: %s - %s \n", hook->method, conn->method);
printf("==== call_hooks: path: %s - %s\n", hook->path, http->request.path);
printf("==== HOOK FOUND !!!!\n");
if ((hook->method == NULL) || (conn->method == NULL) || (strcmp(hook->method, conn->method) != 0))
{
printf("==== Hook found but method failed -> next.\n");
reason = RESTD_ERROR_METHOD_NOT_ALLOWED;
continue;
}
resp_buf = evhttp_request_get_output_buffer(req);
resp_headers = evhttp_request_get_output_headers(req);
if ((hook->path != NULL) && (http->request.path != NULL))
{
int i = 0;
int pos = -1;
while (hook->path[i])
{
if (hook->path[i] == ':')
pos = i;
i++;
}
if (pos != -1 && contain(hook->path, http->request.path, pos))
{
const char *buffer = &http->request.path[pos];
// printf("buffer: <%s>\n", buffer);
conn->id = atoi(buffer);
return hook->cb(event, conn, hook->userdata);
}
else
{
int rett = strcmp(hook->path, http->request.path);
if (rett == 0)
return hook->cb(event, conn, hook->userdata);
}
}
}
}
// No Hook Found check if it's a real file into document root.
root_path = restd_server_get_option(conn->server, "server.root_path");
if ((root_path != NULL) && (strlen(root_path) != 0))
{
FILE *file;
char buf[1024] = "";
qstrcatf(buf, "%s%s", root_path, http->request.path);
if ((file = fopen(buf, "r")))
{
long fsize;
char *file_buf;
fseek(file, 0, SEEK_END);
fsize = ftell(file);
fseek(file, 0, SEEK_SET);
file_buf = malloc(fsize + 1);
fread(file_buf, 1, fsize, file);
fclose(file);
file_buf[fsize] = 0;
len = lseek(fd, 0, SEEK_END);
printf("mime type:%s\n", file_mime_lookup(buf));
restd_http_response(conn, 200, file_mime_lookup(buf), file_buf, fsize);
return RESTD_CLOSE;
}
}
evbuffer_add_file(resp_buf, fd, 0, len);
if (conn->server->error_handler != NULL)
return conn->server->error_handler(reason, conn, NULL);
return RESTD_CLOSE;
}
evhttp_add_header(resp_headers, "Content-Type", content_type);
BUG_EXIT();
return RESTD_CLOSE;
evhttp_send_reply(req, code, NULL, resp_buf);
}
#endif