diff --git a/Makefile b/Makefile index cd46336..8393bfc 100644 --- a/Makefile +++ b/Makefile @@ -4,6 +4,10 @@ CC_FLAGS ?= CC_FLAGS := ${CC_FLAGS} -Ilib DESTDIR ?= / +ifndef NO_GC +CC_FLAGS := ${CC_FLAGS} -lgc +endif + .PHONY: all debug clean build-release install all: @$(shell mkdir -p bin) diff --git a/bin/httpsrv b/bin/httpsrv deleted file mode 100755 index abbe841..0000000 Binary files a/bin/httpsrv and /dev/null differ diff --git a/lib/file.h b/lib/file.h index 848ade6..fbbb0d3 100644 --- a/lib/file.h +++ b/lib/file.h @@ -3,6 +3,7 @@ #include #include #include +#include char* readFile(char* path) { path++; @@ -22,11 +23,10 @@ char* readFile(char* path) { int length = ftell(fp); rewind(fp); - buf = (char*)malloc(sizeof(char) * (length + 1)); + buf = (char*)GC_MALLOC(sizeof(char) * (length + 1)); fread(buf, sizeof(char), length, fp); buf[length] = '\0'; char* tmp = buf; - free(buf); return tmp; } diff --git a/lib/log.h b/lib/log.h index 0ada7e9..e35af6d 100644 --- a/lib/log.h +++ b/lib/log.h @@ -2,50 +2,47 @@ #include #include #include +#include #include "color.h" void info(char* ustr, ...) { char* tstr = GRN "INFO: " CRESET; - char* str = malloc(strlen(tstr) + strlen(ustr) + 1); + char* str = GC_MALLOC(strlen(tstr) + strlen(ustr) + 1); sprintf(str, "%s%s", tstr, ustr); //strcat(str, ustr); va_list lst; va_start(lst, ustr); vprintf(str, lst); - free(str); } void warn(char* ustr, ...) { char* tstr = YEL "WARN: " CRESET; - char* str = malloc(strlen(tstr) + strlen(ustr) + 1); + char* str = GC_MALLOC(strlen(tstr) + strlen(ustr) + 1); sprintf(str, "%s%s", tstr, ustr); va_list lst; va_start(lst, ustr); vprintf(str, lst); - free(str); } void error(char* ustr, ...) { char* tstr = RED "ERR: " CRESET; - char* str = malloc(strlen(tstr) + strlen(ustr) + 1); + char* str = GC_MALLOC(strlen(tstr) + strlen(ustr) + 1); sprintf(str, "%s%s", tstr, ustr); va_list lst; va_start(lst, ustr); vprintf(str, lst); - free(str); } void reqlog(char* ustr, ...) { char* tstr = WHT "REQ: " CRESET; - char* str = malloc(strlen(tstr) + strlen(ustr) + 1); + char* str = GC_MALLOC(strlen(tstr) + strlen(ustr) + 1); sprintf(str, "%s%s", tstr, ustr); va_list lst; va_start(lst, ustr); vprintf(str, lst); - free(str); } diff --git a/src/main.c b/src/main.c index 9c2deea..115bc30 100644 --- a/src/main.c +++ b/src/main.c @@ -1,6 +1,7 @@ #define _GNU_SOURCE #include #include +#include #include "srv.c" @@ -17,6 +18,7 @@ void help(char* exe) { } int main(int argc, char** argv) { + GC_INIT(); char* ADDR = DEFAULT_ADDR; int PORT = DEFAULT_PORT; diff --git a/src/srv.c b/src/srv.c index d5f4204..1dd092f 100644 --- a/src/srv.c +++ b/src/srv.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -7,12 +8,20 @@ #include #include #include +#include #include "resHeader.h" #include "reqHeader.h" #include "file.h" #include "log.h" + +void safeExit(int sig) { + info("Exiting...\n"); + GC_gcollect(); + exit(0); +} + int srv(char* ADDR, int PORT) { int srvSocket; struct sockaddr_in srvAddr; @@ -37,6 +46,8 @@ int srv(char* ADDR, int PORT) { return 1; } + signal(SIGINT, safeExit); + info("Server (probably) listening on http://%s:%d\n", ADDR, PORT); while(1) { @@ -93,11 +104,10 @@ funy: { reqlog("%d %s\n", rawHeader.status, request.path); char tmpHeader[1024]; makeHeader(tmpHeader, &rawHeader); - char* resHeader = malloc(strlen(tmpHeader) + strlen(file) + 1); //= "HTTP/1.1 200 OK\r\n\nHello, world!"; + char* resHeader = GC_MALLOC(strlen(tmpHeader) + strlen(file) + 1); //= "HTTP/1.1 200 OK\r\n\nHello, world!"; sprintf(resHeader, "%s%s", (char*)tmpHeader, file); send(client, resHeader, strlen(resHeader), 0); close(client); - free(resHeader); } } }