From 8087fba4bbd07f38088f38728dcde14a3fc19132 Mon Sep 17 00:00:00 2001 From: oduortoni Date: Fri, 13 Feb 2026 16:00:22 +0300 Subject: [PATCH 1/3] refactor: use the debug macros instead of std library print statements - will make it easier to transition into better logging utilities like log files - refactor da_append to wrap (array) --- src/app/static.c | 5 ++-- src/lib/http/http_handle.c | 1 - src/lib/http/http_server.c | 15 +++++----- .../http/route_dispatchers/regex_dispatcher.c | 15 ++-------- src/lib/net/net_listener.c | 2 -- src/lib/net/net_serve.c | 5 ++-- src/lib/utils/macros.h | 28 +++++++++---------- src/main.c | 13 ++------- 8 files changed, 32 insertions(+), 52 deletions(-) diff --git a/src/app/static.c b/src/app/static.c index 6bbae71..f37cab6 100644 --- a/src/app/static.c +++ b/src/app/static.c @@ -3,6 +3,7 @@ #include "header.h" #include "utils/string/header.h" +#include "utils/logging/header.h" int Static(ResponseWriter* w, Request* r) @@ -11,11 +12,11 @@ Static(ResponseWriter* w, Request* r) // directory assert(r->path.size); char const* filename = r->path.data + 1; - printf("Requested static file: '%s'\n", filename); + info("Requested static file: '%s'\n", filename); struct stat st; if (r->path_regex->re_nsub != 1 || stat(filename, &st) < 0) { - perror("Requested file not found"); + error("Requested file not found"); return Error404(w, r); } diff --git a/src/lib/http/http_handle.c b/src/lib/http/http_handle.c index d5cffb2..a9fca46 100644 --- a/src/lib/http/http_handle.c +++ b/src/lib/http/http_handle.c @@ -34,7 +34,6 @@ http_handle(Router* router, const char* request_data) // If no handler found, try 404 if (handler == nullptr) { - printf("ROUTE NOT FOUND\n"); req->path_regex = nullptr; for (int i = 0; i < router->route_count; i++) { if (strcmp("^/404$", router->patterns[i]) == 0) { diff --git a/src/lib/http/http_server.c b/src/lib/http/http_server.c index 6d40792..224a487 100644 --- a/src/lib/http/http_server.c +++ b/src/lib/http/http_server.c @@ -1,5 +1,6 @@ #include "header.h" #include "utils/macros.h" +#include "utils/logging/header.h" HttpServer http = { .ListenAndServe = listenAndServe, @@ -16,27 +17,25 @@ listenAndServe(char* host, Router* router) // if pprovided, make global point to it http.router = router; } - puts("http/server.c"); if (router == NULL) { - printf("ERROR: Router is NULL\n"); + error("ERROR: Router is NULL\n"); return -1; } - puts("http/server.c"); - printf("Router address: %p\n", (void*)router); - printf("Dispatcher address: %p\n", (void*)router->dispatcher); + // info("http/server.c"); + // info("Router address: %p\n", (void*)router); + // info("Dispatcher address: %p\n", (void*)router->dispatcher); if (!router->dispatcher) { for (size_t i = 0; i < ARRAY_LEN(router->patterns) && router->patterns[i]; i++) { - printf("SERVERT: %s\n", router->patterns[i]); + info("Using old router: %s\n", router->patterns[i]); } } else { - printf("Using dispatcher-based router\n"); + info("Using dispatcher-based router\n"); } - printf("RouteD\n"); RequestContext context = {.router = http.router}; net_serve(host, http_handle_connection, &context); diff --git a/src/lib/http/route_dispatchers/regex_dispatcher.c b/src/lib/http/route_dispatchers/regex_dispatcher.c index b9d00fa..4feebe0 100644 --- a/src/lib/http/route_dispatchers/regex_dispatcher.c +++ b/src/lib/http/route_dispatchers/regex_dispatcher.c @@ -5,8 +5,6 @@ HandlerFunc regex_match(void* impl_data, const char* path, Request* req) { - printf("Search for route '%s'\n", path); - RegexRouterData* data = (RegexRouterData*)impl_data; for (size_t i = 0; i < data->len; i++) { req->path_regex = &data->items[i].compiled_pattern; @@ -23,22 +21,13 @@ regex_match(void* impl_data, const char* path, Request* req) static void regex_add_route(void* impl_data, const char* pattern, HandlerFunc handler) { - RegexRouterData* data_ptr = (RegexRouterData*)impl_data; - RegexRouterData data; - - data.items = data_ptr->items; - data.capacity = data_ptr->capacity; - data.len = data_ptr->len; + RegexRouterData* data = (RegexRouterData*)impl_data; struct Route route = {.pattern = strdup(pattern), .handler = handler}; regcomp(&route.compiled_pattern, pattern, REG_EXTENDED); - da_append(data, route); - - data_ptr->items = data.items; - data_ptr->capacity = data.capacity; - data_ptr->len = data.len; + da_append(*data, route); } static void diff --git a/src/lib/net/net_listener.c b/src/lib/net/net_listener.c index 14d3785..c778c58 100644 --- a/src/lib/net/net_listener.c +++ b/src/lib/net/net_listener.c @@ -13,8 +13,6 @@ net_listener(char* host, int port) address.sin_family = AF_INET; address.sin_port = htons(port); - printf("Host: %s, Port: %d\n", host, port); - // convert the host to a binary form and store it in // address.sin_addr.s_addr if (inet_pton(AF_INET, host, &address.sin_addr) <= 0) { diff --git a/src/lib/net/net_serve.c b/src/lib/net/net_serve.c index 8078045..47ddd9e 100644 --- a/src/lib/net/net_serve.c +++ b/src/lib/net/net_serve.c @@ -1,4 +1,5 @@ #include "header.h" +#include "utils/logging/header.h" /** * Starts a server that listens for incoming connections on the specified host @@ -28,7 +29,7 @@ net_serve(char* host, ProtocolHandler handle_protocol, RequestContext* context) } int server_socket = net_listener(head, port); - printf("Serving requests on %d\n", port); + info("Serving requests on %s\n", host); while (1) { struct sockaddr_in client_addr; @@ -43,7 +44,7 @@ net_serve(char* host, ProtocolHandler handle_protocol, RequestContext* context) exit(1); } - printf("Accepted connection from %s:%d\n", + info("Accepted connection from %s:%d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port)); diff --git a/src/lib/utils/macros.h b/src/lib/utils/macros.h index e1ea409..0a0f575 100644 --- a/src/lib/utils/macros.h +++ b/src/lib/utils/macros.h @@ -20,22 +20,22 @@ #define da_append(array, value) \ do { \ - static_assert(is_same_type(array.len, size_t)); \ - static_assert(is_same_type(array.capacity, size_t)); \ - static_assert(is_same_type(*array.items, typeof(value))); \ - if (array.items == nullptr) { \ - array.len = 0; \ - array.capacity = 10; \ - array.items = \ - malloc(sizeof(*array.items) * array.capacity); \ + static_assert(is_same_type((array).len, size_t)); \ + static_assert(is_same_type((array).capacity, size_t)); \ + static_assert(is_same_type(*(array).items, typeof(value))); \ + if ((array).items == nullptr) { \ + (array).len = 0; \ + (array).capacity = 10; \ + (array).items = \ + malloc(sizeof(*(array).items) * (array).capacity); \ } \ - if (array.len + 1 > array.capacity) { \ - array.capacity *= 2; \ - array.items = \ - realloc(array.items, \ - array.capacity * sizeof(*array.items)); \ + if ((array).len + 1 > (array).capacity) { \ + (array).capacity *= 2; \ + (array).items = \ + realloc((array).items, \ + (array).capacity * sizeof(*(array).items)); \ } \ - if (array.items) array.items[array.len++] = value; \ + if ((array).items) (array).items[(array).len++] = value; \ } while (0) #endif // INCLUDE_UTILS_MACROS_H_ diff --git a/src/main.c b/src/main.c index 3814e6e..e2ace9e 100644 --- a/src/main.c +++ b/src/main.c @@ -1,6 +1,7 @@ #include "app/header.h" #include "lib/env/header.h" #include "lib/http/header.h" +#include "utils/logging/header.h" const int PORT = 9000; const char* HOST = "127.0.0.1"; @@ -29,14 +30,6 @@ main() router_add(router, "^/404$", Error404); router_add(router, "^/static/(.*)$", Static); - /* - * Used to check for memory leaks in allocation and deallocation of - * memory - */ - // router_free(router); - // printf("Freed test router\n"); - - printf("Server listening on %d\n", PORT); http.ListenAndServe(hostname, router); /* @@ -44,8 +37,8 @@ main() * CTRL + C * - Need to add a way to handle graceful shut down */ - printf("\n\n\t << Graceful Shutdown >>\n\n"); - router_free(router); + // info("\n\n\t << Graceful Shutdown >>\n\n"); + // router_free(router); return 0; } From 1c7009e324d98df21bfa96cf1fcaca6bfbcd07bf Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Fri, 13 Feb 2026 13:03:16 +0000 Subject: [PATCH 2/3] style: auto-format code with clang-format [skip ci] --- src/app/static.c | 2 +- src/lib/http/http_server.c | 2 +- src/lib/net/net_serve.c | 4 ++-- src/lib/utils/macros.h | 36 ++++++++++++++++++------------------ 4 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/app/static.c b/src/app/static.c index f37cab6..e2fb2ae 100644 --- a/src/app/static.c +++ b/src/app/static.c @@ -2,8 +2,8 @@ #include #include "header.h" -#include "utils/string/header.h" #include "utils/logging/header.h" +#include "utils/string/header.h" int Static(ResponseWriter* w, Request* r) diff --git a/src/lib/http/http_server.c b/src/lib/http/http_server.c index 224a487..1d03ca4 100644 --- a/src/lib/http/http_server.c +++ b/src/lib/http/http_server.c @@ -1,6 +1,6 @@ #include "header.h" -#include "utils/macros.h" #include "utils/logging/header.h" +#include "utils/macros.h" HttpServer http = { .ListenAndServe = listenAndServe, diff --git a/src/lib/net/net_serve.c b/src/lib/net/net_serve.c index 47ddd9e..51cf00a 100644 --- a/src/lib/net/net_serve.c +++ b/src/lib/net/net_serve.c @@ -45,8 +45,8 @@ net_serve(char* host, ProtocolHandler handle_protocol, RequestContext* context) } info("Accepted connection from %s:%d\n", - inet_ntoa(client_addr.sin_addr), - ntohs(client_addr.sin_port)); + inet_ntoa(client_addr.sin_addr), + ntohs(client_addr.sin_port)); // TODO: avoid using a direct integer in favor of a variable // network layer, read bytes from socket diff --git a/src/lib/utils/macros.h b/src/lib/utils/macros.h index 0a0f575..2846994 100644 --- a/src/lib/utils/macros.h +++ b/src/lib/utils/macros.h @@ -18,24 +18,24 @@ #define is_same_type(value, T) _Generic((value), T: true, default: false) -#define da_append(array, value) \ - do { \ - static_assert(is_same_type((array).len, size_t)); \ - static_assert(is_same_type((array).capacity, size_t)); \ - static_assert(is_same_type(*(array).items, typeof(value))); \ - if ((array).items == nullptr) { \ - (array).len = 0; \ - (array).capacity = 10; \ - (array).items = \ - malloc(sizeof(*(array).items) * (array).capacity); \ - } \ - if ((array).len + 1 > (array).capacity) { \ - (array).capacity *= 2; \ - (array).items = \ - realloc((array).items, \ - (array).capacity * sizeof(*(array).items)); \ - } \ - if ((array).items) (array).items[(array).len++] = value; \ +#define da_append(array, value) \ + do { \ + static_assert(is_same_type((array).len, size_t)); \ + static_assert(is_same_type((array).capacity, size_t)); \ + static_assert(is_same_type(*(array).items, typeof(value))); \ + if ((array).items == nullptr) { \ + (array).len = 0; \ + (array).capacity = 10; \ + (array).items = \ + malloc(sizeof(*(array).items) * (array).capacity); \ + } \ + if ((array).len + 1 > (array).capacity) { \ + (array).capacity *= 2; \ + (array).items = realloc( \ + (array).items, \ + (array).capacity * sizeof(*(array).items)); \ + } \ + if ((array).items) (array).items[(array).len++] = value; \ } while (0) #endif // INCLUDE_UTILS_MACROS_H_ From 979a4e76ce169c189ae6fe4116f233758d1e32a7 Mon Sep 17 00:00:00 2001 From: oduortoni Date: Wed, 18 Feb 2026 05:46:09 +0300 Subject: [PATCH 3/3] fix: remove unnecessary comments --- src/lib/http/http_server.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/lib/http/http_server.c b/src/lib/http/http_server.c index 1d03ca4..a9e9103 100644 --- a/src/lib/http/http_server.c +++ b/src/lib/http/http_server.c @@ -23,10 +23,6 @@ listenAndServe(char* host, Router* router) return -1; } - // info("http/server.c"); - // info("Router address: %p\n", (void*)router); - // info("Dispatcher address: %p\n", (void*)router->dispatcher); - if (!router->dispatcher) { for (size_t i = 0; i < ARRAY_LEN(router->patterns) && router->patterns[i];