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

@@ -62,39 +62,40 @@ static enum MHD_Result handle_opds(struct MHD_Connection *connection, SV url, Se
noh_log(NOH_INFO, "Physical path: "Nsv_Fmt, Nsv_Arg(self_entry.full_path));
DIR *dir = opendir(fs_path_cstr.elems);
if (dir) {
Library_Entries list = {0};
if (!dir) {
if (errno == ENOENT) {
closedir(dir);
noh_arena_reset(&temp);
return send_response(connection, 404, sv(""), false);
// Build up list of entries.
struct dirent *entry = noh_arena_alloc(&temp, sizeof(struct dirent));
while ((entry = readdir(dir)) != NULL) {
Library_Entry result = {0};
if (create_entry(entry, fs_path_cstr.elems, &result)) noh_da_append(&list, result);
} else {
noh_log(NOH_ERROR, "Opening directory failed.");
noh_arena_reset(&temp);
return send_response(connection, 500, sv(""), false);
}
// Sort entries, folders first, then by name.
if (list.count > 1) qsort(list.elems, list.count, sizeof(list.elems[0]), compare_library_entries);
// Create and send response.
Noh_String response = create_feed_response(self_entry, url, list);
bool result = send_response(connection, 200, noh_sv_from_string(response), true);
// Cleanup.
noh_da_free(&list);
noh_string_free(&response);
noh_arena_reset(&temp);
closedir(dir);
return result;
} else if (errno == ENOENT) {
closedir(dir);
noh_arena_reset(&temp);
return send_response(connection, 404, sv(""), false);
} else {
noh_log(NOH_ERROR, "Opening directory failed.");
noh_arena_reset(&temp);
return send_response(connection, 500, sv(""), false);
}
Library_Entries list = {0};
// Build up list of entries.
struct dirent *entry = noh_arena_alloc(&temp, sizeof(struct dirent));
while ((entry = readdir(dir)) != NULL) {
Library_Entry result = {0};
if (create_entry(entry, fs_path_cstr.elems, &result)) noh_da_append(&list, result);
}
// Sort entries, folders first, then by name.
if (list.count > 1) qsort(list.elems, list.count, sizeof(list.elems[0]), compare_library_entries);
// Create and send response.
Noh_String response = create_feed_response(self_entry, url, list);
bool result = send_response(connection, 200, noh_sv_from_string(response), true);
// Cleanup.
noh_da_free(&list);
noh_string_free(&response);
noh_arena_reset(&temp);
closedir(dir);
return result;
}