Files
domo-iot/lib/include/restd.h
NADAL Jean-Baptiste b9beca6d72
All checks were successful
continuous-integration/drone/push Build is passing
Add Error Handler.
2020-02-20 14:32:32 +01:00

143 lines
4.3 KiB
C

/*!
* restd.h
*
* Copyright (c) 2015-2020, 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: 07/02/2020
*
*/
#ifndef _RESTD_H
#define _RESTD_H
/*------------------------------- INCLUDES ----------------------------------*/
#include <pthread.h>
#include <event2/http.h>
#include <qlibc/qlibc.h>
/*------------------------------- TYPEDEFS ----------------------------------*/
typedef struct restd_server_s restd_server_t;
typedef struct restd_hook_s restd_hook_t;
typedef struct restd_resp_s restd_resp_t;
typedef int (*restd_callback)(restd_resp_t *response, void *arg);
typedef int (*restd__error_callback)(restd_resp_t *response, int reason, void *arg);
/*------------------------------- INCLUDES ----------------------------------*/
extern restd_server_t *restd_server_new(void);
extern void restd_server_free(restd_server_t *server);
extern int restd_server_start(restd_server_t *server);
extern int restd_server_attach_event_loop(restd_server_t *server, struct event_base *ev_base);
extern void restd_server_set_option(restd_server_t *server, const char *key, const char *value);
extern void restd_server_register_hook_on_path(restd_server_t *server, enum evhttp_cmd_type method, const char *path,
restd_callback cb, void *userdata);
extern void restd_server_register_error_handler(restd_server_t *server, restd__error_callback cb, void *userdata);
extern void restd_http_response(restd_resp_t *response, int code, const char *contenttype, const char *data);
extern char *restd_http_get_body(restd_resp_t *response);
extern restd_hook_t *restd_hook_new(void);
extern void restd_hook_free(restd_hook_t *hook);
extern restd_resp_t *restd_resp_new(void);
extern void restd_resp_free(restd_resp_t *response);
/*------------------------------- DEFINES ------------------------------------*/
/*
* These flags are used for ad_log_level();
*/
enum restd_log_e
{
RESTD_LOG_DISABLE = 0,
RESTD_LOG_ERROR,
RESTD_LOG_WARN,
RESTD_LOG_INFO,
RESTD_LOG_DEBUG,
RESTD_LOG_DEBUG2,
};
#define RESTD_OK (0) /*!< I'm done with this request. Escalate to other hooks. */
#define RESTD_FAILED (1) /*!< I'm done with this request. But the Process failed. */
#define RESTD_ERROR_METHOD_NOT_ALLOWED (2)
#define RESTD_ERROR_PATH_NOT_FOUND (3)
/*---------------------------------------------------------------------------*\
| DATA STRUCTURES |
\*---------------------------------------------------------------------------*/
/**
* Server info container.
*/
struct restd_server_s
{
int errcode; /*!< exit status. 0 for normal exit, non zero for error. */
pthread_t *thread; /*!< thread object. not null if server runs as a thread */
qhashtbl_t *options; /*!< server options */
qlist_t *hooks; /*!< list of registered hooks */
struct event_base *evbase; /*!< event base */
struct evhttp *evhttp;
struct bufferevent *notify_buffer; /*!< internal notification channel */
restd__error_callback error_handler;
void *error_userdata;
};
/*
* User callback hook container.
*/
struct restd_hook_s
{
enum evhttp_cmd_type method;
char *path;
restd_callback cb;
void *userdata;
qlist_t *path_fragments;
bool has_parameter;
int parameter_position;
char *parameter_name;
char *action_name;
};
/*
* Response callback parameter.
*/
struct restd_resp_s
{
struct evhttp_request *request;
bool has_parameter;
char *parameter_name;
uint32_t parameter_value;
char *action;
};
#endif /*_RESTD_H */