This is an automated email from the ASF dual-hosted git repository. zwoop pushed a commit to branch 9.2.x in repository https://gitbox.apache.org/repos/asf/trafficserver.git
The following commit(s) were added to refs/heads/9.2.x by this push: new 6ca0e9fbf Clear lua plugin http context after each hook handler (#8607) 6ca0e9fbf is described below commit 6ca0e9fbfc90211b8c2118f1d04655ff73048ffe Author: Kit Chan <kic...@apache.org> AuthorDate: Thu Jan 27 15:59:45 2022 -0800 Clear lua plugin http context after each hook handler (#8607) (cherry picked from commit 4e8d9dcd01b8cae7bbfece7d83df2ba852bfc7d3) --- plugins/lua/ts_lua.c | 17 +------- plugins/lua/ts_lua_util.c | 106 ++++++++++++++++++++++++++++++++++++++++------ plugins/lua/ts_lua_util.h | 1 + 3 files changed, 94 insertions(+), 30 deletions(-) diff --git a/plugins/lua/ts_lua.c b/plugins/lua/ts_lua.c index a4c5532b0..2dc6ecbf5 100644 --- a/plugins/lua/ts_lua.c +++ b/plugins/lua/ts_lua.c @@ -661,14 +661,6 @@ globalHookHandler(TSCont contp, TSEvent event ATS_UNUSED, void *edata) break; case TS_EVENT_HTTP_SEND_RESPONSE_HDR: - // client response can be changed within a transaction - // (e.g. due to the follow redirect feature). So, clearing the pointers - // to allow API(s) to fetch the pointers again when it re-enters the hook - if (http_ctx->client_response_hdrp != NULL) { - TSHandleMLocRelease(http_ctx->client_response_bufp, TS_NULL_MLOC, http_ctx->client_response_hdrp); - http_ctx->client_response_bufp = NULL; - http_ctx->client_response_hdrp = NULL; - } lua_getglobal(l, TS_LUA_FUNCTION_G_SEND_RESPONSE); break; @@ -725,14 +717,7 @@ globalHookHandler(TSCont contp, TSEvent event ATS_UNUSED, void *edata) ret = lua_tointeger(l, -1); lua_pop(l, 1); - // client response can be changed within a transaction - // (e.g. due to the follow redirect feature). So, clearing the pointers - // to allow API(s) to fetch the pointers again when it re-enters the hook - if (http_ctx->client_response_hdrp != NULL) { - TSHandleMLocRelease(http_ctx->client_response_bufp, TS_NULL_MLOC, http_ctx->client_response_hdrp); - http_ctx->client_response_bufp = NULL; - http_ctx->client_response_hdrp = NULL; - } + ts_lua_clear_http_ctx(http_ctx); if (http_ctx->has_hook) { // add a hook to release resources for context diff --git a/plugins/lua/ts_lua_util.c b/plugins/lua/ts_lua_util.c index fa9685581..e2cb68b89 100644 --- a/plugins/lua/ts_lua_util.c +++ b/plugins/lua/ts_lua_util.c @@ -57,6 +57,54 @@ ts_lua_update_server_response_hdrp(ts_lua_http_ctx *http_ctx) } } +void +ts_lua_clear_http_ctx(ts_lua_http_ctx *http_ctx) +{ + if (http_ctx->rri == NULL) { + if (http_ctx->client_request_url != NULL) { + TSHandleMLocRelease(http_ctx->client_request_bufp, http_ctx->client_request_hdrp, http_ctx->client_request_url); + http_ctx->client_request_url = NULL; + } + + if (http_ctx->client_request_bufp != NULL) { + TSHandleMLocRelease(http_ctx->client_request_bufp, TS_NULL_MLOC, http_ctx->client_request_hdrp); + http_ctx->client_request_bufp = NULL; + http_ctx->client_request_hdrp = NULL; + } + } + + if (http_ctx->server_request_url != NULL) { + TSHandleMLocRelease(http_ctx->server_request_bufp, http_ctx->server_request_hdrp, http_ctx->server_request_url); + http_ctx->server_request_url = NULL; + } + + if (http_ctx->server_request_hdrp != NULL) { + TSHandleMLocRelease(http_ctx->server_request_bufp, TS_NULL_MLOC, http_ctx->server_request_hdrp); + http_ctx->server_request_bufp = NULL; + http_ctx->server_request_hdrp = NULL; + } + + if (http_ctx->server_response_bufp != NULL) { + TSHandleMLocRelease(http_ctx->server_response_bufp, TS_NULL_MLOC, http_ctx->server_response_hdrp); + http_ctx->server_response_bufp = NULL; + http_ctx->server_response_hdrp = NULL; + } + + if (http_ctx->client_response_hdrp != NULL) { + TSHandleMLocRelease(http_ctx->client_response_bufp, TS_NULL_MLOC, http_ctx->client_response_hdrp); + http_ctx->client_response_bufp = NULL; + http_ctx->client_response_hdrp = NULL; + } + + if (http_ctx->cached_response_bufp != NULL) { + TSMimeHdrDestroy(http_ctx->cached_response_bufp, http_ctx->cached_response_hdrp); + TSHandleMLocRelease(http_ctx->cached_response_bufp, TS_NULL_MLOC, http_ctx->cached_response_hdrp); + TSMBufferDestroy(http_ctx->cached_response_bufp); + http_ctx->cached_response_bufp = NULL; + http_ctx->cached_response_hdrp = NULL; + } +} + int ts_lua_create_vm(ts_lua_main_ctx *arr, int n) { @@ -703,6 +751,10 @@ ts_lua_destroy_http_ctx(ts_lua_http_ctx *http_ctx) ci = &http_ctx->cinfo; if (http_ctx->rri == NULL) { + if (http_ctx->client_request_url) { + TSHandleMLocRelease(http_ctx->client_request_bufp, http_ctx->client_request_hdrp, http_ctx->client_request_url); + } + if (http_ctx->client_request_bufp) { TSHandleMLocRelease(http_ctx->client_request_bufp, TS_NULL_MLOC, http_ctx->client_request_hdrp); } @@ -901,6 +953,9 @@ int ts_lua_http_cont_handler(TSCont contp, TSEvent ev, void *edata) { TSHttpTxn txnp; + TSMBuffer bufp; + TSMLoc hdr_loc; + TSMLoc url_loc; int event, ret, rc, n, t; lua_State *L; ts_lua_http_ctx *http_ctx; @@ -922,6 +977,24 @@ ts_lua_http_cont_handler(TSCont contp, TSEvent ev, void *edata) TSMutexLock(main_ctx->mutexp); + if (!http_ctx->client_request_bufp) { + if (TSHttpTxnClientReqGet(txnp, &bufp, &hdr_loc) == TS_SUCCESS) { + http_ctx->client_request_bufp = bufp; + http_ctx->client_request_hdrp = hdr_loc; + + if (TSHttpHdrUrlGet(bufp, hdr_loc, &url_loc) == TS_SUCCESS) { + http_ctx->client_request_url = url_loc; + } + } + } + + if (!http_ctx->client_request_hdrp) { + TSMutexUnlock(main_ctx->mutexp); + + TSHttpTxnReenable(txnp, TS_EVENT_HTTP_CONTINUE); + return 0; + } + ts_lua_set_cont_info(L, ci); switch (event) { @@ -933,6 +1006,8 @@ ts_lua_http_cont_handler(TSCont contp, TSEvent ev, void *edata) ret = lua_resume(L, 0); } + ts_lua_clear_http_ctx(http_ctx); + break; case TS_EVENT_HTTP_CACHE_LOOKUP_COMPLETE: @@ -943,6 +1018,8 @@ ts_lua_http_cont_handler(TSCont contp, TSEvent ev, void *edata) ret = lua_resume(L, 0); } + ts_lua_clear_http_ctx(http_ctx); + break; case TS_EVENT_HTTP_SEND_REQUEST_HDR: @@ -953,6 +1030,8 @@ ts_lua_http_cont_handler(TSCont contp, TSEvent ev, void *edata) ret = lua_resume(L, 0); } + ts_lua_clear_http_ctx(http_ctx); + break; case TS_EVENT_HTTP_READ_RESPONSE_HDR: @@ -964,30 +1043,19 @@ ts_lua_http_cont_handler(TSCont contp, TSEvent ev, void *edata) ret = lua_resume(L, 0); } + ts_lua_clear_http_ctx(http_ctx); + break; case TS_EVENT_HTTP_SEND_RESPONSE_HDR: - // client response can be changed within a transaction - // (e.g. due to the follow redirect feature). So, clearing the pointers - // to allow API(s) to fetch the pointers again when it re-enters the hook - if (http_ctx->client_response_hdrp != NULL) { - TSHandleMLocRelease(http_ctx->client_response_bufp, TS_NULL_MLOC, http_ctx->client_response_hdrp); - http_ctx->client_response_bufp = NULL; - http_ctx->client_response_hdrp = NULL; - } - lua_getglobal(L, TS_LUA_FUNCTION_SEND_RESPONSE); if (lua_type(L, -1) == LUA_TFUNCTION) { ret = lua_resume(L, 0); } - if (http_ctx->client_response_hdrp != NULL) { - TSHandleMLocRelease(http_ctx->client_response_bufp, TS_NULL_MLOC, http_ctx->client_response_hdrp); - http_ctx->client_response_bufp = NULL; - http_ctx->client_response_hdrp = NULL; - } + ts_lua_clear_http_ctx(http_ctx); break; @@ -998,6 +1066,8 @@ ts_lua_http_cont_handler(TSCont contp, TSEvent ev, void *edata) ret = lua_resume(L, 0); } + ts_lua_clear_http_ctx(http_ctx); + break; case TS_EVENT_HTTP_TXN_START: @@ -1007,6 +1077,8 @@ ts_lua_http_cont_handler(TSCont contp, TSEvent ev, void *edata) ret = lua_resume(L, 0); } + ts_lua_clear_http_ctx(http_ctx); + break; case TS_EVENT_HTTP_PRE_REMAP: @@ -1016,6 +1088,8 @@ ts_lua_http_cont_handler(TSCont contp, TSEvent ev, void *edata) ret = lua_resume(L, 0); } + ts_lua_clear_http_ctx(http_ctx); + break; case TS_EVENT_HTTP_OS_DNS: @@ -1025,6 +1099,8 @@ ts_lua_http_cont_handler(TSCont contp, TSEvent ev, void *edata) ret = lua_resume(L, 0); } + ts_lua_clear_http_ctx(http_ctx); + break; case TS_EVENT_HTTP_READ_CACHE_HDR: @@ -1034,6 +1110,8 @@ ts_lua_http_cont_handler(TSCont contp, TSEvent ev, void *edata) ret = lua_resume(L, 0); } + ts_lua_clear_http_ctx(http_ctx); + break; case TS_EVENT_HTTP_TXN_CLOSE: diff --git a/plugins/lua/ts_lua_util.h b/plugins/lua/ts_lua_util.h index d75db9f18..accf003ca 100644 --- a/plugins/lua/ts_lua_util.h +++ b/plugins/lua/ts_lua_util.h @@ -55,6 +55,7 @@ ts_lua_http_ctx *ts_lua_get_http_ctx(lua_State *L); ts_lua_http_ctx *ts_lua_create_http_ctx(ts_lua_main_ctx *mctx, ts_lua_instance_conf *conf); void ts_lua_destroy_http_ctx(ts_lua_http_ctx *http_ctx); +void ts_lua_clear_http_ctx(ts_lua_http_ctx *http_ctx); ts_lua_http_transform_ctx *ts_lua_create_http_transform_ctx(ts_lua_http_ctx *http_ctx, TSVConn connp); void ts_lua_destroy_http_transform_ctx(ts_lua_http_transform_ctx *transform_ctx);