From 5f7f6df08b27121a6518da605498122c66ba5791 Mon Sep 17 00:00:00 2001 From: Christian Becker Date: Mon, 10 Oct 2016 16:58:53 +0200 Subject: [PATCH 1/4] feature: expose the 'Last-Modified' response header as ngx.header["Last-Modified"]. --- src/ngx_http_lua_headers.c | 16 ++++++++++++++++ t/016-resp-header.t | 21 +++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/ngx_http_lua_headers.c b/src/ngx_http_lua_headers.c index a376a0eac9..29b9dfe617 100644 --- a/src/ngx_http_lua_headers.c +++ b/src/ngx_http_lua_headers.c @@ -1068,6 +1068,7 @@ ngx_http_lua_ffi_get_resp_header(ngx_http_request_t *r, { int found; u_char c, *p; + time_t last_modified; ngx_uint_t i; ngx_table_elt_t *h; ngx_list_part_t *part; @@ -1134,6 +1135,21 @@ ngx_http_lua_ffi_get_resp_header(ngx_http_request_t *r, break; + case 13: + last_modified = r->headers_out.last_modified_time; + if (last_modified >= 0) { + p = ngx_palloc(r->pool, sizeof("Mon, 28 Sep 1970 06:00:00 GMT")); + if (p == NULL) { + *errmsg = "no memory"; + return NGX_ERROR; + } + + values[0].data = p; + values[0].len = ngx_http_time(p, last_modified) - p; + return 1; + } + break; + default: break; } diff --git a/t/016-resp-header.t b/t/016-resp-header.t index b558415b24..6d0a40320c 100644 --- a/t/016-resp-header.t +++ b/t/016-resp-header.t @@ -2141,3 +2141,24 @@ hi --- error_log my Content-Length: 8589934591 upstream prematurely closed connection while sending to client + + + +=== TEST 95: Expose the 'Last-Modified' response header as ngx.header["Last-Modified"] +--- config + location /a.txt { + header_filter_by_lua_block { + local last_mod = ngx.parse_http_time(ngx.header["Last-Modified"]) + local age = ngx.time() - last_mod + ngx.header["Age"] = age + } + } +--- user_files +>>> a.txt +Foo +--- request +GET /a.txt +--- raw_response_headers_like chomp +Age: \d\r\n +--- no_error_log +[error] From a1facf368e6c8530f3d891e96c384193e2d16397 Mon Sep 17 00:00:00 2001 From: lijunlong Date: Thu, 30 Dec 2021 23:26:00 +0800 Subject: [PATCH 2/4] add a blank line. --- src/ngx_http_lua_headers.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ngx_http_lua_headers.c b/src/ngx_http_lua_headers.c index 29b9dfe617..77014ef239 100644 --- a/src/ngx_http_lua_headers.c +++ b/src/ngx_http_lua_headers.c @@ -1148,6 +1148,7 @@ ngx_http_lua_ffi_get_resp_header(ngx_http_request_t *r, values[0].len = ngx_http_time(p, last_modified) - p; return 1; } + break; default: From 6ceb395ff3dd30a71533be82ab22d3230d6adf59 Mon Sep 17 00:00:00 2001 From: lijunlong Date: Sun, 2 Jan 2022 11:27:02 +0800 Subject: [PATCH 3/4] fixed review comments. --- src/ngx_http_lua_headers.c | 25 ++++++++++++++--------- t/016-resp-header.t | 41 +++++++++++++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 10 deletions(-) diff --git a/src/ngx_http_lua_headers.c b/src/ngx_http_lua_headers.c index 77014ef239..ea12c60e14 100644 --- a/src/ngx_http_lua_headers.c +++ b/src/ngx_http_lua_headers.c @@ -1136,17 +1136,24 @@ ngx_http_lua_ffi_get_resp_header(ngx_http_request_t *r, break; case 13: - last_modified = r->headers_out.last_modified_time; - if (last_modified >= 0) { - p = ngx_palloc(r->pool, sizeof("Mon, 28 Sep 1970 06:00:00 GMT")); - if (p == NULL) { - *errmsg = "no memory"; - return NGX_ERROR; + if (ngx_strncasecmp(key_buf, (u_char *) "Last-Modified", 13) == 0) { + + last_modified = r->headers_out.last_modified_time; + if (last_modified >= 0) { + p = ngx_palloc(r->pool, + sizeof("Mon, 28 Sep 1970 06:00:00 GMT")); + if (p == NULL) { + *errmsg = "no memory"; + return NGX_ERROR; + } + + values[0].data = p; + values[0].len = ngx_http_time(p, last_modified) - p; + + return 1; } - values[0].data = p; - values[0].len = ngx_http_time(p, last_modified) - p; - return 1; + return 0; } break; diff --git a/t/016-resp-header.t b/t/016-resp-header.t index 6d0a40320c..caa6088ddf 100644 --- a/t/016-resp-header.t +++ b/t/016-resp-header.t @@ -2148,7 +2148,15 @@ upstream prematurely closed connection while sending to client --- config location /a.txt { header_filter_by_lua_block { - local last_mod = ngx.parse_http_time(ngx.header["Last-Modified"]) + local last_modified = ngx.header["Last-Modified"] + if last_modified == nil then + ngx.log(ngx.ERR, "can not get lasted modified") + ngx.exit(500) + return + end + + + local last_mod = ngx.parse_http_time(last_modified) local age = ngx.time() - last_mod ngx.header["Age"] = age } @@ -2162,3 +2170,34 @@ GET /a.txt Age: \d\r\n --- no_error_log [error] + + + +=== TEST 96: Expose the 'Last-Modified' response header as ngx.header["Last-Modified"] +--- config + location /test/ { + proxy_pass http://127.0.0.1:$server_port/; + + header_filter_by_lua_block { + local last_modified = ngx.header["Last-Modified"] + if last_modified == nil then + ngx.log(ngx.ERR, "can not get lasted modified") + ngx.exit(500) + return + end + + local last_mod = ngx.parse_http_time(last_modified) + local age = ngx.time() - last_mod + ngx.header["Age"] = age + } + } + +--- user_files +>>> a.txt +Foo +--- request +GET /test/a.txt +--- raw_response_headers_like chomp +Age: \d\r\n +--- no_error_log +[error] From ef77a99cf9126331f8680ce67971c5da1121801f Mon Sep 17 00:00:00 2001 From: lijunlong Date: Mon, 3 Jan 2022 21:08:46 +0800 Subject: [PATCH 4/4] more tests and remove extra blank line. --- src/ngx_http_lua_headers.c | 1 - t/016-resp-header.t | 29 +++++++++++++++++++++++++++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/ngx_http_lua_headers.c b/src/ngx_http_lua_headers.c index ea12c60e14..3ebe7847cb 100644 --- a/src/ngx_http_lua_headers.c +++ b/src/ngx_http_lua_headers.c @@ -1137,7 +1137,6 @@ ngx_http_lua_ffi_get_resp_header(ngx_http_request_t *r, case 13: if (ngx_strncasecmp(key_buf, (u_char *) "Last-Modified", 13) == 0) { - last_modified = r->headers_out.last_modified_time; if (last_modified >= 0) { p = ngx_palloc(r->pool, diff --git a/t/016-resp-header.t b/t/016-resp-header.t index caa6088ddf..65c5d75a8e 100644 --- a/t/016-resp-header.t +++ b/t/016-resp-header.t @@ -2155,7 +2155,6 @@ upstream prematurely closed connection while sending to client return end - local last_mod = ngx.parse_http_time(last_modified) local age = ngx.time() - last_mod ngx.header["Age"] = age @@ -2173,7 +2172,7 @@ Age: \d\r\n -=== TEST 96: Expose the 'Last-Modified' response header as ngx.header["Last-Modified"] +=== TEST 96: 'Last-Modified' from upstream --- config location /test/ { proxy_pass http://127.0.0.1:$server_port/; @@ -2201,3 +2200,29 @@ GET /test/a.txt Age: \d\r\n --- no_error_log [error] + + + +=== TEST 97: 'Last-Modified' does not exist +--- config + location /test { + header_filter_by_lua_block { + local last_modified = ngx.header["Last-Modified"] + if last_modified == nil then + ngx.log(ngx.INFO, "Last-Modified is nil as expected") + return + end + + ngx.log(ngx.ERR, "Last-Modified expected to be nil, but got ", last_modified) + } + + content_by_lua_block { + ngx.say("Hello World") + } + } +--- request +GET /test +--- response_body +Hello World +--- no_error_log +[error]