Added dynamic pages
This commit is contained in:
133
src/responses.c
133
src/responses.c
@@ -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,
|
||||
site->content[i].arg);
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user