diff --git a/http/http_method.c b/http/http_method.c index 86230d5..bbe8ab1 100644 --- a/http/http_method.c +++ b/http/http_method.c @@ -43,21 +43,21 @@ void http_method(HttpMethod __method, char *__out, size_t __size) { } HttpMethod get_http_method(char *__met) { - if (strcmp(__met, "GET")) + if (!strcmp(__met, "GET")) return HTTP_METHOD_GET; - if (strcmp(__met, "POST")) + if (!strcmp(__met, "POST")) return HTTP_METHOD_POST; - if (strcmp(__met, "PUT")) + if (!strcmp(__met, "PUT")) return HTTP_METHOD_PUT; - if (strcmp(__met, "DELETE")) + if (!strcmp(__met, "DELETE")) return HTTP_METHOD_DELETE; - if (strcmp(__met, "PATCH")) + if (!strcmp(__met, "PATCH")) return HTTP_METHOD_PATCH; - if (strcmp(__met, "HEAD")) + if (!strcmp(__met, "HEAD")) return HTTP_METHOD_HEAD; - if (strcmp(__met, "OPTIONS")) + if (!strcmp(__met, "OPTIONS")) return HTTP_METHOD_OPTIONS; - if (strcmp(__met, "CONNECT")) + if (!strcmp(__met, "CONNECT")) return HTTP_METHOD_CONNECT; return HTTP_METHOD_TRACE; } diff --git a/http/http_request.c b/http/http_request.c index 3073a53..b548286 100644 --- a/http/http_request.c +++ b/http/http_request.c @@ -1,5 +1,5 @@ -#include "http_method.h" #include "http_request.h" +#include "http_method.h" #include #include #include @@ -20,34 +20,37 @@ void print_request(const HttpRequest *__req) { char method[10]; http_method(__req->method, method, 10); - printf("%s %s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n", method, - __req->path, __req->host, __req->sec_fetch_dest, __req->user_agent, - __req->upgrade_insecure_requests, "OK", __req->sec_fetch_site, - __req->sec_fetch_mode, __req->accept_language, + printf("Method: %s Path: %s\nHost: %s\nSec-Fetch-Dest: %s\nUser-Agent: " + "%s\nUpgrade-Insecure-Requests: %s\nAccept: %s\nSec-Fetch-Site: " + "%s\nSec-Fetch-Mode: %s\nAccept-Language: %s\nPriority: " + "%s\nAccept-Encoding: %s\n", + method, __req->path, __req->host, __req->sec_fetch_dest, + __req->user_agent, __req->upgrade_insecure_requests, "OK", + __req->sec_fetch_site, __req->sec_fetch_mode, __req->accept_language, __req->accept_encoding, __req->connection); } void http_request_set(HttpRequest *__req, char *key, char *value) { - if (strcmp(key, "Host")) + if (!strcmp(key, "Host")) __req->host = value; - else if (strcmp(key, "Sec-Fetch-Dest")) + else if (!strcmp(key, "Sec-Fetch-Dest")) __req->sec_fetch_dest = value; - else if (strcmp(key, "User-Agent")) + else if (!strcmp(key, "User-Agent")) __req->user_agent = value; - else if (strcmp(key, "Upgrade-Insecure-Requests")) + else if (!strcmp(key, "Upgrade-Insecure-Requests")) __req->upgrade_insecure_requests = value; - else if (strcmp(key, "Accept")) + else if (!strcmp(key, "Accept")) ; // __req->accept = value; - else if (strcmp(key, "Sec-Fetch-Site")) + else if (!strcmp(key, "Sec-Fetch-Site")) __req->sec_fetch_site = value; - else if (strcmp(key, "Sec-Fetch-Mode")) + else if (!strcmp(key, "Sec-Fetch-Mode")) __req->sec_fetch_mode = value; - else if (strcmp(key, "Accept-Language")) + else if (!strcmp(key, "Accept-Language")) __req->accept_language = value; - else if (strcmp(key, "Accept-Encoding")) + else if (!strcmp(key, "Accept-Encoding")) __req->accept_encoding = value; - else if (strcmp(key, "Connection")) + else if (!strcmp(key, "Connection")) __req->connection = value; } @@ -64,24 +67,27 @@ HttpRequest *handle_request(char *__req) { return NULL; strncpy(line, line_start, l_length); - line[l_length] = 0; + line[l_length - 1] = 0; if (line_count == 1) { - char method[10]; + char method[10] = {0}; char *path = malloc(sizeof(char) * 1024); - if (sscanf(line, "%s %s", method, path) != 3) { + if (sscanf(line, "%s %s", method, path) != 2) { free(path); return NULL; } - printf("%s", path); request->method = get_http_method(method); request->path = path; } else { - char *key = malloc(sizeof(char) * 20); + char key[100] = {0}; char *value = malloc(sizeof(char) * 256); - if (sscanf(line, "%s: %s", key, value) == 2) { + char *colon_pos = strchr(line, ':'); + if (colon_pos != NULL) { + size_t k_length = colon_pos - line; + strncpy(key, line, k_length); + + strcpy(value, colon_pos + 2); http_request_set(request, key, value); } - free(key); } // TODO: Analyze line free(line); diff --git a/main.c b/main.c index 3787018..758f2d5 100644 --- a/main.c +++ b/main.c @@ -7,6 +7,8 @@ #include #include +#define DEFAULT_HTML "index.html" + int main() { const int PORT = 8080; @@ -50,11 +52,17 @@ int main() { 1024 - 1); // subtract 1 for the null // terminator at the end HttpRequest *req = handle_request(request); - // print_request(req); - HttpResponse *res = from_file("./index.html"); + HttpResponse *res; + if (!strcmp(req->path, "/")) { + res = from_file("./" DEFAULT_HTML); + } else { + char path[] = "."; + strcat(path, req->path); + res = from_file(path); + } + free(req); http_respond(*res, clientfd); free_response(res); - printf("Hello message sent\n"); // closing the connected socket close(clientfd); diff --git a/premake5.lua b/premake5.lua index 773f41c..e1cdcd8 100644 --- a/premake5.lua +++ b/premake5.lua @@ -23,6 +23,8 @@ links { "CWebLibrary" } filter "configurations:Debug" symbols "On" +buildoptions { "-g3", "-O0", "-fno-inline" } -- Ensure debug symbols are generated for static library +optimize "Off" filter "configurations:Release" optimize "On"