diff --git a/src/app/static.c b/src/app/static.c index 6bbae71..e2fb2ae 100644 --- a/src/app/static.c +++ b/src/app/static.c @@ -2,6 +2,7 @@ #include #include "header.h" +#include "utils/logging/header.h" #include "utils/string/header.h" int @@ -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..a9e9103 100644 --- a/src/lib/http/http_server.c +++ b/src/lib/http/http_server.c @@ -1,4 +1,5 @@ #include "header.h" +#include "utils/logging/header.h" #include "utils/macros.h" HttpServer http = { @@ -16,27 +17,21 @@ 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); - 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..51cf00a 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,9 +44,9 @@ net_serve(char* host, ProtocolHandler handle_protocol, RequestContext* context) exit(1); } - printf("Accepted connection from %s:%d\n", - inet_ntoa(client_addr.sin_addr), - ntohs(client_addr.sin_port)); + info("Accepted connection from %s:%d\n", + 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 e1ea409..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_ 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; }