Simple routing and basic auth header checking

This commit is contained in:
2026-05-02 20:30:44 +02:00
parent 8bb4fb1ab7
commit 9ed13e0c45

View File

@@ -2,39 +2,83 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <microhttpd.h> #include <microhttpd.h>
#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( static enum MHD_Result request_handler(
void *cls, void *cls,
struct MHD_Connection *connection, 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, const char *upload_data, size_t *upload_data_size,
void **con_cls) { void **con_cls) {
(void)cls; (void)cls; (void)con_cls; (void)upload_data; (void)upload_data_size;
(void)url;
(void)method;
(void)version;
(void)upload_data;
(void)upload_data_size;
(void)con_cls;
// Create an HTTP response noh_log(NOH_INFO, "Request: %s %s %s", version, method, _url);
const char *response_text = "Hello, World!";
struct MHD_Response *response = MHD_create_response_from_buffer(
strlen(response_text), (void *)response_text, MHD_RESPMEM_PERSISTENT);
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 username = (SV){ .elems = auth->username, .count = auth->username_len };
printf("%s\n", response_text); SV password = { .elems = auth->password, .count = auth->password_len };
enum MHD_Result ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
MHD_destroy_response(response);
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) int main(void)
{ {
temp = noh_arena_init(1 * 1024 * 1024);
struct MHD_Daemon *server; struct MHD_Daemon *server;
// Start the HTTP server // Start the HTTP server