Repository: trafficserver Updated Branches: refs/heads/master 5c404b0d6 -> 4cfbfdfcf
TS-3856: add api support for ts_lua plugin Project: http://git-wip-us.apache.org/repos/asf/trafficserver/repo Commit: http://git-wip-us.apache.org/repos/asf/trafficserver/commit/4cfbfdfc Tree: http://git-wip-us.apache.org/repos/asf/trafficserver/tree/4cfbfdfc Diff: http://git-wip-us.apache.org/repos/asf/trafficserver/diff/4cfbfdfc Branch: refs/heads/master Commit: 4cfbfdfcfee65987d6d4eb7ae9b6b1e6ea2d4a77 Parents: 5c404b0 Author: Kit Chan <[email protected]> Authored: Thu Aug 20 00:55:59 2015 -0700 Committer: Kit Chan <[email protected]> Committed: Thu Aug 20 00:55:59 2015 -0700 ---------------------------------------------------------------------- plugins/experimental/ts_lua/ts_lua.c | 2 + plugins/experimental/ts_lua/ts_lua_common.h | 2 + plugins/experimental/ts_lua/ts_lua_http.c | 102 ++++++++ plugins/experimental/ts_lua/ts_lua_remap.c | 306 +++++++++++++++++++++++ 4 files changed, 412 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/trafficserver/blob/4cfbfdfc/plugins/experimental/ts_lua/ts_lua.c ---------------------------------------------------------------------- diff --git a/plugins/experimental/ts_lua/ts_lua.c b/plugins/experimental/ts_lua/ts_lua.c index b20cf3f..1c1a770 100644 --- a/plugins/experimental/ts_lua/ts_lua.c +++ b/plugins/experimental/ts_lua/ts_lua.c @@ -146,6 +146,7 @@ TSRemapDoRemap(void *ih, TSHttpTxn rh, TSRemapRequestInfo *rri) http_ctx->client_request_bufp = rri->requestBufp; http_ctx->client_request_hdrp = rri->requestHdrp; http_ctx->client_request_url = rri->requestUrl; + http_ctx->rri = rri; http_ctx->remap = 1; http_ctx->has_hook = 0; @@ -218,6 +219,7 @@ globalHookHandler(TSCont contp, TSEvent event ATS_UNUSED, void *edata) http_ctx = ts_lua_create_http_ctx(main_ctx, conf); http_ctx->txnp = txnp; + http_ctx->rri = NULL; http_ctx->remap = 0; http_ctx->has_hook = 0; http://git-wip-us.apache.org/repos/asf/trafficserver/blob/4cfbfdfc/plugins/experimental/ts_lua/ts_lua_common.h ---------------------------------------------------------------------- diff --git a/plugins/experimental/ts_lua/ts_lua_common.h b/plugins/experimental/ts_lua/ts_lua_common.h index 06187d7..04c95d0 100644 --- a/plugins/experimental/ts_lua/ts_lua_common.h +++ b/plugins/experimental/ts_lua/ts_lua_common.h @@ -129,6 +129,8 @@ typedef struct { int remap; int has_hook; + TSRemapRequestInfo *rri; + } ts_lua_http_ctx; http://git-wip-us.apache.org/repos/asf/trafficserver/blob/4cfbfdfc/plugins/experimental/ts_lua/ts_lua_http.c ---------------------------------------------------------------------- diff --git a/plugins/experimental/ts_lua/ts_lua_http.c b/plugins/experimental/ts_lua/ts_lua_http.c index a468cb5..3ed16ea 100644 --- a/plugins/experimental/ts_lua/ts_lua_http.c +++ b/plugins/experimental/ts_lua/ts_lua_http.c @@ -45,6 +45,8 @@ static int ts_lua_http_set_resp(lua_State *L); static int ts_lua_http_get_cache_lookup_status(lua_State *L); static int ts_lua_http_set_cache_lookup_status(lua_State *L); static int ts_lua_http_set_cache_url(lua_State *L); +static int ts_lua_http_get_cache_lookup_url(lua_State *L); +static int ts_lua_http_set_cache_lookup_url(lua_State *L); static int ts_lua_http_set_server_resp_no_store(lua_State *L); static void ts_lua_inject_cache_lookup_result_variables(lua_State *L); @@ -54,6 +56,7 @@ static int ts_lua_http_resp_cache_untransformed(lua_State *L); static int ts_lua_http_is_internal_request(lua_State *L); static int ts_lua_http_skip_remapping_set(lua_State *L); +static int ts_lua_http_transaction_count(lua_State *L); static void ts_lua_inject_http_resp_transform_api(lua_State *L); static int ts_lua_http_resp_transform_get_upstream_bytes(lua_State *L); @@ -101,6 +104,12 @@ ts_lua_inject_http_cache_api(lua_State *L) lua_pushcfunction(L, ts_lua_http_set_cache_url); lua_setfield(L, -2, "set_cache_url"); + lua_pushcfunction(L, ts_lua_http_get_cache_lookup_url); + lua_setfield(L, -2, "get_cache_lookup_url"); + + lua_pushcfunction(L, ts_lua_http_set_cache_lookup_url); + lua_setfield(L, -2, "set_cache_lookup_url"); + lua_pushcfunction(L, ts_lua_http_set_server_resp_no_store); lua_setfield(L, -2, "set_server_resp_no_store"); @@ -140,6 +149,9 @@ ts_lua_inject_http_misc_api(lua_State *L) lua_pushcfunction(L, ts_lua_http_skip_remapping_set); lua_setfield(L, -2, "skip_remapping_set"); + + lua_pushcfunction(L, ts_lua_http_transaction_count); + lua_setfield(L, -2, "transaction_count"); } static void @@ -238,6 +250,78 @@ ts_lua_http_set_cache_lookup_status(lua_State *L) } 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; + + http_ctx = ts_lua_get_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_http_set_cache_lookup_url(lua_State *L) +{ + const char *url; + size_t url_len; + + ts_lua_http_ctx *http_ctx; + + http_ctx = ts_lua_get_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 int ts_lua_http_set_cache_url(lua_State *L) { const char *url; @@ -338,6 +422,24 @@ ts_lua_http_skip_remapping_set(lua_State *L) } static int +ts_lua_http_transaction_count(lua_State *L) +{ + ts_lua_http_ctx *http_ctx; + + http_ctx = ts_lua_get_http_ctx(L); + + TSHttpSsn ssn = TSHttpTxnSsnGet(http_ctx->txnp); + if (ssn) { + int n = TSHttpSsnTransactionCount(ssn); + lua_pushnumber(L, n); + } else { + lua_pushnil(L); + } + + return 1; +} + +static int ts_lua_http_resp_transform_get_upstream_bytes(lua_State *L) { ts_lua_http_transform_ctx *transform_ctx; http://git-wip-us.apache.org/repos/asf/trafficserver/blob/4cfbfdfc/plugins/experimental/ts_lua/ts_lua_remap.c ---------------------------------------------------------------------- diff --git a/plugins/experimental/ts_lua/ts_lua_remap.c b/plugins/experimental/ts_lua/ts_lua_remap.c index e27a612..3459158 100644 --- a/plugins/experimental/ts_lua/ts_lua_remap.c +++ b/plugins/experimental/ts_lua/ts_lua_remap.c @@ -17,6 +17,7 @@ */ +#include "ts_lua_util.h" #include "ts_lua_remap.h" @@ -33,6 +34,17 @@ ts_lua_var_item ts_lua_remap_status_vars[] = { TS_LUA_MAKE_VAR_ITEM(TS_LUA_REMAP_NO_REMAP_STOP), TS_LUA_MAKE_VAR_ITEM(TS_LUA_REMAP_DID_REMAP_STOP), TS_LUA_MAKE_VAR_ITEM(TS_LUA_REMAP_ERROR)}; +static int ts_lua_remap_get_from_url_host(lua_State *L); +static int ts_lua_remap_get_from_url_port(lua_State *L); +static int ts_lua_remap_get_from_url_scheme(lua_State *L); +static int ts_lua_remap_get_from_uri(lua_State *L); +static int ts_lua_remap_get_from_url(lua_State *L); + +static int ts_lua_remap_get_to_url_host(lua_State *L); +static int ts_lua_remap_get_to_url_port(lua_State *L); +static int ts_lua_remap_get_to_url_scheme(lua_State *L); +static int ts_lua_remap_get_to_uri(lua_State *L); +static int ts_lua_remap_get_to_url(lua_State *L); static void ts_lua_inject_remap_variables(lua_State *L); @@ -41,6 +53,40 @@ void ts_lua_inject_remap_api(lua_State *L) { ts_lua_inject_remap_variables(L); + + lua_newtable(L); + + lua_pushcfunction(L, ts_lua_remap_get_from_url_host); + lua_setfield(L, -2, "get_from_url_host"); + + lua_pushcfunction(L, ts_lua_remap_get_from_url_port); + lua_setfield(L, -2, "get_from_url_port"); + + lua_pushcfunction(L, ts_lua_remap_get_from_url_scheme); + lua_setfield(L, -2, "get_from_url_scheme"); + + lua_pushcfunction(L, ts_lua_remap_get_from_uri); + lua_setfield(L, -2, "get_from_uri"); + + lua_pushcfunction(L, ts_lua_remap_get_from_url); + lua_setfield(L, -2, "get_from_url"); + + lua_pushcfunction(L, ts_lua_remap_get_to_url_host); + lua_setfield(L, -2, "get_to_url_host"); + + lua_pushcfunction(L, ts_lua_remap_get_to_url_port); + lua_setfield(L, -2, "get_to_url_port"); + + lua_pushcfunction(L, ts_lua_remap_get_to_url_scheme); + lua_setfield(L, -2, "get_to_url_scheme"); + + lua_pushcfunction(L, ts_lua_remap_get_to_uri); + lua_setfield(L, -2, "get_to_uri"); + + lua_pushcfunction(L, ts_lua_remap_get_to_url); + lua_setfield(L, -2, "get_to_url"); + + lua_setfield(L, -2, "remap"); } static void @@ -53,3 +99,263 @@ ts_lua_inject_remap_variables(lua_State *L) lua_setglobal(L, ts_lua_remap_status_vars[i].svar); } } + +static int +ts_lua_remap_get_from_url_host(lua_State *L) +{ + const char *host; + int len = 0; + + ts_lua_http_ctx *http_ctx; + + http_ctx = ts_lua_get_http_ctx(L); + + if (http_ctx->rri != NULL) { + host = TSUrlHostGet(http_ctx->client_request_bufp, http_ctx->rri->mapFromUrl, &len); + + if (len == 0) { + lua_pushnil(L); + } else { + lua_pushlstring(L, host, len); + } + } else { + lua_pushnil(L); + } + + return 1; +} + +static int +ts_lua_remap_get_from_url_port(lua_State *L) +{ + int port; + + ts_lua_http_ctx *http_ctx; + + http_ctx = ts_lua_get_http_ctx(L); + + if (http_ctx->rri != NULL) { + port = TSUrlPortGet(http_ctx->client_request_bufp, http_ctx->rri->mapFromUrl); + + lua_pushnumber(L, port); + } else { + lua_pushnil(L); + } + + return 1; +} + +static int +ts_lua_remap_get_from_url_scheme(lua_State *L) +{ + const char *scheme; + int len; + + ts_lua_http_ctx *http_ctx; + + http_ctx = ts_lua_get_http_ctx(L); + + if (http_ctx->rri != NULL) { + scheme = TSUrlSchemeGet(http_ctx->client_request_bufp, http_ctx->rri->mapFromUrl, &len); + + if (len == 0) { + lua_pushnil(L); + } else { + lua_pushlstring(L, scheme, len); + } + } else { + lua_pushnil(L); + } + + return 1; +} + +static int +ts_lua_remap_get_from_uri(lua_State *L) +{ + char uri[TS_LUA_MAX_URL_LENGTH]; + const char *path; + int path_len; + int uri_len; + + ts_lua_http_ctx *http_ctx; + + http_ctx = ts_lua_get_http_ctx(L); + + if (http_ctx->rri != NULL) { + path = TSUrlPathGet(http_ctx->client_request_bufp, http_ctx->rri->mapFromUrl, &path_len); + + uri_len = snprintf(uri, TS_LUA_MAX_URL_LENGTH, "/%.*s", path_len, path); + + if (uri_len >= TS_LUA_MAX_URL_LENGTH) { + lua_pushlstring(L, uri, TS_LUA_MAX_URL_LENGTH - 1); + } else { + lua_pushlstring(L, uri, uri_len); + } + } else { + lua_pushnil(L); + } + + return 1; +} + +static int +ts_lua_remap_get_from_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; + + http_ctx = ts_lua_get_http_ctx(L); + + if (http_ctx->rri != NULL) { + url = TSUrlStringGet(http_ctx->client_request_bufp, http_ctx->rri->mapFromUrl, &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_remap_get_to_url_host(lua_State *L) +{ + const char *host; + int len = 0; + + ts_lua_http_ctx *http_ctx; + + http_ctx = ts_lua_get_http_ctx(L); + + if (http_ctx->rri != NULL) { + host = TSUrlHostGet(http_ctx->client_request_bufp, http_ctx->rri->mapToUrl, &len); + + if (len == 0) { + lua_pushnil(L); + } else { + lua_pushlstring(L, host, len); + } + } else { + lua_pushnil(L); + } + + return 1; +} + +static int +ts_lua_remap_get_to_url_port(lua_State *L) +{ + int port; + + ts_lua_http_ctx *http_ctx; + + http_ctx = ts_lua_get_http_ctx(L); + + if (http_ctx->rri != NULL) { + port = TSUrlPortGet(http_ctx->client_request_bufp, http_ctx->rri->mapToUrl); + + lua_pushnumber(L, port); + } else { + lua_pushnil(L); + } + + return 1; +} + +static int +ts_lua_remap_get_to_url_scheme(lua_State *L) +{ + const char *scheme; + int len; + + ts_lua_http_ctx *http_ctx; + + http_ctx = ts_lua_get_http_ctx(L); + + if (http_ctx->rri != NULL) { + scheme = TSUrlSchemeGet(http_ctx->client_request_bufp, http_ctx->rri->mapToUrl, &len); + + if (len == 0) { + lua_pushnil(L); + } else { + lua_pushlstring(L, scheme, len); + } + } else { + lua_pushnil(L); + } + + return 1; +} + +static int +ts_lua_remap_get_to_uri(lua_State *L) +{ + char uri[TS_LUA_MAX_URL_LENGTH]; + const char *path; + int path_len; + int uri_len; + + ts_lua_http_ctx *http_ctx; + + http_ctx = ts_lua_get_http_ctx(L); + + if (http_ctx->rri != NULL) { + path = TSUrlPathGet(http_ctx->client_request_bufp, http_ctx->rri->mapToUrl, &path_len); + + uri_len = snprintf(uri, TS_LUA_MAX_URL_LENGTH, "/%.*s", path_len, path); + + if (uri_len >= TS_LUA_MAX_URL_LENGTH) { + lua_pushlstring(L, uri, TS_LUA_MAX_URL_LENGTH - 1); + } else { + lua_pushlstring(L, uri, uri_len); + } + } else { + lua_pushnil(L); + } + + 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; + + http_ctx = ts_lua_get_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; +}
