From 33a12aaf7374c0638294a5f83408725d382383d0 Mon Sep 17 00:00:00 2001 From: Francesco Date: Sat, 25 Oct 2025 17:53:56 +0200 Subject: [PATCH] refactor(http): use mimetype array --- include/http/http.h | 8 ++++++++ src/http/http.c | 24 +++++++++++++----------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/include/http/http.h b/include/http/http.h index 35fd9e5..456ab7e 100644 --- a/include/http/http.h +++ b/include/http/http.h @@ -5,8 +5,11 @@ #include #include +#define ARR_SIZE(arr) (sizeof(arr) / sizeof(arr[0])) + #define CWS_HTTP_HEADER_MAX 512 #define CWS_HTTP_HEADER_CONTENT_MAX 1024 +#define CWS_HTTP_CONTENT_TYPE 64 typedef enum cws_http_method { HTTP_GET, @@ -23,6 +26,11 @@ typedef enum cws_http_status { HTTP_NOT_IMPLEMENTED, } cws_http_status_e; +typedef struct mimetype { + const char *ext; + const char *type; +} mimetype; + typedef struct cws_http { int sockfd; cws_http_method_e method; diff --git a/src/http/http.c b/src/http/http.c index 2b8bf2b..dbba711 100644 --- a/src/http/http.c +++ b/src/http/http.c @@ -38,6 +38,12 @@ static cws_http_method_e http_parse_method(const char *method) { return HTTP_UNKNOWN; } +static mimetype mimetypes[] = {{"html", "text/html"}, + {"css", "text/css"}, + {"js", "application/javascript"}, + {"jpg", "image/jpeg"}, + {"png", "image/png"}}; + static int http_get_content_type(char *location_path, char *content_type) { /* Find last occurrence of a string */ char *ptr = strrchr(location_path, '.'); @@ -46,18 +52,12 @@ static int http_get_content_type(char *location_path, char *content_type) { } ptr += 1; - char ct[32] = {0}; - - if (strcmp(ptr, "html") == 0 || strcmp(ptr, "css") == 0 || strcmp(ptr, "javascript") == 0) { - strncpy(ct, "text", sizeof ct); + for (size_t i = 0; i < ARR_SIZE(mimetypes); ++i) { + if (!strcmp(ptr, mimetypes[i].ext)) { + snprintf(content_type, CWS_HTTP_CONTENT_TYPE - 1, "%s", mimetypes[i].type); + } } - if (strcmp(ptr, "jpg") == 0 || strcmp(ptr, "png") == 0) { - strncpy(ct, "image", sizeof ct); - } - - snprintf(content_type, 1024, "%s/%s", ct, ptr); - return 0; } @@ -122,8 +122,9 @@ static size_t file_data(const char *path, char **data) { static cws_server_ret http_send_resource(cws_http_s *request) { /* Retrieve correct Content-Type */ - char content_type[1024]; + char content_type[CWS_HTTP_CONTENT_TYPE]; http_get_content_type(request->location_path->data, content_type); + CWS_LOG_DEBUG("content-type: %s", content_type); /* TODO: Check for keep-alive */ @@ -218,6 +219,7 @@ cws_http_s *cws_http_parse(string_s *request_str) { str += len + 1; /* Adjust location path */ + /* @TODO: fix path traversal */ string_append(request->location_path, "www"); if (strcmp(request->location->data, "/") == 0) { string_append(request->location_path, "/index.html");