]> git.kaiwu.me - njs.git/commitdiff
Fetch: refactored request building logic into separate function.
authorDmitry Volyntsev <xeioex@nginx.com>
Thu, 9 Oct 2025 23:16:54 +0000 (16:16 -0700)
committerDmitry Volyntsev <xeioexception@gmail.com>
Mon, 27 Oct 2025 16:03:09 +0000 (09:03 -0700)
nginx/ngx_js_fetch.c
nginx/ngx_js_http.c
nginx/ngx_js_http.h
nginx/ngx_qjs_fetch.c

index b652ef27757ddf96489fe7a6b6647b4b6bad0691..992cc1d0d1e141fcf827db061b8274fb14325134 100644 (file)
@@ -508,15 +508,10 @@ ngx_js_ext_fetch(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
 {
     njs_int_t            ret;
     ngx_url_t            u;
-    ngx_uint_t           i;
-    njs_bool_t           has_host;
-    ngx_str_t            method;
     ngx_pool_t          *pool;
     njs_value_t         *init, *value;
     ngx_js_http_t       *http;
     ngx_js_fetch_t      *fetch;
-    ngx_list_part_t     *part;
-    ngx_js_tb_elt_t     *h;
     ngx_js_request_t     request;
     ngx_connection_t    *c;
     ngx_resolver_ctx_t  *ctx;
@@ -600,126 +595,7 @@ ngx_js_ext_fetch(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
     NJS_CHB_MP_INIT(&http->chain, njs_vm_memory_pool(vm));
     NJS_CHB_MP_INIT(&http->response.chain, njs_vm_memory_pool(vm));
 
-    njs_chb_append(&http->chain, request.method.data, request.method.len);
-    njs_chb_append_literal(&http->chain, " ");
-
-    if (u.uri.len == 0 || u.uri.data[0] != '/') {
-        njs_chb_append_literal(&http->chain, "/");
-    }
-
-    njs_chb_append(&http->chain, u.uri.data, u.uri.len);
-    njs_chb_append_literal(&http->chain, " HTTP/1.1" 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);
-
-        if (!u.no_port) {
-            njs_chb_sprintf(&http->chain, 32, ":%d", u.port);
-        }
-
-        njs_chb_append_literal(&http->chain, CRLF);
-    }
-
-    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)
-        {
-            continue;
-        }
-
-        if (h[i].key.len == 14
-            && ngx_strncasecmp(h[i].key.data, (u_char *) "Content-Length", 14)
-            == 0)
-        {
-            continue;
-        }
-
-        if (h[i].key.len == 10
-            && ngx_strncasecmp(h[i].key.data, (u_char *) "Connection", 10)
-            == 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);
-    }
-
-    if (!http->keepalive) {
-        njs_chb_append_literal(&http->chain, "Connection: close" CRLF);
-    }
-
-    if (request.body.len != 0) {
-        njs_chb_sprintf(&http->chain, 32, "Content-Length: %uz" CRLF CRLF,
-                        request.body.len);
-        njs_chb_append(&http->chain, request.body.data, request.body.len);
-
-    } else {
-        method = request.method;
-
-        if ((method.len == 4
-            && (ngx_strncasecmp(method.data, (u_char *) "POST", 4) == 0))
-            || (method.len == 3
-                && ngx_strncasecmp(method.data, (u_char *) "PUT", 3) == 0))
-        {
-            njs_chb_append_literal(&http->chain, "Content-Length: 0" CRLF CRLF);
-
-        } else {
-            njs_chb_append_literal(&http->chain, CRLF);
-        }
-    }
+    ngx_js_fetch_build_request(http, &request, &u.uri, &u);
 
     if (u.addrs == NULL) {
         ctx = ngx_js_http_resolve(http, ngx_external_resolver(vm, external),
index 259f681dd624d683294ff9f781cee4f87901af62..9389bbca93d6f5ab86fbed22fad37759faa9e762 100644 (file)
@@ -47,6 +47,9 @@ static ngx_int_t ngx_js_http_parse_header_line(ngx_js_http_parse_t *hp,
 static ngx_int_t ngx_js_http_parse_chunked(ngx_js_http_chunk_parse_t *hcp,
     ngx_buf_t *b, njs_chb_t *chain);
 
+static void ngx_js_fetch_append_request_headers(njs_chb_t *chain,
+    ngx_js_request_t *request);
+
 #if (NGX_SSL)
 static void ngx_js_http_ssl_init_connection(ngx_js_http_t *http);
 static void ngx_js_http_ssl_handshake_handler(ngx_connection_t *c);
@@ -1856,3 +1859,148 @@ close:
     ngx_queue_remove(&cache->queue);
     ngx_queue_insert_head(&conf->fetch_keepalive_free, &cache->queue);
 }
+
+
+static void
+ngx_js_fetch_append_request_headers(njs_chb_t *chain,
+    ngx_js_request_t *request)
+{
+    ngx_uint_t        i;
+    ngx_list_part_t  *part;
+    ngx_js_tb_elt_t  *h;
+
+    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)
+        {
+            continue;
+        }
+
+        if (h[i].key.len == 14
+            && ngx_strncasecmp(h[i].key.data, (u_char *) "Content-Length",
+                               14) == 0)
+        {
+            continue;
+        }
+
+        if (h[i].key.len == 10
+            && ngx_strncasecmp(h[i].key.data, (u_char *) "Connection", 10)
+               == 0)
+        {
+            continue;
+        }
+
+        njs_chb_append(chain, h[i].key.data, h[i].key.len);
+        njs_chb_append_literal(chain, ": ");
+        njs_chb_append(chain, h[i].value.data, h[i].value.len);
+        njs_chb_append_literal(chain, CRLF);
+    }
+}
+
+
+void
+ngx_js_fetch_build_request(ngx_js_http_t *http, ngx_js_request_t *request,
+    ngx_str_t *path, ngx_url_t *u)
+{
+    ngx_str_t         method;
+    ngx_uint_t        i;
+    njs_bool_t        has_host;
+    ngx_list_part_t  *part;
+    ngx_js_tb_elt_t  *h;
+
+    njs_chb_append(&http->chain, request->method.data, request->method.len);
+    njs_chb_append_literal(&http->chain, " ");
+
+    if (path->len == 0 || path->data[0] != '/') {
+        njs_chb_append_literal(&http->chain, "/");
+    }
+
+    njs_chb_append(&http->chain, path->data, path->len);
+    njs_chb_append_literal(&http->chain, " HTTP/1.1" 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);
+
+        if (!u->no_port) {
+            njs_chb_sprintf(&http->chain, 32, ":%d", u->port);
+        }
+
+        njs_chb_append_literal(&http->chain, CRLF);
+    }
+
+    ngx_js_fetch_append_request_headers(&http->chain, request);
+
+    if (!http->keepalive) {
+        njs_chb_append_literal(&http->chain, "Connection: close" CRLF);
+    }
+
+    if (request->body.len != 0) {
+        njs_chb_sprintf(&http->chain, 32, "Content-Length: %uz" CRLF CRLF,
+                        request->body.len);
+        njs_chb_append(&http->chain, request->body.data, request->body.len);
+
+    } else {
+        method = request->method;
+
+        if ((method.len == 4
+             && (ngx_strncasecmp(method.data, (u_char *) "POST", 4) == 0))
+            || (method.len == 3
+                && ngx_strncasecmp(method.data, (u_char *) "PUT", 3) == 0))
+        {
+            njs_chb_append_literal(&http->chain, "Content-Length: 0" CRLF CRLF);
+
+        } else {
+            njs_chb_append_literal(&http->chain, CRLF);
+        }
+    }
+}
index d122f28c3d61cc1b789c3cc49eb320c116c0f5de..51f0bcd439df20595be60d70fa7ed73b9d8c0b0f 100644 (file)
@@ -167,5 +167,8 @@ void ngx_js_http_trim(u_char **value, size_t *len,
     int trim_c0_control_or_space);
 ngx_int_t ngx_js_check_header_name(u_char *name, size_t len);
 
+void ngx_js_fetch_build_request(ngx_js_http_t *http, ngx_js_request_t *request,
+    ngx_str_t *path, ngx_url_t *u);
+
 
 #endif /* _NGX_JS_HTTP_H_INCLUDED_ */
index 447dfadd8f75b8a5d4942b7295e6ca618bce35d1..afaeb2d51d8173e8d2284a2404fecfbb89733a3b 100644 (file)
@@ -232,19 +232,14 @@ JSValue
 ngx_qjs_ext_fetch(JSContext *cx, JSValueConst this_val, int argc,
     JSValueConst *argv)
 {
-    int                  has_host;
     void                *external;
     JSValue              init, value, promise;
     ngx_int_t            rc;
     ngx_url_t            u;
-    ngx_str_t            method;
-    ngx_uint_t           i;
     ngx_pool_t          *pool;
     ngx_js_ctx_t        *ctx;
     ngx_js_http_t       *http;
     ngx_qjs_fetch_t     *fetch;
-    ngx_list_part_t     *part;
-    ngx_js_tb_elt_t     *h;
     ngx_connection_t    *c;
     ngx_js_request_t     request;
     ngx_resolver_ctx_t  *rs;
@@ -337,126 +332,7 @@ ngx_qjs_ext_fetch(JSContext *cx, JSValueConst this_val, int argc,
     NJS_CHB_MP_INIT(&http->chain, ctx->engine->pool);
     NJS_CHB_MP_INIT(&http->response.chain, ctx->engine->pool);
 
-    njs_chb_append(&http->chain, request.method.data, request.method.len);
-    njs_chb_append_literal(&http->chain, " ");
-
-    if (u.uri.len == 0 || u.uri.data[0] != '/') {
-        njs_chb_append_literal(&http->chain, "/");
-    }
-
-    njs_chb_append(&http->chain, u.uri.data, u.uri.len);
-    njs_chb_append_literal(&http->chain, " HTTP/1.1" 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);
-
-        if (!u.no_port) {
-            njs_chb_sprintf(&http->chain, 32, ":%d", u.port);
-        }
-
-        njs_chb_append_literal(&http->chain, CRLF);
-    }
-
-    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)
-        {
-            continue;
-        }
-
-        if (h[i].key.len == 14
-            && ngx_strncasecmp(h[i].key.data, (u_char *) "Content-Length", 14)
-            == 0)
-        {
-            continue;
-        }
-
-        if (h[i].key.len == 10
-            && ngx_strncasecmp(h[i].key.data, (u_char *) "Connection", 10)
-               == 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);
-    }
-
-    if (!http->keepalive) {
-        njs_chb_append_literal(&http->chain, "Connection: close" CRLF);
-    }
-
-    if (request.body.len != 0) {
-        njs_chb_sprintf(&http->chain, 32, "Content-Length: %uz" CRLF CRLF,
-                        request.body.len);
-        njs_chb_append(&http->chain, request.body.data, request.body.len);
-
-    } else {
-        method = request.method;
-
-        if ((method.len == 4
-            && (ngx_strncasecmp(method.data, (u_char *) "POST", 4) == 0))
-            || (method.len == 3
-                && ngx_strncasecmp(method.data, (u_char *) "PUT", 3) == 0))
-        {
-            njs_chb_append_literal(&http->chain, "Content-Length: 0" CRLF CRLF);
-
-        } else {
-            njs_chb_append_literal(&http->chain, CRLF);
-        }
-    }
+    ngx_js_fetch_build_request(http, &request, &u.uri, &u);
 
     if (u.addrs == NULL) {
         rs = ngx_js_http_resolve(http, ngx_qjs_external_resolver(cx, external),