Correct names and fix declaration issues

This commit is contained in:
Ilja Kartašov 2019-04-17 10:17:27 +02:00
parent 77ebc47a63
commit db758cd938
16 changed files with 493 additions and 447 deletions

View File

@ -13,6 +13,10 @@
* @see https://lowenware.com/ * @see https://lowenware.com/
*/ */
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include "string.h" #include "string.h"
@ -86,13 +90,13 @@ cstuff_vsprintf(char **out, const char *format, va_list args)
char tmp, *s; char tmp, *s;
va_copy(vc, args); va_copy(vc, args);
result = vsprintf(&tmp, 1, format, vc); result = vsnprintf(&tmp, 1, format, vc);
va_end(vc); va_end(vc);
if (!(s = malloc(result + 1))) if (!(s = malloc(result + 1)))
return -1; return -1;
result = vsprintf(s, result+1, format, args); result = vsnprintf(s, result+1, format, args);
return result; return result;
} }

View File

@ -16,7 +16,6 @@
#ifndef CSTUFF_STRING_H_938B242C_B750_40E9_8B67_A69F2F37EB87 #ifndef CSTUFF_STRING_H_938B242C_B750_40E9_8B67_A69F2F37EB87
#define CSTUFF_STRING_H_938B242C_B750_40E9_8B67_A69F2F37EB87 #define CSTUFF_STRING_H_938B242C_B750_40E9_8B67_A69F2F37EB87
#include <string.h>
#include <stdarg.h> #include <stdarg.h>
#include "retcode.h" #include "retcode.h"
@ -43,6 +42,10 @@ int
cstuff_strset(char **out, const char *in); cstuff_strset(char **out, const char *in);
int
cstuff_strnset(char **out, const char *in, int len);
/** @brief Allocates memory and prints a formatted string into it /** @brief Allocates memory and prints a formatted string into it
* @param out a pointer to an address where new string must be stored * @param out a pointer to an address where new string must be stored
* @param format a format for string (see manual for stdlib sprintf) * @param format a format for string (see manual for stdlib sprintf)

View File

@ -13,17 +13,19 @@
* @see https://lowenware.com/aisl/ * @see https://lowenware.com/aisl/
*/ */
#include <stdlib.h>
#include <string.h>
#include <cStuff/string.h> #include <cStuff/string.h>
#include <curl/curl.h> #include <curl/curl.h>
#include "mail.h" #include "mail.h"
struct payload struct payload {
{
char now[40]; char now[40];
char *to; char *to;
char *from; char *from;
char *subject; char *subject;
char *msg_id;
char *smtp_user; char *smtp_user;
char *smtp_pass; char *smtp_pass;
char *smtp_url; char *smtp_url;
@ -41,12 +43,11 @@ payload_feed(void *ptr, size_t size, size_t nmemb, void *userp)
left = payload->total - payload->offset; left = payload->total - payload->offset;
if (left && (size = size*nmemb)) if (left && (size = size*nmemb)) {
{ if (size > left)
cf (size > left)
size = left; size = left;
memcpy(ptr, payload->data[payload->offset], size); memcpy(ptr, &payload->data[payload->offset], size);
payload->offset += size; payload->offset += size;
return size; return size;
@ -57,25 +58,23 @@ payload_feed(void *ptr, size_t size, size_t nmemb, void *userp)
static void static void
payload__get_now(char * now, size_t size) payload__get_now(char *now, size_t size)
{ {
time_t now = time(NULL); time_t time_now = time(NULL);
struct tm tm; struct tm tm;
gmtime_r(&now, &tm); gmtime_r(&time_now, &tm);
strftime(now, size, "%a, %d %b %Y %H:%M:%S GMT", &tm); strftime(now, size, "%a, %d %b %Y %H:%M:%S GMT", &tm);
} }
static char * static char *
payload__get_id(uuid_t uuid, char * server) payload__get_id(uuid_t uuid, const char *server)
{ {
size_t sz = strlen(server); size_t sz = strlen(server);
char *result = malloc( sz + 3 + 36 + 1 );
char * result = malloc( sz + 3 + 36 + 1 ); if (result) {
if (result)
{
char * p =result; char * p =result;
*(p++) = '<'; *(p++) = '<';
uuid_unparse(uuid, p); uuid_unparse(uuid, p);
@ -91,8 +90,39 @@ payload__get_id(uuid_t uuid, char * server)
} }
static void
payload_free(struct payload *payload)
{
if (payload->data)
free(payload->data);
if (payload->to)
free(payload->to);
if (payload->from)
free(payload->from);
if (payload->subject)
free(payload->subject);
if (payload->msg_id)
free(payload->msg_id);
if (payload->smtp_user)
free(payload->smtp_user);
if (payload->smtp_pass)
free(payload->smtp_pass);
if (payload->smtp_url)
free(payload->smtp_url);
free(payload);
}
static struct payload * static struct payload *
payload_new(aislx_mail_t mail, const char * server) payload_new(aislx_mail_t mail, const char *server)
{ {
size_t total; size_t total;
struct payload *payload; struct payload *payload;
@ -108,17 +138,17 @@ payload_new(aislx_mail_t mail, const char * server)
if (cstuff_strcpy(&payload->from, mail->from) == -1) if (cstuff_strcpy(&payload->from, mail->from) == -1)
goto e_cleanup; goto e_cleanup;
if ( !(payload->message_id = payload__get_id(mail->msg_id, server)) ) if ( !(payload->msg_id = payload__get_id(mail->msg_id, server)) )
goto e_cleanup; goto e_cleanup;
total = cstuff_sprintf( &payload->data, AISLX_MAIL_FORMAT total = cstuff_sprintf( &payload->data, AISLX_MAIL_FORMAT
, date , payload->now
, payload->to , payload->to
, payload->from , payload->from
, mail->reply_to , mail->reply_to
, payload->message_id , payload->msg_id
, mail->subject , mail->subject
, mail->message ); , mail->msg );
if (!(total > 0)) if (!(total > 0))
goto e_cleanup; goto e_cleanup;
@ -135,28 +165,16 @@ except:
} }
static void
payload_free(struct payload *payload)
{
if (payload->data)
free(payload->data);
free(payload);
}
static void * static void *
aislx_mail_execute(void * p_ctx) aislx_mail_execute(void *p_ctx)
{ {
void *result = NULL;
struct payload * payload = (struct payload *)p_ctx; struct payload * payload = (struct payload *)p_ctx;
aisl_status_t status = AISL_MALLOC_ERROR; aisl_status_t status = AISL_MALLOC_ERROR;
CURL *curl; CURL *curl;
struct curl_slist *rcpts; struct curl_slist *rcpts;
if ((curl = curl_easy_init()) != NULL) if ((curl = curl_easy_init()) != NULL) {
{
curl_easy_setopt(curl, CURLOPT_USERNAME, payload->smtp_user); curl_easy_setopt(curl, CURLOPT_USERNAME, payload->smtp_user);
curl_easy_setopt(curl, CURLOPT_PASSWORD, payload->smtp_pass); curl_easy_setopt(curl, CURLOPT_PASSWORD, payload->smtp_pass);
curl_easy_setopt(curl, CURLOPT_URL, payload->smtp_url); curl_easy_setopt(curl, CURLOPT_URL, payload->smtp_url);
@ -169,18 +187,14 @@ aislx_mail_execute(void * p_ctx)
curl_easy_setopt(curl, CURLOPT_MAIL_FROM, payload->from); curl_easy_setopt(curl, CURLOPT_MAIL_FROM, payload->from);
if ((rcpts = curl_slist_append(NULL, payload->to)) != NULL) if ((rcpts = curl_slist_append(NULL, payload->to)) != NULL) {
{
CURLcode res; CURLcode res;
if ((res = curl_easy_perform(curl)) != CURLE_OK) if ((res = curl_easy_perform(curl)) != CURLE_OK) {
{
fprintf(stderr, "curl_easy_perform() failed: %s\n" fprintf(stderr, "curl_easy_perform() failed: %s\n"
, curl_easy_strerror(res)); , curl_easy_strerror(res));
status = AISL_INPUT_ERROR; status = AISL_INPUT_ERROR;
} } else {
else
{
status = AISL_SUCCESS; status = AISL_SUCCESS;
} }
curl_slist_free_all(rcpts); curl_slist_free_all(rcpts);
@ -188,28 +202,28 @@ aislx_mail_execute(void * p_ctx)
curl_easy_cleanup(curl); curl_easy_cleanup(curl);
} }
return result + (int)status; return (void*)status;
} }
aisl_status_t aisl_status_t
aislx_mail_send( aislx_mail_t mail, aislx_mail_send( aislx_mail_t mail,
const char *smtp_server, const char *smtp_server,
const char *smtp_user, const char *smtp_user,
const char *smtp_pass, const char *smtp_pass,
int flags ) int flags )
{ {
struct payload * payload; int rc;
struct payload *payload;
if (!(payload = payload_new(mail, smtp_server))) if (!(payload = payload_new(mail, smtp_server)))
return AISL_MALLOC_ERROR; return AISL_MALLOC_ERROR;
if (pthread_create(&mail->thread, NULL, aislx_mail_execute, (void*)payload)) rc = pthread_create(&mail->thread, NULL, aislx_mail_execute, (void*)payload);
{ if (rc) {
payload_free(payload); payload_free(payload);
return AISL_SYSCALL_ERROR; return AISL_SYSCALL_ERROR;
} }
return AISL_SUCCESS; return AISL_SUCCESS;
} }
@ -222,13 +236,10 @@ aislx_mail_get_status(aislx_mail_t mail)
rc = pthread_tryjoin_np(mail->thread, &retval); rc = pthread_tryjoin_np(mail->thread, &retval);
if (rc == 0) if (rc == 0) {
{ rc = (aisl_status_t)retval;
rc = (int)(retval-0);
return rc; return rc;
} } else {
else
{
return AISL_IDLE; return AISL_IDLE;
} }
} }

View File

@ -39,8 +39,9 @@
struct aislx_mail struct aislx_mail
{ {
pthread_t pthread; pthread_t thread;
const char *to; const char *to;
const char *reply_to;
const char *from; const char *from;
const char *subject; const char *subject;
const char *msg; const char *msg;

View File

@ -13,14 +13,17 @@
* @see https://lowenware.com/aisl/ * @see https://lowenware.com/aisl/
*/ */
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include "query.h" #include "query.h"
aisl_status_t aisl_status_t
aislx_query_init( aislx_query_t query, aislx_query_init( aislx_query_t query,
size_t total, size_t total,
aislx_query_on_var on_var, aislx_query_on_var on_var,
void * p_ctx ) void * p_ctx )
{ {
query->on_var = on_var; query->on_var = on_var;
query->p_ctx = p_ctx; query->p_ctx = p_ctx;
@ -43,13 +46,12 @@ aislx_query_release(aislx_query_t query)
static void static void
aislx_query_notify(const char * key, uint32_t k_len, aislx_query_notify(const char * key, uint32_t k_len,
const cahr * val, uint32_t v_len, const char * val, uint32_t v_len,
aislx_query_t query) aislx_query_t query)
{ {
if (query->on_var) if (query->on_var) {
{
if (query->on_var(key, k_len, val, v_len, query->p_ctx) != 0) if (query->on_var(key, k_len, val, v_len, query->p_ctx) != 0)
query->on_var == NULL; query->on_var = NULL;
} }
} }
@ -57,55 +59,47 @@ aislx_query_notify(const char * key, uint32_t k_len,
static int32_t static int32_t
aislx_query_process(aislx_query_t query, const char * data, int32_t length) aislx_query_process(aislx_query_t query, const char * data, int32_t length)
{ {
const char * i = data, const char *i = data,
* key = data, *key = data,
* val = NULL; *val = NULL;
uint32_t k_len = 0;
uint32_t k_len = 0;
size_t result; size_t result;
for (result=0; result < length; result++) for (result=0; result < length; result++) {
{
char c; char c;
switch( (c = *i) ) switch( (c = *i) ) {
{ case '=':
case '=': if (val)
if (val) return -1;
return -1;
k_len = i-key; k_len = i-key;
val = i+1; val = i+1;
break;
case '&':
case ';':
if (!val)
return -1;
if (!query->separator)
query->separator = c;
if (query->separator == c) {
aislx_query_notify(key, k_len, val, i-val, query);
/* reset parser */
key = (c == ';') ? i+2 : i+1;
k_len = 0;
val = NULL;
break; break;
}
case '&':
case ';'
if (!val)
return -1;
if (!query->separator)
query->separator = c;
if (query->separator == c)
{
aislx_query_notify(key, k_len, val, i-val, query);
/* reset parser */
key = (c == ';') ? i+2 : i+1;
k_len = 0;
val = NULL;
break;
}
} }
i++; i++;
} }
if (query->total == result) if (query->total == result) {
{
aislx_query_notify(key, k_len, val, i-val, query); aislx_query_notify(key, k_len, val, i-val, query);
} } else {
else
{
result = (key > i) ? i - data : key - data; result = (key > i) ? i - data : key - data;
} }
@ -116,33 +110,31 @@ aislx_query_process(aislx_query_t query, const char * data, int32_t length)
aisl_status_t aisl_status_t
aislx_query_feed(aislx_query_t query, char * data, int32_t length) aislx_query_feed(aislx_query_t query, const char *data, int32_t length)
{ {
size_t processed; size_t processed;
char * source; const char *source;
if (query->data) if (query->data) {
{
if (!(source = realloc(query->data, length + query->size))) if (!(source = realloc(query->data, length + query->size)))
return AISL_MALLOC_ERROR; return AISL_MALLOC_ERROR;
length += query->size; length += query->size;
query->data = source; query->data = (char*)source;
query->size = length; query->size = length;
memcpy(&source[query->size], data, length); memcpy((char*)&source[query->size], data, length);
} } else {
else
source = data; source = data;
}
processed = aislx_query_process(query, source, length); processed = aislx_query_process(query, source, length);
if (processed < 0) if (processed < 0)
return AISL_INPUT_ERROR; return AISL_INPUT_ERROR;
if (processed < length) if (processed < length) {
{ char *buffer;
char * buffer;
length -= processed; length -= processed;
if (!(buffer = malloc(length))) if (!(buffer = malloc(length)))

View File

@ -17,33 +17,34 @@
#define AISLX_QUERY_H_9306EAEB_6DFC_4936_934A_6472F00E490C #define AISLX_QUERY_H_9306EAEB_6DFC_4936_934A_6472F00E490C
#include <stdint.h> #include <stdint.h>
#include <stddef.h>
#include <aisl/aisl.h>
typedef int typedef int
(*aislx_query_on_var)( const char * key, uint32_t k_len, (*aislx_query_on_var)( const char *key, uint32_t k_len,
const char * val, uint32_t v_len, const char *val, uint32_t v_len,
void * p_ctx ); void *p_ctx );
struct aislx_query struct aislx_query
{ {
aislx_query_on_var on_var; aislx_query_on_var on_var;
void *p_ctx;
void * p_ctx; char *data;
char * data; size_t size;
size_t size; size_t total;
size_t total; char separator;
char separator;
}; };
typedef struct aislx_query * aislx_query_t; typedef struct aislx_query * aislx_query_t;
aisl_status_t aisl_status_t
aislx_query_init( aislx_query_t query, aislx_query_init( aislx_query_t query,
size_t total, size_t total,
aislx_query_on_var on_var, aislx_query_on_var on_var,
void * p_ctx ); void *p_ctx );
void void
@ -51,7 +52,7 @@ aislx_query_release(aislx_query_t query);
aisl_status_t aisl_status_t
aislx_query_feed(aislx_query_t query, char * data, uint32_t length); aislx_query_feed(aislx_query_t query, const char *data, int32_t length);
#endif /* !AISLX_QUERY_H */ #endif /* !AISLX_QUERY_H */

View File

@ -21,12 +21,12 @@ aislx_quick_response(aisl_stream_t stream, aisl_http_response_t http_response)
{ {
aisl_status_t result; aisl_status_t result;
result = aisl_response(s, http_response, 0); result = aisl_response(stream, http_response, 0);
if (result == AISL_SUCCESS) if (result == AISL_SUCCESS)
aisl_flush(s); aisl_flush(stream);
else else
aisl_reject(s); aisl_reject(stream);
return result; return result;
} }

View File

@ -13,101 +13,96 @@
* @see https://lowenware.com/aisl/ * @see https://lowenware.com/aisl/
*/ */
#include <ctype.h>
#include <stddef.h>
#include <stdlib.h>
#include "validate.h" #include "validate.h"
int int
aislx_validate_email(const char * value) aislx_validate_email(const char *value)
{ {
const char * at = NULL, const char *at = NULL,
* dot = NULL, *dot = NULL,
* i = value; *i = value;
char c; char c;
while ( (c = *i) != 0 ) while ( (c = *i) != 0 ) {
{ switch(c) {
switch(c) case '@':
{ if (at || i == value)
case '@':
if (at || i == value) break;
at = i++;
dot = NULL;
continue;
case '"':
case '(':
case ')':
case ',':
case ';':
case ':':
case '<':
case '>':
case '[':
case '\\':
case ']':
case ' ':
/* These characters are allowed in double quotes only, but it is not
* implemented in this validator, so we count them as forbidden
*/
break; break;
case '!': at = i++;
case '#': dot = NULL;
case '$': continue;
case '%':
case '&':
case '\'':
case '*':
case '+':
case '/':
case '=':
case '?':
case '^':
case '_':
case '`':
case '{':
case '|':
case '}':
case '~':
/* Allowed special local-part characters */
if (!at)
{
i++;
continue;
}
else
break;
case '-': case '"':
/* Allowed characters */ case '(':
case ')':
case ',':
case ';':
case ':':
case '<':
case '>':
case '[':
case '\\':
case ']':
case ' ':
/* These characters are allowed in double quotes only, but it is not
* implemented in this validator, so we count them as forbidden
*/
break;
case '!':
case '#':
case '$':
case '%':
case '&':
case '\'':
case '*':
case '+':
case '/':
case '=':
case '?':
case '^':
case '_':
case '`':
case '{':
case '|':
case '}':
case '~':
/* Allowed special local-part characters */
if (!at) {
i++; i++;
continue; continue;
}
break;
case '.': case '-':
if (dot && i == dot+1) /* Allowed characters */
break; i++;
continue;
if (at)
{
if (i == at+1 || *(i+1) == 0)
break;
}
else
{
if (i == value || *(i+1) == '@')
break;
}
dot = i++;
continue;
default:
if ( isalnum(c) == 0 )
continue;
case '.':
if (dot && i == dot+1)
break; break;
if (at) {
if (i == at+1 || *(i+1) == 0)
break;
} else {
if (i == value || *(i+1) == '@')
break;
}
dot = i++;
continue;
default:
if ( isalnum(c) == 0 )
continue;
break;
} }
return (int)(i-value)+1; return (int)(i-value)+1;
} }

36
mods/ctx.c Normal file
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
mods/ctx.h Normal file
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 */

View File

@ -12,18 +12,21 @@
* *
* @see https://lowenware.com/aisl/ * @see https://lowenware.com/aisl/
*/ */
#include <stdlib.h>
#include <string.h>
#include <components/query.h> #include <components/query.h>
#include <components/mail.h> #include <components/mail.h>
#include <components/validate.h> #include <components/validate.h>
#include <components/quick.h> #include <components/quick.h>
#include "mod-feedback.h" #include <cStuff/string.h>
#include "feedback.h"
struct context struct context
{ {
struct aislx_mod_ctx root; struct aislx_ctx root;
struct aislx_query qs; struct aislx_query qs;
struct aislx_mail mail; struct aislx_mail mail;
char * email; char * email;
char * msg; char * msg;
@ -36,7 +39,6 @@ static void
context_free(context_t ctx) context_free(context_t ctx)
{ {
aislx_query_release(&ctx->qs); aislx_query_release(&ctx->qs);
aislx_mail_release(&ctx->mail);
if (ctx->email) if (ctx->email)
free(ctx->email); free(ctx->email);
@ -44,17 +46,17 @@ context_free(context_t ctx)
if (ctx->msg) if (ctx->msg)
free(ctx->msg); free(ctx->msg);
aislx_mod_ctx_free(ctx); aislx_ctx_free((aislx_ctx_t)ctx);
} }
static int static int
on_input_var( const char *key, int k_len, on_input_var( const char *key, uint32_t k_len,
const char *val, int v_len, const char *val, uint32_t v_len,
void *p_ctx ) void *p_ctx )
{ {
context_t ctx = (context_t) 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) 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 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; context_t ctx;
aisl_stream_t s = (aisl_stream_t) (evt->evt.source); 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; return AISL_MALLOC_ERROR;
if (aislx_mail_init(&ctx->mail) != 0) ctx->mail.from = mod->mail_from;
goto except; ctx->mail.to = mod->mail_to;
ctx->mail.subject = mod->mail_subject;
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;
aisl_set_context(s, ctx); aisl_set_context(s, ctx);
return AISL_SUCCESS; return AISL_SUCCESS;
except: /*
context_free(ctx); except:
return AISL_MALLOC_ERROR; context_free(ctx);
return AISL_MALLOC_ERROR;
*/
} }
static aisl_status_t 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) if (strcmp(evt->key, "content-length")==0)
{ {
size_t total = strtoll(evt->value, NULL, 10); size_t total = strtoll(evt->value, NULL, 10);
if(aislx_query_init(&ctx->qs, total, on_input_var, (void*)ctx) != 0) 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); 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 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); 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; return AISL_SUCCESS;
} }
static aisl_status_t 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; aisl_stream_t s = (aisl_stream_t)evt->source;
/* finalize input */
aislx_query_flush(ctx->qs);
/* verify input */ /* verify input */
if (!ctx->email || !ctx->msg || aislx_validate_email(ctx->email) != 0) if (!ctx->email || !ctx->msg || aislx_validate_email(ctx->email) != 0)
aislx_quick_response(s, AISL_HTTP_BAD_REQUEST); 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); uuid_generate(ctx->mail.msg_id);
/* create thread */ /* 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); aislx_quick_response(s, AISL_HTTP_INTERNAL_SERVER_ERROR);
aisl_set_output_event(s, true); /**< enable output event */ 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 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; 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 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; return AISL_SUCCESS;
} }
static aisl_status_t 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) 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 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_subject = cfg->mail_subject;
mod->mail_from = cfg->mail_from; mod->mail_from = cfg->mail_from;
mod->mail_reply_to = cfg->mail_reply_to; mod->mail_to = cfg->mail_to;
mod->name_email = cfg->name_email; mod->name_email = cfg->name_email;
mod->name_msg = cfg->name_msg; mod->name_msg = cfg->name_msg;
mod->smtp_server = cfg->smtp_server; mod->smtp_host = cfg->smtp_host;
mod->smtp_user = cfg->smtp_user; mod->smtp_user = cfg->smtp_user;
mod->smtp_password = cfg->smtp_password; mod->smtp_pass = cfg->smtp_pass;
mod->smtp_port = cfg->smtp_port; mod->smtp_port = cfg->smtp_port;
mod->name_email_length = strlen(cfg->name_email); mod->name_email_length = strlen(cfg->name_email);
mod->name_msg_length = strlen(cfg->name_msg); 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 void
aislx_mod_feedback_release(aislx_mod_feedback_t mod) aislx_feedback_release(aislx_feedback_t mod)
{ {
} }

View File

@ -16,34 +16,35 @@
#ifndef AISLX_MOD_FEEDBACK_H_6CC516E4_A7F2_4A9D_B467_75DCF6F58108 #ifndef AISLX_MOD_FEEDBACK_H_6CC516E4_A7F2_4A9D_B467_75DCF6F58108
#define 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_feedback_cfg
struct aislx_mod_feedback_cfg
{ {
const char *end_point; const char *end_point;
const char *name_email;
const char *name_msg;
const char *mail_subject; const char *mail_subject;
const char *mail_from; const char *mail_from;
const char *mail_to; const char *mail_to;
const char *name_email;
const char *name_msg;
const char *smtp_host; const char *smtp_host;
const char *smtp_user; const char *smtp_user;
const char *smtp_pass; const char *smtp_pass;
uint16_t smtp_port; 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_email;
const char *name_msg; const char *name_msg;
const char *mail_subject;
const char *mail_from;
const char *mail_to;
const char *smtp_host; const char *smtp_host;
const char *smtp_user; const char *smtp_user;
const char *smtp_pass; 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 aisl_status_t
aislx_mod_feedback_init(aislx_mod_feedback_t mod, aislx_feedback_init(aislx_feedback_t mod,
aislx_mod_feedback_cfg_t cfg); aislx_feedback_cfg_t cfg);
void void
aislx_mod_feedback_release(aislx_mod_feedback_t mod); aislx_feedback_release(aislx_feedback_t mod);
#endif /* !AISLX_MOD_FEEDBACK_H */ #endif /* !AISLX_MOD_FEEDBACK_H */

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);
}

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
mods/module.c Normal file
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
mods/module.h Normal file
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 */