diff --git a/include/kore/kore.h b/include/kore/kore.h index 5b83cc6..ea4fd5d 100644 --- a/include/kore/kore.h +++ b/include/kore/kore.h @@ -649,6 +649,7 @@ int kore_msg_register(u_int8_t, void kore_filemap_init(void); int kore_filemap_create(struct kore_domain *, const char *, const char *); +extern char *kore_filemap_ext; extern char *kore_filemap_index; #endif diff --git a/src/config.c b/src/config.c index cefebbf..cd4e631 100644 --- a/src/config.c +++ b/src/config.c @@ -82,6 +82,7 @@ static int configure_dynamic_handler(char *); static int configure_accesslog(char *); static int configure_http_header_max(char *); static int configure_http_body_max(char *); +static int configure_filemap_ext(char *); static int configure_filemap_index(char *); static int configure_http_media_type(char *); static int configure_http_hsts_enable(char *); @@ -151,6 +152,7 @@ static struct { #endif #if !defined(KORE_NO_HTTP) { "filemap", configure_filemap }, + { "filemap_ext", configure_filemap_ext }, { "filemap_index", configure_filemap_index }, { "static", configure_static_handler }, { "dynamic", configure_dynamic_handler }, @@ -661,6 +663,15 @@ configure_accesslog(char *path) return (KORE_RESULT_OK); } +static int +configure_filemap_ext(char *ext) +{ + kore_free(kore_filemap_ext); + kore_filemap_ext = kore_strdup(ext); + + return (KORE_RESULT_OK); +} + static int configure_filemap_index(char *index) { diff --git a/src/filemap.c b/src/filemap.c index 86c9c30..09e90db 100644 --- a/src/filemap.c +++ b/src/filemap.c @@ -42,6 +42,7 @@ static void filemap_serve(struct http_request *, struct filemap_entry *); static TAILQ_HEAD(, filemap_entry) maps; +char *kore_filemap_ext = NULL; char *kore_filemap_index = NULL; void @@ -126,11 +127,13 @@ filemap_serve(struct http_request *req, struct filemap_entry *map) { struct stat st; struct kore_fileref *ref; + const char *path; int len, fd, index; char fpath[MAXPATHLEN]; - len = snprintf(fpath, sizeof(fpath), "%s/%s", map->ondisk, - req->path + map->root_len); + path = req->path + map->root_len; + + len = snprintf(fpath, sizeof(fpath), "%s/%s", map->ondisk, path); if (len == -1 || (size_t)len >= sizeof(fpath)) { http_response(req, HTTP_STATUS_INTERNAL_ERROR, NULL, 0); return; @@ -153,7 +156,22 @@ lookup: if ((fd = open(fpath, O_RDONLY | O_NOFOLLOW)) == -1) { switch (errno) { case ENOENT: - req->status = HTTP_STATUS_NOT_FOUND; + if (index || kore_filemap_ext == NULL) { + req->status = HTTP_STATUS_NOT_FOUND; + } else { + len = snprintf(fpath, sizeof(fpath), + "%s/%s%s", map->ondisk, path, + kore_filemap_ext); + if (len == -1 || + (size_t)len >= sizeof(fpath)) { + http_response(req, + HTTP_STATUS_INTERNAL_ERROR, + NULL, 0); + return; + } + index++; + goto lookup; + } break; case EPERM: case EACCES: @@ -191,8 +209,7 @@ lookup: } } else if (S_ISDIR(st.st_mode) && index == 0) { len = snprintf(fpath, sizeof(fpath), - "%s/%s%s", map->ondisk, - req->path + map->root_len, + "%s/%s%s", map->ondisk, path, kore_filemap_index != NULL ? kore_filemap_index : "index.html"); if (len == -1 || (size_t)len >= sizeof(fpath)) {