Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/ngx_http_lua_headers.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1134,6 +1135,28 @@ ngx_http_lua_ffi_get_resp_header(ngx_http_request_t *r,

break;

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,
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;
}

return 0;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there should be a blank line before the break;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we can return 0 here when the last_modified < 0? just for better performance.


break;

default:
break;
}
Expand Down
85 changes: 85 additions & 0 deletions t/016-resp-header.t
Original file line number Diff line number Diff line change
Expand Up @@ -2141,3 +2141,88 @@ 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_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 /a.txt
--- raw_response_headers_like chomp
Age: \d\r\n
--- no_error_log
[error]



=== TEST 96: 'Last-Modified' from upstream
--- 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]



=== 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]