From 141deec353c546674719d2bffe2fadeb174c4415 Mon Sep 17 00:00:00 2001 From: Zhidao HONG Date: Tue, 17 Jan 2023 10:37:45 +0800 Subject: [PATCH] NJS: added the keys API for the request objects. This commit is to loop through the request objects headers, arguments, and cookies. --- docs/changes.xml | 6 +++ src/nxt_http_js.c | 133 ++++++++++++++++++++++++++++++++++------------ 2 files changed, 106 insertions(+), 33 deletions(-) diff --git a/docs/changes.xml b/docs/changes.xml index 262b4a7a..414f8672 100644 --- a/docs/changes.xml +++ b/docs/changes.xml @@ -32,6 +32,12 @@ NGINX Unit updated to 1.30.0. date="" time="" packager="Nginx Packaging <nginx-packaging@f5.com>"> + + +support the keys API on the request objects in NJS. + + + args->start, + r->args->start + r->args->length, args); + + if (ret == NJS_ERROR) { return NJS_ERROR; } - start = args->elts; - end = start + args->nelts; + njs_value_assign(retval, args); - for (nv = start; nv < end; nv++) { - - 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); - } - } - - njs_value_undefined_set(retval); - - return NJS_DECLINED; + return NJS_OK; } @@ -227,6 +216,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; +}