You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@trafficserver.apache.org by James Peach <jp...@apache.org> on 2014/05/21 17:30:16 UTC

Re: git commit: TS-2555: add more hook support for ts_lua plugin

On May 20, 2014, at 9:59 PM, kichan@apache.org wrote:

> Repository: trafficserver
> Updated Branches:
>  refs/heads/master 90f44b522 -> 2ba2baaa6
> 
> 
> TS-2555: add more hook support for ts_lua plugin

I still think that declaring global functions for hooks is ugly and not Lua-like. Automatically re-enabling the transaction is also problem, since a very common pattern is to hook the transaction and reenable it later after doing some other work.

> 
> 
> Project: http://git-wip-us.apache.org/repos/asf/trafficserver/repo
> Commit: http://git-wip-us.apache.org/repos/asf/trafficserver/commit/2ba2baaa
> Tree: http://git-wip-us.apache.org/repos/asf/trafficserver/tree/2ba2baaa
> Diff: http://git-wip-us.apache.org/repos/asf/trafficserver/diff/2ba2baaa
> 
> Branch: refs/heads/master
> Commit: 2ba2baaa69c2bb99dfa95c783a79ec0165690c31
> Parents: 90f44b5
> Author: Kit Chan <ki...@apache.org>
> Authored: Tue May 20 21:50:49 2014 -0700
> Committer: Kit Chan <ki...@apache.org>
> Committed: Tue May 20 21:50:49 2014 -0700
> 
> ----------------------------------------------------------------------
> doc/reference/plugins/ts_lua.en.rst             |  60 +++++++----
> .../ts_lua/example/test_global_hook.lua         |  88 ++++++++++++++++
> .../ts_lua/example/test_txn_hook.lua            | 100 +++++++++++++++++++
> plugins/experimental/ts_lua/ts_lua.c            |  81 ++++++++++++++-
> plugins/experimental/ts_lua/ts_lua_common.h     |  16 +++
> plugins/experimental/ts_lua/ts_lua_hook.c       |  64 ++++++++++++
> plugins/experimental/ts_lua/ts_lua_util.c       |  84 ++++++++++++++++
> 7 files changed, 472 insertions(+), 21 deletions(-)
> ----------------------------------------------------------------------
> 
> 
> http://git-wip-us.apache.org/repos/asf/trafficserver/blob/2ba2baaa/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 ebcb88c..8049a53 100644
> --- a/doc/reference/plugins/ts_lua.en.rst
> +++ b/doc/reference/plugins/ts_lua.en.rst
> @@ -261,21 +261,34 @@ Description
> ===========
> 
> This module embeds Lua, into Apache Traffic Server. This module acts as remap plugin of Traffic Server. In this case we
> -should provide **'do_remap'** function in each lua script. We can write this in remap.config:::
> +should provide **'do_remap'** function in each lua script. We can write this in remap.config::
> 
> -     map http://a.tbcdn.cn/ http://inner.tbcdn.cn/ @plugin=/usr/lib64/trafficserver/plugins/tslua.so
> -@pparam=/etc/trafficserver/script/test_hdr.lua
> +     map http://a.tbcdn.cn/ http://inner.tbcdn.cn/ @plugin=/usr/lib64/trafficserver/plugins/tslua.so \
> +       @pparam=/etc/trafficserver/script/test_hdr.lua
> 
> Sometimes we want to receive parameters and process them in the script, we should realize **'\__init__'** function in
> -the lua script(sethost.lua is a reference), and we can write this in remap.config:::
> +the lua script(sethost.lua is a reference), and we can write this in remap.config::
> 
> -     map http://a.tbcdn.cn/ http://inner.tbcdn.cn/ @plugin=/usr/lib64/trafficserver/plugins/tslua.so
> -@pparam=/etc/trafficserver/script/sethost.lua @pparam=img03.tbcdn.cn
> +     map http://a.tbcdn.cn/ http://inner.tbcdn.cn/ @plugin=/usr/lib64/trafficserver/plugins/tslua.so \
> +       @pparam=/etc/trafficserver/script/sethost.lua @pparam=img03.tbcdn.cn
> 
> -This module can also act as a global plugin of Traffic Server. In this case we should provide one of these functions
> -(**'do_global_read_request'**, **'do_global_send_request'**, **'do_global_read_response'**,
> -**'do_global_send_response'**, **'do_global_cache_lookup_complete'**) in each lua script. We can write this in
> -plugin.config:::
> +This module can also act as a global plugin of Traffic Server. In this case we should provide one of these functions in
> +each lua script:
> +
> +- **'do_global_txn_start'**
> +- **'do_global_txn_close'**
> +- **'do_global_os_dns'**
> +- **'do_global_pre_remap'**
> +- **'do_global_post_remap'**
> +- **'do_global_read_request'**
> +- **'do_global_send_request'**
> +- **'do_global_read_response'**
> +- **'do_global_send_response'**
> +- **'do_global_cache_lookup_complete'**
> +- **'do_global_read_cache'**
> +- **'do_global_select_alt'**
> +
> +We can write this in plugin.config::
> 
>      tslua.so /etc/trafficserver/script/test_global_hdr.lua
> 
> @@ -350,12 +363,19 @@ Hook point constants
> --------------------
> **context**: do_remap/do_global_*/later
> 
> -    TS_LUA_HOOK_CACHE_LOOKUP_COMPLETE
> -    TS_LUA_HOOK_SEND_REQUEST_HDR
> -    TS_LUA_HOOK_READ_RESPONSE_HDR
> -    TS_LUA_HOOK_SEND_RESPONSE_HDR
> -    TS_LUA_REQUEST_TRANSFORM
> -    TS_LUA_RESPONSE_TRANSFORM
> +- TS_LUA_HOOK_OS_DNS
> +- TS_LUA_HOOK_PRE_REMAP
> +- TS_LUA_HOOK_POST_REMAP
> +- TS_LUA_HOOK_READ_CACHE_HDR
> +- TS_LUA_HOOK_SELECT_ALT
> +- TS_LUA_HOOK_TXN_CLOSE
> +- TS_LUA_HOOK_CACHE_LOOKUP_COMPLETE
> +- TS_LUA_HOOK_READ_REQUEST_HDR
> +- TS_LUA_HOOK_SEND_REQUEST_HDR
> +- TS_LUA_HOOK_READ_RESPONSE_HDR
> +- TS_LUA_HOOK_SEND_RESPONSE_HDR
> +- TS_LUA_REQUEST_TRANSFORM
> +- TS_LUA_RESPONSE_TRANSFORM
> 
> These constants are usually used in ts.hook method call.
> 
> @@ -417,10 +437,10 @@ Http cache lookup status constants
> ----------------------------------
> **context**: global
> 
> -    TS_LUA_CACHE_LOOKUP_MISS (0)
> -    TS_LUA_CACHE_LOOKUP_HIT_STALE (1)
> -    TS_LUA_CACHE_LOOKUP_HIT_FRESH (2)
> -    TS_LUA_CACHE_LOOKUP_SKIPPED (3)
> +- TS_LUA_CACHE_LOOKUP_MISS (0)
> +- TS_LUA_CACHE_LOOKUP_HIT_STALE (1)
> +- TS_LUA_CACHE_LOOKUP_HIT_FRESH (2)
> +- TS_LUA_CACHE_LOOKUP_SKIPPED (3)
> 
> 
> ts.http.set_cache_url
> 
> http://git-wip-us.apache.org/repos/asf/trafficserver/blob/2ba2baaa/plugins/experimental/ts_lua/example/test_global_hook.lua
> ----------------------------------------------------------------------
> diff --git a/plugins/experimental/ts_lua/example/test_global_hook.lua b/plugins/experimental/ts_lua/example/test_global_hook.lua
> new file mode 100644
> index 0000000..bf71f0c
> --- /dev/null
> +++ b/plugins/experimental/ts_lua/example/test_global_hook.lua
> @@ -0,0 +1,88 @@
> +--  Licensed to the Apache Software Foundation (ASF) under one
> +--  or more contributor license agreements.  See the NOTICE file
> +--  distributed with this work for additional information
> +--  regarding copyright ownership.  The ASF licenses this file
> +--  to you under the Apache License, Version 2.0 (the
> +--  "License"); you may not use this file except in compliance
> +--  with the License.  You may obtain a copy of the License at
> +--
> +--  http://www.apache.org/licenses/LICENSE-2.0
> +--
> +--  Unless required by applicable law or agreed to in writing, software
> +--  distributed under the License is distributed on an "AS IS" BASIS,
> +--  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
> +--  See the License for the specific language governing permissions and
> +--  limitations under the License.
> +
> +function do_global_txn_start()
> +    ts.debug('txn_start')
> +
> +    return 0
> +end 
> +
> +function do_global_read_request()
> +    ts.debug('read_request')
> +
> +    return 0
> +end
> +
> +function do_global_send_request()
> +    ts.debug('send_request')
> +
> +    return 0
> +end
> +
> +function do_global_read_response()
> +    ts.debug('read_response')
> +
> +    return 0
> +end
> +
> +function do_global_send_response()
> +    ts.debug('send_response')
> +
> +    return 0
> +end
> +
> +function do_global_post_remap()
> +    ts.debug('post_remap')
> +
> +    return 0
> +end
> +
> +function do_global_pre_remap()
> +    ts.debug('pre_remap')
> +
> +    return 0
> +end
> +
> +function do_global_os_dns()
> +    ts.debug('os_dns')
> +
> +    return 0
> +end
> +
> +function do_global_cache_lookup_complete()
> +    ts.debug('cache_lookup_complete')
> +  
> +    return 0
> +end
> +
> +function do_global_select_alt()
> +    ts.debug('select_alt')
> +
> +    return 0
> +end
> +
> +function do_global_read_cache()
> +    ts.debug('read_cache')
> +
> +    return 0
> +end
> +
> +function do_global_txn_close()
> +    ts.debug('txn_close')
> +
> +    return 0
> +end
> +
> 
> http://git-wip-us.apache.org/repos/asf/trafficserver/blob/2ba2baaa/plugins/experimental/ts_lua/example/test_txn_hook.lua
> ----------------------------------------------------------------------
> diff --git a/plugins/experimental/ts_lua/example/test_txn_hook.lua b/plugins/experimental/ts_lua/example/test_txn_hook.lua
> new file mode 100644
> index 0000000..e95a4df
> --- /dev/null
> +++ b/plugins/experimental/ts_lua/example/test_txn_hook.lua
> @@ -0,0 +1,100 @@
> +--  Licensed to the Apache Software Foundation (ASF) under one
> +--  or more contributor license agreements.  See the NOTICE file
> +--  distributed with this work for additional information
> +--  regarding copyright ownership.  The ASF licenses this file
> +--  to you under the Apache License, Version 2.0 (the
> +--  "License"); you may not use this file except in compliance
> +--  with the License.  You may obtain a copy of the License at
> +--
> +--  http://www.apache.org/licenses/LICENSE-2.0
> +--
> +--  Unless required by applicable law or agreed to in writing, software
> +--  distributed under the License is distributed on an "AS IS" BASIS,
> +--  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
> +--  See the License for the specific language governing permissions and
> +--  limitations under the License.
> +
> +function do_global_txn_start()
> +    ts.debug('global_txn_start')
> +
> +    ts.hook(TS_LUA_HOOK_READ_REQUEST_HDR, read_request)
> +    ts.hook(TS_LUA_HOOK_SEND_REQUEST_HDR, send_request)
> +    ts.hook(TS_LUA_HOOK_SEND_RESPONSE_HDR, send_response)
> +    ts.hook(TS_LUA_HOOK_READ_RESPONSE_HDR, read_response)
> +    ts.hook(TS_LUA_HOOK_CACHE_LOOKUP_COMPLETE, cache_lookup)
> +    ts.hook(TS_LUA_HOOK_PRE_REMAP, pre_remap)
> +    ts.hook(TS_LUA_HOOK_POST_REMAP, post_remap)
> +    ts.hook(TS_LUA_HOOK_SELECT_ALT, select_alt)
> +    ts.hook(TS_LUA_HOOK_OS_DNS, os_dns)
> +    ts.hook(TS_LUA_HOOK_READ_CACHE_HDR, read_cache)
> +    ts.hook(TS_LUA_HOOK_TXN_CLOSE, txn_close)
> +
> +    return 0
> +end 
> +
> +function read_request()
> +    ts.debug('read_request')
> +
> +    return 0
> +end
> +
> +function send_request()
> +    ts.debug('send_request')
> +
> +    return 0
> +end
> +
> +function read_response()
> +    ts.debug('read_response')
> +
> +    return 0
> +end
> +
> +function send_response()
> +    ts.debug('send_response')
> +
> +    return 0
> +end
> +
> +function post_remap()
> +    ts.debug('post_remap')
> +
> +    return 0
> +end
> +
> +function pre_remap()
> +    ts.debug('pre_remap')
> +
> +    return 0
> +end
> +
> +function os_dns()
> +    ts.debug('os_dns')
> +
> +    return 0
> +end
> +
> +function cache_lookup()
> +    ts.debug('cache_lookup_complete')
> +  
> +    return 0
> +end
> +
> +function select_alt()
> +    ts.debug('select_alt')
> +
> +    return 0
> +end
> +
> +function read_cache()
> +    ts.debug('read_cache')
> +
> +    return 0
> +end
> +
> +function txn_close()
> +    ts.debug('txn_close')
> +
> +    return 0
> +end
> +
> 
> http://git-wip-us.apache.org/repos/asf/trafficserver/blob/2ba2baaa/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 b23783e..51703b7 100644
> --- a/plugins/experimental/ts_lua/ts_lua.c
> +++ b/plugins/experimental/ts_lua/ts_lua.c
> @@ -202,6 +202,34 @@ globalHookHandler(TSCont contp, TSEvent event, void *edata)
>     lua_getglobal(l, TS_LUA_FUNCTION_G_CACHE_LOOKUP_COMPLETE);
>     break;
> 
> +  case TS_EVENT_HTTP_TXN_START:
> +    lua_getglobal(l, TS_LUA_FUNCTION_G_TXN_START);
> +    break;
> +
> +  case TS_EVENT_HTTP_PRE_REMAP:
> +    lua_getglobal(l, TS_LUA_FUNCTION_G_PRE_REMAP);
> +    break;
> +
> +  case TS_EVENT_HTTP_POST_REMAP:
> +    lua_getglobal(l, TS_LUA_FUNCTION_G_POST_REMAP);
> +    break;
> +
> +  case TS_EVENT_HTTP_SELECT_ALT:
> +    lua_getglobal(l, TS_LUA_FUNCTION_G_SELECT_ALT);
> +    break;
> +
> +  case TS_EVENT_HTTP_OS_DNS:
> +    lua_getglobal(l, TS_LUA_FUNCTION_G_OS_DNS);
> +    break;
> +
> +  case TS_EVENT_HTTP_READ_CACHE_HDR:
> +    lua_getglobal(l, TS_LUA_FUNCTION_G_READ_CACHE);
> +    break;
> +
> +  case TS_EVENT_HTTP_TXN_CLOSE:
> +    lua_getglobal(l, TS_LUA_FUNCTION_G_TXN_CLOSE);
> +    break;
> +
>   default:
>     TSHttpTxnReenable(txnp, TS_EVENT_HTTP_CONTINUE);
>     return 0;
> @@ -252,7 +280,6 @@ transactionStartHookHandler(TSCont contp, TSEvent event ATS_UNUSED, void *edata)
>   txn_contp = TSContCreate(ts_lua_http_cont_handler, NULL);
>   TSContDataSet(txn_contp, http_ctx);
>   http_ctx->main_contp = txn_contp;
> -  TSHttpTxnHookAdd(txnp, TS_HTTP_TXN_CLOSE_HOOK, txn_contp);
> 
>   global_contp = TSContCreate(globalHookHandler, NULL);
>   TSContDataSet(global_contp, http_ctx);
> @@ -295,6 +322,58 @@ transactionStartHookHandler(TSCont contp, TSEvent event ATS_UNUSED, void *edata)
>   }
>   lua_pop(l, 1);
> 
> +  lua_getglobal(l, TS_LUA_FUNCTION_G_TXN_START);
> +  if (lua_type(l, -1) == LUA_TFUNCTION) {
> +    TSHttpTxnHookAdd(txnp, TS_HTTP_TXN_START_HOOK, global_contp);
> +    TSDebug(TS_LUA_DEBUG_TAG, "txn_start_hook added");
> +  }
> +  lua_pop(l, 1);
> +
> +  lua_getglobal(l, TS_LUA_FUNCTION_G_PRE_REMAP);
> +  if (lua_type(l, -1) == LUA_TFUNCTION) {
> +    TSHttpTxnHookAdd(txnp, TS_HTTP_PRE_REMAP_HOOK, global_contp);
> +    TSDebug(TS_LUA_DEBUG_TAG, "pre_remap_hook added");
> +  }
> +  lua_pop(l, 1);
> +
> +  lua_getglobal(l, TS_LUA_FUNCTION_G_POST_REMAP);
> +  if (lua_type(l, -1) == LUA_TFUNCTION) {
> +    TSHttpTxnHookAdd(txnp, TS_HTTP_POST_REMAP_HOOK, global_contp);
> +    TSDebug(TS_LUA_DEBUG_TAG, "post_remap_hook added");
> +  }
> +  lua_pop(l, 1);
> +
> +  lua_getglobal(l, TS_LUA_FUNCTION_G_SELECT_ALT);
> +  if (lua_type(l, -1) == LUA_TFUNCTION) {
> +    TSHttpTxnHookAdd(txnp, TS_HTTP_SELECT_ALT_HOOK, global_contp);
> +    TSDebug(TS_LUA_DEBUG_TAG, "select_alt_hook added");
> +  }
> +  lua_pop(l, 1);
> +
> +  lua_getglobal(l, TS_LUA_FUNCTION_G_OS_DNS);
> +  if (lua_type(l, -1) == LUA_TFUNCTION) {
> +    TSHttpTxnHookAdd(txnp, TS_HTTP_OS_DNS_HOOK, global_contp);
> +    TSDebug(TS_LUA_DEBUG_TAG, "os_dns_hook added");
> +  }
> +  lua_pop(l, 1);
> +
> +  lua_getglobal(l, TS_LUA_FUNCTION_G_READ_CACHE);
> +  if (lua_type(l, -1) == LUA_TFUNCTION) {
> +    TSHttpTxnHookAdd(txnp, TS_HTTP_READ_CACHE_HDR_HOOK, global_contp);
> +    TSDebug(TS_LUA_DEBUG_TAG, "read_cache_hdr_hook added");
> +  }
> +  lua_pop(l, 1);
> +
> +  lua_getglobal(l, TS_LUA_FUNCTION_G_TXN_CLOSE);
> +  if (lua_type(l, -1) == LUA_TFUNCTION) {
> +    TSHttpTxnHookAdd(txnp, TS_HTTP_TXN_CLOSE_HOOK, global_contp);
> +    TSDebug(TS_LUA_DEBUG_TAG, "txn_close_hook added");
> +  }
> +  lua_pop(l, 1);
> +
> +  // add a hook to release resources for context
> +  TSHttpTxnHookAdd(txnp, TS_HTTP_TXN_CLOSE_HOOK, txn_contp);
> +
>   TSMutexUnlock(main_ctx->mutexp);
> 
>   TSHttpTxnReenable(txnp, TS_EVENT_HTTP_CONTINUE);
> 
> http://git-wip-us.apache.org/repos/asf/trafficserver/blob/2ba2baaa/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 c4a32b0..c96f36a 100644
> --- a/plugins/experimental/ts_lua/ts_lua_common.h
> +++ b/plugins/experimental/ts_lua/ts_lua_common.h
> @@ -40,12 +40,28 @@
> #define TS_LUA_FUNCTION_SEND_REQUEST            "do_send_request"
> #define TS_LUA_FUNCTION_READ_RESPONSE           "do_read_response"
> #define TS_LUA_FUNCTION_SEND_RESPONSE           "do_send_response"
> +#define TS_LUA_FUNCTION_READ_REQUEST            "do_read_request"
> +#define TS_LUA_FUNCTION_TXN_START               "do_txn_start"
> +#define TS_LUA_FUNCTION_PRE_REMAP               "do_pre_remap"
> +#define TS_LUA_FUNCTION_POST_REMAP              "do_post_remap"
> +#define TS_LUA_FUNCTION_OS_DNS                  "do_os_dns"
> +#define TS_LUA_FUNCTION_SELECT_ALT              "do_select_alt"
> +#define TS_LUA_FUNCTION_READ_CACHE              "do_read_cache"
> +#define TS_LUA_FUNCTION_TXN_CLOSE               "do_txn_close"
> 
> #define TS_LUA_FUNCTION_G_SEND_REQUEST "do_global_send_request"
> #define TS_LUA_FUNCTION_G_READ_REQUEST "do_global_read_request"
> #define TS_LUA_FUNCTION_G_SEND_RESPONSE "do_global_send_response"
> #define TS_LUA_FUNCTION_G_READ_RESPONSE "do_global_read_response"
> #define TS_LUA_FUNCTION_G_CACHE_LOOKUP_COMPLETE "do_global_cache_lookup_complete"
> +#define TS_LUA_FUNCTION_G_TXN_START "do_global_txn_start"
> +#define TS_LUA_FUNCTION_G_PRE_REMAP "do_global_pre_remap"
> +#define TS_LUA_FUNCTION_G_POST_REMAP "do_global_post_remap"
> +#define TS_LUA_FUNCTION_G_OS_DNS "do_global_os_dns"
> +#define TS_LUA_FUNCTION_G_SELECT_ALT "do_global_select_alt"
> +#define TS_LUA_FUNCTION_G_READ_CACHE "do_global_read_cache"
> +#define TS_LUA_FUNCTION_G_TXN_CLOSE "do_global_txn_close"
> +
> 
> #define TS_LUA_MAX_SCRIPT_FNAME_LENGTH      1024
> #define TS_LUA_MAX_URL_LENGTH               2048
> 
> http://git-wip-us.apache.org/repos/asf/trafficserver/blob/2ba2baaa/plugins/experimental/ts_lua/ts_lua_hook.c
> ----------------------------------------------------------------------
> diff --git a/plugins/experimental/ts_lua/ts_lua_hook.c b/plugins/experimental/ts_lua/ts_lua_hook.c
> index 79f6ea7..e70312c 100644
> --- a/plugins/experimental/ts_lua/ts_lua_hook.c
> +++ b/plugins/experimental/ts_lua/ts_lua_hook.c
> @@ -29,6 +29,14 @@ typedef enum
>   TS_LUA_HOOK_SEND_REQUEST_HDR,
>   TS_LUA_HOOK_READ_RESPONSE_HDR,
>   TS_LUA_HOOK_SEND_RESPONSE_HDR,
> +  TS_LUA_HOOK_READ_REQUEST_HDR,
> +  TS_LUA_HOOK_TXN_START,
> +  TS_LUA_HOOK_PRE_REMAP,
> +  TS_LUA_HOOK_POST_REMAP,
> +  TS_LUA_HOOK_OS_DNS,
> +  TS_LUA_HOOK_SELECT_ALT,
> +  TS_LUA_HOOK_READ_CACHE_HDR,
> +  TS_LUA_HOOK_TXN_CLOSE,
>   TS_LUA_REQUEST_TRANSFORM,
>   TS_LUA_RESPONSE_TRANSFORM,
>   TS_LUA_HOOK_LAST
> @@ -41,6 +49,14 @@ char *ts_lua_hook_id_string[] = {
>   "TS_LUA_HOOK_SEND_REQUEST_HDR",
>   "TS_LUA_HOOK_READ_RESPONSE_HDR",
>   "TS_LUA_HOOK_SEND_RESPONSE_HDR",
> +  "TS_LUA_HOOK_READ_REQUEST_HDR",
> +  "TS_LUA_HOOK_TXN_START",
> +  "TS_LUA_HOOK_PRE_REMAP",
> +  "TS_LUA_HOOK_POST_REMAP",
> +  "TS_LUA_HOOK_OS_DNS",
> +  "TS_LUA_HOOK_SELECT_ALT",
> +  "TS_LUA_HOOK_READ_CACHE_HDR",
> +  "TS_LUA_HOOK_TXN_CLOSE",
>   "TS_LUA_REQUEST_TRANSFORM",
>   "TS_LUA_RESPONSE_TRANSFORM",
>   "TS_LUA_HOOK_LAST"
> @@ -115,6 +131,54 @@ ts_lua_add_hook(lua_State * L)
>     lua_setglobal(L, TS_LUA_FUNCTION_SEND_RESPONSE);
>     break;
> 
> +  case TS_LUA_HOOK_READ_REQUEST_HDR:
> +    TSHttpTxnHookAdd(http_ctx->txnp, TS_HTTP_READ_REQUEST_HDR_HOOK, http_ctx->main_contp);
> +    lua_pushvalue(L, 2);
> +    lua_setglobal(L, TS_LUA_FUNCTION_READ_REQUEST);
> +    break;
> +
> +  case TS_LUA_HOOK_TXN_START:
> +    TSHttpTxnHookAdd(http_ctx->txnp, TS_HTTP_TXN_START_HOOK, http_ctx->main_contp);
> +    lua_pushvalue(L, 2);
> +    lua_setglobal(L, TS_LUA_FUNCTION_TXN_START);
> +    break;
> +
> +  case TS_LUA_HOOK_PRE_REMAP:
> +    TSHttpTxnHookAdd(http_ctx->txnp, TS_HTTP_PRE_REMAP_HOOK, http_ctx->main_contp);
> +    lua_pushvalue(L, 2);
> +    lua_setglobal(L, TS_LUA_FUNCTION_PRE_REMAP);
> +    break;
> +
> +  case TS_LUA_HOOK_POST_REMAP:
> +    TSHttpTxnHookAdd(http_ctx->txnp, TS_HTTP_POST_REMAP_HOOK, http_ctx->main_contp);
> +    lua_pushvalue(L, 2);
> +    lua_setglobal(L, TS_LUA_FUNCTION_POST_REMAP);
> +    break;
> +
> +  case TS_LUA_HOOK_OS_DNS:
> +    TSHttpTxnHookAdd(http_ctx->txnp, TS_HTTP_OS_DNS_HOOK, http_ctx->main_contp);
> +    lua_pushvalue(L, 2);
> +    lua_setglobal(L, TS_LUA_FUNCTION_OS_DNS);
> +    break;
> +
> +  case TS_LUA_HOOK_SELECT_ALT:
> +    TSHttpTxnHookAdd(http_ctx->txnp, TS_HTTP_SELECT_ALT_HOOK, http_ctx->main_contp);
> +    lua_pushvalue(L, 2);
> +    lua_setglobal(L, TS_LUA_FUNCTION_SELECT_ALT);
> +    break;
> +
> +  case TS_LUA_HOOK_READ_CACHE_HDR:
> +    TSHttpTxnHookAdd(http_ctx->txnp, TS_HTTP_READ_CACHE_HDR_HOOK, http_ctx->main_contp);
> +    lua_pushvalue(L, 2);
> +    lua_setglobal(L, TS_LUA_FUNCTION_READ_CACHE);
> +    break;
> +
> +  case TS_LUA_HOOK_TXN_CLOSE:
> +    // we don't need to add a hook because we already have added one by default
> +    lua_pushvalue(L, 2);
> +    lua_setglobal(L, TS_LUA_FUNCTION_TXN_CLOSE);
> +    break;
> +
>   case TS_LUA_REQUEST_TRANSFORM:
>   case TS_LUA_RESPONSE_TRANSFORM:
>     transform_ctx = (ts_lua_transform_ctx *) TSmalloc(sizeof(ts_lua_transform_ctx));
> 
> http://git-wip-us.apache.org/repos/asf/trafficserver/blob/2ba2baaa/plugins/experimental/ts_lua/ts_lua_util.c
> ----------------------------------------------------------------------
> diff --git a/plugins/experimental/ts_lua/ts_lua_util.c b/plugins/experimental/ts_lua/ts_lua_util.c
> index 0d0a0ec..010a31d 100644
> --- a/plugins/experimental/ts_lua/ts_lua_util.c
> +++ b/plugins/experimental/ts_lua/ts_lua_util.c
> @@ -444,7 +444,91 @@ ts_lua_http_cont_handler(TSCont contp, TSEvent event, void *edata)
> 
>     break;
> 
> +  case TS_EVENT_HTTP_READ_REQUEST_HDR:
> +
> +    lua_getglobal(l, TS_LUA_FUNCTION_READ_REQUEST);
> +    if (lua_type(l, -1) == LUA_TFUNCTION) {
> +      if (lua_pcall(l, 0, 1, 0)) {
> +        fprintf(stderr, "lua_pcall failed: %s\n", lua_tostring(l, -1));
> +      }
> +    }
> +
> +    break;
> +
> +  case TS_EVENT_HTTP_TXN_START:
> +
> +    lua_getglobal(l, TS_LUA_FUNCTION_TXN_START);
> +    if (lua_type(l, -1) == LUA_TFUNCTION) {
> +      if (lua_pcall(l, 0, 1, 0)) {
> +        fprintf(stderr, "lua_pcall failed: %s\n", lua_tostring(l, -1));
> +      }
> +    }
> +
> +    break;
> +
> +  case TS_EVENT_HTTP_PRE_REMAP:
> +
> +    lua_getglobal(l, TS_LUA_FUNCTION_PRE_REMAP);
> +    if (lua_type(l, -1) == LUA_TFUNCTION) {
> +      if (lua_pcall(l, 0, 1, 0)) {
> +        fprintf(stderr, "lua_pcall failed: %s\n", lua_tostring(l, -1));
> +      }
> +    }
> +
> +    break;
> +
> +  case TS_EVENT_HTTP_POST_REMAP:
> +
> +    lua_getglobal(l, TS_LUA_FUNCTION_POST_REMAP);
> +    if (lua_type(l, -1) == LUA_TFUNCTION) {
> +      if (lua_pcall(l, 0, 1, 0)) {
> +        fprintf(stderr, "lua_pcall failed: %s\n", lua_tostring(l, -1));
> +      }
> +    }
> +
> +    break;
> +
> +  case TS_EVENT_HTTP_OS_DNS:
> +
> +    lua_getglobal(l, TS_LUA_FUNCTION_OS_DNS);
> +    if (lua_type(l, -1) == LUA_TFUNCTION) {
> +      if (lua_pcall(l, 0, 1, 0)) {
> +        fprintf(stderr, "lua_pcall failed: %s\n", lua_tostring(l, -1));
> +      }
> +    }
> +
> +    break;
> +
> +  case TS_EVENT_HTTP_SELECT_ALT:
> +
> +    lua_getglobal(l, TS_LUA_FUNCTION_SELECT_ALT);
> +    if (lua_type(l, -1) == LUA_TFUNCTION) {
> +      if (lua_pcall(l, 0, 1, 0)) {
> +        fprintf(stderr, "lua_pcall failed: %s\n", lua_tostring(l, -1));
> +      }
> +    }
> +
> +    break;
> +
> +  case TS_EVENT_HTTP_READ_CACHE_HDR:
> +
> +    lua_getglobal(l, TS_LUA_FUNCTION_READ_CACHE);
> +    if (lua_type(l, -1) == LUA_TFUNCTION) {
> +      if (lua_pcall(l, 0, 1, 0)) {
> +        fprintf(stderr, "lua_pcall failed: %s\n", lua_tostring(l, -1));
> +      }
> +    }
> +
> +    break;
> +
>   case TS_EVENT_HTTP_TXN_CLOSE:
> +    lua_getglobal(l, TS_LUA_FUNCTION_TXN_CLOSE);
> +    if (lua_type(l, -1) == LUA_TFUNCTION) {
> +      if (lua_pcall(l, 0, 1, 0)) {
> +        fprintf(stderr, "lua_pcall failed: %s\n", lua_tostring(l, -1));
> +      }
> +    }
> +
>     ts_lua_destroy_http_ctx(http_ctx);
>     TSContDestroy(contp);
>     break;
> 


Re: git commit: TS-2555: add more hook support for ts_lua plugin

Posted by Shu Kit Chan <ch...@gmail.com>.
I agree that global functions for hooks is ugly. I can add similar thing as
the lua plugin and allow registering hooks outside of global functions
before 5.0.0 release.

Quehan also has a patch with the idea of using the return value of the
registered function of hooks to control the reenabling of the transaction.
0 is to continue while 1 is error. I can add that in as well.

These are really good points. any more suggestions?

Kit



On Wed, May 21, 2014 at 8:30 AM, James Peach <jp...@apache.org> wrote:

> On May 20, 2014, at 9:59 PM, kichan@apache.org wrote:
>
> > Repository: trafficserver
> > Updated Branches:
> >  refs/heads/master 90f44b522 -> 2ba2baaa6
> >
> >
> > TS-2555: add more hook support for ts_lua plugin
>
> I still think that declaring global functions for hooks is ugly and not
> Lua-like. Automatically re-enabling the transaction is also problem, since
> a very common pattern is to hook the transaction and reenable it later
> after doing some other work.
>
> >
> >
> > Project: http://git-wip-us.apache.org/repos/asf/trafficserver/repo
> > Commit:
> http://git-wip-us.apache.org/repos/asf/trafficserver/commit/2ba2baaa
> > Tree: http://git-wip-us.apache.org/repos/asf/trafficserver/tree/2ba2baaa
> > Diff: http://git-wip-us.apache.org/repos/asf/trafficserver/diff/2ba2baaa
> >
> > Branch: refs/heads/master
> > Commit: 2ba2baaa69c2bb99dfa95c783a79ec0165690c31
> > Parents: 90f44b5
> > Author: Kit Chan <ki...@apache.org>
> > Authored: Tue May 20 21:50:49 2014 -0700
> > Committer: Kit Chan <ki...@apache.org>
> > Committed: Tue May 20 21:50:49 2014 -0700
> >
> > ----------------------------------------------------------------------
> > doc/reference/plugins/ts_lua.en.rst             |  60 +++++++----
> > .../ts_lua/example/test_global_hook.lua         |  88 ++++++++++++++++
> > .../ts_lua/example/test_txn_hook.lua            | 100 +++++++++++++++++++
> > plugins/experimental/ts_lua/ts_lua.c            |  81 ++++++++++++++-
> > plugins/experimental/ts_lua/ts_lua_common.h     |  16 +++
> > plugins/experimental/ts_lua/ts_lua_hook.c       |  64 ++++++++++++
> > plugins/experimental/ts_lua/ts_lua_util.c       |  84 ++++++++++++++++
> > 7 files changed, 472 insertions(+), 21 deletions(-)
> > ----------------------------------------------------------------------
> >
> >
> >
> http://git-wip-us.apache.org/repos/asf/trafficserver/blob/2ba2baaa/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 ebcb88c..8049a53 100644
> > --- a/doc/reference/plugins/ts_lua.en.rst
> > +++ b/doc/reference/plugins/ts_lua.en.rst
> > @@ -261,21 +261,34 @@ Description
> > ===========
> >
> > This module embeds Lua, into Apache Traffic Server. This module acts as
> remap plugin of Traffic Server. In this case we
> > -should provide **'do_remap'** function in each lua script. We can write
> this in remap.config:::
> > +should provide **'do_remap'** function in each lua script. We can write
> this in remap.config::
> >
> > -     map http://a.tbcdn.cn/ http://inner.tbcdn.cn/@plugin=/usr/lib64/trafficserver/plugins/tslua.so
> > -@pparam=/etc/trafficserver/script/test_hdr.lua
> > +     map http://a.tbcdn.cn/ http://inner.tbcdn.cn/@plugin=/usr/lib64/trafficserver/plugins/tslua.so \
> > +       @pparam=/etc/trafficserver/script/test_hdr.lua
> >
> > Sometimes we want to receive parameters and process them in the script,
> we should realize **'\__init__'** function in
> > -the lua script(sethost.lua is a reference), and we can write this in
> remap.config:::
> > +the lua script(sethost.lua is a reference), and we can write this in
> remap.config::
> >
> > -     map http://a.tbcdn.cn/ http://inner.tbcdn.cn/@plugin=/usr/lib64/trafficserver/plugins/tslua.so
> > -@pparam=/etc/trafficserver/script/sethost.lua @pparam=img03.tbcdn.cn
> > +     map http://a.tbcdn.cn/ http://inner.tbcdn.cn/@plugin=/usr/lib64/trafficserver/plugins/tslua.so \
> > +       @pparam=/etc/trafficserver/script/sethost.lua @pparam=
> img03.tbcdn.cn
> >
> > -This module can also act as a global plugin of Traffic Server. In this
> case we should provide one of these functions
> > -(**'do_global_read_request'**, **'do_global_send_request'**,
> **'do_global_read_response'**,
> > -**'do_global_send_response'**, **'do_global_cache_lookup_complete'**)
> in each lua script. We can write this in
> > -plugin.config:::
> > +This module can also act as a global plugin of Traffic Server. In this
> case we should provide one of these functions in
> > +each lua script:
> > +
> > +- **'do_global_txn_start'**
> > +- **'do_global_txn_close'**
> > +- **'do_global_os_dns'**
> > +- **'do_global_pre_remap'**
> > +- **'do_global_post_remap'**
> > +- **'do_global_read_request'**
> > +- **'do_global_send_request'**
> > +- **'do_global_read_response'**
> > +- **'do_global_send_response'**
> > +- **'do_global_cache_lookup_complete'**
> > +- **'do_global_read_cache'**
> > +- **'do_global_select_alt'**
> > +
> > +We can write this in plugin.config::
> >
> >      tslua.so /etc/trafficserver/script/test_global_hdr.lua
> >
> > @@ -350,12 +363,19 @@ Hook point constants
> > --------------------
> > **context**: do_remap/do_global_*/later
> >
> > -    TS_LUA_HOOK_CACHE_LOOKUP_COMPLETE
> > -    TS_LUA_HOOK_SEND_REQUEST_HDR
> > -    TS_LUA_HOOK_READ_RESPONSE_HDR
> > -    TS_LUA_HOOK_SEND_RESPONSE_HDR
> > -    TS_LUA_REQUEST_TRANSFORM
> > -    TS_LUA_RESPONSE_TRANSFORM
> > +- TS_LUA_HOOK_OS_DNS
> > +- TS_LUA_HOOK_PRE_REMAP
> > +- TS_LUA_HOOK_POST_REMAP
> > +- TS_LUA_HOOK_READ_CACHE_HDR
> > +- TS_LUA_HOOK_SELECT_ALT
> > +- TS_LUA_HOOK_TXN_CLOSE
> > +- TS_LUA_HOOK_CACHE_LOOKUP_COMPLETE
> > +- TS_LUA_HOOK_READ_REQUEST_HDR
> > +- TS_LUA_HOOK_SEND_REQUEST_HDR
> > +- TS_LUA_HOOK_READ_RESPONSE_HDR
> > +- TS_LUA_HOOK_SEND_RESPONSE_HDR
> > +- TS_LUA_REQUEST_TRANSFORM
> > +- TS_LUA_RESPONSE_TRANSFORM
> >
> > These constants are usually used in ts.hook method call.
> >
> > @@ -417,10 +437,10 @@ Http cache lookup status constants
> > ----------------------------------
> > **context**: global
> >
> > -    TS_LUA_CACHE_LOOKUP_MISS (0)
> > -    TS_LUA_CACHE_LOOKUP_HIT_STALE (1)
> > -    TS_LUA_CACHE_LOOKUP_HIT_FRESH (2)
> > -    TS_LUA_CACHE_LOOKUP_SKIPPED (3)
> > +- TS_LUA_CACHE_LOOKUP_MISS (0)
> > +- TS_LUA_CACHE_LOOKUP_HIT_STALE (1)
> > +- TS_LUA_CACHE_LOOKUP_HIT_FRESH (2)
> > +- TS_LUA_CACHE_LOOKUP_SKIPPED (3)
> >
> >
> > ts.http.set_cache_url
> >
> >
> http://git-wip-us.apache.org/repos/asf/trafficserver/blob/2ba2baaa/plugins/experimental/ts_lua/example/test_global_hook.lua
> > ----------------------------------------------------------------------
> > diff --git a/plugins/experimental/ts_lua/example/test_global_hook.lua
> b/plugins/experimental/ts_lua/example/test_global_hook.lua
> > new file mode 100644
> > index 0000000..bf71f0c
> > --- /dev/null
> > +++ b/plugins/experimental/ts_lua/example/test_global_hook.lua
> > @@ -0,0 +1,88 @@
> > +--  Licensed to the Apache Software Foundation (ASF) under one
> > +--  or more contributor license agreements.  See the NOTICE file
> > +--  distributed with this work for additional information
> > +--  regarding copyright ownership.  The ASF licenses this file
> > +--  to you under the Apache License, Version 2.0 (the
> > +--  "License"); you may not use this file except in compliance
> > +--  with the License.  You may obtain a copy of the License at
> > +--
> > +--  http://www.apache.org/licenses/LICENSE-2.0
> > +--
> > +--  Unless required by applicable law or agreed to in writing, software
> > +--  distributed under the License is distributed on an "AS IS" BASIS,
> > +--  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
> implied.
> > +--  See the License for the specific language governing permissions and
> > +--  limitations under the License.
> > +
> > +function do_global_txn_start()
> > +    ts.debug('txn_start')
> > +
> > +    return 0
> > +end
> > +
> > +function do_global_read_request()
> > +    ts.debug('read_request')
> > +
> > +    return 0
> > +end
> > +
> > +function do_global_send_request()
> > +    ts.debug('send_request')
> > +
> > +    return 0
> > +end
> > +
> > +function do_global_read_response()
> > +    ts.debug('read_response')
> > +
> > +    return 0
> > +end
> > +
> > +function do_global_send_response()
> > +    ts.debug('send_response')
> > +
> > +    return 0
> > +end
> > +
> > +function do_global_post_remap()
> > +    ts.debug('post_remap')
> > +
> > +    return 0
> > +end
> > +
> > +function do_global_pre_remap()
> > +    ts.debug('pre_remap')
> > +
> > +    return 0
> > +end
> > +
> > +function do_global_os_dns()
> > +    ts.debug('os_dns')
> > +
> > +    return 0
> > +end
> > +
> > +function do_global_cache_lookup_complete()
> > +    ts.debug('cache_lookup_complete')
> > +
> > +    return 0
> > +end
> > +
> > +function do_global_select_alt()
> > +    ts.debug('select_alt')
> > +
> > +    return 0
> > +end
> > +
> > +function do_global_read_cache()
> > +    ts.debug('read_cache')
> > +
> > +    return 0
> > +end
> > +
> > +function do_global_txn_close()
> > +    ts.debug('txn_close')
> > +
> > +    return 0
> > +end
> > +
> >
> >
> http://git-wip-us.apache.org/repos/asf/trafficserver/blob/2ba2baaa/plugins/experimental/ts_lua/example/test_txn_hook.lua
> > ----------------------------------------------------------------------
> > diff --git a/plugins/experimental/ts_lua/example/test_txn_hook.lua
> b/plugins/experimental/ts_lua/example/test_txn_hook.lua
> > new file mode 100644
> > index 0000000..e95a4df
> > --- /dev/null
> > +++ b/plugins/experimental/ts_lua/example/test_txn_hook.lua
> > @@ -0,0 +1,100 @@
> > +--  Licensed to the Apache Software Foundation (ASF) under one
> > +--  or more contributor license agreements.  See the NOTICE file
> > +--  distributed with this work for additional information
> > +--  regarding copyright ownership.  The ASF licenses this file
> > +--  to you under the Apache License, Version 2.0 (the
> > +--  "License"); you may not use this file except in compliance
> > +--  with the License.  You may obtain a copy of the License at
> > +--
> > +--  http://www.apache.org/licenses/LICENSE-2.0
> > +--
> > +--  Unless required by applicable law or agreed to in writing, software
> > +--  distributed under the License is distributed on an "AS IS" BASIS,
> > +--  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
> implied.
> > +--  See the License for the specific language governing permissions and
> > +--  limitations under the License.
> > +
> > +function do_global_txn_start()
> > +    ts.debug('global_txn_start')
> > +
> > +    ts.hook(TS_LUA_HOOK_READ_REQUEST_HDR, read_request)
> > +    ts.hook(TS_LUA_HOOK_SEND_REQUEST_HDR, send_request)
> > +    ts.hook(TS_LUA_HOOK_SEND_RESPONSE_HDR, send_response)
> > +    ts.hook(TS_LUA_HOOK_READ_RESPONSE_HDR, read_response)
> > +    ts.hook(TS_LUA_HOOK_CACHE_LOOKUP_COMPLETE, cache_lookup)
> > +    ts.hook(TS_LUA_HOOK_PRE_REMAP, pre_remap)
> > +    ts.hook(TS_LUA_HOOK_POST_REMAP, post_remap)
> > +    ts.hook(TS_LUA_HOOK_SELECT_ALT, select_alt)
> > +    ts.hook(TS_LUA_HOOK_OS_DNS, os_dns)
> > +    ts.hook(TS_LUA_HOOK_READ_CACHE_HDR, read_cache)
> > +    ts.hook(TS_LUA_HOOK_TXN_CLOSE, txn_close)
> > +
> > +    return 0
> > +end
> > +
> > +function read_request()
> > +    ts.debug('read_request')
> > +
> > +    return 0
> > +end
> > +
> > +function send_request()
> > +    ts.debug('send_request')
> > +
> > +    return 0
> > +end
> > +
> > +function read_response()
> > +    ts.debug('read_response')
> > +
> > +    return 0
> > +end
> > +
> > +function send_response()
> > +    ts.debug('send_response')
> > +
> > +    return 0
> > +end
> > +
> > +function post_remap()
> > +    ts.debug('post_remap')
> > +
> > +    return 0
> > +end
> > +
> > +function pre_remap()
> > +    ts.debug('pre_remap')
> > +
> > +    return 0
> > +end
> > +
> > +function os_dns()
> > +    ts.debug('os_dns')
> > +
> > +    return 0
> > +end
> > +
> > +function cache_lookup()
> > +    ts.debug('cache_lookup_complete')
> > +
> > +    return 0
> > +end
> > +
> > +function select_alt()
> > +    ts.debug('select_alt')
> > +
> > +    return 0
> > +end
> > +
> > +function read_cache()
> > +    ts.debug('read_cache')
> > +
> > +    return 0
> > +end
> > +
> > +function txn_close()
> > +    ts.debug('txn_close')
> > +
> > +    return 0
> > +end
> > +
> >
> >
> http://git-wip-us.apache.org/repos/asf/trafficserver/blob/2ba2baaa/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 b23783e..51703b7 100644
> > --- a/plugins/experimental/ts_lua/ts_lua.c
> > +++ b/plugins/experimental/ts_lua/ts_lua.c
> > @@ -202,6 +202,34 @@ globalHookHandler(TSCont contp, TSEvent event, void
> *edata)
> >     lua_getglobal(l, TS_LUA_FUNCTION_G_CACHE_LOOKUP_COMPLETE);
> >     break;
> >
> > +  case TS_EVENT_HTTP_TXN_START:
> > +    lua_getglobal(l, TS_LUA_FUNCTION_G_TXN_START);
> > +    break;
> > +
> > +  case TS_EVENT_HTTP_PRE_REMAP:
> > +    lua_getglobal(l, TS_LUA_FUNCTION_G_PRE_REMAP);
> > +    break;
> > +
> > +  case TS_EVENT_HTTP_POST_REMAP:
> > +    lua_getglobal(l, TS_LUA_FUNCTION_G_POST_REMAP);
> > +    break;
> > +
> > +  case TS_EVENT_HTTP_SELECT_ALT:
> > +    lua_getglobal(l, TS_LUA_FUNCTION_G_SELECT_ALT);
> > +    break;
> > +
> > +  case TS_EVENT_HTTP_OS_DNS:
> > +    lua_getglobal(l, TS_LUA_FUNCTION_G_OS_DNS);
> > +    break;
> > +
> > +  case TS_EVENT_HTTP_READ_CACHE_HDR:
> > +    lua_getglobal(l, TS_LUA_FUNCTION_G_READ_CACHE);
> > +    break;
> > +
> > +  case TS_EVENT_HTTP_TXN_CLOSE:
> > +    lua_getglobal(l, TS_LUA_FUNCTION_G_TXN_CLOSE);
> > +    break;
> > +
> >   default:
> >     TSHttpTxnReenable(txnp, TS_EVENT_HTTP_CONTINUE);
> >     return 0;
> > @@ -252,7 +280,6 @@ transactionStartHookHandler(TSCont contp, TSEvent
> event ATS_UNUSED, void *edata)
> >   txn_contp = TSContCreate(ts_lua_http_cont_handler, NULL);
> >   TSContDataSet(txn_contp, http_ctx);
> >   http_ctx->main_contp = txn_contp;
> > -  TSHttpTxnHookAdd(txnp, TS_HTTP_TXN_CLOSE_HOOK, txn_contp);
> >
> >   global_contp = TSContCreate(globalHookHandler, NULL);
> >   TSContDataSet(global_contp, http_ctx);
> > @@ -295,6 +322,58 @@ transactionStartHookHandler(TSCont contp, TSEvent
> event ATS_UNUSED, void *edata)
> >   }
> >   lua_pop(l, 1);
> >
> > +  lua_getglobal(l, TS_LUA_FUNCTION_G_TXN_START);
> > +  if (lua_type(l, -1) == LUA_TFUNCTION) {
> > +    TSHttpTxnHookAdd(txnp, TS_HTTP_TXN_START_HOOK, global_contp);
> > +    TSDebug(TS_LUA_DEBUG_TAG, "txn_start_hook added");
> > +  }
> > +  lua_pop(l, 1);
> > +
> > +  lua_getglobal(l, TS_LUA_FUNCTION_G_PRE_REMAP);
> > +  if (lua_type(l, -1) == LUA_TFUNCTION) {
> > +    TSHttpTxnHookAdd(txnp, TS_HTTP_PRE_REMAP_HOOK, global_contp);
> > +    TSDebug(TS_LUA_DEBUG_TAG, "pre_remap_hook added");
> > +  }
> > +  lua_pop(l, 1);
> > +
> > +  lua_getglobal(l, TS_LUA_FUNCTION_G_POST_REMAP);
> > +  if (lua_type(l, -1) == LUA_TFUNCTION) {
> > +    TSHttpTxnHookAdd(txnp, TS_HTTP_POST_REMAP_HOOK, global_contp);
> > +    TSDebug(TS_LUA_DEBUG_TAG, "post_remap_hook added");
> > +  }
> > +  lua_pop(l, 1);
> > +
> > +  lua_getglobal(l, TS_LUA_FUNCTION_G_SELECT_ALT);
> > +  if (lua_type(l, -1) == LUA_TFUNCTION) {
> > +    TSHttpTxnHookAdd(txnp, TS_HTTP_SELECT_ALT_HOOK, global_contp);
> > +    TSDebug(TS_LUA_DEBUG_TAG, "select_alt_hook added");
> > +  }
> > +  lua_pop(l, 1);
> > +
> > +  lua_getglobal(l, TS_LUA_FUNCTION_G_OS_DNS);
> > +  if (lua_type(l, -1) == LUA_TFUNCTION) {
> > +    TSHttpTxnHookAdd(txnp, TS_HTTP_OS_DNS_HOOK, global_contp);
> > +    TSDebug(TS_LUA_DEBUG_TAG, "os_dns_hook added");
> > +  }
> > +  lua_pop(l, 1);
> > +
> > +  lua_getglobal(l, TS_LUA_FUNCTION_G_READ_CACHE);
> > +  if (lua_type(l, -1) == LUA_TFUNCTION) {
> > +    TSHttpTxnHookAdd(txnp, TS_HTTP_READ_CACHE_HDR_HOOK, global_contp);
> > +    TSDebug(TS_LUA_DEBUG_TAG, "read_cache_hdr_hook added");
> > +  }
> > +  lua_pop(l, 1);
> > +
> > +  lua_getglobal(l, TS_LUA_FUNCTION_G_TXN_CLOSE);
> > +  if (lua_type(l, -1) == LUA_TFUNCTION) {
> > +    TSHttpTxnHookAdd(txnp, TS_HTTP_TXN_CLOSE_HOOK, global_contp);
> > +    TSDebug(TS_LUA_DEBUG_TAG, "txn_close_hook added");
> > +  }
> > +  lua_pop(l, 1);
> > +
> > +  // add a hook to release resources for context
> > +  TSHttpTxnHookAdd(txnp, TS_HTTP_TXN_CLOSE_HOOK, txn_contp);
> > +
> >   TSMutexUnlock(main_ctx->mutexp);
> >
> >   TSHttpTxnReenable(txnp, TS_EVENT_HTTP_CONTINUE);
> >
> >
> http://git-wip-us.apache.org/repos/asf/trafficserver/blob/2ba2baaa/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 c4a32b0..c96f36a 100644
> > --- a/plugins/experimental/ts_lua/ts_lua_common.h
> > +++ b/plugins/experimental/ts_lua/ts_lua_common.h
> > @@ -40,12 +40,28 @@
> > #define TS_LUA_FUNCTION_SEND_REQUEST            "do_send_request"
> > #define TS_LUA_FUNCTION_READ_RESPONSE           "do_read_response"
> > #define TS_LUA_FUNCTION_SEND_RESPONSE           "do_send_response"
> > +#define TS_LUA_FUNCTION_READ_REQUEST            "do_read_request"
> > +#define TS_LUA_FUNCTION_TXN_START               "do_txn_start"
> > +#define TS_LUA_FUNCTION_PRE_REMAP               "do_pre_remap"
> > +#define TS_LUA_FUNCTION_POST_REMAP              "do_post_remap"
> > +#define TS_LUA_FUNCTION_OS_DNS                  "do_os_dns"
> > +#define TS_LUA_FUNCTION_SELECT_ALT              "do_select_alt"
> > +#define TS_LUA_FUNCTION_READ_CACHE              "do_read_cache"
> > +#define TS_LUA_FUNCTION_TXN_CLOSE               "do_txn_close"
> >
> > #define TS_LUA_FUNCTION_G_SEND_REQUEST "do_global_send_request"
> > #define TS_LUA_FUNCTION_G_READ_REQUEST "do_global_read_request"
> > #define TS_LUA_FUNCTION_G_SEND_RESPONSE "do_global_send_response"
> > #define TS_LUA_FUNCTION_G_READ_RESPONSE "do_global_read_response"
> > #define TS_LUA_FUNCTION_G_CACHE_LOOKUP_COMPLETE
> "do_global_cache_lookup_complete"
> > +#define TS_LUA_FUNCTION_G_TXN_START "do_global_txn_start"
> > +#define TS_LUA_FUNCTION_G_PRE_REMAP "do_global_pre_remap"
> > +#define TS_LUA_FUNCTION_G_POST_REMAP "do_global_post_remap"
> > +#define TS_LUA_FUNCTION_G_OS_DNS "do_global_os_dns"
> > +#define TS_LUA_FUNCTION_G_SELECT_ALT "do_global_select_alt"
> > +#define TS_LUA_FUNCTION_G_READ_CACHE "do_global_read_cache"
> > +#define TS_LUA_FUNCTION_G_TXN_CLOSE "do_global_txn_close"
> > +
> >
> > #define TS_LUA_MAX_SCRIPT_FNAME_LENGTH      1024
> > #define TS_LUA_MAX_URL_LENGTH               2048
> >
> >
> http://git-wip-us.apache.org/repos/asf/trafficserver/blob/2ba2baaa/plugins/experimental/ts_lua/ts_lua_hook.c
> > ----------------------------------------------------------------------
> > diff --git a/plugins/experimental/ts_lua/ts_lua_hook.c
> b/plugins/experimental/ts_lua/ts_lua_hook.c
> > index 79f6ea7..e70312c 100644
> > --- a/plugins/experimental/ts_lua/ts_lua_hook.c
> > +++ b/plugins/experimental/ts_lua/ts_lua_hook.c
> > @@ -29,6 +29,14 @@ typedef enum
> >   TS_LUA_HOOK_SEND_REQUEST_HDR,
> >   TS_LUA_HOOK_READ_RESPONSE_HDR,
> >   TS_LUA_HOOK_SEND_RESPONSE_HDR,
> > +  TS_LUA_HOOK_READ_REQUEST_HDR,
> > +  TS_LUA_HOOK_TXN_START,
> > +  TS_LUA_HOOK_PRE_REMAP,
> > +  TS_LUA_HOOK_POST_REMAP,
> > +  TS_LUA_HOOK_OS_DNS,
> > +  TS_LUA_HOOK_SELECT_ALT,
> > +  TS_LUA_HOOK_READ_CACHE_HDR,
> > +  TS_LUA_HOOK_TXN_CLOSE,
> >   TS_LUA_REQUEST_TRANSFORM,
> >   TS_LUA_RESPONSE_TRANSFORM,
> >   TS_LUA_HOOK_LAST
> > @@ -41,6 +49,14 @@ char *ts_lua_hook_id_string[] = {
> >   "TS_LUA_HOOK_SEND_REQUEST_HDR",
> >   "TS_LUA_HOOK_READ_RESPONSE_HDR",
> >   "TS_LUA_HOOK_SEND_RESPONSE_HDR",
> > +  "TS_LUA_HOOK_READ_REQUEST_HDR",
> > +  "TS_LUA_HOOK_TXN_START",
> > +  "TS_LUA_HOOK_PRE_REMAP",
> > +  "TS_LUA_HOOK_POST_REMAP",
> > +  "TS_LUA_HOOK_OS_DNS",
> > +  "TS_LUA_HOOK_SELECT_ALT",
> > +  "TS_LUA_HOOK_READ_CACHE_HDR",
> > +  "TS_LUA_HOOK_TXN_CLOSE",
> >   "TS_LUA_REQUEST_TRANSFORM",
> >   "TS_LUA_RESPONSE_TRANSFORM",
> >   "TS_LUA_HOOK_LAST"
> > @@ -115,6 +131,54 @@ ts_lua_add_hook(lua_State * L)
> >     lua_setglobal(L, TS_LUA_FUNCTION_SEND_RESPONSE);
> >     break;
> >
> > +  case TS_LUA_HOOK_READ_REQUEST_HDR:
> > +    TSHttpTxnHookAdd(http_ctx->txnp, TS_HTTP_READ_REQUEST_HDR_HOOK,
> http_ctx->main_contp);
> > +    lua_pushvalue(L, 2);
> > +    lua_setglobal(L, TS_LUA_FUNCTION_READ_REQUEST);
> > +    break;
> > +
> > +  case TS_LUA_HOOK_TXN_START:
> > +    TSHttpTxnHookAdd(http_ctx->txnp, TS_HTTP_TXN_START_HOOK,
> http_ctx->main_contp);
> > +    lua_pushvalue(L, 2);
> > +    lua_setglobal(L, TS_LUA_FUNCTION_TXN_START);
> > +    break;
> > +
> > +  case TS_LUA_HOOK_PRE_REMAP:
> > +    TSHttpTxnHookAdd(http_ctx->txnp, TS_HTTP_PRE_REMAP_HOOK,
> http_ctx->main_contp);
> > +    lua_pushvalue(L, 2);
> > +    lua_setglobal(L, TS_LUA_FUNCTION_PRE_REMAP);
> > +    break;
> > +
> > +  case TS_LUA_HOOK_POST_REMAP:
> > +    TSHttpTxnHookAdd(http_ctx->txnp, TS_HTTP_POST_REMAP_HOOK,
> http_ctx->main_contp);
> > +    lua_pushvalue(L, 2);
> > +    lua_setglobal(L, TS_LUA_FUNCTION_POST_REMAP);
> > +    break;
> > +
> > +  case TS_LUA_HOOK_OS_DNS:
> > +    TSHttpTxnHookAdd(http_ctx->txnp, TS_HTTP_OS_DNS_HOOK,
> http_ctx->main_contp);
> > +    lua_pushvalue(L, 2);
> > +    lua_setglobal(L, TS_LUA_FUNCTION_OS_DNS);
> > +    break;
> > +
> > +  case TS_LUA_HOOK_SELECT_ALT:
> > +    TSHttpTxnHookAdd(http_ctx->txnp, TS_HTTP_SELECT_ALT_HOOK,
> http_ctx->main_contp);
> > +    lua_pushvalue(L, 2);
> > +    lua_setglobal(L, TS_LUA_FUNCTION_SELECT_ALT);
> > +    break;
> > +
> > +  case TS_LUA_HOOK_READ_CACHE_HDR:
> > +    TSHttpTxnHookAdd(http_ctx->txnp, TS_HTTP_READ_CACHE_HDR_HOOK,
> http_ctx->main_contp);
> > +    lua_pushvalue(L, 2);
> > +    lua_setglobal(L, TS_LUA_FUNCTION_READ_CACHE);
> > +    break;
> > +
> > +  case TS_LUA_HOOK_TXN_CLOSE:
> > +    // we don't need to add a hook because we already have added one by
> default
> > +    lua_pushvalue(L, 2);
> > +    lua_setglobal(L, TS_LUA_FUNCTION_TXN_CLOSE);
> > +    break;
> > +
> >   case TS_LUA_REQUEST_TRANSFORM:
> >   case TS_LUA_RESPONSE_TRANSFORM:
> >     transform_ctx = (ts_lua_transform_ctx *)
> TSmalloc(sizeof(ts_lua_transform_ctx));
> >
> >
> http://git-wip-us.apache.org/repos/asf/trafficserver/blob/2ba2baaa/plugins/experimental/ts_lua/ts_lua_util.c
> > ----------------------------------------------------------------------
> > diff --git a/plugins/experimental/ts_lua/ts_lua_util.c
> b/plugins/experimental/ts_lua/ts_lua_util.c
> > index 0d0a0ec..010a31d 100644
> > --- a/plugins/experimental/ts_lua/ts_lua_util.c
> > +++ b/plugins/experimental/ts_lua/ts_lua_util.c
> > @@ -444,7 +444,91 @@ ts_lua_http_cont_handler(TSCont contp, TSEvent
> event, void *edata)
> >
> >     break;
> >
> > +  case TS_EVENT_HTTP_READ_REQUEST_HDR:
> > +
> > +    lua_getglobal(l, TS_LUA_FUNCTION_READ_REQUEST);
> > +    if (lua_type(l, -1) == LUA_TFUNCTION) {
> > +      if (lua_pcall(l, 0, 1, 0)) {
> > +        fprintf(stderr, "lua_pcall failed: %s\n", lua_tostring(l, -1));
> > +      }
> > +    }
> > +
> > +    break;
> > +
> > +  case TS_EVENT_HTTP_TXN_START:
> > +
> > +    lua_getglobal(l, TS_LUA_FUNCTION_TXN_START);
> > +    if (lua_type(l, -1) == LUA_TFUNCTION) {
> > +      if (lua_pcall(l, 0, 1, 0)) {
> > +        fprintf(stderr, "lua_pcall failed: %s\n", lua_tostring(l, -1));
> > +      }
> > +    }
> > +
> > +    break;
> > +
> > +  case TS_EVENT_HTTP_PRE_REMAP:
> > +
> > +    lua_getglobal(l, TS_LUA_FUNCTION_PRE_REMAP);
> > +    if (lua_type(l, -1) == LUA_TFUNCTION) {
> > +      if (lua_pcall(l, 0, 1, 0)) {
> > +        fprintf(stderr, "lua_pcall failed: %s\n", lua_tostring(l, -1));
> > +      }
> > +    }
> > +
> > +    break;
> > +
> > +  case TS_EVENT_HTTP_POST_REMAP:
> > +
> > +    lua_getglobal(l, TS_LUA_FUNCTION_POST_REMAP);
> > +    if (lua_type(l, -1) == LUA_TFUNCTION) {
> > +      if (lua_pcall(l, 0, 1, 0)) {
> > +        fprintf(stderr, "lua_pcall failed: %s\n", lua_tostring(l, -1));
> > +      }
> > +    }
> > +
> > +    break;
> > +
> > +  case TS_EVENT_HTTP_OS_DNS:
> > +
> > +    lua_getglobal(l, TS_LUA_FUNCTION_OS_DNS);
> > +    if (lua_type(l, -1) == LUA_TFUNCTION) {
> > +      if (lua_pcall(l, 0, 1, 0)) {
> > +        fprintf(stderr, "lua_pcall failed: %s\n", lua_tostring(l, -1));
> > +      }
> > +    }
> > +
> > +    break;
> > +
> > +  case TS_EVENT_HTTP_SELECT_ALT:
> > +
> > +    lua_getglobal(l, TS_LUA_FUNCTION_SELECT_ALT);
> > +    if (lua_type(l, -1) == LUA_TFUNCTION) {
> > +      if (lua_pcall(l, 0, 1, 0)) {
> > +        fprintf(stderr, "lua_pcall failed: %s\n", lua_tostring(l, -1));
> > +      }
> > +    }
> > +
> > +    break;
> > +
> > +  case TS_EVENT_HTTP_READ_CACHE_HDR:
> > +
> > +    lua_getglobal(l, TS_LUA_FUNCTION_READ_CACHE);
> > +    if (lua_type(l, -1) == LUA_TFUNCTION) {
> > +      if (lua_pcall(l, 0, 1, 0)) {
> > +        fprintf(stderr, "lua_pcall failed: %s\n", lua_tostring(l, -1));
> > +      }
> > +    }
> > +
> > +    break;
> > +
> >   case TS_EVENT_HTTP_TXN_CLOSE:
> > +    lua_getglobal(l, TS_LUA_FUNCTION_TXN_CLOSE);
> > +    if (lua_type(l, -1) == LUA_TFUNCTION) {
> > +      if (lua_pcall(l, 0, 1, 0)) {
> > +        fprintf(stderr, "lua_pcall failed: %s\n", lua_tostring(l, -1));
> > +      }
> > +    }
> > +
> >     ts_lua_destroy_http_ctx(http_ctx);
> >     TSContDestroy(contp);
> >     break;
> >
>
>