Skip to content

Commit 55743ae

Browse files
dndxagentzh
authored andcommitted
bugfix: when variable is cacheable and indexed, setting the request "Host" header should clear any existing cached $host variable value so that subsequent reads return the expected result.
Signed-off-by: Yichun Zhang (agentzh) <agentzh@gmail.com>
1 parent 70b843f commit 55743ae

File tree

4 files changed

+40
-2
lines changed

4 files changed

+40
-2
lines changed

src/ngx_http_lua_common.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,8 @@ struct ngx_http_lua_main_conf_s {
227227
ngx_int_t busy_buf_ptr_count;
228228
#endif
229229

230+
ngx_int_t host_var_index;
231+
230232
unsigned requires_header_filter:1;
231233
unsigned requires_body_filter:1;
232234
unsigned requires_capture_filter:1;

src/ngx_http_lua_headers_in.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,10 +432,14 @@ static ngx_int_t
432432
ngx_http_set_host_header(ngx_http_request_t *r, ngx_http_lua_header_val_t *hv,
433433
ngx_str_t *value)
434434
{
435-
ngx_str_t host;
435+
ngx_str_t host;
436+
ngx_http_lua_main_conf_t *lmcf;
437+
ngx_http_variable_value_t *var;
436438

437439
dd("server new value len: %d", (int) value->len);
438440

441+
lmcf = ngx_http_get_module_main_conf(r, ngx_http_lua_module);
442+
439443
if (value->len) {
440444
host= *value;
441445

@@ -449,6 +453,10 @@ ngx_http_set_host_header(ngx_http_request_t *r, ngx_http_lua_header_val_t *hv,
449453
r->headers_in.server = *value;
450454
}
451455

456+
var = &r->variables[lmcf->host_var_index];
457+
var->valid = 0;
458+
var->not_found = 0;
459+
452460
return ngx_http_set_builtin_header(r, hv, value);
453461
}
454462

src/ngx_http_lua_module.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,9 +638,15 @@ ngx_http_lua_init(ngx_conf_t *cf)
638638
#if !defined(NGX_LUA_NO_FFI_API) || nginx_version >= 1011011
639639
ngx_pool_cleanup_t *cln;
640640
#endif
641+
ngx_str_t name = ngx_string("host");
641642

642643
lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_lua_module);
643644

645+
lmcf->host_var_index = ngx_http_get_variable_index(cf, &name);
646+
if (lmcf->host_var_index == NGX_ERROR) {
647+
return NGX_ERROR;
648+
}
649+
644650
if (ngx_http_lua_prev_cycle != ngx_cycle) {
645651
ngx_http_lua_prev_cycle = ngx_cycle;
646652
multi_http_blocks = 0;

t/028-req-header.t

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use Test::Nginx::Socket::Lua;
88

99
repeat_each(2);
1010

11-
plan tests => repeat_each() * (2 * blocks() + 43);
11+
plan tests => repeat_each() * (2 * blocks() + 44);
1212

1313
#no_diff();
1414
no_long_string();
@@ -2008,3 +2008,25 @@ found 3 headers.
20082008
--- no_error_log
20092009
lua exceeding request header limit
20102010
[error]
2011+
2012+
2013+
2014+
=== TEST 61: setting Host header clears cached $host variable
2015+
--- config
2016+
location /req-header {
2017+
# this makes $host indexed and cacheable
2018+
set $foo $host;
2019+
2020+
content_by_lua_block {
2021+
ngx.say(ngx.var.host)
2022+
ngx.req.set_header("Host", "new");
2023+
ngx.say(ngx.var.host)
2024+
}
2025+
}
2026+
--- request
2027+
GET /req-header
2028+
--- response_body
2029+
localhost
2030+
new
2031+
--- no_error_log
2032+
[error]

0 commit comments

Comments
 (0)