Created
September 6, 2018 21:50
-
-
Save bjne/e061f61b0be8e54bdfc50f31e6fe3434 to your computer and use it in GitHub Desktop.
ngx lua ctx passing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/build/ngx_lua-0.10.13/src/ngx_http_lua_common.h b/build/ngx_lua-0.10.13/src/ngx_http_lua_common.h | |
index 01ef2be..048e1bf 100644 | |
--- a/build/ngx_lua-0.10.13/src/ngx_http_lua_common.h | |
+++ b/build/ngx_lua-0.10.13/src/ngx_http_lua_common.h | |
@@ -227,6 +227,8 @@ struct ngx_http_lua_main_conf_s { | |
ngx_int_t busy_buf_ptr_count; | |
#endif | |
+ ngx_int_t ctx_idx; | |
+ | |
unsigned requires_header_filter:1; | |
unsigned requires_body_filter:1; | |
unsigned requires_capture_filter:1; | |
diff --git a/build/ngx_lua-0.10.13/src/ngx_http_lua_ctx.c b/build/ngx_lua-0.10.13/src/ngx_http_lua_ctx.c | |
index a14bb4d..f5f729f 100644 | |
--- a/build/ngx_lua-0.10.13/src/ngx_http_lua_ctx.c | |
+++ b/build/ngx_lua-0.10.13/src/ngx_http_lua_ctx.c | |
@@ -24,6 +24,19 @@ static ngx_int_t ngx_http_lua_ngx_ctx_add_cleanup(ngx_http_request_t *r, | |
int ref); | |
static void ngx_http_lua_ngx_ctx_cleanup(void *data); | |
+int | |
+ngx_http_lua_ngx_get_ctx_idx(ngx_http_request_t *r) | |
+{ | |
+ ngx_variable_value_t *vv; | |
+ ngx_http_lua_main_conf_t *lmcf; | |
+ | |
+ lmcf = ngx_http_get_module_main_conf(r, ngx_http_lua_module); | |
+ | |
+ vv = &r->variables[lmcf->ctx_idx]; | |
+ | |
+ return vv->len == 0 ? LUA_NOREF : ngx_atoi(vv->data, vv->len); | |
+} | |
+ | |
int | |
ngx_http_lua_ngx_get_ctx(lua_State *L) | |
@@ -41,6 +54,13 @@ ngx_http_lua_ngx_get_ctx(lua_State *L) | |
return luaL_error(L, "no request ctx found"); | |
} | |
+ if (ctx->ctx_ref == LUA_NOREF) { | |
+ ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
+ "lua fetch ngx.ctx table for the current request"); | |
+ | |
+ ctx->ctx_ref = ngx_http_lua_ngx_get_ctx_idx(r); | |
+ } | |
+ | |
if (ctx->ctx_ref == LUA_NOREF) { | |
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
"lua create ngx.ctx table for the current request"); | |
@@ -141,7 +161,8 @@ ngx_http_lua_ffi_get_ctx_ref(ngx_http_request_t *r) | |
return NGX_HTTP_LUA_FFI_NO_REQ_CTX; | |
} | |
- return ctx->ctx_ref; | |
+ return ctx->ctx_ref != LUA_NOREF ? ctx->ctx_ref : | |
+ ngx_http_lua_ngx_get_ctx_idx(r); | |
} | |
diff --git a/build/ngx_lua-0.10.13/src/ngx_http_lua_module.c b/build/ngx_lua-0.10.13/src/ngx_http_lua_module.c | |
index ae8bc0e..a4b4c11 100644 | |
--- a/build/ngx_lua-0.10.13/src/ngx_http_lua_module.c | |
+++ b/build/ngx_lua-0.10.13/src/ngx_http_lua_module.c | |
@@ -56,6 +56,9 @@ static ngx_conf_post_t ngx_http_lua_lowat_post = | |
static volatile ngx_cycle_t *ngx_http_lua_prev_cycle = NULL; | |
+static ngx_str_t ngx_http_lua_ctx_ref_key = ngx_string("$_ctx_ref_$"); | |
+ | |
+ | |
#if (NGX_HTTP_SSL) && defined(nginx_version) && nginx_version >= 1001013 | |
@@ -802,6 +805,25 @@ ngx_http_lua_lowat_check(ngx_conf_t *cf, void *post, void *data) | |
} | |
+static int | |
+ngx_http_lua_variable_init(ngx_conf_t *cf, ngx_http_lua_main_conf_t *lmcf) | |
+{ | |
+ ngx_http_variable_t *v; | |
+ ngx_http_core_main_conf_t *cmcf; | |
+ | |
+ lmcf->ctx_idx = ngx_http_get_variable_index(cf, &ngx_http_lua_ctx_ref_key); | |
+ | |
+ cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module); | |
+ v = cmcf->variables.elts; | |
+ v[lmcf->ctx_idx].get_handler = 1; | |
+ | |
+ if (lmcf->ctx_idx == NGX_ERROR) | |
+ return NGX_ERROR; | |
+ | |
+ return NGX_OK; | |
+} | |
+ | |
+ | |
static void * | |
ngx_http_lua_create_main_conf(ngx_conf_t *cf) | |
{ | |
@@ -898,6 +920,9 @@ ngx_http_lua_init_main_conf(ngx_conf_t *cf, void *conf) | |
lmcf->cycle = cf->cycle; | |
+ if (ngx_http_lua_variable_init(cf, lmcf) != NGX_OK) | |
+ return NULL; | |
+ | |
return NGX_CONF_OK; | |
} | |
diff --git a/build/ngx_lua-0.10.13/src/ngx_http_lua_util.c b/build/ngx_lua-0.10.13/src/ngx_http_lua_util.c | |
index f7a537e..bfdc1d8 100644 | |
--- a/build/ngx_lua-0.10.13/src/ngx_http_lua_util.c | |
+++ b/build/ngx_lua-0.10.13/src/ngx_http_lua_util.c | |
@@ -2128,7 +2128,9 @@ static ngx_int_t | |
ngx_http_lua_handle_exec(lua_State *L, ngx_http_request_t *r, | |
ngx_http_lua_ctx_t *ctx) | |
{ | |
- ngx_int_t rc; | |
+ ngx_int_t rc; | |
+ ngx_variable_value_t *vv; | |
+ ngx_http_lua_main_conf_t *lmcf; | |
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
"lua thread initiated internal redirect to %V", | |
@@ -2146,6 +2148,13 @@ ngx_http_lua_handle_exec(lua_State *L, ngx_http_request_t *r, | |
ngx_http_lua_request_cleanup(ctx, 1 /* forcible */); | |
+ if (ctx->ctx_ref != LUA_NOREF) { | |
+ lmcf = ngx_http_get_module_main_conf(r, ngx_http_lua_module); | |
+ vv = &r->variables[lmcf->ctx_idx]; | |
+ vv->data = ngx_pnalloc(r->pool, NGX_INT32_LEN); | |
+ vv->len = ngx_sprintf(vv->data, "%d", ctx->ctx_ref) - vv->data; | |
+ } | |
+ | |
if (ctx->exec_uri.data[0] == '@') { | |
if (ctx->exec_args.len > 0) { | |
ngx_log_error(NGX_LOG_WARN, r->connection->log, 0, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment