diff options
author | jobol <jobol@nonadev.net> | 2016-03-22 16:58:03 +0100 |
---|---|---|
committer | José Bollo <jose.bollo@iot.bzh> | 2016-03-23 13:15:57 +0100 |
commit | c3a32993d50685abff8e8d4f7669350c98ae5a49 (patch) | |
tree | 2af42f1e7188040cef82d8c18aaadbe1d98aea7c | |
parent | 2a3fbda636c93c9f22a064e5c5f537c2232d626d (diff) |
http-svc: handle alises more efficiently
but buggy see later commit
Change-Id: I82557a20f646bf3ceb159fc5e664a9a5655d2011
Signed-off-by: jobol <jobol@nonadev.net>
-rw-r--r-- | src/http-svc.c | 50 |
1 files 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; |