[njs] Fetch: added support for HEAD method.

Dmitry Volyntsev xeioex at nginx.com
Tue Sep 27 23:41:06 UTC 2022


details:   https://hg.nginx.org/njs/rev/770f64020ada
branches:  
changeset: 1966:770f64020ada
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Tue Sep 27 16:40:06 2022 -0700
description:
Fetch: added support for HEAD method.

This closes #577 issue on Github.

diffstat:

 nginx/ngx_js_fetch.c |  15 ++++++++++++---
 1 files changed, 12 insertions(+), 3 deletions(-)

diffs (52 lines):

diff -r 43e35b05fd1b -r 770f64020ada nginx/ngx_js_fetch.c
--- a/nginx/ngx_js_fetch.c	Tue Sep 27 10:56:54 2022 -0700
+++ b/nginx/ngx_js_fetch.c	Tue Sep 27 16:40:06 2022 -0700
@@ -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
 
     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 *
             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 *
     } 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 {



More information about the nginx-devel mailing list