Skip to content

Instantly share code, notes, and snippets.

@Inviz
Last active July 8, 2016 07:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Inviz/a4796b2fb05621d3ad4ef1f42f123dee to your computer and use it in GitHub Desktop.
Save Inviz/a4796b2fb05621d3ad4ef1f42f123dee to your computer and use it in GitHub Desktop.
--- a/nginx/ngx_http_js_module.c Thu Apr 14 18:23:09 2016 +0300
+++ b/nginx/ngx_http_js_module.c Wed May 04 16:46:03 2016 +0800
@@ -95,6 +95,8 @@
njs_value_t *value, void *obj, uintptr_t data);
static njs_ret_t ngx_http_js_ext_get_remote_address(njs_vm_t *vm,
njs_value_t *value, void *obj, uintptr_t data);
+static njs_ret_t ngx_http_js_ext_get_var(njs_vm_t *vm,
+ njs_value_t *value, void *obj, uintptr_t data);
static njs_ret_t ngx_http_js_ext_get_header_in(njs_vm_t *vm, njs_value_t *value,
void *obj, uintptr_t data);
static njs_ret_t ngx_http_js_ext_foreach_header_in(njs_vm_t *vm, void *obj,
@@ -366,6 +368,18 @@
NULL,
NULL,
0 },
+
+ { nxt_string("$v"),
+ NJS_EXTERN_OBJECT,
+ NULL,
+ 0,
+ ngx_http_js_ext_get_var,
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ 0 },
};
@@ -939,6 +953,38 @@
static njs_ret_t
+ngx_http_js_ext_get_var(njs_vm_t *vm, njs_value_t *value, void *obj,
+ uintptr_t data)
+{
+ nxt_str_t *v;
+ ngx_http_request_t *r;
+
+ ngx_str_t name;
+ ngx_uint_t hash;
+ ngx_http_variable_value_t *val;
+
+ v = (nxt_str_t*)data;
+ if (!v) {
+ return njs_string_create(vm, value, NULL, 0, 0);
+ }
+
+ name.len = v->len;
+ name.data = (u_char *) v->data;
+ hash = ngx_hash_key (v->data, v->len);;
+
+ r = (ngx_http_request_t *) obj;
+ val = ngx_http_get_variable( r, &name, hash);
+
+ if( val && !val->not_found && val->valid)
+ {
+ return njs_string_create(vm, value, val->data, val->len, 0);
+ }
+
+ return njs_string_create(vm, value, NULL, 0, 0);
+}
+
+
+static njs_ret_t
ngx_http_js_ext_get_remote_address(njs_vm_t *vm, njs_value_t *value, void *obj,
uintptr_t data)
{
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment