static int ts_lua_client_request_set_url_host(lua_State *L) { const char *host; size_t len; ts_lua_http_ctx *http_ctx; http_ctx = ts_lua_get_http_ctx(L); host = luaL_checklstring(L, 1, &len); TSUrlHostSet(http_ctx->client_request_bufp, http_ctx->client_request_url, host, len); return 0; }
static int ts_lua_server_request_set_url_host(lua_State *L) { const char *host; size_t len; ts_lua_http_ctx *http_ctx; GET_HTTP_CONTEXT(http_ctx, L); TS_LUA_CHECK_SERVER_REQUEST_URL(http_ctx); host = luaL_checklstring(L, 1, &len); TSUrlHostSet(http_ctx->server_request_bufp, http_ctx->server_request_url, host, len); return 0; }
TSRemapStatus TSRemapDoRemap(void* ih, TSHttpTxn rh, TSRemapRequestInfo *rri) { hash_remap_struct *hash = (hash_remap_struct*) ih; if (rri == NULL || ih == NULL) { TSError(PLUGIN_NAME "NULL pointer for rri or ih"); return TSREMAP_NO_REMAP; } int req_host_len; int req_path_len; const char* req_host = TSUrlHostGet(rri->requestBufp, rri->requestUrl, &req_host_len); const char* req_path = TSUrlPathGet(rri->requestBufp, rri->requestUrl, &req_path_len); uint32_t hash_value_host = hash_fnv32((char *) req_host, req_host_len); uint64_t hash_value_host_and_path = hash_fnv64((char *) req_host, req_host_len); hash_fnv64_continue((char *) req_path, req_path_len, &hash_value_host_and_path); memset(hash->hash, 0, hash->hash_len); int len = sprintf(hash->hash, "%lx%x.%.*s", hash_value_host_and_path, hash_value_host, hash->isp_name_len, hash->isp_name); if (len > hash->hash_len) { TSError(PLUGIN_NAME "Memory probably corrupt :whistle:"); return TSREMAP_NO_REMAP; } if (TSUrlHostSet(rri->requestBufp, rri->requestUrl, hash->hash, len) != TS_SUCCESS) { /* the request was not modified, TS will use the toURL from the remap rule */ TSError(PLUGIN_NAME "Failed to modify the Host in request URL"); return TSREMAP_NO_REMAP; } TSDebug(PLUGIN_NAME, "host changed from [%.*s] to [%.*s]", req_host_len, req_host, len, hash->hash); // fprintf(stderr, "host changed from [%.*s] to [%.*s/%.*s]\n", req_host_len, req_host, len, hash->hash, req_path_len, req_path); return TSREMAP_DID_REMAP; /* host has been modified */ }