[njs] HTTP: improved iteration over header objects with duplicates.

Dmitry Volyntsev xeioex at nginx.com
Tue Apr 21 12:37:30 UTC 2020


details:   https://hg.nginx.org/njs/rev/55dd0c0acb66
branches:  
changeset: 1378:55dd0c0acb66
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Tue Apr 21 11:57:29 2020 +0000
description:
HTTP: improved iteration over header objects with duplicates.

In 9e327cd3a33e duplicates were filtered out only for Cookie and
X-Forwarded-For.

diffstat:

 nginx/ngx_http_js_module.c |  54 ++++++++++++++++++++-------------------------
 src/njs.h                  |   2 +
 src/njs_vm.c               |   7 +++++
 3 files changed, 33 insertions(+), 30 deletions(-)

diffs (113 lines):

diff -r 9c3d692d9f4c -r 55dd0c0acb66 nginx/ngx_http_js_module.c
--- a/nginx/ngx_http_js_module.c	Tue Apr 21 11:56:46 2020 +0000
+++ b/nginx/ngx_http_js_module.c	Tue Apr 21 11:57:29 2020 +0000
@@ -801,19 +801,19 @@ static njs_int_t
 ngx_http_js_ext_keys_header(njs_vm_t *vm, njs_value_t *value, njs_value_t *keys,
     ngx_list_t *headers)
 {
-    njs_int_t         rc, cookie, x_for;
+    int64_t           i, length;
+    njs_int_t         rc;
+    njs_str_t         hdr;
     ngx_uint_t        item;
+    njs_value_t      *start;
     ngx_list_part_t  *part;
     ngx_table_elt_t  *header, *h;
 
     part = &headers->part;
     item = 0;
-
-    cookie = 0;
-    x_for = 0;
+    length = 0;
 
     while (part) {
-
         if (item >= part->nelts) {
             part = part->next;
             item = 0;
@@ -827,36 +827,30 @@ ngx_http_js_ext_keys_header(njs_vm_t *vm
             continue;
         }
 
-        if (h->key.len == njs_length("Cookie")
-            && ngx_strncasecmp(h->key.data, (u_char *) "Cookie",
-                               h->key.len) == 0)
-        {
-            if (cookie) {
-                continue;
+        start = njs_vm_array_start(vm, keys);
+
+        for (i = 0; i < length; i++) {
+            njs_value_string_get(njs_argument(start, i), &hdr);
+
+            if (h->key.len == hdr.length
+                && ngx_strncasecmp(h->key.data, hdr.start, hdr.length) == 0)
+            {
+                break;
             }
-
-            cookie = 1;
         }
 
-        if (h->key.len == njs_length("X-Forwarded-For")
-            && ngx_strncasecmp(h->key.data, (u_char *) "X-Forwarded-For",
-                               h->key.len) == 0)
-        {
-            if (x_for) {
-                continue;
+        if (i == length) {
+            value = njs_vm_array_push(vm, keys);
+            if (value == NULL) {
+                return NJS_ERROR;
             }
 
-            x_for = 1;
-        }
-
-        value = njs_vm_array_push(vm, keys);
-        if (value == NULL) {
-            return NJS_ERROR;
-        }
-
-        rc = njs_vm_value_string_set(vm, value, h->key.data, h->key.len);
-        if (rc != NJS_OK) {
-            return NJS_ERROR;
+            rc = njs_vm_value_string_set(vm, value, h->key.data, h->key.len);
+            if (rc != NJS_OK) {
+                return NJS_ERROR;
+            }
+
+            length++;
         }
     }
 
diff -r 9c3d692d9f4c -r 55dd0c0acb66 src/njs.h
--- a/src/njs.h	Tue Apr 21 11:56:46 2020 +0000
+++ b/src/njs.h	Tue Apr 21 11:57:29 2020 +0000
@@ -302,6 +302,8 @@ NJS_EXPORT njs_function_t *njs_vm_functi
 NJS_EXPORT njs_value_t *njs_vm_retval(njs_vm_t *vm);
 NJS_EXPORT void njs_vm_retval_set(njs_vm_t *vm, const njs_value_t *value);
 
+/*  Gets string value, no copy. */
+NJS_EXPORT void njs_value_string_get(njs_value_t *value, njs_str_t *dst);
 /*
  * Sets a byte string value.
  *   start data is not copied and should not be freed.
diff -r 9c3d692d9f4c -r 55dd0c0acb66 src/njs_vm.c
--- a/src/njs_vm.c	Tue Apr 21 11:56:46 2020 +0000
+++ b/src/njs_vm.c	Tue Apr 21 11:57:29 2020 +0000
@@ -670,6 +670,13 @@ njs_vm_bind(njs_vm_t *vm, const njs_str_
 }
 
 
+void
+njs_value_string_get(njs_value_t *value, njs_str_t *dst)
+{
+    njs_string_get(value, dst);
+}
+
+
 njs_int_t
 njs_vm_value_string_set(njs_vm_t *vm, njs_value_t *value, const u_char *start,
     uint32_t size)


More information about the nginx-devel mailing list