From 0af66d3aa47c63f8c375b6ac98ae1e708ced59bb Mon Sep 17 00:00:00 2001 From: Francesco Date: Tue, 2 Dec 2025 01:48:14 +0100 Subject: [PATCH] feat(error): add content-type error --- include/http/mime.h | 2 +- include/utils/error.h | 1 + src/http/mime.c | 10 ++++++---- src/utils/error.c | 1 + 4 files changed, 9 insertions(+), 5 deletions(-) diff --git a/include/http/mime.h b/include/http/mime.h index fbfe008..d85f42f 100644 --- a/include/http/mime.h +++ b/include/http/mime.h @@ -8,6 +8,6 @@ typedef struct mimetype { const char *type; } mimetype; -int http_get_content_type(const char *location_path, char *content_type); +int mime_get_content_type(const char *location_path, char *content_type); #endif diff --git a/include/utils/error.h b/include/utils/error.h index 553a77a..c2e680f 100644 --- a/include/utils/error.h +++ b/include/utils/error.h @@ -16,6 +16,7 @@ typedef enum cws_ret { CWS_BIND_ERROR, CWS_LISTEN_ERROR, CWS_WORKER_ERROR, + CWS_CONTENT_TYPE_ERROR, CWS_UNKNOWN_ERROR, } cws_return; diff --git a/src/http/mime.c b/src/http/mime.c index 6c323aa..88c27c8 100644 --- a/src/http/mime.c +++ b/src/http/mime.c @@ -4,26 +4,28 @@ #include #include "http/request.h" +#include "utils/error.h" static mimetype mimetypes[] = {{"html", "text/html"}, {"css", "text/css"}, {"js", "application/javascript"}, {"jpg", "image/jpeg"}, {"png", "image/png"}, {"ico", "image/x-icon"}}; -int http_get_content_type(const char *location_path, char *content_type) { +int mime_get_content_type(const char *location_path, char *content_type) { /* Find last occurrence of a string */ char *ptr = strrchr(location_path, '.'); if (ptr == NULL) { - return -1; + return CWS_CONTENT_TYPE_ERROR; } ptr += 1; 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); - return 0; + + return CWS_OK; } } snprintf(content_type, CWS_HTTP_CONTENT_TYPE - 1, "%s", "Content-Type not supported"); - return 0; + return CWS_OK; } diff --git a/src/utils/error.c b/src/utils/error.c index 09693cc..5459fc5 100644 --- a/src/utils/error.c +++ b/src/utils/error.c @@ -14,6 +14,7 @@ static const cws_error_s errors[] = {{CWS_OK, "No error"}, {CWS_BIND_ERROR, "bind() failed"}, {CWS_LISTEN_ERROR, "listen() failed"}, {CWS_WORKER_ERROR, "Worker thread initialization failed"}, + {CWS_CONTENT_TYPE_ERROR, "Unable to get content type"}, {CWS_UNKNOWN_ERROR, "Unknown error"}}; const char *cws_error_str(cws_return code) {