feat(src): add internal/common

This commit is contained in:
2026-03-25 01:55:47 +01:00
parent 0de6dfda58
commit 3cb8fda44d
4 changed files with 25 additions and 15 deletions
+6 -3
View File
@@ -9,9 +9,12 @@ add_global_arguments('-Wno-pedantic', language: 'c')
cc = meson.get_compiler('c') cc = meson.get_compiler('c')
subdir('src')
incdir = include_directories('include') incdir = include_directories('include')
srcdir = include_directories('src')
include_dirs = [incdir, srcdir]
subdir('src')
libtomlc17 = dependency('libtomlc17', required: true) libtomlc17 = dependency('libtomlc17', required: true)
libmath = cc.find_library('m', 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('-DEVELOPER', language: 'c')
add_global_arguments('-D_POSIX_C_SOURCE=200809L', 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
test_src = files('test/server.c') test_src = files('test/server.c')
+4 -3
View File
@@ -3,9 +3,10 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include "http/request.h"
#include "utils/error.h" #include "utils/error.h"
#include "internal/common.h"
static mimetype mimetypes[] = {{"html", "text/html"}, {"css", "text/css"}, {"js", "application/javascript"}, static mimetype mimetypes[] = {{"html", "text/html"}, {"css", "text/css"}, {"js", "application/javascript"},
{"jpg", "image/jpeg"}, {"png", "image/png"}, {"ico", "image/x-icon"}}; {"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) { for (size_t i = 0; i < ARR_SIZE(mimetypes); ++i) {
if (!strcmp(ptr, mimetypes[i].ext)) { 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; 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; return CWS_OK;
} }
+5 -9
View File
@@ -9,12 +9,11 @@
#include <string.h> #include <string.h>
#include <sys/stat.h> #include <sys/stat.h>
#define CHUNK_SIZE 8192 #include "internal/common.h"
#define HEADERS_BUFFER_SIZE 2048
static hashmap_s *response_headers_new(void) { 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, 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) * 512); sizeof(char) * HEADER_VALUE_MAX);
} }
cws_response_s *cws_response_new(cws_http_status_e status) { 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->body_file = NULL;
resp->content_length = 0; resp->content_length = 0;
/* TODO: get the value from connection */
cws_response_set_header(resp, "Connection", "close");
return resp; return resp;
} }
@@ -61,7 +57,7 @@ void cws_response_set_header(cws_response_s *response, const char *key, const ch
return; return;
} }
char k[256], v[512]; char k[HEADER_KEY_MAX], v[HEADER_VALUE_MAX];
strncpy(k, key, sizeof(k) - 1); strncpy(k, key, sizeof(k) - 1);
k[sizeof(k) - 1] = '\0'; k[sizeof(k) - 1] = '\0';
strncpy(v, value, sizeof(v) - 1); strncpy(v, value, sizeof(v) - 1);
@@ -101,7 +97,7 @@ void cws_response_set_body_file(cws_response_s *response, const char *filepath)
return; return;
} }
char content_type[64]; char content_type[CONTENT_TYPE_MAX];
cws_mime_get_ct(filepath, content_type); cws_mime_get_ct(filepath, content_type);
cws_response_set_header(response, "Content-Type", content_type); cws_response_set_header(response, "Content-Type", content_type);
+10
View File
@@ -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