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;
/* 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]);
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;
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)
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);