From b1cd9df8a992487c9425c24bc43f274cae9877f2 Mon Sep 17 00:00:00 2001 From: Kit Chan Date: Fri, 1 Jul 2016 06:05:44 -0700 Subject: [PATCH] TS-4627: support TSRemapOSResponse in ts_lua --- doc/admin-guide/plugins/ts_lua.en.rst | 261 +++++++++++++------- plugins/experimental/ts_lua/ts_lua.c | 37 ++- plugins/experimental/ts_lua/ts_lua_common.h | 2 +- plugins/experimental/ts_lua/ts_lua_http.c | 90 ++++++- plugins/experimental/ts_lua/ts_lua_util.c | 2 +- 5 files changed, 281 insertions(+), 111 deletions(-) diff --git a/doc/admin-guide/plugins/ts_lua.en.rst b/doc/admin-guide/plugins/ts_lua.en.rst index caad94f97dc..d3d9abf6b99 100644 --- a/doc/admin-guide/plugins/ts_lua.en.rst +++ b/doc/admin-guide/plugins/ts_lua.en.rst @@ -79,8 +79,8 @@ is given at build time. Configuration ============= -This module acts as remap plugin of Traffic Server, so we should realize 'do_remap' function in each lua script. We can -write this in remap.config: +This module acts as remap plugin of Traffic Server, so we should realize 'do_remap' or 'do_os_response' function in each +lua script. We can write this in remap.config: :: @@ -298,7 +298,7 @@ ts.hook ------- **syntax:** *ts.hook(HOOK_POINT, FUNCTION)* -**context:** global or do_remap or do_global_* or later +**context:** global or do_remap/do_os_response or do_global_* or later **description**: Hooks are points in http transaction processing where we can step in and do some work. FUNCTION will be called when the http transaction steps in to HOOK_POINT. @@ -377,46 +377,55 @@ These constants are usually used in ts.hook method call. Additional Information: -+------------------------------------+------------------------------------+----------------------+---------------------+ -| Hook Point | Lua Hook Point constant | Hook function be | Hook function be | -| | | registered within | registered within | -| | | do_remap() via | global context via| -| | | ts.hook()? | ts.hook()? | -+====================================+====================================+======================+=====================+ -| TS_HTTP_TXN_START_HOOK | TS_LUA_HOOK_TXN_START | NO | YES | -+------------------------------------+------------------------------------+----------------------+---------------------+ -| TS_HTTP_READ_REQUEST_HDR_HOOK | TS_LUA_HOOK_READ_REQUEST_HDR | NO | YES | -+------------------------------------+------------------------------------+----------------------+---------------------+ -| TS_HTTP_PRE_REMAP_HOOK | TS_LUA_HOOK_PRE_REMAP | NO | YES | -+------------------------------------+------------------------------------+----------------------+---------------------+ -| TS_HTTP_POST_REMAP_HOOK | TS_LUA_HOOK_POST_REMAP | YES | YES | -+------------------------------------+------------------------------------+----------------------+---------------------+ -| TS_HTTP_SELECT_ALT_HOOK | TS_LUA_HOOK_SELECT_ALT | NO | NO | -+------------------------------------+------------------------------------+----------------------+---------------------+ -| TS_HTTP_READ_CACHE_HDR_HOOK | TS_LUA_HOOK_READ_CACHE_HDR | YES | YES | -+------------------------------------+------------------------------------+----------------------+---------------------+ -| TS_HTTP_OS_DNS_HOOK | TS_LUA_HOOK_OS_DNS | YES | YES | -+------------------------------------+------------------------------------+----------------------+---------------------+ -| TS_HTTP | TS_LUA_HOOK | YES | YES | -| _CACHE_LOOKUP_COMPLETE_HOOK | _CACHE_LOOKUP_COMPLETE | | | -+------------------------------------+------------------------------------+----------------------+---------------------+ -| TS_HTTP | TS_LUA_HOOK | YES | YES | -| _SEND_REQUEST_HDR_HOOK | _SEND_REQUEST_HDR | | | -+------------------------------------+------------------------------------+----------------------+---------------------+ -| TS_HTTP | TS_LUA_HOOK | YES | YES | -| _READ_RESPONSE_HDR_HOOK | _READ_RESPONSE_HDR | | | -+------------------------------------+------------------------------------+----------------------+---------------------+ -| TS_HTTP | TS_LUA_HOOK | YES | YES | -| _SEND_RESPONSE_HDR_HOOK | _SEND_RESPONSE_HDR | | | -+------------------------------------+------------------------------------+----------------------+---------------------+ -| TS_HTTP | TS_LUA_REQUEST_TRANSFORM | YES | YES | -| _REQUEST_TRANSFORM_HOOK | | | | -+------------------------------------+------------------------------------+----------------------+---------------------+ -| TS_HTTP | TS_LUA_RESPONSE_TRANSFORM | YES | YES | -| _RESPONSE_TRANSFORM_HOOK | | | | -+------------------------------------+------------------------------------+----------------------+---------------------+ -| TS_HTTP_TXN_CLOSE_HOOK | TS_LUA_HOOK_TXN_CLOSE | YES | YES | -+------------------------------------+------------------------------------+----------------------+---------------------+ ++-----------------------+---------------------------+----------------------+--------------------+----------------------+ +| Hook Point | Lua Hook Point constant | Hook function be | Hook function be | Hook function be | +| | | registered within | registered within | registered within | +| | | do_remap() via | do_os_response() | global context via | +| | | ts.hook()? | via ts.hook()? | ts.hook()? | ++=======================+===========================+======================+====================+======================+ +| TS_HTTP_TXN | TS_LUA_HOOK | NO | NO | YES | +| _START_HOOK | _TXN_START | | | | ++-----------------------+---------------------------+----------------------+--------------------+----------------------+ +| TS_HTTP_READ | TS_LUA_HOOK | NO | NO | YES | +| _REQUEST_HDR_HOOK | _READ_REQUEST_HDR | | | | ++-----------------------+---------------------------+----------------------+--------------------+----------------------+ +| TS_HTTP_PRE | TS_LUA_HOOK | NO | NO | YES | +| _REMAP_HOOK | _PRE_REMAP | | | | ++-----------------------+---------------------------+----------------------+--------------------+----------------------+ +| TS_HTTP_POST | TS_LUA_HOOK | YES | NO | YES | +| _REMAP_HOOK | _POST_REMAP | | | | ++-----------------------+---------------------------+----------------------+--------------------+----------------------+ +| TS_HTTP_SELECT | TS_LUA_HOOK | NO | NO | NO | +| _ALT_HOOK | _SELECT_ALT | | | | ++-----------------------+---------------------------+----------------------+--------------------+----------------------+ +| TS_HTTP_READ | TS_LUA_HOOK | YES | NO | YES | +| _CACHE_HDR_HOOK | _READ_CACHE_HDR | | | | ++-----------------------+---------------------------+----------------------+--------------------+----------------------+ +| TS_HTTP_OS | TS_LUA_HOOK_ | YES | NO | YES | +| _DNS_HOOK | _OS_DNS | | | | ++-----------------------+---------------------------+----------------------+--------------------+----------------------+ +| TS_HTTP_CACHE | TS_LUA_HOOK | YES | NO | YES | +| _LOOKUP_COMPLETE_HOOK | _CACHE_LOOKUP_COMPLETE | | | | ++-----------------------+---------------------------+----------------------+--------------------+----------------------+ +| TS_HTTP_SEND | TS_LUA_HOOK | YES | NO | YES | +| _REQUEST_HDR_HOOK | _SEND_REQUEST_HDR | | | | ++-----------------------+---------------------------+----------------------+--------------------+----------------------+ +| TS_HTTP_READ | TS_LUA_HOOK | YES | YES | YES | +| _RESPONSE_HDR_HOOK | _READ_RESPONSE_HDR | | | | ++-----------------------+---------------------------+----------------------+--------------------+----------------------+ +| TS_HTTP_SEND | TS_LUA_HOOK | YES | YES | YES | +| _RESPONSE_HDR_HOOK | _SEND_RESPONSE_HDR | | | | ++-----------------------+---------------------------+----------------------+--------------------+----------------------+ +| TS_HTTP_REQUEST | TS_LUA_REQUEST_TRANSFORM | YES | NO | YES | +| _TRANSFORM_HOOK | | | | | ++-----------------------+---------------------------+----------------------+--------------------+----------------------+ +| TS_HTTP_RESPONSE | TS_LUA_RESPONSE_TRANSFORM | YES | YES | YES | +| _TRANSFORM_HOOK | | | | | ++-----------------------+---------------------------+----------------------+--------------------+----------------------+ +| TS_HTTP_TXN | TS_LUA_HOOK_TXN_CLOSE | YES | YES | YES | +| _CLOSE_HOOK | | | | | ++-----------------------+---------------------------+----------------------+--------------------+----------------------+ + `TOP <#ts-lua-plugin>`_ @@ -426,7 +435,7 @@ ts.ctx **syntax:** *VALUE = ts.ctx[KEY]* -**context:** do_remap or do_global_* or later +**context:** do_remap/do_os_response or do_global_* or later **description:** This table can be used to store per-request Lua context data and has a life time identical to the current request. @@ -463,7 +472,7 @@ ts.client_request.get_method ---------------------------- **syntax:** *ts.client_request.get_method()* -**context:** do_remap or do_global_* or later +**context:** do_remap/do_os_response or do_global_* or later **description:** This function can be used to retrieve the current client request's method name. String like "GET" or "POST" is returned. @@ -482,7 +491,7 @@ ts.client_request.get_version ----------------------------- **syntax:** *ver = ts.client_request.get_version()* -**context:** do_remap or do_global_* or later +**context:** do_remap/do_os_response or do_global_* or later **description:** Return the http version string of the client request. @@ -508,7 +517,7 @@ ts.client_request.get_uri ------------------------- **syntax:** *ts.client_request.get_uri()* -**context:** do_remap or later +**context:** do_remap/do_os_response or later **description:** This function can be used to retrieve the client request's path. @@ -545,7 +554,7 @@ ts.client_request.get_uri_args ------------------------------ **syntax:** *ts.client_request.get_uri_args()* -**context:** do_remap or do_global_* or later +**context:** do_remap/do_os_response or do_global_* or later **description:** This function can be used to retrieve the client request's query string. @@ -584,7 +593,7 @@ ts.client_request.get_uri_params -------------------------------- **syntax:** *ts.client_request.get_uri_params()* -**context:** do_remap or do_global_* or later +**context:** do_remap/do_os_response or do_global_* or later **description:** This function can be used to retrieve the client request's parameter string. @@ -623,7 +632,7 @@ ts.client_request.get_url ------------------------- **syntax:** *ts.client_request.get_url()* -**context:** do_remap or do_global_* or later +**context:** do_remap/do_os_response or do_global_* or later **description:** This function can be used to retrieve the whole client request's url. @@ -650,7 +659,7 @@ ts.client_request.header.HEADER **syntax:** *VALUE = ts.client_request.header.HEADER* -**context:** do_remap or do_global_* or later +**context:** do_remap/do_os_response or do_global_* or later **description:** Set, add to, clear or get the current client request's HEADER. @@ -675,7 +684,7 @@ ts.client_request.get_headers ----------------------------- **syntax:** *ts.client_request.get_headers()* -**context:** do_remap or do_global_* or later +**context:** do_remap/do_os_response or do_global_* or later **description:** Returns a Lua table holding all the headers for the current client request. @@ -703,7 +712,7 @@ ts.client_request.client_addr.get_addr -------------------------------------- **syntax:** *ts.client_request.client_addr.get_addr()* -**context:** do_remap or do_global_* or later +**context:** do_remap/do_os_response or do_global_* or later **description**: This function can be used to get socket address of the client. @@ -727,7 +736,7 @@ ts.client_request.client_addr.get_incoming_port ----------------------------------------------- **syntax:** *ts.client_request.client_addr.get_incoming_port()* -**context:** do_remap or do_global_* or later +**context:** do_remap/do_os_response or do_global_* or later **description**: This function can be used to get incoming port of the request. @@ -748,7 +757,7 @@ ts.client_request.get_url_host ------------------------------ **syntax:** *host = ts.client_request.get_url_host()* -**context:** do_remap or do_global_* or later +**context:** do_remap/do_os_response or do_global_* or later **description:** Return the ``host`` field of the request url. @@ -801,7 +810,7 @@ ts.client_request.get_url_port ------------------------------ **syntax:** *port = ts.client_request.get_url_port()* -**context:** do_remap or do_global_* or later +**context:** do_remap/do_os_response or do_global_* or later **description:** Returns the ``port`` field of the request url as a Lua number. @@ -837,7 +846,7 @@ ts.client_request.get_url_scheme -------------------------------- **syntax:** *scheme = ts.client_request.get_url_scheme()* -**context:** do_remap or do_global_* or later +**context:** do_remap/do_os_response or do_global_* or later **description:** Return the ``scheme`` field of the request url. @@ -1368,7 +1377,7 @@ ts.server_request.server_addr.get_addr -------------------------------------- **syntax:** *ts.server_request.server_addr.get_addr()* -**context:** do_remap or do_global_* or later +**context:** function @ TS_LUA_HOOK_SEND_REQUEST_HDR hook point or later **description**: This function can be used to get socket address of the origin server. @@ -1391,7 +1400,7 @@ ts.server_request.server_addr.get_ip ------------------------------------ **syntax:** *ts.server_request.server_addr.get_ip()* -**context:** do_remap or do_global_* or later +**context:** function @ TS_LUA_HOOK_SEND_REQUEST_HDR hook point or later **description**: This function can be used to get ip address of the origin server. @@ -1412,7 +1421,7 @@ ts.server_request.server_addr.get_port -------------------------------------- **syntax:** *ts.server_request.server_addr.get_port()* -**context:** do_remap or do_global_* or later +**context:** function @ TS_LUA_HOOK_SEND_REQUEST_HDR hook point or later **description**: This function can be used to get port of the origin server. @@ -1433,7 +1442,7 @@ ts.server_request.server_addr.get_outgoing_port ----------------------------------------------- **syntax:** *ts.server_request.server_addr.get_outgoing_port()* -**context:** do_remap or do_global_* or later +**context:** function @ TS_LUA_HOOK_SEND_REQUEST_HDR hook point or later **description**: This function can be used to get outgoing port to the origin server. @@ -1813,7 +1822,7 @@ ts.http.resp_cache_transformed ------------------------------ **syntax:** *ts.http.resp_cache_transformed(BOOL)* -**context:** do_remap or do_global_* or later +**context:** do_remap/do_os_response or do_global_* or later **description**: This function can be used to tell trafficserver whether to cache the transformed data. @@ -1840,7 +1849,7 @@ ts.http.resp_cache_untransformed -------------------------------- **syntax:** *ts.http.resp_cache_untransformed(BOOL)* -**context:** do_remap or do_global_* or later +**context:** do_remap/do_os_response or do_global_* or later **description**: This function can be used to tell trafficserver whether to cache the untransformed data. @@ -1944,7 +1953,7 @@ ts.http.is_internal_request --------------------------- **syntax:** *ts.http.is_internal_request()* -**context:** do_remap or do_global_* or later +**context:** do_remap/do_os_response or do_global_* or later **description:** This function can be used to tell is a request is internal or not @@ -1964,7 +1973,7 @@ ts.http.transaction_count ------------------------- **syntax:** *ts.http.transaction_count()* -**context:** do_remap or do_global_* or later +**context:** do_remap/do_os_response or do_global_* or later **description:** This function returns the number of transaction in this connection @@ -1980,6 +1989,68 @@ Here is an example `TOP <#ts-lua-plugin>`_ +ts.http.redirect_url_set +------------------------ +**syntax:** *ts.http.redirect_url_set()* + +**context:** do_remap/do_os_response or do_global_* or later + +**description:** This function sets the redirect url and instructs the transaction to follow the redirection as response + +Here is an example + +:: + + function do_global_read_response() + ts.http.redirect_url_set('http://foo.com') + return 0 + end + +`TOP <#ts-lua-plugin>`_ + +ts.http.get_server_state +------------------------ +**syntax:** *ts.http.get_server_state()* + +**context:** do_remap/do_os_response or do_global_* or later + +**description:** This function returns the current server state + +Here is an example + +:: + + function do_os_response() + local result = ts.http.get_server_state() + if result == TS_LUA_SRVSTATE_CONNECTION_ALIVE then + ts.debug('Alive') + end + end + +`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.add_package_path ------------------- **syntax:** *ts.add_package_path(lua-style-path-str)* @@ -2195,7 +2266,7 @@ ts.fetch ----------- **syntax:** *res = ts.fetch(url, table?)* -**context:** after do_remap +**context:** hook point functions added after do_remap **description:** Issues a synchronous but still non-block http request with the ``url`` and the optional ``table``. @@ -2250,7 +2321,7 @@ ts.fetch_multi -------------- **syntax:** *vec = ts.fetch_multi({{url, table?}, {url, table?}, ...})* -**context:** after do_remap +**context:** hook point functions added after do_remap Just like `ts.fetch`, but supports multiple http requests running in parallel. @@ -2479,7 +2550,7 @@ ts.sleep -------- **syntax:** *ts.sleep(sec)* -**context:** *after do_remap* +**context:** *hook point functions added after do_remap* **description:** Sleeps for the specified seconds without blocking. @@ -2508,7 +2579,7 @@ ts.schedule ----------- **syntax:** *ts.schedule(THREAD_TYPE, sec, FUNCTION, param1?, param2?, ...)* -**context:** *after do_remap* +**context:** *hook point functions added after do_remap* **description:** Schedule function to be run after specified seconds without blocking. @@ -2538,7 +2609,7 @@ ts.http.config_int_get ---------------------- **syntax:** *val = ts.http.config_int_get(CONFIG)* -**context:** do_remap or do_global_* or later. +**context:** do_remap/do_os_response or do_global_* or later **description:** Configuration option which has a int value can be retrieved with this function. @@ -2553,7 +2624,7 @@ ts.http.config_int_set ---------------------- **syntax:** *ts.http.config_int_set(CONFIG, NUMBER)* -**context:** do_remap or do_global_* or later. +**context:** do_remap/do_os_response or do_global_* or later **description:** This function can be used to overwrite the configuration options. @@ -2573,7 +2644,7 @@ ts.http.config_float_get ------------------------ **syntax:** *val = ts.http.config_float_get(CONFIG)* -**context:** do_remap or do_global_* or later. +**context:** do_remap/do_os_response or do_global_* or later **description:** Configuration option which has a float value can be retrieved with this function. @@ -2584,7 +2655,7 @@ ts.http.config_float_set ------------------------ **syntax:** *ts.http.config_float_set(CONFIG, NUMBER)* -**context:** do_remap or do_global_* or later. +**context:** do_remap/do_os_response or do_global_* or later **description:** This function can be used to overwrite the configuration options. @@ -2595,7 +2666,7 @@ ts.http.config_string_get ------------------------- **syntax:** *val = ts.http.config_string_get(CONFIG)* -**context:** do_remap or do_global_* or later. +**context:** do_remap/do_os_response or do_global_* or later **description:** Configuration option which has a string value can be retrieved with this function. @@ -2606,7 +2677,7 @@ ts.http.config_string_set ------------------------- **syntax:** *ts.http.config_string_set(CONFIG, NUMBER)* -**context:** do_remap or do_global_* or later. +**context:** do_remap/do_os_response or do_global_* or later **description:** This function can be used to overwrite the configuration options. @@ -2615,7 +2686,7 @@ ts.http.config_string_set Http config constants --------------------- -**context:** do_remap or do_global_* or later +**context:** do_remap/do_os_response or do_global_* or later :: @@ -2719,7 +2790,7 @@ ts.http.timeout_set ------------------- **syntax:** *ts.http.timeout_set(CONFIG, NUMBER)* -**context:** do_remap or do_global_* or later. +**context:** do_remap/do_os_response or do_global_* or later. **description:** This function can be used to overwrite the timeout settings. @@ -2737,7 +2808,7 @@ Here is an example: Timeout constants ----------------- -**context:** do_remap or do_global_* or later +**context:** do_remap/do_os_response or do_global_* or later :: @@ -2753,7 +2824,7 @@ ts.http.client_packet_mark_set ------------------------------ **syntax:** *ts.http.client_packet_mark_set(NUMBER)* -**context:** do_remap or do_global_* or later. +**context:** do_remap/do_os_response or do_global_* or later. **description:** This function can be used to set packet mark for client connection. @@ -2772,7 +2843,7 @@ ts.http.server_packet_mark_set ------------------------------ **syntax:** *ts.http.server_packet_mark_set(NUMBER)* -**context:** do_remap or do_global_* or later. +**context:** do_remap/do_os_response or do_global_* or later. **description:** This function can be used to set packet mark for server connection. @@ -2783,7 +2854,7 @@ ts.http.client_packet_tos_set ----------------------------- **syntax:** *ts.http.client_packet_tos_set(NUMBER)* -**context:** do_remap or do_global_* or later. +**context:** do_remap/do_os_response or do_global_* or later. **description:** This function can be used to set packet tos for client connection. @@ -2794,7 +2865,7 @@ ts.http.server_packet_tos_set ----------------------------- **syntax:** *ts.http.server_packet_tos_set(NUMBER)* -**context:** do_remap or do_global_* or later. +**context:** do_remap/do_os_response or do_global_* or later. **description:** This function can be used to set packet tos for server connection. @@ -2805,7 +2876,7 @@ ts.http.client_packet_dscp_set ------------------------------ **syntax:** *ts.http.client_packet_dscp_set(NUMBER)* -**context:** do_remap or do_global_* or later. +**context:** do_remap/do_os_response or do_global_* or later. **description:** This function can be used to set packet dscp for client connection. @@ -2816,7 +2887,7 @@ ts.http.server_packet_dscp_set ------------------------------ **syntax:** *ts.http.server_packet_dscp_set(NUMBER)* -**context:** do_remap or do_global_* or later. +**context:** do_remap/do_os_response or do_global_* or later. **description:** This function can be used to set packet dscp for server connection. @@ -2827,7 +2898,7 @@ ts.http.enable_redirect ----------------------- **syntax:** *ts.http.enable_redirect(NUMBER)* -**context:** do_remap or do_global_* or later. +**context:** do_remap/do_os_response or do_global_* or later. **decription:** This function can be used to make transaction follow redirect @@ -2847,7 +2918,7 @@ ts.http.set_debug ----------------- **syntax:** *ts.http.set_debug(NUMBER)* -**context:** do_remap or do_global_* or later. +**context:** do_remap/do_os_response or do_global_* or later. **decription:** This function can be used to enable debug log for the transaction @@ -2867,7 +2938,7 @@ ts.http.cntl_get ---------------- **syntax:** *val = ts.http.cntl_get(CNTL_TYPE)* -**context:** do_remap or do_global_* or later. +**context:** do_remap/do_os_response or do_global_* or later. **description:** This function can be used to retireve the value of control channel. @@ -2882,7 +2953,7 @@ ts.http.cntl_set ---------------- **syntax:** *ts.http.cntl_set(CNTL_TYPE, BOOL)* -**context:** do_remap or do_global_* or later. +**context:** do_remap/do_os_response or do_global_* or later. **description:** This function can be used to set the value of control channel. @@ -2900,7 +2971,7 @@ Here is an example: Http control channel constants ------------------------------ -**context:** do_remap or do_global_* or later +**context:** do_remap/do_os_response or do_global_* or later :: @@ -2916,7 +2987,7 @@ ts.http.milestone_get --------------------- **syntax:** *val = ts.http.milestone_get(MILESTONE_TYPE)* -**context:** do_remap or do_global_* or later. +**context:** do_remap/do_os_response or do_global_* or later. **description:** This function can be used to retireve the various milestone times. They are how long the transaction took to traverse portions of the HTTP state machine. Each milestone value is a fractional number @@ -2930,7 +3001,7 @@ of seconds since the beginning of the transaction. Milestone constants ------------------------------ -**context:** do_remap or do_global_* or later +**context:** do_remap/do_os_response or do_global_* or later :: @@ -2964,7 +3035,7 @@ ts.mgmt.get_counter ------------------- **syntax:** *val = ts.mgmt.get_counter(RECORD_NAME)* -**context:** do_remap or do_global_* or later. +**context:** do_remap/do_os_response or do_global_* or later. **description:** This function can be used to retrieve the record value which has a counter type. @@ -2978,7 +3049,7 @@ ts.mgmt.get_int --------------- **syntax:** *val = ts.mgmt.get_int(RECORD_NAME)* -**context:** do_remap or do_global_* or later. +**context:** do_remap/do_os_response or do_global_* or later. **description:** This function can be used to retrieve the record value which has a int type. @@ -2988,7 +3059,7 @@ ts.mgmt.get_float ----------------- **syntax:** *val = ts.mgmt.get_float(RECORD_NAME)* -**context:** do_remap or do_global_* or later. +**context:** do_remap/do_os_response or do_global_* or later. **description:** This function can be used to retrieve the record value which has a float type. @@ -2998,7 +3069,7 @@ ts.mgmt.get_string ------------------ **syntax:** *val = ts.mgmt.get_string(RECORD_NAME)* -**context:** do_remap or do_global_* or later. +**context:** do_remap/do_os_response or do_global_* or later. **description:** This function can be used to retrieve the record value which has a string type. diff --git a/plugins/experimental/ts_lua/ts_lua.c b/plugins/experimental/ts_lua/ts_lua.c index c4e3a6a6b0e..476c04a439d 100644 --- a/plugins/experimental/ts_lua/ts_lua.c +++ b/plugins/experimental/ts_lua/ts_lua.c @@ -116,8 +116,8 @@ TSRemapDeleteInstance(void *ih) return; } -TSRemapStatus -TSRemapDoRemap(void *ih, TSHttpTxn rh, TSRemapRequestInfo *rri) +static TSRemapStatus +ts_lua_remap_plugin_init(void *ih, TSHttpTxn rh, TSRemapRequestInfo *rri) { int ret; uint64_t req_id; @@ -131,6 +131,7 @@ TSRemapDoRemap(void *ih, TSHttpTxn rh, TSRemapRequestInfo *rri) ts_lua_instance_conf *instance_conf; + int remap = (rri == NULL ? 0 : 1); instance_conf = (ts_lua_instance_conf *)ih; req_id = __sync_fetch_and_add(&ts_lua_http_next_id, 1); @@ -140,13 +141,14 @@ TSRemapDoRemap(void *ih, TSHttpTxn rh, TSRemapRequestInfo *rri) http_ctx = ts_lua_create_http_ctx(main_ctx, instance_conf); - http_ctx->txnp = rh; - 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; + http_ctx->txnp = rh; + http_ctx->has_hook = 0; + http_ctx->rri = rri; + if (rri != NULL) { + http_ctx->client_request_bufp = rri->requestBufp; + http_ctx->client_request_hdrp = rri->requestHdrp; + http_ctx->client_request_url = rri->requestUrl; + } ci = &http_ctx->cinfo; L = ci->routine.lua; @@ -157,7 +159,7 @@ TSRemapDoRemap(void *ih, TSHttpTxn rh, TSRemapRequestInfo *rri) ci->contp = contp; ci->mutex = TSContMutexGet((TSCont)rh); - lua_getglobal(L, TS_LUA_FUNCTION_REMAP); + lua_getglobal(L, (remap ? TS_LUA_FUNCTION_REMAP : TS_LUA_FUNCTION_OS_RESPONSE)); if (lua_type(L, -1) != LUA_TFUNCTION) { TSMutexUnlock(main_ctx->mutexp); return TSREMAP_NO_REMAP; @@ -187,6 +189,20 @@ TSRemapDoRemap(void *ih, TSHttpTxn rh, TSRemapRequestInfo *rri) return ret; } +void +TSRemapOSResponse(void *ih, TSHttpTxn rh, int os_response_type) +{ + TSDebug(TS_LUA_DEBUG_TAG, "[%s] os response function and type - %d", __FUNCTION__, os_response_type); + ts_lua_remap_plugin_init(ih, rh, NULL); +} + +TSRemapStatus +TSRemapDoRemap(void *ih, TSHttpTxn rh, TSRemapRequestInfo *rri) +{ + TSDebug(TS_LUA_DEBUG_TAG, "[%s] remap function", __FUNCTION__); + return ts_lua_remap_plugin_init(ih, rh, rri); +} + static int globalHookHandler(TSCont contp, TSEvent event ATS_UNUSED, void *edata) { @@ -218,7 +234,6 @@ 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; if (!http_ctx->client_request_bufp) { diff --git a/plugins/experimental/ts_lua/ts_lua_common.h b/plugins/experimental/ts_lua/ts_lua_common.h index 3927eef3f9e..ef4973de311 100644 --- a/plugins/experimental/ts_lua/ts_lua_common.h +++ b/plugins/experimental/ts_lua/ts_lua_common.h @@ -34,6 +34,7 @@ #include "ts_lua_coroutine.h" #define TS_LUA_FUNCTION_REMAP "do_remap" +#define TS_LUA_FUNCTION_OS_RESPONSE "do_os_response" #define TS_LUA_FUNCTION_CACHE_LOOKUP_COMPLETE "do_cache_lookup_complete" #define TS_LUA_FUNCTION_SEND_REQUEST "do_send_request" #define TS_LUA_FUNCTION_READ_RESPONSE "do_read_response" @@ -123,7 +124,6 @@ typedef struct { ts_lua_instance_conf *instance_conf; - int remap; int has_hook; TSRemapRequestInfo *rri; diff --git a/plugins/experimental/ts_lua/ts_lua_http.c b/plugins/experimental/ts_lua/ts_lua_http.c index 5f89c47a1e8..68cc63cc4b0 100644 --- a/plugins/experimental/ts_lua/ts_lua_http.c +++ b/plugins/experimental/ts_lua/ts_lua_http.c @@ -26,11 +26,42 @@ typedef enum { TS_LUA_CACHE_LOOKUP_MISS, TS_LUA_CACHE_LOOKUP_HIT_STALE, TS_LUA_CACHE_LOOKUP_HIT_FRESH, - TS_LUA_CACHE_LOOKUP_SKIPPED + TS_LUA_CACHE_LOOKUP_SKIPPED, } TSLuaCacheLookupResult; -char *ts_lua_cache_lookup_result_string[] = {"TS_LUA_CACHE_LOOKUP_MISS", "TS_LUA_CACHE_LOOKUP_HIT_STALE", - "TS_LUA_CACHE_LOOKUP_HIT_FRESH", "TS_LUA_CACHE_LOOKUP_SKIPPED"}; +typedef enum { + TS_LUA_SRVSTATE_STATE_UNDEFINED, + TS_LUA_SRVSTATE_ACTIVE_TIMEOUT, + TS_LUA_SRVSTATE_BAD_INCOMING_RESPONSE, + TS_LUA_SRVSTATE_CONNECTION_ALIVE, + TS_LUA_SRVSTATE_CONNECTION_CLOSED, + TS_LUA_SRVSTATE_CONNECTION_ERROR, + TS_LUA_SRVSTATE_INACTIVE_TIMEOUT, + TS_LUA_SRVSTATE_OPEN_RAW_ERROR, + TS_LUA_SRVSTATE_PARSE_ERROR, + TS_LUA_SRVSTATE_TRANSACTION_COMPLETE, + TS_LUA_SRVSTATE_CONGEST_CONTROL_CONGESTED_ON_F, + TS_LUA_SRVSTATE_CONGEST_CONTROL_CONGESTED_ON_M, +} TSLuaServerState; + +const char *ts_lua_cache_lookup_result_string[] = { + "TS_LUA_CACHE_LOOKUP_MISS", "TS_LUA_CACHE_LOOKUP_HIT_STALE", "TS_LUA_CACHE_LOOKUP_HIT_FRESH", "TS_LUA_CACHE_LOOKUP_SKIPPED", +}; + +const char *ts_lua_server_state_string[] = { + "TS_LUA_SRVSTATE_STATE_UNDEFINED", + "TS_LUA_SRVSTATE_ACTIVE_TIMEOUT", + "TS_LUA_SRVSTATE_BAD_INCOMING_RESPONSE", + "TS_LUA_SRVSTATE_CONNECTION_ALIVE", + "TS_LUA_SRVSTATE_CONNECTION_CLOSED", + "TS_LUA_SRVSTATE_CONNECTION_ERROR", + "TS_LUA_SRVSTATE_INACTIVE_TIMEOUT", + "TS_LUA_SRVSTATE_OPEN_RAW_ERROR", + "TS_LUA_SRVSTATE_PARSE_ERROR", + "TS_LUA_SRVSTATE_TRANSACTION_COMPLETE", + "TS_LUA_SRVSTATE_CONGEST_CONTROL_CONGESTED_ON_F", + "TS_LUA_SRVSTATE_CONGEST_CONTROL_CONGESTED_ON_M", +}; static void ts_lua_inject_http_retset_api(lua_State *L); static void ts_lua_inject_http_cache_api(lua_State *L); @@ -56,6 +87,10 @@ 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 int ts_lua_http_redirect_url_set(lua_State *L); +static int ts_lua_http_get_server_state(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); static int ts_lua_http_resp_transform_get_upstream_bytes(lua_State *L); @@ -151,6 +186,14 @@ ts_lua_inject_http_misc_api(lua_State *L) lua_pushcfunction(L, ts_lua_http_transaction_count); lua_setfield(L, -2, "transaction_count"); + + lua_pushcfunction(L, ts_lua_http_redirect_url_set); + lua_setfield(L, -2, "redirect_url_set"); + + lua_pushcfunction(L, ts_lua_http_get_server_state); + lua_setfield(L, -2, "get_server_state"); + + ts_lua_inject_server_state_variables(L); } static void @@ -164,6 +207,17 @@ ts_lua_inject_cache_lookup_result_variables(lua_State *L) } } +static void +ts_lua_inject_server_state_variables(lua_State *L) +{ + size_t i; + + for (i = 0; i < sizeof(ts_lua_server_state_string) / sizeof(char *); i++) { + lua_pushinteger(L, (lua_Integer)i); + lua_setglobal(L, ts_lua_server_state_string[i]); + } +} + static int ts_lua_http_set_retstatus(lua_State *L) { @@ -440,6 +494,36 @@ ts_lua_http_transaction_count(lua_State *L) return 1; } +static int +ts_lua_http_redirect_url_set(lua_State *L) +{ + const char *url; + const char *redirect_url; + size_t url_len; + ts_lua_http_ctx *http_ctx; + + GET_HTTP_CONTEXT(http_ctx, L); + + url = luaL_checklstring(L, 1, &url_len); + redirect_url = TSstrndup(url, url_len); + TSHttpTxnRedirectUrlSet(http_ctx->txnp, redirect_url, url_len); + + return 0; +} + +static int +ts_lua_http_get_server_state(lua_State *L) +{ + ts_lua_http_ctx *http_ctx; + + GET_HTTP_CONTEXT(http_ctx, L); + + TSServerState ss = TSHttpTxnServerStateGet(http_ctx->txnp); + lua_pushnumber(L, ss); + + return 1; +} + static int ts_lua_http_resp_transform_get_upstream_bytes(lua_State *L) { diff --git a/plugins/experimental/ts_lua/ts_lua_util.c b/plugins/experimental/ts_lua/ts_lua_util.c index 13d2a582c1e..4cfc9b8ac99 100644 --- a/plugins/experimental/ts_lua/ts_lua_util.c +++ b/plugins/experimental/ts_lua/ts_lua_util.c @@ -454,7 +454,7 @@ ts_lua_destroy_http_ctx(ts_lua_http_ctx *http_ctx) ci = &http_ctx->cinfo; - if (!http_ctx->remap) { + if (http_ctx->rri == NULL) { if (http_ctx->client_request_bufp) { TSHandleMLocRelease(http_ctx->client_request_bufp, TS_NULL_MLOC, http_ctx->client_request_hdrp); }