diff --git a/.gitignore b/.gitignore index 9fd7e64..cc8d1f5 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ compile_commands.json # Service certs/ +log/ diff --git a/Makefile b/Makefile index b899dc9..ea245d4 100644 --- a/Makefile +++ b/Makefile @@ -15,6 +15,7 @@ install: output_dir: mkdir -p ${OUTPUT_DIR} + mkdir -p ./log CERT_DIR = ./certs/ diff --git a/cmd/server.c b/cmd/server.c index 069f691..4e31638 100644 --- a/cmd/server.c +++ b/cmd/server.c @@ -13,6 +13,8 @@ #include "returnRequest.h" #include "server.h" +#define WEB_ROOT "content/" + static int verbose_flag = 0; bool enableHTTPS = 0; @@ -23,13 +25,13 @@ int printDebug(char message[]) { return 0; } -int parseHTTPRequest(char buffer[], struct HTTPRequest *r) { +int parseHTTPRequest(unsigned char *buffer, struct HTTPRequest *r) { char temp[1]; // Used to check newlines char *token = calloc(8, sizeof(char)); int line = 0; char *checkLine = calloc(1000, sizeof(char)); - for (int i = 0; i < strlen(buffer); i++) { + for (int i = 0; i < strlen((char *)buffer); i++) { temp[0] = buffer[i]; if ((!strcmp(temp, "\n")) && (i != 0)) { // Config Check @@ -48,8 +50,11 @@ int parseHTTPRequest(char buffer[], struct HTTPRequest *r) { token = strtok(NULL, ""); r->requestVersion = malloc(strlen(token)); strcpy(r->requestVersion, token); + } else if (!strcmp(token, "POST")) { + printDebug("POST RECEIVED"); + return 2; } else { - return 1; + return 2; } } else { token = strtok(checkLine, ":"); @@ -70,11 +75,60 @@ int parseHTTPRequest(char buffer[], struct HTTPRequest *r) { strcat(checkLine, temp); } } + + if (strlen(r->requestType) == 0) { + return -1; + } + free(checkLine); return 0; } -int handleRequest(char buffer[], int socket, SSL *ssl) { +int handleGetRequest(int socket, struct HTTPRequest *r, SSL *ssl) { + char errResponse[256]; + char ch; + size_t size = 8; + int i = 0; + unsigned char *fileContent = calloc(size, sizeof(unsigned char)); + FILE *fp; + char *workingReqDir; + // If requesting root directory change to index.html + if (!strcmp(r->requestDir, "/")) { + workingReqDir = "index.html"; + } else { + workingReqDir = r->requestDir; + } + char *reqDir = calloc(strlen(WEB_ROOT) + strlen(workingReqDir), sizeof(char)); + + sprintf(reqDir, "%s%s", WEB_ROOT, workingReqDir); + + fp = fopen(reqDir, "r"); + + if (fp == NULL) { + sprintf(errResponse, "Error opening file: %s", workingReqDir); + printDebug(errResponse); + return404Request(socket, ssl); + return -1; + } + + while((ch = fgetc(fp)) != EOF) { + if (i == size) { + size *= 2; + fileContent = realloc(fileContent, size); + } + fileContent[i] = ch; + i++; + } + + return200Request(socket, fileContent, ssl); + + fclose(fp); + free(reqDir); + free(fileContent); + return 0; +} + +int handleRequest(unsigned char buffer[], int socket, SSL *ssl) { struct HTTPRequest r; // Holds relevant HTTP request information int checkerr = 0; @@ -83,11 +137,21 @@ int handleRequest(char buffer[], int socket, SSL *ssl) { // Grabbing relevant information out of request checkerr = parseHTTPRequest(buffer, &r); if (checkerr != 0) { // Checking for HTTP parsing error - printf("Error parsing: exit code %d\n", checkerr); - return return404Request(socket, ssl); + if (checkerr == -1) { + printDebug("Error reading request, returning empty 404"); + } else { + printDebug("Error parsing, returning 500"); + // return return500Request(socket, ssl); + } + return return400Request(socket, ssl); + } + + if (!strcmp(r.requestType, "GET")) { + handleGetRequest(socket, &r, ssl); + return 0; } // Return response to socket - return200Request(socket, ssl); + return200Request(socket, NULL ,ssl); return 0; } @@ -98,18 +162,18 @@ int main(int argc, char **argv) { int checkerr = 0; // Used for error checking int addrlen = sizeof(address); - char *certFile = malloc(1); - char *privKeyFile = malloc(1); + char *certFile = malloc(sizeof(char)); + char *privKeyFile = malloc(sizeof(char)); certFile[0] = '\0'; privKeyFile[0] = '\0'; uint32_t listenAddrNum = -1; - char *listenAddr = malloc(1); + char *listenAddr = malloc(sizeof(char)); listenAddr[0] = '\0'; SSL_CTX *ctx = NULL; - char buffer[1024] = {}; + unsigned char *buffer = calloc(8096, sizeof(unsigned char)); // Setting up options static const struct option long_options[] = { @@ -215,7 +279,7 @@ int main(int argc, char **argv) { ssl = SSL_new(ctx); SSL_set_fd(ssl, new_socket); SSL_accept(ssl); - SSL_read(ssl, buffer, sizeof(buffer)); + SSL_read(ssl, buffer, 8096); handleRequest(buffer, new_socket, ssl); } else { read(new_socket, buffer, 1024); @@ -228,6 +292,7 @@ int main(int argc, char **argv) { free(privKeyFile); free(certFile); free(listenAddr); + free(buffer); close(server_fd); exit(EXIT_SUCCESS); } diff --git a/content/index.html b/content/index.html new file mode 100644 index 0000000..557db03 --- /dev/null +++ b/content/index.html @@ -0,0 +1 @@ +Hello World diff --git a/content/testpage.html b/content/testpage.html new file mode 100644 index 0000000..33735a7 --- /dev/null +++ b/content/testpage.html @@ -0,0 +1,9 @@ + + +
+ +My first paragraph.
+ + + diff --git a/include/returnRequest.h b/include/returnRequest.h index 6eda11e..9cc10d4 100644 --- a/include/returnRequest.h +++ b/include/returnRequest.h @@ -1,5 +1,16 @@ #include