]> git.kaiwu.me - njs.git/commitdiff
HTTP: improved iteration over header objects with duplicates.
authorDmitry Volyntsev <xeioex@nginx.com>
Tue, 21 Apr 2020 11:57:29 +0000 (11:57 +0000)
committerDmitry Volyntsev <xeioex@nginx.com>
Tue, 21 Apr 2020 11:57:29 +0000 (11:57 +0000)
In 9e327cd3a33e duplicates were filtered out only for Cookie and
X-Forwarded-For.

nginx/ngx_http_js_module.c
src/njs.h
src/njs_vm.c

index 9c3074f73488074efdf1a8911bd74308efd3ba4a..23bd54a7e0cf0a29f39da66381203d7887ac5a4a 100644 (file)
@@ -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, njs_value_t *value, njs_value_t *keys,
             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);
 
-            cookie = 1;
-        }
+        for (i = 0; i < length; i++) {
+            njs_value_string_get(njs_argument(start, i), &hdr);
 
-        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 (h->key.len == hdr.length
+                && ngx_strncasecmp(h->key.data, hdr.start, hdr.length) == 0)
+            {
+                break;
             }
-
-            x_for = 1;
         }
 
-        value = njs_vm_array_push(vm, keys);
-        if (value == NULL) {
-            return NJS_ERROR;
-        }
+        if (i == length) {
+            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++;
         }
     }
 
index 4d5d351bdbc649812bb264e5ea84437bc91c381d..00cdcfc412fb7960d7c702e7feed66c6fc472e64 100644 (file)
--- a/src/njs.h
+++ b/src/njs.h
@@ -302,6 +302,8 @@ NJS_EXPORT njs_function_t *njs_vm_function(njs_vm_t *vm, const njs_str_t *name);
 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.
index 42700815817f6e486e37299c0afc68bf4ea5b296..56618306cdbd37b78f8392e11cdb6e59321264ec 100644 (file)
@@ -670,6 +670,13 @@ njs_vm_bind(njs_vm_t *vm, const njs_str_t *var_name, const njs_value_t *value,
 }
 
 
+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)