feat(error): add content-type error

This commit is contained in:
2025-12-02 01:48:14 +01:00
parent 115109efb9
commit 0af66d3aa4
4 changed files with 9 additions and 5 deletions

View File

@@ -4,26 +4,28 @@
#include <string.h>
#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;
}

View File

@@ -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) {