diff --git a/bin/httpsrv b/bin/httpsrv index 63fe9fe..606e68f 100755 Binary files a/bin/httpsrv and b/bin/httpsrv differ diff --git a/lib/color.h b/lib/color.h index 3362e12..e0c9e1b 100644 --- a/lib/color.h +++ b/lib/color.h @@ -1,3 +1,4 @@ +#pragma once /* * This is free and unencumbered software released into the public domain. * diff --git a/lib/file.h b/lib/file.h index 85e2701..e8beffd 100644 --- a/lib/file.h +++ b/lib/file.h @@ -1,3 +1,4 @@ +#pragma once #include #include #include diff --git a/lib/log.h b/lib/log.h index a62b19c..0108905 100644 --- a/lib/log.h +++ b/lib/log.h @@ -1,3 +1,4 @@ +#pragma once #include #include #include diff --git a/lib/mime.h b/lib/mime.h new file mode 100644 index 0000000..e618ed5 --- /dev/null +++ b/lib/mime.h @@ -0,0 +1,53 @@ +#pragma once +#include +#ifdef NO_GC +#include +#define malloc(n) GC_malloc(n) +#else +#include +#endif + +#include "log.h" + +int hasTriedIndex; + +int getMime(char** mime, char* filename) { + char* dot = strrchr(filename, '.'); + + if (!dot || dot == filename) { + if (!hasTriedIndex) { + hasTriedIndex++; + strcat(filename, "/index.html"); + info("trying to upgrade directory to index.html"); + return getMime(mime, filename); + } else { + warn("file extension does not exist or is the same as request's path.\n"); + hasTriedIndex = 1; + *mime = "text/plain"; + return 0; + } + } else { + if (!strcmp(dot, ".html")) { + *mime = "text/html"; + } else if (!strcmp(dot, ".css")) { + *mime = "text/css"; + } else if (!strcmp(dot, ".js")) { + *mime = "text/javascript"; + } else if (!strcmp(dot, ".woff2")) { + *mime = "font/woff2"; + return 1; + } else if (!strcmp(dot, ".ico")) { + *mime = "image/vnd.microsoft.icon"; + return 1; + } else { + warn("mimetype for %s not found\n", filename); + *mime = "text/plain"; + } + if (hasTriedIndex == 1) { + hasTriedIndex = 2; + } else { + hasTriedIndex = 0; + } + } + return 0; +} diff --git a/lib/reqHeader.h b/lib/reqHeader.h index 2b7cfc2..0ffecfb 100644 --- a/lib/reqHeader.h +++ b/lib/reqHeader.h @@ -1,3 +1,4 @@ +#pragma once #include #include #include diff --git a/lib/resHeader.h b/lib/resHeader.h index 6b66d7d..7178afb 100644 --- a/lib/resHeader.h +++ b/lib/resHeader.h @@ -1,3 +1,4 @@ +#pragma once #include typedef struct { diff --git a/src/srv.c b/src/srv.c index 39916c9..0a57a1b 100644 --- a/src/srv.c +++ b/src/srv.c @@ -19,7 +19,7 @@ #include "reqHeader.h" #include "file.h" #include "log.h" - +#include "mime.h" void safeExit(int sig) { info("Exiting...\n"); @@ -32,51 +32,13 @@ void safeExit(int sig) { void *handle(void* arg) { int client = *(int*)arg; - - int useBinary = 0; - int hasTriedIndex; ReqHeader request = readReqHeader(client); -funy: { - char* dot = strrchr(request.path, '.'); - ResHeader rawHeader; rawHeader.protocol = request.protocol; rawHeader.status = 200; rawHeader.notice = "OK"; - 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")) { - rawHeader.mime = "text/css"; - } else if (!strcmp(dot, ".ico")) { - rawHeader.mime = "image/vnd.microsoft.icon"; - useBinary = 1; - } else if (!strcmp(dot, ".js")) { - rawHeader.mime = "text/javascript"; - } else if (!strcmp(dot, ".woff2")) { - rawHeader.mime = "font/woff2"; - useBinary = 1; - } else { - warn("mimetype for %s not found\n", request.path); - rawHeader.mime = "text/plain"; - } - if (hasTriedIndex == 1) { - hasTriedIndex = 2; - } else { - hasTriedIndex = 0; - } - } + int useBinary = getMime(&rawHeader.mime, request.path); + if (errno == -1) { rawHeader.status = 404; rawHeader.notice = "Not Found"; @@ -104,7 +66,6 @@ funy: { //sprintf(resHeader, "%s%s", (char*)tmpHeader, file); //send(client, resHeader, strlen(resHeader), 0); close(client); - } return 0; }