From 9ed13e0c45754b8216dec597842a8ac402be5c49 Mon Sep 17 00:00:00 2001 From: ThoNohT Date: Sat, 2 May 2026 20:30:44 +0200 Subject: [PATCH] Simple routing and basic auth header checking --- src/main.c | 80 ++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 62 insertions(+), 18 deletions(-) diff --git a/src/main.c b/src/main.c index 0dcf0c2..28737e3 100644 --- a/src/main.c +++ b/src/main.c @@ -2,39 +2,83 @@ #include #include #include +#define NOH_IMPLEMENTATION +#include "noh.h" + + +/* To disable Basic auth for testing: +#define NO_AUTH +This will ignore any auth headers and always log in with username demo. +*/ + +#define SV Noh_String_View +#define sv noh_sv_from_cstr + +Noh_Arena temp; + +static enum MHD_Result send_response(struct MHD_Connection *connection, int status, SV message) { + struct MHD_Response *response = MHD_create_response_from_buffer( + message.count, (void*)message.elems, MHD_RESPMEM_PERSISTENT); + if (!response) return MHD_NO; + + enum MHD_Result ret = MHD_queue_response(connection, status, response); + MHD_destroy_response(response); + return ret; +} + +static enum MHD_Result handle_opds(struct MHD_Connection *connection, SV url) { + (void)url; + return send_response(connection, 200, sv("TODO: OPDS\n")); +} + +static enum MHD_Result handle_get(struct MHD_Connection *connection, SV url) { + (void)url; + return send_response(connection, 200, sv("TODO: Get\n")); +} + +bool is_slash(const char c) { return c == '/'; } static enum MHD_Result request_handler( void *cls, struct MHD_Connection *connection, - const char *url, const char *method, const char *version, + const char *_url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **con_cls) { - (void)cls; - (void)url; - (void)method; - (void)version; - (void)upload_data; - (void)upload_data_size; - (void)con_cls; + (void)cls; (void)con_cls; (void)upload_data; (void)upload_data_size; - // Create an HTTP response - const char *response_text = "Hello, World!"; - struct MHD_Response *response = MHD_create_response_from_buffer( - strlen(response_text), (void *)response_text, MHD_RESPMEM_PERSISTENT); + noh_log(NOH_INFO, "Request: %s %s %s", version, method, _url); - if (!response) return MHD_NO; + SV username; +#ifdef NO_AUTH + username = sv("demo"); +#else + struct MHD_BasicAuthInfo *auth = MHD_basic_auth_get_username_password3(connection); + if (!auth) return send_response(connection, 401, sv("")); - // Send the response - printf("%s\n", response_text); - enum MHD_Result ret = MHD_queue_response(connection, MHD_HTTP_OK, response); - MHD_destroy_response(response); + username = (SV){ .elems = auth->username, .count = auth->username_len }; + SV password = { .elems = auth->password, .count = auth->password_len }; - return ret; + // TODO: Read username from user config. + if (!noh_sv_eq(password, sv("jaja"))) return send_response(connection, 401, sv("")); +#endif + + noh_log(NOH_INFO, "Authenticated as "Nsv_Fmt, Nsv_Arg(username)); + + if (strcmp(method, "GET") != 0) return send_response(connection, 405, sv("")); + + SV url = sv(_url); + noh_sv_trim_left(&url, is_slash);; + SV controller = noh_sv_chop_by_delim(&url, '/'); + + if (noh_sv_eq_ci(controller, sv("opds"))) return handle_opds(connection, url, username); + if (noh_sv_eq_ci(controller, sv("get"))) return handle_get(connection, url, username); + return send_response(connection, 404, sv("")); } int main(void) { + temp = noh_arena_init(1 * 1024 * 1024); struct MHD_Daemon *server; // Start the HTTP server