static void read_blacklist(TSCont contp) { char blacklist_file[1024]; TSFile file; sprintf(blacklist_file, "%s/blacklist.txt", TSPluginDirGet()); file = TSfopen(blacklist_file, "r"); nsites = 0; /* If the Mutext lock is not successful try again in RETRY_TIME */ if (TSMutexLockTry(sites_mutex) != TS_SUCCESS) { if (file != NULL) { TSfclose(file); } TSContSchedule(contp, RETRY_TIME, TS_THREAD_POOL_DEFAULT); return; } if (file != NULL) { char buffer[1024]; while (TSfgets(file, buffer, sizeof(buffer) - 1) != NULL && nsites < MAX_NSITES) { char *eol; if ((eol = strstr(buffer, "\r\n")) != NULL) { /* To handle newlines on Windows */ *eol = '\0'; } else if ((eol = strchr(buffer, '\n')) != NULL) { *eol = '\0'; } else { /* Not a valid line, skip it */ continue; } if (sites[nsites] != NULL) { TSfree(sites[nsites]); } sites[nsites] = TSstrdup(buffer); nsites++; } TSfclose(file); } else { TSError("[%s] Unable to open %s", PLUGIN_NAME, blacklist_file); TSError("[%s] All sites will be allowed", PLUGIN_NAME); } TSMutexUnlock(sites_mutex); }
/*------------------------------------------------------------------------- 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"); }
static pr_list* load_config_file(const char *config_file) { char buffer[1024]; char default_config_file[1024]; TSFile fh; pr_list *prl = TSmalloc(sizeof(pr_list)); prl->patterncount = 0; /* locations in a config file line, end of line, split start, split end */ char *eol, *spstart, *spend; int lineno = 0; int retval; regex_info *info = 0; if (!config_file) { /* Default config file of plugins/cacheurl.config */ sprintf(default_config_file, "%s/cacheurl.config", TSPluginDirGet()); config_file = (const char *)default_config_file; } TSDebug(PLUGIN_NAME, "Opening config file: %s", config_file); fh = TSfopen(config_file, "r"); if (!fh) { TSError("[%s] Unable to open %s. No patterns will be loaded\n", PLUGIN_NAME, config_file); return prl; } while (TSfgets(fh, buffer, sizeof(buffer) - 1)) { lineno++; if (*buffer == '#') { /* # Comments, only at line beginning */ continue; } eol = strstr(buffer, "\n"); if (eol) { *eol = 0; /* Terminate string at newline */ } else { /* Malformed line - skip */ continue; } /* Split line into two parts based on whitespace */ /* Find first whitespace */ spstart = strstr(buffer, " "); if (!spstart) { spstart = strstr(buffer, "\t"); } if (!spstart) { TSError("[%s] ERROR: Invalid format on line %d. Skipping\n", PLUGIN_NAME, lineno); continue; } /* Find part of the line after any whitespace */ spend = spstart + 1; while(*spend == ' ' || *spend == '\t') { spend++; } if (*spend == 0) { /* We reached the end of the string without any non-whitepace */ TSError("[%s] ERROR: Invalid format on line %d. Skipping\n", PLUGIN_NAME, lineno); continue; } *spstart = 0; /* We have the pattern/replacement, now do precompilation. * buffer is the first part of the line. spend is the second part just * after the whitespace */ if (log) { TSTextLogObjectWrite(log, "Adding pattern/replacement pair: '%s' -> '%s'", buffer, spend); } TSDebug(PLUGIN_NAME, "Adding pattern/replacement pair: '%s' -> '%s'\n", buffer, spend); retval = regex_compile(&info, buffer, spend); if (!retval) { TSError("[%s] Error precompiling regex/replacement. Skipping.\n", PLUGIN_NAME); } // TODO - remove patterncount and make pr_list infinite (linked list) if (prl->patterncount >= PATTERNCOUNT) { TSError("[%s] Warning, too many patterns - skipping the rest" "(max: %d)\n", PLUGIN_NAME, PATTERNCOUNT); TSfree(info); break; } prl->pr[prl->patterncount] = info; prl->patterncount++; } TSfclose(fh); // Make sure the last element is null if (prl->patterncount < PATTERNCOUNT) { prl->pr[prl->patterncount] = NULL; } return prl; }