static int ts_lua_http_set_cache_lookup_url(lua_State *L) { const char *url; size_t url_len; ts_lua_http_ctx *http_ctx; GET_HTTP_CONTEXT(http_ctx, L); url = luaL_checklstring(L, 1, &url_len); if (url && url_len) { const char *start = url; const char *end = url + url_len; TSMLoc new_url_loc; if (TSUrlCreate(http_ctx->client_request_bufp, &new_url_loc) == TS_SUCCESS && TSUrlParse(http_ctx->client_request_bufp, new_url_loc, &start, end) == TS_PARSE_DONE && TSHttpTxnCacheLookupUrlSet(http_ctx->txnp, http_ctx->client_request_bufp, new_url_loc) == TS_SUCCESS) { TSDebug(TS_LUA_DEBUG_TAG, "Set cache lookup URL"); } else { TSError("[ts_lua] Failed to set cache lookup URL"); } } return 0; }
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 TSCacheKey ts_lua_cache_key_create(const char *keystr, size_t key_len, const char *optstr, size_t opt_len) { const char *ptr, *end, *host; char c; int host_len; TSCacheKey key; TSMBuffer urlBuf; TSMLoc urlLoc; int url, http; ptr = optstr; end = optstr + opt_len; url = http = 0; while (ptr < end) { c = *ptr; switch (c) { case 'u': url = 1; break; case 'h': http = 1; break; default: break; } ptr++; } key = TSCacheKeyCreate(); urlBuf = NULL; if (url == 0) { TSCacheKeyDigestSet(key, keystr, key_len); } else { end = keystr + key_len; ptr = memchr(keystr, ':', key_len); if (ptr == NULL) goto fail; ptr += 3; if (ptr >= end) goto fail; host = ptr; ptr = memchr(host, '/', end-host); if (ptr != NULL) { host_len = ptr - host; } else { host_len = end - host; } if (host_len <= 0) goto fail; ptr = memchr(host, ':', host_len); if (ptr != NULL) { host_len = ptr - host; } urlBuf = TSMBufferCreate(); TSUrlCreate(urlBuf, &urlLoc); if (TSUrlParse(urlBuf, urlLoc, (const char **) &keystr, end) != TS_PARSE_DONE || TSCacheKeyDigestFromUrlSet(key, urlLoc) != TS_SUCCESS) { goto fail; } TSHandleMLocRelease(urlBuf, NULL, urlLoc); TSMBufferDestroy(urlBuf); TSCacheKeyHostNameSet(key, host, host_len); } if (http) { TSCacheKeyDataTypeSet(key, TS_CACHE_DATA_TYPE_HTTP); } return key; fail: TSCacheKeyDestroy(key); if (urlBuf) { TSHandleMLocRelease(urlBuf, NULL, urlLoc); TSMBufferDestroy(urlBuf); } return NULL; }