Static: supporting new "index" option.

This supports a new option "index" that configures a custom index
file name to be served when a directory is requested.  This
initial support only allows a single fixed string.  An example:

{
	"share": "/www/data/static/$uri",
	"index": "lookatthis.htm"
}

When <example.com/foo/bar/> is requested,
</www/data/static/foo/bar/lookatthis.html> is served.

Default is "index.html".

===

nxt_conf_validator.c:

Accept "index" as a member of "share", and make sure it's a string.

===

I tried this feature in my own computer, where I tried the
following:

- Setting "index" to "lookatthis.htm", and check that the correct
  file is being served (check both a different name and a
  different extension).
- Not setting "index", and check that <index.html> is being
  served.
- Settind "index" to an array of strings, and check that the
  configuration fails:

{
	"error": "Invalid configuration.",
	"detail": "The \"index\" value must be a string, but not an array."
}
This commit is contained in:
Alejandro Colomar
2021-12-09 03:00:23 +01:00
parent 237ddbe177
commit 9af5f36951
5 changed files with 39 additions and 10 deletions

View File

@@ -43,6 +43,12 @@ supporting empty strings in the "location" option of the "return" action.
</para> </para>
</change> </change>
<change type="feature">
<para>
ability to specify a custom index file name when serving static files.
</para>
</change>
<change type="feature"> <change type="feature">
<para> <para>
variables support in the "location" option of the "return" action. variables support in the "location" option of the "return" action.

View File

@@ -647,6 +647,9 @@ static nxt_conf_vldt_object_t nxt_conf_vldt_share_action_members[] = {
.name = nxt_string("share"), .name = nxt_string("share"),
.type = NXT_CONF_VLDT_STRING | NXT_CONF_VLDT_ARRAY, .type = NXT_CONF_VLDT_STRING | NXT_CONF_VLDT_ARRAY,
.validator = nxt_conf_vldt_share, .validator = nxt_conf_vldt_share,
}, {
.name = nxt_string("index"),
.type = NXT_CONF_VLDT_STRING,
}, { }, {
.name = nxt_string("types"), .name = nxt_string("types"),
.type = NXT_CONF_VLDT_STRING | NXT_CONF_VLDT_ARRAY, .type = NXT_CONF_VLDT_STRING | NXT_CONF_VLDT_ARRAY,

View File

@@ -218,6 +218,7 @@ typedef struct {
nxt_conf_value_t *location; nxt_conf_value_t *location;
nxt_conf_value_t *proxy; nxt_conf_value_t *proxy;
nxt_conf_value_t *share; nxt_conf_value_t *share;
nxt_conf_value_t *index;
nxt_str_t chroot; nxt_str_t chroot;
nxt_conf_value_t *follow_symlinks; nxt_conf_value_t *follow_symlinks;
nxt_conf_value_t *traverse_mounts; nxt_conf_value_t *traverse_mounts;

View File

@@ -610,6 +610,11 @@ static nxt_conf_map_t nxt_http_route_action_conf[] = {
NXT_CONF_MAP_PTR, NXT_CONF_MAP_PTR,
offsetof(nxt_http_action_conf_t, share) offsetof(nxt_http_action_conf_t, share)
}, },
{
nxt_string("index"),
NXT_CONF_MAP_PTR,
offsetof(nxt_http_action_conf_t, index)
},
{ {
nxt_string("chroot"), nxt_string("chroot"),
NXT_CONF_MAP_STR, NXT_CONF_MAP_STR,

View File

@@ -19,6 +19,7 @@ typedef struct {
typedef struct { typedef struct {
nxt_uint_t nshares; nxt_uint_t nshares;
nxt_http_static_share_t *shares; nxt_http_static_share_t *shares;
nxt_str_t index;
#if (NXT_HAVE_OPENAT2) #if (NXT_HAVE_OPENAT2)
nxt_var_t *chroot; nxt_var_t *chroot;
nxt_uint_t resolve; nxt_uint_t resolve;
@@ -75,7 +76,7 @@ nxt_http_static_init(nxt_task_t *task, nxt_router_temp_conf_t *tmcf,
{ {
uint32_t i; uint32_t i;
nxt_mp_t *mp; nxt_mp_t *mp;
nxt_str_t str; nxt_str_t str, *ret;
nxt_var_t *var; nxt_var_t *var;
nxt_conf_value_t *cv; nxt_conf_value_t *cv;
nxt_http_static_conf_t *conf; nxt_http_static_conf_t *conf;
@@ -110,6 +111,18 @@ nxt_http_static_init(nxt_task_t *task, nxt_router_temp_conf_t *tmcf,
conf->shares[i].is_const = nxt_var_is_const(var); conf->shares[i].is_const = nxt_var_is_const(var);
} }
if (acf->index == NULL) {
nxt_str_set(&conf->index, "index.html");
} else {
nxt_conf_get_string(acf->index, &str);
ret = nxt_str_dup(mp, &conf->index, &str);
if (nxt_slow_path(ret == NULL)) {
return NXT_ERROR;
}
}
#if (NXT_HAVE_OPENAT2) #if (NXT_HAVE_OPENAT2)
if (acf->chroot.length > 0) { if (acf->chroot.length > 0) {
nxt_str_t chr, shr; nxt_str_t chr, shr;
@@ -222,8 +235,10 @@ nxt_http_static_iterate(nxt_task_t *task, nxt_http_request_t *r,
#if (NXT_DEBUG) #if (NXT_DEBUG)
nxt_str_t shr; nxt_str_t shr;
nxt_str_t idx;
nxt_var_raw(share->var, &shr); nxt_var_raw(share->var, &shr);
idx = conf->index;
#if (NXT_HAVE_OPENAT2) #if (NXT_HAVE_OPENAT2)
nxt_str_t chr; nxt_str_t chr;
@@ -235,9 +250,10 @@ nxt_http_static_iterate(nxt_task_t *task, nxt_http_request_t *r,
nxt_str_set(&chr, ""); nxt_str_set(&chr, "");
} }
nxt_debug(task, "http static: \"%V\" (chroot: \"%V\")", &shr, &chr); nxt_debug(task, "http static: \"%V\", index: \"%V\" (chroot: \"%V\")",
&shr, &idx, &chr);
#else #else
nxt_debug(task, "http static: \"%V\"", &shr); nxt_debug(task, "http static: \"%V\", index: \"%V\"", &shr, &idx);
#endif #endif
#endif /* NXT_DEBUG */ #endif /* NXT_DEBUG */
@@ -282,7 +298,7 @@ nxt_http_static_send_ready(nxt_task_t *task, void *obj, void *data)
struct tm tm; struct tm tm;
nxt_buf_t *fb; nxt_buf_t *fb;
nxt_int_t ret; nxt_int_t ret;
nxt_str_t *shr, exten, *mtype; nxt_str_t *shr, *index, exten, *mtype;
nxt_uint_t level; nxt_uint_t level;
nxt_file_t *f, file; nxt_file_t *f, file;
nxt_file_info_t fi; nxt_file_info_t fi;
@@ -295,8 +311,6 @@ nxt_http_static_send_ready(nxt_task_t *task, void *obj, void *data)
nxt_http_static_ctx_t *ctx; nxt_http_static_ctx_t *ctx;
nxt_http_static_conf_t *conf; nxt_http_static_conf_t *conf;
static const nxt_str_t index = nxt_string("index.html");
r = obj; r = obj;
ctx = data; ctx = data;
action = ctx->action; action = ctx->action;
@@ -307,12 +321,12 @@ nxt_http_static_send_ready(nxt_task_t *task, void *obj, void *data)
mtype = NULL; mtype = NULL;
shr = &ctx->share; shr = &ctx->share;
index = &conf->index;
if (shr->start[shr->length - 1] == '/') { if (shr->start[shr->length - 1] == '/') {
/* TODO: dynamic index setting. */ nxt_http_static_extract_extension(index, &exten);
nxt_str_set(&exten, ".html");
length = shr->length + index.length; length = shr->length + index->length;
fname = nxt_mp_nget(r->mem_pool, length + 1); fname = nxt_mp_nget(r->mem_pool, length + 1);
if (nxt_slow_path(fname == NULL)) { if (nxt_slow_path(fname == NULL)) {
@@ -321,7 +335,7 @@ nxt_http_static_send_ready(nxt_task_t *task, void *obj, void *data)
p = fname; p = fname;
p = nxt_cpymem(p, shr->start, shr->length); p = nxt_cpymem(p, shr->start, shr->length);
p = nxt_cpymem(p, index.start, index.length); p = nxt_cpymem(p, index->start, index->length);
*p = '\0'; *p = '\0';
} else { } else {