[PATCH v7 10/14] Static: variables in the "index" option

Alejandro Colomar alx.manpages at gmail.com
Tue Feb 15 15:25:20 UTC 2022


This adds support for variables in the "index" option.
An example:

{
	"share": "/srv/www/data/static/$uri",
	"index": "$method.html"
}

When <example.com/foo/bar/> is requested,
</srv/www/data/static/foo/bar/GET.html> will be served.

===

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

- Setting "index" to "$method.html", and check that <GET.html> is
  being served.
- Not setting index, and check that <index.html> is being served.
- Setting "index" to "$asdf", and check that the configuration
  fails:

{
	"error": "Invalid configuration.",
	"detail": "Unknown variable \"asdf\" in the \"index\" value."
}

Tested-by: Alejandro Colomar <alx.manpages at gmail.com>
Signed-off-by: Alejandro Colomar <alx.manpages at gmail.com>
Cc: Nginx Unit <unit at nginx.org>
Cc: "Valentin V. Bartenev" <vbart at nginx.com>
Cc: Zhidao HONG <z.hong at f5.com>
Cc: Igor Sysoev <igor at sysoev.ru>
Cc: Oisin Canty <o.canty at f5.com>
Cc: Andrei Zeliankou <zelenkov at nginx.com>
Cc: Maxim Romanov <m.romanov at f5.com>
---
 docs/changes.xml          |  6 ++++++
 src/nxt_conf_validation.c |  1 +
 src/nxt_http_static.c     | 29 +++++++++++++++++++++--------
 3 files changed, 28 insertions(+), 8 deletions(-)

diff --git a/docs/changes.xml b/docs/changes.xml
index 81708c3..7709fc8 100644
--- a/docs/changes.xml
+++ b/docs/changes.xml
@@ -18,6 +18,12 @@
          date="" time=""
          packager="Andrei Belov <defan at nginx.com>">
 
+<change type="feature">
+<para>
+variables support in the "index" option.
+</para>
+</change>
+
 <change type="feature">
 <para>
 new "index" option to specify a custom index file name.
diff --git a/src/nxt_conf_validation.c b/src/nxt_conf_validation.c
index bdc38b0..6236720 100644
--- a/src/nxt_conf_validation.c
+++ b/src/nxt_conf_validation.c
@@ -648,6 +648,7 @@ static nxt_conf_vldt_object_t  nxt_conf_vldt_share_action_members[] = {
     }, {
         .name       = nxt_string("index"),
         .type       = NXT_CONF_VLDT_STRING,
+        .flags      = NXT_CONF_VLDT_VAR,
     }, {
         .name       = nxt_string("types"),
         .type       = NXT_CONF_VLDT_STRING | NXT_CONF_VLDT_ARRAY,
diff --git a/src/nxt_http_static.c b/src/nxt_http_static.c
index 2528b95..bee85b2 100644
--- a/src/nxt_http_static.c
+++ b/src/nxt_http_static.c
@@ -19,7 +19,7 @@ typedef struct {
 typedef struct {
     nxt_uint_t                  nshares;
     nxt_http_static_share_t     *shares;
-    nxt_str_t                   index;
+    nxt_var_t                   *index;
 #if (NXT_HAVE_OPENAT2)
     nxt_var_t                   *chroot;
     nxt_uint_t                  resolve;
@@ -115,12 +115,19 @@ nxt_http_static_init(nxt_task_t *task, nxt_router_temp_conf_t *tmcf,
     }
 
     if (acf->index == NULL) {
-        conf->index = default_index;
+        str = default_index;
 
     } else {
-        nxt_conf_get_string(acf->index, &conf->index);
+        nxt_conf_get_string(acf->index, &str);
     }
 
+    var = nxt_var_compile(&str, mp, 1);
+    if (nxt_slow_path(var == NULL)) {
+        return NXT_ERROR;
+    }
+
+    conf->index = var;
+
 #if (NXT_HAVE_OPENAT2)
     if (acf->chroot.length > 0) {
         nxt_str_t   chr, shr;
@@ -226,7 +233,7 @@ nxt_http_static_iterate(nxt_task_t *task, nxt_http_request_t *r,
     nxt_int_t                ret;
     nxt_http_static_conf_t   *conf;
     nxt_http_static_share_t  *share;
-    nxt_bool_t               shr_is_const;
+    nxt_bool_t               shr_is_const, idx_is_const;
 
     conf = ctx->action->u.conf;
 
@@ -237,7 +244,7 @@ nxt_http_static_iterate(nxt_task_t *task, nxt_http_request_t *r,
     nxt_str_t  idx;
 
     nxt_var_raw(share->var, &shr);
-    idx = conf->index;
+    nxt_var_raw(conf->index, &idx);
 
 #if (NXT_HAVE_OPENAT2)
     nxt_str_t  chr;
@@ -257,8 +264,9 @@ nxt_http_static_iterate(nxt_task_t *task, nxt_http_request_t *r,
 #endif /* NXT_DEBUG */
 
     shr_is_const = share->is_const;
+    idx_is_const = nxt_var_is_const(conf->index);
 
-    if (!shr_is_const) {
+    if (!shr_is_const || !idx_is_const) {
         ret = nxt_var_query_init(&r->var_query, r, r->mem_pool);
         if (nxt_slow_path(ret != NXT_OK)) {
             nxt_http_request_error(task, r, NXT_HTTP_INTERNAL_SERVER_ERROR);
@@ -266,7 +274,12 @@ nxt_http_static_iterate(nxt_task_t *task, nxt_http_request_t *r,
         }
     }
 
-    ctx->index = conf->index;
+    if (idx_is_const) {
+        nxt_var_raw(conf->index, &ctx->index);
+
+    } else {
+        nxt_var_query(task, r->var_query, conf->index, &ctx->index);
+    }
 
     if (shr_is_const) {
         nxt_var_raw(share->var, &ctx->share);
@@ -287,7 +300,7 @@ nxt_http_static_iterate(nxt_task_t *task, nxt_http_request_t *r,
 #endif
     }
 
-    if (shr_is_const) {
+    if (shr_is_const && idx_is_const) {
         nxt_http_static_send_ready(task, r, ctx);
 
     } else {
-- 
2.34.1



More information about the unit mailing list