[njs] Fetch: insuring Host header is always the first header.

Dmitry Volyntsev xeioex at nginx.com
Thu May 11 04:26:14 UTC 2023


details:   https://hg.nginx.org/njs/rev/89c821242caf
branches:  
changeset: 2114:89c821242caf
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Tue May 09 22:09:13 2023 -0700
description:
Fetch: insuring Host header is always the first header.

diffstat:

 nginx/ngx_js_fetch.c |  49 +++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 45 insertions(+), 4 deletions(-)

diffs (79 lines):

diff -r 4aed0532158c -r 89c821242caf nginx/ngx_js_fetch.c
--- a/nginx/ngx_js_fetch.c	Tue May 09 22:09:13 2023 -0700
+++ b/nginx/ngx_js_fetch.c	Tue May 09 22:09:13 2023 -0700
@@ -658,6 +658,7 @@ ngx_js_ext_fetch(njs_vm_t *vm, njs_value
     njs_int_t            ret;
     ngx_url_t            u;
     ngx_uint_t           i;
+    njs_bool_t           has_host;
     ngx_pool_t          *pool;
     njs_value_t         *init, *value;
     ngx_js_http_t       *http;
@@ -746,10 +747,42 @@ ngx_js_ext_fetch(njs_vm_t *vm, njs_value
     njs_chb_append(&http->chain, u.uri.data, u.uri.len);
     njs_chb_append_literal(&http->chain, " HTTP/1.1" CRLF);
 
-    njs_chb_append_literal(&http->chain, "Host: ");
-    njs_chb_append(&http->chain, u.host.data, u.host.len);
-    njs_chb_append_literal(&http->chain, CRLF);
-    njs_chb_append_literal(&http->chain, "Connection: close" CRLF);
+    has_host = 0;
+    part = &request.headers.header_list.part;
+    h = part->elts;
+
+    for (i = 0; /* void */; i++) {
+
+        if (i >= part->nelts) {
+            if (part->next == NULL) {
+                break;
+            }
+
+            part = part->next;
+            h = part->elts;
+            i = 0;
+        }
+
+        if (h[i].hash == 0) {
+            continue;
+        }
+
+        if (h[i].key.len == 4
+            && ngx_strncasecmp(h[i].key.data, (u_char *) "Host", 4) == 0)
+        {
+            has_host = 1;
+            njs_chb_append_literal(&http->chain, "Host: ");
+            njs_chb_append(&http->chain, h[i].value.data, h[i].value.len);
+            njs_chb_append_literal(&http->chain, CRLF);
+            break;
+        }
+    }
+
+    if (!has_host) {
+        njs_chb_append_literal(&http->chain, "Host: ");
+        njs_chb_append(&http->chain, u.host.data, u.host.len);
+        njs_chb_append_literal(&http->chain, CRLF);
+    }
 
     part = &request.headers.header_list.part;
     h = part->elts;
@@ -770,12 +803,20 @@ ngx_js_ext_fetch(njs_vm_t *vm, njs_value
             continue;
         }
 
+        if (h[i].key.len == 4
+            && ngx_strncasecmp(h[i].key.data, (u_char *) "Host", 4) == 0)
+        {
+            continue;
+        }
+
         njs_chb_append(&http->chain, h[i].key.data, h[i].key.len);
         njs_chb_append_literal(&http->chain, ": ");
         njs_chb_append(&http->chain, h[i].value.data, h[i].value.len);
         njs_chb_append_literal(&http->chain, CRLF);
     }
 
+    njs_chb_append_literal(&http->chain, "Connection: close" CRLF);
+
 #if (NGX_SSL)
     http->tls_name.data = u.host.data;
     http->tls_name.len = u.host.len;


More information about the nginx-devel mailing list