[njs] Added support for setting nginx variables.

Dmitry Volyntsev xeioex at nginx.com
Thu Feb 21 18:06:26 UTC 2019


details:   https://hg.nginx.org/njs/rev/c0e7041165c0
branches:  
changeset: 794:c0e7041165c0
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Thu Feb 21 20:47:52 2019 +0300
description:
Added support for setting nginx variables.

This closes #37 issue on Github.

diffstat:

 nginx/ngx_http_js_module.c   |  71 ++++++++++++++++++++++++++++++++++++++++++-
 nginx/ngx_stream_js_module.c |  72 +++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 141 insertions(+), 2 deletions(-)

diffs (191 lines):

diff -r 2a6f5ffd5e96 -r c0e7041165c0 nginx/ngx_http_js_module.c
--- a/nginx/ngx_http_js_module.c	Thu Feb 21 16:35:52 2019 +0300
+++ b/nginx/ngx_http_js_module.c	Thu Feb 21 20:47:52 2019 +0300
@@ -117,6 +117,8 @@ static njs_ret_t ngx_http_js_ext_next_ar
     void *obj, void *next);
 static njs_ret_t ngx_http_js_ext_get_variable(njs_vm_t *vm, njs_value_t *value,
     void *obj, uintptr_t data);
+static njs_ret_t ngx_http_js_ext_set_variable(njs_vm_t *vm, void *obj,
+       uintptr_t data, nxt_str_t *value);
 static njs_ret_t ngx_http_js_ext_subrequest(njs_vm_t *vm, njs_value_t *args,
     nxt_uint_t nargs, njs_index_t unused);
 static ngx_int_t ngx_http_js_subrequest(ngx_http_request_t *r,
@@ -321,7 +323,7 @@ static njs_external_t  ngx_http_js_ext_r
       NULL,
       0,
       ngx_http_js_ext_get_variable,
-      NULL,
+      ngx_http_js_ext_set_variable,
       NULL,
       NULL,
       NULL,
@@ -1624,6 +1626,73 @@ ngx_http_js_ext_get_variable(njs_vm_t *v
 
 
 static njs_ret_t
+ngx_http_js_ext_set_variable(njs_vm_t *vm, void *obj, uintptr_t data,
+    nxt_str_t *value)
+{
+    nxt_str_t                  *val;
+    ngx_str_t                   name;
+    ngx_uint_t                  key;
+    ngx_http_request_t         *r;
+    ngx_http_variable_t        *v;
+    ngx_http_variable_value_t  *vv;
+    ngx_http_core_main_conf_t  *cmcf;
+
+    r = (ngx_http_request_t *) obj;
+    val = (nxt_str_t *) data;
+
+    name.data = val->start;
+    name.len = val->length;
+
+    cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
+
+    key = ngx_hash_strlow(name.data, name.data, name.len);
+
+    v = ngx_hash_find(&cmcf->variables_hash, key, name.data, name.len);
+
+    if (v == NULL) {
+        njs_vm_error(vm, "variable not found");
+        return NJS_ERROR;
+    }
+
+    if (v->set_handler != NULL) {
+        vv = ngx_pcalloc(r->pool, sizeof(ngx_http_variable_value_t));
+        if (vv == NULL) {
+            return NJS_ERROR;
+        }
+
+        vv->valid = 1;
+        vv->not_found = 0;
+        vv->data = value->start;
+        vv->len = value->length;
+
+        v->set_handler(r, vv, v->data);
+
+        return NJS_OK;
+    }
+
+    if (!(v->flags & NGX_HTTP_VAR_INDEXED)) {
+        njs_vm_error(vm, "variable is not writable");
+        return NJS_ERROR;
+    }
+
+    vv = &r->variables[v->index];
+
+    vv->valid = 1;
+    vv->not_found = 0;
+
+    vv->data = ngx_pnalloc(r->pool, value->length);
+    if (vv->data == NULL) {
+        return NJS_ERROR;
+    }
+
+    vv->len = value->length;
+    ngx_memcpy(vv->data, value->start, vv->len);
+
+    return NJS_OK;
+}
+
+
+static njs_ret_t
 ngx_http_js_ext_subrequest(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
     njs_index_t unused)
 {
diff -r 2a6f5ffd5e96 -r c0e7041165c0 nginx/ngx_stream_js_module.c
--- a/nginx/ngx_stream_js_module.c	Thu Feb 21 16:35:52 2019 +0300
+++ b/nginx/ngx_stream_js_module.c	Thu Feb 21 20:47:52 2019 +0300
@@ -99,6 +99,8 @@ static njs_ret_t ngx_stream_js_ext_send(
 
 static njs_ret_t ngx_stream_js_ext_get_variable(njs_vm_t *vm,
     njs_value_t *value, void *obj, uintptr_t data);
+static njs_ret_t ngx_stream_js_ext_set_variable(njs_vm_t *vm, void *obj,
+    uintptr_t data, nxt_str_t *value);
 
 static njs_host_event_t ngx_stream_js_set_timer(njs_external_ptr_t external,
     uint64_t delay, njs_vm_event_t vm_event);
@@ -207,7 +209,7 @@ static njs_external_t  ngx_stream_js_ext
       NULL,
       0,
       ngx_stream_js_ext_get_variable,
-      NULL,
+      ngx_stream_js_ext_set_variable,
       NULL,
       NULL,
       NULL,
@@ -1208,6 +1210,74 @@ ngx_stream_js_ext_get_variable(njs_vm_t 
 }
 
 
+static njs_ret_t
+ngx_stream_js_ext_set_variable(njs_vm_t *vm, void *obj, uintptr_t data,
+    nxt_str_t *value)
+{
+    nxt_str_t                    *val;
+    ngx_str_t                     name;
+    ngx_uint_t                    key;
+    ngx_stream_variable_t        *v;
+    ngx_stream_session_t         *s;
+    ngx_stream_core_main_conf_t  *cmcf;
+    ngx_stream_variable_value_t  *vv;
+
+    s = (ngx_stream_session_t *) obj;
+    val = (nxt_str_t *) data;
+
+    name.data = val->start;
+    name.len = val->length;
+
+    cmcf = ngx_stream_get_module_main_conf(s, ngx_stream_core_module);
+
+    key = ngx_hash_strlow(name.data, name.data, name.len);
+
+    v = ngx_hash_find(&cmcf->variables_hash, key, name.data, name.len);
+
+    if (v == NULL) {
+        njs_vm_error(vm, "variable not found");
+        return NJS_ERROR;
+    }
+
+    if (v->set_handler != NULL) {
+        vv = ngx_pcalloc(s->connection->pool,
+                         sizeof(ngx_stream_variable_value_t));
+        if (vv == NULL) {
+            return NJS_ERROR;
+        }
+
+        vv->valid = 1;
+        vv->not_found = 0;
+        vv->data = value->start;
+        vv->len = value->length;
+
+        v->set_handler(s, vv, v->data);
+
+        return NJS_OK;
+    }
+
+    if (!(v->flags & NGX_STREAM_VAR_INDEXED)) {
+        njs_vm_error(vm, "variable is not writable");
+        return NJS_ERROR;
+    }
+
+    vv = &s->variables[v->index];
+
+    vv->valid = 1;
+    vv->not_found = 0;
+
+    vv->data = ngx_pnalloc(s->connection->pool, value->length);
+    if (vv->data == NULL) {
+        return NJS_ERROR;
+    }
+
+    vv->len = value->length;
+    ngx_memcpy(vv->data, value->start, vv->len);
+
+    return NJS_OK;
+}
+
+
 static njs_host_event_t
 ngx_stream_js_set_timer(njs_external_ptr_t external, uint64_t delay,
     njs_vm_event_t vm_event)


More information about the nginx-devel mailing list