Added dynamic pages
This commit is contained in:
@@ -14,6 +14,7 @@ sitefiles also allow comments with #
|
|||||||
* ```set [variable] [value]``` - sets some local variable for the following pages
|
* ```set [variable] [value]``` - sets some local variable for the following pages
|
||||||
* ```define [variable] [value]``` - sets some global variable
|
* ```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.
|
* ```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.
|
##### Other than set, commands should take in a regex as argument 1 and operate on a file specified in argument 2.
|
||||||
|
|
||||||
|
|||||||
1296
example/logs
1296
example/logs
File diff suppressed because it is too large
Load Diff
3
example/site/blog/blog/2022-1-31.sh
Executable file
3
example/site/blog/blog/2022-1-31.sh
Executable file
@@ -0,0 +1,3 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
echo "<p>By the way today's date is $(date)</p>"
|
||||||
@@ -2,4 +2,5 @@
|
|||||||
|
|
||||||
<h3>It's not very polished but it's certainly a website</h3>
|
<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>
|
||||||
|
|||||||
@@ -6,8 +6,10 @@ define timeout 2000
|
|||||||
set host localhost:8000
|
set host localhost:8000
|
||||||
read / site/index.html
|
read / site/index.html
|
||||||
read /hello site/hello.html
|
read /hello site/hello.html
|
||||||
|
exec /blog/2022-1-31 site/blog/blog/2022-1-31.sh
|
||||||
read /blog/.* site/blog/
|
read /blog/.* site/blog/
|
||||||
#/blog/2021-1-25.html turns into site/blog//blog/2021-1-25.html
|
#/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
|
set host 127.0.0.1:8000
|
||||||
read / site/easteregg.html
|
read / site/easteregg.html
|
||||||
read /egg.png site/egg.png
|
read /egg.png site/egg.png
|
||||||
|
|||||||
@@ -198,26 +198,7 @@ static int processChar(Connection *conn, char c, Sitefile *site) {
|
|||||||
}
|
}
|
||||||
if (conn->progress == RECEIVE_BODY &&
|
if (conn->progress == RECEIVE_BODY &&
|
||||||
conn->receivedBody >= conn->bodylen)
|
conn->receivedBody >= conn->bodylen)
|
||||||
getResponse(conn, site);
|
sendResponse(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);
|
|
||||||
}
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -25,7 +25,6 @@ typedef enum {
|
|||||||
RECEIVE_REQUEST,
|
RECEIVE_REQUEST,
|
||||||
RECEIVE_HEADER,
|
RECEIVE_HEADER,
|
||||||
RECEIVE_BODY,
|
RECEIVE_BODY,
|
||||||
SEND_RESPONSE,
|
|
||||||
} ConnectionSteps;
|
} ConnectionSteps;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
@@ -54,10 +53,6 @@ typedef struct Connection {
|
|||||||
size_t bodylen;
|
size_t bodylen;
|
||||||
size_t receivedBody;
|
size_t receivedBody;
|
||||||
|
|
||||||
int fd;
|
|
||||||
size_t len;
|
|
||||||
//the fd and length of the response left to send
|
|
||||||
|
|
||||||
char *currLine;
|
char *currLine;
|
||||||
//persistent
|
//persistent
|
||||||
size_t currLineAlloc;
|
size_t currLineAlloc;
|
||||||
|
|||||||
@@ -20,6 +20,6 @@
|
|||||||
#include <sitefile.h>
|
#include <sitefile.h>
|
||||||
#include <connections.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
|
//returns 1 on error, sets conn->progress to SEND_RESPONSE and sets conn->fd
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -23,6 +23,7 @@
|
|||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
READ,
|
READ,
|
||||||
|
EXEC,
|
||||||
} Command;
|
} Command;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
|||||||
133
src/responses.c
133
src/responses.c
@@ -25,16 +25,29 @@
|
|||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
|
|
||||||
#include <responses.h>
|
#include <responses.h>
|
||||||
#include <responseutil.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) {
|
static int readResponse(Connection *conn, char *path) {
|
||||||
int fd = -1;;
|
int fd = 1;
|
||||||
struct stat statbuf;
|
struct stat statbuf;
|
||||||
if (stat(path, &statbuf)) {
|
if (stat(path, &statbuf)) {
|
||||||
sendErrorResponse(conn, ERROR_404);
|
sendErrorResponse(conn, ERROR_404);
|
||||||
return -1;
|
return 1;
|
||||||
}
|
}
|
||||||
if (S_ISDIR(statbuf.st_mode)) {
|
if (S_ISDIR(statbuf.st_mode)) {
|
||||||
long reqPathLen = strlen(conn->path);
|
long reqPathLen = strlen(conn->path);
|
||||||
@@ -50,7 +63,7 @@ static int readResponse(Connection *conn, char *path) {
|
|||||||
if (errno == ENOENT) {
|
if (errno == ENOENT) {
|
||||||
free(assembledPath);
|
free(assembledPath);
|
||||||
sendErrorResponse(conn, ERROR_404);
|
sendErrorResponse(conn, ERROR_404);
|
||||||
return -1;
|
return 1;
|
||||||
}
|
}
|
||||||
free(assembledPath);
|
free(assembledPath);
|
||||||
goto error;
|
goto error;
|
||||||
@@ -75,12 +88,12 @@ static int readResponse(Connection *conn, char *path) {
|
|||||||
if (stat(requestPath, &requestbuf)) {
|
if (stat(requestPath, &requestbuf)) {
|
||||||
free(assembledPath);
|
free(assembledPath);
|
||||||
sendErrorResponse(conn, ERROR_404);
|
sendErrorResponse(conn, ERROR_404);
|
||||||
return -1;
|
return 1;
|
||||||
}
|
}
|
||||||
if (S_ISDIR(requestbuf.st_mode)) {
|
if (S_ISDIR(requestbuf.st_mode)) {
|
||||||
free(assembledPath);
|
free(assembledPath);
|
||||||
sendErrorResponse(conn, ERROR_400);
|
sendErrorResponse(conn, ERROR_400);
|
||||||
return -1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
fd = open(requestPath, O_RDONLY);
|
fd = open(requestPath, O_RDONLY);
|
||||||
@@ -96,17 +109,100 @@ static int readResponse(Connection *conn, char *path) {
|
|||||||
if (lseek(fd, 0, SEEK_SET) < 0) {
|
if (lseek(fd, 0, SEEK_SET) < 0) {
|
||||||
sendErrorResponse(conn, ERROR_500);
|
sendErrorResponse(conn, ERROR_500);
|
||||||
close(fd);
|
close(fd);
|
||||||
return -1;
|
return 1;
|
||||||
}
|
}
|
||||||
sendHeader(conn, CODE_200, len);
|
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:
|
error:
|
||||||
sendErrorResponse(conn, ERROR_500);
|
sendErrorResponse(conn, ERROR_500);
|
||||||
return -1;
|
return 1;
|
||||||
forbidden:
|
forbidden:
|
||||||
sendErrorResponse(conn, ERROR_403);
|
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) {
|
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);
|
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;
|
char *host = NULL;
|
||||||
for (int i = 0; i < conn->fieldCount; i++) {
|
for (int i = 0; i < conn->fieldCount; i++) {
|
||||||
if (strcmp(conn->fields[i].field, "Host") == 0) {
|
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))
|
if (fullmatch(&site->content[i].host, host))
|
||||||
continue;
|
continue;
|
||||||
if (fullmatch(&site->content[i].path, conn->path) == 0) {
|
if (fullmatch(&site->content[i].path, conn->path) == 0) {
|
||||||
int fd = -1;
|
|
||||||
switch (site->content[i].command) {
|
switch (site->content[i].command) {
|
||||||
case READ:
|
case READ:
|
||||||
fd = readResponse(conn,
|
readResponse(conn,
|
||||||
site->content[i].arg);
|
site->content[i].arg);
|
||||||
break;
|
break;
|
||||||
|
case EXEC:
|
||||||
|
execResponse(conn,
|
||||||
|
site->content[i].arg);
|
||||||
default:
|
default:
|
||||||
sendErrorResponse(conn, ERROR_500);
|
sendErrorResponse(conn, ERROR_500);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
if (fd == -1)
|
resetConnection(conn);
|
||||||
return 1;
|
|
||||||
conn->fd = fd;
|
|
||||||
conn->progress = SEND_RESPONSE;
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sendErrorResponse(conn, ERROR_404);
|
sendErrorResponse(conn, ERROR_404);
|
||||||
return -1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -258,6 +258,13 @@ Sitefile *parseSitefile(char *path) {
|
|||||||
ret->content[ret->size].arg = copyString(argv[2]);
|
ret->content[ret->size].arg = copyString(argv[2]);
|
||||||
if (ret->content[ret->size].arg == NULL)
|
if (ret->content[ret->size].arg == NULL)
|
||||||
goto error;
|
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
|
else
|
||||||
goto error;
|
goto error;
|
||||||
|
|||||||
Reference in New Issue
Block a user