Compare commits

..

3 Commits

Author SHA1 Message Date
13e01894d8 build size optimizations 2026-04-18 19:02:23 -05:00
4a306746f9 add request url parsing / handling 2026-04-17 20:32:46 -05:00
3dc05cccb0 fix spacing 2026-04-11 15:08:22 -05:00
4 changed files with 75 additions and 18 deletions

View File

@@ -1,14 +1,17 @@
CC = gcc CC = gcc
INCLUDE := include INCLUDE := include
CFLAGS = -Wall -I$(INCLUDE) CFLAGS = -Wall -I$(INCLUDE) -Os -ffunction-sections -fdata-sections -Wl,--gc-sections
BUILD_DIR := build BUILD_DIR := build
all: server all: server
server: $(BUILD_DIR)/server.o $(BUILD_DIR)/mongoose.o debug: CFLAGS += -g
$(CC) $(CFLAGS) -o $(BUILD_DIR)/server $(BUILD_DIR)/server.o $(BUILD_DIR)/mongoose.o debug: server
server: $(BUILD_DIR)/server.o $(BUILD_DIR)/mongoose.o $(BUILD_DIR)/array.o
$(CC) $(CFLAGS) -o $(BUILD_DIR)/server $(BUILD_DIR)/server.o $(BUILD_DIR)/mongoose.o $(BUILD_DIR)/array.o
$(BUILD_DIR)/%.o: %.c | $(BUILD_DIR) $(BUILD_DIR)/%.o: %.c | $(BUILD_DIR)
$(CC) $(CFLAGS) -c $< -o $@ $(CC) $(CFLAGS) -c $< -o $@

30
server/array.c Normal file
View File

@@ -0,0 +1,30 @@
#include "include/array.h"
#include "include/mongoose.h"
void array_mg_str_append(array_mg_str* arr, struct mg_str item) {
if (arr->count >= arr->capacity) {
if (arr->capacity == 0) arr->capacity = 8;
else arr->capacity *= 2;
arr->items = realloc(arr->items, arr->capacity * sizeof(*arr->items));
}
arr->items[arr->count++] = item;
}
void split_mg_str(struct mg_str* string, const char token, array_mg_str* return_array) {
int start_index = 0;
for (int i = 0; i < string->len; i++) {
if (string->buf[i] == token || string->len == i + 1) {
if (i != start_index) {
int slice_len = i - start_index;
if (string->len == i + 1 && string->buf[i] != token) slice_len++;
struct mg_str tmp_mg_str = mg_str_n(string->buf + start_index, slice_len);
array_mg_str_append(return_array, tmp_mg_str);
}
start_index = i + 1;
}
}
}

15
server/include/array.h Normal file
View File

@@ -0,0 +1,15 @@
#ifndef HMAIL_ARRAY_H
#define HMAIL_ARRAY_H
#include "mongoose.h"
typedef struct {
struct mg_str* items;
size_t count;
size_t capacity;
} array_mg_str;
void array_mg_str_append(array_mg_str* arr, struct mg_str item);
void split_mg_str(struct mg_str* string, const char token, array_mg_str* return_array);
#endif

View File

@@ -1,13 +1,22 @@
#include "include/mongoose.h" #include "include/mongoose.h"
#include "include/array.h"
static void api_handler(struct mg_connection *c, int ev, struct mg_http_message *hm) {
mg_http_reply(c, 200, "", "%s : %.*s", "api route response", hm->uri.len, hm->uri.buf);
}
static void ev_handler(struct mg_connection *c, int ev, void *ev_data) { static void ev_handler(struct mg_connection *c, int ev, void *ev_data) {
if (ev == MG_EV_HTTP_MSG) { if (ev == MG_EV_HTTP_MSG) {
struct mg_http_message *hm = (struct mg_http_message *) ev_data; struct mg_http_message *hm = (struct mg_http_message *)ev_data;
printf("URL: %.*s\n", (int) hm->uri.len, hm->uri.buf);
if (mg_match(hm->uri, mg_str("/api/test"), NULL)) { array_mg_str path_array = {0};
mg_http_reply(c, 200, "", "%s", "test"); split_mg_str(&hm->uri, '/', &path_array);
if (path_array.count > 0) {
if (mg_match(path_array.items[0], mg_str("api"), NULL)) {
api_handler(c, ev, hm);
return; return;
} }
}
mg_http_reply(c, 200, "", "%s", "<html>Hello world</html>"); mg_http_reply(c, 200, "", "%s", "<html>Hello world</html>");
} }
} }