wip
This commit is contained in:
@@ -29,18 +29,13 @@
|
||||
/*------------------------------- INCLUDES ----------------------------------*/
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <strings.h>
|
||||
#include <unistd.h>
|
||||
#include <limits.h>
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <event2/buffer.h>
|
||||
#include <qlibc/qlibc.h>
|
||||
|
||||
#include "restd_server.h"
|
||||
|
||||
#include "restd_http_handler.h"
|
||||
|
||||
#include "macro.h"
|
||||
@@ -100,6 +95,11 @@ int restd_http_handler(short event, restd_conn_t *conn, void *userdata)
|
||||
{
|
||||
restd_conn_set_method(conn, http->request.method);
|
||||
}
|
||||
DEBUG("==> HTTP PATH: %s METHOD: %s", http->request.path, http->request.method);
|
||||
if (conn->server->request_handler != NULL)
|
||||
{
|
||||
status = conn->server->request_handler(event, conn, NULL);
|
||||
}
|
||||
return status;
|
||||
}
|
||||
else if (event & RESTD_EVENT_WRITE)
|
||||
@@ -251,7 +251,7 @@ int restd_http_is_keepalive_request(restd_conn_t *conn)
|
||||
* @return 0 on success, -1 if we already sent it out.
|
||||
*/
|
||||
int restd_http_set_response_header(restd_conn_t *conn, const char *name,
|
||||
const char *value)
|
||||
const char *value)
|
||||
{
|
||||
restd_http_t *http = (restd_http_t *)restd_conn_get_extra(conn);
|
||||
if (http->response.frozen_header)
|
||||
@@ -309,7 +309,7 @@ int restd_http_set_response_code(restd_conn_t *conn, int code, const char *reaso
|
||||
* @return 0 on success, -1 if we already sent it out.
|
||||
*/
|
||||
int restd_http_set_response_content(restd_conn_t *conn, const char *contenttype,
|
||||
off_t size)
|
||||
off_t size)
|
||||
{
|
||||
restd_http_t *http = (restd_http_t *)restd_conn_get_extra(conn);
|
||||
if (http->response.frozen_header)
|
||||
@@ -341,7 +341,7 @@ int restd_http_set_response_content(restd_conn_t *conn, const char *contenttype,
|
||||
* @return total bytes sent, 0 on error.
|
||||
*/
|
||||
size_t restd_http_response(restd_conn_t *conn, int code, const char *contenttype,
|
||||
const void *data, off_t size)
|
||||
const void *data, off_t size)
|
||||
{
|
||||
restd_http_t *http = (restd_http_t *)restd_conn_get_extra(conn);
|
||||
if (http->response.frozen_header)
|
||||
@@ -544,10 +544,9 @@ static restd_http_t *http_new(struct evbuffer *out)
|
||||
|
||||
// Allocate additional resources.
|
||||
http->request.inbuf = evbuffer_new();
|
||||
http->request.headers = qlisttbl(
|
||||
QLISTTBL_UNIQUE | QLISTTBL_CASEINSENSITIVE);
|
||||
http->response.headers = qlisttbl(
|
||||
QLISTTBL_UNIQUE | QLISTTBL_CASEINSENSITIVE);
|
||||
http->request.headers = qlisttbl(QLISTTBL_UNIQUE | QLISTTBL_CASEINSENSITIVE);
|
||||
http->response.headers = qlisttbl(QLISTTBL_UNIQUE | QLISTTBL_CASEINSENSITIVE);
|
||||
|
||||
if (http->request.inbuf == NULL || http->request.headers == NULL || http->response.headers == NULL)
|
||||
{
|
||||
http_free(http);
|
||||
|
||||
93
lib/src/restd_rest_handler.c
Normal file
93
lib/src/restd_rest_handler.c
Normal file
@@ -0,0 +1,93 @@
|
||||
/*!
|
||||
* restd_rest_handler.c
|
||||
*
|
||||
* Copyright (c) 2015-2019, NADAL Jean-Baptiste. All rights reserved.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301 USA
|
||||
*
|
||||
* @Author: NADAL Jean-Baptiste
|
||||
* @Date: 23/12/2019
|
||||
*
|
||||
*/
|
||||
|
||||
// This is an independent project of an individual developer. Dear PVS-Studio, please check it.
|
||||
// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: http://www.viva64.com
|
||||
|
||||
/*------------------------------- INCLUDES ----------------------------------*/
|
||||
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <event2/buffer.h>
|
||||
|
||||
#include "restd_server.h"
|
||||
#include "restd_http_handler.h"
|
||||
#include "restd_rest_handler.h"
|
||||
|
||||
#include "macro.h"
|
||||
|
||||
/*! ----------------------------------------------------------------------------
|
||||
* @fn restd_rest_handler
|
||||
*
|
||||
* @brief Main function to manage http request.
|
||||
*/
|
||||
int restd_rest_handler(short event, restd_conn_t *conn, void *userdata)
|
||||
{
|
||||
bool found = false;
|
||||
DEBUG("JBN call_hooks: event 0x%x", event);
|
||||
restd_http_t *http = (restd_http_t *)restd_conn_get_extra(conn);
|
||||
qlist_t *hooks = conn->server->hooks;
|
||||
|
||||
qlist_obj_t obj;
|
||||
bzero((void *)&obj, sizeof(qlist_obj_t));
|
||||
while (hooks->getnext(hooks, &obj, false) == true)
|
||||
{
|
||||
DEBUG(">>>>>>>>>\n");
|
||||
restd_hook_t *hook = (restd_hook_t *)obj.data;
|
||||
if (hook->cb)
|
||||
{
|
||||
DEBUG("JBN call_hooks: method: %s - %s \n", hook->method, conn->method);
|
||||
DEBUG("JBN call_hooks: path: %s - %s\n", hook->path, http->request.path);
|
||||
if ((hook->method == NULL) || (conn->method == NULL) || (strcmp(hook->method, conn->method) != 0))
|
||||
{
|
||||
DEBUG("JBN - ca va pas 1 \n");
|
||||
continue;
|
||||
}
|
||||
if ((hook->path == NULL) || (http->request.path == NULL) || (strcmp(hook->path, http->request.path) != 0))
|
||||
{
|
||||
DEBUG("JBN - ca va pas 2 \n");
|
||||
continue;
|
||||
}
|
||||
DEBUG("JBN FOUND !!!!\n");
|
||||
|
||||
return RESTD_OK;
|
||||
//int status = hook->cb(event, conn, hook->userdata);
|
||||
//if (status != RESTD_OK)
|
||||
//{
|
||||
// return status;
|
||||
//}
|
||||
}
|
||||
}
|
||||
|
||||
if (found == false)
|
||||
{
|
||||
printf("Call error handler...\n");
|
||||
return RESTD_NOT_ALLOWED;
|
||||
}
|
||||
|
||||
DEBUG("JBN call_hooks - DONE\n");
|
||||
return RESTD_OK;
|
||||
}
|
||||
@@ -57,16 +57,6 @@ static bool initialized = false;
|
||||
*/
|
||||
int _restd_log_level = RESTD_LOG_WARN;
|
||||
|
||||
/*
|
||||
* User callback hook container.
|
||||
*/
|
||||
typedef struct restd_hook_s restd_hook_t;
|
||||
struct restd_hook_s
|
||||
{
|
||||
char *method;
|
||||
restd_callback cb;
|
||||
void *userdata;
|
||||
};
|
||||
|
||||
/*
|
||||
* Local functions.
|
||||
@@ -686,6 +676,20 @@ static void conn_cb(restd_conn_t *conn, int event)
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
void restd_server_register_request_handler(restd_server_t *server, restd_callback cb)
|
||||
{
|
||||
server->request_handler = cb;
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
void restd_server_register_error_handler(restd_server_t *server, restd_callback cb)
|
||||
{
|
||||
server->error_handler = cb;
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
/**
|
||||
* Register user hook.
|
||||
*/
|
||||
@@ -700,10 +704,18 @@ void restd_server_register_hook(restd_server_t *server, restd_callback cb, void
|
||||
* Register user hook on method name.
|
||||
*/
|
||||
void restd_server_register_hook_on_method(restd_server_t *server, const char *method, restd_callback cb, void *userdata)
|
||||
{
|
||||
restd_server_register_hook_on_path(server, method, NULL, cb, userdata);
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
void restd_server_register_hook_on_path(restd_server_t *server, const char *method, const char *path, restd_callback cb, void *userdata)
|
||||
{
|
||||
restd_hook_t hook;
|
||||
bzero((void *)&hook, sizeof(restd_hook_t));
|
||||
hook.method = (method) ? strdup(method) : NULL;
|
||||
hook.path = (path) ? strdup(path) : NULL;
|
||||
hook.cb = cb;
|
||||
hook.userdata = userdata;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user