Repository: trafficserver Updated Branches: refs/heads/master 46d05a3cc -> e02420a42
TS-3225: add more API support to 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/e02420a4 Tree: http://git-wip-us.apache.org/repos/asf/trafficserver/tree/e02420a4 Diff: http://git-wip-us.apache.org/repos/asf/trafficserver/diff/e02420a4 Branch: refs/heads/master Commit: e02420a424c049808c56f62c21ef84757060ffe8 Parents: 46d05a3 Author: Kit Chan <[email protected]> Authored: Tue Dec 23 22:40:34 2014 +0000 Committer: Kit Chan <[email protected]> Committed: Tue Dec 23 22:40:34 2014 +0000 ---------------------------------------------------------------------- CHANGES | 4 +- doc/reference/plugins/ts_lua.en.rst | 198 +++++++++++++++++++ plugins/experimental/ts_lua/ts_lua_common.h | 1 + plugins/experimental/ts_lua/ts_lua_crypto.c | 156 +++++++++++++++ plugins/experimental/ts_lua/ts_lua_http.c | 20 ++ .../experimental/ts_lua/ts_lua_http_config.c | 146 +++++++++++++- 6 files changed, 523 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/trafficserver/blob/e02420a4/CHANGES ---------------------------------------------------------------------- diff --git a/CHANGES b/CHANGES index 050abfe..d696bbd 100644 --- a/CHANGES +++ b/CHANGES @@ -1,7 +1,9 @@ -*- coding: utf-8 -*- Changes with Apache Traffic Server 5.3.0 - *) [TS-3255] support flush option in gzip plugins. + *) [TS-3225] add more API support to ts_lua plugin. + + *) [TS-3255] support flush option in gzip plugin. *) [TS-3222] Fix port print to not have leading 0. http://git-wip-us.apache.org/repos/asf/trafficserver/blob/e02420a4/doc/reference/plugins/ts_lua.en.rst ---------------------------------------------------------------------- diff --git a/doc/reference/plugins/ts_lua.en.rst b/doc/reference/plugins/ts_lua.en.rst index 3e190c0..72a2cc5 100644 --- a/doc/reference/plugins/ts_lua.en.rst +++ b/doc/reference/plugins/ts_lua.en.rst @@ -734,6 +734,36 @@ Here is an example: `TOP <#ts-lua-plugin>`_ +ts.http.set_cache_lookup_status +------------------------------- +**syntax:** *ts.http.set_cache_lookup_status()* + +**context:** function after TS_LUA_HOOK_CACHE_LOOKUP_COMPLETE hook point + +**description:** This function can be used to set cache lookup status. + +Here is an example: + +:: + + function cache_lookup() + local cache_status = ts.http.get_cache_lookup_status() + if cache_status == TS_LUA_CACHE_LOOKUP_HIT_FRESH then + print('hit') + else + print('not hit') + end + ts.http.set_cache_lookup_status(TS_LUA_CACHE_LOOKUP_MISS) + end + + function do_remap() + ts.hook(TS_LUA_HOOK_CACHE_LOOKUP_COMPLETE, cache_lookup) + return 0 + end + + +`TOP <#ts-lua-plugin>`_ + Http cache lookup status constants ---------------------------------- **context:** global @@ -1595,6 +1625,87 @@ Here is an example: `TOP <#ts-lua-plugin>`_ +ts.base64_encode +----------- +**syntax:** *value = ts.base64_encode(str)* + +**context:** global + +**description:** Returns the base64 encoding of the ``str`` argument. + +Here is an example: + +:: + + function do_remap() + uri = ts.client_request.get_uri() + value = ts.base64_encode(uri) + end + + +`TOP <#ts-lua-plugin>`_ + +ts.base64_decode +----------- +**syntax:** *value = ts.base64_decode(str)* + +**context:** global + +**description:** Returns the base64 decoding of the ``str`` argument. + +Here is an example: + +:: + + function do_remap() + uri = ts.client_request.get_uri() + encoded_value = ts.base64_encode(uri) + decoded_value = ts.base64_decode(encoded_value) + end + + +`TOP <#ts-lua-plugin>`_ + +ts.escape_uri +----------- +**syntax:** *value = ts.escape_uri(str)* + +**context:** global + +**description:** Returns the uri-escaped value of the ``str`` argument. + +Here is an example: + +:: + + function do_remap() + test = '/some value/' + value = ts.escape_uri(test) + end + +`TOP <#ts-lua-plugin>`_ + +ts.unescape_uri +----------- +**syntax:** *value = ts.unescape_uri(str)* + +**context:** global + +**description:** Returns the uri-unescaped value of the ``str`` argument. + +Here is an example: + +:: + + function do_remap() + test = '/some value/' + escaped_value = ts.escape_uri(test) + unescaped_value = ts.unescape_uri(escaped_value) + end + + +`TOP <#ts-lua-plugin>`_ + ts.intercept ------------ **syntax:** *ts.intercept(FUNCTION)* @@ -1962,6 +2073,93 @@ Http config constants TS_LUA_CONFIG_NET_SOCK_PACKET_MARK_OUT TS_LUA_CONFIG_NET_SOCK_PACKET_TOS_OUT + +`TOP <#ts-lua-plugin>`_ + +ts.http.timeout_set +---------------------- +**syntax:** *ts.http.timeout_set(CONFIG, NUMBER)* + +**context:** do_remap or do_global_* or later. + +**description:** This function can be used to overwrite the timeout settings. + +Here is an example: + +:: + + function do_remap() + ts.http.timeout_set(TS_LUA_TIMEOUT_DNS, 30) -- 30 seconds + return 0 + end + + +`TOP <#ts-lua-plugin>`_ + +Timeout constants +--------------------- +**context:** do_remap or do_global_* or later + +:: + + TS_LUA_TIMEOUT_ACTIVE + TS_LUA_TIMEOUT_DNS + TS_LUA_TIMEOUT_CONNECT + TS_LUA_TIMEOUT_NO_ACTIVITY + + +`TOP <#ts-lua-plugin>`_ + +ts.http.client_packet_mark_set +---------------------- +**syntax:** *ts.http.client_packet_mark_set(NUMBER)* + +**context:** do_remap or do_global_* or later. + +**description:** This function can be used to set packet mark for client connection. + +Here is an example: + +:: + + function do_remap() + ts.http.client_packet_mark_set(TS_LUA_TIMEOUT_DNS, 0) + return 0 + end + +`TOP <#ts-lua-plugin>`_ + +ts.http.server_packet_mark_set +------------------------- +**syntax:** *ts.http.server_packet_mark_set(NUMBER)* + +**context:** do_remap or do_global_* or later. + +**description:** This function can be used to set packet mark for server connection. + + +`TOP <#ts-lua-plugin>`_ + +ts.http.client_packet_tos_set +------------------------- +**syntax:** *ts.http.client_packet_tos_set(NUMBER)* + +**context:** do_remap or do_global_* or later. + +**description:** This function can be used to set packet tos for client connection. + + +`TOP <#ts-lua-plugin>`_ + +ts.http.server_packet_tos_set +------------------------- +**syntax:** *ts.http.server_packet_tos_set(NUMBER)* + +**context:** do_remap or do_global_* or later. + +**description:** This function can be used to set packet tos for server connection. + + `TOP <#ts-lua-plugin>`_ ts.http.cntl_get http://git-wip-us.apache.org/repos/asf/trafficserver/blob/e02420a4/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 1ba925d..4599787 100644 --- a/plugins/experimental/ts_lua/ts_lua_common.h +++ b/plugins/experimental/ts_lua/ts_lua_common.h @@ -70,6 +70,7 @@ #define TS_LUA_MAX_URL_LENGTH 2048 #define TS_LUA_MAX_OVEC_SIZE (3 * 32) #define TS_LUA_MAX_RESIDENT_PCRE 64 +#define TS_LUA_MAX_STR_LENGTH 2048 #define TS_LUA_MIN_ALIGN sizeof(void*) #define TS_LUA_MEM_ALIGN(size) (((size) + ((TS_LUA_MIN_ALIGN) - 1)) & ~((TS_LUA_MIN_ALIGN) - 1)) http://git-wip-us.apache.org/repos/asf/trafficserver/blob/e02420a4/plugins/experimental/ts_lua/ts_lua_crypto.c ---------------------------------------------------------------------- diff --git a/plugins/experimental/ts_lua/ts_lua_crypto.c b/plugins/experimental/ts_lua/ts_lua_crypto.c index addd128..a3aea2f 100644 --- a/plugins/experimental/ts_lua/ts_lua_crypto.c +++ b/plugins/experimental/ts_lua/ts_lua_crypto.c @@ -31,6 +31,12 @@ static int ts_lua_md5_bin(lua_State * L); static int ts_lua_sha1(lua_State * L); static int ts_lua_sha1_bin(lua_State * L); +static int ts_lua_base64_encode(lua_State *L); +static int ts_lua_base64_decode(lua_State *L); + +static int ts_lua_escape_uri(lua_State *L); +static int ts_lua_unescape_uri(lua_State *L); + void ts_lua_inject_crypto_api(lua_State * L) { @@ -49,6 +55,22 @@ ts_lua_inject_crypto_api(lua_State * L) /* ts.sha1_bin(...) */ lua_pushcfunction(L, ts_lua_sha1_bin); lua_setfield(L, -2, "sha1_bin"); + + /* ts.base64_encode(...) */ + lua_pushcfunction(L, ts_lua_base64_encode); + lua_setfield(L, -2, "base64_encode"); + + /* ts.base64_decode(...) */ + lua_pushcfunction(L, ts_lua_base64_decode); + lua_setfield(L, -2, "base64_decode"); + + /* ts.escape_uri(...) */ + lua_pushcfunction(L, ts_lua_escape_uri); + lua_setfield(L, -2, "escape_uri"); + + /* ts.unescape_uri(...) */ + lua_pushcfunction(L, ts_lua_unescape_uri); + lua_setfield(L, -2, "unescape_uri"); } static int @@ -175,3 +197,137 @@ ts_lua_sha1_bin(lua_State * L) return 1; } + +static int +ts_lua_base64_encode(lua_State *L) +{ + u_char *src; + u_char *dst; + size_t slen; + size_t dlen; + + size_t length; + + if (lua_gettop(L) != 1) { + return luaL_error(L, "expecting one argument"); + } + + if (lua_isnil(L, 1)) { + src = (u_char *)""; + slen = 0; + } else { + src = (u_char*)luaL_checklstring(L, 1, &slen); + } + + dlen = TS_LUA_MAX_STR_LENGTH; + dst = lua_newuserdata(L, dlen); + + if(TS_SUCCESS == TSBase64Encode((const char *)src, slen, (char *)dst, dlen, + &length)) { + lua_pushlstring( L, (char*)dst, length); + return 1; + } else { + return luaL_error(L, "base64 encoding error"); + } +} + +static int +ts_lua_base64_decode(lua_State *L) +{ + u_char *src; + u_char *dst; + size_t slen; + size_t dlen; + + size_t length; + + if (lua_gettop(L) != 1) { + return luaL_error(L, "expecting one argument"); + } + + if (lua_isnil(L, 1)) { + src = (u_char *)""; + slen = 0; + } else { + src = (u_char*)luaL_checklstring(L, 1, &slen); + } + + dlen = TS_LUA_MAX_STR_LENGTH; + dst = lua_newuserdata(L, dlen); + + if(TS_SUCCESS == TSBase64Decode((const char *)src, slen, (unsigned char *)dst, + dlen, &length)) { + lua_pushlstring(L, (char*)dst, length); + return 1; + } else { + return luaL_error(L, "base64 decoding error"); + } +} + +static int +ts_lua_escape_uri(lua_State *L) +{ + size_t len, dlen; + u_char *src, *dst; + + size_t length; + + if (lua_gettop(L) != 1) { + return luaL_error(L, "expecting one argument for ts.escape_uri(...)"); + } + + if (lua_isnil(L, 1)) { + lua_pushliteral(L, ""); + return 1; + } + + src = (u_char*)luaL_checklstring(L, 1, &len); + if (len == 0) + return 1; + + dlen = TS_LUA_MAX_STR_LENGTH; + dst = lua_newuserdata(L, dlen); + + if(TS_SUCCESS == TSStringPercentEncode((const char *)src, len, (char *)dst, + dlen, &length, NULL)) { + lua_pushlstring(L, (char *) dst, length); + return 1; + } else { + return luaL_error(L, "percent encoding error"); + } +} + +static int +ts_lua_unescape_uri(lua_State *L) +{ + size_t len, dlen; + u_char *src, *dst; + + size_t length; + + if (lua_gettop(L) != 1) { + return luaL_error(L, "expecting one argument for ts.unescape_uri(...)"); + } + + if (lua_isnil(L, 1)) { + lua_pushliteral(L, ""); + return 1; + } + + src = (u_char *) luaL_checklstring(L, 1, &len); + if(len == 0) + return 1; + + /* the unescaped string can only be smaller */ + dlen = len; + dst = lua_newuserdata(L, dlen); + + if(TS_SUCCESS == TSStringPercentDecode((const char *)src, len, (char *)dst, + dlen, &length)) { + lua_pushlstring(L, (char *) dst, length); + return 1; + } else { + return luaL_error(L, "percent decoding error"); + } +} + http://git-wip-us.apache.org/repos/asf/trafficserver/blob/e02420a4/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 c79680b..dfc981e 100644 --- a/plugins/experimental/ts_lua/ts_lua_http.c +++ b/plugins/experimental/ts_lua/ts_lua_http.c @@ -47,6 +47,7 @@ static int ts_lua_http_set_retbody(lua_State * L); 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 void ts_lua_inject_cache_lookup_result_variables(lua_State * L); @@ -92,6 +93,9 @@ ts_lua_inject_http_cache_api(lua_State * L) lua_pushcfunction(L, ts_lua_http_get_cache_lookup_status); lua_setfield(L, -2, "get_cache_lookup_status"); + lua_pushcfunction(L, ts_lua_http_set_cache_lookup_status); + lua_setfield(L, -2, "set_cache_lookup_status"); + lua_pushcfunction(L, ts_lua_http_set_cache_url); lua_setfield(L, -2, "set_cache_url"); @@ -198,6 +202,22 @@ ts_lua_http_get_cache_lookup_status(lua_State * L) } static int +ts_lua_http_set_cache_lookup_status(lua_State *L) +{ + int status; + + ts_lua_http_ctx *http_ctx; + + http_ctx = ts_lua_get_http_ctx(L); + + status = luaL_checknumber(L, 1); + + TSHttpTxnCacheLookupStatusSet(http_ctx->txnp, status); + + return 0; +} + +static int ts_lua_http_set_cache_url(lua_State * L) { const char *url; http://git-wip-us.apache.org/repos/asf/trafficserver/blob/e02420a4/plugins/experimental/ts_lua/ts_lua_http_config.c ---------------------------------------------------------------------- diff --git a/plugins/experimental/ts_lua/ts_lua_http_config.c b/plugins/experimental/ts_lua/ts_lua_http_config.c index 6fe190e..ee1c8d3 100644 --- a/plugins/experimental/ts_lua/ts_lua_http_config.c +++ b/plugins/experimental/ts_lua/ts_lua_http_config.c @@ -85,6 +85,13 @@ typedef enum TS_LUA_CONFIG_LAST_ENTRY = TS_CONFIG_LAST_ENTRY } TSLuaOverridableConfigKey; +typedef enum +{ + TS_LUA_TIMEOUT_ACTIVE = 0, + TS_LUA_TIMEOUT_CONNECT = 1, + TS_LUA_TIMEOUT_DNS = 2, + TS_LUA_TIMEOUT_NO_ACTIVITY = 3 +} TSLuaTimeoutKey; ts_lua_var_item ts_lua_http_config_vars[] = { TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_URL_REMAP_PRISTINE_HOST_HDR), @@ -151,6 +158,13 @@ ts_lua_var_item ts_lua_http_config_vars[] = { TS_LUA_MAKE_VAR_ITEM(TS_LUA_CONFIG_LAST_ENTRY), }; +ts_lua_var_item ts_lua_http_timeout_vars[] = { + TS_LUA_MAKE_VAR_ITEM(TS_LUA_TIMEOUT_ACTIVE), + TS_LUA_MAKE_VAR_ITEM(TS_LUA_TIMEOUT_CONNECT), + TS_LUA_MAKE_VAR_ITEM(TS_LUA_TIMEOUT_DNS), + TS_LUA_MAKE_VAR_ITEM(TS_LUA_TIMEOUT_NO_ACTIVITY), +}; + static void ts_lua_inject_http_config_variables(lua_State * L); static int ts_lua_http_config_int_set(lua_State * L); @@ -159,7 +173,11 @@ static int ts_lua_http_config_float_set(lua_State * L); static int ts_lua_http_config_float_get(lua_State * L); static int ts_lua_http_config_string_set(lua_State * L); static int ts_lua_http_config_string_get(lua_State * L); - +static int ts_lua_http_timeout_set(lua_State * L); +static int ts_lua_http_client_packet_mark_set(lua_State * L); +static int ts_lua_http_server_packet_mark_set(lua_State * L); +static int ts_lua_http_client_packet_tos_set(lua_State * L); +static int ts_lua_http_server_packet_tos_set(lua_State * L); void ts_lua_inject_http_config_api(lua_State * L) @@ -183,6 +201,21 @@ ts_lua_inject_http_config_api(lua_State * L) lua_pushcfunction(L, ts_lua_http_config_string_get); lua_setfield(L, -2, "config_string_get"); + + lua_pushcfunction(L, ts_lua_http_timeout_set); + lua_setfield(L, -2, "timeout_set"); + + lua_pushcfunction(L, ts_lua_http_client_packet_mark_set); + lua_setfield(L, -2, "client_packet_mark_set"); + + lua_pushcfunction(L, ts_lua_http_server_packet_mark_set); + lua_setfield(L, -2, "server_packet_mark_set"); + + lua_pushcfunction(L, ts_lua_http_client_packet_tos_set); + lua_setfield(L, -2, "client_packet_tos_set"); + + lua_pushcfunction(L, ts_lua_http_server_packet_tos_set); + lua_setfield(L, -2, "server_packet_tos_set"); } static void @@ -194,6 +227,11 @@ ts_lua_inject_http_config_variables(lua_State * L) lua_pushinteger(L, ts_lua_http_config_vars[i].nvar); lua_setglobal(L, ts_lua_http_config_vars[i].svar); } + + for (i = 0; i < sizeof(ts_lua_http_timeout_vars)/ sizeof(ts_lua_var_item); i++) { + lua_pushinteger(L, ts_lua_http_timeout_vars[i].nvar); + lua_setglobal(L, ts_lua_http_timeout_vars[i].svar); + } } static int @@ -302,3 +340,109 @@ ts_lua_http_config_string_get(lua_State * L) return 1; } + +static int +ts_lua_http_timeout_set(lua_State * L) +{ + int conf; + int value; + ts_lua_http_ctx *http_ctx; + + http_ctx = ts_lua_get_http_ctx(L); + + conf = luaL_checkinteger(L, 1); + value = luaL_checkinteger(L, 2); + + switch(conf) { + case TS_LUA_TIMEOUT_ACTIVE: + TSDebug(TS_LUA_DEBUG_TAG, "setting active timeout"); + TSHttpTxnActiveTimeoutSet(http_ctx->txnp, value); + break; + + case TS_LUA_TIMEOUT_CONNECT: + TSDebug(TS_LUA_DEBUG_TAG, "setting connect timeout"); + TSHttpTxnConnectTimeoutSet(http_ctx->txnp, value); + break; + + case TS_LUA_TIMEOUT_DNS: + TSDebug(TS_LUA_DEBUG_TAG, "setting dns timeout"); + TSHttpTxnDNSTimeoutSet(http_ctx->txnp, value); + break; + + case TS_LUA_TIMEOUT_NO_ACTIVITY: + TSDebug(TS_LUA_DEBUG_TAG, "setting no activity timeout"); + TSHttpTxnNoActivityTimeoutSet(http_ctx->txnp, value); + break; + + default: + TSError("unsupported timeout config option for lua plugin"); + break; + } + + return 0; +} + +static int +ts_lua_http_client_packet_mark_set(lua_State * L) +{ + int value; + ts_lua_http_ctx *http_ctx; + + http_ctx = ts_lua_get_http_ctx(L); + + value = luaL_checkinteger(L, 1); + + TSDebug(TS_LUA_DEBUG_TAG, "client packet mark set"); + TSHttpTxnClientPacketMarkSet(http_ctx->txnp, value); + + return 0; +} + +static int +ts_lua_http_server_packet_mark_set(lua_State * L) +{ + int value; + ts_lua_http_ctx *http_ctx; + + http_ctx = ts_lua_get_http_ctx(L); + + value = luaL_checkinteger(L, 1); + + TSDebug(TS_LUA_DEBUG_TAG, "server packet mark set"); + TSHttpTxnServerPacketMarkSet(http_ctx->txnp, value); + + return 0; +} + +static int +ts_lua_http_client_packet_tos_set(lua_State * L) +{ + int value; + ts_lua_http_ctx *http_ctx; + + http_ctx = ts_lua_get_http_ctx(L); + + value = luaL_checkinteger(L, 1); + + TSDebug(TS_LUA_DEBUG_TAG, "client packet tos set"); + TSHttpTxnClientPacketTosSet(http_ctx->txnp, value); + + return 0; +} + +static int +ts_lua_http_server_packet_tos_set(lua_State * L) +{ + int value; + ts_lua_http_ctx *http_ctx; + + http_ctx = ts_lua_get_http_ctx(L); + + value = luaL_checkinteger(L, 1); + + TSDebug(TS_LUA_DEBUG_TAG, "server packet tos set"); + TSHttpTxnServerPacketTosSet(http_ctx->txnp, value); + + return 0; +} +
