diff --git a/documentation/sitefiles.md b/documentation/sitefiles.md index 15fe970..371e410 100644 --- a/documentation/sitefiles.md +++ b/documentation/sitefiles.md @@ -18,6 +18,7 @@ sitefiles also allow comments with # # Part 3: Variables -* ```type```: - The type of http request to respond to. One of: - * get - * post +* ```respondto```: - The type of http request to respond to. One of: + * GET + * POST +Default: GET diff --git a/src/connections.c b/src/connections.c index 63d063b..7386264 100644 --- a/src/connections.c +++ b/src/connections.c @@ -26,6 +26,7 @@ #include #include +#include #include #include #include @@ -80,41 +81,7 @@ static int processRequest(Connection *conn) { for (int i = 0;; i++) { if (line[i] == ' ') { line[i] = '\0'; - if (i >= 8) - return 1; - uint64_t type = 0; - for (int j = 0; j < i; j++) { - type <<= 8; - type |= line[j]; - } - switch (type) { - case 0x474554l: - conn->type = GET; - break; - case 0x504f5354l: - conn->type = POST; - break; - case 0x505554l: - conn->type = PUT; - break; - case 0x48454144l: - conn->type = HEAD; - break; - case 0x44454c455445l: - conn->type = DELETE; - break; - case 0x5041544348l: - conn->type = PATCH; - break; - case 0x4f5054494f4e53l: - conn->type = OPTIONS; - break; - default: - return 1; - } - //This would actually be far nicer in HolyC of all - //languages. I feel like the context immediately - //following each magic number is enough. + conn->type = getType(line); line += i + 1; break; } diff --git a/src/include/connections.h b/src/include/connections.h index bb67261..0a8d693 100644 --- a/src/include/connections.h +++ b/src/include/connections.h @@ -27,21 +27,11 @@ typedef enum { SEND_RESPONSE, } ConnectionSteps; -typedef enum { - GET, - POST, - PUT, - HEAD, - DELETE, - PATCH, - OPTIONS, -} RequestTypes; - typedef struct Connection { int fd; ConnectionSteps progress; - RequestTypes type; + RequestType type; char *path; //ephemeral diff --git a/src/include/sitefile.h b/src/include/sitefile.h index 6ee5e3c..db8fcf7 100644 --- a/src/include/sitefile.h +++ b/src/include/sitefile.h @@ -17,9 +17,17 @@ */ #ifndef _HAVE_SITEFILE #define _HAVE_SITEFILE +#include + +typedef enum { + READ, +} Command; + typedef struct { - int argc; - char **argv; + RequestType respondto; + Command command; + char *path; + char *arg; } SiteCommand; typedef struct { diff --git a/src/main.c b/src/main.c index cc3c092..3247703 100644 --- a/src/main.c +++ b/src/main.c @@ -89,18 +89,18 @@ int main(int argc, char **argv) { fprintf(stderr, "No sitefile configured\n"); exit(EXIT_FAILURE); } - logs = fopen(logout, "a"); - if (logs == NULL) { - fprintf(stderr, "Couldn't open logs file %s\n", logout); - exit(EXIT_FAILURE); - } - Sitefile *site = parseFile(sitefile); if (site == NULL) { fprintf(stderr, "Invalid sitefile %s\n", sitefile); exit(EXIT_FAILURE); } + logs = fopen(logout, "a"); + if (logs == NULL) { + fprintf(stderr, "Couldn't open logs file %s\n", logout); + exit(EXIT_FAILURE); + } + int *pending = calloc(processes - 1, sizeof(int)); int *schedule = malloc(2 * sizeof(int)); if (schedule == NULL) @@ -114,6 +114,7 @@ int main(int argc, char **argv) { RunnerArgs *args = malloc(sizeof(RunnerArgs)); if (args == NULL) exit(EXIT_FAILURE); + args->site = site; args->pending = pending; args->schedule = schedule; args->id = i; diff --git a/src/responses.c b/src/responses.c index b61d289..72533ee 100644 --- a/src/responses.c +++ b/src/responses.c @@ -29,7 +29,11 @@ static int sendConn(int fd, char *str) { } int sendResponse(Connection *conn, Sitefile *site) { - printf("test\n"); + printf("test %d\n", site->size); + for (int i = 0; i < site->size; i++) { + printf("%s %s\n", site->content[i].path, site->content[i].arg); + } + sendConn(conn->fd, "HTTP/1.1 200 OK\r\n"); sendConn(conn->fd, "Content-Type: text/html\r\n"); sendConn(conn->fd, "Content-Length: 16\r\n"); diff --git a/src/sitefile.c b/src/sitefile.c index 3ba0083..8c43af8 100644 --- a/src/sitefile.c +++ b/src/sitefile.c @@ -17,8 +17,10 @@ */ #include #include +#include #include +#include #include typedef enum { @@ -93,6 +95,13 @@ static ReturnCode getToken(FILE *file, char **ret) { goto gotToken; goto error; } + if (len >= allocatedLen) { + allocatedLen *= 2; + char *newret = realloc(*ret, allocatedLen); + if (newret == NULL) + goto error; + *ret = newret; + } (*ret)[len] = c; } gotToken: @@ -111,6 +120,14 @@ static ReturnCode getCommand(FILE *file, int *argcret, char ***argvret) { int allocatedTokens = 5; char **argv = malloc(allocatedTokens * sizeof(char *)); for (;;) { + if (argc >= allocatedTokens) { + allocatedTokens *= 2; + char **newargv = realloc(argv, + allocatedTokens * sizeof(char *)); + if (newargv == NULL) + goto error; + argv = newargv; + } ReturnCode code = getToken(file, argv + argc); switch (code) { @@ -131,14 +148,6 @@ static ReturnCode getCommand(FILE *file, int *argcret, char ***argvret) { return SUCCESS; case SUCCESS: argc++; - if (argc >= allocatedTokens) { - allocatedTokens *= 2; - char **newargv = realloc(*argv, - allocatedTokens * sizeof(char *)); - if (newargv == NULL) - goto error; - argv = newargv; - } break; } } @@ -147,6 +156,15 @@ error: return ERROR; } +static char *copyString(char *str) { + size_t len = strlen(str); + char *ret = malloc(len + 1); + if (ret == NULL) + return NULL; + memcpy(ret, str, len + 1); + return ret; +} + Sitefile *parseFile(char *path) { FILE *file = fopen(path, "r"); if (file == NULL) @@ -161,19 +179,32 @@ Sitefile *parseFile(char *path) { free(ret); return NULL; } + RequestType respondto = GET; + int argc; + char **argv; for (;;) { - int argc; - char **argv; ReturnCode status = getCommand(file, &argc, &argv); switch (status) { case FILE_END: fclose(file); return ret; case ERROR: case LINE_END: - goto error; + goto nterror; case SUCCESS: break; } + if (strcmp(argv[0], "set") == 0) { + if (argc < 3) + goto error; + if (strcmp(argv[1], "respondto") == 0) { + respondto = getType(argv[2]); + if (respondto == INVALID) + goto error; + } + else + goto error; + continue; + } if (ret->size >= allocatedLength) { allocatedLength *= 2; SiteCommand *newcontent = realloc(ret->content, @@ -182,17 +213,35 @@ Sitefile *parseFile(char *path) { goto error; ret->content = newcontent; } - ret->content[ret->size].argc = argc; - ret->content[ret->size].argv = argv; + if (argc < 3) + goto error; + + ret->content[ret->size].path = copyString(argv[1]); + if (ret->content[ret->size].path == NULL) + goto error; + + if (strcmp(argv[0], "read") == 0) { + ret->content[ret->size].arg = copyString(argv[2]); + if (ret->content[ret->size].arg == NULL) + goto error; + } + else + goto error; + freeTokens(argc, argv); + ret->size++; } error: + freeTokens(argc, argv); +nterror: freeSitefile(ret); return NULL; } void freeSitefile(Sitefile *site) { - for (long i = 0; i < site->size; i++) - freeTokens(site->content[i].argc, site->content[i].argv); + for (long i = 0; i < site->size; i++) { + free(site->content[i].path); + free(site->content[i].arg); + } free(site->content); free(site); }