From 89fb77db7ffd42d66df29609bf1d04346d573bf8 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Tue, 30 May 2023 15:18:39 +0200 Subject: [PATCH] feat: add mime types --- include/http.h | 45 +++++++++++++++++--- include/http_loop.h | 2 + src/http/http.c | 6 --- src/http/{http_res_names.c => http_consts.c} | 38 ++++++++++++++++- src/http_loop/http_loop_ctx.c | 3 +- src/http_loop/http_loop_tools.c | 5 +++ src/lander/lander_get.c | 1 + 7 files changed, 86 insertions(+), 14 deletions(-) delete mode 100644 src/http/http.c rename src/http/{http_res_names.c => http_consts.c} (64%) diff --git a/include/http.h b/include/http.h index d1a2a9f..fdff087 100644 --- a/include/http.h +++ b/include/http.h @@ -11,9 +11,19 @@ #define HTTP_MAX_ALLOWED_HEADERS 16 #define HTTP_MAX_REGEX_GROUPS 4 -extern const char *http_response_type_names[5][32]; +// Array mapping the http_response_type enum to strings +extern const char *http_response_type_names[][32]; + +// Array mapping the http_header enum to strings extern const char *http_header_names[]; +// Array mapping the http_mime_type enum to strings +extern const char *http_mime_type_names[][2]; + +// Array mapping the http_request_method enum to strings +extern const char *request_method_names[]; +extern const size_t request_method_names_len; + typedef enum http_request_method { http_get = 0, http_post = 1, @@ -22,9 +32,6 @@ typedef enum http_request_method { http_delete = 4 } http_request_method; -extern const char *request_method_names[]; -extern const size_t request_method_names_len; - typedef enum http_body_type { http_body_buf = 0, http_body_file = 1 @@ -132,7 +139,8 @@ typedef enum http_response_type { typedef enum http_header { http_header_connection = 0, - http_header_location = 1 + http_header_location, + http_header_content_type } http_header; typedef struct http_response_header { @@ -159,4 +167,31 @@ typedef struct http_response { size_t header_count; } http_response; +typedef enum http_mime_type { + http_mime_aac = 0, + http_mime_bz, + http_mime_bz2, + http_mime_css, + http_mime_csv, + http_mime_gz, + http_mime_gif, + http_mime_htm, + http_mime_html, + http_mime_jar, + http_mime_jpeg, + http_mime_js, + http_mime_json, + http_mime_mp3, + http_mime_mp4, + http_mime_png, + http_mime_pdf, + http_mime_rar, + http_mime_sh, + http_mime_svg, + http_mime_tar, + http_mime_txt, + http_mime_wav, + http_mime_7z +} http_mime_type; + #endif diff --git a/include/http_loop.h b/include/http_loop.h index 5e10932..99833b8 100644 --- a/include/http_loop.h +++ b/include/http_loop.h @@ -106,6 +106,8 @@ void http_loop_res_set_body_file(http_loop_ctx *ctx, const char *filename); void http_loop_res_add_header(http_loop_ctx *ctx, http_header type, const char *value, bool owned); +void http_loop_res_set_mime_type(http_loop_ctx *ctx, http_mime_type mime_type); + /* * Request step that consumes the request body and stores it in a buffer */ diff --git a/src/http/http.c b/src/http/http.c deleted file mode 100644 index 68ef55e..0000000 --- a/src/http/http.c +++ /dev/null @@ -1,6 +0,0 @@ -#include "http.h" - -// Very important that this is in the same order as http_request_method -const char *request_method_names[] = {"GET", "POST", "PUT", "PATCH", "DELETE"}; -const size_t request_method_names_len = - sizeof(request_method_names) / sizeof(request_method_names[0]); diff --git a/src/http/http_res_names.c b/src/http/http_consts.c similarity index 64% rename from src/http/http_res_names.c rename to src/http/http_consts.c index 31efe6a..e693107 100644 --- a/src/http/http_res_names.c +++ b/src/http/http_consts.c @@ -1,8 +1,13 @@ #include "http.h" +// Very important that this is in the same order as http_request_method +const char *request_method_names[] = {"GET", "POST", "PUT", "PATCH", "DELETE"}; +const size_t request_method_names_len = + sizeof(request_method_names) / sizeof(request_method_names[0]); + // clang-format off -const char *http_response_type_names[5][32] = { +const char *http_response_type_names[][32] = { // 1xx { "Continue", // 100 @@ -88,7 +93,36 @@ const char *http_response_type_names[5][32] = { const char *http_header_names[] = { "Connection", - "Location" + "Location", + "Content-Type" +}; + +const char *http_mime_type_names[][2] = { + { "aac", "audio/aac" }, + { "bz", "application/x-bzip" }, + { "bz2", "application/x-bzip2" }, + { "css", "text/css" }, + { "csv", "text/csv" }, + { "gz", "application/gzip" }, + { "gif", "image/gif" }, + { "htm", "text/html" }, + { "html", "text/html" }, + { "jar", "application/java-archive" }, + { "jpeg", "image/jpeg" }, + { "jpg", "image/jpeg" }, + { "js", "text/javascript" }, + { "json", "application/json" }, + { "mp3", "audio/mpeg" }, + { "mp4", "video/mp4" }, + { "png", "image/png" }, + { "pdf", "application/pdf" }, + { "rar", "application/vnd.rar" }, + { "sh", "application/x-sh" }, + { "svg", "image/svg+xml" }, + { "tar", "application/x-tar" }, + { "txt", "text/plain" }, + { "wav", "audio/wav" }, + { "7z", "application/x-7z-compressed" }, }; // clang-format on diff --git a/src/http_loop/http_loop_ctx.c b/src/http_loop/http_loop_ctx.c index 9696956..c138fa0 100644 --- a/src/http_loop/http_loop_ctx.c +++ b/src/http_loop/http_loop_ctx.c @@ -40,7 +40,8 @@ void http_loop_ctx_reset(http_loop_ctx *ctx) { ctx->res.head = NULL; } - if (ctx->res.body_type == http_body_buf && ctx->res.body.buf != NULL) { + if (ctx->res.body_type == http_body_buf && ctx->res.body.buf != NULL && + ctx->res.owns_body) { free(ctx->res.body.buf); ctx->res.body.buf = NULL; } else if (ctx->res.body_type == http_body_file && diff --git a/src/http_loop/http_loop_tools.c b/src/http_loop/http_loop_tools.c index b1c5e8d..bcc8e57 100644 --- a/src/http_loop/http_loop_tools.c +++ b/src/http_loop/http_loop_tools.c @@ -30,3 +30,8 @@ void http_loop_res_add_header(http_loop_ctx *ctx, http_header type, ctx->res.header_count++; } + +void http_loop_res_set_mime_type(http_loop_ctx *ctx, http_mime_type mime_type) { + http_loop_res_add_header(ctx, http_header_content_type, + http_mime_type_names[mime_type][1], false); +} diff --git a/src/lander/lander_get.c b/src/lander/lander_get.c index c512730..62040e9 100644 --- a/src/lander/lander_get.c +++ b/src/lander/lander_get.c @@ -16,6 +16,7 @@ bool lander_get_index(event_loop_conn *conn) { http_loop_ctx *ctx = conn->ctx; http_loop_res_set_body_buf(ctx, index_page, sizeof(index_page) - 1, false); + http_loop_res_set_mime_type(ctx, http_mime_html); conn->state = event_loop_conn_state_res; return true;