Added dynamic pages

This commit is contained in:
Nate Choe
2022-01-31 22:35:46 -06:00
parent 1086edf6d0
commit 7c7460d37f
12 changed files with 1428 additions and 46 deletions

View File

@@ -14,6 +14,7 @@ sitefiles also allow comments with #
* ```set [variable] [value]``` - sets some local variable for the following pages
* ```define [variable] [value]``` - sets some global variable
* ```read [http path] [file path]``` - if the requested path matches ```[http path]```, return the contents of ```[file path]```. If [file path] is a directory, then the http path is appended to [file path] and that is read instead.
* ```exec [http path] [file path]``` - if the requested path matches ```[http path]```, execute ```[file path]``` and send the the contents of stdout. ```argv[0]``` is the ```[file path]```, ```argv[1]``` is a header, ```argv[2]``` is the value for that header, ```argv[3]``` is the next header, and so on.
##### Other than set, commands should take in a regex as argument 1 and operate on a file specified in argument 2.

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,3 @@
#!/bin/sh
echo "<p>By the way today's date is $(date)</p>"

View File

@@ -2,4 +2,5 @@
<h3>It's not very polished but it's certainly a website</h3>
<a href="blog/2021-1-25.html">2021-1-25</a>
<a href="blog/2022-1-25.html">2021-1-25</a>
<a href="blog/2022-1-31">2021-1-31</a>

View File

@@ -6,8 +6,10 @@ define timeout 2000
set host localhost:8000
read / site/index.html
read /hello site/hello.html
exec /blog/2022-1-31 site/blog/blog/2022-1-31.sh
read /blog/.* site/blog/
#/blog/2021-1-25.html turns into site/blog//blog/2021-1-25.html
exec /dynamic site/build-page.sh
set host 127.0.0.1:8000
read / site/easteregg.html
read /egg.png site/egg.png

View File

@@ -198,26 +198,7 @@ static int processChar(Connection *conn, char c, Sitefile *site) {
}
if (conn->progress == RECEIVE_BODY &&
conn->receivedBody >= conn->bodylen)
getResponse(conn, site);
if (conn->progress == SEND_RESPONSE) {
lseek(conn->fd, 0, SEEK_SET);
while (conn->len > 0) {
char buff[1024];
ssize_t readLen = read(conn->fd, buff, sizeof(buff));
if (readLen < 0)
return 1;
ssize_t leftToSend = readLen;
while (leftToSend > 0) {
size_t writeLen = sendStream(conn->stream,
buff, leftToSend);
if (writeLen < 0)
return 1;
leftToSend -= writeLen;
}
conn->len -= readLen;
}
resetConnection(conn);
}
sendResponse(conn, site);
return 0;
}

View File

@@ -25,7 +25,6 @@ typedef enum {
RECEIVE_REQUEST,
RECEIVE_HEADER,
RECEIVE_BODY,
SEND_RESPONSE,
} ConnectionSteps;
typedef struct {
@@ -54,10 +53,6 @@ typedef struct Connection {
size_t bodylen;
size_t receivedBody;
int fd;
size_t len;
//the fd and length of the response left to send
char *currLine;
//persistent
size_t currLineAlloc;

View File

@@ -20,6 +20,6 @@
#include <sitefile.h>
#include <connections.h>
int getResponse(Connection *conn, Sitefile *site);
int sendResponse(Connection *conn, Sitefile *site);
//returns 1 on error, sets conn->progress to SEND_RESPONSE and sets conn->fd
#endif

View File

@@ -23,6 +23,7 @@
typedef enum {
READ,
EXEC,
} Command;
typedef struct {

View File

@@ -25,16 +25,29 @@
#include <limits.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <responses.h>
#include <responseutil.h>
static int resilientSend(Stream *stream, char *buff, size_t len) {
//Will either write len bytes, or return 1 for error.
while (len > 0) {
ssize_t sent = sendStream(stream, buff, len);
if (sent < 0)
return 1;
len -= sent;
buff += sent;
}
return 0;
}
static int readResponse(Connection *conn, char *path) {
int fd = -1;;
int fd = 1;
struct stat statbuf;
if (stat(path, &statbuf)) {
sendErrorResponse(conn, ERROR_404);
return -1;
return 1;
}
if (S_ISDIR(statbuf.st_mode)) {
long reqPathLen = strlen(conn->path);
@@ -50,7 +63,7 @@ static int readResponse(Connection *conn, char *path) {
if (errno == ENOENT) {
free(assembledPath);
sendErrorResponse(conn, ERROR_404);
return -1;
return 1;
}
free(assembledPath);
goto error;
@@ -75,12 +88,12 @@ static int readResponse(Connection *conn, char *path) {
if (stat(requestPath, &requestbuf)) {
free(assembledPath);
sendErrorResponse(conn, ERROR_404);
return -1;
return 1;
}
if (S_ISDIR(requestbuf.st_mode)) {
free(assembledPath);
sendErrorResponse(conn, ERROR_400);
return -1;
return 1;
}
fd = open(requestPath, O_RDONLY);
@@ -96,17 +109,100 @@ static int readResponse(Connection *conn, char *path) {
if (lseek(fd, 0, SEEK_SET) < 0) {
sendErrorResponse(conn, ERROR_500);
close(fd);
return -1;
return 1;
}
sendHeader(conn, CODE_200, len);
conn->len = len;
return fd;
while (len > 0) {
char buff[1024];
size_t readLen = read(fd, buff, sizeof(buff));
if (resilientSend(conn->stream, buff, readLen))
goto error;
len -= readLen;
}
return 0;
error:
sendErrorResponse(conn, ERROR_500);
return -1;
return 1;
forbidden:
sendErrorResponse(conn, ERROR_403);
return -1;
return 1;
}
static int execResponse(Connection *conn, char *path) {
int output[2];
if (pipe(output))
goto error;
pid_t pid = fork();
if (pid < 0)
goto error;
if (pid == 0) {
close(1);
close(output[0]);
dup2(output[1], 1);
char **args = malloc((conn->fieldCount*2 + 2) * sizeof(char *));
if (args == NULL) {
close(output[1]);
exit(EXIT_FAILURE);
}
args[0] = path;
for (int i = 0; i < conn->fieldCount; i++) {
args[i * 2 + 1] = conn->fields[i].field;
args[i * 2 + 2] = conn->fields[i].value;
}
args[conn->fieldCount*2 + 1] = NULL;
if (execv(path, args) < 0) {
close(output[1]);
free(args);
exit(EXIT_FAILURE);
}
close(output[1]);
free(args);
exit(EXIT_SUCCESS);
}
size_t allocResponse = 1024;
size_t responseLen = 0;
char *response = malloc(allocResponse);
close(output[1]);
for (;;) {
if (responseLen == allocResponse) {
allocResponse *= 2;
char *newresponse = realloc(response, allocResponse);
if (newresponse == NULL)
goto looperror;
response = newresponse;
}
ssize_t len = read(output[0],
response + responseLen,
allocResponse - responseLen);
if (len < 0)
goto looperror;
if (len == 0)
break;
responseLen += len;
continue;
looperror:
close(output[0]);
int status;
waitpid(pid, &status, 0);
free(response);
goto error;
}
close(output[0]);
int status;
waitpid(pid, &status, 0);
sendHeader(conn, CODE_200, responseLen);
if (resilientSend(conn->stream, response, responseLen)) {
free(response);
return 1;
}
free(response);
return 0;
error:
sendErrorResponse(conn, ERROR_500);
return 1;
}
static int fullmatch(regex_t *regex, char *str) {
@@ -116,7 +212,7 @@ static int fullmatch(regex_t *regex, char *str) {
return match.rm_so != 0 || match.rm_eo != strlen(str);
}
int getResponse(Connection *conn, Sitefile *site) {
int sendResponse(Connection *conn, Sitefile *site) {
char *host = NULL;
for (int i = 0; i < conn->fieldCount; i++) {
if (strcmp(conn->fields[i].field, "Host") == 0) {
@@ -134,23 +230,22 @@ int getResponse(Connection *conn, Sitefile *site) {
if (fullmatch(&site->content[i].host, host))
continue;
if (fullmatch(&site->content[i].path, conn->path) == 0) {
int fd = -1;
switch (site->content[i].command) {
case READ:
fd = readResponse(conn,
readResponse(conn,
site->content[i].arg);
break;
case EXEC:
execResponse(conn,
site->content[i].arg);
default:
sendErrorResponse(conn, ERROR_500);
return 1;
}
if (fd == -1)
return 1;
conn->fd = fd;
conn->progress = SEND_RESPONSE;
resetConnection(conn);
return 0;
}
}
sendErrorResponse(conn, ERROR_404);
return -1;
return 1;
}

View File

@@ -258,6 +258,13 @@ Sitefile *parseSitefile(char *path) {
ret->content[ret->size].arg = copyString(argv[2]);
if (ret->content[ret->size].arg == NULL)
goto error;
ret->content[ret->size].command = READ;
}
else if (strcmp(argv[0], "exec") == 0) {
ret->content[ret->size].arg = copyString(argv[2]);
if (ret->content[ret->size].arg == NULL)
goto error;
ret->content[ret->size].command = EXEC;
}
else
goto error;