Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 1 addition & 16 deletions plugins/lua/ts_lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -644,14 +644,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;

Expand Down Expand Up @@ -708,14 +700,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
Expand Down
106 changes: 92 additions & 14 deletions plugins/lua/ts_lua_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -674,6 +722,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);
}
Expand Down Expand Up @@ -872,6 +924,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;
Expand All @@ -893,6 +948,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) {
Expand All @@ -904,6 +977,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:
Expand All @@ -914,6 +989,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:
Expand All @@ -924,6 +1001,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:
Expand All @@ -935,30 +1014,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;

Expand All @@ -969,6 +1037,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:
Expand All @@ -978,6 +1048,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:
Expand All @@ -987,6 +1059,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:
Expand All @@ -996,6 +1070,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:
Expand All @@ -1005,6 +1081,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:
Expand Down
1 change: 1 addition & 0 deletions plugins/lua/ts_lua_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down