From 522656e8b68f7189d1c221e19211946ba7774ab5 Mon Sep 17 00:00:00 2001 From: ken Date: Sat, 1 Oct 2016 04:34:34 -0700 Subject: [PATCH] cgi frontend --- .gitignore | 1 + src/Makefile | 10 ++-- src/bin/tools/ockpromo-fcgi.c | 50 ++++++++++++++++++ src/bin/tools/ockpromo-fcgi.ld | 1 + src/bin/tools/testir.c | 94 ++++++++++++++++++++++++++++------ src/ock/curl_sendmail.c | 23 +++++++++ src/ock/db_store.c | 75 +++++++++++++++++++++++++++ src/ock/ock.h | 7 +++ 8 files changed, 240 insertions(+), 21 deletions(-) create mode 100644 src/bin/tools/ockpromo-fcgi.c create mode 100644 src/bin/tools/ockpromo-fcgi.ld create mode 100644 src/ock/curl_sendmail.c create mode 100644 src/ock/db_store.c create mode 100644 src/ock/ock.h diff --git a/.gitignore b/.gitignore index 6ec091e..436c46e 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ include/* lib*/* Makefile~ dist/* +*.*~ diff --git a/src/Makefile b/src/Makefile index 89ebf6e..b4c8225 100644 --- a/src/Makefile +++ b/src/Makefile @@ -144,7 +144,7 @@ $(eval OLDER_TIME := $(firstword $(sort $(NEWEST_TRG_TIME) $(SRC_TIME)))) $(if $(MAKECMDGOALS), $(eval BUILDGOALS := $(filter-out clean scrub purge uninstall,$(MAKECMDGOALS))), $(eval BUILDGOALS := all)) -$(if $(or $(MISSING_TRG), $(and $(BUILDGOALS),$(filter $(OLDER_TIME),$(NEWEST_TRG_TIME)))), +$(if $(and $(BUILDGOALS),$(or $(MISSING_TRG), $(filter $(OLDER_TIME),$(NEWEST_TRG_TIME)))), $(if $(findstring n,$(MAKEFLAGS)), $(SLANG_TRG): $(SHELL_CMD) @@ -242,7 +242,9 @@ $(eval DRIVER_MODULES := $(filter-out $(DRIVER_DIR) ..,$(sort $(DRIVER_MODULES)) # any internal modules, the developer may instead create a file with the same # basename as the driver, but with the '.ld' suffix, which contains a space # separated list of internal modules to link together during compilation -$(eval DRIVER_MODULES += $(file <$(1:%.$2=%.ld))) +$(eval DRIVER_LDVALS := $(file <$(1:%.$2=%.ld))) +$(eval DRIVER_LFLAGS := $(filter -l%,$(DRIVER_LDVALS))) +$(eval DRIVER_MODULES += $(filter-out -l%,$(DRIVER_LDVALS))) # List of module archives to link together during compilation $(eval DRIVER_ARCHIVES := $(DRIVER_MODULES:%=%.$($2_AROBJ))) @@ -271,8 +273,8 @@ $(if $($2_DBG),$(eval MAKE_DIRS += $(DRIVER_TARG_DIR).$($2_DBG))) # '_start'). This driver object is then simply linked to its libraries and # module archives to create an executable binary in the output folder. # ORDER MATTERS HERE, this is for the linker: -$(eval DRIVER_SRC := $(DRIVER_DEPS)) -$(eval DRIVER_DBGSRC := $(DRIVER_DBGDEPS)) +$(eval DRIVER_SRC := $(DRIVER_LFLAGS) $(DRIVER_DEPS)) +$(eval DRIVER_DBGSRC := $(DRIVER_LFLAGS) $(DRIVER_DBGDEPS)) # Iterate through the list of libraries in our language and stack commands from # left to right in the "sources" section of the LD command (or LD section of the # commandline for the compiler), to preserve ordering and also ensure that it is diff --git a/src/bin/tools/ockpromo-fcgi.c b/src/bin/tools/ockpromo-fcgi.c new file mode 100644 index 0000000..9ecc790 --- /dev/null +++ b/src/bin/tools/ockpromo-fcgi.c @@ -0,0 +1,50 @@ +/*!@file + \brief Mail router for OCKoreanMartialArts.com + \details This mail routing system is intended to run as a daemon for fastcgi + and stores usage information in a database before sending mail to the + administrator + \author Ken + \date Sept 2016 + ----------------------------------------------------------------------------*/ +/* Standard */ +#include //atoi +#include //mem +/* Third Party */ +#include +/* Internal */ +#include + +int get_body(void); + +int +main +() +{ if (db_init()) + return -1; + + while(FCGI_Accept() >= 0) + { printf("Content-type: application/json\r\n\r\n"); + printf("{ \"submission\" : "); + if (!get_body()) + printf("\"fail\""); + printf("\"pass\""); + printf(" }"); + } + return 0; +} + +int +get_body +() +{ char *lencp; + int len, i; + + if ((lencp = getenv("CONTENT_LENGTH")) == NULL + || (len = atoi(lencp)) < 1) + return -1; + for (i = 0; i < len; i++) + putchar(getchar()); + + return 0; +} + diff --git a/src/bin/tools/ockpromo-fcgi.ld b/src/bin/tools/ockpromo-fcgi.ld new file mode 100644 index 0000000..7e72f14 --- /dev/null +++ b/src/bin/tools/ockpromo-fcgi.ld @@ -0,0 +1 @@ +-lfcgi -lmysqlclient diff --git a/src/bin/tools/testir.c b/src/bin/tools/testir.c index 69c9c28..e2d423e 100644 --- a/src/bin/tools/testir.c +++ b/src/bin/tools/testir.c @@ -6,8 +6,16 @@ ----------------------------------------------------------------------------*/ /* Standard */ #include //print +#include //itoa +#include //lib errors /* Internal */ -#include //link to IR +#include +extern //irmem.c +void ir_quit(void); +extern +int ir_init(void); + +struct irmem; /* Ansi Term Colors */ #define RED "\x1b[31m" @@ -16,39 +24,91 @@ #define BLUE "\x1b[34m" #define MAGENTA "\x1b[35m" #define CYAN "\x1b[36m" -#define CLRX "\x1b[0m" //clear current color - -extern //ir.c -void ir_quit(void); -extern -int ir_init(void); -extern - +#define CLRC "\x1b[0m" //clear current color int main(void); int test_init(void); +int test_mem(void); +int test_ir(void); +int test_ir_densedir(void); int main () -{ test_init(); +#define $($)#$ +#define PRINTFAIL(U) printf(RED $(U) "FAILED\n" CLRC) +#define PRINTPASS(U) printf(GREEN $(U) "PASS\n" CLRC) +#define PRINTINFO(S) printf(YELLOW S CLRC) +#define RUN_UNIT(U,T) \ + do { \ + PRINTINFO(T); \ + if (U()) \ + PRINTFAIL(U); \ + PRINTPASS(U); \ + } while (0) +{ RUN_UNIT(test_init,"Initializing\n"); + RUN_UNIT(test_mem,"Memtest\n"); + RUN_UNIT(test_ir,"Testing IR API\n"); return 0; } int test_init () -{ /* Test Init */ - printf("YELLOW Initializing\n"); +#define TESTS 50 +{ static int n = 0; + printf("Init Run %-2i\n",n+1); if (ir_init()) - { printf("RED FAILED CLRX"); - perror("ir init"); - return 1; + { perror("ir init"); + return -1; } - printf("Quitting CLRX\n"); ir_quit(); - printf("GREEN PASS"); + return (++n < TESTS) ? test_init() : ir_init(); +} + +int +test_mem +() +{ return 0; +} + +int +test_ir +() +{ +} + +/* Test for 64*64 dense directories */ +int +test_ir_densedir +() +#define DISTANCE 64 +#define SSTRLEN(S) (sizeof(S)/sizeof(S[0])) +#define PUSHDIR() \ + do {\ + + } while (0) +{ static char dbuf[256] = "DEEP"; + static char wbuf[256] = "WIDE"; + static int depth = -1; + static int width = -1; + static char *dnum = dbuf + SSTRLEN(dbuf) - 1; + static char *wnum = wbuf + SSTRLEN(wbuf) - 1; + + while (++depth < DISTANCE) + { itoa(depth,dnum,10); + push_cdat(dbuf); + while (++width < DISTANCE) + { itoa(width,wnum,10); + push_cdat(wbuf); + pop_cdat(); + } + pop_cdat(); + width = -1; + } return 0; } + + diff --git a/src/ock/curl_sendmail.c b/src/ock/curl_sendmail.c new file mode 100644 index 0000000..c139262 --- /dev/null +++ b/src/ock/curl_sendmail.c @@ -0,0 +1,23 @@ +/*!@file + \brief Mail router for OCKoreanMartialArts.com + \details This mail routing system is intended to run as a daemon for fastcgi + and stores usage information in a database before sending mail to the + administrator + \author Ken + \date Sept 2016 + ----------------------------------------------------------------------------*/ +/* Standard */ +#include +#include //mem +/* Third Party */ +#include +/* Internal */ +#include + +int send_mail(void); + +int +send_mail +() +{ return 0; +} diff --git a/src/ock/db_store.c b/src/ock/db_store.c new file mode 100644 index 0000000..85a4ee8 --- /dev/null +++ b/src/ock/db_store.c @@ -0,0 +1,75 @@ +/*!@file + \brief Database storage for OCK's promo handler + \details + \author Ken + \date Sept 2016 + ----------------------------------------------------------------------------*/ +/* Standard */ +#include //print +#include //mem +/* Third Party */ +#include +/* Internal */ +#include + +int db_init(void); +int db_insert(char*,char*,char*,char*); + +MYSQL *mysql; + +int +db_init +() +#define HOST "localhost" +#define USER "ock" +#define PASS "#0CK0r34!" +#define DATABASE "ock_db" +#define PORT 3306 +#define SOCKET NULL +#define FLAGS 0 +#define DATTABLE "promo_submissions" +#define DTABFMT \ + "id not null auto_increment," \ + "firstname varchar (32) not null," \ + "lastname varchar (32) not null," \ + "email varchar (64) not null," \ + "phone varchar (16) not null," \ + "primary key (id)" +{ mysql = mysql_init(NULL); + if ((mysql = mysql_real_connect(mysql, HOST, USER, PASS, DATABASE, PORT, SOCKET, FLAGS)) == NULL) + { fprintf(stderr,"Failed to establish connection to db.\n"); + return -1; + } + if (mysql_query(mysql, "SHOW TABLES LIKE '" DATTABLE "';")) + { fprintf(stderr,"Initial query failed\n"); + return -1; + } + if (mysql_num_rows(mysql_use_result(mysql)) < 1) + if (mysql_query(mysql, "create table " DATTABLE " (" DTABFMT ");")) + { fprintf(stderr,"Failed to create table.\n"); + return -1; + } + return 0; +} + +int +db_insert +( char *fname, + char *lname, + char *email, + char *phone +) +#define INSERT_FMT \ + "INSERT INTO " DATTABLE \ + " VALUES ('%s','%s','%s','%s')" \ + , fname, lname, email, phone +#define INSERT_ERR "SQL Insertion Failed for %s %s - %s %s\n" \ + , fname, lname, email, phone +{ char sqlinsert[256]; + sprintf(sqlinsert, INSERT_FMT); + if (mysql_query(mysql, sqlinsert)) + { fprintf(stderr, INSERT_ERR); + return -1; + } + return 0; +} diff --git a/src/ock/ock.h b/src/ock/ock.h new file mode 100644 index 0000000..182ed0a --- /dev/null +++ b/src/ock/ock.h @@ -0,0 +1,7 @@ +#ifndef _OCK_H +#define _OCK_H + +int db_init(void); +int db_insert(char*,char*,char*,char*); + +#endif //_OCK_H -- 2.18.0