Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/app/static.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <sys/stat.h>

#include "header.h"
#include "utils/logging/header.h"
#include "utils/string/header.h"

int
Expand All @@ -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);
}

Expand Down
1 change: 0 additions & 1 deletion src/lib/http/http_handle.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
13 changes: 4 additions & 9 deletions src/lib/http/http_server.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "header.h"
#include "utils/logging/header.h"
#include "utils/macros.h"

HttpServer http = {
Expand All @@ -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);
Expand Down
15 changes: 2 additions & 13 deletions src/lib/http/route_dispatchers/regex_dispatcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
2 changes: 0 additions & 2 deletions src/lib/net/net_listener.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
9 changes: 5 additions & 4 deletions src/lib/net/net_serve.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "header.h"
#include "utils/logging/header.h"

/**
* Starts a server that listens for incoming connections on the specified host
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down
36 changes: 18 additions & 18 deletions src/lib/utils/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_
13 changes: 3 additions & 10 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -29,23 +30,15 @@ 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);

/*
* TODO: This is never reached due to infinite listener that stops on
* 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);
Comment thread
koutoftimer marked this conversation as resolved.

return 0;
}