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.
This commit is contained in:
Joris Vink 2018-07-03 19:58:43 +02:00
parent b5e122419b
commit 04077c66b6
3 changed files with 34 additions and 5 deletions

View File

@ -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

View File

@ -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)
{

View File

@ -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)) {