Generate ids
This commit is contained in:
2
Makefile
2
Makefile
@@ -6,7 +6,7 @@ LIBWS_LIB=$(LIBWS)lib/libmicrohttpd.a
|
|||||||
LIBSHA=./vendor/sha256/
|
LIBSHA=./vendor/sha256/
|
||||||
LIBSHA_SRC=$(LIBSHA)sha256.c
|
LIBSHA_SRC=$(LIBSHA)sha256.c
|
||||||
|
|
||||||
LDFLAGS=-lm
|
LDFLAGS=-lm -luuid
|
||||||
INCL_FLAGS=-I$(LIBWS)include -I$(LIBSHA)
|
INCL_FLAGS=-I$(LIBWS)include -I$(LIBSHA)
|
||||||
|
|
||||||
main: src/main.c $(LIBSHA_SRC) output
|
main: src/main.c $(LIBSHA_SRC) output
|
||||||
|
|||||||
47
src/main.c
47
src/main.c
@@ -1,5 +1,6 @@
|
|||||||
#include <microhttpd.h>
|
#include <microhttpd.h>
|
||||||
#include <sha256.h>
|
#include <sha256.h>
|
||||||
|
#include <uuid/uuid.h>
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
#define NOH_IMPLEMENTATION
|
#define NOH_IMPLEMENTATION
|
||||||
#include "noh.h"
|
#include "noh.h"
|
||||||
@@ -15,6 +16,40 @@ This will ignore any auth headers and always log in with username demo.
|
|||||||
|
|
||||||
Noh_Arena temp;
|
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) {
|
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(
|
struct MHD_Response *response = MHD_create_response_from_buffer(
|
||||||
message.count, (void*)message.elems, MHD_RESPMEM_MUST_COPY);
|
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.
|
// Set entry data.
|
||||||
result->is_dir = type == DT_DIR;
|
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);
|
result->name = noh_sv_copy_cstr(&temp, dir_entry->d_name);
|
||||||
set_modified_time(result);
|
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) {
|
static void append_entry(Noh_String *response, SV url, Library_Entry entry) {
|
||||||
noh_string_append_cstr(response, "<entry>");
|
noh_string_append_cstr(response, "<entry>");
|
||||||
noh_string_append_cstr(response, "<id>TODO</id>");
|
noh_string_append_cstr(response, noh_arena_sprintf(&temp, "<id>"Nsv_Fmt"</id>", Nsv_Arg(get_id(entry.full_path))));
|
||||||
noh_string_append_cstr(response, noh_arena_sprintf(&temp, "<title>"Nsv_Fmt"</title>", entry.name));
|
noh_string_append_cstr(response, noh_arena_sprintf(&temp, "<title>"Nsv_Fmt"</title>", entry.name));
|
||||||
noh_string_append_cstr(response, noh_arena_sprintf(&temp, "<updated>%s</updated>", format_time(entry)));
|
noh_string_append_cstr(response, noh_arena_sprintf(&temp, "<updated>%s</updated>", format_time(entry)));
|
||||||
if (entry.is_dir) {
|
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 response = {0};
|
||||||
|
|
||||||
noh_string_append_cstr(&response, "<feed>");
|
noh_string_append_cstr(&response, "<feed>");
|
||||||
noh_string_append_cstr(&response, "<id>TODO</id>");
|
noh_string_append_cstr(&response, noh_arena_sprintf(&temp, "<id>"Nsv_Fmt"</id>", Nsv_Arg(get_id(self_path.full_path))));
|
||||||
noh_string_append_cstr(&response, noh_arena_sprintf(&temp, "<title>"Nsv_Fmt"</title>", self_path.name));
|
noh_string_append_cstr(&response, noh_arena_sprintf(&temp, "<title>"Nsv_Fmt"</title>", self_path.name));
|
||||||
noh_string_append_cstr(&response, noh_arena_sprintf(&temp, "<updated>%s</updated>", format_time(self_path)));
|
noh_string_append_cstr(&response, noh_arena_sprintf(&temp, "<updated>%s</updated>", 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 = {
|
Library_Entry self_entry = {
|
||||||
.is_dir = true,
|
.is_dir = true,
|
||||||
.full_path = 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)),
|
.name = (url.count == 0) ? settings.library_name : path_to_name(noh_sv_from_string(fs_path)),
|
||||||
.modified_at = {0},
|
.modified_at = {0},
|
||||||
};
|
};
|
||||||
set_modified_time(&self_entry);
|
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.
|
// Create and send response.
|
||||||
Noh_String response = create_feed_response(self_entry, url, list);
|
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.
|
// Cleanup.
|
||||||
noh_da_free(&list);
|
noh_da_free(&list);
|
||||||
|
|||||||
@@ -404,7 +404,7 @@ inline void noh_sv_trim_space(Noh_String_View *sv);
|
|||||||
Noh_String_View noh_sv_from_cstr(const char *cstr);
|
Noh_String_View noh_sv_from_cstr(const char *cstr);
|
||||||
|
|
||||||
// Creates a string view from a string.
|
// 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.
|
// Compares two string views.
|
||||||
// Returns:
|
// Returns:
|
||||||
@@ -1009,10 +1009,10 @@ Noh_String_View noh_sv_from_cstr(const char *cstr) {
|
|||||||
return result;
|
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};
|
Noh_String_View result = {0};
|
||||||
result.elems = string->elems;
|
result.elems = string.elems;
|
||||||
result.count = string->count;
|
result.count = string.count;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user