makes a function to ensure trailing slash
authorJosé Bollo <jose.bollo@iot.bzh>
Thu, 25 Aug 2016 21:11:17 +0000 (23:11 +0200)
committerJosé Bollo <jose.bollo@iot.bzh>
Thu, 25 Aug 2016 21:54:20 +0000 (23:54 +0200)
This function is usefull to ensure that links to otherfile
are epanded to directory/otherfile (not directoryotherfile!)

Change-Id: If8ad1c275598b322516c41706b464d5d90067a8e
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
src/afb-hreq.c
src/afb-hreq.h
src/afb-hswitch.c

index d1a262f..a4ccf67 100644 (file)
@@ -359,6 +359,22 @@ void afb_hreq_reply_error(struct afb_hreq *hreq, unsigned int status)
        afb_hreq_reply_empty(hreq, status, NULL);
 }
 
+int afb_hreq_redirect_to_ending_slash_if_needed(struct afb_hreq *hreq)
+{
+       char *tourl;
+
+       if (hreq->url[hreq->lenurl - 1] == '/')
+               return 0;
+
+       /* the redirect is needed for reliability of relative path */
+       tourl = alloca(hreq->lenurl + 2);
+       memcpy(tourl, hreq->url, hreq->lenurl);
+       tourl[hreq->lenurl] = '/';
+       tourl[hreq->lenurl + 1] = 0;
+       afb_hreq_redirect_to(hreq, tourl, 1);
+       return 1;
+}
+
 int afb_hreq_reply_file_if_exist(struct afb_hreq *hreq, int dirfd, const char *filename)
 {
        int rc;
@@ -396,17 +412,10 @@ 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, 1);
-               } else {
+               rc = afb_hreq_redirect_to_ending_slash_if_needed(hreq);
+               if (rc == 0) {
                        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) {
                                        rc = afb_hreq_reply_file_if_exist(hreq, fd, indexes[i]);
@@ -564,7 +573,7 @@ static char *url_with_query(struct afb_hreq *hreq, const char *url)
        return mkq.text;
 }
 
-int afb_hreq_redirect_to(struct afb_hreq *hreq, const char *url, int add_query_part)
+void afb_hreq_redirect_to(struct afb_hreq *hreq, const char *url, int add_query_part)
 {
        const char *to;
        char *wqp;
@@ -575,7 +584,6 @@ int afb_hreq_redirect_to(struct afb_hreq *hreq, const char *url, int add_query_p
                        MHD_HTTP_HEADER_LOCATION, to, NULL);
        DEBUG("redirect from [%s] to [%s]", hreq->url, url);
        free(wqp);
-       return 1;
 }
 
 const char *afb_hreq_get_cookie(struct afb_hreq *hreq, const char *name)
index 772cd67..81dd3ea 100644 (file)
@@ -61,7 +61,9 @@ extern int afb_hreq_reply_file_if_exist(struct afb_hreq *request, int dirfd, con
 
 extern int afb_hreq_reply_file(struct afb_hreq *request, int dirfd, const char *filename);
 
-extern int afb_hreq_redirect_to(struct afb_hreq *request, const char *url, int add_query_part);
+extern void afb_hreq_redirect_to(struct afb_hreq *request, const char *url, int add_query_part);
+
+extern int afb_hreq_redirect_to_ending_slash_if_needed(struct afb_hreq *hreq);
 
 extern const char *afb_hreq_get_cookie(struct afb_hreq *hreq, const char *name);
 
index c6552d4..621856e 100644 (file)
@@ -73,7 +73,8 @@ int afb_hswitch_one_page_api_redirect(struct afb_hreq *hreq, void *data)
        url[plen++] = '#';
        url[plen++] = '!';
        memcpy(&url[plen], &hreq->tail[1], hreq->lentail);
-       return afb_hreq_redirect_to(hreq, url, 1);
+       afb_hreq_redirect_to(hreq, url, 1);
+       return 1;
 }
 
 int afb_hswitch_websocket_switch(struct afb_hreq *hreq, void *data)