[njs] HTTP: improved setting empty headers.

Dmitry Volyntsev xeioex at nginx.com
Wed Feb 6 17:21:25 UTC 2019


details:   https://hg.nginx.org/njs/rev/016d5d9077ef
branches:  
changeset: 766:016d5d9077ef
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Wed Feb 06 19:52:54 2019 +0300
description:
HTTP: improved setting empty headers.

Treating empty value as deleting.

diffstat:

 nginx/ngx_http_js_module.c |  50 ++++++++++++++++++++++++++++-----------------
 1 files changed, 31 insertions(+), 19 deletions(-)

diffs (78 lines):

diff -r 9bd38c6bf046 -r 016d5d9077ef nginx/ngx_http_js_module.c
--- a/nginx/ngx_http_js_module.c	Wed Feb 06 19:22:18 2019 +0300
+++ b/nginx/ngx_http_js_module.c	Wed Feb 06 19:52:54 2019 +0300
@@ -949,7 +949,12 @@ ngx_http_js_ext_set_header_out(njs_vm_t 
     h = ngx_http_js_get_header(&r->headers_out.headers.part, v->start,
                                v->length);
 
-    if (h == NULL || h->hash == 0) {
+    if (h != NULL && value->length == 0) {
+        h->hash = 0;
+        h = NULL;
+    }
+
+    if (h == NULL && value->length != 0) {
         h = ngx_list_push(&r->headers_out.headers);
         if (h == NULL) {
             return NJS_ERROR;
@@ -964,19 +969,21 @@ ngx_http_js_ext_set_header_out(njs_vm_t 
 
         h->key.data = p;
         h->key.len = v->length;
+    }
+
+    if (h != NULL) {
+        p = ngx_pnalloc(r->pool, value->length);
+        if (p == NULL) {
+            return NJS_ERROR;
+        }
+
+        ngx_memcpy(p, value->start, value->length);
+
+        h->value.data = p;
+        h->value.len = value->length;
         h->hash = 1;
     }
 
-    p = ngx_pnalloc(r->pool, value->length);
-    if (p == NULL) {
-        return NJS_ERROR;
-    }
-
-    ngx_memcpy(p, value->start, value->length);
-
-    h->value.data = p;
-    h->value.len = value->length;
-
     if (v->length == nxt_length("Content-Encoding")
         && ngx_strncasecmp(v->start, (u_char *) "Content-Encoding",
                            v->length) == 0)
@@ -988,15 +995,20 @@ ngx_http_js_ext_set_header_out(njs_vm_t 
         && ngx_strncasecmp(v->start, (u_char *) "Content-Length",
                            v->length) == 0)
     {
-        n = ngx_atoi(value->start, value->length);
-        if (n == NGX_ERROR) {
-            h->hash = 0;
-            njs_vm_error(vm, "failed converting argument to integer");
-            return NJS_ERROR;
+        if (h != NULL) {
+            n = ngx_atoi(value->start, value->length);
+            if (n == NGX_ERROR) {
+                h->hash = 0;
+                njs_vm_error(vm, "failed converting argument to integer");
+                return NJS_ERROR;
+            }
+
+            r->headers_out.content_length = h;
+            r->headers_out.content_length_n = n;
+
+        } else {
+            ngx_http_clear_content_length(r);
         }
-
-        r->headers_out.content_length_n = n;
-        r->headers_out.content_length = h;
     }
 
     return NJS_OK;


More information about the nginx-devel mailing list