diff options
author | José Bollo <jose.bollo@iot.bzh> | 2016-08-25 23:11:17 +0200 |
---|---|---|
committer | José Bollo <jose.bollo@iot.bzh> | 2016-08-25 23:54:20 +0200 |
commit | c44a48f295de04e4d0385b05d1afd93fac898d10 (patch) | |
tree | 68a6369f354dfbe8bfe4e4e266857dfcf8e5440a | |
parent | 240329595f08192e7b88918cc8868b5af9504dc2 (diff) |
makes a function to ensure trailing slash
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>
-rw-r--r-- | src/afb-hreq.c | 30 | ||||
-rw-r--r-- | src/afb-hreq.h | 4 | ||||
-rw-r--r-- | src/afb-hswitch.c | 3 |
3 files changed, 24 insertions, 13 deletions
diff --git a/src/afb-hreq.c b/src/afb-hreq.c index d1a262f7..a4ccf676 100644 --- a/src/afb-hreq.c +++ b/src/afb-hreq.c @@ -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) diff --git a/src/afb-hreq.h b/src/afb-hreq.h index 772cd677..81dd3eaf 100644 --- a/src/afb-hreq.h +++ b/src/afb-hreq.h @@ -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); diff --git a/src/afb-hswitch.c b/src/afb-hswitch.c index c6552d44..621856e9 100644 --- a/src/afb-hswitch.c +++ b/src/afb-hswitch.c @@ -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) |