]> git.kaiwu.me - njs.git/commitdiff
HTTP: improved setting empty headers.
authorDmitry Volyntsev <xeioex@nginx.com>
Wed, 6 Feb 2019 16:52:54 +0000 (19:52 +0300)
committerDmitry Volyntsev <xeioex@nginx.com>
Wed, 6 Feb 2019 16:52:54 +0000 (19:52 +0300)
Treating empty value as deleting.

nginx/ngx_http_js_module.c

index 1c1727083a42bc28460d4597998bccdbaf216fe2..c6608e202d0d53f4ca9727e9344e43fead228e99 100644 (file)
@@ -949,7 +949,12 @@ ngx_http_js_ext_set_header_out(njs_vm_t *vm, void *obj, uintptr_t data,
     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,18 +969,20 @@ ngx_http_js_ext_set_header_out(njs_vm_t *vm, void *obj, uintptr_t data,
 
         h->key.data = p;
         h->key.len = v->length;
-        h->hash = 1;
     }
 
-    p = ngx_pnalloc(r->pool, value->length);
-    if (p == NULL) {
-        return NJS_ERROR;
-    }
+    if (h != NULL) {
+        p = ngx_pnalloc(r->pool, value->length);
+        if (p == NULL) {
+            return NJS_ERROR;
+        }
 
-    ngx_memcpy(p, value->start, value->length);
+        ngx_memcpy(p, value->start, value->length);
 
-    h->value.data = p;
-    h->value.len = value->length;
+        h->value.data = p;
+        h->value.len = value->length;
+        h->hash = 1;
+    }
 
     if (v->length == nxt_length("Content-Encoding")
         && ngx_strncasecmp(v->start, (u_char *) "Content-Encoding",
@@ -988,15 +995,20 @@ ngx_http_js_ext_set_header_out(njs_vm_t *vm, void *obj, uintptr_t data,
         && 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;
 
-        r->headers_out.content_length_n = n;
-        r->headers_out.content_length = h;
+        } else {
+            ngx_http_clear_content_length(r);
+        }
     }
 
     return NJS_OK;