Skip to content

Commit 0c755ed

Browse files
committed
bugfix: ensured set_by_lua_file with nginx variables re-computes closures code cache key.
1 parent 39c2ead commit 0c755ed

File tree

2 files changed

+60
-14
lines changed

2 files changed

+60
-14
lines changed

src/ngx_http_lua_directive.c

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,9 @@ ngx_http_lua_set_by_lua_file(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
301301
ngx_str_t target;
302302
ndk_set_var_t filter;
303303

304-
ngx_http_lua_set_var_data_t *filter_data;
304+
ngx_http_lua_set_var_data_t *filter_data;
305+
ngx_http_complex_value_t cv;
306+
ngx_http_compile_complex_value_t ccv;
305307

306308
/*
307309
* value[0] = "set_by_lua_file"
@@ -324,16 +326,28 @@ ngx_http_lua_set_by_lua_file(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
324326

325327
filter_data->size = filter.size;
326328

327-
p = ngx_palloc(cf->pool, NGX_HTTP_LUA_FILE_KEY_LEN + 1);
328-
if (p == NULL) {
329+
ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
330+
ccv.cf = cf;
331+
ccv.value = &value[2];
332+
ccv.complex_value = &cv;
333+
334+
if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
329335
return NGX_CONF_ERROR;
330336
}
331337

332-
filter_data->key = p;
338+
if (cv.lengths == NULL) {
339+
/* no variable found */
340+
p = ngx_palloc(cf->pool, NGX_HTTP_LUA_FILE_KEY_LEN + 1);
341+
if (p == NULL) {
342+
return NGX_CONF_ERROR;
343+
}
333344

334-
p = ngx_copy(p, NGX_HTTP_LUA_FILE_TAG, NGX_HTTP_LUA_FILE_TAG_LEN);
335-
p = ngx_http_lua_digest_hex(p, value[2].data, value[2].len);
336-
*p = '\0';
345+
filter_data->key = p;
346+
347+
p = ngx_copy(p, NGX_HTTP_LUA_FILE_TAG, NGX_HTTP_LUA_FILE_TAG_LEN);
348+
p = ngx_http_lua_digest_hex(p, value[2].data, value[2].len);
349+
*p = '\0';
350+
}
337351

338352
ngx_str_null(&filter_data->script);
339353

t/025-codecache.t

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use Test::Nginx::Socket::Lua;
44

55
repeat_each(2);
66

7-
plan tests => repeat_each() * 181;
7+
plan tests => repeat_each() * 184;
88

99
#$ENV{LUA_PATH} = $ENV{HOME} . '/work/JSON4Lua-0.9.30/json/?.lua';
1010

@@ -1403,7 +1403,39 @@ code cache hit (key='nhlf_042c9b3a136fbacbbd0e4b9ad10896b7', ref=3)
14031403
14041404
14051405
1406-
=== TEST 34: variables in rewrite_by_lua_file's file path
1406+
=== TEST 34: variables in set_by_lua_file's file path
1407+
--- config
1408+
location ~ ^/lua/(.+)$ {
1409+
set_by_lua_file $res html/$1.lua;
1410+
echo $res;
1411+
}
1412+
1413+
location /main {
1414+
echo_location /lua/a;
1415+
echo_location /lua/b;
1416+
echo_location /lua/a;
1417+
echo_location /lua/a;
1418+
echo_location /lua/b;
1419+
}
1420+
--- user_files
1421+
>>> a.lua
1422+
return "a"
1423+
>>> b.lua
1424+
return "b"
1425+
--- request
1426+
GET /main
1427+
--- response_body
1428+
a
1429+
b
1430+
a
1431+
a
1432+
b
1433+
--- no_error_log
1434+
[error]
1435+
1436+
1437+
1438+
=== TEST 35: variables in rewrite_by_lua_file's file path
14071439
--- config
14081440
location ~ ^/lua/(.+)$ {
14091441
rewrite_by_lua_file html/$1.lua;
@@ -1434,7 +1466,7 @@ b
14341466
14351467
14361468
1437-
=== TEST 35: variables in access_by_lua_file's file path
1469+
=== TEST 36: variables in access_by_lua_file's file path
14381470
--- config
14391471
location ~ ^/lua/(.+)$ {
14401472
access_by_lua_file html/$1.lua;
@@ -1483,7 +1515,7 @@ b
14831515
14841516
14851517
1486-
=== TEST 36: variables in content_by_lua_file's file path
1518+
=== TEST 37: variables in content_by_lua_file's file path
14871519
--- config
14881520
location ~ ^/lua/(.+)$ {
14891521
content_by_lua_file html/$1.lua;
@@ -1514,7 +1546,7 @@ b
15141546
15151547
15161548
1517-
=== TEST 37: variables in header_filter_by_lua_file's file path
1549+
=== TEST 38: variables in header_filter_by_lua_file's file path
15181550
--- config
15191551
location ~ ^/lua/(.+)$ {
15201552
return 200;
@@ -1561,7 +1593,7 @@ b
15611593
15621594
15631595
1564-
=== TEST 38: variables in body_filter_by_lua_file's file path
1596+
=== TEST 39: variables in body_filter_by_lua_file's file path
15651597
--- config
15661598
location ~ ^/lua/(.+)$ {
15671599
echo hello;
@@ -1596,7 +1628,7 @@ b
15961628
15971629
15981630
1599-
=== TEST 39: variables in log_by_lua_file's file path
1631+
=== TEST 40: variables in log_by_lua_file's file path
16001632
--- config
16011633
log_subrequest on;
16021634

0 commit comments

Comments
 (0)