From c3a32993d50685abff8e8d4f7669350c98ae5a49 Mon Sep 17 00:00:00 2001 From: jobol Date: Tue, 22 Mar 2016 16:58:03 +0100 Subject: [PATCH] http-svc: handle alises more efficiently but buggy see later commit Change-Id: I82557a20f646bf3ceb159fc5e664a9a5655d2011 Signed-off-by: jobol --- src/http-svc.c | 50 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 38 insertions(+), 12 deletions(-) diff --git a/src/http-svc.c b/src/http-svc.c index 8af11d2e..f2a5baa4 100644 --- a/src/http-svc.c +++ b/src/http-svc.c @@ -328,10 +328,17 @@ static int afb_req_reply_file(struct afb_req *request, int dirfd, const char *fi return 1; } +struct afb_diralias { + const char *alias; + const char *directory; + size_t lendir; + int dirfd; +}; + static int handle_alias(struct afb_req *request, struct afb_req_post *post, void *data) { char *path; - const char *alias = data; + struct afb_diralias *da = data; size_t lenalias; if (request->method != afb_method_get) { @@ -344,16 +351,31 @@ static int handle_alias(struct afb_req *request, struct afb_req_post *post, void return 1; } - lenalias = strlen(alias); - path = alloca(lenalias + request->lentail + 1); - memcpy(path, alias, lenalias); - memcpy(&path[lenalias], request->tail, request->lentail + 1); - return afb_req_reply_file(request, AT_FDCWD, path); + return afb_req_reply_file(request, da->dirfd, &request->tail[request->lentail + 1]); } int afb_req_add_alias(AFB_session * session, const char *prefix, const char *alias, int priority) { - return afb_req_add_handler(session, prefix, handle_alias, (void *)alias, priority); + struct afb_diralias *da; + int dirfd; + + dirfd = open(alias, O_PATH|O_DIRECTORY); + if (dirfd < 0) { + /* TODO message */ + return 0; + } + da = malloc(sizeof *da); + if (da != NULL) { + da->alias = prefix; + da->directory = alias; + da->lendir = strlen(da->directory); + da->dirfd = dirfd; + if (afb_req_add_handler(session, prefix, handle_alias, (void *)alias, priority)) + return 1; + free(da); + } + close(dirfd); + return 0; } static int my_default_init(AFB_session * session) @@ -377,11 +399,15 @@ static int my_default_init(AFB_session * session) return 1; } -static int access_handler(void *cls, - struct MHD_Connection *connection, - const char *url, - const char *methodstr, - const char *version, const char *upload_data, size_t * upload_data_size, void **recorder) +static int access_handler( + void *cls, + struct MHD_Connection *connection, + const char *url, + const char *methodstr, + const char *version, + const char *upload_data, + size_t * upload_data_size, + void **recorder) { struct afb_req_post post; struct afb_req request; -- 2.16.6