Improved sitefiles
This commit is contained in:
@@ -26,6 +26,7 @@
|
||||
#include <unistd.h>
|
||||
#include <sys/socket.h>
|
||||
|
||||
#include <util.h>
|
||||
#include <runner.h>
|
||||
#include <sitefile.h>
|
||||
#include <responses.h>
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -17,9 +17,17 @@
|
||||
*/
|
||||
#ifndef _HAVE_SITEFILE
|
||||
#define _HAVE_SITEFILE
|
||||
#include <util.h>
|
||||
|
||||
typedef enum {
|
||||
READ,
|
||||
} Command;
|
||||
|
||||
typedef struct {
|
||||
int argc;
|
||||
char **argv;
|
||||
RequestType respondto;
|
||||
Command command;
|
||||
char *path;
|
||||
char *arg;
|
||||
} SiteCommand;
|
||||
|
||||
typedef struct {
|
||||
|
||||
13
src/main.c
13
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;
|
||||
|
||||
@@ -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");
|
||||
|
||||
@@ -17,8 +17,10 @@
|
||||
*/
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <util.h>
|
||||
#include <sitefile.h>
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user