From 3cb8fda44d90f15c289ca4d16b237815d2e129fd Mon Sep 17 00:00:00 2001 From: Francesco Date: Wed, 25 Mar 2026 01:55:47 +0100 Subject: [PATCH] feat(src): add internal/common --- meson.build | 9 ++++++--- src/http/mime.c | 7 ++++--- src/http/response.c | 14 +++++--------- src/internal/common.h | 10 ++++++++++ 4 files changed, 25 insertions(+), 15 deletions(-) create mode 100644 src/internal/common.h diff --git a/meson.build b/meson.build index baaba88..80bff5f 100644 --- a/meson.build +++ b/meson.build @@ -9,9 +9,12 @@ add_global_arguments('-Wno-pedantic', language: 'c') cc = meson.get_compiler('c') -subdir('src') - incdir = include_directories('include') +srcdir = include_directories('src') + +include_dirs = [incdir, srcdir] + +subdir('src') libtomlc17 = dependency('libtomlc17', required: true) libmath = cc.find_library('m', required: true) @@ -23,7 +26,7 @@ add_global_arguments('-DUSE_COLORS', language: 'c') add_global_arguments('-DEVELOPER', language: 'c') add_global_arguments('-D_POSIX_C_SOURCE=200809L', language: 'c') -exe = executable('cws', server, include_directories: incdir, dependencies: deps) +exe = executable('cws', server, include_directories: include_dirs, dependencies: deps) # Test test_src = files('test/server.c') diff --git a/src/http/mime.c b/src/http/mime.c index a9fbedd..6388e2b 100644 --- a/src/http/mime.c +++ b/src/http/mime.c @@ -3,9 +3,10 @@ #include #include -#include "http/request.h" #include "utils/error.h" +#include "internal/common.h" + static mimetype mimetypes[] = {{"html", "text/html"}, {"css", "text/css"}, {"js", "application/javascript"}, {"jpg", "image/jpeg"}, {"png", "image/png"}, {"ico", "image/x-icon"}}; @@ -18,13 +19,13 @@ int cws_mime_get_ct(const char *location_path, char *content_type) { 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); + snprintf(content_type, CONTENT_TYPE_MAX - 1, "%s", mimetypes[i].type); return CWS_OK; } } - snprintf(content_type, CWS_HTTP_CONTENT_TYPE - 1, "%s", "Content-Type not supported"); + snprintf(content_type, CONTENT_TYPE_MAX - 1, "%s", "Content-Type not supported"); return CWS_OK; } diff --git a/src/http/response.c b/src/http/response.c index 03339bc..10dd155 100644 --- a/src/http/response.c +++ b/src/http/response.c @@ -9,12 +9,11 @@ #include #include -#define CHUNK_SIZE 8192 -#define HEADERS_BUFFER_SIZE 2048 +#include "internal/common.h" static hashmap_s *response_headers_new(void) { - return hm_new(my_str_hash_fn, my_str_equal_fn, my_str_free_fn, my_str_free_fn, sizeof(char) * 256, - sizeof(char) * 512); + return hm_new(my_str_hash_fn, my_str_equal_fn, my_str_free_fn, my_str_free_fn, sizeof(char) * HEADER_KEY_MAX, + sizeof(char) * HEADER_VALUE_MAX); } cws_response_s *cws_response_new(cws_http_status_e status) { @@ -30,9 +29,6 @@ cws_response_s *cws_response_new(cws_http_status_e status) { resp->body_file = NULL; resp->content_length = 0; - /* TODO: get the value from connection */ - cws_response_set_header(resp, "Connection", "close"); - return resp; } @@ -61,7 +57,7 @@ void cws_response_set_header(cws_response_s *response, const char *key, const ch return; } - char k[256], v[512]; + char k[HEADER_KEY_MAX], v[HEADER_VALUE_MAX]; strncpy(k, key, sizeof(k) - 1); k[sizeof(k) - 1] = '\0'; strncpy(v, value, sizeof(v) - 1); @@ -101,7 +97,7 @@ void cws_response_set_body_file(cws_response_s *response, const char *filepath) return; } - char content_type[64]; + char content_type[CONTENT_TYPE_MAX]; cws_mime_get_ct(filepath, content_type); cws_response_set_header(response, "Content-Type", content_type); diff --git a/src/internal/common.h b/src/internal/common.h new file mode 100644 index 0000000..e463193 --- /dev/null +++ b/src/internal/common.h @@ -0,0 +1,10 @@ +#ifndef CWS_INTERNALS_COMMON_H +#define CWS_INTERNALS_COMMON_H + +#define CONTENT_TYPE_MAX 64 +#define HEADER_KEY_MAX 256 +#define HEADER_VALUE_MAX 1024 +#define CHUNK_SIZE 8192 +#define HEADERS_BUFFER_SIZE 2048 + +#endif