Implement get handler

This commit is contained in:
2026-05-19 14:46:24 +02:00
parent bacda4cd00
commit 22b796ae26
3 changed files with 74 additions and 38 deletions

View File

@@ -1,6 +1,30 @@
static enum MHD_Result handle_get(struct MHD_Connection *connection, SV url, SV base_path, SV username) {
(void)url;
(void)username;
(void)base_path;
return send_response(connection, 200, sv("TODO: Get\n"), false);
static enum MHD_Result handle_get(struct MHD_Connection *connection, SV url, Settings settings, SV username) {
noh_arena_save(&temp);
Noh_String fs_path = build_path(false, &settings.base_path, &username, &url);
noh_log(NOH_INFO, "Physical path: "Nsv_Fmt, Nsv_Arg(fs_path));
if (!noh_sv_ends_with(noh_sv_from_string(fs_path), sv(".epub"))) {
return send_response(connection, 400, sv("File is not an epub file."), false);
}
noh_string_append_null(&fs_path);
// Try to open the file.
FILE *file = fopen(fs_path.elems, "r");
if (file == NULL) {
if (errno == ENOENT) {
noh_arena_reset(&temp);
return send_response(connection, 404, sv("File does not exist."), false);
} else {
noh_log(NOH_ERROR, "Opening file failed.");
noh_arena_reset(&temp);
return send_response(connection, 500, sv(""), false);
}
}
fseek(file, 0L, SEEK_END);
size_t file_size = ftell(file);
fseek(file, 0L, SEEK_SET);
return send_epub_file(connection, fileno(file), file_size);
}