void TSPluginInit(int argc, const char *argv[]) { int ret = 0; ts_lua_g_main_ctx_array = TSmalloc(sizeof(ts_lua_main_ctx) * TS_LUA_MAX_STATE_COUNT); memset(ts_lua_g_main_ctx_array, 0, sizeof(ts_lua_main_ctx) * TS_LUA_MAX_STATE_COUNT); ret = ts_lua_create_vm(ts_lua_g_main_ctx_array, TS_LUA_MAX_STATE_COUNT); if (ret) { ts_lua_destroy_vm(ts_lua_g_main_ctx_array, TS_LUA_MAX_STATE_COUNT); TSfree(ts_lua_g_main_ctx_array); return; } if (argc < 2) { TSError("[%s] lua script file required !!", __FUNCTION__); return; } if (strlen(argv[1]) >= TS_LUA_MAX_SCRIPT_FNAME_LENGTH - 16) { TSError("[%s] lua script file name too long !!", __FUNCTION__); return; } ts_lua_instance_conf *conf = TSmalloc(sizeof(ts_lua_instance_conf)); if (!conf) { TSError("[%s] TSmalloc failed !!", __FUNCTION__); return; } sprintf(conf->script, "%s", argv[1]); ret = ts_lua_add_module(conf, ts_lua_g_main_ctx_array, TS_LUA_MAX_STATE_COUNT, argc - 1, (char **) &argv[1]); if (ret != 0) { TSError("[%s] ts_lua_add_module failed", __FUNCTION__); return; } TSCont txn_start_contp = TSContCreate(transactionStartHookHandler, NULL); if (!txn_start_contp) { TSError("[%s] could not create transaction start continuation", __FUNCTION__); return; } TSContDataSet(txn_start_contp, conf); TSHttpHookAdd(TS_HTTP_TXN_START_HOOK, txn_start_contp); }
TSReturnCode TSRemapNewInstance(int argc, char* argv[], void** ih, char* errbuf, int errbuf_size) { int fn; int ret; if (argc < 3) { strncpy(errbuf, "[TSRemapNewInstance] - lua script file or string is required !!", errbuf_size - 1); return TS_ERROR; } fn = 1; if (argv[2][0] != '/') { fn = 0; } else if (strlen(argv[2]) >= TS_LUA_MAX_SCRIPT_FNAME_LENGTH - 16) { return TS_ERROR; } ts_lua_instance_conf *conf = TSmalloc(sizeof(ts_lua_instance_conf)); if (!conf) { fprintf(stderr, "[%s] TSmalloc failed !!\n", __FUNCTION__); return TS_ERROR; } memset(conf, 0, sizeof(ts_lua_instance_conf)); if (fn) { sprintf(conf->script, "%s", argv[2]); } else { conf->content = argv[2]; } ts_lua_init_instance(conf); ret = ts_lua_add_module(conf, ts_lua_main_ctx_array, TS_LUA_MAX_STATE_COUNT, argc-2, &argv[2]); if (ret != 0) { fprintf(stderr, "[%s] ts_lua_add_module failed\n", __FUNCTION__); return TS_ERROR; } *ih = conf; return TS_SUCCESS; }
void TSPluginInit(int argc, const char *argv[]) { int ret = 0; ts_lua_g_main_ctx_array = TSmalloc(sizeof(ts_lua_main_ctx) * TS_LUA_MAX_STATE_COUNT); memset(ts_lua_g_main_ctx_array, 0, sizeof(ts_lua_main_ctx) * TS_LUA_MAX_STATE_COUNT); ret = ts_lua_create_vm(ts_lua_g_main_ctx_array, TS_LUA_MAX_STATE_COUNT); if (ret) { ts_lua_destroy_vm(ts_lua_g_main_ctx_array, TS_LUA_MAX_STATE_COUNT); TSfree(ts_lua_g_main_ctx_array); return; } if (argc < 2) { TSError("[%s] lua script file required !!", __FUNCTION__); return; } if (strlen(argv[1]) >= TS_LUA_MAX_SCRIPT_FNAME_LENGTH - 16) { TSError("[%s] lua script file name too long !!", __FUNCTION__); return; } ts_lua_instance_conf *conf = TSmalloc(sizeof(ts_lua_instance_conf)); if (!conf) { TSError("[%s] TSmalloc failed !!", __FUNCTION__); return; } memset(conf, 0, sizeof(ts_lua_instance_conf)); sprintf(conf->script, "%s", argv[1]); ts_lua_init_instance(conf); ret = ts_lua_add_module(conf, ts_lua_g_main_ctx_array, TS_LUA_MAX_STATE_COUNT, argc - 1, (char **) &argv[1]); if (ret != 0) { TSError("[%s] ts_lua_add_module failed", __FUNCTION__); return; } TSCont global_contp = TSContCreate(globalHookHandler, NULL); if (!global_contp) { TSError("[%s] could not create transaction start continuation", __FUNCTION__); return; } TSContDataSet(global_contp, conf); //adding hook based on whether the lua global function exists. ts_lua_main_ctx *main_ctx = &ts_lua_g_main_ctx_array[0]; ts_lua_http_ctx *http_ctx = ts_lua_create_http_ctx(main_ctx, conf); lua_State *l = http_ctx->lua; lua_getglobal(l, TS_LUA_FUNCTION_G_SEND_REQUEST); if (lua_type(l, -1) == LUA_TFUNCTION) { TSHttpHookAdd(TS_HTTP_SEND_REQUEST_HDR_HOOK, global_contp); TSDebug(TS_LUA_DEBUG_TAG, "send_request_hdr_hook added"); } lua_pop(l, 1); lua_getglobal(l, TS_LUA_FUNCTION_G_READ_RESPONSE); if (lua_type(l, -1) == LUA_TFUNCTION) { TSHttpHookAdd(TS_HTTP_READ_RESPONSE_HDR_HOOK, global_contp); TSDebug(TS_LUA_DEBUG_TAG, "read_response_hdr_hook added"); } lua_pop(l, 1); lua_getglobal(l, TS_LUA_FUNCTION_G_SEND_RESPONSE); if (lua_type(l, -1) == LUA_TFUNCTION) { TSHttpHookAdd(TS_HTTP_SEND_RESPONSE_HDR_HOOK, global_contp); TSDebug(TS_LUA_DEBUG_TAG, "send_response_hdr_hook added"); } lua_pop(l, 1); lua_getglobal(l, TS_LUA_FUNCTION_G_CACHE_LOOKUP_COMPLETE); if (lua_type(l, -1) == LUA_TFUNCTION) { TSHttpHookAdd(TS_HTTP_CACHE_LOOKUP_COMPLETE_HOOK, global_contp); TSDebug(TS_LUA_DEBUG_TAG, "cache_lookup_complete_hook added"); } lua_pop(l, 1); lua_getglobal(l, TS_LUA_FUNCTION_G_READ_REQUEST); if (lua_type(l, -1) == LUA_TFUNCTION) { TSHttpHookAdd(TS_HTTP_READ_REQUEST_HDR_HOOK, global_contp); TSDebug(TS_LUA_DEBUG_TAG, "read_request_hdr_hook added"); } lua_pop(l, 1); lua_getglobal(l, TS_LUA_FUNCTION_G_TXN_START); if (lua_type(l, -1) == LUA_TFUNCTION) { TSHttpHookAdd(TS_HTTP_TXN_START_HOOK, global_contp); TSDebug(TS_LUA_DEBUG_TAG, "txn_start_hook added"); } lua_pop(l, 1); lua_getglobal(l, TS_LUA_FUNCTION_G_PRE_REMAP); if (lua_type(l, -1) == LUA_TFUNCTION) { TSHttpHookAdd(TS_HTTP_PRE_REMAP_HOOK, global_contp); TSDebug(TS_LUA_DEBUG_TAG, "pre_remap_hook added"); } lua_pop(l, 1); lua_getglobal(l, TS_LUA_FUNCTION_G_POST_REMAP); if (lua_type(l, -1) == LUA_TFUNCTION) { TSHttpHookAdd(TS_HTTP_POST_REMAP_HOOK, global_contp); TSDebug(TS_LUA_DEBUG_TAG, "post_remap_hook added"); } lua_pop(l, 1); lua_getglobal(l, TS_LUA_FUNCTION_G_SELECT_ALT); if (lua_type(l, -1) == LUA_TFUNCTION) { TSHttpHookAdd(TS_HTTP_SELECT_ALT_HOOK, global_contp); TSDebug(TS_LUA_DEBUG_TAG, "select_alt_hook added"); } lua_pop(l, 1); lua_getglobal(l, TS_LUA_FUNCTION_G_OS_DNS); if (lua_type(l, -1) == LUA_TFUNCTION) { TSHttpHookAdd(TS_HTTP_OS_DNS_HOOK, global_contp); TSDebug(TS_LUA_DEBUG_TAG, "os_dns_hook added"); } lua_pop(l, 1); lua_getglobal(l, TS_LUA_FUNCTION_G_READ_CACHE); if (lua_type(l, -1) == LUA_TFUNCTION) { TSHttpHookAdd(TS_HTTP_READ_CACHE_HDR_HOOK, global_contp); TSDebug(TS_LUA_DEBUG_TAG, "read_cache_hdr_hook added"); } lua_pop(l, 1); lua_getglobal(l, TS_LUA_FUNCTION_G_TXN_CLOSE); if (lua_type(l, -1) == LUA_TFUNCTION) { TSHttpHookAdd(TS_HTTP_TXN_CLOSE_HOOK, global_contp); TSDebug(TS_LUA_DEBUG_TAG, "txn_close_hook added"); } lua_pop(l, 1); ts_lua_destroy_http_ctx(http_ctx); }
TSReturnCode TSRemapNewInstance(int argc, char *argv[], void **ih, char *errbuf, int errbuf_size) { int fn; int ret; int states = TS_LUA_MAX_STATE_COUNT; static const struct option longopt[] = { {"states", required_argument, 0, 's'}, {0, 0, 0, 0}, }; argc--; argv++; for (;;) { int opt; opt = getopt_long(argc, (char *const *)argv, "", longopt, NULL); switch (opt) { case 's': states = atoi(optarg); TSDebug(TS_LUA_DEBUG_TAG, "[%s] setting number of lua VM [%d]", __FUNCTION__, states); // set state break; } if (opt == -1) { break; } } if (states > TS_LUA_MAX_STATE_COUNT || states < 1) { snprintf(errbuf, errbuf_size, "[TSRemapNewInstance] - invalid state in option input. Must be between 1 and %d", TS_LUA_MAX_STATE_COUNT); return TS_ERROR; } if (argc - optind < 1) { strncpy(errbuf, "[TSRemapNewInstance] - lua script file or string is required !!", errbuf_size - 1); errbuf[errbuf_size - 1] = '\0'; return TS_ERROR; } fn = 1; if (argv[optind][0] != '/') { fn = 0; } else if (strlen(argv[optind]) >= TS_LUA_MAX_SCRIPT_FNAME_LENGTH - 16) { return TS_ERROR; } ts_lua_instance_conf *conf = NULL; // check to make sure it is a lua file and there is no parameter for the lua file if (fn && (argc - optind < 2)) { TSDebug(TS_LUA_DEBUG_TAG, "[%s] checking if script has been registered", __FUNCTION__); char script[TS_LUA_MAX_SCRIPT_FNAME_LENGTH]; snprintf(script, TS_LUA_MAX_SCRIPT_FNAME_LENGTH, "%s", argv[optind]); // we only need to check the first lua VM for script registration conf = ts_lua_script_registered(ts_lua_main_ctx_array[0].lua, script); } if (!conf) { TSDebug(TS_LUA_DEBUG_TAG, "[%s] creating new conf instance", __FUNCTION__); conf = TSmalloc(sizeof(ts_lua_instance_conf)); if (!conf) { strncpy(errbuf, "[TSRemapNewInstance] TSmalloc failed!!", errbuf_size - 1); errbuf[errbuf_size - 1] = '\0'; return TS_ERROR; } memset(conf, 0, sizeof(ts_lua_instance_conf)); conf->states = states; conf->remap = 1; conf->init_func = 0; if (fn) { snprintf(conf->script, TS_LUA_MAX_SCRIPT_FNAME_LENGTH, "%s", argv[optind]); } else { conf->content = argv[optind]; } ts_lua_init_instance(conf); ret = ts_lua_add_module(conf, ts_lua_main_ctx_array, conf->states, argc - optind, &argv[optind], errbuf, errbuf_size); if (ret != 0) { return TS_ERROR; } // register the script only if it is from a file and has no __init__ function if (fn && !conf->init_func) { // we only need to register the script for the first lua VM ts_lua_script_register(ts_lua_main_ctx_array[0].lua, conf->script, conf); } } *ih = conf; return TS_SUCCESS; }