From a6f6be96fc523250bdc36893c6402b42db73b87d Mon Sep 17 00:00:00 2001 From: Dmitry Volyntsev Date: Tue, 27 Sep 2022 16:40:06 -0700 Subject: [PATCH] Fetch: added support for HEAD method. This closes #577 issue on Github. --- nginx/ngx_js_fetch.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/nginx/ngx_js_fetch.c b/nginx/ngx_js_fetch.c index 34059ddd..afa9a500 100644 --- a/nginx/ngx_js_fetch.c +++ b/nginx/ngx_js_fetch.c @@ -66,6 +66,8 @@ struct ngx_js_http_s { njs_str_t url; ngx_array_t headers; + unsigned header_only; + #if (NGX_SSL) ngx_str_t tls_name; ngx_ssl_t *ssl; @@ -473,6 +475,8 @@ ngx_js_ext_fetch(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, njs_chb_init(&http->chain, njs_vm_memory_pool(vm)); + http->header_only = njs_strstr_case_eq(&method, &njs_str_value("HEAD")); + njs_chb_append(&http->chain, method.start, method.length); njs_chb_append_literal(&http->chain, " "); @@ -1404,14 +1408,16 @@ ngx_js_http_process_body(ngx_js_http_t *http) return NGX_ERROR; } - if (http->http_parse.chunked + if (!http->header_only + && http->http_parse.chunked && http->http_parse.content_length_n == -1) { ngx_js_http_error(http, 0, "invalid fetch chunked response"); return NGX_ERROR; } - if (http->http_parse.content_length_n == -1 + if (http->header_only + || http->http_parse.content_length_n == -1 || size == http->http_parse.content_length_n) { ret = njs_vm_external_create(http->vm, njs_value_arg(&http->reply), @@ -1459,7 +1465,10 @@ ngx_js_http_process_body(ngx_js_http_t *http) } else { size = njs_chb_size(&http->chain); - if (http->http_parse.content_length_n == -1) { + if (http->header_only) { + need = 0; + + } else if (http->http_parse.content_length_n == -1) { need = http->max_response_body_size - size; } else { -- 2.47.3