From 8a397f574fdeabdc23cabea4aaf1869546399043 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20M=C3=A4kel=C3=A4?= Date: Wed, 21 Mar 2018 00:17:47 +0200 Subject: [PATCH] Expose modutil_get_canonical in luafilter Exposing the canonicalization code in the luafilter allows it to be used on the Lua side of things. This should enable some pretty cool stuff to be done with it. --- Documentation/Filters/Luafilter.md | 6 ++ server/modules/filter/luafilter/luafilter.c | 62 ++++++++++++++------- 2 files changed, 47 insertions(+), 21 deletions(-) diff --git a/Documentation/Filters/Luafilter.md b/Documentation/Filters/Luafilter.md index 81bdabf9e..3fc8a7466 100644 --- a/Documentation/Filters/Luafilter.md +++ b/Documentation/Filters/Luafilter.md @@ -90,6 +90,12 @@ The luafilter exposes three functions that can be called from the Lua script. This function can only be called from the `routeQuery` entry point. +- `string lua_get_canonical()` + + - Returns the canonical version of a query by replacing all user-defined constant values with question marks. + + This function can only be called from the `routeQuery` entry point. + - `number id_gen()` - This function generates unique integers that can be used to distinct diff --git a/server/modules/filter/luafilter/luafilter.c b/server/modules/filter/luafilter/luafilter.c index 8706de491..ebe7fecc3 100644 --- a/server/modules/filter/luafilter/luafilter.c +++ b/server/modules/filter/luafilter/luafilter.c @@ -169,6 +169,25 @@ static int lua_qc_get_operation(lua_State* state) return 1; } +static int lua_get_canonical(lua_State* state) +{ + int ibuf = lua_upvalueindex(1); + GWBUF *buf = *((GWBUF**)lua_touserdata(state, ibuf)); + + if (buf) + { + char* canon = modutil_get_canonical(buf); + lua_pushstring(state, canon); + MXS_FREE(canon); + } + else + { + lua_pushliteral(state, ""); + } + + return 1; +} + /** * The Lua filter instance. */ @@ -193,6 +212,26 @@ typedef struct MXS_UPSTREAM up; } LUA_SESSION; +void expose_functions(lua_State* state, GWBUF** active_buffer) +{ + /** Expose an ID generation function */ + lua_pushcfunction(state, id_gen); + lua_setglobal(state, "id_gen"); + + /** Expose a part of the query classifier API */ + lua_pushlightuserdata(state, active_buffer); + lua_pushcclosure(state, lua_qc_get_type_mask, 1); + lua_setglobal(state, "lua_qc_get_type_mask"); + + lua_pushlightuserdata(state, active_buffer); + lua_pushcclosure(state, lua_qc_get_operation, 1); + lua_setglobal(state, "lua_qc_get_operation"); + + lua_pushlightuserdata(state, active_buffer); + lua_pushcclosure(state, lua_get_canonical, 1); + lua_setglobal(state, "lua_get_canonical"); +} + /** * Create a new instance of the Lua filter. * @@ -244,15 +283,7 @@ createInstance(const char *name, char **options, MXS_CONFIG_PARAMETER *params) lua_pop(my_instance->global_lua_state, -1); // Pop the error off the stack } - /** Expose a part of the query classifier API */ - lua_pushlightuserdata(my_instance->global_lua_state, ¤t_global_query); - lua_pushcclosure(my_instance->global_lua_state, lua_qc_get_type_mask, 1); - lua_setglobal(my_instance->global_lua_state, "lua_qc_get_type_mask"); - - lua_pushlightuserdata(my_instance->global_lua_state, ¤t_global_query); - lua_pushcclosure(my_instance->global_lua_state, lua_qc_get_operation, 1); - lua_setglobal(my_instance->global_lua_state, "lua_qc_get_operation"); - + expose_functions(my_instance->global_lua_state, ¤t_global_query); } } else @@ -311,18 +342,7 @@ static MXS_FILTER_SESSION *newSession(MXS_FILTER *instance, MXS_SESSION *session } else { - /** Expose an ID generation function */ - lua_pushcfunction(my_session->lua_state, id_gen); - lua_setglobal(my_session->lua_state, "id_gen"); - - /** Expose a part of the query classifier API */ - lua_pushlightuserdata(my_session->lua_state, &my_session->current_query); - lua_pushcclosure(my_session->lua_state, lua_qc_get_type_mask, 1); - lua_setglobal(my_session->lua_state, "lua_qc_get_type_mask"); - - lua_pushlightuserdata(my_session->lua_state, &my_session->current_query); - lua_pushcclosure(my_session->lua_state, lua_qc_get_operation, 1); - lua_setglobal(my_session->lua_state, "lua_qc_get_operation"); + expose_functions(my_session->lua_state, &my_session->current_query); /** Call the newSession entry point */ lua_getglobal(my_session->lua_state, "newSession");