void TSPluginInit(int argc, const char *argv[]) { TSPluginRegistrationInfo info; TSCont main_cont, config_cont; config_holder_t *config_holder; info.plugin_name = PLUGIN_TAG; info.vendor_name = "Comcast"; info.support_email = "*****@*****.**"; astatsLoad = time(NULL); if (TSPluginRegister(TS_SDK_VERSION_2_0, &info) != TS_SUCCESS) TSError("Plugin registration failed. \n"); if (!check_ts_version()) { TSError("Plugin requires Traffic Server 2.0 or later\n"); return; } config_holder = new_config_holder(argc > 1 ? argv[1] : NULL); main_cont = TSContCreate(astats_origin, NULL); TSContDataSet(main_cont, (void *) config_holder); TSHttpHookAdd(TS_HTTP_READ_REQUEST_HDR_HOOK, main_cont); config_cont = TSContCreate(config_handler, TSMutexCreate()); TSContDataSet(config_cont, (void *) config_holder); TSMgmtUpdateRegister(config_cont, PLUGIN_TAG); /* Create a continuation with a mutex as there is a shared global structure containing the headers to add */ TSDebug(PLUGIN_TAG, "astats module registered, path: '%s'", config_holder->config->stats_path); }
void TSPluginInit(int argc, const char *argv[]) { int i; TSPluginRegistrationInfo info; info.plugin_name = "blacklist-0"; info.vendor_name = "MyCompany"; info.support_email = "*****@*****.**"; if (TSPluginRegister(&info) != TS_SUCCESS) { TSError("[blacklist-0] Plugin registration failed."); } nsites = argc - 1; if (nsites > 0) { sites = (char **)TSmalloc(sizeof(char *) * nsites); for (i = 0; i < nsites; i++) { sites[i] = TSstrdup(argv[i + 1]); } TSHttpHookAdd(TS_HTTP_OS_DNS_HOOK, TSContCreate(blacklist_plugin, NULL)); } }
TSRemapStatus TSRemapDoRemap(void* ih, TSHttpTxn rh, TSRemapRequestInfo *rri) { int ret; uint64_t req_id; TSCont contp; lua_State *L; ts_lua_main_ctx *main_ctx; ts_lua_http_ctx *http_ctx; ts_lua_cont_info *ci; ts_lua_instance_conf *instance_conf; instance_conf = (ts_lua_instance_conf*)ih; req_id = __sync_fetch_and_add(&ts_lua_http_next_id, 1); main_ctx = &ts_lua_main_ctx_array[req_id%TS_LUA_MAX_STATE_COUNT]; TSMutexLock(main_ctx->mutexp); http_ctx = ts_lua_create_http_ctx(main_ctx, instance_conf); http_ctx->txnp = rh; http_ctx->client_request_bufp = rri->requestBufp; http_ctx->client_request_hdrp = rri->requestHdrp; http_ctx->client_request_url = rri->requestUrl; http_ctx->rri = rri; ci = &http_ctx->cinfo; L = ci->routine.lua; contp = TSContCreate(ts_lua_http_cont_handler, NULL); TSContDataSet(contp, http_ctx); ci->contp = contp; ci->mutex = TSContMutexGet((TSCont)rh); // push do_remap function on the stack, and no async operation should exist here. lua_getglobal(L, TS_LUA_FUNCTION_REMAP); if (lua_pcall(L, 0, 1, 0) != 0) { ee("lua_pcall failed: %s", lua_tostring(L, -1)); ret = TSREMAP_NO_REMAP; } else { ret = lua_tointeger(L, -1); } lua_pop(L, 1); // pop the result if (http_ctx->hooks > 0) { TSMutexUnlock(main_ctx->mutexp); TSHttpTxnHookAdd(rh, TS_HTTP_TXN_CLOSE_HOOK, contp); } else { ts_lua_destroy_http_ctx(http_ctx); TSMutexUnlock(main_ctx->mutexp); } return ret; }
static int ts_lua_http_server_intercept(lua_State *L) { TSCont contp; int type; ts_lua_http_ctx *http_ctx; http_ctx = ts_lua_get_http_ctx(L); http_ctx->has_hook = 1; type = lua_type(L, 1); if (type != LUA_TFUNCTION) { TSError("[%s] param in ts.http.server_intercept should be a function", __FUNCTION__); return 0; } lua_pushvalue(L, 1); lua_setglobal(L, TS_LUA_FUNCTION_HTTP_SERVER_INTERCEPT); http_ctx->intercept_type = TS_LUA_TYPE_HTTP_SERVER_INTERCEPT; contp = TSContCreate(ts_lua_http_intercept_entry, TSMutexCreate()); TSContDataSet(contp, http_ctx); TSHttpTxnServerIntercept(contp, http_ctx->txnp); return 0; }
void ts_http_fetcher_launch(http_fetcher *fch) { int64_t hdr_len; // post body , content-length TSIOBufferWrite(fch->req_buffer, "\r\n", sizeof("\r\n")-1); hdr_len = TSIOBufferReaderAvail(fch->req_reader); fch->http_vc = TSHttpConnect(&(fch->aip)); fch->hdr_buffer = TSIOBufferSizedCreate(TS_IOBUFFER_SIZE_INDEX_8K); fch->hdr_reader = TSIOBufferReaderAlloc(fch->hdr_buffer); fch->hdr_bufp = TSMBufferCreate(); fch->hdr_loc = TSHttpHdrCreate(fch->hdr_bufp); fch->resp_buffer = TSIOBufferCreate(); fch->resp_reader = TSIOBufferReaderAlloc(fch->resp_buffer); fch->http_parser = TSHttpParserCreate(); fch->fetch_contp = TSContCreate(ts_http_fetch_handler, fch->mutexp); TSContDataSet(fch->fetch_contp, fch); fch->read_vio = TSVConnRead(fch->http_vc, fch->fetch_contp, fch->resp_buffer, INT64_MAX); if (fch->method == TS_FETCH_METHOD_POST && fch->post_cl >= 0) { fch->write_vio = TSVConnWrite(fch->http_vc, fch->fetch_contp, fch->req_reader, hdr_len + fch->post_cl); } else { fch->write_vio = TSVConnWrite(fch->http_vc, fch->fetch_contp, fch->req_reader, hdr_len); } fch->launched = 1; }
static void protocol_init(int accept_port, int server_port ATS_UNUSED) { TSCont contp; int ret_val; /* create customized log */ ret_val = TSTextLogObjectCreate("protocol", TS_LOG_MODE_ADD_TIMESTAMP, &protocol_plugin_log); if (ret_val != TS_SUCCESS) { TSError("[protocol] Failed to create log"); } /* format of the log entries, for caching_status, 1 for HIT and 0 for MISS */ ret_val = TSTextLogObjectWrite(protocol_plugin_log, "timestamp filename servername caching_status\n\n"); if (ret_val != TS_SUCCESS) { TSError("[protocol] Failed to write into log"); } contp = TSContCreate(accept_handler, TSMutexCreate()); /* Accept network traffic from the accept_port. When there are requests coming in, contp's handler should be called, in this case, contp's handler is accept_event, see AcceptSM.c */ pending_action = TSNetAccept(contp, accept_port, -1, 1); }
static void ironbee_plugin_ssn_start(TSCont contp, TSHttpSsn ssnp) { assert(contp != NULL); assert(ssnp != NULL); TSCont mycont; tsib_ssn_ctx *ssndata; /* start of connection */ /* But we can't initialize conn stuff here, because there's * no API to get the connection stuff required by ironbee * at this point. So instead, intercept the first TXN * * what we can and must do: create a new contp whose * lifetime is our ssn */ mycont = TSContCreate(ironbee_plugin, NULL); TSHttpSsnHookAdd (ssnp, TS_HTTP_TXN_START_HOOK, mycont); ssndata = TSmalloc(sizeof(*ssndata)); memset(ssndata, 0, sizeof(*ssndata)); ssndata->contp = mycont; TSContDataSet(mycont, ssndata); TSHttpSsnHookAdd (ssnp, TS_HTTP_SSN_CLOSE_HOOK, mycont); TSHttpSsnReenable (ssnp, TS_EVENT_HTTP_CONTINUE); }
void TSPluginInit(int argc, const char *argv[]) { TSPluginRegistrationInfo info; info.plugin_name = "null-transform"; info.vendor_name = "MyCompany"; info.support_email = "*****@*****.**"; if (TSPluginRegister(TS_SDK_VERSION_3_0, &info) != TS_SUCCESS) { TSError("[null-transform] Plugin registration failed.\n"); goto Lerror; } if (!check_ts_version()) { TSError("[null-transform] Plugin requires Traffic Server 3.0 " "or later\n"); goto Lerror; } TSHttpHookAdd(TS_HTTP_READ_RESPONSE_HDR_HOOK, TSContCreate(transform_plugin, NULL)); return; Lerror: TSError("[null-tranform] Unable to initialize plugin (disabled).\n"); }
void TSPluginInit(int argc, const char *argv[]) { TSPluginRegistrationInfo info; TSCont cont; info.plugin_name = "server-transform"; info.vendor_name = "MyCompany"; info.support_email = "*****@*****.**"; if (TSPluginRegister(TS_SDK_VERSION_3_0, &info) != TS_SUCCESS) { TSError("Plugin registration failed.\n"); } if (!check_ts_version()) { TSError("Plugin requires Traffic Server 3.0 or later\n"); return; } /* connect to the echo port on localhost */ server_ip = (127 << 24) | (0 << 16) | (0 << 8) | (1); server_ip = htonl(server_ip); server_port = 7; cont = TSContCreate(transform_plugin, NULL); TSHttpHookAdd(TS_HTTP_READ_REQUEST_HDR_HOOK, cont); }
void TSPluginInit(int argc, const char *argv[]) { const char prefix[] = "http://"; int uri_len; TSPluginRegistrationInfo info; info.plugin_name = "redirect-1"; info.vendor_name = "MyCompany"; info.support_email = "*****@*****.**"; if (TSPluginRegister(TS_SDK_VERSION_3_0, &info) != TS_SUCCESS) { TSError("Plugin registration failed.\n"); } if (!check_ts_version()) { TSError("Plugin requires Traffic Server 3.0 or later\n"); return; } if (argc == 3) { block_ip = TSstrdup(argv[1]); /* * The Location header must contain an absolute URI: */ url_redirect = TSstrdup(argv[2]); uri_len = strlen(prefix) + strlen(url_redirect) + 1; uri_redirect = TSmalloc(uri_len); TSstrlcpy(uri_redirect, prefix, uri_len); TSstrlcat(uri_redirect, url_redirect, uri_len); } else { TSError("Incorrect syntax in plugin.conf: correct usage is" "redirect-1.so ip_deny url_redirect"); return; } ip_deny = inet_addr(block_ip); TSDebug("redirect_init", "initializing stats..."); init_stats(); TSHttpHookAdd(TS_HTTP_READ_REQUEST_HDR_HOOK, TSContCreate(redirect_plugin, NULL)); TSDebug("redirect_init", "block_ip is %s, url_redirect is %s, and uri_redirect is %s", block_ip, url_redirect, uri_redirect); // ToDo: Should figure out how to print IPs which are IPv4 / v6. // TSDebug("redirect_init", "ip_deny is %ld\n", ip_deny); /* * Demonstrate another tracing function. This can be used to * enable debug calculations and other work that should only * be done in debug mode. */ if (TSIsDebugTagSet("redirect_demo")) TSDebug("redirect_init", "The redirect_demo tag is set"); else TSDebug("redirect_init", "The redirect_demo tag is not set"); }
static int ts_lua_sleep(lua_State *L) { int sec; TSAction action; TSCont contp; ts_lua_async_item *ai; ts_lua_cont_info *ci; ci = ts_lua_get_cont_info(L); if (ci == NULL) return 0; sec = luaL_checknumber(L, 1); if (sec < 1) { sec = 1; } contp = TSContCreate(ts_lua_sleep_handler, ci->mutex); action = TSContSchedule(contp, sec * 1000, TS_THREAD_POOL_DEFAULT); ai = ts_lua_async_create_item(contp, ts_lua_sleep_cleanup, (void *)action, ci); TSContDataSet(contp, ai); return lua_yield(L, 0); }
void TSPluginInit(int argc, const char *argv[]) { TSPluginRegistrationInfo info; TSMutex mutex = TS_NULL_MUTEX; info.plugin_name = "buffered-null-transform"; info.vendor_name = "MyCompany"; info.support_email = "*****@*****.**"; if (TSPluginRegister(TS_SDK_VERSION_3_0, &info) != TS_SUCCESS) { TSError("[bnull-transform] Plugin registration failed.\n"); goto Lerror; } if (!check_ts_version()) { TSError("[bnull-transform] Plugin requires Traffic Server 3.0" " or later\n"); goto Lerror; } /* This is call we could use if we need to protect global data */ /* TSReleaseAssert ((mutex = TSMutexCreate()) != TS_NULL_MUTEX); */ TSHttpHookAdd(TS_HTTP_READ_RESPONSE_HDR_HOOK, TSContCreate(transform_plugin, mutex)); return; Lerror: TSError("[bnull-transform] Plugin disabled\n"); }
void TSPluginInit(int argc, const char *argv[]) { TSCont contp; TSPluginRegistrationInfo info; info.plugin_name = "session-1"; info.vendor_name = "MyCompany"; info.support_email = "*****@*****.**"; if (TSPluginRegister(&info) != TS_SUCCESS) { TSError("[session-1] Plugin registration failed.\n"); goto error; } transaction_count = INKStatCreate("transaction.count", INKSTAT_TYPE_INT64); session_count = INKStatCreate("session.count", INKSTAT_TYPE_INT64); av_transaction = INKStatCreate("avg.transactions", INKSTAT_TYPE_FLOAT); contp = TSContCreate(ssn_handler, NULL); TSHttpHookAdd(TS_HTTP_SSN_START_HOOK, contp); error: TSError("[session-1] Plugin not initialized"); }
/** Create and return top-level cont with no transient data * Sets up engine manager and kill-or-continue txn hook before launching * potentially-slow mainconfiguration in separate thread. */ static ib_status_t tsib_pre_init(TSCont *contp) { int rv; ib_status_t rc; TSCont cont; assert(contp != NULL); /* create a cont to fend off traffic while we read config */ *contp = cont = TSContCreate(ironbee_plugin, NULL); if (cont == NULL) { TSError("[ironbee] failed to create initial continuation: disabled"); return IB_EUNKNOWN; } if (module_data.allow_at_startup) { /* SSN_START doesn't use contdata; READ_REQUEST_HDR only needs non-null flag. * Using &module_data might let us clean up some tsib_api stuff in future. */ TSContDataSet(cont, &module_data); } else { /* NULL contdata signals the READ_REQUEST_HDR hook to reject requests */ TSContDataSet(cont, NULL); } TSHttpHookAdd(TS_HTTP_READ_REQUEST_HDR_HOOK, cont); if (!module_data.log_disable) { /* success is documented as TS_LOG_ERROR_NO_ERROR but that's undefined. * It's actually a TS_SUCCESS (proxy/InkAPI.cc line 6641). */ printf("Logging to \"%s\"\n", module_data.log_file); rv = TSTextLogObjectCreate(module_data.log_file, TS_LOG_MODE_ADD_TIMESTAMP, &module_data.logger); if (rv != TS_SUCCESS) { TSError("[ironbee] Error creating log file."); return IB_EUNKNOWN; } } /* Initialize IronBee (including util) */ rc = ib_initialize(); if (rc != IB_OK) { TSError("[ironbee] Error initializing IronBee: %s", ib_status_to_string(rc)); return rc; } /* Create the IronBee engine manager */ TSDebug("ironbee", "Creating IronBee engine manager"); rc = ib_manager_create(&(module_data.manager), /* Engine Manager */ &ibplugin, /* Server object */ module_data.max_engines); /* Default max */ if (rc != IB_OK) { TSError("[ironbee] Error creating IronBee engine manager: %s", ib_status_to_string(rc)); } return rc; }
/* Create the Txn data structure and the continuation for the Txn. */ TSCont TxnSMCreate(TSMutex pmutex, TSVConn client_vc, int server_port) { TSCont contp; TxnSM *txn_sm; txn_sm = (TxnSM *)TSmalloc(sizeof(TxnSM)); txn_sm->q_mutex = pmutex; txn_sm->q_pending_action = NULL; /* Txn will use this server port to connect to the origin server. */ txn_sm->q_server_port = server_port; /* The client_vc is returned by TSNetAccept, refer to Protocol.c. */ txn_sm->q_client_vc = client_vc; /* The server_vc will be created if Txn connects to the origin server. */ txn_sm->q_server_vc = NULL; txn_sm->q_client_read_vio = NULL; txn_sm->q_client_write_vio = NULL; txn_sm->q_client_request_buffer = NULL; txn_sm->q_client_response_buffer = NULL; txn_sm->q_client_request_buffer_reader = NULL; txn_sm->q_client_response_buffer_reader = NULL; txn_sm->q_server_read_vio = NULL; txn_sm->q_server_write_vio = NULL; txn_sm->q_server_request_buffer = NULL; txn_sm->q_server_response_buffer = NULL; txn_sm->q_server_request_buffer_reader = NULL; /* Char buffers to store client request and server response. */ txn_sm->q_client_request = (char *)TSmalloc(sizeof(char) * (MAX_REQUEST_LENGTH + 1)); memset(txn_sm->q_client_request, '\0', (sizeof(char) * (MAX_REQUEST_LENGTH + 1))); txn_sm->q_server_response = NULL; txn_sm->q_server_response_length = 0; txn_sm->q_block_bytes_read = 0; txn_sm->q_cache_vc = NULL; txn_sm->q_cache_response_length = 0; txn_sm->q_cache_read_buffer = NULL; txn_sm->q_cache_read_buffer_reader = NULL; txn_sm->q_server_name = (char *)TSmalloc(sizeof(char) * (MAX_SERVER_NAME_LENGTH + 1)); txn_sm->q_file_name = (char *)TSmalloc(sizeof(char) * (MAX_FILE_NAME_LENGTH + 1)); txn_sm->q_key = NULL; txn_sm->q_magic = TXN_SM_ALIVE; /* Set the current handler to be state_start. */ set_handler(txn_sm->q_current_handler, &state_start); contp = TSContCreate(main_handler, txn_sm->q_mutex); TSContDataSet(contp, txn_sm); return contp; }
TSRemapStatus TSRemapDoRemap(void *ih, TSHttpTxn rh, TSRemapRequestInfo * rri) { int ret; uint64_t req_id; TSCont contp; lua_State *l; ts_lua_main_ctx *main_ctx; ts_lua_http_ctx *http_ctx; ts_lua_instance_conf *instance_conf; instance_conf = (ts_lua_instance_conf *) ih; req_id = __sync_fetch_and_add(&ts_lua_http_next_id, 1); main_ctx = &ts_lua_main_ctx_array[req_id % TS_LUA_MAX_STATE_COUNT]; TSMutexLock(main_ctx->mutexp); http_ctx = ts_lua_create_http_ctx(main_ctx, instance_conf); http_ctx->txnp = rh; http_ctx->client_request_bufp = rri->requestBufp; http_ctx->client_request_hdrp = rri->requestHdrp; http_ctx->client_request_url = rri->requestUrl; http_ctx->remap = 1; l = http_ctx->lua; lua_getglobal(l, TS_LUA_FUNCTION_REMAP); if (lua_type(l, -1) != LUA_TFUNCTION) { TSMutexUnlock(main_ctx->mutexp); return TSREMAP_NO_REMAP; } contp = TSContCreate(ts_lua_http_cont_handler, NULL); TSContDataSet(contp, http_ctx); http_ctx->main_contp = contp; if (lua_pcall(l, 0, 1, 0) != 0) { fprintf(stderr, "lua_pcall failed: %s\n", lua_tostring(l, -1)); } ret = lua_tointeger(l, -1); lua_pop(l, 1); TSHttpTxnHookAdd(rh, TS_HTTP_TXN_CLOSE_HOOK, contp); TSMutexUnlock(main_ctx->mutexp); return ret; }
void TSPluginInit(int argc, const char *argv[]) { const char prefix[] = "http://"; int uri_len; TSPluginRegistrationInfo info; info.plugin_name = PLUGIN_NAME; info.vendor_name = "Apache Software Foundation"; info.support_email = "*****@*****.**"; if (TSPluginRegister(&info) != TS_SUCCESS) { TSError("[%s] Plugin registration failed", PLUGIN_NAME); } if (argc == 3) { block_ip = TSstrdup(argv[1]); /* * The Location header must contain an absolute URI: */ url_redirect = TSstrdup(argv[2]); uri_len = strlen(prefix) + strlen(url_redirect) + 1; uri_redirect = TSmalloc(uri_len); TSstrlcpy(uri_redirect, prefix, uri_len); TSstrlcat(uri_redirect, url_redirect, uri_len); } else { TSError("[%s] Incorrect syntax in plugin.conf: correct usage is", PLUGIN_NAME "redirect_1.so ip_deny url_redirect"); return; } ip_deny = inet_addr(block_ip); TSDebug(PLUGIN_NAME, "initializing stats"); init_stats(); TSHttpHookAdd(TS_HTTP_READ_REQUEST_HDR_HOOK, TSContCreate(redirect_plugin, NULL)); TSDebug(PLUGIN_NAME ".init", "block_ip is %s, url_redirect is %s, and uri_redirect is %s", block_ip, url_redirect, uri_redirect); /* * Demonstrate another tracing function. This can be used to * enable debug calculations and other work that should only * be done in debug mode. */ if (TSIsDebugTagSet(PLUGIN_NAME ".demo")) { TSDebug(PLUGIN_NAME ".init", "The redirect_demo tag is set"); } else { TSDebug(PLUGIN_NAME ".init", "The redirect_demo tag is not set"); } }
static void load_config_file(config_holder_t *config_holder) { TSFile fh; struct stat s; config_t *newconfig, *oldconfig; TSCont free_cont; configReloadRequests++; lastReloadRequest = time(NULL); // check date if (stat(config_holder->config_path, &s) < 0) { TSDebug(PLUGIN_TAG, "Could not stat %s", config_holder->config_path); if(config_holder->config) { return; } } else { TSDebug(PLUGIN_TAG, "s.st_mtime=%lu, last_load=%lu", s.st_mtime, config_holder->last_load); if (s.st_mtime < config_holder->last_load) { return; } } TSDebug(PLUGIN_TAG, "Opening config file: %s", config_holder->config_path); fh = TSfopen(config_holder->config_path, "r"); if (!fh) { TSError("[%s] Unable to open config: %s.\n", PLUGIN_TAG, config_holder->config_path); if(config_holder->config) { return; } } newconfig = 0; newconfig = new_config(fh); if(newconfig) { configReloads++; lastReload = lastReloadRequest; config_holder->last_load = lastReloadRequest; config_t ** confp = &(config_holder->config); oldconfig = __sync_lock_test_and_set(confp, newconfig); if (oldconfig) { TSDebug(PLUGIN_TAG, "scheduling free: %p (%p)", oldconfig, newconfig); free_cont = TSContCreate(free_handler, NULL); TSContDataSet(free_cont, (void *) oldconfig); TSContSchedule(free_cont, FREE_TMOUT, TS_THREAD_POOL_TASK); } } if(fh) TSfclose(fh); return; }
static int stats_origin(TSCont contp ATS_UNUSED, TSEvent event ATS_UNUSED, void *edata) { TSCont icontp; stats_state *my_state; TSHttpTxn txnp = (TSHttpTxn)edata; TSMBuffer reqp; TSMLoc hdr_loc = NULL, url_loc = NULL; TSEvent reenable = TS_EVENT_HTTP_CONTINUE; TSDebug(PLUGIN_NAME, "in the read stuff"); if (TSHttpTxnClientReqGet(txnp, &reqp, &hdr_loc) != TS_SUCCESS) { goto cleanup; } if (TSHttpHdrUrlGet(reqp, hdr_loc, &url_loc) != TS_SUCCESS) { goto cleanup; } int path_len = 0; const char *path = TSUrlPathGet(reqp, url_loc, &path_len); TSDebug(PLUGIN_NAME, "Path: %.*s", path_len, path); if (!(path_len != 0 && path_len == url_path_len && !memcmp(path, url_path, url_path_len))) { goto notforme; } TSSkipRemappingSet(txnp, 1); // not strictly necessary, but speed is everything these days /* This is us -- register our intercept */ TSDebug(PLUGIN_NAME, "Intercepting request"); icontp = TSContCreate(stats_dostuff, TSMutexCreate()); my_state = (stats_state *)TSmalloc(sizeof(*my_state)); memset(my_state, 0, sizeof(*my_state)); TSContDataSet(icontp, my_state); TSHttpTxnIntercept(icontp, txnp); goto cleanup; notforme: cleanup: if (url_loc) { TSHandleMLocRelease(reqp, hdr_loc, url_loc); } if (hdr_loc) { TSHandleMLocRelease(reqp, TS_NULL_MLOC, hdr_loc); } TSHttpTxnReenable(txnp, reenable); return 0; }
/* Initialize the plugin / global continuation hook */ void TSPluginInit(int argc, const char *argv[]) { TSPluginRegistrationInfo info; const char *proof = "acme"; static const struct option longopt[] = { {(char *)"proof-directory", optional_argument, NULL, 'p'}, {NULL, no_argument, NULL, '\0'}, }; memset(&gConfig, 0, sizeof(gConfig)); while (true) { int opt = getopt_long(argc, (char *const *)argv, "", longopt, NULL); switch (opt) { case 'p': proof = optarg; break; } if (opt == -1) { break; } } if ('/' != *proof) { const char *confdir = TSConfigDirGet(); int len = strlen(proof) + strlen(confdir) + 8; gConfig.proof = TSmalloc(len); snprintf(gConfig.proof, len, "%s/%s", confdir, proof); TSDebug(PLUGIN_NAME, "base directory for proof-types is %s", gConfig.proof); } else { gConfig.proof = TSstrdup(proof); } info.plugin_name = "acme"; info.vendor_name = "Apache Software Foundation"; info.support_email = "*****@*****.**"; if (TS_SUCCESS != TSPluginRegister(&info)) { TSError("[%s] Plugin registration failed", PLUGIN_NAME); return; } TSDebug(PLUGIN_NAME, "Started the %s plugin", PLUGIN_NAME); TSDebug(PLUGIN_NAME, "\tproof-type dir = %s", gConfig.proof); TSHttpHookAdd(TS_HTTP_READ_REQUEST_HDR_HOOK, TSContCreate(acme_hook, NULL)); }
static int ts_lua_schedule(lua_State *L) { int sec; int type; int entry; ts_lua_http_ctx *actx; int n; TSCont contp; ts_lua_cont_info *ci; ts_lua_cont_info *nci; ci = ts_lua_get_cont_info(L); if (ci == NULL) return 0; entry = lua_tointeger(L, 1); sec = luaL_checknumber(L, 2); if (sec < 1) { sec = 0; } type = lua_type(L, 3); if (type != LUA_TFUNCTION) return 0; n = lua_gettop(L); if (n < 3) { TSError("[ts_lua] ts.http.schedule need at least three params"); return 0; } // TO-DO unset the original context in L actx = ts_lua_create_async_ctx(L, ci, n); contp = TSContCreate(ts_lua_schedule_handler, ci->mutex); TSContDataSet(contp, actx); nci = &actx->cinfo; nci->contp = contp; nci->mutex = ci->mutex; TSContSchedule(contp, sec * 1000, entry); return 0; }
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); }
void TSPluginInit(int argc, const char *argv[]) { TSPluginRegistrationInfo info; static const char usage[] = PLUGIN_NAME ".so [--integer-counters] [PATH]"; static const struct option longopts[] = {{(char *)("integer-counters"), no_argument, NULL, 'i'}, {(char *)("wrap-counters"), no_argument, NULL, 'w'}, {NULL, 0, NULL, 0}}; info.plugin_name = PLUGIN_NAME; info.vendor_name = "Apache Software Foundation"; info.support_email = "*****@*****.**"; if (TSPluginRegister(&info) != TS_SUCCESS) { TSError("[%s] registration failed", PLUGIN_NAME); } for (;;) { switch (getopt_long(argc, (char *const *)argv, "iw", longopts, NULL)) { case 'i': integer_counters = true; break; case 'w': wrap_counters = true; break; case -1: goto init; default: TSError("[%s] usage: %s", PLUGIN_NAME, usage); } } init: argc -= optind; argv += optind; if (argc > 0) { url_path = TSstrdup(argv[0] + ('/' == argv[0][0] ? 1 : 0)); /* Skip leading / */ } url_path_len = strlen(url_path); /* Create a continuation with a mutex as there is a shared global structure containing the headers to add */ TSHttpHookAdd(TS_HTTP_READ_REQUEST_HDR_HOOK, TSContCreate(stats_origin, NULL)); TSDebug(PLUGIN_NAME, "stats module registered"); }
/* Read-request header continuation, used to kick off the server intercept if necessary */ static int health_check_origin(TSCont contp, TSEvent event, void *edata) { TSMBuffer reqp; TSMLoc hdr_loc = NULL, url_loc = NULL; TSCont icontp; HCState *my_state; TSHttpTxn txnp = (TSHttpTxn) edata; HCFileInfo *info = g_config; if ((TS_SUCCESS == TSHttpTxnClientReqGet(txnp, &reqp, &hdr_loc)) && (TS_SUCCESS == TSHttpHdrUrlGet(reqp, hdr_loc, &url_loc))) { int path_len = 0; const char* path = TSUrlPathGet(reqp, url_loc, &path_len); while (info) { if (info->p_len == path_len && !memcmp(info->path, path, path_len)) break; info = info->_next; } if (!info) goto cleanup; TSSkipRemappingSet(txnp, 1); /* not strictly necessary, but speed is everything these days */ /* This is us -- register our intercept */ icontp = TSContCreate(hc_intercept, TSMutexCreate()); my_state = (HCState *)TSmalloc(sizeof(*my_state)); memset(my_state, 0, sizeof(*my_state)); my_state->info = info; my_state->data = info->data; TSContDataSet(icontp, my_state); TSHttpTxnIntercept(icontp, txnp); } cleanup: if (url_loc) TSHandleMLocRelease(reqp, hdr_loc, url_loc); if (hdr_loc) TSHandleMLocRelease(reqp, TS_NULL_MLOC, hdr_loc); TSHttpTxnReenable(txnp, TS_EVENT_HTTP_CONTINUE); return 0; }
void TSPluginInit(int argc, const char *argv[]) { TSPluginRegistrationInfo info; info.plugin_name = PLUGIN_NAME; info.vendor_name = "Apache Software Foundation"; info.support_email = "*****@*****.**"; if (TSPluginRegister(&info) != TS_SUCCESS) { TSError(PCP "Plugin registration failed.\n"); return; } TSHttpHookAdd(TS_HTTP_READ_RESPONSE_HDR_HOOK, TSContCreate(main_hook, NULL)); return; }
/*------------------------------------------------------------------------- TSPluginInit Function called at plugin init time Input: argc number of args argv list vof args Output : Return Value: -------------------------------------------------------------------------*/ void TSPluginInit(int argc, const char *argv[]) { TSPluginRegistrationInfo info; int i; TSReturnCode retval; info.plugin_name = "psi"; info.vendor_name = "Apache"; info.support_email = ""; if (TSPluginRegister(TS_SDK_VERSION_3_0, &info) != TS_SUCCESS) { TSError("Plugin registration failed.\n"); } if (!check_ts_version()) { TSError("Plugin requires Traffic Server 3.0 or later\n"); return; } /* Initialize the psi directory = <plugin_path>/include */ sprintf(psi_directory, "%s/%s", TSPluginDirGet(), PSI_PATH); /* create an TSTextLogObject to log any psi include */ retval = TSTextLogObjectCreate("psi", TS_LOG_MODE_ADD_TIMESTAMP, &log); if (retval == TS_ERROR) { TSError("Failed creating log for psi plugin"); log = NULL; } /* Create working threads */ thread_init(); init_queue(&job_queue); for (i = 0; i < NB_THREADS; i++) { char *thread_name = (char *) TSmalloc(64); sprintf(thread_name, "Thread[%d]", i); if (!TSThreadCreate((TSThreadFunc) thread_loop, thread_name)) { TSError("[TSPluginInit] Error while creating threads"); return; } } TSHttpHookAdd(TS_HTTP_READ_RESPONSE_HDR_HOOK, TSContCreate(read_response_handler, TSMutexCreate())); TSDebug(DBG_TAG, "Plugin started"); }
/* Read-request header continuation, used to kick off the server intercept if necessary */ static int acme_hook(TSCont contp ATS_UNUSED, TSEvent event ATS_UNUSED, void *edata) { TSMBuffer reqp; TSMLoc hdr_loc = NULL, url_loc = NULL; TSCont icontp; AcmeState *my_state; TSHttpTxn txnp = (TSHttpTxn)edata; TSDebug(PLUGIN_NAME, "kicking off ACME hook"); if ((TS_SUCCESS == TSHttpTxnClientReqGet(txnp, &reqp, &hdr_loc)) && (TS_SUCCESS == TSHttpHdrUrlGet(reqp, hdr_loc, &url_loc))) { int path_len = 0; const char *path = TSUrlPathGet(reqp, url_loc, &path_len); /* Short circuit the / path, common case */ if (!path || path_len < (int)(strlen(ACME_WK_PATH) + 2) || *path != '.' || memcmp(path, ACME_WK_PATH, strlen(ACME_WK_PATH))) { TSDebug(PLUGIN_NAME, "skipping URL path = %.*s", path_len, path); goto cleanup; } TSSkipRemappingSet(txnp, 1); /* not strictly necessary, but speed is everything these days */ /* This request is for us -- register our intercept */ icontp = TSContCreate(acme_intercept, TSMutexCreate()); my_state = make_acme_state(); open_acme_file(my_state, path + strlen(ACME_WK_PATH), path_len - strlen(ACME_WK_PATH)); TSContDataSet(icontp, my_state); TSHttpTxnIntercept(icontp, txnp); TSDebug(PLUGIN_NAME, "created intercept hook"); } cleanup: if (url_loc) { TSHandleMLocRelease(reqp, hdr_loc, url_loc); } if (hdr_loc) { TSHandleMLocRelease(reqp, TS_NULL_MLOC, hdr_loc); } TSHttpTxnReenable(txnp, TS_EVENT_HTTP_CONTINUE); return 0; }
static void handle_txn_start(TSCont contp ATS_UNUSED, TSHttpTxn txnp) { TSCont txn_contp; cdata *cd; txn_contp = TSContCreate((TSEventFunc)blacklist_plugin, TSMutexCreate()); /* create the data that'll be associated with the continuation */ cd = (cdata *)TSmalloc(sizeof(cdata)); TSContDataSet(txn_contp, cd); cd->txnp = txnp; TSHttpTxnHookAdd(txnp, TS_HTTP_OS_DNS_HOOK, txn_contp); TSHttpTxnHookAdd(txnp, TS_HTTP_TXN_CLOSE_HOOK, txn_contp); TSHttpTxnReenable(txnp, TS_EVENT_HTTP_CONTINUE); }
static int config_handler(TSCont cont, TSEvent event, void *edata) { plugin_state_t *pstate; invalidate_t *i, *iptr; TSCont free_cont; bool updated; TSMutex mutex; mutex = TSContMutexGet(cont); TSMutexLock(mutex); TSDebug(LOG_PREFIX, "In config Handler"); pstate = (plugin_state_t *)TSContDataGet(cont); i = copy_config(pstate->invalidate_list); updated = prune_config(&i); updated = load_config(pstate, &i) || updated; if (updated) { list_config(pstate, i); iptr = __sync_val_compare_and_swap(&(pstate->invalidate_list), pstate->invalidate_list, i); if (iptr) { free_cont = TSContCreate(free_handler, TSMutexCreate()); TSContDataSet(free_cont, (void *)iptr); TSContScheduleOnPool(free_cont, FREE_TMOUT, TS_THREAD_POOL_TASK); } } else { TSDebug(LOG_PREFIX, "No Changes"); if (i) { free_invalidate_t_list(i); } } TSMutexUnlock(mutex); // Don't reschedule for TS_EVENT_MGMT_UPDATE if (event == TS_EVENT_TIMEOUT) { TSContScheduleOnPool(cont, CONFIG_TMOUT, TS_THREAD_POOL_TASK); } return 0; }
/* Initialize the plugin / global continuation hook */ void TSPluginInit(int argc, const char *argv[]) { TSPluginRegistrationInfo info; if (2 != argc) { TSError("Must specify a configuration file.\n"); return; } info.plugin_name = "health_checks"; info.vendor_name = "Apache Software Foundation"; info.support_email = "*****@*****.**"; if (TS_SUCCESS != TSPluginRegister(TS_SDK_VERSION_3_0, &info)) { TSError("Plugin registration failed. \n"); return; } if (!check_ts_version()) { TSError("Plugin requires Traffic Server 3.0 or later\n"); return; } /* This will update the global configuration file, and is not reloaded at run time */ /* ToDo: Support reloading with traffic_line -x ? */ if (NULL == (g_config = parse_configs(argv[1]))) { TSError("Unable to read / parse %s config file", argv[1]); return; } /* Setup the background thread */ if (!TSThreadCreate(hc_thread, NULL)) { TSError("Failure in thread creation"); return; } /* Create a continuation with a mutex as there is a shared global structure containing the headers to add */ TSDebug(PLUGIN_NAME, "Started %s plugin", PLUGIN_NAME); TSHttpHookAdd(TS_HTTP_READ_REQUEST_HDR_HOOK, TSContCreate(health_check_origin, NULL)); }