6
#include "ngx_http_lua_ctx.h"
10
ngx_http_lua_ngx_get_ctx(lua_State *L)
12
ngx_http_request_t *r;
13
ngx_http_lua_ctx_t *ctx;
15
lua_getglobal(L, GLOBALS_SYMBOL_REQUEST);
16
r = lua_touserdata(L, -1);
20
return luaL_error(L, "no request object found");
23
ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
25
return luaL_error(L, "no request ctx found");
28
if (ctx->ctx_ref == LUA_NOREF) {
29
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
30
"lua create ngx.ctx table for the current request");
32
lua_getfield(L, LUA_REGISTRYINDEX, NGX_LUA_REQ_CTX_REF);
35
ctx->ctx_ref = luaL_ref(L, -3);
39
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
40
"lua fetching existing ngx.ctx table for the current request");
42
lua_getfield(L, LUA_REGISTRYINDEX, NGX_LUA_REQ_CTX_REF);
43
lua_rawgeti(L, -1, ctx->ctx_ref);
51
ngx_http_lua_ngx_set_ctx(lua_State *L)
53
ngx_http_request_t *r;
54
ngx_http_lua_ctx_t *ctx;
56
lua_getglobal(L, GLOBALS_SYMBOL_REQUEST);
57
r = lua_touserdata(L, -1);
61
return luaL_error(L, "no request object found");
64
ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
66
return luaL_error(L, "no request ctx found");
69
if (ctx->ctx_ref == LUA_NOREF) {
70
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
71
"lua create ngx.ctx table for the current request");
73
lua_getfield(L, LUA_REGISTRYINDEX, NGX_LUA_REQ_CTX_REF);
75
ctx->ctx_ref = luaL_ref(L, -2);
79
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
80
"lua fetching existing ngx.ctx table for the current request");
82
lua_getfield(L, LUA_REGISTRYINDEX, NGX_LUA_REQ_CTX_REF);
83
luaL_unref(L, -1, ctx->ctx_ref);
85
ctx->ctx_ref = luaL_ref(L, -2);