You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by ki...@apache.org on 2018/05/08 15:02:40 UTC

[trafficserver] 01/02: Add misc functions to ts_lua and update document to avoid using print

This is an automated email from the ASF dual-hosted git repository.

kichan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficserver.git

commit e1f12c000ffd23389fe91ed511f8ed351e8dd238
Author: Kit Chan <ki...@apache.org>
AuthorDate: Sat May 5 15:34:14 2018 -0700

    Add misc functions to ts_lua and update document to avoid using print
---
 doc/admin-guide/plugins/ts_lua.en.rst     | 218 ++++++++++++++++++++----------
 plugins/experimental/ts_lua/ts_lua_http.c |  43 ++++++
 plugins/experimental/ts_lua/ts_lua_misc.c | 160 +++++++++++++++++++++-
 3 files changed, 352 insertions(+), 69 deletions(-)

diff --git a/doc/admin-guide/plugins/ts_lua.en.rst b/doc/admin-guide/plugins/ts_lua.en.rst
index 8ada7c8..d1e4ae4 100644
--- a/doc/admin-guide/plugins/ts_lua.en.rst
+++ b/doc/admin-guide/plugins/ts_lua.en.rst
@@ -53,7 +53,7 @@ Synopsis
     local HOSTNAME = ''
     function __init__(argtb)
         if (#argtb) < 1 then
-            print(argtb[0], 'hostname parameter required!!')
+            ts.debug(argtb[0] .. ' hostname parameter required!!')
             return -1
         end
         HOSTNAME = argtb[1]
@@ -212,9 +212,30 @@ Here is an example:
 
 `TOP <#ts-lua-plugin>`_
 
+TS Basic Internal Information
+-----------------------------
+**syntax:** *ts.get_install_dir()*
+**syntax:** *ts.get_runtime_dir()*
+**syntax:** *ts.get_config_dir()*
+**syntax:** *ts.get_plugin_dir()*
+**syntax:** *ts.get_traffic_server_version()*
+
+**context:** global 
+
+**description**: get basic internal information for the TS instance, such as install directory, runtime directory,
+config directory, plugin directory and version of ATS
+
+Here is an example:
+
+::
+
+       local config_dir = ts.get_config_dir()
+
+`TOP <#ts-lua-plugin>`_
+
 Remap status constants
 ----------------------
-**context:** do_remap
+**context:** do_emap
 
 ::
 
@@ -567,7 +588,7 @@ Here is an example:
 
     function do_remap()
         local uri = ts.client_request.get_uri()
-        print(uri)
+        ts.debug(uri)
     end
 
 Then ``GET /st?a=1`` will yield the output:
@@ -604,7 +625,7 @@ Here is an example:
 
     function do_remap()
         local query = ts.client_request.get_uri_args()
-        print(query)
+        ts.debug(query)
     end
 
 Then ``GET /st?a=1&b=2`` will yield the output:
@@ -643,7 +664,7 @@ Here is an example:
 
     function do_remap()
         local query = ts.client_request.get_uri_params()
-        print(query)
+        ts.debug(query)
     end
 
 Then ``GET /st;a=1`` will yield the output:
@@ -682,7 +703,7 @@ Here is an example:
 
     function do_remap()
         local url = ts.client_request.get_url()
-        print(url)
+        ts.debug(url)
     end
 
 Then ``GET /st?a=1&b=2 HTTP/1.1\r\nHost: a.tbcdn.cn\r\n...`` will yield the output:
@@ -709,7 +730,7 @@ Here is an example:
 
     function do_remap()
         local ua = ts.client_request.header['User-Agent']
-        print(ua)
+        ts.debug(ua)
         ts.client_request.header['Host'] = 'a.tbcdn.cn'
     end
 
@@ -735,7 +756,7 @@ Here is an example:
     function do_remap()
         hdrs = ts.client_request.get_headers()
         for k, v in pairs(hdrs) do
-            print(k..': '..v)
+            ts.debug(k..': '..v)
         end
     end
 
@@ -764,9 +785,9 @@ Here is an example:
 
     function do_remap()
         ip, port, family = ts.client_request.client_addr.get_addr()
-        print(ip)               -- 192.168.231.17
-        print(port)             -- 17786
-        print(family)           -- 2(AF_INET)
+        ts.debug(ip)               -- 192.168.231.17
+        ts.debug(port)             -- 17786
+        ts.debug(family)           -- 2(AF_INET)
         return 0
     end
 
@@ -788,7 +809,7 @@ Here is an example:
 
     function do_global_read_request()
         port = ts.client_request.client_addr.get_incoming_port()
-        print(port)             -- 80
+        ts.debug(port)             -- 80
     end
 
 `TOP <#ts-lua-plugin>`_
@@ -807,7 +828,7 @@ Here is an example:
 
     function do_remap()
         local url_host = ts.client_request.get_url_host()
-        print(url_host)
+        ts.debug(url_host)
     end
 
 Then ``GET /liuyurou.txt HTTP/1.1\r\nHost: 192.168.231.129:8080\r\n...`` will yield the output:
@@ -860,7 +881,7 @@ Here is an example:
 
     function do_remap()
         local url_port = ts.client_request.get_url_port()
-        print(url_port)
+        ts.debug(url_port)
     end
 
 Then Then ``GET /liuyurou.txt HTTP/1.1\r\nHost: 192.168.231.129:8080\r\n...`` will yield the output:
@@ -896,7 +917,7 @@ Here is an example:
 
     function do_remap()
         local url_scheme = ts.client_request.get_url_scheme()
-        print(url_scheme)
+        ts.debug(url_scheme)
     end
 
 Then ``GET /liuyurou.txt HTTP/1.1\r\nHost: 192.168.231.129:8080\r\n...`` will yield the output:
@@ -1112,9 +1133,9 @@ 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')
+            ts.debug('hit')
         else
-            print('not hit')
+            ts.debug('not hit')
         end
     end
 
@@ -1141,9 +1162,9 @@ 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')
+            ts.debug('hit')
         else
-            print('not hit')
+            ts.debug('not hit')
         end
         ts.http.set_cache_lookup_status(TS_LUA_CACHE_LOOKUP_MISS)
     end
@@ -1187,7 +1208,7 @@ Here is an example:
         local cache_status = ts.http.get_cache_lookup_status()
         if cache_status == TS_LUA_CACHE_LOOKUP_HIT_FRESH then
             code = ts.cached_response.get_status()
-            print(code)         -- 200
+            ts.debug(code)         -- 200
         end
     end
 
@@ -1229,7 +1250,7 @@ Here is an example:
         local status = ts.http.get_cache_lookup_status()
         if status == TS_LUA_CACHE_LOOKUP_HIT_FRESH then
             local ct = ts.cached_response.header['Content-Type']
-            print(ct)         -- text/plain
+            ts.debug(ct)         -- text/plain
         end
     end
 
@@ -1258,7 +1279,7 @@ Here is an example:
         if status == TS_LUA_CACHE_LOOKUP_HIT_FRESH then
             hdrs = ts.cached_response.get_headers()
             for k, v in pairs(hdrs) do
-                print(k..': '..v)
+                ts.debug(k..': '..v)
             end
         end
     end
@@ -1299,7 +1320,7 @@ Here is an example:
 
     function send_request()
         local uri = ts.server_request.get_uri()
-        print(uri)
+        ts.debug(uri)
     end
 
     function do_remap()
@@ -1341,7 +1362,7 @@ Here is an example:
 
     function send_request()
         local query = ts.server_request.get_uri_args()
-        print(query)
+        ts.debug(query)
     end
 
     function do_remap()
@@ -1385,7 +1406,7 @@ Here is an example:
 
     function send_request()
         local query = ts.server_request.get_uri_params()
-        print(query)
+        ts.debug(query)
     end
 
     function do_remap()
@@ -1433,7 +1454,7 @@ Here is an example:
 
     function send_request()
         local ua = ts.server_request.header['User-Agent']
-        print(ua)
+        ts.debug(ua)
         ts.server_request.header['Accept-Encoding'] = 'gzip'
     end
 
@@ -1464,7 +1485,7 @@ Here is an example:
     function send_request()
         hdrs = ts.cached_response.get_headers()
         for k, v in pairs(hdrs) do
-            print(k..': '..v)
+            ts.debug(k..': '..v)
         end
     end
 
@@ -1532,9 +1553,9 @@ Here is an example:
 
     function do_global_send_request()
         ip, port, family = ts.server_request.server_addr.get_addr()
-        print(ip)               -- 192.168.231.17
-        print(port)             -- 80
-        print(family)           -- 2(AF_INET)
+        ts.debug(ip)               -- 192.168.231.17
+        ts.debug(port)             -- 80
+        ts.debug(family)           -- 2(AF_INET)
     end
 
 `TOP <#ts-lua-plugin>`_
@@ -1555,9 +1576,9 @@ Here is an example:
 
     function do_global_send_request()
         ip, port, family = ts.server_request.server_addr.get_nexthop_addr()
-        print(ip)               -- 192.168.231.17
-        print(port)             -- 80
-        print(family)           -- 2(AF_INET)
+        ts.debug(ip)               -- 192.168.231.17
+        ts.debug(port)             -- 80
+        ts.debug(family)           -- 2(AF_INET)
     end
 
 `TOP <#ts-lua-plugin>`_
@@ -1578,7 +1599,7 @@ Here is an example:
 
     function do_global_send_request()
         ip = ts.server_request.server_addr.get_ip()
-        print(ip)               -- 192.168.231.17
+        ts.debug(ip)               -- 192.168.231.17
     end
 
 `TOP <#ts-lua-plugin>`_
@@ -1599,7 +1620,7 @@ Here is an example:
 
     function do_global_send_request()
         port = ts.server_request.server_addr.get_port()
-        print(port)             -- 80
+        ts.debug(port)             -- 80
     end
 
 `TOP <#ts-lua-plugin>`_
@@ -1620,7 +1641,7 @@ Here is an example:
 
     function do_global_send_request()
         port = ts.server_request.server_addr.get_outgoing_port()
-        print(port)             -- 50880
+        ts.debug(port)             -- 50880
     end
 
 `TOP <#ts-lua-plugin>`_
@@ -1659,7 +1680,7 @@ Here is an example:
 
     function send_request()
         local url_host = ts.server_request.get_url_host()
-        print(url_host)
+        ts.debug(url_host)
     end
 
     function do_remap()
@@ -1737,7 +1758,7 @@ Here is an example:
 
     function send_request()
         local url_scheme = ts.server_request.get_url_scheme()
-        print(url_host)
+        ts.debug(url_host)
     end
 
     function do_remap()
@@ -1776,7 +1797,7 @@ Here is an example:
 
     function read_response()
         local code = ts.server_response.get_status()
-        print(code)         -- 200
+        ts.debug(code)         -- 200
     end
 
     function do_remap()
@@ -1855,7 +1876,7 @@ Here is an example:
 
     function read_response()
         local ct = ts.server_response.header['Content-Type']
-        print(ct)
+        ts.debug(ct)
         ts.server_response.header['Cache-Control'] = 'max-age=14400'
     end
 
@@ -1886,7 +1907,7 @@ Here is an example:
     function read_response()
         hdrs = ts.server_response.get_headers()
         for k, v in pairs(hdrs) do
-            print(k..': '..v)
+            ts.debug(k..': '..v)
         end
     end
 
@@ -1927,7 +1948,7 @@ Here is an example:
 
     function send_response()
         local code = ts.client_response.get_status()
-        print(code)         -- 200
+        ts.debug(code)         -- 200
     end
 
     function do_remap()
@@ -2006,7 +2027,7 @@ Here is an example:
 
     function send_response()
         local ct = ts.client_response.header['Content-Type']
-        print(ct)
+        ts.debug(ct)
         ts.client_response.header['Cache-Control'] = 'max-age=3600'
     end
 
@@ -2037,7 +2058,7 @@ Here is an example:
     function send_response()
         hdrs = ts.client_response.get_headers()
         for k, v in pairs(hdrs) do
-            print(k..': '..v)
+            ts.debug(k..': '..v)
         end
     end
 
@@ -2432,6 +2453,27 @@ Here is an example
 
 `TOP <#ts-lua-plugin>`_
 
+Server state constants
+----------------------
+**context:** global
+
+::
+
+    TS_LUA_SRVSTATE_STATE_UNDEFINED (0)
+    TS_LUA_SRVSTATE_ACTIVE_TIMEOUT (1)
+    TS_LUA_SRVSTATE_BAD_INCOMING_RESPONSE (2)
+    TS_LUA_SRVSTATE_CONNECTION_ALIVE (3)
+    TS_LUA_SRVSTATE_CONNECTION_CLOSED (4)
+    TS_LUA_SRVSTATE_CONNECTION_ERROR (5)
+    TS_LUA_SRVSTATE_INACTIVE_TIMEOUT(6)
+    TS_LUA_SRVSTATE_OPEN_RAW_ERROR (7)
+    TS_LUA_SRVSTATE_PARSE_ERROR (8)
+    TS_LUA_SRVSTATE_TRANSACTION_COMPLETE (9)
+    TS_LUA_SRVSTATE_CONGEST_CONTROL_CONGESTED_ON_F (10)
+    TS_LUA_SRVSTATE_CONGEST_CONTROL_CONGESTED_ON_M (11)
+
+`TOP <#ts-lua-plugin>`_
+
 ts.http.get_remap_from_url
 --------------------------
 **syntax:** *ts.http.get_remap_from_url()*
@@ -2470,27 +2512,43 @@ Here is an example
 
 `TOP <#ts-lua-plugin>`_
 
-Server state constants
-----------------------
-**context:** global
+ts.http.get_client_fd
+---------------------
+**syntax:** *ts.http.get_client_fd()*
+
+**context:** after do_global_read_request
+
+**description:** This function can be used to get the client FD for the transaction.
+
+Here is an example
 
 ::
 
-    TS_LUA_SRVSTATE_STATE_UNDEFINED (0)
-    TS_LUA_SRVSTATE_ACTIVE_TIMEOUT (1)
-    TS_LUA_SRVSTATE_BAD_INCOMING_RESPONSE (2)
-    TS_LUA_SRVSTATE_CONNECTION_ALIVE (3)
-    TS_LUA_SRVSTATE_CONNECTION_CLOSED (4)
-    TS_LUA_SRVSTATE_CONNECTION_ERROR (5)
-    TS_LUA_SRVSTATE_INACTIVE_TIMEOUT(6)
-    TS_LUA_SRVSTATE_OPEN_RAW_ERROR (7)
-    TS_LUA_SRVSTATE_PARSE_ERROR (8)
-    TS_LUA_SRVSTATE_TRANSACTION_COMPLETE (9)
-    TS_LUA_SRVSTATE_CONGEST_CONTROL_CONGESTED_ON_F (10)
-    TS_LUA_SRVSTATE_CONGEST_CONTROL_CONGESTED_ON_M (11)
+    function do_global_read_request()
+        local fd = ts.http.get_client_fd()
+        ts.debug(fd)
+    end
 
 `TOP <#ts-lua-plugin>`_
 
+ts.http.get_server_fd
+---------------------
+**syntax:** *ts.http.get_server_fd()*
+
+**context:** after do_global_send_request
+
+**description:** This function can be used to get the server (origin) FD for the transaction.
+
+Here is an example
+
+::
+
+    function do_global_send_request()
+        local fd = ts.http.get_server_fd()
+        ts.debug(fd)
+    end
+
+`TOP <#ts-lua-plugin>`_
 
 ts.add_package_path
 -------------------
@@ -2509,7 +2567,7 @@ Here is an example:
     ts.add_package_path('/home/a/test/lua/pac/?.lua')
     local nt = require("nt")
     function do_remap()
-        print(nt.t9(7979))
+        ts.debug(nt.t9(7979))
         return 0
     end
 
@@ -2532,7 +2590,7 @@ Here is an example:
     ts.add_package_cpath('/home/a/test/c/module/?.so')
     local ma = require("ma")
     function do_remap()
-        print(ma.ft())
+        ts.debug(ma.ft())
         return 0
     end
 
@@ -2554,8 +2612,8 @@ Here is an example:
 
     function do_remap()
         uri = ts.client_request.get_uri()
-        print(uri)              -- /foo
-        print(ts.md5(uri))      -- 1effb2475fcfba4f9e8b8a1dbc8f3caf
+        ts.debug(uri)              -- /foo
+        ts.debug(ts.md5(uri))      -- 1effb2475fcfba4f9e8b8a1dbc8f3caf
     end
 
 
@@ -2595,8 +2653,8 @@ Here is an example:
 
     function do_remap()
         uri = ts.client_request.get_uri()
-        print(uri)              -- /foo
-        print(ts.sha1(uri))     -- 6dbd548cc03e44b8b44b6e68e56255ce4273ae49
+        ts.debug(uri)              -- /foo
+        ts.debug(ts.sha1(uri))     -- 6dbd548cc03e44b8b44b6e68e56255ce4273ae49
     end
 
 
@@ -2728,7 +2786,7 @@ Here is a basic example:
         local url = string.format('http://%s/foo.txt', ts.ctx['host'])
         local res = ts.fetch(url)
         if res.status == 200 then
-            print(res.body)
+            ts.debug(res.body)
         end
     end
 
@@ -2780,7 +2838,7 @@ Here is an example:
                 })
 
     for i = 1, #(vec) do
-        print(vec[i].status)
+        ts.debug(vec[i].status)
     end
 
 
@@ -2818,7 +2876,7 @@ Here is an example:
                       'Cache-Control: max-age=7200\r\n' ..
                       'Accept-Ranges: bytes\r\n\r\n' ..
                       nt
-        print(dstr)
+        ts.debug(dstr)
         ts.say(resp)
     end
 
@@ -3016,6 +3074,30 @@ Here is an example:
 
 `TOP <#ts-lua-plugin>`_
 
+ts.host_lookup
+--------------
+**syntax:** *ts.host_lookup(hostname)*
+
+**context:** *hook point functions added after do_remap*
+
+**description:** Look for ip address of the host name without blocking. Nil if address cannot be found.
+
+Behind the scene, this method makes use of the ATS event model.
+
+Here is an example:
+
+::
+
+    function send_response()
+        local result = ts.host_lookup("www.xyz.com") -- ip address of www.xyz.com
+    end
+
+    function do_remap()
+        ts.hook(TS_LUA_HOOK_SEND_RESPONSE_HDR, send_response)
+    end
+
+`TOP <#ts-lua-plugin>`_
+
 ts.schedule
 -----------
 **syntax:** *ts.schedule(THREAD_TYPE, sec, FUNCTION, param1?, param2?, ...)*
diff --git a/plugins/experimental/ts_lua/ts_lua_http.c b/plugins/experimental/ts_lua/ts_lua_http.c
index cf93f3f..daf6f53 100644
--- a/plugins/experimental/ts_lua/ts_lua_http.c
+++ b/plugins/experimental/ts_lua/ts_lua_http.c
@@ -96,6 +96,9 @@ static int ts_lua_http_get_server_state(lua_State *L);
 static int ts_lua_http_get_remap_from_url(lua_State *L);
 static int ts_lua_http_get_remap_to_url(lua_State *L);
 
+static int ts_lua_http_get_server_fd(lua_State *L);
+static int ts_lua_http_get_client_fd(lua_State *L);
+
 static void ts_lua_inject_server_state_variables(lua_State *L);
 
 static void ts_lua_inject_http_resp_transform_api(lua_State *L);
@@ -232,6 +235,12 @@ ts_lua_inject_http_misc_api(lua_State *L)
   lua_pushcfunction(L, ts_lua_http_get_remap_to_url);
   lua_setfield(L, -2, "get_remap_to_url");
 
+  lua_pushcfunction(L, ts_lua_http_get_server_fd);
+  lua_setfield(L, -2, "get_server_fd");
+
+  lua_pushcfunction(L, ts_lua_http_get_client_fd);
+  lua_setfield(L, -2, "get_client_fd");
+
   ts_lua_inject_server_state_variables(L);
 }
 
@@ -789,6 +798,40 @@ done:
 }
 
 static int
+ts_lua_http_get_server_fd(lua_State *L)
+{
+  int fd;
+  ts_lua_http_ctx *http_ctx;
+
+  GET_HTTP_CONTEXT(http_ctx, L);
+
+  if (TSHttpTxnServerFdGet(http_ctx->txnp, &fd) != TS_SUCCESS) {
+    lua_pushnil(L);
+  } else {
+    lua_pushnumber(L, fd);
+  }
+
+  return 1;
+}
+
+static int
+ts_lua_http_get_client_fd(lua_State *L)
+{
+  int fd;
+  ts_lua_http_ctx *http_ctx;
+
+  GET_HTTP_CONTEXT(http_ctx, L);
+
+  if (TSHttpTxnClientFdGet(http_ctx->txnp, &fd) != TS_SUCCESS) {
+    lua_pushnil(L);
+  } else {
+    lua_pushnumber(L, fd);
+  }
+
+  return 1;
+}
+
+static int
 ts_lua_http_resp_transform_get_upstream_bytes(lua_State *L)
 {
   ts_lua_http_transform_ctx *transform_ctx;
diff --git a/plugins/experimental/ts_lua/ts_lua_misc.c b/plugins/experimental/ts_lua/ts_lua_misc.c
index e7260f4..380ea92 100644
--- a/plugins/experimental/ts_lua/ts_lua_misc.c
+++ b/plugins/experimental/ts_lua/ts_lua_misc.c
@@ -16,6 +16,8 @@
   limitations under the License.
 */
 
+#include "ts/ink_platform.h"
+#include <netinet/in.h>
 #include "ts_lua_util.h"
 
 static int ts_lua_get_process_id(lua_State *L);
@@ -23,10 +25,18 @@ static int ts_lua_get_now_time(lua_State *L);
 static int ts_lua_debug(lua_State *L);
 static int ts_lua_error(lua_State *L);
 static int ts_lua_sleep(lua_State *L);
+static int ts_lua_host_lookup(lua_State *L);
 static int ts_lua_schedule(lua_State *L);
+static int ts_lua_get_install_dir(lua_State *L);
+static int ts_lua_get_config_dir(lua_State *L);
+static int ts_lua_get_runtime_dir(lua_State *L);
+static int ts_lua_get_plugin_dir(lua_State *L);
+static int ts_lua_get_traffic_server_version(lua_State *L);
 
 static int ts_lua_sleep_cleanup(ts_lua_async_item *ai);
 static int ts_lua_sleep_handler(TSCont contp, TSEvent event, void *edata);
+static int ts_lua_host_lookup_cleanup(ts_lua_async_item *ai);
+static int ts_lua_host_lookup_handler(TSCont contp, TSEvent event, void *edata);
 static int ts_lua_schedule_handler(TSCont contp, TSEvent event, void *edata);
 
 static void ts_lua_inject_misc_variables(lua_State *L);
@@ -62,6 +72,30 @@ ts_lua_inject_misc_api(lua_State *L)
   lua_pushcfunction(L, ts_lua_schedule);
   lua_setfield(L, -2, "schedule");
 
+  /* ts.host_lookup(...) */
+  lua_pushcfunction(L, ts_lua_host_lookup);
+  lua_setfield(L, -2, "host_lookup");
+
+  /* ts.get_install_dir(...) */
+  lua_pushcfunction(L, ts_lua_get_install_dir);
+  lua_setfield(L, -2, "get_install_dir");
+
+  /* ts.get_config_dir(...) */
+  lua_pushcfunction(L, ts_lua_get_config_dir);
+  lua_setfield(L, -2, "get_config_dir");
+
+  /* ts.get_runtime_dir(...) */
+  lua_pushcfunction(L, ts_lua_get_runtime_dir);
+  lua_setfield(L, -2, "get_runtime_dir");
+
+  /* ts.get_plugin_dir(...) */
+  lua_pushcfunction(L, ts_lua_get_plugin_dir);
+  lua_setfield(L, -2, "get_plugin_dir");
+
+  /* ts.get_traffic_server_version(...) */
+  lua_pushcfunction(L, ts_lua_get_traffic_server_version);
+  lua_setfield(L, -2, "get_traffic_server_version");
+
   ts_lua_inject_misc_variables(L);
 }
 
@@ -163,7 +197,7 @@ ts_lua_schedule(lua_State *L)
   n = lua_gettop(L);
 
   if (n < 3) {
-    TSError("[ts_lua] ts.http.schedule need at least three params");
+    TSError("[ts_lua] ts.schedule need at least three parameters");
     return 0;
   }
 
@@ -291,3 +325,127 @@ ts_lua_sleep_cleanup(ts_lua_async_item *ai)
 
   return 0;
 }
+
+static int
+ts_lua_host_lookup(lua_State *L)
+{
+  const char *host;
+  size_t host_len = 0;
+  TSAction action;
+  TSCont contp;
+  ts_lua_async_item *ai;
+  ts_lua_cont_info *ci;
+
+  ci = ts_lua_get_cont_info(L);
+  if (ci == NULL) {
+    return 0;
+  }
+
+  if (lua_gettop(L) != 1) {
+    TSError("[ts_lua] ts.host_lookup need at least one parameter");
+    return 0;
+  }
+
+  host = luaL_checklstring(L, 1, &host_len);
+
+  contp  = TSContCreate(ts_lua_host_lookup_handler, ci->mutex);
+  action = TSHostLookup(contp, host, host_len);
+
+  ai = ts_lua_async_create_item(contp, ts_lua_host_lookup_cleanup, (void *)action, ci);
+  TSContDataSet(contp, ai);
+
+  return lua_yield(L, 0);
+}
+
+static int
+ts_lua_host_lookup_handler(TSCont contp, TSEvent event, void *edata)
+{
+  ts_lua_async_item *ai;
+  ts_lua_cont_info *ci;
+  struct sockaddr const *addr;
+  char cip[128];
+  lua_State *L;
+  ts_lua_coroutine *crt;
+
+  ai  = TSContDataGet(contp);
+  ci  = ai->cinfo;
+  crt = &ci->routine;
+  L   = crt->lua;
+
+  ai->data = NULL;
+  ts_lua_host_lookup_cleanup(ai);
+
+  if (event != TS_EVENT_HOST_LOOKUP) {
+    TSError("[ts_lua] ts.host_lookup receives unknown event");
+    lua_pushnil(L);
+  } else if (!edata) {
+    lua_pushnil(L);
+  } else {
+    TSHostLookupResult result = (TSHostLookupResult)edata;
+    addr                      = TSHostLookupResultAddrGet(result);
+    if (addr->sa_family == AF_INET) {
+      inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)addr)->sin_addr, cip, sizeof(cip));
+    } else {
+      inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)addr)->sin6_addr, cip, sizeof(cip));
+    }
+    lua_pushstring(L, cip);
+  }
+
+  TSContCall(ci->contp, TS_LUA_EVENT_COROUTINE_CONT, (void *)1);
+
+  return 0;
+}
+
+static int
+ts_lua_host_lookup_cleanup(ts_lua_async_item *ai)
+{
+  if (ai->data) {
+    TSActionCancel((TSAction)ai->data);
+    ai->data = NULL;
+  }
+
+  TSContDestroy(ai->contp);
+  ai->deleted = 1;
+
+  return 0;
+}
+
+static int
+ts_lua_get_install_dir(lua_State *L)
+{
+  const char *s = TSInstallDirGet();
+  lua_pushstring(L, s);
+  return 1;
+}
+
+static int
+ts_lua_get_config_dir(lua_State *L)
+{
+  const char *s = TSConfigDirGet();
+  lua_pushstring(L, s);
+  return 1;
+}
+
+static int
+ts_lua_get_runtime_dir(lua_State *L)
+{
+  const char *s = TSRuntimeDirGet();
+  lua_pushstring(L, s);
+  return 1;
+}
+
+static int
+ts_lua_get_plugin_dir(lua_State *L)
+{
+  const char *s = TSPluginDirGet();
+  lua_pushstring(L, s);
+  return 1;
+}
+
+static int
+ts_lua_get_traffic_server_version(lua_State *L)
+{
+  const char *s = TSTrafficServerVersionGet();
+  lua_pushstring(L, s);
+  return 1;
+}

-- 
To stop receiving notification emails like this one, please contact
kichan@apache.org.