static int ts_lua_client_request_get_pristine_url(lua_State *L) { char *url; int url_len; TSMBuffer bufp; TSMLoc url_loc; ts_lua_http_ctx *http_ctx; http_ctx = ts_lua_get_http_ctx(L); if (TSHttpTxnPristineUrlGet(http_ctx->txnp, &bufp, &url_loc) != TS_SUCCESS) return 0; url = TSUrlStringGet(bufp, url_loc, &url_len); if (url) { lua_pushlstring(L, url, url_len); TSfree(url); } else { lua_pushnil(L); } TSHandleMLocRelease(bufp, NULL, url_loc); return 1; }
static int ts_lua_http_get_remap_to_url(lua_State *L) { TSMLoc url = TS_NULL_MLOC; char *str = NULL; int len; ts_lua_http_ctx *http_ctx; GET_HTTP_CONTEXT(http_ctx, L); if (TSRemapToUrlGet(http_ctx->txnp, &url) != TS_SUCCESS) { lua_pushnil(L); goto done; } str = TSUrlStringGet(NULL, url, &len); lua_pushlstring(L, str, len >= TS_LUA_MAX_URL_LENGTH ? TS_LUA_MAX_URL_LENGTH - 1 : len); done: if (str != NULL) { TSfree(str); } return 1; }
static int ts_lua_remap_get_to_url(lua_State *L) { char output[TS_LUA_MAX_URL_LENGTH]; char *url; int url_len; int output_len; ts_lua_http_ctx *http_ctx; GET_HTTP_CONTEXT(http_ctx, L); if (http_ctx->rri != NULL) { url = TSUrlStringGet(http_ctx->client_request_bufp, http_ctx->rri->mapToUrl, &url_len); output_len = snprintf(output, TS_LUA_MAX_URL_LENGTH, "%.*s", url_len, url); if (output_len >= TS_LUA_MAX_URL_LENGTH) { lua_pushlstring(L, output, TS_LUA_MAX_URL_LENGTH - 1); } else { lua_pushlstring(L, output, output_len); } TSfree(url); } else { lua_pushnil(L); } return 1; }
static int ts_lua_client_request_get_url(lua_State *L) { const char *url; int url_len; ts_lua_http_ctx *http_ctx; http_ctx = ts_lua_get_http_ctx(L); url = TSUrlStringGet(http_ctx->client_request_bufp, http_ctx->client_request_url, &url_len); lua_pushlstring(L, url, url_len); return 1; }
static int regex_match(TSCont contp, TSHttpTxn txnp) { static char *url; TSMBuffer bufp; TSMLoc hdr_loc, url_loc; int url_length; regex_t *re; int ovector[30], res; if (TSHttpTxnClientReqGet(txnp, &bufp, &hdr_loc) != TS_SUCCESS) { TSError("couldn't retrieve client response header\n"); goto done; } if (TSHttpHdrUrlGet(bufp, hdr_loc, &url_loc) != TS_SUCCESS) { TSError("couldn't retrieve request url\n"); TSHandleMLocRelease(bufp, TS_NULL_MLOC, hdr_loc); goto done; } url = TSUrlStringGet(bufp, url_loc, &url_length); if (url == NULL) { TSError("couldn't get url"); goto done; } for (re = reg; re; re = re->next) { res = pcre_exec(re->re, NULL, url, strlen(url), 0, 0, ovector, 30); if (res <= 0) { goto done; } break; } TSContDataSet(contp, re->hdr); TSHttpTxnHookAdd(txnp, TS_HTTP_SEND_RESPONSE_HDR_HOOK, contp); done: TSfree(url); TSHandleMLocRelease(bufp, hdr_loc, url_loc); TSHandleMLocRelease(bufp, TS_NULL_MLOC, hdr_loc); TSHttpTxnReenable(txnp, TS_EVENT_HTTP_CONTINUE); return 0; }
static void handle_response(TSHttpTxn txnp, TSCont contp ATS_UNUSED) { TSMBuffer bufp; TSMLoc hdr_loc; TSMLoc url_loc; char *url_str; char *buf; int url_length; if (TSHttpTxnClientRespGet(txnp, &bufp, &hdr_loc) != TS_SUCCESS) { TSError("[%s] Couldn't retrieve client response header", PLUGIN_NAME); goto done; } TSHttpHdrStatusSet(bufp, hdr_loc, TS_HTTP_STATUS_FORBIDDEN); TSHttpHdrReasonSet(bufp, hdr_loc, TSHttpHdrReasonLookup(TS_HTTP_STATUS_FORBIDDEN), strlen(TSHttpHdrReasonLookup(TS_HTTP_STATUS_FORBIDDEN))); if (TSHttpTxnClientReqGet(txnp, &bufp, &hdr_loc) != TS_SUCCESS) { TSError("[%s] Couldn't retrieve client request header", PLUGIN_NAME); TSHandleMLocRelease(bufp, TS_NULL_MLOC, hdr_loc); goto done; } if (TSHttpHdrUrlGet(bufp, hdr_loc, &url_loc) != TS_SUCCESS) { TSError("[%s] Couldn't retrieve request url", PLUGIN_NAME); TSHandleMLocRelease(bufp, TS_NULL_MLOC, hdr_loc); goto done; } buf = (char *)TSmalloc(4096); url_str = TSUrlStringGet(bufp, url_loc, &url_length); sprintf(buf, "You are forbidden from accessing \"%s\"\n", url_str); TSfree(url_str); TSHandleMLocRelease(bufp, hdr_loc, url_loc); TSHandleMLocRelease(bufp, TS_NULL_MLOC, hdr_loc); TSHttpTxnErrorBodySet(txnp, buf, strlen(buf), NULL); done: TSHttpTxnReenable(txnp, TS_EVENT_HTTP_CONTINUE); }
static int ts_lua_http_get_cache_lookup_url(lua_State *L) { char output[TS_LUA_MAX_URL_LENGTH]; int output_len; TSMLoc url = TS_NULL_MLOC; char *str = NULL; int len; ts_lua_http_ctx *http_ctx; GET_HTTP_CONTEXT(http_ctx, L); if (TSUrlCreate(http_ctx->client_request_bufp, &url) != TS_SUCCESS) { lua_pushnil(L); goto done; } if (TSHttpTxnCacheLookupUrlGet(http_ctx->txnp, http_ctx->client_request_bufp, url) != TS_SUCCESS) { lua_pushnil(L); goto done; } str = TSUrlStringGet(http_ctx->client_request_bufp, url, &len); output_len = snprintf(output, TS_LUA_MAX_URL_LENGTH, "%.*s", len, str); if (output_len >= TS_LUA_MAX_URL_LENGTH) { lua_pushlstring(L, output, TS_LUA_MAX_URL_LENGTH - 1); } else { lua_pushlstring(L, output, output_len); } done: if (url != TS_NULL_MLOC) { TSHandleMLocRelease(http_ctx->client_request_bufp, TS_NULL_MLOC, url); } if (str != NULL) { TSfree(str); } return 1; }
static int ts_lua_client_request_get_url(lua_State *L) { char *url; int url_len; ts_lua_http_ctx *http_ctx; http_ctx = ts_lua_get_http_ctx(L); url = TSUrlStringGet(http_ctx->client_request_bufp, http_ctx->client_request_url, &url_len); if (url) { lua_pushlstring(L, url, url_len); TSfree(url); } else { lua_pushnil(L); } return 1; }
static int ts_lua_http_get_parent_selection_url(lua_State *L) { TSMLoc url = TS_NULL_MLOC; char *str = NULL; int len; ts_lua_http_ctx *http_ctx; GET_HTTP_CONTEXT(http_ctx, L); if (TSUrlCreate(http_ctx->client_request_bufp, &url) != TS_SUCCESS) { lua_pushnil(L); goto done; } if (TSHttpTxnParentSelectionUrlGet(http_ctx->txnp, http_ctx->client_request_bufp, url) != TS_SUCCESS) { lua_pushnil(L); goto done; } str = TSUrlStringGet(http_ctx->client_request_bufp, url, &len); lua_pushlstring(L, str, len >= TS_LUA_MAX_URL_LENGTH ? TS_LUA_MAX_URL_LENGTH - 1 : len); done: if (url != TS_NULL_MLOC) { TSHandleMLocRelease(http_ctx->client_request_bufp, TS_NULL_MLOC, url); } if (str != NULL) { TSfree(str); } return 1; }
TSRemapStatus TSRemapDoRemap(void *ih, TSHttpTxn rh, TSRemapRequestInfo* rri) { int i, len; time_t t, e; MD5_CTX ctx; struct sockaddr_in *in; const char *qh, *ph, *ip; unsigned char md[MD5_DIGEST_LENGTH]; secure_link_info *sli = (secure_link_info *)ih; char *token = NULL, *expire = NULL, *path = NULL; char *s, *ptr, *saveptr = NULL, *val, hash[32] = ""; in = (struct sockaddr_in *)TSHttpTxnClientAddrGet(rh); ip = inet_ntoa(in->sin_addr); s = TSUrlStringGet(rri->requestBufp, rri->requestUrl, &len); TSDebug(PLUGIN_NAME, "request [%.*s] from [%s]", len, s, ip); TSfree(s); qh = TSUrlHttpQueryGet(rri->requestBufp, rri->requestUrl, &len); if(qh && len > 0) { s = (char *)TSstrndup(qh, len); if((ptr = strtok_r(s, "&", &saveptr)) != NULL) { do { if((val = strchr(ptr, '=')) != NULL) { *val++ = '\0'; if(strcmp(ptr, "st") == 0) { token = TSstrdup(val); } else if(strcmp(ptr, "ex") == 0) { expire = TSstrdup(val); } } else { TSError("Invalid parameter [%s]", ptr); break; } } while((ptr = strtok_r(NULL, "&", &saveptr)) != NULL); } else { TSError("strtok didn't find a & in the query string"); /* this is just example, so set fake params to prevent plugin crash */ token = TSstrdup("d41d8cd98f00b204e9800998ecf8427e"); expire = TSstrdup("00000000"); } TSfree(s); } else { TSError("TSUrlHttpQueryGet returns empty value"); } ph = TSUrlPathGet(rri->requestBufp, rri->requestUrl, &len); if(ph && len > 0) { s = TSstrndup(ph, len); if((ptr = strrchr(s, '/')) != NULL) { *++ptr = '\0'; } path = TSstrdup(s); TSfree(s); } else { TSError("TSUrlPathGet returns empty value"); /* this is just example, so set fake params to prevent plugin crash */ path = TSstrdup("example/"); } MD5_Init(&ctx); MD5_Update(&ctx, sli->secret, strlen(sli->secret)); MD5_Update(&ctx, ip, strlen(ip)); if (path) MD5_Update(&ctx, path, strlen(path)); if (expire) MD5_Update(&ctx, expire, strlen(expire)); MD5_Final(md, &ctx); for(i = 0; i < MD5_DIGEST_LENGTH; i++) { sprintf(&hash[i * 2], "%02x", md[i]); } time(&t); e = strtol(expire, NULL, 16); i = TSREMAP_DID_REMAP; if(e < t || strcmp(hash, token) != 0) { if(e < t) { TSDebug(PLUGIN_NAME, "link expired: [%lu] vs [%lu]", t, e); } else { TSDebug(PLUGIN_NAME, "tokens mismatch: [%s] vs [%s]", hash, token); } if(sli->strict) { TSDebug(PLUGIN_NAME, "request is DENY"); TSHttpTxnSetHttpRetStatus(rh, TS_HTTP_STATUS_FORBIDDEN); i = TSREMAP_NO_REMAP; } else { TSDebug(PLUGIN_NAME, "request is PASS"); } } if(i == TSREMAP_DID_REMAP) { if(TSUrlHttpQuerySet(rri->requestBufp, rri->requestUrl, "", -1) == TS_SUCCESS) { s = TSUrlStringGet(rri->requestBufp, rri->requestUrl, &len); TSDebug(PLUGIN_NAME, "new request string is [%.*s]", len, s); TSfree(s); } else { i = TSREMAP_NO_REMAP; } } TSfree(expire); TSfree(token); TSfree(path); return i; }