feat: rework on plugin support
All checks were successful
gitea-sync/ant-http/pipeline/head This commit looks good

- New plugin interface that supports multiple instances
- Fix and improve memory bugs
- Refactory and cleanup lib
- Improve scheduler
This commit is contained in:
DanyLE
2024-03-13 18:07:07 +01:00
parent 3bedc3ffb5
commit 58a7738afe
19 changed files with 389 additions and 315 deletions

View File

@ -152,6 +152,7 @@ void free_association(chain_t *asoc)
if (a->key)
{
//printf("Free key %s\n", a->key);
free(a->key);
if (a->value)
free(a->value);

View File

@ -100,11 +100,31 @@ int require_plugin(const char *name)
return 0;
}
#ifdef USE_ZLIB
static list_t g_gzip_types = NULL;
void set_gzip_types(list_t list)
{
g_gzip_types = list;
}
int compressable(char *ctype)
{
UNUSED(ctype);
if (g_gzip_types == NULL)
return 0;
item_t it;
list_for_each(it, g_gzip_types)
{
if(it->type == LIST_TYPE_POINTER && it->value.ptr)
{
//LOG("Checking content type %s against GZIP support %s", ctype,(const char *)it->value.ptr);
if (regex_match((const char *)it->value.ptr, ctype, 0, NULL))
{
return 1;
}
}
}
return 0;
}
#endif
const char *get_status_str(int stat)
{

View File

@ -5,6 +5,7 @@
#include "list.h"
#include "dictionary.h"
#include "plugin.h"
#define SERVER_NAME "Antd"
#define IS_POST(method) (strcmp(method, "POST") == 0)
@ -28,9 +29,6 @@
#define ANTD_CLIENT_RQ_DATA_DECODE 0x7
#define ANTD_CLIENT_PROXY_MONITOR 0x8
#define ANTD_PLUGIN_READY 0x0
#define ANTD_PLUGIN_PANNIC 0x1
#define ANTD_PLUGIN_INIT 0x2
#define MAX_PATH_LEN 256
typedef enum
@ -63,6 +61,7 @@ typedef struct
{
antd_client_t *client;
dictionary_t request;
antd_plugin_ctx_t * context;
} antd_request_t;
typedef struct
@ -73,23 +72,12 @@ typedef struct
} antd_response_header_t;
typedef struct
{
char name[MAX_PATH_LEN];
char dbpath[MAX_PATH_LEN];
char tmpdir[MAX_PATH_LEN];
char pdir[MAX_PATH_LEN];
dictionary_t config;
int raw_body;
int status;
void *instance_data;
} plugin_header_t;
void set_nonblock(int socket);
//void set_block(int socket);
int __attribute__((weak)) compressable(char *ctype);
#ifdef USE_ZLIB
int compressable(char *ctype);
void set_gzip_types(list_t list);
#endif
void antd_send_header(void *, antd_response_header_t *);
const char *get_status_str(int stat);
int __t(void *, const char *, ...);

51
lib/plugin.c Normal file
View File

@ -0,0 +1,51 @@
#include "plugin_ctx.h"
#include "plugin.h"
const char * antd_plugin_basedir(antd_plugin_ctx_t * ctx)
{
return ctx->basedir;
}
const char * antd_plugin_tmpdir(antd_plugin_ctx_t * ctx)
{
return ctx->tmpdir;
}
const char * antd_plugin_confdir(antd_plugin_ctx_t *ctx)
{
if(ctx->confdir == NULL)
{
struct stat st;
ctx->confdir = __s("%s%s%s", ctx->basedir,DIR_SEP, ctx->name);
if (stat(ctx->confdir, &st) == -1)
mkdir(ctx->confdir, 0755);
}
return ctx->confdir;
}
const char * antd_plugin_name(antd_plugin_ctx_t *ctx)
{
return ctx->name;
}
void antd_plugin_set_status(antd_plugin_ctx_t * ctx, int stat)
{
ctx->status = stat;
}
int antd_plugin_status(antd_plugin_ctx_t * ctx)
{
return ctx->status;
}
void antd_plugin_use_raw_body(antd_plugin_ctx_t * ctx)
{
ctx->raw_body = 1;
}
int antd_plugin_is_raw_body(antd_plugin_ctx_t *ctx)
{
return ctx->raw_body == 1;
}
void* antd_plugin_data(antd_plugin_ctx_t* ctx)
{
return ctx->data;
}
dictionary_t antd_plugin_config(antd_plugin_ctx_t* ctx)
{
return ctx->config;
}

View File

@ -1,61 +1,41 @@
#ifndef PLUGIN_H
#define PLUGIN_H
#include <sys/stat.h>
#define ANTD_PLUGIN_READY 0x0
#define ANTD_PLUGIN_PANNIC 0x1
#define ANTD_PLUGIN_INIT 0x2
#define PLUGIN_INIT "create"
#define PLUGIN_HANDLE "handle"
#define PLUGIN_DROP "drop"
#define DEF_PLUGIN_INTERFACE(name, param, ret) ret##name(param)
#include "utils.h"
#include "handle.h"
#include "dictionary.h"
#define PLUGIN_PANIC(ctx, a, ...) \
ERROR("%s: " a, antd_plugin_name(ctx), ##__VA_ARGS__); \
antd_plugin_set_status(ctx, ANTD_PLUGIN_PANNIC);
char* config_dir();
/*Default function for plugin*/
// init the plugin
void init();
void destroy();
void* handle(void*);
plugin_header_t* meta();
void use_raw_body();
typedef struct _plugin_ctx_t antd_plugin_ctx_t;
/*
STATIC PART, should be included in any plugin
*/
#ifdef PLUGIN_IMPLEMENT
const char *antd_plugin_basedir(antd_plugin_ctx_t *);
const char *antd_plugin_tmpdir(antd_plugin_ctx_t *);
const char *antd_plugin_confdir(antd_plugin_ctx_t *);
const char *antd_plugin_name(antd_plugin_ctx_t *);
void antd_plugin_set_status(antd_plugin_ctx_t *, int);
int antd_plugin_status(antd_plugin_ctx_t *);
void antd_plugin_use_raw_body(antd_plugin_ctx_t *);
int antd_plugin_is_raw_body(antd_plugin_ctx_t *);
void *antd_plugin_data(antd_plugin_ctx_t *);
dictionary_t antd_plugin_config(antd_plugin_ctx_t*);
#define PLUGIN_PANIC(a,...) \
ERROR("%s: "a,__plugin__.name, ##__VA_ARGS__); \
__plugin__.status = ANTD_PLUGIN_PANNIC;
/*Default interfaces shall be implemented by plugin*/
void *create(antd_plugin_ctx_t *);
void drop(antd_plugin_ctx_t *);
void *handle(void *);
static plugin_header_t __plugin__;
// private function
void __init_plugin__(plugin_header_t* pl, dictionary_t* conf){
(void) memcpy(&__plugin__, pl, sizeof(plugin_header_t));
__plugin__.status = ANTD_PLUGIN_READY;
init();
};
void use_raw_body()
{
__plugin__.raw_body = 1;
}
plugin_header_t* meta()
{
return &__plugin__;
}
char* config_dir()
{
struct stat st;
char* path = __s("%s%s%s", __plugin__.pdir,DIR_SEP, __plugin__.name);
if (stat(path, &st) == -1)
mkdir(path, 0755);
return path;
}
void __release__()
{
destroy();
}
#endif
#endif

18
lib/plugin_ctx.h Normal file
View File

@ -0,0 +1,18 @@
#ifndef PLUGIN_CTX_H
#define PLUGIN_CTX_H
#include "handle.h"
struct _plugin_ctx_t
{
char * name;
const char * tmpdir;
const char * basedir;
char * confdir;
int raw_body;
int status;
dictionary_t config;
void * data;
void *(*handle)(void *);
void *(*create)(struct _plugin_ctx_t *);
void (*drop)(struct _plugin_ctx_t *);
} ;
#endif

View File

@ -533,7 +533,7 @@ void antd_scheduler_destroy(antd_scheduler_t *scheduler)
*/
antd_task_t *antd_create_task(void *(*handle)(void *), void *data, void *(*callback)(void *), time_t atime)
{
antd_task_t *task = (antd_task_t *)malloc(sizeof *task);
antd_task_t *task = (antd_task_t *)malloc(sizeof(antd_task_t));
task->stamp = (unsigned long)time(NULL);
task->data = data;
task->handle = handle;

View File

@ -1,8 +1,6 @@
#ifndef ANT_SCHEDULER
#define ANT_SCHEDULER
#include <pthread.h>
#include <semaphore.h>
#include <stdint.h>

View File

@ -46,6 +46,8 @@ THE SOFTWARE.
#include "dictionary.h"
// #include <time.h>
static dictionary_t g_mime_list = NULL;
/**
* Trim a string by a character on both ends
* @param str The target string
@ -184,8 +186,14 @@ void verify_header(char *k)
dictionary_t mimes_list()
{
return NULL;
return g_mime_list;
}
void set_mimes_list(dictionary_t dict)
{
g_mime_list = dict;
}
/*get mime file info from type*/
mime_t mime_from_type(const char *type)
{

View File

@ -34,6 +34,7 @@ THE SOFTWARE.
#include "dictionary.h"
#define STRINGIFY(x) #x
#define LEFTROTATE(x, c) (((x) << (c)) | ((x) >> (32 - (c))))
#define EQU(a,b) (strcmp(a,b) == 0)
#define IEQU(a,b) (strcasecmp(a,b) == 0)
@ -44,9 +45,9 @@ THE SOFTWARE.
#define true 1
#define false 0
#define LOG(a,...) syslog (LOG_NOTICE,"ANTD_LOG@[%s: %d]: " a "\n", __FILE__, \
#define LOG(a,...) syslog (LOG_NOTICE,"ANTD_LOG@[%s:%d]: " a "\n", __FILE__, \
__LINE__, ##__VA_ARGS__)
#define ERROR(a,...) syslog (LOG_ERR, "ANTD_ERROR@[%s: %d]: " a "\n", __FILE__, \
#define ERROR(a,...) syslog (LOG_ERR, "ANTD_ERROR@[%s:%d]: " a "\n", __FILE__, \
__LINE__, ##__VA_ARGS__)
// add this to the utils
#define UNUSED(x) (void)(x)
@ -65,7 +66,8 @@ typedef union
struct sockaddr_in addr4;
} antd_sockaddr_t;
dictionary_t __attribute__((weak)) mimes_list();
dictionary_t mimes_list();
void set_mimes_list(dictionary_t);
char* __s(const char*,...);
void trim(char*,const char);
void removeAll(const char* path,int mode);