Improved sitefiles

This commit is contained in:
Nate Choe
2022-01-22 03:55:44 -06:00
parent ad64e4ecb2
commit 55da97269b
7 changed files with 93 additions and 73 deletions

View File

@@ -18,6 +18,7 @@ sitefiles also allow comments with #
# Part 3: Variables # Part 3: Variables
* ```type```: - The type of http request to respond to. One of: * ```respondto```: - The type of http request to respond to. One of:
* get * GET
* post * POST
Default: GET

View File

@@ -26,6 +26,7 @@
#include <unistd.h> #include <unistd.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <util.h>
#include <runner.h> #include <runner.h>
#include <sitefile.h> #include <sitefile.h>
#include <responses.h> #include <responses.h>
@@ -80,41 +81,7 @@ static int processRequest(Connection *conn) {
for (int i = 0;; i++) { for (int i = 0;; i++) {
if (line[i] == ' ') { if (line[i] == ' ') {
line[i] = '\0'; line[i] = '\0';
if (i >= 8) conn->type = getType(line);
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.
line += i + 1; line += i + 1;
break; break;
} }

View File

@@ -27,21 +27,11 @@ typedef enum {
SEND_RESPONSE, SEND_RESPONSE,
} ConnectionSteps; } ConnectionSteps;
typedef enum {
GET,
POST,
PUT,
HEAD,
DELETE,
PATCH,
OPTIONS,
} RequestTypes;
typedef struct Connection { typedef struct Connection {
int fd; int fd;
ConnectionSteps progress; ConnectionSteps progress;
RequestTypes type; RequestType type;
char *path; char *path;
//ephemeral //ephemeral

View File

@@ -17,9 +17,17 @@
*/ */
#ifndef _HAVE_SITEFILE #ifndef _HAVE_SITEFILE
#define _HAVE_SITEFILE #define _HAVE_SITEFILE
#include <util.h>
typedef enum {
READ,
} Command;
typedef struct { typedef struct {
int argc; RequestType respondto;
char **argv; Command command;
char *path;
char *arg;
} SiteCommand; } SiteCommand;
typedef struct { typedef struct {

View File

@@ -89,18 +89,18 @@ int main(int argc, char **argv) {
fprintf(stderr, "No sitefile configured\n"); fprintf(stderr, "No sitefile configured\n");
exit(EXIT_FAILURE); 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); Sitefile *site = parseFile(sitefile);
if (site == NULL) { if (site == NULL) {
fprintf(stderr, "Invalid sitefile %s\n", sitefile); fprintf(stderr, "Invalid sitefile %s\n", sitefile);
exit(EXIT_FAILURE); 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 *pending = calloc(processes - 1, sizeof(int));
int *schedule = malloc(2 * sizeof(int)); int *schedule = malloc(2 * sizeof(int));
if (schedule == NULL) if (schedule == NULL)
@@ -114,6 +114,7 @@ int main(int argc, char **argv) {
RunnerArgs *args = malloc(sizeof(RunnerArgs)); RunnerArgs *args = malloc(sizeof(RunnerArgs));
if (args == NULL) if (args == NULL)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
args->site = site;
args->pending = pending; args->pending = pending;
args->schedule = schedule; args->schedule = schedule;
args->id = i; args->id = i;

View File

@@ -29,7 +29,11 @@ static int sendConn(int fd, char *str) {
} }
int sendResponse(Connection *conn, Sitefile *site) { 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, "HTTP/1.1 200 OK\r\n");
sendConn(conn->fd, "Content-Type: text/html\r\n"); sendConn(conn->fd, "Content-Type: text/html\r\n");
sendConn(conn->fd, "Content-Length: 16\r\n"); sendConn(conn->fd, "Content-Length: 16\r\n");

View File

@@ -17,8 +17,10 @@
*/ */
#include <ctype.h> #include <ctype.h>
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <util.h>
#include <sitefile.h> #include <sitefile.h>
typedef enum { typedef enum {
@@ -93,6 +95,13 @@ static ReturnCode getToken(FILE *file, char **ret) {
goto gotToken; goto gotToken;
goto error; goto error;
} }
if (len >= allocatedLen) {
allocatedLen *= 2;
char *newret = realloc(*ret, allocatedLen);
if (newret == NULL)
goto error;
*ret = newret;
}
(*ret)[len] = c; (*ret)[len] = c;
} }
gotToken: gotToken:
@@ -111,6 +120,14 @@ static ReturnCode getCommand(FILE *file, int *argcret, char ***argvret) {
int allocatedTokens = 5; int allocatedTokens = 5;
char **argv = malloc(allocatedTokens * sizeof(char *)); char **argv = malloc(allocatedTokens * sizeof(char *));
for (;;) { 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); ReturnCode code = getToken(file, argv + argc);
switch (code) { switch (code) {
@@ -131,14 +148,6 @@ static ReturnCode getCommand(FILE *file, int *argcret, char ***argvret) {
return SUCCESS; return SUCCESS;
case SUCCESS: case SUCCESS:
argc++; argc++;
if (argc >= allocatedTokens) {
allocatedTokens *= 2;
char **newargv = realloc(*argv,
allocatedTokens * sizeof(char *));
if (newargv == NULL)
goto error;
argv = newargv;
}
break; break;
} }
} }
@@ -147,6 +156,15 @@ error:
return 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) { Sitefile *parseFile(char *path) {
FILE *file = fopen(path, "r"); FILE *file = fopen(path, "r");
if (file == NULL) if (file == NULL)
@@ -161,19 +179,32 @@ Sitefile *parseFile(char *path) {
free(ret); free(ret);
return NULL; return NULL;
} }
for (;;) { RequestType respondto = GET;
int argc; int argc;
char **argv; char **argv;
for (;;) {
ReturnCode status = getCommand(file, &argc, &argv); ReturnCode status = getCommand(file, &argc, &argv);
switch (status) { switch (status) {
case FILE_END: case FILE_END:
fclose(file); fclose(file);
return ret; return ret;
case ERROR: case LINE_END: case ERROR: case LINE_END:
goto error; goto nterror;
case SUCCESS: case SUCCESS:
break; 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) { if (ret->size >= allocatedLength) {
allocatedLength *= 2; allocatedLength *= 2;
SiteCommand *newcontent = realloc(ret->content, SiteCommand *newcontent = realloc(ret->content,
@@ -182,17 +213,35 @@ Sitefile *parseFile(char *path) {
goto error; goto error;
ret->content = newcontent; ret->content = newcontent;
} }
ret->content[ret->size].argc = argc; if (argc < 3)
ret->content[ret->size].argv = argv; 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: error:
freeTokens(argc, argv);
nterror:
freeSitefile(ret); freeSitefile(ret);
return NULL; return NULL;
} }
void freeSitefile(Sitefile *site) { void freeSitefile(Sitefile *site) {
for (long i = 0; i < site->size; i++) for (long i = 0; i < site->size; i++) {
freeTokens(site->content[i].argc, site->content[i].argv); free(site->content[i].path);
free(site->content[i].arg);
}
free(site->content); free(site->content);
free(site); free(site);
} }