[njs] HTTP: fixed setting of Date header.

Dmitry Volyntsev xeioex at nginx.com
Wed Jul 12 05:51:55 UTC 2023


details:   https://hg.nginx.org/njs/rev/a35035c52201
branches:  
changeset: 2180:a35035c52201
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Tue Jul 11 19:12:34 2023 -0700
description:
HTTP: fixed setting of Date header.

Previously, r.headersOut['Date'] setter did not update
r->headers_out.date. As a result a client might get two
Date headers.

diffstat:

 nginx/ngx_http_js_module.c |  36 ++++++++++++++++++++++++++++++++++++
 nginx/t/js_headers.t       |  22 ++++++++++++++++++++--
 2 files changed, 56 insertions(+), 2 deletions(-)

diffs (138 lines):

diff -r ac56314cc3be -r a35035c52201 nginx/ngx_http_js_module.c
--- a/nginx/ngx_http_js_module.c	Mon Jul 10 17:57:45 2023 -0700
+++ b/nginx/ngx_http_js_module.c	Tue Jul 11 19:12:34 2023 -0700
@@ -125,6 +125,9 @@ static njs_int_t ngx_http_js_content_len
 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);
+static njs_int_t ngx_http_js_date122(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_location122(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);
@@ -222,6 +225,9 @@ 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_date(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_location(njs_vm_t *vm, ngx_http_request_t *r,
     unsigned flags, njs_str_t *name, njs_value_t *setval,
     njs_value_t *retval);
@@ -1526,6 +1532,7 @@ ngx_http_js_ext_header_out(njs_vm_t *vm,
         { 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("Date"), ngx_http_js_date122 },
         { 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 },
@@ -1538,6 +1545,7 @@ ngx_http_js_ext_header_out(njs_vm_t *vm,
         { njs_str("Content-Encoding"), 0, ngx_http_js_content_encoding },
         { njs_str("Content-Length"), 0, ngx_http_js_content_length },
         { njs_str("Content-Type"), 0, ngx_http_js_content_type },
+        { njs_str("Date"), 0, ngx_http_js_date },
         { njs_str("Etag"), NJS_HEADER_SINGLE, ngx_http_js_header_out },
         { njs_str("Expires"), NJS_HEADER_SINGLE, ngx_http_js_header_out },
         { njs_str("Last-Modified"), NJS_HEADER_SINGLE, ngx_http_js_header_out },
@@ -1955,6 +1963,14 @@ ngx_http_js_content_type122(njs_vm_t *vm
 
 
 static njs_int_t
+ngx_http_js_date122(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)
+{
+    return ngx_http_js_date(vm, r, 0, v, setval, retval);
+}
+
+
+static njs_int_t
 ngx_http_js_location122(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)
 {
@@ -3961,6 +3977,26 @@ ngx_http_js_content_type(njs_vm_t *vm, n
 
 
 static njs_int_t
+ngx_http_js_date(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.date = h;
+    }
+
+    return NJS_OK;
+}
+
+
+static njs_int_t
 ngx_http_js_location(njs_vm_t *vm, ngx_http_request_t *r, unsigned flags,
     njs_str_t *v, njs_value_t *setval, njs_value_t *retval)
 {
diff -r ac56314cc3be -r a35035c52201 nginx/t/js_headers.t
--- a/nginx/t/js_headers.t	Mon Jul 10 17:57:45 2023 -0700
+++ b/nginx/t/js_headers.t	Tue Jul 11 19:12:34 2023 -0700
@@ -84,6 +84,10 @@ http {
             js_content test.content_encoding_arr;
         }
 
+        location /date {
+            js_content test.date;
+        }
+
         location /location {
             js_content test.location;
         }
@@ -245,6 +249,11 @@ EOF
         r.return(200);
     }
 
+    function date(r) {
+        r.headersOut['Date'] = 'Sun, 09 Sep 2001 01:46:40 GMT';
+        r.return(200);
+    }
+
     function location(r) {
         if (njs.version_number >= 0x000705) {
             var lc = r.headersOut['Location'];
@@ -437,12 +446,13 @@ EOF
                     hdr_in, raw_hdr_in, hdr_sorted_keys, foo_in, ifoo_in,
                     hdr_out, raw_hdr_out, hdr_out_array, hdr_out_single,
                     hdr_out_set_cookie, ihdr_out, hdr_out_special_set,
-                    copy_subrequest_hdrs, subrequest, location, location_sr};
+                    copy_subrequest_hdrs, subrequest, date, location,
+                    location_sr};
 
 
 EOF
 
-$t->try_run('no njs')->plan(44);
+$t->try_run('no njs')->plan(45);
 
 ###############################################################################
 
@@ -590,6 +600,14 @@ unlike(http_get('/location_sr'), qr/Loca
 
 }
 
+TODO: {
+local $TODO = 'not yet' unless has_version('0.8.1');
+
+like(http_get('/date'), qr/Date: Sun, 09 Sep 2001 01:46:40 GMT/,
+	'set date');
+
+}
+
 ###############################################################################
 
 sub has_version {


More information about the nginx-devel mailing list