summaryrefslogtreecommitdiffhomepage
path: root/src/nxt_http_js.c
diff options
context:
space:
mode:
authorZhidao HONG <z.hong@f5.com>2023-01-17 10:37:45 +0800
committerZhidao HONG <z.hong@f5.com>2023-01-17 10:37:45 +0800
commit141deec353c546674719d2bffe2fadeb174c4415 (patch)
treea6dceaf16a112f0c3ce91f15d85bde5e11862c20 /src/nxt_http_js.c
parent97caab0e7a40c4034888e3269cdcbb858e47b45b (diff)
downloadunit-141deec353c546674719d2bffe2fadeb174c4415.tar.gz
unit-141deec353c546674719d2bffe2fadeb174c4415.tar.bz2
NJS: added the keys API for the request objects.
This commit is to loop through the request objects headers, arguments, and cookies.
Diffstat (limited to 'src/nxt_http_js.c')
-rw-r--r--src/nxt_http_js.c133
1 files changed, 100 insertions, 33 deletions
diff --git a/src/nxt_http_js.c b/src/nxt_http_js.c
index 5a08a309..72ba761f 100644
--- a/src/nxt_http_js.c
+++ b/src/nxt_http_js.c
@@ -15,15 +15,19 @@ static njs_int_t nxt_http_js_ext_host(njs_vm_t *vm, njs_object_prop_t *prop,
static njs_int_t nxt_http_js_ext_remote_addr(njs_vm_t *vm,
njs_object_prop_t *prop, njs_value_t *value, njs_value_t *setval,
njs_value_t *retval);
-static njs_int_t nxt_http_js_ext_get_arg(njs_vm_t *vm,
+static njs_int_t nxt_http_js_ext_get_args(njs_vm_t *vm,
njs_object_prop_t *prop, njs_value_t *value, njs_value_t *setval,
njs_value_t *retval);
static njs_int_t nxt_http_js_ext_get_header(njs_vm_t *vm,
njs_object_prop_t *prop, njs_value_t *value, njs_value_t *setval,
njs_value_t *retval);
+static njs_int_t nxt_http_js_ext_keys_header(njs_vm_t *vm,
+ njs_value_t *value, njs_value_t *keys);
static njs_int_t nxt_http_js_ext_get_cookie(njs_vm_t *vm,
njs_object_prop_t *prop, njs_value_t *value, njs_value_t *setval,
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_external_t nxt_http_js_proto[] = {
@@ -55,12 +59,11 @@ static njs_external_t nxt_http_js_proto[] = {
},
{
- .flags = NJS_EXTERN_OBJECT,
+ .flags = NJS_EXTERN_PROPERTY,
.name.string = njs_str("args"),
.enumerable = 1,
- .u.object = {
- .enumerable = 1,
- .prop_handler = nxt_http_js_ext_get_arg,
+ .u.property = {
+ .handler = nxt_http_js_ext_get_args,
}
},
@@ -71,6 +74,7 @@ static njs_external_t nxt_http_js_proto[] = {
.u.object = {
.enumerable = 1,
.prop_handler = nxt_http_js_ext_get_header,
+ .keys = nxt_http_js_ext_keys_header,
}
},
@@ -81,6 +85,7 @@ static njs_external_t nxt_http_js_proto[] = {
.u.object = {
.enumerable = 1,
.prop_handler = nxt_http_js_ext_get_cookie,
+ .keys = nxt_http_js_ext_keys_cookie,
}
},
};
@@ -144,14 +149,13 @@ nxt_http_js_ext_remote_addr(njs_vm_t *vm, njs_object_prop_t *prop,
static njs_int_t
-nxt_http_js_ext_get_arg(njs_vm_t *vm, njs_object_prop_t *prop,
+nxt_http_js_ext_get_args(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_array_t *args;
- nxt_http_request_t *r;
- nxt_http_name_value_t *nv, *start, *end;
+ njs_int_t ret;
+ njs_value_t *args;
+ njs_opaque_value_t val;
+ nxt_http_request_t *r;
r = njs_vm_external(vm, nxt_js_proto_id, value);
if (r == NULL) {
@@ -159,33 +163,18 @@ nxt_http_js_ext_get_arg(njs_vm_t *vm, njs_object_prop_t *prop,
return NJS_DECLINED;
}
- rc = njs_vm_prop_name(vm, prop, &key);
- if (rc != NJS_OK) {
- njs_value_undefined_set(retval);
- return NJS_DECLINED;
- }
-
- args = nxt_http_arguments_parse(r);
- if (nxt_slow_path(args == NULL)) {
- return NJS_ERROR;
- }
-
- start = args->elts;
- end = start + args->nelts;
+ args = njs_value_arg(&val);
- for (nv = start; nv < end; nv++) {
+ ret = njs_vm_query_string_parse(vm, r->args->start,
+ r->args->start + r->args->length, args);
- if (key.length == nv->name_length
- && memcmp(key.start, nv->name, nv->name_length) == 0)
- {
- return njs_vm_value_string_set(vm, retval, nv->value,
- nv->value_length);
- }
+ if (ret == NJS_ERROR) {
+ return NJS_ERROR;
}
- njs_value_undefined_set(retval);
+ njs_value_assign(retval, args);
- return NJS_DECLINED;
+ return NJS_OK;
}
@@ -228,6 +217,41 @@ nxt_http_js_ext_get_header(njs_vm_t *vm, njs_object_prop_t *prop,
static njs_int_t
+nxt_http_js_ext_keys_header(njs_vm_t *vm, njs_value_t *value, njs_value_t *keys)
+{
+ njs_int_t rc;
+ nxt_http_field_t *f;
+ nxt_http_request_t *r;
+
+ rc = njs_vm_array_alloc(vm, keys, 4);
+ if (rc != NJS_OK) {
+ return NJS_ERROR;
+ }
+
+ r = njs_vm_external(vm, nxt_js_proto_id, value);
+ if (r == NULL) {
+ return NJS_OK;
+ }
+
+ nxt_list_each(f, r->fields) {
+
+ value = njs_vm_array_push(vm, keys);
+ if (value == NULL) {
+ return NJS_ERROR;
+ }
+
+ rc = njs_vm_value_string_set(vm, value, f->name, f->name_length);
+ if (rc != NJS_OK) {
+ return NJS_ERROR;
+ }
+
+ } nxt_list_loop;
+
+ return NJS_OK;
+}
+
+
+static njs_int_t
nxt_http_js_ext_get_cookie(njs_vm_t *vm, njs_object_prop_t *prop,
njs_value_t *value, njs_value_t *setval, njs_value_t *retval)
{
@@ -271,3 +295,46 @@ nxt_http_js_ext_get_cookie(njs_vm_t *vm, njs_object_prop_t *prop,
return NJS_DECLINED;
}
+
+
+static njs_int_t
+nxt_http_js_ext_keys_cookie(njs_vm_t *vm, njs_value_t *value, njs_value_t *keys)
+{
+ njs_int_t rc;
+ nxt_array_t *cookies;
+ nxt_http_request_t *r;
+ nxt_http_name_value_t *nv, *start, *end;
+
+ rc = njs_vm_array_alloc(vm, keys, 4);
+ if (rc != NJS_OK) {
+ return NJS_ERROR;
+ }
+
+ r = njs_vm_external(vm, nxt_js_proto_id, value);
+ if (r == NULL) {
+ return NJS_OK;
+ }
+
+ cookies = nxt_http_cookies_parse(r);
+ if (nxt_slow_path(cookies == NULL)) {
+ return NJS_ERROR;
+ }
+
+ start = cookies->elts;
+ end = start + cookies->nelts;
+
+ for (nv = start; nv < end; nv++) {
+
+ value = njs_vm_array_push(vm, keys);
+ if (value == NULL) {
+ return NJS_ERROR;
+ }
+
+ rc = njs_vm_value_string_set(vm, value, nv->name, nv->name_length);
+ if (rc != NJS_OK) {
+ return NJS_ERROR;
+ }
+ }
+
+ return NJS_OK;
+}