From efd1ac1a0cefa2efaf491943d825bbe36db8c477 Mon Sep 17 00:00:00 2001 From: Nate Choe Date: Sun, 30 Jan 2022 21:26:12 -0600 Subject: [PATCH] Fixed a bug where the entire thing just didn't work at all --- example/logs | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/responses.c | 24 +++++++++++++----------- 2 files changed, 58 insertions(+), 11 deletions(-) create mode 100644 example/logs diff --git a/example/logs b/example/logs new file mode 100644 index 0000000..7e2e489 --- /dev/null +++ b/example/logs @@ -0,0 +1,45 @@ +[2022-01-31T03:15:10Z] Couldn't find swebs user +[2022-01-31T03:15:10Z] swebs started +[2022-01-31T03:15:42Z] Couldn't find swebs user +[2022-01-31T03:15:42Z] swebs started +[2022-01-31T03:16:33Z] Couldn't find swebs user +[2022-01-31T03:16:33Z] swebs started +[2022-01-31T03:16:57Z] Couldn't find swebs user +[2022-01-31T03:16:57Z] swebs started +[2022-01-31T03:16:58Z] Accepting a stream failed +[2022-01-31T03:16:58Z] Accepting a stream failed +[2022-01-31T03:16:58Z] Accepting a stream failed +[2022-01-31T03:16:58Z] Accepting a stream failed +[2022-01-31T03:16:58Z] Accepting a stream failed +[2022-01-31T03:16:58Z] Accepting a stream failed +[2022-01-31T03:16:58Z] Accepting a stream failed +[2022-01-31T03:16:58Z] Accepting a stream failed +[2022-01-31T03:16:58Z] Accepting a stream failed +[2022-01-31T03:16:58Z] Accepting a stream failed +[2022-01-31T03:17:00Z] Accepting a stream failed +[2022-01-31T03:17:32Z] Couldn't find swebs user +[2022-01-31T03:17:32Z] swebs started +[2022-01-31T03:17:42Z] Couldn't find swebs user +[2022-01-31T03:17:42Z] swebs started +[2022-01-31T03:17:55Z] Couldn't find swebs user +[2022-01-31T03:17:55Z] swebs started +[2022-01-31T03:18:02Z] Couldn't find swebs user +[2022-01-31T03:18:02Z] swebs started +[2022-01-31T03:19:05Z] Couldn't find swebs user +[2022-01-31T03:19:05Z] swebs started +[2022-01-31T03:19:24Z] Couldn't find swebs user +[2022-01-31T03:19:24Z] swebs started +[2022-01-31T03:19:29Z] Couldn't find swebs user +[2022-01-31T03:19:29Z] swebs started +[2022-01-31T03:20:29Z] Couldn't find swebs user +[2022-01-31T03:20:29Z] swebs started +[2022-01-31T03:21:42Z] Couldn't find swebs user +[2022-01-31T03:21:42Z] swebs started +[2022-01-31T03:22:03Z] Couldn't find swebs user +[2022-01-31T03:22:03Z] swebs started +[2022-01-31T03:24:53Z] Couldn't find swebs user +[2022-01-31T03:24:53Z] swebs started +[2022-01-31T03:25:44Z] Couldn't find swebs user +[2022-01-31T03:25:44Z] swebs started +[2022-01-31T03:25:48Z] Couldn't find swebs user +[2022-01-31T03:25:48Z] swebs started diff --git a/src/responses.c b/src/responses.c index 6e5d80d..7f9bfa2 100644 --- a/src/responses.c +++ b/src/responses.c @@ -21,6 +21,7 @@ #include #include +#include #include #include #include @@ -29,7 +30,7 @@ #include static int readResponse(Connection *conn, char *path) { - FILE *file; + int fd = -1;; struct stat statbuf; if (stat(path, &statbuf)) { sendErrorResponse(conn, ERROR_404); @@ -82,18 +83,19 @@ static int readResponse(Connection *conn, char *path) { return -1; } - file = fopen(requestPath, "r"); + fd = open(requestPath, O_RDONLY); free(assembledPath); } else - file = fopen(path, "r"); - if (file == NULL) + fd = open(path, O_RDONLY); + if (fd < 0) goto forbidden; - fseek(file, 0, SEEK_END); - long len = ftell(file); - fseek(file, 0, SEEK_SET); + off_t len = lseek(fd, 0, SEEK_END); + if (len < 0) + goto error; + lseek(fd, 0, SEEK_SET); sendHeader(conn, CODE_200, len); - return fileno(file); + return fd; error: sendErrorResponse(conn, ERROR_500); return -1; @@ -130,9 +132,9 @@ int getResponse(Connection *conn, Sitefile *site) { int fd = -1; switch (site->content[i].command) { case READ: - return - fd = readResponse(conn, - site->content[i].arg); + fd = readResponse(conn, + site->content[i].arg); + break; default: sendErrorResponse(conn, ERROR_500); return 1;