Correct names and fix declaration issues

This commit is contained in:
2019-04-17 10:17:27 +02:00
parent 77ebc47a63
commit db758cd938
16 changed files with 493 additions and 447 deletions
+36
View File
@@ -0,0 +1,36 @@
/******************************************************************************
*
* Copyright (c) 2017-2019 by Löwenware Ltd
* Please, refer LICENSE file for legal information
*
******************************************************************************/
/**
* @file ctx.c
* @author Ilja Kartašov <ik@lowenware.com>
* @brief
*
* @see https://lowenware.com/
*/
#include "ctx.h"
aislx_ctx_t
aislx_ctx_new(aislx_module_t mod)
{
aislx_ctx_t ctx;
if ((ctx = calloc(1, mod->ctx_size)) != NULL) {
ctx->mod = mod;
}
return ctx;
}
void
aislx_ctx_free(aislx_ctx_t ctx)
{
free(ctx);
}
+48
View File
@@ -0,0 +1,48 @@
/******************************************************************************
*
* Copyright (c) 2017-2019 by Löwenware Ltd
* Please, refer LICENSE file for legal information
*
******************************************************************************/
/**
* @file context.h
* @author Ilja Kartašov <ik@lowenware.com>
* @brief
*
* @see https://lowenware.com/aisl/
*/
#ifndef AISLX_CTX_H_683B0A86_2A15_483D_B0F9_7BFBB4BA068F
#define AISLX_CTX_H_683B0A86_2A15_483D_B0F9_7BFBB4BA068F
#define AISLX_CTX(x) ((aislx_ctx_t)x)
#include <mods/module.h>
/** @brief Root level ASIL mod's context structure
*/
struct aislx_ctx {
aislx_module_t mod; /**< Mod's pointer */
};
/** @brief Root level ASIL mod's context structure pointer
*/
typedef struct aislx_ctx * aislx_ctx_t;
/** @brief Allocates zeroed #aislx_t context
* @param mod an instance of #aislx_t
* @return pointer to newly allocated #aisl_mox_ctx_t
*/
aislx_ctx_t
aislx_ctx_new(aislx_module_t mod);
/** @brief Frees previosly allocated #aislx_ctx_t
* @param ctx an instance of mod's context structure
* */
void
aislx_ctx_free(aislx_ctx_t ctx);
#endif /* !AISLX_CTX_H */
+54 -51
View File
@@ -12,18 +12,21 @@
*
* @see https://lowenware.com/aisl/
*/
#include <stdlib.h>
#include <string.h>
#include <components/query.h>
#include <components/mail.h>
#include <components/validate.h>
#include <components/quick.h>
#include "mod-feedback.h"
#include <cStuff/string.h>
#include "feedback.h"
struct context
{
struct aislx_mod_ctx root;
struct aislx_query qs;
struct aislx_mail mail;
struct aislx_ctx root;
struct aislx_query qs;
struct aislx_mail mail;
char * email;
char * msg;
@@ -36,7 +39,6 @@ static void
context_free(context_t ctx)
{
aislx_query_release(&ctx->qs);
aislx_mail_release(&ctx->mail);
if (ctx->email)
free(ctx->email);
@@ -44,17 +46,17 @@ context_free(context_t ctx)
if (ctx->msg)
free(ctx->msg);
aislx_mod_ctx_free(ctx);
aislx_ctx_free((aislx_ctx_t)ctx);
}
static int
on_input_var( const char *key, int k_len,
const char *val, int v_len,
void *p_ctx )
on_input_var( const char *key, uint32_t k_len,
const char *val, uint32_t v_len,
void *p_ctx )
{
context_t ctx = (context_t) p_ctx;
aislx_mod_feedback_t mod = (aislx_mod_feedback_t)((aislx_mod_ctx_t)ctx)->mod;
aislx_feedback_t mod = (aislx_feedback_t)((aislx_ctx_t)ctx)->mod;
if (!ctx->email && k_len == mod->name_email_length)
{
@@ -73,45 +75,42 @@ on_input_var( const char *key, int k_len,
static aisl_status_t
on_stream_open(aislx_mod_feedback_t mod, aisl_evt_stream_open_t const evt)
on_stream_open(aislx_feedback_t mod, aisl_evt_stream_open_t const evt)
{
context_t ctx;
aisl_stream_t s = (aisl_stream_t) (evt->evt.source);
if (!(ctx = (context_t)aislx_mod_ctx_new(mod)))
if (!(ctx = (context_t)aislx_ctx_new(AISLX_MODULE(mod))))
return AISL_MALLOC_ERROR;
if (aislx_mail_init(&ctx->mail) != 0)
goto except;
ctx->mail.from = mod->mail_from;
ctx->mail.reply_to = mod->mail_reply_to;
ctx->mail.subject = mod->mail_subject;
ctx->mail.smtp_server = mod->smtp_server;
ctx->mail.smtp_user = mod->smtp_user;
ctx->mail.smtp_password = mod->smtp_password;
ctx->mail.smtp_port = mod->smtp_port;
ctx->mail.from = mod->mail_from;
ctx->mail.to = mod->mail_to;
ctx->mail.subject = mod->mail_subject;
aisl_set_context(s, ctx);
return AISL_SUCCESS;
except:
context_free(ctx);
return AISL_MALLOC_ERROR;
/*
except:
context_free(ctx);
return AISL_MALLOC_ERROR;
*/
}
static aisl_status_t
on_stream_header(aislx_mod_feedback_t mod, aisl_evt_stream_header_t const evt)
on_stream_header(aislx_feedback_t mod, aisl_evt_stream_header_t const evt)
{
aisl_stream_t s = (aisl_stream_t)evt->evt.source;
context_t ctx = aisl_get_context(s);
if (strcmp(evt->key, "content-length")==0)
{
size_t total = strtoll(evt->value, NULL, 10);
if(aislx_query_init(&ctx->qs, total, on_input_var, (void*)ctx) != 0)
{
aisl_stream_t s = (aisl_stream_source_t)evt->source;
aislx_quick_response(s, AISL_HTTP_INTERNAL_SERVER_ERROR);
}
}
@@ -120,25 +119,23 @@ on_stream_header(aislx_mod_feedback_t mod, aisl_evt_stream_header_t const evt)
static aisl_status_t
on_stream_input(aislx_mod_feedback_t mod, aisl_evt_stream_input_t const evt)
on_stream_input(aislx_feedback_t mod, aisl_evt_stream_input_t const evt)
{
context_t ctx = aisl_get_context((aisl_stream_t)evt->evt.source);
aislx_query_feed(ctx->qs, evt->data, evt->size);
aislx_query_feed(&ctx->qs, evt->data, evt->size);
return AISL_SUCCESS;
}
static aisl_status_t
on_stream_request(aislx_mod_feedback_t mod, aisl_evt_t const evt)
on_stream_request(aislx_feedback_t mod, aisl_evt_t const evt)
{
context_t ctx = aisl_get_context((aisl_stream_t)evt->evt.source);
aisl_status_t status;
context_t ctx = aisl_get_context((aisl_stream_t)evt->source);
aisl_stream_t s = (aisl_stream_t)evt->source;
/* finalize input */
aislx_query_flush(ctx->qs);
/* verify input */
if (!ctx->email || !ctx->msg || aislx_validate_email(ctx->email) != 0)
aislx_quick_response(s, AISL_HTTP_BAD_REQUEST);
@@ -148,7 +145,13 @@ on_stream_request(aislx_mod_feedback_t mod, aisl_evt_t const evt)
uuid_generate(ctx->mail.msg_id);
/* create thread */
if (aislx_mail_send(&ctx->mail) != AISL_SUCCESS)
status = aislx_mail_send(&ctx->mail
, mod->smtp_host
, mod->smtp_user
, mod->smtp_pass
, 0);
if (status != AISL_SUCCESS)
aislx_quick_response(s, AISL_HTTP_INTERNAL_SERVER_ERROR);
aisl_set_output_event(s, true); /**< enable output event */
@@ -158,9 +161,9 @@ on_stream_request(aislx_mod_feedback_t mod, aisl_evt_t const evt)
static aisl_status_t
on_stream_output(aislx_mod_feedback_t mod, aisl_evt_t const evt)
on_stream_output(aislx_feedback_t mod, aisl_evt_t const evt)
{
context_t ctx = aisl_get_context((aisl_stream_t)evt->evt.source);
context_t ctx = aisl_get_context((aisl_stream_t)evt->source);
aisl_http_response_t rc = AISL_HTTP_OK;
@@ -180,15 +183,15 @@ on_stream_output(aislx_mod_feedback_t mod, aisl_evt_t const evt)
static aisl_status_t
on_stream_close(aislx_mod_feedback_t mod, aisl_evt_t const evt)
on_stream_close(aislx_feedback_t mod, aisl_evt_t const evt)
{
context_free((context_t)aisl_get_context((aisl_stream_t)evt->evt.source));
context_free((context_t)aisl_get_context((aisl_stream_t)evt->source));
return AISL_SUCCESS;
}
static aisl_status_t
aislx_mod_feedback_on_event(aislx_mod_feedback_t mod, aisl_evt_t const evt)
aislx_feedback_on_event(aislx_feedback_t mod, aisl_evt_t const evt)
{
switch(evt->code)
{
@@ -217,19 +220,19 @@ aislx_mod_feedback_on_event(aislx_mod_feedback_t mod, aisl_evt_t const evt)
aisl_status_t
aislx_mod_feedback_init(aislx_mod_feedback_t mod, aislx_mod_feedback_cfg_t cfg)
aislx_feedback_init(aislx_feedback_t mod, aislx_feedback_cfg_t cfg)
{
AISLX_MOD_INIT(aislx_mod_feedback, cfg->end_point);
AISLX_MODULE_INIT(aislx_feedback, cfg->end_point);
mod->mail_subject = cfg->mail_subject;
mod->mail_from = cfg->mail_from;
mod->mail_reply_to = cfg->mail_reply_to;
mod->name_email = cfg->name_email;
mod->name_msg = cfg->name_msg;
mod->smtp_server = cfg->smtp_server;
mod->smtp_user = cfg->smtp_user;
mod->smtp_password = cfg->smtp_password;
mod->smtp_port = cfg->smtp_port;
mod->mail_from = cfg->mail_from;
mod->mail_to = cfg->mail_to;
mod->name_email = cfg->name_email;
mod->name_msg = cfg->name_msg;
mod->smtp_host = cfg->smtp_host;
mod->smtp_user = cfg->smtp_user;
mod->smtp_pass = cfg->smtp_pass;
mod->smtp_port = cfg->smtp_port;
mod->name_email_length = strlen(cfg->name_email);
mod->name_msg_length = strlen(cfg->name_msg);
@@ -239,7 +242,7 @@ aislx_mod_feedback_init(aislx_mod_feedback_t mod, aislx_mod_feedback_cfg_t cfg)
void
aislx_mod_feedback_release(aislx_mod_feedback_t mod)
aislx_feedback_release(aislx_feedback_t mod)
{
}
+15 -14
View File
@@ -16,34 +16,35 @@
#ifndef AISLX_MOD_FEEDBACK_H_6CC516E4_A7F2_4A9D_B467_75DCF6F58108
#define AISLX_MOD_FEEDBACK_H_6CC516E4_A7F2_4A9D_B467_75DCF6F58108
#include <mods/interface.h>
#include <mods/ctx.h>
#include <mods/module.h>
struct aislx_mod_feedback_cfg
struct aislx_feedback_cfg
{
const char *end_point;
const char *name_email;
const char *name_msg;
const char *mail_subject;
const char *mail_from;
const char *mail_to;
const char *name_email;
const char *name_msg;
const char *smtp_host;
const char *smtp_user;
const char *smtp_pass;
uint16_t smtp_port;
};
typedef struct aislx_mod_feedback_cfg * aislx_mod_feedback_cfg_t;
typedef struct aislx_feedback_cfg * aislx_feedback_cfg_t;
struct aislx_mod_feedback
struct aislx_feedback
{
struct aislx_mod root;
struct aislx_module root;
const char *mail_subject;
const char *mail_from;
const char *name_email;
const char *name_msg;
const char *mail_subject;
const char *mail_from;
const char *mail_to;
const char *smtp_host;
const char *smtp_user;
const char *smtp_pass;
@@ -54,15 +55,15 @@ struct aislx_mod_feedback
};
typedef struct aislx_mod_feedback * aislx_mod_feedback_t;
typedef struct aislx_feedback * aislx_feedback_t;
aisl_status_t
aislx_mod_feedback_init(aislx_mod_feedback_t mod,
aislx_mod_feedback_cfg_t cfg);
aislx_feedback_init(aislx_feedback_t mod,
aislx_feedback_cfg_t cfg);
void
aislx_mod_feedback_release(aislx_mod_feedback_t mod);
aislx_feedback_release(aislx_feedback_t mod);
#endif /* !AISLX_MOD_FEEDBACK_H */
-75
View File
@@ -1,75 +0,0 @@
/******************************************************************************
*
* Copyright (c) 2017-2019 by Löwenware Ltd
* Please, refer LICENSE file for legal information
*
******************************************************************************/
/**
* @file interface.c
* @author Ilja Kartašov <ik@lowenware.com>
* @brief Interface source file for AISL SDK modules
*
* @see https://lowenware.com/
*/
#include <stdlib.h>
#include <string.h>
#include "interface.h"
aisl_mod_ctx_t
aisl_mod_ctx_new(aislx_mod_t mod)
{
aisl_mod_ctx_t ctx;
if ((ctx = calloc(1, mod->ctx_size)) != NULL)
{
ctx->mod = mod;
}
return ctx;
}
void
aislx_mod_ctx_free(aislx_mod_ctx_t ctx)
{
free(ctx);
}
aisl_status_t
aislx_mod_on_event(aislx_mod_t mod, aisl_evt_t const evt)
{
switch(evt->code)
{
case AISL_EVENT_STREAM_OPEN:
{
aisl_evt_stream_open_t so_evt = (aisl_evt_stream_open_t)evt;
if ( strncmp(so_evt->path, mod->end_point, mod->ep_length) != 0)
return AISL_IDLE;
break;
}
case AISL_EVENT_STREAM_HEADER:
case AISL_EVENT_STREAM_INPUT:
case AISL_EVENT_STREAM_REQUEST:
case AISL_EVENT_STREAM_OUTPUT:
case AISL_EVENT_STREAM_CLOSE:
{
aislx_mod_ctx_t ctx = aisl_get_context((aisl_stream_t)evt->source);
if (!ctx)
return AISL_SUCCESS;
if (ctx->mod == mod)
break;
}
default:
return AISL_IDLE;
}
return mod->on_event(mod, evt);
}
-90
View File
@@ -1,90 +0,0 @@
/******************************************************************************
*
* Copyright (c) 2017-2019 by Löwenware Ltd
* Please, refer LICENSE file for legal information
*
******************************************************************************/
/**
* @file interface.h
* @author Ilja Kartašov <ik@lowenware.com>
* @brief Interface header file for AISL SDK modules
*
* @see https://lowenware.com/aisl/
*/
#ifndef AISL_SDK_MODS_INTERFACE_H_1AEFECA5_9341_431B_8194_EA0C2E76B5EA
#define AISL_SDK_MODS_INTERFACE_H_1AEFECA5_9341_431B_8194_EA0C2E76B5EA
#include <aisl/aisl.h>
#define AISLX_MOD_INIT(MOD, END_POINT) \
do { \
((aislx_mod_t) mod)->end_point = END_POINT; \
((aislx_mod_t) mod)->ep_length = strlen(END_POINT); \
((aislx_mod_t) mod)->on_stream_event = MOD##_on_event; \
((aislx_mod_t) mod)->ctx_size = sizeof(struct context); \
} while(0) \
/** @brief AISL mod structure pointer
*/
typedef struct aislx_mod * aislx_mod_t;
/** @brief Pointer to AISL stream event observer
*/
typedef aisl_status_t
(* aislx_mod_observer_t)(aislx_mod_t mod, aisl_evt_t const evt);
/** @brief Root level AISL mod's structure
*/
struct aislx_mod
{
const char * end_point; /**< Root mod's URL or NULL */
aislx_mod_observer_t on_event; /**< Mod's stream event observer */
size_t ctx_size; /**< Mod's context size */
uint8_t ep_length; /**< Mod's context size */
};
/** @brief Root level ASIL mod's context structure
*/
struct aislx_mod_ctx
{
aislx_mod_t mod; /**< Mod's pointer */
};
/** @brief Root level ASIL mod's context structure pointer
*/
typedef struct aislx_mod_ctx * aislx_mod_ctx_t;
/** @brief Allocates zeroed #aislx_mod_t context
* @param mod an instance of #aislx_mod_t
* @return pointer to newly allocated #aisl_mox_ctx_t
*/
aislx_mod_ctx_t
aislx_mod_ctx_new(aislx_mod_t mod);
/** @brief Frees previosly allocated #aislx_mod_ctx_t
* @param ctx an instance of mod's context structure
* */
void
aislx_mod_ctx_free(aislx_mod_ctx_t ctx);
/** @brief Wrapper function that calles mod's observer
* @param mod an instance of #aislx_mod_t
* @param evt an AISL stream event
* @return AISL_SUCCESS if event handled, AISL_IDLE if event not handled or an
* error code
*/
aisl_status_t
aislx_mod_on_event(aislx_mod_t mod, aisl_evt_t const evt);
#endif /* !AISL_SDK_MODS_INTERFACE_H */
+54
View File
@@ -0,0 +1,54 @@
/******************************************************************************
*
* Copyright (c) 2017-2019 by Löwenware Ltd
* Please, refer LICENSE file for legal information
*
******************************************************************************/
/**
* @file interface.c
* @author Ilja Kartašov <ik@lowenware.com>
* @brief Interface source file for AISL SDK modules
*
* @see https://lowenware.com/
*/
#include <stdlib.h>
#include <string.h>
#include "module.h"
#include "ctx.h"
aisl_status_t
aislx_module_on_event(aislx_module_t mod, aisl_evt_t const evt)
{
aislx_ctx_t ctx;
aisl_evt_stream_open_t so_evt;
switch(evt->code)
{
case AISL_EVENT_STREAM_OPEN:
so_evt = (aisl_evt_stream_open_t)evt;
if (strncmp(so_evt->path, mod->end_point, mod->ep_length))
return AISL_IDLE;
break;
case AISL_EVENT_STREAM_HEADER:
case AISL_EVENT_STREAM_INPUT:
case AISL_EVENT_STREAM_REQUEST:
case AISL_EVENT_STREAM_OUTPUT:
case AISL_EVENT_STREAM_CLOSE:
if (!(ctx = aisl_get_context((aisl_stream_t)evt->source)))
return AISL_SUCCESS;
if (ctx->mod == mod)
break;
/* go through default */
default:
return AISL_IDLE;
}
return mod->on_event(mod, evt);
}
+62
View File
@@ -0,0 +1,62 @@
/******************************************************************************
*
* Copyright (c) 2017-2019 by Löwenware Ltd
* Please, refer LICENSE file for legal information
*
******************************************************************************/
/**
* @file interface.h
* @author Ilja Kartašov <ik@lowenware.com>
* @brief Interface header file for AISL SDK modules
*
* @see https://lowenware.com/aisl/
*/
#ifndef AISLX_MODULE_H_1AEFECA5_9341_431B_8194_EA0C2E76B5EA
#define AISLX_MODULE_H_1AEFECA5_9341_431B_8194_EA0C2E76B5EA
#include <aisl/aisl.h>
#define AISLX_MODULE_INIT(MOD, END_POINT) \
do { \
((aislx_module_t) mod)->end_point = END_POINT; \
((aislx_module_t) mod)->ep_length = strlen(END_POINT); \
((aislx_module_t) mod)->on_event = (aislx_observer_t)MOD##_on_event; \
((aislx_module_t) mod)->ctx_size = sizeof(struct context); \
} while(0) \
#define AISLX_MODULE(x) ((aislx_module_t)x)
/** @brief AISL module structure pointer
*/
typedef struct aislx_module * aislx_module_t;
/** @brief Pointer to AISL stream event observer
*/
typedef aisl_status_t
(* aislx_observer_t)(aislx_module_t mod, aisl_evt_t const evt);
/** @brief Root level AISL mod's structure
*/
struct aislx_module {
const char *end_point; /**< Root mod's URL or NULL */
aislx_observer_t on_event; /**< Mod's stream event observer */
size_t ctx_size; /**< Mod's context size */
uint8_t ep_length; /**< End-Point length */
};
/** @brief Wrapper function that calles mod's observer
* @param mod an instance of #aislx_t
* @param evt an AISL stream event
* @return AISL_SUCCESS if event handled, AISL_IDLE if event not handled or an
* error code
*/
aisl_status_t
aislx_module_on_event(aislx_module_t mod, aisl_evt_t const evt);
#endif /* !AISLX_MODULE;_H */