| 501 | } |
| 502 | |
| 503 | static TSRemapStatus |
| 504 | ts_lua_remap_plugin_init(void *ih, TSHttpTxn rh, TSRemapRequestInfo *rri) |
| 505 | { |
| 506 | int ret; |
| 507 | uint64_t req_id; |
| 508 | |
| 509 | TSCont contp; |
| 510 | lua_State *L; |
| 511 | |
| 512 | ts_lua_main_ctx *main_ctx; |
| 513 | ts_lua_http_ctx *http_ctx; |
| 514 | ts_lua_cont_info *ci; |
| 515 | |
| 516 | ts_lua_instance_conf *instance_conf; |
| 517 | |
| 518 | int remap = (rri == nullptr ? 0 : 1); |
| 519 | instance_conf = (ts_lua_instance_conf *)ih; |
| 520 | |
| 521 | main_ctx = static_cast<decltype(main_ctx)>(pthread_getspecific(lua_state_key)); |
| 522 | if (main_ctx == nullptr) { |
| 523 | req_id = __sync_fetch_and_add(&ts_lua_http_next_id, 1); |
| 524 | main_ctx = &ts_lua_main_ctx_array[req_id % instance_conf->states]; |
| 525 | pthread_setspecific(lua_state_key, main_ctx); |
| 526 | } |
| 527 | |
| 528 | TSMutexLock(main_ctx->mutexp); |
| 529 | |
| 530 | http_ctx = ts_lua_create_http_ctx(main_ctx, instance_conf); |
| 531 | |
| 532 | http_ctx->txnp = rh; |
| 533 | http_ctx->has_hook = 0; |
| 534 | http_ctx->rri = rri; |
| 535 | if (rri != nullptr) { |
| 536 | http_ctx->client_request_bufp = rri->requestBufp; |
| 537 | http_ctx->client_request_hdrp = rri->requestHdrp; |
| 538 | http_ctx->client_request_url = rri->requestUrl; |
| 539 | } |
| 540 | |
| 541 | ci = &http_ctx->cinfo; |
| 542 | L = ci->routine.lua; |
| 543 | |
| 544 | contp = TSContCreate(ts_lua_http_cont_handler, nullptr); |
| 545 | TSContDataSet(contp, http_ctx); |
| 546 | |
| 547 | ci->contp = contp; |
| 548 | ci->mutex = TSContMutexGet((TSCont)rh); |
| 549 | |
| 550 | lua_getglobal(L, (remap ? TS_LUA_FUNCTION_REMAP : TS_LUA_FUNCTION_OS_RESPONSE)); |
| 551 | if (lua_type(L, -1) != LUA_TFUNCTION) { |
| 552 | lua_pop(L, 1); |
| 553 | ts_lua_destroy_http_ctx(http_ctx); |
| 554 | TSMutexUnlock(main_ctx->mutexp); |
| 555 | return TSREMAP_NO_REMAP; |
| 556 | } |
| 557 | |
| 558 | ts_lua_set_cont_info(L, nullptr); |
| 559 | if (lua_pcall(L, 0, 1, 0) != 0) { |
| 560 | TSError("[ts_lua][%s] lua_pcall failed: %s", __FUNCTION__, lua_tostring(L, -1)); |
no test coverage detected