feat(error): add content-type error
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user