Patch for the implementation "index" option to configure the index file name while serving static files

Javier Manteiga javier_manteiga at yahoo.es
Mon Nov 22 18:09:21 UTC 2021


diff --git a/src/nxt_conf_validation.c b/src/nxt_conf_validation.cindex 3f068bb..5c0e74b 100644--- a/src/nxt_conf_validation.c+++ b/src/nxt_conf_validation.c@@ -117,6 +117,10 @@ static nxt_int_t nxt_conf_vldt_share(nxt_conf_validation_t *vldt,      nxt_conf_value_t *value, void *data); static nxt_int_t nxt_conf_vldt_share_element(nxt_conf_validation_t *vldt,     nxt_conf_value_t *value);+static nxt_int_t nxt_conf_vldt_action_index(nxt_conf_validation_t *vldt,+        nxt_conf_value_t *value, void *data);+static nxt_int_t nxt_conf_vldt_action_index_element(nxt_conf_validation_t *vldt,+    nxt_conf_value_t *value); static nxt_int_t nxt_conf_vldt_proxy(nxt_conf_validation_t *vldt,     nxt_conf_value_t *value, void *data); static nxt_int_t nxt_conf_vldt_python(nxt_conf_validation_t *vldt,@@ -675,6 +679,10 @@ static nxt_conf_vldt_object_t  nxt_conf_vldt_share_action_members[] = {         .validator  = nxt_conf_vldt_unsupported,         .u.string   = "traverse_mounts", #endif+    },  {+        .name       = nxt_string("index"),+        .type       = NXT_CONF_VLDT_STRING | NXT_CONF_VLDT_ARRAY,+        .validator  = nxt_conf_vldt_action_index,     },      NXT_CONF_VLDT_END@@ -1690,6 +1698,52 @@ nxt_conf_vldt_share_element(nxt_conf_validation_t *vldt, }  +static nxt_int_t+nxt_conf_vldt_action_index(nxt_conf_validation_t *vldt,+    nxt_conf_value_t *value, void *data)+{+    nxt_str_t  name;++    if (nxt_conf_type(value) == NXT_CONF_ARRAY) {+        if (nxt_conf_array_elements_count(value) == 0) {+            return nxt_conf_vldt_error(vldt, "The \"index\" array "+                                       "must contain at least one element.");+        }++        return nxt_conf_vldt_array_iterator(vldt, value,+                                        &nxt_conf_vldt_action_index_element);+    }++    /* NXT_CONF_STRING */+    nxt_conf_get_string(value, &name);++    return nxt_conf_vldt_share_element(vldt, value);+}+++static nxt_int_t+nxt_conf_vldt_action_index_element(nxt_conf_validation_t *vldt,+    nxt_conf_value_t *value)+{+    nxt_str_t  str;++    static nxt_str_t  index = nxt_string("index");++    if (nxt_conf_type(value) != NXT_CONF_STRING) {+        return nxt_conf_vldt_error(vldt, "The \"index\" array must "+                                   "contain only string values.");+    }++    nxt_conf_get_string(value, &str);++    if (nxt_is_var(&str)) {+        return nxt_conf_vldt_var(vldt, &index, &str);+    }++    return NXT_OK;+}++ static nxt_int_t nxt_conf_vldt_proxy(nxt_conf_validation_t *vldt, nxt_conf_value_t *value,     void *data)diff --git a/src/nxt_http.h b/src/nxt_http.hindex 02d66f5..bc97f83 100644--- a/src/nxt_http.h+++ b/src/nxt_http.h@@ -214,6 +214,7 @@ typedef struct {     nxt_conf_value_t                *traverse_mounts;     nxt_conf_value_t                *types;     nxt_conf_value_t                *fallback;+    nxt_conf_value_t                *index; } nxt_http_action_conf_t;  diff --git a/src/nxt_http_static.c b/src/nxt_http_static.cindex 36c1ebc..6860e2b 100644--- a/src/nxt_http_static.c+++ b/src/nxt_http_static.c@@ -15,15 +15,22 @@ typedef struct {     uint8_t                     is_const;  /* 1 bit */ } nxt_http_static_share_t; +typedef struct {+    nxt_var_t                   *var;+    uint8_t                     is_const;  /* 1 bit */+} nxt_http_static_share_index_t;+  typedef struct {-    nxt_uint_t                  nshares;-    nxt_http_static_share_t     *shares;+    nxt_uint_t                      nshares;+    nxt_http_static_share_t         *shares; #if (NXT_HAVE_OPENAT2)-    nxt_var_t                   *chroot;-    nxt_uint_t                  resolve;+    nxt_var_t                       *chroot;+    nxt_uint_t                      resolve; #endif-    nxt_http_route_rule_t       *types;+    nxt_http_route_rule_t           *types;+    nxt_uint_t                      nindex;+    nxt_http_static_share_index_t  *index; } nxt_http_static_conf_t;  @@ -34,6 +41,10 @@ typedef struct {     nxt_str_t                   chroot; #endif     uint32_t                    index;++    nxt_str_t                   *share_index;+    uint32_t                    share_index_read;+    uint32_t                    share_index_write;     uint8_t                     need_body;  /* 1 bit */ } nxt_http_static_ctx_t; @@ -181,6 +192,38 @@ nxt_http_static_init(nxt_task_t *task, nxt_router_temp_conf_t *tmcf,                                     action->fallback);     } +    if (acf->index != NULL) {+       array = (nxt_conf_type(acf->index) == NXT_CONF_ARRAY);+       conf->nindex = array ? nxt_conf_array_elements_count(acf->index) : 1;++       conf->index = nxt_mp_zget(mp, sizeof(nxt_str_t) * conf->nindex);+       if (nxt_slow_path(conf->index == NULL)) {+          return NXT_ERROR;+       }++       if (array) {+          for (i = 0; i < conf->nindex; i++) {+               cv = nxt_conf_get_array_element(acf->index, i);+               nxt_conf_get_string(cv, &str);+               var = nxt_var_compile(&str, mp, 1);+               if (nxt_slow_path(var == NULL)) {+                   return NXT_ERROR;+               }++               conf->index[i].var = var;+               conf->index[i].is_const = nxt_var_is_const(var);+           }+       } else {+           nxt_conf_get_string(acf->share, &str);+           var = nxt_var_compile(&str, mp, 1);+           if (nxt_slow_path(var == NULL)) {+               return NXT_ERROR;+           }+           conf->index[0].var = var;+           conf->index[0].is_const = nxt_var_is_const(var);+       }+    }+     return NXT_OK; } @@ -219,10 +262,49 @@ nxt_http_static(nxt_task_t *task, nxt_http_request_t *r,     ctx->need_body = need_body;      nxt_http_static_iterate(task, r, ctx);+    nxt_mp_free(r->mem_pool, ctx->share_index);      return NULL; } +static void+nxt_http_static_share_index_resolv(nxt_task_t *task, void *obj, void *data) {+    nxt_int_t                ret;+    nxt_http_static_share_index_t* index;+    nxt_http_request_t      *r;+    nxt_http_static_ctx_t   *ctx;+    nxt_http_static_conf_t  *conf;+    nxt_str_t               *share_index;++    r = obj;+    ctx = data;+    conf = ctx->action->u.conf;++    if (ctx->share_index_write >= conf->nindex) {+        return;+    }++    index = &conf->index[ctx->share_index_write];+    share_index = &ctx->share_index[ctx->share_index_write++];++    if (index->is_const) {+        nxt_var_raw(index->var, share_index);+        nxt_http_static_share_index_resolv(task, r, ctx);+    }+    else {+        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);+            return;+        }++        nxt_var_query(task, r->var_query, index->var, share_index);+        nxt_var_query_resolve(task, r->var_query, ctx,+                              nxt_http_static_share_index_resolv,+                              nxt_http_static_var_error);+    }+}+  static void nxt_http_static_iterate(nxt_task_t *task, nxt_http_request_t *r,@@ -234,6 +316,12 @@ nxt_http_static_iterate(nxt_task_t *task, nxt_http_request_t *r,      conf = ctx->action->u.conf; +    if (ctx->index == 0 && ctx->share_index_read == 0) {+        ctx->share_index = nxt_mp_zalloc(r->mem_pool,+                                         sizeof(nxt_str_t) * conf->nindex);+        nxt_http_static_share_index_resolv(task, r, ctx);+    }+     share = &conf->shares[ctx->index];  #if (NXT_DEBUG)@@ -265,9 +353,7 @@ nxt_http_static_iterate(nxt_task_t *task, nxt_http_request_t *r,             nxt_var_raw(conf->chroot, &ctx->chroot);         } #endif-         nxt_http_static_send_ready(task, r, ctx);-     } else {         ret = nxt_var_query_init(&r->var_query, r, r->mem_pool);         if (nxt_slow_path(ret != NXT_OK)) {@@ -299,6 +385,7 @@ nxt_http_static_send_ready(nxt_task_t *task, void *obj, void *data)     nxt_buf_t               *fb;     nxt_int_t               ret;     nxt_str_t               *shr, exten, *mtype;+    nxt_str_t               *index;     nxt_uint_t              level;     nxt_file_t              *f, file;     nxt_file_info_t         fi;@@ -311,8 +398,6 @@ nxt_http_static_send_ready(nxt_task_t *task, void *obj, void *data)     nxt_http_static_ctx_t   *ctx;     nxt_http_static_conf_t  *conf; -    static const nxt_str_t  index = nxt_string("index.html");-     r = obj;     ctx = data;     action = ctx->action;@@ -323,12 +408,13 @@ nxt_http_static_send_ready(nxt_task_t *task, void *obj, void *data)     mtype = NULL;      shr = &ctx->share;+    index = &ctx->share_index[ctx->share_index_read];      if (shr->start[shr->length - 1] == '/') {         /* TODO: dynamic index setting. */         nxt_str_set(&exten, ".html"); -        length = shr->length + index.length;+        length = shr->length + index->length;          fname = nxt_mp_nget(r->mem_pool, length + 1);         if (nxt_slow_path(fname == NULL)) {@@ -337,7 +423,7 @@ nxt_http_static_send_ready(nxt_task_t *task, void *obj, void *data)          p = fname;         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';      } else {@@ -387,7 +473,7 @@ nxt_http_static_send_ready(nxt_task_t *task, void *obj, void *data)              if (fname != NULL) {                 file.name = chr->start;-                ret = nxt_file_open(task, &file, NXT_FILE_SEARCH, NXT_FILE_OPEN,+                ret =  (task, &file, NXT_FILE_SEARCH, NXT_FILE_OPEN,                                     0);              } else {@@ -675,7 +761,11 @@ nxt_http_static_next(nxt_task_t *task, nxt_http_request_t *r,     action = ctx->action;     conf = action->u.conf; -    ctx->index++;+    ctx->share_index_read++;+    if (ctx->share_index_read >= conf->nindex) {+        ctx->share_index_read = 0;+        ctx->index++;+    }      if (ctx->index < conf->nshares) {         nxt_http_static_iterate(task, r, ctx);

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.nginx.org/pipermail/unit/attachments/20211122/9e04b4e2/attachment.htm>


More information about the unit mailing list