[njs] HTTP: deduplicated special header handlers for nginx <= 1.22.

Dmitry Volyntsev xeioex at nginx.com
Tue Jun 6 01:31:39 UTC 2023


details:   https://hg.nginx.org/njs/rev/a5affeb25558
branches:  
changeset: 2148:a5affeb25558
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Mon Jun 05 18:21:06 2023 -0700
description:
HTTP: deduplicated special header handlers for nginx <= 1.22.

diffstat:

 nginx/ngx_http_js_module.c |  373 ++++++++++++++++----------------------------
 1 files changed, 140 insertions(+), 233 deletions(-)

diffs (441 lines):

diff -r 5952d7709956 -r a5affeb25558 nginx/ngx_http_js_module.c
--- a/nginx/ngx_http_js_module.c	Mon Feb 06 09:36:14 2023 -0800
+++ b/nginx/ngx_http_js_module.c	Mon Jun 05 18:21:06 2023 -0700
@@ -116,15 +116,15 @@ static njs_int_t ngx_http_js_header_arra
 static njs_int_t ngx_http_js_header_generic(njs_vm_t *vm,
     ngx_http_request_t *r, ngx_list_t *headers, njs_str_t *name,
     njs_value_t *setval, njs_value_t *retval);
-static njs_int_t ngx_http_js_content_length(njs_vm_t *vm, ngx_http_request_t *r,
-    ngx_list_t *headers, njs_str_t *name, njs_value_t *setval,
-    njs_value_t *retval);
-static njs_int_t ngx_http_js_content_encoding(njs_vm_t *vm,
+static njs_int_t ngx_http_js_content_encoding122(njs_vm_t *vm,
     ngx_http_request_t *r, ngx_list_t *headers, njs_str_t *name,
     njs_value_t *setval, njs_value_t *retval);
-static njs_int_t ngx_http_js_content_type(njs_vm_t *vm, ngx_http_request_t *r,
-    ngx_list_t *headers, njs_str_t *name, njs_value_t *setval,
-    njs_value_t *retval);
+static njs_int_t ngx_http_js_content_length122(njs_vm_t *vm,
+    ngx_http_request_t *r, ngx_list_t *headers, njs_str_t *name,
+    njs_value_t *setval, njs_value_t *retval);
+static njs_int_t ngx_http_js_content_type122(njs_vm_t *vm,
+    ngx_http_request_t *r, ngx_list_t *headers, njs_str_t *name,
+    njs_value_t *setval, njs_value_t *retval);
 #endif
 static njs_int_t ngx_http_js_ext_keys_header_out(njs_vm_t *vm,
     njs_value_t *value, njs_value_t *keys);
@@ -203,6 +203,13 @@ static njs_int_t ngx_http_js_header_in(n
     unsigned flags, njs_str_t *name, njs_value_t *retval);
 static njs_int_t ngx_http_js_header_out(njs_vm_t *vm, ngx_http_request_t *r,
     unsigned flags, njs_str_t *name, njs_value_t *setval, njs_value_t *retval);
+static njs_int_t ngx_http_js_header_out_special(njs_vm_t *vm,
+    ngx_http_request_t *r, njs_str_t *v, njs_value_t *setval,
+    njs_value_t *retval, ngx_table_elt_t **hh);
+static njs_int_t ngx_http_js_header_generic(njs_vm_t *vm,
+    ngx_http_request_t *r, ngx_list_t *headers, ngx_table_elt_t **ph,
+    unsigned flags, njs_str_t *name, njs_value_t *retval);
+#endif
 static njs_int_t ngx_http_js_content_encoding(njs_vm_t *vm,
     ngx_http_request_t *r, unsigned flags, njs_str_t *name,
     njs_value_t *setval, njs_value_t *retval);
@@ -212,13 +219,6 @@ static njs_int_t ngx_http_js_content_len
 static njs_int_t ngx_http_js_content_type(njs_vm_t *vm, ngx_http_request_t *r,
     unsigned flags, njs_str_t *name, njs_value_t *setval,
     njs_value_t *retval);
-static njs_int_t ngx_http_js_header_out_special(njs_vm_t *vm,
-    ngx_http_request_t *r, njs_str_t *v, njs_value_t *setval,
-    njs_value_t *retval, ngx_table_elt_t **hh);
-static njs_int_t ngx_http_js_header_generic(njs_vm_t *vm,
-    ngx_http_request_t *r, ngx_list_t *headers, ngx_table_elt_t **ph,
-    unsigned flags, njs_str_t *name, njs_value_t *retval);
-#endif
 
 static njs_host_event_t ngx_http_js_set_timer(njs_external_ptr_t external,
     uint64_t delay, njs_vm_event_t vm_event);
@@ -1476,9 +1476,9 @@ ngx_http_js_ext_header_out(njs_vm_t *vm,
     static ngx_http_js_header_t headers_out[] = {
 #if defined(nginx_version) && (nginx_version < 1023000)
         { njs_str("Age"), ngx_http_js_header_single },
-        { njs_str("Content-Type"), ngx_http_js_content_type },
-        { njs_str("Content-Length"), ngx_http_js_content_length },
-        { njs_str("Content-Encoding"), ngx_http_js_content_encoding },
+        { njs_str("Content-Type"), ngx_http_js_content_type122 },
+        { njs_str("Content-Length"), ngx_http_js_content_length122 },
+        { njs_str("Content-Encoding"), ngx_http_js_content_encoding122 },
         { njs_str("Etag"), ngx_http_js_header_single },
         { njs_str("Expires"), ngx_http_js_header_single },
         { njs_str("Last-Modified"), ngx_http_js_header_single },
@@ -1884,119 +1884,26 @@ ngx_http_js_header_generic(njs_vm_t *vm,
 
 
 static njs_int_t
-ngx_http_js_content_encoding(njs_vm_t *vm, ngx_http_request_t *r,
+ngx_http_js_content_encoding122(njs_vm_t *vm, ngx_http_request_t *r,
     ngx_list_t *headers, njs_str_t *v, njs_value_t *setval, njs_value_t *retval)
 {
-    njs_int_t         rc;
-    ngx_table_elt_t  *h;
-
-    rc = ngx_http_js_header_out_special(vm, r, v, setval, retval, &h);
-    if (rc == NJS_ERROR) {
-        return NJS_ERROR;
-    }
-
-    if (setval != NULL || retval == NULL) {
-        r->headers_out.content_encoding = h;
-    }
-
-    return NJS_OK;
+    return ngx_http_js_content_encoding(vm, r, 0, v, setval, retval);
 }
 
 
 static njs_int_t
-ngx_http_js_content_length(njs_vm_t *vm, ngx_http_request_t *r,
+ngx_http_js_content_length122(njs_vm_t *vm, ngx_http_request_t *r,
     ngx_list_t *headers, njs_str_t *v, njs_value_t *setval, njs_value_t *retval)
 {
-    u_char           *p, *start;
-    njs_int_t         rc;
-    ngx_int_t         n;
-    ngx_table_elt_t  *h;
-    u_char            content_len[NGX_OFF_T_LEN];
-
-    if (retval != NULL && setval == NULL) {
-        if (r->headers_out.content_length == NULL
-            && r->headers_out.content_length_n >= 0)
-        {
-            p = ngx_sprintf(content_len, "%O", r->headers_out.content_length_n);
-
-            start = njs_vm_value_string_alloc(vm, retval, p - content_len);
-            if (start == NULL) {
-                return NJS_ERROR;
-            }
-
-            ngx_memcpy(start, content_len, p - content_len);
-
-            return NJS_OK;
-        }
-    }
-
-    rc = ngx_http_js_header_out_special(vm, r, v, setval, retval, &h);
-    if (rc == NJS_ERROR) {
-        return NJS_ERROR;
-    }
-
-    if (setval != NULL || retval == NULL) {
-        if (h != NULL) {
-            n = ngx_atoi(h->value.data, h->value.len);
-            if (n == NGX_ERROR) {
-                h->hash = 0;
-                njs_vm_error(vm, "failed converting argument "
-                             "to positive integer");
-                return NJS_ERROR;
-            }
-
-            r->headers_out.content_length = h;
-            r->headers_out.content_length_n = n;
-
-        } else {
-            ngx_http_clear_content_length(r);
-        }
-    }
-
-    return NJS_OK;
+    return ngx_http_js_content_length(vm, r, 0, v, setval, retval);
 }
 
 
 static njs_int_t
-ngx_http_js_content_type(njs_vm_t *vm, ngx_http_request_t *r,
+ngx_http_js_content_type122(njs_vm_t *vm, ngx_http_request_t *r,
     ngx_list_t *headers, njs_str_t *v, njs_value_t *setval, njs_value_t *retval)
 {
-    int64_t              length;
-    njs_int_t            rc;
-    njs_str_t            s;
-    ngx_str_t           *hdr;
-    njs_opaque_value_t   lvalue;
-
-    if (retval != NULL && setval == NULL) {
-        hdr = &r->headers_out.content_type;
-
-        if (hdr->len == 0) {
-            njs_value_undefined_set(retval);
-            return NJS_OK;
-        }
-
-        return njs_vm_value_string_set(vm, retval, hdr->data, hdr->len);
-    }
-
-    if (setval != NULL && njs_value_is_array(setval)) {
-        rc = njs_vm_array_length(vm, setval, &length);
-        if (rc != NJS_OK) {
-            return NJS_ERROR;
-        }
-
-        setval = njs_vm_array_prop(vm, setval, length - 1, &lvalue);
-    }
-
-    if (ngx_js_string(vm, setval, &s) != NGX_OK) {
-        return NJS_ERROR;
-    }
-
-    r->headers_out.content_type.len = s.length;
-    r->headers_out.content_type_len = r->headers_out.content_type.len;
-    r->headers_out.content_type.data = s.start;
-    r->headers_out.content_type_lowcase = NULL;
-
-    return NJS_OK;
+    return ngx_http_js_content_type(vm, r, 0, v, setval, retval);
 }
 #endif
 
@@ -3659,123 +3566,6 @@ ngx_http_js_header_out(njs_vm_t *vm, ngx
 
 
 static njs_int_t
-ngx_http_js_content_encoding(njs_vm_t *vm, ngx_http_request_t *r,
-    unsigned flags, njs_str_t *v, njs_value_t *setval, njs_value_t *retval)
-{
-    njs_int_t         rc;
-    ngx_table_elt_t  *h;
-
-    rc = ngx_http_js_header_out_special(vm, r, v, setval, retval, &h);
-    if (rc == NJS_ERROR) {
-        return NJS_ERROR;
-    }
-
-    if (setval != NULL || retval == NULL) {
-        r->headers_out.content_encoding = h;
-    }
-
-    return NJS_OK;
-}
-
-
-static njs_int_t
-ngx_http_js_content_length(njs_vm_t *vm, ngx_http_request_t *r,
-    unsigned flags, njs_str_t *v, njs_value_t *setval, njs_value_t *retval)
-{
-    u_char           *p, *start;
-    njs_int_t         rc;
-    ngx_int_t         n;
-    ngx_table_elt_t  *h;
-    u_char            content_len[NGX_OFF_T_LEN];
-
-    if (retval != NULL && setval == NULL) {
-        if (r->headers_out.content_length == NULL
-            && r->headers_out.content_length_n >= 0)
-        {
-            p = ngx_sprintf(content_len, "%O", r->headers_out.content_length_n);
-
-            start = njs_vm_value_string_alloc(vm, retval, p - content_len);
-            if (start == NULL) {
-                return NJS_ERROR;
-            }
-
-            ngx_memcpy(start, content_len, p - content_len);
-
-            return NJS_OK;
-        }
-    }
-
-    rc = ngx_http_js_header_out_special(vm, r, v, setval, retval, &h);
-    if (rc == NJS_ERROR) {
-        return NJS_ERROR;
-    }
-
-    if (setval != NULL || retval == NULL) {
-        if (h != NULL) {
-            n = ngx_atoi(h->value.data, h->value.len);
-            if (n == NGX_ERROR) {
-                h->hash = 0;
-                njs_vm_error(vm, "failed converting argument "
-                             "to positive integer");
-                return NJS_ERROR;
-            }
-
-            r->headers_out.content_length = h;
-            r->headers_out.content_length_n = n;
-
-        } else {
-            ngx_http_clear_content_length(r);
-        }
-    }
-
-    return NJS_OK;
-}
-
-
-static njs_int_t
-ngx_http_js_content_type(njs_vm_t *vm, ngx_http_request_t *r,
-    unsigned flags, njs_str_t *v, njs_value_t *setval, njs_value_t *retval)
-{
-    int64_t              length;
-    njs_int_t            rc;
-    njs_str_t            s;
-    ngx_str_t           *hdr;
-    njs_opaque_value_t   lvalue;
-
-    if (retval != NULL && setval == NULL) {
-        hdr = &r->headers_out.content_type;
-
-        if (hdr->len == 0) {
-            njs_value_undefined_set(retval);
-            return NJS_OK;
-        }
-
-        return njs_vm_value_string_set(vm, retval, hdr->data, hdr->len);
-    }
-
-    if (setval != NULL && njs_value_is_array(setval)) {
-        rc = njs_vm_array_length(vm, setval, &length);
-        if (rc != NJS_OK) {
-            return NJS_ERROR;
-        }
-
-        setval = njs_vm_array_prop(vm, setval, length - 1, &lvalue);
-    }
-
-    if (ngx_js_string(vm, setval, &s) != NGX_OK) {
-        return NJS_ERROR;
-    }
-
-    r->headers_out.content_type.len = s.length;
-    r->headers_out.content_type_len = r->headers_out.content_type.len;
-    r->headers_out.content_type.data = s.start;
-    r->headers_out.content_type_lowcase = NULL;
-
-    return NJS_OK;
-}
-
-
-static njs_int_t
 ngx_http_js_header_out_special(njs_vm_t *vm, ngx_http_request_t *r,
     njs_str_t *v, njs_value_t *setval, njs_value_t *retval,
     ngx_table_elt_t **hh)
@@ -3998,6 +3788,123 @@ ngx_http_js_header_generic(njs_vm_t *vm,
 #endif
 
 
+static njs_int_t
+ngx_http_js_content_encoding(njs_vm_t *vm, ngx_http_request_t *r,
+    unsigned flags, njs_str_t *v, njs_value_t *setval, njs_value_t *retval)
+{
+    njs_int_t         rc;
+    ngx_table_elt_t  *h;
+
+    rc = ngx_http_js_header_out_special(vm, r, v, setval, retval, &h);
+    if (rc == NJS_ERROR) {
+        return NJS_ERROR;
+    }
+
+    if (setval != NULL || retval == NULL) {
+        r->headers_out.content_encoding = h;
+    }
+
+    return NJS_OK;
+}
+
+
+static njs_int_t
+ngx_http_js_content_length(njs_vm_t *vm, ngx_http_request_t *r,
+    unsigned flags, njs_str_t *v, njs_value_t *setval, njs_value_t *retval)
+{
+    u_char           *p, *start;
+    njs_int_t         rc;
+    ngx_int_t         n;
+    ngx_table_elt_t  *h;
+    u_char            content_len[NGX_OFF_T_LEN];
+
+    if (retval != NULL && setval == NULL) {
+        if (r->headers_out.content_length == NULL
+            && r->headers_out.content_length_n >= 0)
+        {
+            p = ngx_sprintf(content_len, "%O", r->headers_out.content_length_n);
+
+            start = njs_vm_value_string_alloc(vm, retval, p - content_len);
+            if (start == NULL) {
+                return NJS_ERROR;
+            }
+
+            ngx_memcpy(start, content_len, p - content_len);
+
+            return NJS_OK;
+        }
+    }
+
+    rc = ngx_http_js_header_out_special(vm, r, v, setval, retval, &h);
+    if (rc == NJS_ERROR) {
+        return NJS_ERROR;
+    }
+
+    if (setval != NULL || retval == NULL) {
+        if (h != NULL) {
+            n = ngx_atoi(h->value.data, h->value.len);
+            if (n == NGX_ERROR) {
+                h->hash = 0;
+                njs_vm_error(vm, "failed converting argument "
+                             "to positive integer");
+                return NJS_ERROR;
+            }
+
+            r->headers_out.content_length = h;
+            r->headers_out.content_length_n = n;
+
+        } else {
+            ngx_http_clear_content_length(r);
+        }
+    }
+
+    return NJS_OK;
+}
+
+
+static njs_int_t
+ngx_http_js_content_type(njs_vm_t *vm, ngx_http_request_t *r,
+    unsigned flags, njs_str_t *v, njs_value_t *setval, njs_value_t *retval)
+{
+    int64_t              length;
+    njs_int_t            rc;
+    njs_str_t            s;
+    ngx_str_t           *hdr;
+    njs_opaque_value_t   lvalue;
+
+    if (retval != NULL && setval == NULL) {
+        hdr = &r->headers_out.content_type;
+
+        if (hdr->len == 0) {
+            njs_value_undefined_set(retval);
+            return NJS_OK;
+        }
+
+        return njs_vm_value_string_set(vm, retval, hdr->data, hdr->len);
+    }
+
+    if (setval != NULL && njs_value_is_array(setval)) {
+        rc = njs_vm_array_length(vm, setval, &length);
+        if (rc != NJS_OK) {
+            return NJS_ERROR;
+        }
+
+        setval = njs_vm_array_prop(vm, setval, length - 1, &lvalue);
+    }
+
+    if (ngx_js_string(vm, setval, &s) != NGX_OK) {
+        return NJS_ERROR;
+    }
+
+    r->headers_out.content_type.len = s.length;
+    r->headers_out.content_type_len = r->headers_out.content_type.len;
+    r->headers_out.content_type.data = s.start;
+    r->headers_out.content_type_lowcase = NULL;
+
+    return NJS_OK;
+}
+
+
 static njs_host_event_t
 ngx_http_js_set_timer(njs_external_ptr_t external, uint64_t delay,
     njs_vm_event_t vm_event)


More information about the nginx-devel mailing list