[njs] HTTP: fixed setting of Last-Modified header.

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


details:   https://hg.nginx.org/njs/rev/3af6c729b7cb
branches:  
changeset: 2181:3af6c729b7cb
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Tue Jul 11 22:50:44 2023 -0700
description:
HTTP: fixed setting of Last-Modified header.

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

diffstat:

 nginx/ngx_http_js_module.c |  38 ++++++++++++++++++++++++++++++++++++--
 nginx/t/js_headers.t       |  18 +++++++++++++++---
 2 files changed, 51 insertions(+), 5 deletions(-)

diffs (136 lines):

diff -r a35035c52201 -r 3af6c729b7cb nginx/ngx_http_js_module.c
--- a/nginx/ngx_http_js_module.c	Tue Jul 11 19:12:34 2023 -0700
+++ b/nginx/ngx_http_js_module.c	Tue Jul 11 22:50:44 2023 -0700
@@ -128,6 +128,9 @@ static njs_int_t ngx_http_js_content_typ
 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_last_modified122(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);
@@ -228,6 +231,9 @@ static njs_int_t ngx_http_js_content_typ
 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_last_modified(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);
@@ -1535,7 +1541,7 @@ ngx_http_js_ext_header_out(njs_vm_t *vm,
         { 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 },
+        { njs_str("Last-Modified"), ngx_http_js_last_modified122 },
         { njs_str("Location"), ngx_http_js_location122 },
         { njs_str("Set-Cookie"), ngx_http_js_header_array },
         { njs_str("Retry-After"), ngx_http_js_header_single },
@@ -1548,7 +1554,7 @@ ngx_http_js_ext_header_out(njs_vm_t *vm,
         { 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 },
+        { njs_str("Last-Modified"), 0, ngx_http_js_last_modified },
         { njs_str("Location"), 0, ngx_http_js_location },
         { njs_str("Set-Cookie"), NJS_HEADER_ARRAY, ngx_http_js_header_out },
         { njs_str("Retry-After"), NJS_HEADER_SINGLE, ngx_http_js_header_out },
@@ -1971,6 +1977,14 @@ ngx_http_js_date122(njs_vm_t *vm, ngx_ht
 
 
 static njs_int_t
+ngx_http_js_last_modified122(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_last_modified(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)
 {
@@ -3997,6 +4011,26 @@ ngx_http_js_date(njs_vm_t *vm, ngx_http_
 
 
 static njs_int_t
+ngx_http_js_last_modified(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.last_modified = 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 a35035c52201 -r 3af6c729b7cb nginx/t/js_headers.t
--- a/nginx/t/js_headers.t	Tue Jul 11 19:12:34 2023 -0700
+++ b/nginx/t/js_headers.t	Tue Jul 11 22:50:44 2023 -0700
@@ -88,6 +88,10 @@ http {
             js_content test.date;
         }
 
+        location /last_modified {
+            js_content test.last_modified;
+        }
+
         location /location {
             js_content test.location;
         }
@@ -254,6 +258,11 @@ EOF
         r.return(200);
     }
 
+    function last_modified(r) {
+        r.headersOut['Last-Modified'] = 'Sun, 09 Sep 2001 01:46:40 GMT';
+        r.return(200);
+    }
+
     function location(r) {
         if (njs.version_number >= 0x000705) {
             var lc = r.headersOut['Location'];
@@ -446,13 +455,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, date, location,
-                    location_sr};
+                    copy_subrequest_hdrs, subrequest, date, last_modified,
+                    location, location_sr};
 
 
 EOF
 
-$t->try_run('no njs')->plan(45);
+$t->try_run('no njs')->plan(46);
 
 ###############################################################################
 
@@ -605,6 +614,9 @@ local $TODO = 'not yet' unless has_versi
 
 like(http_get('/date'), qr/Date: Sun, 09 Sep 2001 01:46:40 GMT/,
 	'set date');
+like(http_get('/last_modified'),
+	qr/Last-Modified: Sun, 09 Sep 2001 01:46:40 GMT/,
+	'set Last-Modified');
 
 }
 


More information about the nginx-devel mailing list