diff --git a/bin/httpsrv b/bin/httpsrv index bd95929..ff7a87b 100755 Binary files a/bin/httpsrv and b/bin/httpsrv differ diff --git a/lib/color.h b/lib/color.h new file mode 100644 index 0000000..3362e12 --- /dev/null +++ b/lib/color.h @@ -0,0 +1,80 @@ +/* + * This is free and unencumbered software released into the public domain. + * + * For more information, please refer to + */ + +//Regular text +#define BLK "\e[0;30m" +#define RED "\e[0;31m" +#define GRN "\e[0;32m" +#define YEL "\e[0;33m" +#define BLU "\e[0;34m" +#define MAG "\e[0;35m" +#define CYN "\e[0;36m" +#define WHT "\e[0;37m" + +//Regular bold text +#define BBLK "\e[1;30m" +#define BRED "\e[1;31m" +#define BGRN "\e[1;32m" +#define BYEL "\e[1;33m" +#define BBLU "\e[1;34m" +#define BMAG "\e[1;35m" +#define BCYN "\e[1;36m" +#define BWHT "\e[1;37m" + +//Regular underline text +#define UBLK "\e[4;30m" +#define URED "\e[4;31m" +#define UGRN "\e[4;32m" +#define UYEL "\e[4;33m" +#define UBLU "\e[4;34m" +#define UMAG "\e[4;35m" +#define UCYN "\e[4;36m" +#define UWHT "\e[4;37m" + +//Regular background +#define BLKB "\e[40m" +#define REDB "\e[41m" +#define GRNB "\e[42m" +#define YELB "\e[43m" +#define BLUB "\e[44m" +#define MAGB "\e[45m" +#define CYNB "\e[46m" +#define WHTB "\e[47m" + +//High intensty background +#define BLKHB "\e[0;100m" +#define REDHB "\e[0;101m" +#define GRNHB "\e[0;102m" +#define YELHB "\e[0;103m" +#define BLUHB "\e[0;104m" +#define MAGHB "\e[0;105m" +#define CYNHB "\e[0;106m" +#define WHTHB "\e[0;107m" + +//High intensty text +#define HBLK "\e[0;90m" +#define HRED "\e[0;91m" +#define HGRN "\e[0;92m" +#define HYEL "\e[0;93m" +#define HBLU "\e[0;94m" +#define HMAG "\e[0;95m" +#define HCYN "\e[0;96m" +#define HWHT "\e[0;97m" + +//Bold high intensity text +#define BHBLK "\e[1;90m" +#define BHRED "\e[1;91m" +#define BHGRN "\e[1;92m" +#define BHYEL "\e[1;93m" +#define BHBLU "\e[1;94m" +#define BHMAG "\e[1;95m" +#define BHCYN "\e[1;96m" +#define BHWHT "\e[1;97m" + +//Reset +#define reset "\e[0m" +#define CRESET "\e[0m" +#define COLOR_RESET "\e[0m" diff --git a/lib/log.h b/lib/log.h new file mode 100644 index 0000000..e992a98 --- /dev/null +++ b/lib/log.h @@ -0,0 +1,47 @@ +#include +#include +#include +#include + +#include "color.h" + +void info(char* ustr, ...) { + char* tstr = GRN "INFO: " CRESET; + char* str = malloc(strlen(tstr) + strlen(ustr)); + sprintf(str, "%s%s", tstr, ustr); + //strcat(str, ustr); + + va_list lst; + va_start(lst, ustr); + vprintf(str, lst); +} + +void warn(char* ustr, ...) { + char* tstr = YEL "WARN: " CRESET; + char* str = malloc(strlen(tstr) + strlen(ustr)); + sprintf(str, "%s%s", tstr, ustr); + + va_list lst; + va_start(lst, ustr); + vprintf(str, lst); +} + +void error(char* ustr, ...) { + char* tstr = RED "ERR: " CRESET; + char* str = malloc(strlen(tstr) + strlen(ustr)); + sprintf(str, "%s%s", tstr, ustr); + + va_list lst; + va_start(lst, ustr); + vprintf(str, lst); +} + +void reqlog(char* ustr, ...) { + char* tstr = WHT "REQ: " CRESET; + char* str = malloc(strlen(tstr) + strlen(ustr)); + sprintf(str, "%s%s", tstr, ustr); + + va_list lst; + va_start(lst, ustr); + vprintf(str, lst); +} diff --git a/src/srv.c b/src/srv.c index d8d8ee8..9b08f32 100644 --- a/src/srv.c +++ b/src/srv.c @@ -11,6 +11,7 @@ #include "resHeader.h" #include "reqHeader.h" #include "file.h" +#include "log.h" int srv(char* ADDR, int PORT) { char buffer[BUFSIZ]; @@ -28,29 +29,41 @@ int srv(char* ADDR, int PORT) { (struct sockaddr*) &srvAddr, sizeof(srvAddr) ) < 0) { - printf("Error: Failed to bind to %s:%d.\n", ADDR, PORT); + error("Failed to bind to %s:%d.\n", ADDR, PORT); return 1; } if (listen(srvSocket, 10) < 0) { - printf("Error: Unable to listen on %s:%d.\n", ADDR, PORT); + error("Unable to listen on %s:%d.\n", ADDR, PORT); return 1; } - printf("Server (probably) listening on http://%s:%d\n", ADDR, PORT); + info("Server (probably) listening on http://%s:%d\n", ADDR, PORT); while(1) { + int hasTriedIndex; int client = accept(srvSocket, NULL, NULL); ReqHeader request = readReqHeader(client); - char* tPath = request.path; - char* dot = strrchr(tPath, '.'); +funy: { + char* dot = strrchr(request.path, '.'); char *file = readFile(request.path); ResHeader rawHeader; rawHeader.protocol = request.protocol; rawHeader.status = 200; rawHeader.notice = "OK"; - if (!dot || dot == tPath) { rawHeader.mime = "text/html"; } else { + if (!dot || dot == request.path) { + if (!hasTriedIndex) { + strcat(request.path, "/index.html"); + info("trying to upgrade directory to index.html\n"); + hasTriedIndex = 1; + goto funy; + } else { + warn("file extension does not exist or is the same as request path\n"); + hasTriedIndex = 1; + rawHeader.mime = "text/plain"; + } + } else { if (!strcmp(dot, ".html")) { rawHeader.mime = "text/html"; } else if (!strcmp(dot, ".css")) { @@ -60,18 +73,25 @@ int srv(char* ADDR, int PORT) { } else if (!strcmp(dot, ".js")) { rawHeader.mime = "text/javascript"; } else { + warn("mimetype for %s not found\n", request.path); rawHeader.mime = "text/plain"; } + if (hasTriedIndex == 1) { + hasTriedIndex = 2; + } else { + hasTriedIndex = 0; + } } if (errno == -1) { rawHeader.status = 404; rawHeader.notice = "Not Found"; - } else if (errno > 0) { + } else if (errno > 0 && !(hasTriedIndex == 2)) { rawHeader.status = 500; rawHeader.notice = "Internal Server Error"; } errno = 0; - printf("%d %s\n", rawHeader.status, request.path); + hasTriedIndex = 0; + reqlog("%d %s\n", rawHeader.status, request.path); char tmpHeader[1024]; makeHeader(tmpHeader, &rawHeader); char resHeader[1024]; //= "HTTP/1.1 200 OK\r\n\nHello, world!"; @@ -79,4 +99,5 @@ int srv(char* ADDR, int PORT) { send(client, resHeader, strlen(resHeader), 0); close(client); } + } }