diff --git a/src/ngx_http_lua_headerfilterby.c b/src/ngx_http_lua_headerfilterby.c index 63d629db..4e18c7cb 100644 --- a/src/ngx_http_lua_headerfilterby.c +++ b/src/ngx_http_lua_headerfilterby.c @@ -19,6 +19,7 @@ #include "ngx_http_lua_string.h" #include "ngx_http_lua_misc.h" #include "ngx_http_lua_consts.h" +#include "ngx_http_lua_shdict.h" static ngx_http_output_header_filter_pt ngx_http_next_header_filter; @@ -38,6 +39,10 @@ static ngx_http_output_header_filter_pt ngx_http_next_header_filter; static void ngx_http_lua_header_filter_by_lua_env(lua_State *L, ngx_http_request_t *r) { + ngx_http_lua_main_conf_t *lmcf; + + lmcf = ngx_http_get_module_main_conf(r, ngx_http_lua_module); + /* set nginx request pointer to current lua thread's globals table */ lua_pushlightuserdata(L, r); lua_setglobal(L, GLOBALS_SYMBOL_REQUEST); @@ -74,6 +79,7 @@ ngx_http_lua_header_filter_by_lua_env(lua_State *L, ngx_http_request_t *r) ngx_http_lua_inject_req_api_no_io(L); ngx_http_lua_inject_resp_header_api(L); ngx_http_lua_inject_variable_api(L); + ngx_http_lua_inject_shdict_api(lmcf, L); ngx_http_lua_inject_misc_api(L); lua_setfield(L, -2, "ngx"); diff --git a/src/ngx_http_lua_setby.c b/src/ngx_http_lua_setby.c index e3cf75aa..778710fd 100644 --- a/src/ngx_http_lua_setby.c +++ b/src/ngx_http_lua_setby.c @@ -17,6 +17,7 @@ #include "ngx_http_lua_string.h" #include "ngx_http_lua_misc.h" #include "ngx_http_lua_consts.h" +#include "ngx_http_lua_shdict.h" static void ngx_http_lua_inject_arg_api(lua_State *L, @@ -164,6 +165,10 @@ static void ngx_http_lua_set_by_lua_env(lua_State *L, ngx_http_request_t *r, size_t nargs, ngx_http_variable_value_t *args) { + ngx_http_lua_main_conf_t *lmcf; + + lmcf = ngx_http_get_module_main_conf(r, ngx_http_lua_module); + /* set nginx request pointer to current lua thread's globals table */ lua_pushlightuserdata(L, r); lua_setglobal(L, GLOBALS_SYMBOL_REQUEST); @@ -201,6 +206,7 @@ ngx_http_lua_set_by_lua_env(lua_State *L, ngx_http_request_t *r, size_t nargs, #if (NGX_PCRE) ngx_http_lua_inject_regex_api(L); #endif + ngx_http_lua_inject_shdict_api(lmcf, L); ngx_http_lua_inject_misc_api(L); lua_setfield(L, -2, "ngx"); diff --git a/src/ngx_http_lua_shdict.c b/src/ngx_http_lua_shdict.c index a5d2104e..4eb638eb 100644 --- a/src/ngx_http_lua_shdict.c +++ b/src/ngx_http_lua_shdict.c @@ -238,14 +238,12 @@ ngx_http_lua_shdict_expire(ngx_http_lua_shdict_ctx_t *ctx, ngx_uint_t n) void -ngx_http_lua_inject_shdict_api(ngx_conf_t *cf, lua_State *L) +ngx_http_lua_inject_shdict_api(ngx_http_lua_main_conf_t *lmcf, lua_State *L) { - ngx_http_lua_main_conf_t *lmcf; ngx_http_lua_shdict_ctx_t *ctx; ngx_uint_t i; ngx_shm_zone_t **zone; - lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_lua_module); if (lmcf->shm_zones != NULL) { lua_createtable(L, 0, 1 /* nrec */); /* ngx.shared */ diff --git a/src/ngx_http_lua_shdict.h b/src/ngx_http_lua_shdict.h index aa2c7a47..43d8197f 100644 --- a/src/ngx_http_lua_shdict.h +++ b/src/ngx_http_lua_shdict.h @@ -36,7 +36,8 @@ ngx_int_t ngx_http_lua_shdict_init_zone(ngx_shm_zone_t *shm_zone, void *data); void ngx_http_lua_shdict_rbtree_insert_value(ngx_rbtree_node_t *temp, ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel); -void ngx_http_lua_inject_shdict_api(ngx_conf_t *cf, lua_State *L); +void ngx_http_lua_inject_shdict_api(ngx_http_lua_main_conf_t *lmcf, + lua_State *L); #endif /* NGX_HTTP_LUA_SHDICT_H */ diff --git a/src/ngx_http_lua_util.c b/src/ngx_http_lua_util.c index ee350997..922db285 100644 --- a/src/ngx_http_lua_util.c +++ b/src/ngx_http_lua_util.c @@ -500,6 +500,10 @@ init_ngx_lua_registry(ngx_conf_t *cf, lua_State *L) static void init_ngx_lua_globals(ngx_conf_t *cf, lua_State *L) { + ngx_http_lua_main_conf_t *lmcf; + + lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_lua_module); + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, cf->log, 0, "lua initializing lua globals"); @@ -529,7 +533,7 @@ init_ngx_lua_globals(ngx_conf_t *cf, lua_State *L) ngx_http_lua_inject_req_api(L); ngx_http_lua_inject_resp_header_api(L); ngx_http_lua_inject_variable_api(L); - ngx_http_lua_inject_shdict_api(cf, L); + ngx_http_lua_inject_shdict_api(lmcf, L); ngx_http_lua_inject_misc_api(L); lua_getglobal(L, "package"); /* ngx package */ diff --git a/t/043-shdict.t b/t/043-shdict.t index 090104c3..2af3ce95 100644 --- a/t/043-shdict.t +++ b/t/043-shdict.t @@ -8,7 +8,7 @@ use Test::Nginx::Socket; #repeat_each(2); -plan tests => repeat_each() * (blocks() * 2 + 4); +plan tests => repeat_each() * (blocks() * 2 + 5); #no_diff(); no_long_string(); @@ -462,3 +462,91 @@ GET /test --- response_body_like ^abort at (?:139|142)$ + + +=== TEST 19: string key, int value (write_by_lua) +--- http_config + lua_shared_dict dogs 1m; +--- config + location = /test { + rewrite_by_lua ' + local dogs = ngx.shared.dogs + dogs:set("foo", 32) + dogs:set("bah", 10502) + local val = dogs:get("foo") + ngx.say(val, " ", type(val)) + val = dogs:get("bah") + ngx.say(val, " ", type(val)) + '; + content_by_lua return; + } +--- request +GET /test +--- response_body +32 number +10502 number + + + +=== TEST 20: string key, int value (access_by_lua) +--- http_config + lua_shared_dict dogs 1m; +--- config + location = /test { + access_by_lua ' + local dogs = ngx.shared.dogs + dogs:set("foo", 32) + dogs:set("bah", 10502) + local val = dogs:get("foo") + ngx.say(val, " ", type(val)) + val = dogs:get("bah") + ngx.say(val, " ", type(val)) + '; + content_by_lua return; + } +--- request +GET /test +--- response_body +32 number +10502 number + + + +=== TEST 21: string key, int value (set_by_lua) +--- http_config + lua_shared_dict dogs 1m; +--- config + location = /test { + set_by_lua $res ' + local dogs = ngx.shared.dogs + dogs:set("foo", 32) + return dogs:get("foo") + '; + echo $res; + } +--- request +GET /test +--- response_body +32 + + + +=== TEST 21: string key, int value (header_by_lua) +--- http_config + lua_shared_dict dogs 1m; +--- config + location = /test { + echo hello; + header_filter_by_lua ' + local dogs = ngx.shared.dogs + dogs:set("foo", 32) + ngx.header["X-Foo"] = dogs:get("foo") + '; + } +--- request +GET /test +--- response_headers +X-Foo: 32 +--- response_body +hello +