commit 04077c66b6b12abb01cb0db87bff891bb510c388
parent b5e122419b5c443dfb93a94276ea8c510f297a47
Author: Joris Vink <joris@coders.se>
Date: Tue, 3 Jul 2018 19:58:43 +0200
Add filemap_ext configuration option.
Allows you to specify the default extensions used for a file served
via a filemap, eg:
filemap_ext .html
Gives us ability to provide clean urls.
Diffstat:
3 files changed, 34 insertions(+), 5 deletions(-)
diff --git 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
@@ -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 },
@@ -662,6 +664,15 @@ configure_accesslog(char *path)
}
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)
{
kore_free(kore_filemap_index);
diff --git 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)) {