Format requested file path in OPDS handler
This commit is contained in:
29
src/main.c
29
src/main.c
@@ -1,6 +1,3 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <microhttpd.h>
|
||||
#define NOH_IMPLEMENTATION
|
||||
#include "noh.h"
|
||||
@@ -24,15 +21,22 @@ static enum MHD_Result send_response(struct MHD_Connection *connection, int stat
|
||||
return ret;
|
||||
}
|
||||
|
||||
static enum MHD_Result handle_opds(struct MHD_Connection *connection, SV url, SV username) {
|
||||
(void)url;
|
||||
(void)username;
|
||||
static enum MHD_Result handle_opds(struct MHD_Connection *connection, SV url, SV base_path, SV username) {
|
||||
Noh_String fs_path = {0};
|
||||
noh_string_append_sv(&fs_path, base_path);
|
||||
noh_string_append_cstr(&fs_path, "/");
|
||||
noh_string_append_sv(&fs_path, username);
|
||||
noh_string_append_cstr(&fs_path, "/");
|
||||
noh_string_append_sv(&fs_path, url);
|
||||
|
||||
noh_log(NOH_INFO, "Rquest: "Nsv_Fmt, Nsv_Arg(fs_path));
|
||||
return send_response(connection, 200, sv("TODO: OPDS\n"));
|
||||
}
|
||||
|
||||
static enum MHD_Result handle_get(struct MHD_Connection *connection, SV url, SV username) {
|
||||
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"));
|
||||
}
|
||||
|
||||
@@ -44,7 +48,8 @@ static enum MHD_Result request_handler(
|
||||
const char *_url, const char *method, const char *version,
|
||||
const char *upload_data, size_t *upload_data_size,
|
||||
void **con_cls) {
|
||||
(void)cls; (void)con_cls; (void)upload_data; (void)upload_data_size;
|
||||
(void)con_cls; (void)upload_data; (void)upload_data_size;
|
||||
SV base_path = *(SV*)cls;
|
||||
|
||||
noh_log(NOH_INFO, "Request: %s %s %s", version, method, _url);
|
||||
|
||||
@@ -70,8 +75,8 @@ static enum MHD_Result request_handler(
|
||||
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);
|
||||
if (noh_sv_eq_ci(controller, sv("opds"))) return handle_opds(connection, url, base_path, username);
|
||||
if (noh_sv_eq_ci(controller, sv("get"))) return handle_get(connection, url, base_path, username);
|
||||
return send_response(connection, 404, sv(""));
|
||||
}
|
||||
|
||||
@@ -99,6 +104,8 @@ int main(int argc, char **argv)
|
||||
if (port < 1000 || port > 65535) show_usage(program_name, "Invalid port.");
|
||||
|
||||
if (argc < 1) show_usage(program_name, "Library path not provided.");
|
||||
char *library_path = noh_shift_args(&argc, &argv);
|
||||
SV base_path = sv(library_path);
|
||||
|
||||
struct MHD_Daemon *server;
|
||||
|
||||
@@ -107,7 +114,7 @@ int main(int argc, char **argv)
|
||||
MHD_USE_INTERNAL_POLLING_THREAD,
|
||||
port,
|
||||
NULL, NULL,
|
||||
&request_handler, NULL,
|
||||
&request_handler, &base_path,
|
||||
MHD_OPTION_END);
|
||||
|
||||
if (!server) {
|
||||
|
||||
Reference in New Issue
Block a user