X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fafb-hreq.c;h=953c80a15372af2df1ad043695f36a978fd12889;hb=83ce3b29a598141f8f28afde655beb07ec78c312;hp=aff204d9a30d08cb5b15e85daee4c12b97e8fe43;hpb=fc19b7d7974f9c64dffc40e180464d595a9805cd;p=src%2Fapp-framework-binder.git diff --git a/src/afb-hreq.c b/src/afb-hreq.c index aff204d9..953c80a1 100644 --- a/src/afb-hreq.c +++ b/src/afb-hreq.c @@ -27,12 +27,12 @@ #include #include +#include #if defined(USE_MAGIC_MIME_TYPE) #include #endif -#include "local-def.h" #include "afb-method.h" #include "afb-req-itf.h" #include "afb-hreq.h" @@ -312,15 +312,24 @@ int afb_hreq_reply_file_if_exist(struct afb_hreq *hreq, int dirfd, const char *f /* serve directory */ if (S_ISDIR(st.st_mode)) { - if (hreq->url[hreq->lenurl - 1] != '/') { - /* the redirect is needed for reliability of relative path */ - char *tourl = alloca(hreq->lenurl + 2); - memcpy(tourl, hreq->url, hreq->lenurl); - tourl[hreq->lenurl] = '/'; - tourl[hreq->lenurl + 1] = 0; - rc = afb_hreq_redirect_to(hreq, tourl); - } else { - rc = afb_hreq_reply_file_if_exist(hreq, fd, "index.html"); + static const char *indexes[] = { "index.html", NULL }; + int i = 0; + rc = 0; + while (indexes[i] != NULL) { + if (faccessat(fd, indexes[i], R_OK, 0) == 0) { + if (hreq->url[hreq->lenurl - 1] != '/') { + /* the redirect is needed for reliability of relative path */ + char *tourl = alloca(hreq->lenurl + 2); + memcpy(tourl, hreq->url, hreq->lenurl); + tourl[hreq->lenurl] = '/'; + tourl[hreq->lenurl + 1] = 0; + rc = afb_hreq_redirect_to(hreq, tourl); + } else { + rc = afb_hreq_reply_file_if_exist(hreq, fd, indexes[i]); + } + break; + } + i++; } close(fd); return rc; @@ -371,7 +380,7 @@ int afb_hreq_reply_file_if_exist(struct afb_hreq *hreq, int dirfd, const char *f } /* fills the value and send */ - MHD_add_response_header(response, MHD_HTTP_HEADER_CACHE_CONTROL, hreq->session->cacheTimeout); + MHD_add_response_header(response, MHD_HTTP_HEADER_CACHE_CONTROL, hreq->cacheTimeout); MHD_add_response_header(response, MHD_HTTP_HEADER_ETAG, etag); MHD_queue_response(hreq->connection, status, response); MHD_destroy_response(response); @@ -438,13 +447,15 @@ static int opentempfile(char **path) int fd; char *fname; - fname = strdup("XXXXXX"); + fname = strdup("XXXXXX"); /* TODO improve the path */ if (fname == NULL) return -1; - fd = mkostemp(fname, O_CLOEXEC); + fd = mkostemp(fname, O_CLOEXEC|O_WRONLY); if (fd < 0) free(fname); + else + *path = fname; return fd; } @@ -454,6 +465,7 @@ int afb_hreq_post_add_file(struct afb_hreq *hreq, const char *key, const char *f ssize_t sz; struct hreq_data *hdat = get_data(hreq, key, 1); +fprintf(stderr, "%s=%s %s=%s %s\n",key,hdat->key,file,hdat->value,hdat->path); if (hdat->value == NULL) { hdat->value = strdup(file); if (hdat->value == NULL) @@ -516,8 +528,8 @@ static int _iterargs_(struct iterdata *id, enum MHD_ValueKind kind, const char * return 1; return id->iterator(id->closure, (struct afb_arg){ .name = key, - .value = value, - .size = 0, + .value = value ? : "", + .size = value ? strlen(value) : 0, .path = NULL }); }