diff --git a/Makefile b/Makefile index e6b5f3e..b38f0c2 100644 --- a/Makefile +++ b/Makefile @@ -16,7 +16,7 @@ all: main main: @${CC} -o bin/httpsrv src/main.c ${CC_FLAGS} -debug: CC_FLAGS:=-g -O0 -v ${CC_FLAGS} +debug: CC_FLAGS:=-g -O0 -v -fsanitize=undefined -fsanitize=address ${CC_FLAGS} debug: all clean: diff --git a/bin/httpsrv b/bin/httpsrv index 0261de7..12b1a95 100755 Binary files a/bin/httpsrv and b/bin/httpsrv differ diff --git a/lib/reqHeader.h b/lib/reqHeader.h index c9d0ead..b3a6573 100644 --- a/lib/reqHeader.h +++ b/lib/reqHeader.h @@ -2,6 +2,7 @@ #include #include #include +#include #ifdef NO_GC #include #define malloc(n) GC_malloc(n) @@ -19,13 +20,14 @@ ReqHeader readReqHeader(int client) { char* buf = malloc(1024); read(client, buf, 1024); ReqHeader header; - header.method = malloc(1024); - header.path = malloc(1024); - char* tBuf = malloc(1024); - /*strcpy(header.method, strtok(buf, " ")); - strcpy(header.path, strtok(NULL, " "));*/ - sprintf(header.method, "%s", strtok_r(buf, " ", &tBuf)); - sprintf(header.path, "%s", strtok_r(NULL, " ", &tBuf)); + if (buf == NULL || !strcmp(buf, "")) { + errno = -1; + return header; + } + //header.method = malloc(1024); + //header.path = malloc(1024); + header.method = strtok(buf, " "); + header.path = strtok(NULL, " "); if (header.path[1] == '\0') { //strcpy(header.path, "/index.html"); sprintf(header.path, "/index.html"); diff --git a/src/srv.c b/src/srv.c index a4f2fbe..fe9ea87 100644 --- a/src/srv.c +++ b/src/srv.c @@ -21,22 +21,42 @@ #include "log.h" #include "mime.h" +int srvSocket; + void safeExit(int sig) { info("Exiting...\n"); #ifdef NO_GC GC_gcollect(); #endif + close(srvSocket); _exit(0); } +void segfault(int sig) { + warn("may or may not have segfaulted\n"); +#ifdef NO_GC + GC_gcollect(); +#endif +} + void *handle(void* arg) { int client = *(int*)arg; ReqHeader request = readReqHeader(client); ResHeader rawHeader; - rawHeader.protocol = request.protocol; - rawHeader.status = 200; - rawHeader.notice = "OK"; + if (errno == -1) { + warn("somehow we got a NULL request????\n"); + rawHeader.status = 500; + rawHeader.notice = "Internal Server Error"; + char* tmpHeader = malloc(1024); + makeHeader(tmpHeader, &rawHeader); + send(client, tmpHeader, strlen(tmpHeader), 0); + close(client); + return 0; + } else { + rawHeader.status = 200; + rawHeader.notice = "OK"; + } int useBinary = getMime(&rawHeader.mime, request.path); if (errno == -1) { @@ -67,7 +87,6 @@ void *handle(void* arg) { } int srv(char* ADDR, int PORT) { - int srvSocket; struct sockaddr_in srvAddr; srvSocket = socket(AF_INET, SOCK_STREAM, 0); @@ -91,6 +110,7 @@ int srv(char* ADDR, int PORT) { } signal(SIGINT, safeExit); + //signal(SIGSEGV, segfault); info("Server (probably) listening on http://%s:%d\n", ADDR, PORT);