diff --git a/Makefile b/Makefile index 4615429..d99b209 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ LIBWS_LIB=$(LIBWS)lib/libmicrohttpd.a LIBSHA=./vendor/sha256/ LIBSHA_SRC=$(LIBSHA)sha256.c -LDFLAGS=-lm +LDFLAGS=-lm -luuid INCL_FLAGS=-I$(LIBWS)include -I$(LIBSHA) main: src/main.c $(LIBSHA_SRC) output diff --git a/src/main.c b/src/main.c index 841c709..2fe7f9d 100644 --- a/src/main.c +++ b/src/main.c @@ -1,5 +1,6 @@ #include #include +#include #include #define NOH_IMPLEMENTATION #include "noh.h" @@ -15,6 +16,40 @@ This will ignore any auth headers and always log in with username demo. Noh_Arena temp; +typedef struct { + Noh_String path; + Noh_String id; +} Id; + +typedef struct { + Id *elems; + size_t count; + size_t capacity; +} Id_Store; + +Id_Store id_store = {0}; + +static SV get_id(SV path) { + for (size_t i = 0; i < id_store.count; i++) { + if (noh_sv_eq(path, noh_sv_from_string(id_store.elems[i].path))) return noh_sv_from_string(id_store.elems[i].id); + } + + uuid_t uuid; + uuid_generate(uuid); + char uuid_str[36]; + uuid_unparse(uuid, uuid_str); + + Noh_String new_id = {0}; + noh_string_append_cstr(&new_id, "urn:uuid:"); + noh_string_append_cstr(&new_id, uuid_str); + + Noh_String stored_path = {0}; + noh_string_append_sv(&stored_path, path); + + noh_da_append(&id_store, ((Id){ .path = stored_path, .id = new_id })); + return noh_sv_from_string(new_id); +} + static enum MHD_Result send_response(struct MHD_Connection *connection, int status, SV message, bool xml) { struct MHD_Response *response = MHD_create_response_from_buffer( message.count, (void*)message.elems, MHD_RESPMEM_MUST_COPY); @@ -111,7 +146,7 @@ static bool create_entry(struct dirent *dir_entry, const char *parent_path, Libr // Set entry data. result->is_dir = type == DT_DIR; - result->full_path = noh_sv_from_string(&full_path); + result->full_path = noh_sv_from_string(full_path); result->name = noh_sv_copy_cstr(&temp, dir_entry->d_name); set_modified_time(result); @@ -130,7 +165,7 @@ static char *format_time(Library_Entry entry) { static void append_entry(Noh_String *response, SV url, Library_Entry entry) { noh_string_append_cstr(response, ""); - noh_string_append_cstr(response, "TODO"); + noh_string_append_cstr(response, noh_arena_sprintf(&temp, ""Nsv_Fmt"", Nsv_Arg(get_id(entry.full_path)))); noh_string_append_cstr(response, noh_arena_sprintf(&temp, ""Nsv_Fmt"", entry.name)); noh_string_append_cstr(response, noh_arena_sprintf(&temp, "%s", format_time(entry))); if (entry.is_dir) { @@ -152,7 +187,7 @@ static Noh_String create_feed_response(Library_Entry self_path, SV url, Library_ Noh_String response = {0}; noh_string_append_cstr(&response, ""); - noh_string_append_cstr(&response, "TODO"); + noh_string_append_cstr(&response, noh_arena_sprintf(&temp, ""Nsv_Fmt"", Nsv_Arg(get_id(self_path.full_path)))); noh_string_append_cstr(&response, noh_arena_sprintf(&temp, ""Nsv_Fmt"", self_path.name)); noh_string_append_cstr(&response, noh_arena_sprintf(&temp, "%s", format_time(self_path))); @@ -182,8 +217,8 @@ static enum MHD_Result handle_opds(struct MHD_Connection *connection, SV url, Se Library_Entry self_entry = { .is_dir = true, - .full_path = noh_sv_from_string(&fs_path), - .name = (url.count == 0) ? settings.library_name : path_to_name(noh_sv_from_string(&fs_path)), + .full_path = noh_sv_from_string(fs_path), + .name = (url.count == 0) ? settings.library_name : path_to_name(noh_sv_from_string(fs_path)), .modified_at = {0}, }; set_modified_time(&self_entry); @@ -205,7 +240,7 @@ static enum MHD_Result handle_opds(struct MHD_Connection *connection, SV url, Se // 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); + bool result = send_response(connection, 200, noh_sv_from_string(response), true); // Cleanup. noh_da_free(&list); diff --git a/src/noh.h b/src/noh.h index f57a8ff..66aca74 100644 --- a/src/noh.h +++ b/src/noh.h @@ -404,7 +404,7 @@ inline void noh_sv_trim_space(Noh_String_View *sv); Noh_String_View noh_sv_from_cstr(const char *cstr); // Creates a string view from a string. -Noh_String_View noh_sv_from_string(const Noh_String *string); +Noh_String_View noh_sv_from_string(const Noh_String string); // Compares two string views. // Returns: @@ -1009,10 +1009,10 @@ Noh_String_View noh_sv_from_cstr(const char *cstr) { return result; } -Noh_String_View noh_sv_from_string(const Noh_String *string) { +Noh_String_View noh_sv_from_string(const Noh_String string) { Noh_String_View result = {0}; - result.elems = string->elems; - result.count = string->count; + result.elems = string.elems; + result.count = string.count; return result; }