From: Dmitry Volyntsev Date: Thu, 9 Oct 2025 23:16:54 +0000 (-0700) Subject: Fetch: refactored request building logic into separate function. X-Git-Tag: 0.9.4~4 X-Git-Url: http://www.kaiwu.me/postgresql/commit/?a=commitdiff_plain;h=6594528b662d68105d90cc3f039a04680a496ea8;p=njs.git Fetch: refactored request building logic into separate function. --- diff --git a/nginx/ngx_js_fetch.c b/nginx/ngx_js_fetch.c index b652ef27..992cc1d0 100644 --- a/nginx/ngx_js_fetch.c +++ b/nginx/ngx_js_fetch.c @@ -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), diff --git a/nginx/ngx_js_http.c b/nginx/ngx_js_http.c index 259f681d..9389bbca 100644 --- a/nginx/ngx_js_http.c +++ b/nginx/ngx_js_http.c @@ -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); + } + } +} diff --git a/nginx/ngx_js_http.h b/nginx/ngx_js_http.h index d122f28c..51f0bcd4 100644 --- a/nginx/ngx_js_http.h +++ b/nginx/ngx_js_http.h @@ -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_ */ diff --git a/nginx/ngx_qjs_fetch.c b/nginx/ngx_qjs_fetch.c index 447dfadd..afaeb2d5 100644 --- a/nginx/ngx_qjs_fetch.c +++ b/nginx/ngx_qjs_fetch.c @@ -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),