Skip to content

Commit f36fe29

Browse files
feature: expose the 'Last-Modified' response header as ngx.header["Last-Modified"]. (openresty#1983)
Co-authored-by: Christian Becker <c.becker@mes-info.de>
1 parent 75c68b2 commit f36fe29

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

src/ngx_http_lua_headers.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,6 +1068,7 @@ ngx_http_lua_ffi_get_resp_header(ngx_http_request_t *r,
10681068
{
10691069
int found;
10701070
u_char c, *p;
1071+
time_t last_modified;
10711072
ngx_uint_t i;
10721073
ngx_table_elt_t *h;
10731074
ngx_list_part_t *part;
@@ -1134,6 +1135,28 @@ ngx_http_lua_ffi_get_resp_header(ngx_http_request_t *r,
11341135

11351136
break;
11361137

1138+
case 13:
1139+
if (ngx_strncasecmp(key_buf, (u_char *) "Last-Modified", 13) == 0) {
1140+
last_modified = r->headers_out.last_modified_time;
1141+
if (last_modified >= 0) {
1142+
p = ngx_palloc(r->pool,
1143+
sizeof("Mon, 28 Sep 1970 06:00:00 GMT"));
1144+
if (p == NULL) {
1145+
*errmsg = "no memory";
1146+
return NGX_ERROR;
1147+
}
1148+
1149+
values[0].data = p;
1150+
values[0].len = ngx_http_time(p, last_modified) - p;
1151+
1152+
return 1;
1153+
}
1154+
1155+
return 0;
1156+
}
1157+
1158+
break;
1159+
11371160
default:
11381161
break;
11391162
}

t/016-resp-header.t

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2141,3 +2141,88 @@ hi
21412141
--- error_log
21422142
my Content-Length: 8589934591
21432143
upstream prematurely closed connection while sending to client
2144+
2145+
2146+
2147+
=== TEST 95: Expose the 'Last-Modified' response header as ngx.header["Last-Modified"]
2148+
--- config
2149+
location /a.txt {
2150+
header_filter_by_lua_block {
2151+
local last_modified = ngx.header["Last-Modified"]
2152+
if last_modified == nil then
2153+
ngx.log(ngx.ERR, "can not get lasted modified")
2154+
ngx.exit(500)
2155+
return
2156+
end
2157+
2158+
local last_mod = ngx.parse_http_time(last_modified)
2159+
local age = ngx.time() - last_mod
2160+
ngx.header["Age"] = age
2161+
}
2162+
}
2163+
--- user_files
2164+
>>> a.txt
2165+
Foo
2166+
--- request
2167+
GET /a.txt
2168+
--- raw_response_headers_like chomp
2169+
Age: \d\r\n
2170+
--- no_error_log
2171+
[error]
2172+
2173+
2174+
2175+
=== TEST 96: 'Last-Modified' from upstream
2176+
--- config
2177+
location /test/ {
2178+
proxy_pass http://127.0.0.1:$server_port/;
2179+
2180+
header_filter_by_lua_block {
2181+
local last_modified = ngx.header["Last-Modified"]
2182+
if last_modified == nil then
2183+
ngx.log(ngx.ERR, "can not get lasted modified")
2184+
ngx.exit(500)
2185+
return
2186+
end
2187+
2188+
local last_mod = ngx.parse_http_time(last_modified)
2189+
local age = ngx.time() - last_mod
2190+
ngx.header["Age"] = age
2191+
}
2192+
}
2193+
2194+
--- user_files
2195+
>>> a.txt
2196+
Foo
2197+
--- request
2198+
GET /test/a.txt
2199+
--- raw_response_headers_like chomp
2200+
Age: \d\r\n
2201+
--- no_error_log
2202+
[error]
2203+
2204+
2205+
2206+
=== TEST 97: 'Last-Modified' does not exist
2207+
--- config
2208+
location /test {
2209+
header_filter_by_lua_block {
2210+
local last_modified = ngx.header["Last-Modified"]
2211+
if last_modified == nil then
2212+
ngx.log(ngx.INFO, "Last-Modified is nil as expected")
2213+
return
2214+
end
2215+
2216+
ngx.log(ngx.ERR, "Last-Modified expected to be nil, but got ", last_modified)
2217+
}
2218+
2219+
content_by_lua_block {
2220+
ngx.say("Hello World")
2221+
}
2222+
}
2223+
--- request
2224+
GET /test
2225+
--- response_body
2226+
Hello World
2227+
--- no_error_log
2228+
[error]

0 commit comments

Comments
 (0)