improves index.html handling
authorJosé Bollo <jose.bollo@iot.bzh>
Thu, 7 Apr 2016 11:56:04 +0000 (13:56 +0200)
committerJosé Bollo <jose.bollo@iot.bzh>
Thu, 7 Apr 2016 11:56:04 +0000 (13:56 +0200)
Change-Id: I5209fafe92dcf77a2ae8e2a2431ab01c5d8f5df9
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
src/afb-hreq.c

index 8928266..badddd1 100644 (file)
@@ -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;