static RequestInfo * create_request_info(TSHttpTxn txn) { RequestInfo *req_info; char *url; int url_len; TSMBuffer buf; TSMLoc loc; req_info = (RequestInfo *)TSmalloc(sizeof(RequestInfo)); url = TSHttpTxnEffectiveUrlStringGet(txn, &url_len); req_info->effective_url = TSstrndup(url, url_len); TSfree(url); TSHttpTxnClientReqGet(txn, &buf, &loc); req_info->buf = TSMBufferCreate(); TSHttpHdrClone(req_info->buf, buf, loc, &(req_info->http_hdr_loc)); TSHandleMLocRelease(buf, TS_NULL_MLOC, loc); req_info->client_addr = TSmalloc(sizeof(struct sockaddr)); memmove((void *)req_info->client_addr, (void *)TSHttpTxnClientAddrGet(txn), sizeof(struct sockaddr)); return req_info; }
/* handle_hook * Fires on TS_EVENT_HTTP_READ_REQUEST_HDR events, gets the effectiveUrl * finds the host, gets the generation ID, gen_id, for the host * and runs TSCacheUrlSet to change the cache key for the read */ static int handle_hook(TSCont *contp, TSEvent event, void *edata) { TSHttpTxn txnp = (TSHttpTxn)edata; char *url = NULL, *host = NULL; int url_length; int gen_id; int ok = 1; switch (event) { case TS_EVENT_HTTP_READ_REQUEST_HDR: TSDebug(PLUGIN_NAME, "handling TS_EVENT_HTTP_READ_REQUEST_HDR"); if (ok) { url = TSHttpTxnEffectiveUrlStringGet(txnp, &url_length); if (!url) { TSError("[%s] could not retrieve request url", PLUGIN_NAME); ok = 0; } } if (ok) { get_genid_host(&host, url); if (!host) { TSError("[%s] could not retrieve request host", PLUGIN_NAME); ok = 0; } } if (ok) { TSDebug(PLUGIN_NAME, "From url (%s) discovered host (%s)", url, host); if ((gen_id = get_genid(host)) != 0) { if (TSHttpTxnConfigIntSet(txnp, TS_CONFIG_HTTP_CACHE_GENERATION, gen_id) != TS_SUCCESS) { TSDebug(PLUGIN_NAME, "Error, unable to modify cache url"); TSError("[%s] Unable to set cache generation for %s to %d", PLUGIN_NAME, url, gen_id); ok = 0; } } } /* Clean up */ if (url) { TSfree(url); } if (host) { TSfree(host); } TSHttpTxnReenable(txnp, TS_EVENT_HTTP_CONTINUE); break; default: TSAssert(!"Unexpected event"); ok = 0; break; } return ok; }
static int main_handler(TSCont cont, TSEvent event, void *edata) { TSHttpTxn txn = (TSHttpTxn)edata; int status; invalidate_t *iptr; plugin_state_t *pstate; time_t date = 0, now = 0; char *url = NULL; int url_len = 0; switch (event) { case TS_EVENT_HTTP_CACHE_LOOKUP_COMPLETE: if (TSHttpTxnCacheLookupStatusGet(txn, &status) == TS_SUCCESS) { if (status == TS_CACHE_LOOKUP_HIT_FRESH) { pstate = (plugin_state_t *)TSContDataGet(cont); iptr = pstate->invalidate_list; while (iptr) { if (!date) { date = get_date_from_cached_hdr(txn); now = time(NULL); } if ((difftime(iptr->epoch, date) >= 0) && (difftime(iptr->expiry, now) >= 0)) { if (!url) { url = TSHttpTxnEffectiveUrlStringGet(txn, &url_len); } if (pcre_exec(iptr->regex, iptr->regex_extra, url, url_len, 0, 0, NULL, 0) >= 0) { TSHttpTxnCacheLookupStatusSet(txn, TS_CACHE_LOOKUP_HIT_STALE); iptr = NULL; TSDebug(LOG_PREFIX, "Forced revalidate - %.*s", url_len, url); } } if (iptr) { iptr = iptr->next; } } if (url) { TSfree(url); } } } break; default: break; } TSHttpTxnReenable(txn, TS_EVENT_HTTP_CONTINUE); return 0; }
static int rewrite_cacheurl(pr_list *prl, TSHttpTxn txnp) { int ok = 1; char *newurl = 0; int retval; char *url; int url_length; int i; if (ok) { url = TSHttpTxnEffectiveUrlStringGet(txnp, &url_length); if (!url) { TSError("[%s] couldn't retrieve request url\n", PLUGIN_NAME); ok = 0; } } if (ok) { i=0; while (i < prl->patterncount && prl->pr[i]) { retval = regex_substitute(&newurl, url, prl->pr[i]); if (retval) { /* Successful match/substitution */ break; } i++; } if (newurl) { if (log) { TSTextLogObjectWrite(log, "Rewriting cache URL for %s to %s", url, newurl); } TSDebug(PLUGIN_NAME, "Rewriting cache URL for %s to %s\n", url, newurl); if (TSCacheUrlSet(txnp, newurl, strlen(newurl)) != TS_SUCCESS) { TSError("[%s] Unable to modify cache url from " "%s to %s\n", PLUGIN_NAME, url, newurl); ok = 0; } } } /* Clean up */ if (url) TSfree(url); if (newurl) TSfree(newurl); return ok; }
static char * get_effective_host(TSHttpTxn txn) { char *effective_url, *tmp; const char *host; int len; TSMBuffer buf; TSMLoc url_loc; effective_url = TSHttpTxnEffectiveUrlStringGet(txn, &len); buf = TSMBufferCreate(); TSUrlCreate(buf, &url_loc); tmp = effective_url; TSUrlParse(buf, url_loc, (const char **) (&tmp), (const char *) (effective_url + len)); TSfree(effective_url); host = TSUrlHostGet(buf, url_loc, &len); tmp = TSstrndup(host, len); TSHandleMLocRelease(buf, TS_NULL_MLOC, url_loc); TSMBufferDestroy(buf); return tmp; }
static RequestInfo * create_request_info(TSHttpTxn txn) { RequestInfo *req_info; char *url; int url_len; TSMBuffer buf; TSMLoc loc; const struct sockaddr *sa; req_info = (RequestInfo *)TSmalloc(sizeof(RequestInfo)); memset(req_info, 0, sizeof(RequestInfo)); url = TSHttpTxnEffectiveUrlStringGet(txn, &url_len); req_info->effective_url = TSstrndup(url, url_len); TSfree(url); TSHttpTxnClientReqGet(txn, &buf, &loc); req_info->buf = TSMBufferCreate(); TSHttpHdrClone(req_info->buf, buf, loc, &(req_info->http_hdr_loc)); TSHandleMLocRelease(buf, TS_NULL_MLOC, loc); sa = TSHttpTxnClientAddrGet(txn); switch (sa->sa_family) { case AF_INET: memcpy(&req_info->client_addr.sin, sa, sizeof(struct sockaddr_in)); break; case AF_INET6: memcpy(&req_info->client_addr.sin6, sa, sizeof(struct sockaddr_in6)); break; default: break; } return req_info; }