Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 84 additions & 4 deletions doc/admin-guide/plugins/lua.en.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2804,7 +2804,7 @@ ts.http.resp_transform.get_upstream_bytes
-----------------------------------------
**syntax:** *ts.http.resp_transform.get_upstream_bytes()*

**context:** transform handler
**context:** transform handler for response

**description**: This function can be used to retrieve the total bytes to be received from the upstream. If we got
chunked response body from origin server, TS_LUA_INT64_MAX will be returned.
Expand Down Expand Up @@ -2848,7 +2848,7 @@ ts.http.resp_transform.get_upstream_watermark_bytes
---------------------------------------------------
**syntax:** *ts.http.resp_transform.get_upstream_watermark_bytes()*

**context:** transform handler
**context:** transform handler for response

**description**: This function can be used to retrieve the current watermark bytes for the upstream transform buffer.

Expand All @@ -2859,7 +2859,7 @@ ts.http.resp_transform.set_upstream_watermark_bytes
---------------------------------------------------
**syntax:** *ts.http.resp_transform.set_upstream_watermark_bytes(NUMBER)*

**context:** transform handler
**context:** transform handler for response

**description**: This function can be used to set the watermark bytes of the upstream transform buffer.

Expand All @@ -2872,14 +2872,94 @@ ts.http.resp_transform.set_downstream_bytes
-------------------------------------------
**syntax:** *ts.http.resp_transform.set_downstream_bytes(NUMBER)*

**context:** transform handler
**context:** transform handler for response

**description**: This function can be used to set the total bytes to be sent to the downstream.

Sometimes we want to set Content-Length header in client_response, and this function should be called before any real
data is returned from the transform handler.


:ref:`TOP <admin-plugins-ts-lua>`

ts.http.req_transform.get_downstream_bytes
------------------------------------------
**syntax:** *ts.http.req_transform.get_downstream_bytes()*

**context:** transform handler for request

**description**: This function can be used to retrieve the total bytes to be received from downstream.

Here is an example:

::

function transform_print(data, eos)
ts.ctx['reqbody'] = ts.ctx['reqbody'] .. data

if ts.ctx['len_set'] == nil then
local sz = ts.http.req_transform.get_downstream_bytes()
ts.http.req_transform.set_upstream_bytes(sz)
ts.ctx['len_set'] = true
end

if (eos == 1) then
ts.debug('End of Stream and the reqbody is ... ')
ts.debug(ts.ctx['reqbody'])
end

return data, eos
end

function do_remap()
if (ts.client_request.get_method() == 'POST') then
ts.ctx['reqbody'] = ''
ts.hook(TS_LUA_REQUEST_TRANSFORM, transform_print)
end

return 0
end

The above example also shows the use of eos passed as a parameter to transform function. It indicates the end of the
data stream to the transform function.

:ref:`TOP <admin-plugins-ts-lua>`

ts.http.req_transform.get_downstream_watermark_bytes
----------------------------------------------------
**syntax:** *ts.http.req_transform.get_downstream_watermark_bytes()*

**context:** transform handler for request

**description**: This function can be used to retrieve the current watermark bytes for the downstream transform buffer.


:ref:`TOP <admin-plugins-ts-lua>`

ts.http.req_transform.set_downstream_watermark_bytes
----------------------------------------------------
**syntax:** *ts.http.req_transform.set_downstream_watermark_bytes(NUMBER)*

**context:** transform handler for request

**description**: This function can be used to set the watermark bytes of the downstream transform buffer.

Setting the watermark bytes above 32kb may improve the performance of the transform handler.


:ref:`TOP <admin-plugins-ts-lua>`

ts.http.req_transform.set_upstream_bytes
----------------------------------------
**syntax:** *ts.http.req_transform.set_upstream_bytes(NUMBER)*

**context:** transform handler for request

**description**: This function can be used to set the total bytes to be sent to the upstream.

This function should be called before any real data is returned from the transform handler.


:ref:`TOP <admin-plugins-ts-lua>`

ts.http.skip_remapping_set
Expand Down
17 changes: 15 additions & 2 deletions example/plugins/lua-api/reqbody.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,22 @@
-- See the License for the specific language governing permissions and
-- limitations under the License.

function encrypt(data, eos)
-- This example illustrates how to do request body transform.
-- It stores the request body and prints it at the end of the transform.

function request_transform(data, eos)
ts.ctx['reqbody'] = ts.ctx['reqbody'] .. data

if ts.ctx['len_set'] == nil then
ts.debug("len not set")
local sz = ts.http.req_transform.get_downstream_bytes()
ts.debug("len "..sz)
ts.http.req_transform.set_upstream_bytes(sz)
ts.ctx['len_set'] = true
end

ts.debug("req transform got " .. string.len(data) .. "bytes, eos=" .. eos)

if (eos == 1) then
ts.debug('End of Stream and the reqbody is ... ')
ts.debug(ts.ctx['reqbody'])
Expand All @@ -29,7 +42,7 @@ function do_remap()
ts.debug('do_remap')
if (ts.client_request.get_method() == 'POST') then
ts.ctx['reqbody'] = ''
ts.hook(TS_LUA_REQUEST_TRANSFORM, encrypt)
ts.hook(TS_LUA_REQUEST_TRANSFORM, request_transform)
end

return 0
Expand Down
54 changes: 54 additions & 0 deletions plugins/lua/ts_lua_http.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ static int ts_lua_http_resp_transform_get_upstream_watermark_bytes(lua_State *L)
static int ts_lua_http_resp_transform_set_upstream_watermark_bytes(lua_State *L);
static int ts_lua_http_resp_transform_set_downstream_bytes(lua_State *L);

static void ts_lua_inject_http_req_transform_api(lua_State *L);
static int ts_lua_http_req_transform_get_downstream_bytes(lua_State *L);
static int ts_lua_http_req_transform_get_downstream_watermark_bytes(lua_State *L);
static int ts_lua_http_req_transform_set_downstream_watermark_bytes(lua_State *L);
static int ts_lua_http_req_transform_set_upstream_bytes(lua_State *L);

void
ts_lua_inject_http_api(lua_State *L)
{
Expand Down Expand Up @@ -196,6 +202,11 @@ ts_lua_inject_http_transform_api(lua_State *L)
lua_newtable(L);
ts_lua_inject_http_resp_transform_api(L);
lua_setfield(L, -2, "resp_transform");

/* ts.http.req_transform api */
lua_newtable(L);
ts_lua_inject_http_req_transform_api(L);
lua_setfield(L, -2, "req_transform");
}

static void
Expand All @@ -214,6 +225,22 @@ ts_lua_inject_http_resp_transform_api(lua_State *L)
lua_setfield(L, -2, "set_downstream_bytes");
}

static void
ts_lua_inject_http_req_transform_api(lua_State *L)
{
lua_pushcfunction(L, ts_lua_http_req_transform_get_downstream_bytes);
lua_setfield(L, -2, "get_downstream_bytes");

lua_pushcfunction(L, ts_lua_http_req_transform_get_downstream_watermark_bytes);
lua_setfield(L, -2, "get_downstream_watermark_bytes");

lua_pushcfunction(L, ts_lua_http_req_transform_set_downstream_watermark_bytes);
lua_setfield(L, -2, "set_downstream_watermark_bytes");

lua_pushcfunction(L, ts_lua_http_req_transform_set_upstream_bytes);
lua_setfield(L, -2, "set_upstream_bytes");
}

static void
ts_lua_inject_http_misc_api(lua_State *L)
{
Expand Down Expand Up @@ -1010,3 +1037,30 @@ ts_lua_http_resp_transform_set_downstream_bytes(lua_State *L)

return 0;
}

// Request transform are similar to response transform. It works against the transform context available.
// We can get the downstream bytes and set the upstream bytes.
// We can also get and set the downstream watermark as well.
static int
ts_lua_http_req_transform_get_downstream_bytes(lua_State *L)
{
return ts_lua_http_resp_transform_get_upstream_bytes(L);
}

static int
ts_lua_http_req_transform_get_downstream_watermark_bytes(lua_State *L)
{
return ts_lua_http_resp_transform_get_upstream_watermark_bytes(L);
}

static int
ts_lua_http_req_transform_set_downstream_watermark_bytes(lua_State *L)
{
return ts_lua_http_resp_transform_set_upstream_watermark_bytes(L);
}

static int
ts_lua_http_req_transform_set_upstream_bytes(lua_State *L)
{
return ts_lua_http_resp_transform_set_downstream_bytes(L);
}