Fixed a bug where the entire thing just didn't work at all

This commit is contained in:
Nate Choe
2022-01-30 21:26:12 -06:00
parent 1955176e1a
commit efd1ac1a0c
2 changed files with 58 additions and 11 deletions

45
example/logs Normal file
View File

@@ -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

View File

@@ -21,6 +21,7 @@
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <unistd.h>
#include <sys/stat.h>
@@ -29,7 +30,7 @@
#include <responseutil.h>
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);
break;
default:
sendErrorResponse(conn, ERROR_500);
return 1;