summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorZhidao HONG <z.hong@f5.com>2024-01-31 14:51:21 +0800
committerZhidao HONG <z.hong@f5.com>2024-02-20 06:09:23 +0800
commit33c6c4d4c0e060a974791a472b739214366dead6 (patch)
treefd754daca475f3c13af4a3420ee657d7f63afaba /src
parent63ad4deb8a9a7955c5eec3098a2acc3e149831c7 (diff)
downloadunit-33c6c4d4c0e060a974791a472b739214366dead6.tar.gz
unit-33c6c4d4c0e060a974791a472b739214366dead6.tar.bz2
NJS: variable access support
This commit introduces the 'vars' JavaScript object to NJS, enabling direct access to native variables such as $uri and $arg_foo. The syntax is `${vars.var_name}` or `${'vars[var_name]'}`. For example: { "action": { "share": "`/www/html${vars.uri}`" } }
Diffstat (limited to 'src')
-rw-r--r--src/nxt_http_js.c49
-rw-r--r--src/nxt_js.c5
2 files changed, 52 insertions, 2 deletions
diff --git a/src/nxt_http_js.c b/src/nxt_http_js.c
index 72ba761f..e3beb8b4 100644
--- a/src/nxt_http_js.c
+++ b/src/nxt_http_js.c
@@ -28,6 +28,8 @@ static njs_int_t nxt_http_js_ext_get_cookie(njs_vm_t *vm,
njs_value_t *retval);
static njs_int_t nxt_http_js_ext_keys_cookie(njs_vm_t *vm, njs_value_t *value,
njs_value_t *keys);
+static njs_int_t nxt_http_js_ext_get_var(njs_vm_t *vm, njs_object_prop_t *prop,
+ njs_value_t *value, njs_value_t *setval, njs_value_t *retval);
static njs_external_t nxt_http_js_proto[] = {
@@ -88,6 +90,14 @@ static njs_external_t nxt_http_js_proto[] = {
.keys = nxt_http_js_ext_keys_cookie,
}
},
+
+ {
+ .flags = NJS_EXTERN_OBJECT,
+ .name.string = njs_str("vars"),
+ .u.object = {
+ .prop_handler = nxt_http_js_ext_get_var,
+ }
+ },
};
@@ -338,3 +348,42 @@ nxt_http_js_ext_keys_cookie(njs_vm_t *vm, njs_value_t *value, njs_value_t *keys)
return NJS_OK;
}
+
+
+static njs_int_t
+nxt_http_js_ext_get_var(njs_vm_t *vm, njs_object_prop_t *prop,
+ njs_value_t *value, njs_value_t *setval, njs_value_t *retval)
+{
+ njs_int_t rc;
+ njs_str_t key;
+ nxt_str_t name, *vv;
+ nxt_router_conf_t *rtcf;
+ nxt_http_request_t *r;
+
+ r = njs_vm_external(vm, nxt_js_proto_id, value);
+ if (r == NULL) {
+ njs_value_undefined_set(retval);
+ return NJS_DECLINED;
+ }
+
+ rc = njs_vm_prop_name(vm, prop, &key);
+ if (rc != NJS_OK) {
+ njs_value_undefined_set(retval);
+ return NJS_DECLINED;
+ }
+
+ rtcf = r->conf->socket_conf->router_conf;
+
+ name.start = key.start;
+ name.length = key.length;
+
+ vv = nxt_var_get(&r->task, rtcf->tstr_state, &r->tstr_cache.var, &name, r);
+
+ if (vv != NULL) {
+ return njs_vm_value_string_set(vm, retval, vv->start, vv->length);
+ }
+
+ njs_value_undefined_set(retval);
+
+ return NJS_DECLINED;
+}
diff --git a/src/nxt_js.c b/src/nxt_js.c
index 0e1fe463..6885afb7 100644
--- a/src/nxt_js.c
+++ b/src/nxt_js.c
@@ -240,7 +240,7 @@ nxt_js_add_tpl(nxt_js_conf_t *jcf, nxt_str_t *str, nxt_bool_t strz)
nxt_str_t *func;
static nxt_str_t func_str = nxt_string("function(uri, host, remoteAddr, "
- "args, headers, cookies) {"
+ "args, headers, cookies, vars) {"
" return ");
/*
@@ -391,7 +391,7 @@ nxt_js_call(nxt_task_t *task, nxt_js_conf_t *jcf, nxt_js_cache_t *cache,
njs_uint_t i, n;
njs_value_t *value;
njs_function_t *func;
- njs_opaque_value_t retval, opaque_value, arguments[6];
+ njs_opaque_value_t retval, opaque_value, arguments[7];
static const njs_str_t js_args[] = {
njs_str("uri"),
@@ -400,6 +400,7 @@ nxt_js_call(nxt_task_t *task, nxt_js_conf_t *jcf, nxt_js_cache_t *cache,
njs_str("args"),
njs_str("headers"),
njs_str("cookies"),
+ njs_str("vars"),
};
vm = cache->vm;