diff --git a/TODO.md b/TODO.md index e99b5fd..8e7e4fc 100644 --- a/TODO.md +++ b/TODO.md @@ -3,4 +3,3 @@ - threading or async - optional allowlist of available paths (throw something else on unavailable ones) - denylist of known-bad url strings -- fix memory issues or use a GC (bdwgc probably) diff --git a/bin/httpsrv b/bin/httpsrv index 0617f48..8f6f9f9 100755 Binary files a/bin/httpsrv and b/bin/httpsrv differ diff --git a/lib/file.h b/lib/file.h index d22dd29..52b29ec 100644 --- a/lib/file.h +++ b/lib/file.h @@ -2,6 +2,7 @@ #include #include #include +#include #ifdef NO_GC #include #define malloc(n) GC_malloc(n) @@ -30,6 +31,7 @@ char* readFile(char* path) { buf = (char*)malloc(sizeof(char) * (length + 1)); fread(buf, sizeof(char), length, fp); buf[length] = '\0'; + fclose(fp); char* tmp = buf; return tmp; diff --git a/lib/reqHeader.h b/lib/reqHeader.h index f6f2d96..2b7cfc2 100644 --- a/lib/reqHeader.h +++ b/lib/reqHeader.h @@ -1,22 +1,32 @@ #include #include #include +#ifdef NO_GC +#include +#define malloc(n) GC_malloc(n) +#else +#include +#endif typedef struct { - char method[16]; - char path[256]; + char *method; + char *path; char protocol[16]; } ReqHeader; ReqHeader readReqHeader(int client) { - char buf[1024]; + char* buf = malloc(1024); read(client, buf, 1024); ReqHeader header; - strcpy(header.method, strtok(buf, " ")); - strcpy(header.path, strtok(NULL, " ")); - strcpy(header.protocol, strtok(NULL, " ")); + header.method = malloc(1024); + header.path = malloc(1024); + /*strcpy(header.method, strtok(buf, " ")); + strcpy(header.path, strtok(NULL, " "));*/ + sprintf(header.method, "%s", strtok(buf, " ")); + sprintf(header.path, "%s", strtok(NULL, " ")); if (header.path[1] == '\0') { - strcpy(header.path, "/index.html"); + //strcpy(header.path, "/index.html"); + sprintf(header.path, "/index.html"); } return header; } diff --git a/lib/resHeader.h b/lib/resHeader.h index 4b6c49b..6b66d7d 100644 --- a/lib/resHeader.h +++ b/lib/resHeader.h @@ -8,5 +8,5 @@ typedef struct { } ResHeader; void makeHeader(char* out, ResHeader* header) { - sprintf(out, "%s %d %s\r\nContent-Type: %s\r\n\n", /*header->protocol TODO:this breaks everything because newlines*/ "HTTP/1.1", header->status, header->notice, header->mime); + sprintf(out, "%s %d %s\r\nContent-Type: %s\r\nServer: httpsrv/1.0-indev\r\n\n", /*header->protocol TODO:this breaks everything because newlines*/ "HTTP/1.1", header->status, header->notice, header->mime); } diff --git a/src/srv.c b/src/srv.c index 6aa9a80..4391201 100644 --- a/src/srv.c +++ b/src/srv.c @@ -7,6 +7,7 @@ #include #include #include +#include #ifdef NO_GC #include #define malloc(n) GC_malloc(n) @@ -28,37 +29,10 @@ void safeExit(int sig) { _exit(0); } -int srv(char* ADDR, int PORT) { - int srvSocket; - struct sockaddr_in srvAddr; - - srvSocket = socket(AF_INET, SOCK_STREAM, 0); - - srvAddr.sin_family = AF_INET; - srvAddr.sin_port = htons(PORT); - srvAddr.sin_addr.s_addr = inet_addr(ADDR); +void *handle(void* arg) { + int client = *(int*)arg; - if (bind( - srvSocket, - (struct sockaddr*) &srvAddr, - sizeof(srvAddr) - ) < 0) { - error("Failed to bind to %s:%d.\n", ADDR, PORT); - return 1; - } - - if (listen(srvSocket, 10) < 0) { - error("Unable to listen on %s:%d.\n", ADDR, PORT); - return 1; - } - - signal(SIGINT, safeExit); - - 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); funy: { char* dot = strrchr(request.path, '.'); @@ -115,5 +89,39 @@ funy: { send(client, resHeader, strlen(resHeader), 0); close(client); } + return 0; +} + +int srv(char* ADDR, int PORT) { + int srvSocket; + struct sockaddr_in srvAddr; + + srvSocket = socket(AF_INET, SOCK_STREAM, 0); + + srvAddr.sin_family = AF_INET; + srvAddr.sin_port = htons(PORT); + srvAddr.sin_addr.s_addr = inet_addr(ADDR); + + if (bind( + srvSocket, + (struct sockaddr*) &srvAddr, + sizeof(srvAddr) + ) < 0) { + error("Failed to bind to %s:%d.\n", ADDR, PORT); + return 1; + } + + if (listen(srvSocket, 10) < 0) { + error("Unable to listen on %s:%d.\n", ADDR, PORT); + return 1; + } + + signal(SIGINT, safeExit); + + info("Server (probably) listening on http://%s:%d\n", ADDR, PORT); + + while(1) { + int client = accept(srvSocket, NULL, NULL); + handle(&client); } }