Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1 | #include <sys/socket.h> |
| 2 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 3 | #include <ctype.h> |
| 4 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 5 | #include <lauxlib.h> |
| 6 | #include <lua.h> |
| 7 | #include <lualib.h> |
| 8 | |
Thierry FOURNIER | 463119c | 2015-03-10 00:35:36 +0100 | [diff] [blame] | 9 | #if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 503 |
| 10 | #error "Requires Lua 5.3 or later." |
Cyril Bonté | dc0306e | 2015-03-02 00:08:40 +0100 | [diff] [blame] | 11 | #endif |
| 12 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 13 | #include <ebpttree.h> |
| 14 | |
| 15 | #include <common/cfgparse.h> |
| 16 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 17 | #include <types/connection.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 18 | #include <types/hlua.h> |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 19 | #include <types/proto_tcp.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 20 | #include <types/proxy.h> |
| 21 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 22 | #include <proto/arg.h> |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 23 | #include <proto/applet.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 24 | #include <proto/channel.h> |
Thierry FOURNIER | 9a819e7 | 2015-02-16 20:22:55 +0100 | [diff] [blame] | 25 | #include <proto/hdr_idx.h> |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 26 | #include <proto/hlua.h> |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 27 | #include <proto/map.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 28 | #include <proto/obj_type.h> |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 29 | #include <proto/pattern.h> |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 30 | #include <proto/payload.h> |
| 31 | #include <proto/proto_http.h> |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 32 | #include <proto/proto_tcp.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 33 | #include <proto/raw_sock.h> |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 34 | #include <proto/sample.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 35 | #include <proto/server.h> |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 36 | #include <proto/session.h> |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 37 | #include <proto/stream.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 38 | #include <proto/ssl_sock.h> |
| 39 | #include <proto/stream_interface.h> |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 40 | #include <proto/task.h> |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 41 | #include <proto/vars.h> |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 42 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 43 | /* Lua uses longjmp to perform yield or throwing errors. This |
| 44 | * macro is used only for identifying the function that can |
| 45 | * not return because a longjmp is executed. |
| 46 | * __LJMP marks a prototype of hlua file that can use longjmp. |
| 47 | * WILL_LJMP() marks an lua function that will use longjmp. |
| 48 | * MAY_LJMP() marks an lua function that may use longjmp. |
| 49 | */ |
| 50 | #define __LJMP |
| 51 | #define WILL_LJMP(func) func |
| 52 | #define MAY_LJMP(func) func |
| 53 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 54 | /* The main Lua execution context. */ |
| 55 | struct hlua gL; |
| 56 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 57 | /* This is the memory pool containing all the signal structs. These |
| 58 | * struct are used to store each requiered signal between two tasks. |
| 59 | */ |
| 60 | struct pool_head *pool2_hlua_com; |
| 61 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 62 | /* Used for Socket connection. */ |
| 63 | static struct proxy socket_proxy; |
| 64 | static struct server socket_tcp; |
| 65 | #ifdef USE_OPENSSL |
| 66 | static struct server socket_ssl; |
| 67 | #endif |
| 68 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 69 | /* List head of the function called at the initialisation time. */ |
| 70 | struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions); |
| 71 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 72 | /* The following variables contains the reference of the different |
| 73 | * Lua classes. These references are useful for identify metadata |
| 74 | * associated with an object. |
| 75 | */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 76 | static int class_txn_ref; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 77 | static int class_socket_ref; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 78 | static int class_channel_ref; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 79 | static int class_fetches_ref; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 80 | static int class_converters_ref; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 81 | static int class_http_ref; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 82 | static int class_map_ref; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 83 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 84 | /* Global Lua execution timeout. By default Lua, execution linked |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 85 | * with stream (actions, sample-fetches and converters) have a |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 86 | * short timeout. Lua linked with tasks doesn't have a timeout |
| 87 | * because a task may remain alive during all the haproxy execution. |
| 88 | */ |
| 89 | static unsigned int hlua_timeout_session = 4000; /* session timeout. */ |
| 90 | static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */ |
| 91 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 92 | /* Interrupts the Lua processing each "hlua_nb_instruction" instructions. |
| 93 | * it is used for preventing infinite loops. |
| 94 | * |
| 95 | * I test the scheer with an infinite loop containing one incrementation |
| 96 | * and one test. I run this loop between 10 seconds, I raise a ceil of |
| 97 | * 710M loops from one interrupt each 9000 instructions, so I fix the value |
| 98 | * to one interrupt each 10 000 instructions. |
| 99 | * |
| 100 | * configured | Number of |
| 101 | * instructions | loops executed |
| 102 | * between two | in milions |
| 103 | * forced yields | |
| 104 | * ---------------+--------------- |
| 105 | * 10 | 160 |
| 106 | * 500 | 670 |
| 107 | * 1000 | 680 |
| 108 | * 5000 | 700 |
| 109 | * 7000 | 700 |
| 110 | * 8000 | 700 |
| 111 | * 9000 | 710 <- ceil |
| 112 | * 10000 | 710 |
| 113 | * 100000 | 710 |
| 114 | * 1000000 | 710 |
| 115 | * |
| 116 | */ |
| 117 | static unsigned int hlua_nb_instruction = 10000; |
| 118 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 119 | /* Descriptor for the memory allocation state. If limit is not null, it will |
| 120 | * be enforced on any memory allocation. |
| 121 | */ |
| 122 | struct hlua_mem_allocator { |
| 123 | size_t allocated; |
| 124 | size_t limit; |
| 125 | }; |
| 126 | |
| 127 | static struct hlua_mem_allocator hlua_global_allocator; |
| 128 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 129 | /* These functions converts types between HAProxy internal args or |
| 130 | * sample and LUA types. Another function permits to check if the |
| 131 | * LUA stack contains arguments according with an required ARG_T |
| 132 | * format. |
| 133 | */ |
| 134 | static int hlua_arg2lua(lua_State *L, const struct arg *arg); |
| 135 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 136 | __LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp, |
| 137 | unsigned int mask, struct proxy *p); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 138 | static int hlua_smp2lua(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 139 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 140 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp); |
| 141 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 142 | /* Used to check an Lua function type in the stack. It creates and |
| 143 | * returns a reference of the function. This function throws an |
| 144 | * error if the rgument is not a "function". |
| 145 | */ |
| 146 | __LJMP unsigned int hlua_checkfunction(lua_State *L, int argno) |
| 147 | { |
| 148 | if (!lua_isfunction(L, argno)) { |
| 149 | const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, -1)); |
| 150 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 151 | } |
| 152 | lua_pushvalue(L, argno); |
| 153 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 154 | } |
| 155 | |
| 156 | /* The three following functions are useful for adding entries |
| 157 | * in a table. These functions takes a string and respectively an |
| 158 | * integer, a string or a function and add it to the table in the |
| 159 | * top of the stack. |
| 160 | * |
| 161 | * These functions throws an error if no more stack size is |
| 162 | * available. |
| 163 | */ |
| 164 | __LJMP static inline void hlua_class_const_int(lua_State *L, const char *name, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 165 | int value) |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 166 | { |
| 167 | if (!lua_checkstack(L, 2)) |
| 168 | WILL_LJMP(luaL_error(L, "full stack")); |
| 169 | lua_pushstring(L, name); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 170 | lua_pushinteger(L, value); |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 171 | lua_settable(L, -3); |
| 172 | } |
| 173 | __LJMP static inline void hlua_class_const_str(lua_State *L, const char *name, |
| 174 | const char *value) |
| 175 | { |
| 176 | if (!lua_checkstack(L, 2)) |
| 177 | WILL_LJMP(luaL_error(L, "full stack")); |
| 178 | lua_pushstring(L, name); |
| 179 | lua_pushstring(L, value); |
| 180 | lua_settable(L, -3); |
| 181 | } |
| 182 | __LJMP static inline void hlua_class_function(lua_State *L, const char *name, |
| 183 | int (*function)(lua_State *L)) |
| 184 | { |
| 185 | if (!lua_checkstack(L, 2)) |
| 186 | WILL_LJMP(luaL_error(L, "full stack")); |
| 187 | lua_pushstring(L, name); |
| 188 | lua_pushcclosure(L, function, 0); |
| 189 | lua_settable(L, -3); |
| 190 | } |
| 191 | |
| 192 | /* This function check the number of arguments available in the |
| 193 | * stack. If the number of arguments available is not the same |
| 194 | * then <nb> an error is throwed. |
| 195 | */ |
| 196 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 197 | { |
| 198 | if (lua_gettop(L) == nb) |
| 199 | return; |
| 200 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 201 | } |
| 202 | |
| 203 | /* Return true if the data in stack[<ud>] is an object of |
| 204 | * type <class_ref>. |
| 205 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 206 | static int hlua_metaistype(lua_State *L, int ud, int class_ref) |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 207 | { |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 208 | if (!lua_getmetatable(L, ud)) |
| 209 | return 0; |
| 210 | |
| 211 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_ref); |
| 212 | if (!lua_rawequal(L, -1, -2)) { |
| 213 | lua_pop(L, 2); |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | lua_pop(L, 2); |
| 218 | return 1; |
| 219 | } |
| 220 | |
| 221 | /* Return an object of the expected type, or throws an error. */ |
| 222 | __LJMP static void *hlua_checkudata(lua_State *L, int ud, int class_ref) |
| 223 | { |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 224 | void *p; |
| 225 | |
| 226 | /* Check if the stack entry is an array. */ |
| 227 | if (!lua_istable(L, ud)) |
| 228 | WILL_LJMP(luaL_argerror(L, ud, NULL)); |
| 229 | /* Check if the metadata have the expected type. */ |
| 230 | if (!hlua_metaistype(L, ud, class_ref)) |
| 231 | WILL_LJMP(luaL_argerror(L, ud, NULL)); |
| 232 | /* Push on the stack at the entry [0] of the table. */ |
| 233 | lua_rawgeti(L, ud, 0); |
| 234 | /* Check if this entry is userdata. */ |
| 235 | p = lua_touserdata(L, -1); |
| 236 | if (!p) |
| 237 | WILL_LJMP(luaL_argerror(L, ud, NULL)); |
| 238 | /* Remove the entry returned by lua_rawgeti(). */ |
| 239 | lua_pop(L, 1); |
| 240 | /* Return the associated struct. */ |
| 241 | return p; |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | /* This fucntion push an error string prefixed by the file name |
| 245 | * and the line number where the error is encountered. |
| 246 | */ |
| 247 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 248 | { |
| 249 | va_list argp; |
| 250 | va_start(argp, fmt); |
| 251 | luaL_where(L, 1); |
| 252 | lua_pushvfstring(L, fmt, argp); |
| 253 | va_end(argp); |
| 254 | lua_concat(L, 2); |
| 255 | return 1; |
| 256 | } |
| 257 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 258 | /* This function register a new signal. "lua" is the current lua |
| 259 | * execution context. It contains a pointer to the associated task. |
| 260 | * "link" is a list head attached to an other task that must be wake |
| 261 | * the lua task if an event occurs. This is useful with external |
| 262 | * events like TCP I/O or sleep functions. This funcion allocate |
| 263 | * memory for the signal. |
| 264 | */ |
| 265 | static int hlua_com_new(struct hlua *lua, struct list *link) |
| 266 | { |
| 267 | struct hlua_com *com = pool_alloc2(pool2_hlua_com); |
| 268 | if (!com) |
| 269 | return 0; |
| 270 | LIST_ADDQ(&lua->com, &com->purge_me); |
| 271 | LIST_ADDQ(link, &com->wake_me); |
| 272 | com->task = lua->task; |
| 273 | return 1; |
| 274 | } |
| 275 | |
| 276 | /* This function purge all the pending signals when the LUA execution |
| 277 | * is finished. This prevent than a coprocess try to wake a deleted |
| 278 | * task. This function remove the memory associated to the signal. |
| 279 | */ |
| 280 | static void hlua_com_purge(struct hlua *lua) |
| 281 | { |
| 282 | struct hlua_com *com, *back; |
| 283 | |
| 284 | /* Delete all pending communication signals. */ |
| 285 | list_for_each_entry_safe(com, back, &lua->com, purge_me) { |
| 286 | LIST_DEL(&com->purge_me); |
| 287 | LIST_DEL(&com->wake_me); |
| 288 | pool_free2(pool2_hlua_com, com); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | /* This function sends signals. It wakes all the tasks attached |
| 293 | * to a list head, and remove the signal, and free the used |
| 294 | * memory. |
| 295 | */ |
| 296 | static void hlua_com_wake(struct list *wake) |
| 297 | { |
| 298 | struct hlua_com *com, *back; |
| 299 | |
| 300 | /* Wake task and delete all pending communication signals. */ |
| 301 | list_for_each_entry_safe(com, back, wake, wake_me) { |
| 302 | LIST_DEL(&com->purge_me); |
| 303 | LIST_DEL(&com->wake_me); |
| 304 | task_wakeup(com->task, TASK_WOKEN_MSG); |
| 305 | pool_free2(pool2_hlua_com, com); |
| 306 | } |
| 307 | } |
| 308 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 309 | /* This functions is used with sample fetch and converters. It |
| 310 | * converts the HAProxy configuration argument in a lua stack |
| 311 | * values. |
| 312 | * |
| 313 | * It takes an array of "arg", and each entry of the array is |
| 314 | * converted and pushed in the LUA stack. |
| 315 | */ |
| 316 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 317 | { |
| 318 | switch (arg->type) { |
| 319 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 320 | case ARGT_TIME: |
| 321 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 322 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 323 | break; |
| 324 | |
| 325 | case ARGT_STR: |
| 326 | lua_pushlstring(L, arg->data.str.str, arg->data.str.len); |
| 327 | break; |
| 328 | |
| 329 | case ARGT_IPV4: |
| 330 | case ARGT_IPV6: |
| 331 | case ARGT_MSK4: |
| 332 | case ARGT_MSK6: |
| 333 | case ARGT_FE: |
| 334 | case ARGT_BE: |
| 335 | case ARGT_TAB: |
| 336 | case ARGT_SRV: |
| 337 | case ARGT_USR: |
| 338 | case ARGT_MAP: |
| 339 | default: |
| 340 | lua_pushnil(L); |
| 341 | break; |
| 342 | } |
| 343 | return 1; |
| 344 | } |
| 345 | |
| 346 | /* This function take one entrie in an LUA stack at the index "ud", |
| 347 | * and try to convert it in an HAProxy argument entry. This is useful |
| 348 | * with sample fetch wrappers. The input arguments are gived to the |
| 349 | * lua wrapper and converted as arg list by thi function. |
| 350 | */ |
| 351 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 352 | { |
| 353 | switch (lua_type(L, ud)) { |
| 354 | |
| 355 | case LUA_TNUMBER: |
| 356 | case LUA_TBOOLEAN: |
| 357 | arg->type = ARGT_SINT; |
| 358 | arg->data.sint = lua_tointeger(L, ud); |
| 359 | break; |
| 360 | |
| 361 | case LUA_TSTRING: |
| 362 | arg->type = ARGT_STR; |
| 363 | arg->data.str.str = (char *)lua_tolstring(L, ud, (size_t *)&arg->data.str.len); |
| 364 | break; |
| 365 | |
| 366 | case LUA_TUSERDATA: |
| 367 | case LUA_TNIL: |
| 368 | case LUA_TTABLE: |
| 369 | case LUA_TFUNCTION: |
| 370 | case LUA_TTHREAD: |
| 371 | case LUA_TLIGHTUSERDATA: |
| 372 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 373 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 374 | break; |
| 375 | } |
| 376 | return 1; |
| 377 | } |
| 378 | |
| 379 | /* the following functions are used to convert a struct sample |
| 380 | * in Lua type. This useful to convert the return of the |
| 381 | * fetchs or converters. |
| 382 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 383 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 384 | { |
| 385 | switch (smp->type) { |
| 386 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 387 | case SMP_T_BOOL: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 388 | lua_pushinteger(L, smp->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 389 | break; |
| 390 | |
| 391 | case SMP_T_BIN: |
| 392 | case SMP_T_STR: |
| 393 | lua_pushlstring(L, smp->data.str.str, smp->data.str.len); |
| 394 | break; |
| 395 | |
| 396 | case SMP_T_METH: |
| 397 | switch (smp->data.meth.meth) { |
| 398 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 399 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 400 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 401 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 402 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 403 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 404 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 405 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 406 | case HTTP_METH_OTHER: |
| 407 | lua_pushlstring(L, smp->data.meth.str.str, smp->data.meth.str.len); |
| 408 | break; |
| 409 | default: |
| 410 | lua_pushnil(L); |
| 411 | break; |
| 412 | } |
| 413 | break; |
| 414 | |
| 415 | case SMP_T_IPV4: |
| 416 | case SMP_T_IPV6: |
| 417 | case SMP_T_ADDR: /* This type is never used to qualify a sample. */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 418 | if (sample_casts[smp->type][SMP_T_STR] && |
| 419 | sample_casts[smp->type][SMP_T_STR](smp)) |
| 420 | lua_pushlstring(L, smp->data.str.str, smp->data.str.len); |
| 421 | else |
| 422 | lua_pushnil(L); |
| 423 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 424 | default: |
| 425 | lua_pushnil(L); |
| 426 | break; |
| 427 | } |
| 428 | return 1; |
| 429 | } |
| 430 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 431 | /* the following functions are used to convert a struct sample |
| 432 | * in Lua strings. This is useful to convert the return of the |
| 433 | * fetchs or converters. |
| 434 | */ |
| 435 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 436 | { |
| 437 | switch (smp->type) { |
| 438 | |
| 439 | case SMP_T_BIN: |
| 440 | case SMP_T_STR: |
| 441 | lua_pushlstring(L, smp->data.str.str, smp->data.str.len); |
| 442 | break; |
| 443 | |
| 444 | case SMP_T_METH: |
| 445 | switch (smp->data.meth.meth) { |
| 446 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 447 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 448 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 449 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 450 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 451 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 452 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 453 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 454 | case HTTP_METH_OTHER: |
| 455 | lua_pushlstring(L, smp->data.meth.str.str, smp->data.meth.str.len); |
| 456 | break; |
| 457 | default: |
| 458 | lua_pushstring(L, ""); |
| 459 | break; |
| 460 | } |
| 461 | break; |
| 462 | |
| 463 | case SMP_T_SINT: |
| 464 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 465 | case SMP_T_IPV4: |
| 466 | case SMP_T_IPV6: |
| 467 | case SMP_T_ADDR: /* This type is never used to qualify a sample. */ |
| 468 | if (sample_casts[smp->type][SMP_T_STR] && |
| 469 | sample_casts[smp->type][SMP_T_STR](smp)) |
| 470 | lua_pushlstring(L, smp->data.str.str, smp->data.str.len); |
| 471 | else |
| 472 | lua_pushstring(L, ""); |
| 473 | break; |
| 474 | default: |
| 475 | lua_pushstring(L, ""); |
| 476 | break; |
| 477 | } |
| 478 | return 1; |
| 479 | } |
| 480 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 481 | /* the following functions are used to convert an Lua type in a |
| 482 | * struct sample. This is useful to provide data from a converter |
| 483 | * to the LUA code. |
| 484 | */ |
| 485 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 486 | { |
| 487 | switch (lua_type(L, ud)) { |
| 488 | |
| 489 | case LUA_TNUMBER: |
| 490 | smp->type = SMP_T_SINT; |
| 491 | smp->data.sint = lua_tointeger(L, ud); |
| 492 | break; |
| 493 | |
| 494 | |
| 495 | case LUA_TBOOLEAN: |
| 496 | smp->type = SMP_T_BOOL; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 497 | smp->data.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 498 | break; |
| 499 | |
| 500 | case LUA_TSTRING: |
| 501 | smp->type = SMP_T_STR; |
| 502 | smp->flags |= SMP_F_CONST; |
| 503 | smp->data.str.str = (char *)lua_tolstring(L, ud, (size_t *)&smp->data.str.len); |
| 504 | break; |
| 505 | |
| 506 | case LUA_TUSERDATA: |
| 507 | case LUA_TNIL: |
| 508 | case LUA_TTABLE: |
| 509 | case LUA_TFUNCTION: |
| 510 | case LUA_TTHREAD: |
| 511 | case LUA_TLIGHTUSERDATA: |
| 512 | smp->type = SMP_T_BOOL; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 513 | smp->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 514 | break; |
| 515 | } |
| 516 | return 1; |
| 517 | } |
| 518 | |
| 519 | /* This function check the "argp" builded by another conversion function |
| 520 | * is in accord with the expected argp defined by the "mask". The fucntion |
| 521 | * returns true or false. It can be adjust the types if there compatibles. |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 522 | * |
| 523 | * This function assumes thant the argp argument contains ARGM_NBARGS + 1 |
| 524 | * entries. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 525 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 526 | __LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp, |
| 527 | unsigned int mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 528 | { |
| 529 | int min_arg; |
| 530 | int idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 531 | struct proxy *px; |
| 532 | char *sname, *pname; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 533 | |
| 534 | idx = 0; |
| 535 | min_arg = ARGM(mask); |
| 536 | mask >>= ARGM_BITS; |
| 537 | |
| 538 | while (1) { |
| 539 | |
| 540 | /* Check oversize. */ |
| 541 | if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) { |
Cyril Bonté | 577a36a | 2015-03-02 00:08:38 +0100 | [diff] [blame] | 542 | WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | /* Check for mandatory arguments. */ |
| 546 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 547 | if (idx < min_arg) { |
| 548 | |
| 549 | /* If miss other argument than the first one, we return an error. */ |
| 550 | if (idx > 0) |
| 551 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 552 | |
| 553 | /* If first argument have a certain type, some default values |
| 554 | * may be used. See the function smp_resolve_args(). |
| 555 | */ |
| 556 | switch (mask & ARGT_MASK) { |
| 557 | |
| 558 | case ARGT_FE: |
| 559 | if (!(p->cap & PR_CAP_FE)) |
| 560 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 561 | argp[idx].data.prx = p; |
| 562 | argp[idx].type = ARGT_FE; |
| 563 | argp[idx+1].type = ARGT_STOP; |
| 564 | break; |
| 565 | |
| 566 | case ARGT_BE: |
| 567 | if (!(p->cap & PR_CAP_BE)) |
| 568 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 569 | argp[idx].data.prx = p; |
| 570 | argp[idx].type = ARGT_BE; |
| 571 | argp[idx+1].type = ARGT_STOP; |
| 572 | break; |
| 573 | |
| 574 | case ARGT_TAB: |
| 575 | argp[idx].data.prx = p; |
| 576 | argp[idx].type = ARGT_TAB; |
| 577 | argp[idx+1].type = ARGT_STOP; |
| 578 | break; |
| 579 | |
| 580 | default: |
| 581 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 582 | break; |
| 583 | } |
| 584 | } |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 585 | return 0; |
| 586 | } |
| 587 | |
| 588 | /* Check for exceed the number of requiered argument. */ |
| 589 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 590 | argp[idx].type != ARGT_STOP) { |
| 591 | WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected")); |
| 592 | } |
| 593 | |
| 594 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 595 | argp[idx].type == ARGT_STOP) { |
| 596 | return 0; |
| 597 | } |
| 598 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 599 | /* Convert some argument types. */ |
| 600 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 601 | case ARGT_SINT: |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 602 | if (argp[idx].type != ARGT_SINT) |
| 603 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
| 604 | argp[idx].type = ARGT_SINT; |
| 605 | break; |
| 606 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 607 | case ARGT_TIME: |
| 608 | if (argp[idx].type != ARGT_SINT) |
| 609 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 610 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 611 | break; |
| 612 | |
| 613 | case ARGT_SIZE: |
| 614 | if (argp[idx].type != ARGT_SINT) |
| 615 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 616 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 617 | break; |
| 618 | |
| 619 | case ARGT_FE: |
| 620 | if (argp[idx].type != ARGT_STR) |
| 621 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
| 622 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 623 | trash.str[argp[idx].data.str.len] = 0; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 624 | argp[idx].data.prx = proxy_fe_by_name(trash.str); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 625 | if (!argp[idx].data.prx) |
| 626 | WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist")); |
| 627 | argp[idx].type = ARGT_FE; |
| 628 | break; |
| 629 | |
| 630 | case ARGT_BE: |
| 631 | if (argp[idx].type != ARGT_STR) |
| 632 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
| 633 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 634 | trash.str[argp[idx].data.str.len] = 0; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 635 | argp[idx].data.prx = proxy_be_by_name(trash.str); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 636 | if (!argp[idx].data.prx) |
| 637 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 638 | argp[idx].type = ARGT_BE; |
| 639 | break; |
| 640 | |
| 641 | case ARGT_TAB: |
| 642 | if (argp[idx].type != ARGT_STR) |
| 643 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
| 644 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 645 | trash.str[argp[idx].data.str.len] = 0; |
Willy Tarreau | e2dc1fa | 2015-05-26 12:08:07 +0200 | [diff] [blame] | 646 | argp[idx].data.prx = proxy_tbl_by_name(trash.str); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 647 | if (!argp[idx].data.prx) |
| 648 | WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist")); |
| 649 | argp[idx].type = ARGT_TAB; |
| 650 | break; |
| 651 | |
| 652 | case ARGT_SRV: |
| 653 | if (argp[idx].type != ARGT_STR) |
| 654 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
| 655 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 656 | trash.str[argp[idx].data.str.len] = 0; |
| 657 | sname = strrchr(trash.str, '/'); |
| 658 | if (sname) { |
| 659 | *sname++ = '\0'; |
| 660 | pname = trash.str; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 661 | px = proxy_be_by_name(pname); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 662 | if (!px) |
| 663 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 664 | } |
| 665 | else { |
| 666 | sname = trash.str; |
| 667 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 668 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 669 | argp[idx].data.srv = findserver(px, sname); |
| 670 | if (!argp[idx].data.srv) |
| 671 | WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist")); |
| 672 | argp[idx].type = ARGT_SRV; |
| 673 | break; |
| 674 | |
| 675 | case ARGT_IPV4: |
| 676 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 677 | trash.str[argp[idx].data.str.len] = 0; |
| 678 | if (inet_pton(AF_INET, trash.str, &argp[idx].data.ipv4)) |
| 679 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address")); |
| 680 | argp[idx].type = ARGT_IPV4; |
| 681 | break; |
| 682 | |
| 683 | case ARGT_MSK4: |
| 684 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 685 | trash.str[argp[idx].data.str.len] = 0; |
| 686 | if (!str2mask(trash.str, &argp[idx].data.ipv4)) |
| 687 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask")); |
| 688 | argp[idx].type = ARGT_MSK4; |
| 689 | break; |
| 690 | |
| 691 | case ARGT_IPV6: |
| 692 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 693 | trash.str[argp[idx].data.str.len] = 0; |
| 694 | if (inet_pton(AF_INET6, trash.str, &argp[idx].data.ipv6)) |
| 695 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address")); |
| 696 | argp[idx].type = ARGT_IPV6; |
| 697 | break; |
| 698 | |
| 699 | case ARGT_MSK6: |
| 700 | case ARGT_MAP: |
| 701 | case ARGT_REG: |
| 702 | case ARGT_USR: |
| 703 | WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 704 | break; |
| 705 | } |
| 706 | |
| 707 | /* Check for type of argument. */ |
| 708 | if ((mask & ARGT_MASK) != argp[idx].type) { |
| 709 | const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 710 | arg_type_names[(mask & ARGT_MASK)], |
| 711 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 712 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 713 | } |
| 714 | |
| 715 | /* Next argument. */ |
| 716 | mask >>= ARGT_BITS; |
| 717 | idx++; |
| 718 | } |
| 719 | } |
| 720 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 721 | /* |
| 722 | * The following functions are used to make correspondance between the the |
| 723 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 724 | * |
| 725 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 726 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 727 | */ |
| 728 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 729 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 730 | struct hlua **hlua = lua_getextraspace(L); |
| 731 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 732 | } |
| 733 | static inline void hlua_sethlua(struct hlua *hlua) |
| 734 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 735 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 736 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 737 | } |
| 738 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 739 | /* This function is used to send logs. It try to send on screen (stderr) |
| 740 | * and on the default syslog server. |
| 741 | */ |
| 742 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 743 | { |
| 744 | struct tm tm; |
| 745 | char *p; |
| 746 | |
| 747 | /* Cleanup the log message. */ |
| 748 | p = trash.str; |
| 749 | for (; *msg != '\0'; msg++, p++) { |
| 750 | if (p >= trash.str + trash.size - 1) |
| 751 | return; |
| 752 | if (isprint(*msg)) |
| 753 | *p = *msg; |
| 754 | else |
| 755 | *p = '.'; |
| 756 | } |
| 757 | *p = '\0'; |
| 758 | |
| 759 | send_log(px, level, "%s", trash.str); |
| 760 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
| 761 | get_localtime(date.tv_sec, &tm); |
| 762 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
| 763 | log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, |
| 764 | (int)getpid(), trash.str); |
| 765 | fflush(stderr); |
| 766 | } |
| 767 | } |
| 768 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 769 | /* This function just ensure that the yield will be always |
| 770 | * returned with a timeout and permit to set some flags |
| 771 | */ |
| 772 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 773 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 774 | { |
| 775 | struct hlua *hlua = hlua_gethlua(L); |
| 776 | |
| 777 | /* Set the wake timeout. If timeout is required, we set |
| 778 | * the expiration time. |
| 779 | */ |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 780 | hlua->wake_time = tick_first(timeout, hlua->expire); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 781 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 782 | hlua->flags |= flags; |
| 783 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 784 | /* Process the yield. */ |
| 785 | WILL_LJMP(lua_yieldk(L, nresults, ctx, k)); |
| 786 | } |
| 787 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 788 | /* This function initialises the Lua environment stored in the stream. |
| 789 | * It must be called at the start of the stream. This function creates |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 790 | * an LUA coroutine. It can not be use to crete the main LUA context. |
| 791 | */ |
| 792 | int hlua_ctx_init(struct hlua *lua, struct task *task) |
| 793 | { |
| 794 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 795 | lua->flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 796 | LIST_INIT(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 797 | lua->T = lua_newthread(gL.T); |
| 798 | if (!lua->T) { |
| 799 | lua->Tref = LUA_REFNIL; |
| 800 | return 0; |
| 801 | } |
| 802 | hlua_sethlua(lua); |
| 803 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 804 | lua->task = task; |
| 805 | return 1; |
| 806 | } |
| 807 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 808 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 809 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 810 | * is not freed. |
| 811 | */ |
| 812 | void hlua_ctx_destroy(struct hlua *lua) |
| 813 | { |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 814 | if (!lua->T) |
| 815 | return; |
| 816 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 817 | /* Purge all the pending signals. */ |
| 818 | hlua_com_purge(lua); |
| 819 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 820 | /* The thread is garbage collected by Lua. */ |
| 821 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 822 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 823 | } |
| 824 | |
| 825 | /* This function is used to restore the Lua context when a coroutine |
| 826 | * fails. This function copy the common memory between old coroutine |
| 827 | * and the new coroutine. The old coroutine is destroyed, and its |
| 828 | * replaced by the new coroutine. |
| 829 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 830 | * as string error message and it is copied in the new stack. |
| 831 | */ |
| 832 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 833 | { |
| 834 | lua_State *T; |
| 835 | int new_ref; |
| 836 | |
| 837 | /* Renew the main LUA stack doesn't have sense. */ |
| 838 | if (lua == &gL) |
| 839 | return 0; |
| 840 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 841 | /* New Lua coroutine. */ |
| 842 | T = lua_newthread(gL.T); |
| 843 | if (!T) |
| 844 | return 0; |
| 845 | |
| 846 | /* Copy last error message. */ |
| 847 | if (keep_msg) |
| 848 | lua_xmove(lua->T, T, 1); |
| 849 | |
| 850 | /* Copy data between the coroutines. */ |
| 851 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 852 | lua_xmove(lua->T, T, 1); |
| 853 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */ |
| 854 | |
| 855 | /* Destroy old data. */ |
| 856 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 857 | |
| 858 | /* The thread is garbage collected by Lua. */ |
| 859 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 860 | |
| 861 | /* Fill the struct with the new coroutine values. */ |
| 862 | lua->Mref = new_ref; |
| 863 | lua->T = T; |
| 864 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 865 | |
| 866 | /* Set context. */ |
| 867 | hlua_sethlua(lua); |
| 868 | |
| 869 | return 1; |
| 870 | } |
| 871 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 872 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 873 | { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 874 | struct hlua *hlua = hlua_gethlua(L); |
| 875 | |
| 876 | /* Lua cannot yield when its returning from a function, |
| 877 | * so, we can fix the interrupt hook to 1 instruction, |
| 878 | * expecting that the function is finnished. |
| 879 | */ |
| 880 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 881 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 882 | return; |
| 883 | } |
| 884 | |
| 885 | /* restore the interrupt condition. */ |
| 886 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 887 | |
| 888 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 889 | * If the state is not yieldable, trying yield causes an error. |
| 890 | */ |
| 891 | if (lua_isyieldable(L)) |
| 892 | WILL_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
| 893 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 894 | /* If we cannot yield, update the clock and check the timeout. */ |
| 895 | tv_update_date(0, 1); |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 896 | if (tick_is_expired(hlua->expire, now_ms)) { |
| 897 | lua_pushfstring(L, "execution timeout"); |
| 898 | WILL_LJMP(lua_error(L)); |
| 899 | } |
| 900 | |
| 901 | /* Try to interrupt the process at the end of the current |
| 902 | * unyieldable function. |
| 903 | */ |
| 904 | lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 905 | } |
| 906 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 907 | /* This function start or resumes the Lua stack execution. If the flag |
| 908 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 909 | * The function return an error. |
| 910 | * |
| 911 | * The function can returns 4 values: |
| 912 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 913 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 914 | * task wakeup. |
| 915 | * - HLUA_E_ERRMSG : An error has occured, an error message is set in |
| 916 | * the top of the stack. |
| 917 | * - HLUA_E_ERR : An error has occured without error message. |
| 918 | * |
| 919 | * If an error occured, the stack is renewed and it is ready to run new |
| 920 | * LUA code. |
| 921 | */ |
| 922 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 923 | { |
| 924 | int ret; |
| 925 | const char *msg; |
| 926 | |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 927 | HLUA_SET_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 928 | |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 929 | /* If we want to resume the task, then check first the execution timeout. |
| 930 | * if it is reached, we can interrupt the Lua processing. |
| 931 | */ |
| 932 | if (tick_is_expired(lua->expire, now_ms)) |
| 933 | goto timeout_reached; |
| 934 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 935 | resume_execution: |
| 936 | |
| 937 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 938 | * instructions. it is used for preventing infinite loops. |
| 939 | */ |
| 940 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 941 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 942 | /* Remove all flags except the running flags. */ |
| 943 | lua->flags = HLUA_RUN; |
| 944 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 945 | /* Call the function. */ |
| 946 | ret = lua_resume(lua->T, gL.T, lua->nargs); |
| 947 | switch (ret) { |
| 948 | |
| 949 | case LUA_OK: |
| 950 | ret = HLUA_E_OK; |
| 951 | break; |
| 952 | |
| 953 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 954 | /* Check if the execution timeout is expired. It it is the case, we |
| 955 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 956 | */ |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 957 | if (tick_is_expired(lua->expire, now_ms)) { |
| 958 | |
| 959 | timeout_reached: |
| 960 | |
| 961 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 962 | if (!lua_checkstack(lua->T, 1)) { |
| 963 | ret = HLUA_E_ERR; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 964 | break; |
| 965 | } |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 966 | lua_pushfstring(lua->T, "execution timeout"); |
| 967 | ret = HLUA_E_ERRMSG; |
| 968 | break; |
| 969 | } |
| 970 | /* Process the forced yield. if the general yield is not allowed or |
| 971 | * if no task were associated this the current Lua execution |
| 972 | * coroutine, we resume the execution. Else we want to return in the |
| 973 | * scheduler and we want to be waked up again, to continue the |
| 974 | * current Lua execution. So we schedule our own task. |
| 975 | */ |
| 976 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 977 | if (!yield_allowed || !lua->task) |
| 978 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 979 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 980 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 981 | if (!yield_allowed) { |
| 982 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 983 | if (!lua_checkstack(lua->T, 1)) { |
| 984 | ret = HLUA_E_ERR; |
| 985 | break; |
| 986 | } |
| 987 | lua_pushfstring(lua->T, "yield not allowed"); |
| 988 | ret = HLUA_E_ERRMSG; |
| 989 | break; |
| 990 | } |
| 991 | ret = HLUA_E_AGAIN; |
| 992 | break; |
| 993 | |
| 994 | case LUA_ERRRUN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 995 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 996 | if (!lua_checkstack(lua->T, 1)) { |
| 997 | ret = HLUA_E_ERR; |
| 998 | break; |
| 999 | } |
| 1000 | msg = lua_tostring(lua->T, -1); |
| 1001 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1002 | lua_pop(lua->T, 1); |
| 1003 | if (msg) |
| 1004 | lua_pushfstring(lua->T, "runtime error: %s", msg); |
| 1005 | else |
| 1006 | lua_pushfstring(lua->T, "unknown runtime error"); |
| 1007 | ret = HLUA_E_ERRMSG; |
| 1008 | break; |
| 1009 | |
| 1010 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1011 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1012 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1013 | if (!lua_checkstack(lua->T, 1)) { |
| 1014 | ret = HLUA_E_ERR; |
| 1015 | break; |
| 1016 | } |
| 1017 | lua_pushfstring(lua->T, "out of memory error"); |
| 1018 | ret = HLUA_E_ERRMSG; |
| 1019 | break; |
| 1020 | |
| 1021 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1022 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1023 | if (!lua_checkstack(lua->T, 1)) { |
| 1024 | ret = HLUA_E_ERR; |
| 1025 | break; |
| 1026 | } |
| 1027 | msg = lua_tostring(lua->T, -1); |
| 1028 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1029 | lua_pop(lua->T, 1); |
| 1030 | if (msg) |
| 1031 | lua_pushfstring(lua->T, "message handler error: %s", msg); |
| 1032 | else |
| 1033 | lua_pushfstring(lua->T, "message handler error"); |
| 1034 | ret = HLUA_E_ERRMSG; |
| 1035 | break; |
| 1036 | |
| 1037 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1038 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1039 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1040 | if (!lua_checkstack(lua->T, 1)) { |
| 1041 | ret = HLUA_E_ERR; |
| 1042 | break; |
| 1043 | } |
| 1044 | lua_pushfstring(lua->T, "unknonwn error"); |
| 1045 | ret = HLUA_E_ERRMSG; |
| 1046 | break; |
| 1047 | } |
| 1048 | |
| 1049 | switch (ret) { |
| 1050 | case HLUA_E_AGAIN: |
| 1051 | break; |
| 1052 | |
| 1053 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1054 | hlua_com_purge(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1055 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1056 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1057 | break; |
| 1058 | |
| 1059 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1060 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1061 | hlua_com_purge(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1062 | hlua_ctx_renew(lua, 0); |
| 1063 | break; |
| 1064 | |
| 1065 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1066 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1067 | hlua_com_purge(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1068 | break; |
| 1069 | } |
| 1070 | |
| 1071 | return ret; |
| 1072 | } |
| 1073 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1074 | /* This function is an LUA binding. It provides a function |
| 1075 | * for deleting ACL from a referenced ACL file. |
| 1076 | */ |
| 1077 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1078 | { |
| 1079 | const char *name; |
| 1080 | const char *key; |
| 1081 | struct pat_ref *ref; |
| 1082 | |
| 1083 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1084 | |
| 1085 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1086 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1087 | |
| 1088 | ref = pat_ref_lookup(name); |
| 1089 | if (!ref) |
| 1090 | WILL_LJMP(luaL_error(L, "'del_acl': unkown acl file '%s'", name)); |
| 1091 | |
| 1092 | pat_ref_delete(ref, key); |
| 1093 | return 0; |
| 1094 | } |
| 1095 | |
| 1096 | /* This function is an LUA binding. It provides a function |
| 1097 | * for deleting map entry from a referenced map file. |
| 1098 | */ |
| 1099 | static int hlua_del_map(lua_State *L) |
| 1100 | { |
| 1101 | const char *name; |
| 1102 | const char *key; |
| 1103 | struct pat_ref *ref; |
| 1104 | |
| 1105 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1106 | |
| 1107 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1108 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1109 | |
| 1110 | ref = pat_ref_lookup(name); |
| 1111 | if (!ref) |
| 1112 | WILL_LJMP(luaL_error(L, "'del_map': unkown acl file '%s'", name)); |
| 1113 | |
| 1114 | pat_ref_delete(ref, key); |
| 1115 | return 0; |
| 1116 | } |
| 1117 | |
| 1118 | /* This function is an LUA binding. It provides a function |
| 1119 | * for adding ACL pattern from a referenced ACL file. |
| 1120 | */ |
| 1121 | static int hlua_add_acl(lua_State *L) |
| 1122 | { |
| 1123 | const char *name; |
| 1124 | const char *key; |
| 1125 | struct pat_ref *ref; |
| 1126 | |
| 1127 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1128 | |
| 1129 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1130 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1131 | |
| 1132 | ref = pat_ref_lookup(name); |
| 1133 | if (!ref) |
| 1134 | WILL_LJMP(luaL_error(L, "'add_acl': unkown acl file '%s'", name)); |
| 1135 | |
| 1136 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1137 | pat_ref_add(ref, key, NULL, NULL); |
| 1138 | return 0; |
| 1139 | } |
| 1140 | |
| 1141 | /* This function is an LUA binding. It provides a function |
| 1142 | * for setting map pattern and sample from a referenced map |
| 1143 | * file. |
| 1144 | */ |
| 1145 | static int hlua_set_map(lua_State *L) |
| 1146 | { |
| 1147 | const char *name; |
| 1148 | const char *key; |
| 1149 | const char *value; |
| 1150 | struct pat_ref *ref; |
| 1151 | |
| 1152 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1153 | |
| 1154 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1155 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1156 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1157 | |
| 1158 | ref = pat_ref_lookup(name); |
| 1159 | if (!ref) |
| 1160 | WILL_LJMP(luaL_error(L, "'set_map': unkown map file '%s'", name)); |
| 1161 | |
| 1162 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1163 | pat_ref_set(ref, key, value, NULL); |
| 1164 | else |
| 1165 | pat_ref_add(ref, key, value, NULL); |
| 1166 | return 0; |
| 1167 | } |
| 1168 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1169 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1170 | * an integer or user data. This data is associated with a metatable. This |
| 1171 | * metatable have an original version registred in the global context with |
| 1172 | * the name of the object (_G[<name>] = <metable> ). |
| 1173 | * |
| 1174 | * A metable is a table that modify the standard behavior of a standard |
| 1175 | * access to the associated data. The entries of this new metatable are |
| 1176 | * defined as is: |
| 1177 | * |
| 1178 | * http://lua-users.org/wiki/MetatableEvents |
| 1179 | * |
| 1180 | * __index |
| 1181 | * |
| 1182 | * we access an absent field in a table, the result is nil. This is |
| 1183 | * true, but it is not the whole truth. Actually, such access triggers |
| 1184 | * the interpreter to look for an __index metamethod: If there is no |
| 1185 | * such method, as usually happens, then the access results in nil; |
| 1186 | * otherwise, the metamethod will provide the result. |
| 1187 | * |
| 1188 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1189 | * the key does not appear in the table, but the metatable has an __index |
| 1190 | * property: |
| 1191 | * |
| 1192 | * - if the value is a function, the function is called, passing in the |
| 1193 | * table and the key; the return value of that function is returned as |
| 1194 | * the result. |
| 1195 | * |
| 1196 | * - if the value is another table, the value of the key in that table is |
| 1197 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1198 | * table's metatable has an __index property, then it continues on up) |
| 1199 | * |
| 1200 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1201 | * |
| 1202 | * http://www.lua.org/pil/13.4.1.html |
| 1203 | * |
| 1204 | * __newindex |
| 1205 | * |
| 1206 | * Like __index, but control property assignment. |
| 1207 | * |
| 1208 | * __mode - Control weak references. A string value with one or both |
| 1209 | * of the characters 'k' and 'v' which specifies that the the |
| 1210 | * keys and/or values in the table are weak references. |
| 1211 | * |
| 1212 | * __call - Treat a table like a function. When a table is followed by |
| 1213 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1214 | * a __call key pointing to a function, that function is invoked |
| 1215 | * (passing any specified arguments) and the return value is |
| 1216 | * returned. |
| 1217 | * |
| 1218 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1219 | * called, if the metatable for myTable has a __metatable |
| 1220 | * key, the value of that key is returned instead of the |
| 1221 | * actual metatable. |
| 1222 | * |
| 1223 | * __tostring - Control string representation. When the builtin |
| 1224 | * "tostring( myTable )" function is called, if the metatable |
| 1225 | * for myTable has a __tostring property set to a function, |
| 1226 | * that function is invoked (passing myTable to it) and the |
| 1227 | * return value is used as the string representation. |
| 1228 | * |
| 1229 | * __len - Control table length. When the table length is requested using |
| 1230 | * the length operator ( '#' ), if the metatable for myTable has |
| 1231 | * a __len key pointing to a function, that function is invoked |
| 1232 | * (passing myTable to it) and the return value used as the value |
| 1233 | * of "#myTable". |
| 1234 | * |
| 1235 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1236 | * collected, if the metatable has a __gc field pointing to a |
| 1237 | * function, that function is first invoked, passing the userdata |
| 1238 | * to it. The __gc metamethod is not called for tables. |
| 1239 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1240 | * |
| 1241 | * Special metamethods for redefining standard operators: |
| 1242 | * http://www.lua.org/pil/13.1.html |
| 1243 | * |
| 1244 | * __add "+" |
| 1245 | * __sub "-" |
| 1246 | * __mul "*" |
| 1247 | * __div "/" |
| 1248 | * __unm "!" |
| 1249 | * __pow "^" |
| 1250 | * __concat ".." |
| 1251 | * |
| 1252 | * Special methods for redfining standar relations |
| 1253 | * http://www.lua.org/pil/13.2.html |
| 1254 | * |
| 1255 | * __eq "==" |
| 1256 | * __lt "<" |
| 1257 | * __le "<=" |
| 1258 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1259 | |
| 1260 | /* |
| 1261 | * |
| 1262 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1263 | * Class Map |
| 1264 | * |
| 1265 | * |
| 1266 | */ |
| 1267 | |
| 1268 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1269 | * a class session, otherwise it throws an error. |
| 1270 | */ |
| 1271 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1272 | { |
| 1273 | return (struct map_descriptor *)MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
| 1274 | } |
| 1275 | |
| 1276 | /* This function is the map constructor. It don't need |
| 1277 | * the class Map object. It creates and return a new Map |
| 1278 | * object. It must be called only during "body" or "init" |
| 1279 | * context because it process some filesystem accesses. |
| 1280 | */ |
| 1281 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1282 | { |
| 1283 | const char *fn; |
| 1284 | int match = PAT_MATCH_STR; |
| 1285 | struct sample_conv conv; |
| 1286 | const char *file = ""; |
| 1287 | int line = 0; |
| 1288 | lua_Debug ar; |
| 1289 | char *err = NULL; |
| 1290 | struct arg args[2]; |
| 1291 | |
| 1292 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1293 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1294 | |
| 1295 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1296 | |
| 1297 | if (lua_gettop(L) >= 2) { |
| 1298 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1299 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1300 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1301 | } |
| 1302 | |
| 1303 | /* Get Lua filename and line number. */ |
| 1304 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1305 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1306 | if (ar.currentline > 0) { /* is there info? */ |
| 1307 | file = ar.short_src; |
| 1308 | line = ar.currentline; |
| 1309 | } |
| 1310 | } |
| 1311 | |
| 1312 | /* fill fake sample_conv struct. */ |
| 1313 | conv.kw = ""; /* unused. */ |
| 1314 | conv.process = NULL; /* unused. */ |
| 1315 | conv.arg_mask = 0; /* unused. */ |
| 1316 | conv.val_args = NULL; /* unused. */ |
| 1317 | conv.out_type = SMP_T_STR; |
| 1318 | conv.private = (void *)(long)match; |
| 1319 | switch (match) { |
| 1320 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1321 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1322 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1323 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1324 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1325 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1326 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1327 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1328 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1329 | default: |
| 1330 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1331 | } |
| 1332 | |
| 1333 | /* fill fake args. */ |
| 1334 | args[0].type = ARGT_STR; |
| 1335 | args[0].data.str.str = (char *)fn; |
| 1336 | args[1].type = ARGT_STOP; |
| 1337 | |
| 1338 | /* load the map. */ |
| 1339 | if (!sample_load_map(args, &conv, file, line, &err)) { |
| 1340 | /* error case: we cant use luaL_error because we must |
| 1341 | * free the err variable. |
| 1342 | */ |
| 1343 | luaL_where(L, 1); |
| 1344 | lua_pushfstring(L, "'new': %s.", err); |
| 1345 | lua_concat(L, 2); |
| 1346 | free(err); |
| 1347 | WILL_LJMP(lua_error(L)); |
| 1348 | } |
| 1349 | |
| 1350 | /* create the lua object. */ |
| 1351 | lua_newtable(L); |
| 1352 | lua_pushlightuserdata(L, args[0].data.map); |
| 1353 | lua_rawseti(L, -2, 0); |
| 1354 | |
| 1355 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1356 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1357 | lua_setmetatable(L, -2); |
| 1358 | |
| 1359 | |
| 1360 | return 1; |
| 1361 | } |
| 1362 | |
| 1363 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1364 | { |
| 1365 | struct map_descriptor *desc; |
| 1366 | struct pattern *pat; |
| 1367 | struct sample smp; |
| 1368 | |
| 1369 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1370 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1371 | if (desc->pat.expect_type == SMP_T_SINT) { |
| 1372 | smp.type = SMP_T_SINT; |
| 1373 | smp.data.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1374 | } |
| 1375 | else { |
| 1376 | smp.type = SMP_T_STR; |
| 1377 | smp.flags = SMP_F_CONST; |
| 1378 | smp.data.str.str = (char *)MAY_LJMP(luaL_checklstring(L, 2, (size_t *)&smp.data.str.len)); |
| 1379 | } |
| 1380 | |
| 1381 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
| 1382 | if (!pat || !pat->smp) { |
| 1383 | if (str) |
| 1384 | lua_pushstring(L, ""); |
| 1385 | else |
| 1386 | lua_pushnil(L); |
| 1387 | return 1; |
| 1388 | } |
| 1389 | |
| 1390 | /* The Lua pattern must return a string, so we can't check the returned type */ |
| 1391 | lua_pushlstring(L, pat->smp->data.str.str, pat->smp->data.str.len); |
| 1392 | return 1; |
| 1393 | } |
| 1394 | |
| 1395 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1396 | { |
| 1397 | return _hlua_map_lookup(L, 0); |
| 1398 | } |
| 1399 | |
| 1400 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1401 | { |
| 1402 | return _hlua_map_lookup(L, 1); |
| 1403 | } |
| 1404 | |
| 1405 | /* |
| 1406 | * |
| 1407 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1408 | * Class Socket |
| 1409 | * |
| 1410 | * |
| 1411 | */ |
| 1412 | |
| 1413 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1414 | { |
| 1415 | return (struct hlua_socket *)MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
| 1416 | } |
| 1417 | |
| 1418 | /* This function is the handler called for each I/O on the established |
| 1419 | * connection. It is used for notify space avalaible to send or data |
| 1420 | * received. |
| 1421 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1422 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1423 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1424 | struct stream_interface *si = appctx->owner; |
Willy Tarreau | 50fe03b | 2014-11-28 13:59:31 +0100 | [diff] [blame] | 1425 | struct connection *c = objt_conn(si_opposite(si)->end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1426 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1427 | /* Wakeup the main stream if the client connection is closed. */ |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 1428 | if (!c || channel_output_closed(si_ic(si)) || channel_input_closed(si_oc(si))) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1429 | if (appctx->ctx.hlua.socket) { |
| 1430 | appctx->ctx.hlua.socket->s = NULL; |
| 1431 | appctx->ctx.hlua.socket = NULL; |
| 1432 | } |
| 1433 | si_shutw(si); |
| 1434 | si_shutr(si); |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 1435 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1436 | hlua_com_wake(&appctx->ctx.hlua.wake_on_read); |
| 1437 | hlua_com_wake(&appctx->ctx.hlua.wake_on_write); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1438 | return; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1439 | } |
| 1440 | |
| 1441 | if (!(c->flags & CO_FL_CONNECTED)) |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1442 | return; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1443 | |
| 1444 | /* This function is called after the connect. */ |
| 1445 | appctx->ctx.hlua.connected = 1; |
| 1446 | |
| 1447 | /* Wake the tasks which wants to write if the buffer have avalaible space. */ |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 1448 | if (channel_may_recv(si_oc(si))) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1449 | hlua_com_wake(&appctx->ctx.hlua.wake_on_write); |
| 1450 | |
| 1451 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 1452 | if (channel_is_empty(si_ic(si))) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1453 | hlua_com_wake(&appctx->ctx.hlua.wake_on_read); |
| 1454 | } |
| 1455 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1456 | /* This function is called when the "struct stream" is destroyed. |
| 1457 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1458 | * Wake all the pending signals. |
| 1459 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1460 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1461 | { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1462 | /* Remove my link in the original object. */ |
| 1463 | if (appctx->ctx.hlua.socket) |
| 1464 | appctx->ctx.hlua.socket->s = NULL; |
| 1465 | |
| 1466 | /* Wake all the task waiting for me. */ |
| 1467 | hlua_com_wake(&appctx->ctx.hlua.wake_on_read); |
| 1468 | hlua_com_wake(&appctx->ctx.hlua.wake_on_write); |
| 1469 | } |
| 1470 | |
| 1471 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1472 | * uses this object. If the stream does not exists, just quit. |
| 1473 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1474 | * pending signal can rest in the read and write lists. destroy |
| 1475 | * it. |
| 1476 | */ |
| 1477 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1478 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1479 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1480 | struct appctx *appctx; |
| 1481 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1482 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1483 | |
| 1484 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1485 | if (!socket->s) |
| 1486 | return 0; |
| 1487 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1488 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1489 | appctx = objt_appctx(socket->s->si[0].end); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1490 | stream_shutdown(socket->s, SF_ERR_KILLED); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1491 | socket->s = NULL; |
| 1492 | appctx->ctx.hlua.socket = NULL; |
| 1493 | |
| 1494 | return 0; |
| 1495 | } |
| 1496 | |
| 1497 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1498 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1499 | */ |
| 1500 | __LJMP static int hlua_socket_close(lua_State *L) |
| 1501 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1502 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1503 | struct appctx *appctx; |
| 1504 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1505 | MAY_LJMP(check_args(L, 1, "close")); |
| 1506 | |
| 1507 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1508 | if (!socket->s) |
| 1509 | return 0; |
| 1510 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1511 | /* Close the stream and remove the associated stop task. */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1512 | stream_shutdown(socket->s, SF_ERR_KILLED); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1513 | appctx = objt_appctx(socket->s->si[0].end); |
| 1514 | appctx->ctx.hlua.socket = NULL; |
| 1515 | socket->s = NULL; |
| 1516 | |
| 1517 | return 0; |
| 1518 | } |
| 1519 | |
| 1520 | /* This Lua function assumes that the stack contain three parameters. |
| 1521 | * 1 - USERDATA containing a struct socket |
| 1522 | * 2 - INTEGER with values of the macro defined below |
| 1523 | * If the integer is -1, we must read at most one line. |
| 1524 | * If the integer is -2, we ust read all the data until the |
| 1525 | * end of the stream. |
| 1526 | * If the integer is positive value, we must read a number of |
| 1527 | * bytes corresponding to this value. |
| 1528 | */ |
| 1529 | #define HLSR_READ_LINE (-1) |
| 1530 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1531 | __LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1532 | { |
| 1533 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1534 | int wanted = lua_tointeger(L, 2); |
| 1535 | struct hlua *hlua = hlua_gethlua(L); |
| 1536 | struct appctx *appctx; |
| 1537 | int len; |
| 1538 | int nblk; |
| 1539 | char *blk1; |
| 1540 | int len1; |
| 1541 | char *blk2; |
| 1542 | int len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1543 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1544 | struct channel *oc; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1545 | |
| 1546 | /* Check if this lua stack is schedulable. */ |
| 1547 | if (!hlua || !hlua->task) |
| 1548 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 1549 | "'frontend', 'backend' or 'task'")); |
| 1550 | |
| 1551 | /* check for connection closed. If some data where read, return it. */ |
| 1552 | if (!socket->s) |
| 1553 | goto connection_closed; |
| 1554 | |
Willy Tarreau | 94aa617 | 2015-03-13 14:19:06 +0100 | [diff] [blame] | 1555 | oc = &socket->s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1556 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1557 | /* Read line. */ |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1558 | nblk = bo_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1559 | if (nblk < 0) /* Connection close. */ |
| 1560 | goto connection_closed; |
| 1561 | if (nblk == 0) /* No data avalaible. */ |
| 1562 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1563 | |
| 1564 | /* remove final \r\n. */ |
| 1565 | if (nblk == 1) { |
| 1566 | if (blk1[len1-1] == '\n') { |
| 1567 | len1--; |
| 1568 | skip_at_end++; |
| 1569 | if (blk1[len1-1] == '\r') { |
| 1570 | len1--; |
| 1571 | skip_at_end++; |
| 1572 | } |
| 1573 | } |
| 1574 | } |
| 1575 | else { |
| 1576 | if (blk2[len2-1] == '\n') { |
| 1577 | len2--; |
| 1578 | skip_at_end++; |
| 1579 | if (blk2[len2-1] == '\r') { |
| 1580 | len2--; |
| 1581 | skip_at_end++; |
| 1582 | } |
| 1583 | } |
| 1584 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1585 | } |
| 1586 | |
| 1587 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1588 | /* Read all the available data. */ |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1589 | nblk = bo_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1590 | if (nblk < 0) /* Connection close. */ |
| 1591 | goto connection_closed; |
| 1592 | if (nblk == 0) /* No data avalaible. */ |
| 1593 | goto connection_empty; |
| 1594 | } |
| 1595 | |
| 1596 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1597 | /* Read a block of data. */ |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1598 | nblk = bo_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1599 | if (nblk < 0) /* Connection close. */ |
| 1600 | goto connection_closed; |
| 1601 | if (nblk == 0) /* No data avalaible. */ |
| 1602 | goto connection_empty; |
| 1603 | |
| 1604 | if (len1 > wanted) { |
| 1605 | nblk = 1; |
| 1606 | len1 = wanted; |
| 1607 | } if (nblk == 2 && len1 + len2 > wanted) |
| 1608 | len2 = wanted - len1; |
| 1609 | } |
| 1610 | |
| 1611 | len = len1; |
| 1612 | |
| 1613 | luaL_addlstring(&socket->b, blk1, len1); |
| 1614 | if (nblk == 2) { |
| 1615 | len += len2; |
| 1616 | luaL_addlstring(&socket->b, blk2, len2); |
| 1617 | } |
| 1618 | |
| 1619 | /* Consume data. */ |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1620 | bo_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1621 | |
| 1622 | /* Don't wait anything. */ |
Willy Tarreau | 828824a | 2015-04-19 17:20:03 +0200 | [diff] [blame] | 1623 | si_applet_done(&socket->s->si[0]); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1624 | |
| 1625 | /* If the pattern reclaim to read all the data |
| 1626 | * in the connection, got out. |
| 1627 | */ |
| 1628 | if (wanted == HLSR_READ_ALL) |
| 1629 | goto connection_empty; |
| 1630 | else if (wanted >= 0 && len < wanted) |
| 1631 | goto connection_empty; |
| 1632 | |
| 1633 | /* Return result. */ |
| 1634 | luaL_pushresult(&socket->b); |
| 1635 | return 1; |
| 1636 | |
| 1637 | connection_closed: |
| 1638 | |
| 1639 | /* If the buffer containds data. */ |
| 1640 | if (socket->b.n > 0) { |
| 1641 | luaL_pushresult(&socket->b); |
| 1642 | return 1; |
| 1643 | } |
| 1644 | lua_pushnil(L); |
| 1645 | lua_pushstring(L, "connection closed."); |
| 1646 | return 2; |
| 1647 | |
| 1648 | connection_empty: |
| 1649 | |
| 1650 | appctx = objt_appctx(socket->s->si[0].end); |
| 1651 | if (!hlua_com_new(hlua, &appctx->ctx.hlua.wake_on_read)) |
| 1652 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 1653 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1654 | return 0; |
| 1655 | } |
| 1656 | |
| 1657 | /* This Lus function gets two parameters. The first one can be string |
| 1658 | * or a number. If the string is "*l", the user require one line. If |
| 1659 | * the string is "*a", the user require all the content of the stream. |
| 1660 | * If the value is a number, the user require a number of bytes equal |
| 1661 | * to the value. The default value is "*l" (a line). |
| 1662 | * |
| 1663 | * This paraeter with a variable type is converted in integer. This |
| 1664 | * integer takes this values: |
| 1665 | * -1 : read a line |
| 1666 | * -2 : read all the stream |
| 1667 | * >0 : amount if bytes. |
| 1668 | * |
| 1669 | * The second parameter is optinal. It contains a string that must be |
| 1670 | * concatenated with the read data. |
| 1671 | */ |
| 1672 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 1673 | { |
| 1674 | int wanted = HLSR_READ_LINE; |
| 1675 | const char *pattern; |
| 1676 | int type; |
| 1677 | char *error; |
| 1678 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1679 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1680 | |
| 1681 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 1682 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 1683 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1684 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1685 | |
| 1686 | /* check for pattern. */ |
| 1687 | if (lua_gettop(L) >= 2) { |
| 1688 | type = lua_type(L, 2); |
| 1689 | if (type == LUA_TSTRING) { |
| 1690 | pattern = lua_tostring(L, 2); |
| 1691 | if (strcmp(pattern, "*a") == 0) |
| 1692 | wanted = HLSR_READ_ALL; |
| 1693 | else if (strcmp(pattern, "*l") == 0) |
| 1694 | wanted = HLSR_READ_LINE; |
| 1695 | else { |
| 1696 | wanted = strtoll(pattern, &error, 10); |
| 1697 | if (*error != '\0') |
| 1698 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 1699 | } |
| 1700 | } |
| 1701 | else if (type == LUA_TNUMBER) { |
| 1702 | wanted = lua_tointeger(L, 2); |
| 1703 | if (wanted < 0) |
| 1704 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 1705 | } |
| 1706 | } |
| 1707 | |
| 1708 | /* Set pattern. */ |
| 1709 | lua_pushinteger(L, wanted); |
| 1710 | lua_replace(L, 2); |
| 1711 | |
| 1712 | /* init bufffer, and fiil it wih prefix. */ |
| 1713 | luaL_buffinit(L, &socket->b); |
| 1714 | |
| 1715 | /* Check prefix. */ |
| 1716 | if (lua_gettop(L) >= 3) { |
| 1717 | if (lua_type(L, 3) != LUA_TSTRING) |
| 1718 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 1719 | pattern = lua_tolstring(L, 3, &len); |
| 1720 | luaL_addlstring(&socket->b, pattern, len); |
| 1721 | } |
| 1722 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1723 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1724 | } |
| 1725 | |
| 1726 | /* Write the Lua input string in the output buffer. |
| 1727 | * This fucntion returns a yield if no space are available. |
| 1728 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1729 | static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1730 | { |
| 1731 | struct hlua_socket *socket; |
| 1732 | struct hlua *hlua = hlua_gethlua(L); |
| 1733 | struct appctx *appctx; |
| 1734 | size_t buf_len; |
| 1735 | const char *buf; |
| 1736 | int len; |
| 1737 | int send_len; |
| 1738 | int sent; |
| 1739 | |
| 1740 | /* Check if this lua stack is schedulable. */ |
| 1741 | if (!hlua || !hlua->task) |
| 1742 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 1743 | "'frontend', 'backend' or 'task'")); |
| 1744 | |
| 1745 | /* Get object */ |
| 1746 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1747 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1748 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1749 | |
| 1750 | /* Check for connection close. */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1751 | if (!socket->s || channel_output_closed(&socket->s->req)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1752 | lua_pushinteger(L, -1); |
| 1753 | return 1; |
| 1754 | } |
| 1755 | |
| 1756 | /* Update the input buffer data. */ |
| 1757 | buf += sent; |
| 1758 | send_len = buf_len - sent; |
| 1759 | |
| 1760 | /* All the data are sent. */ |
| 1761 | if (sent >= buf_len) |
| 1762 | return 1; /* Implicitly return the length sent. */ |
| 1763 | |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 1764 | /* Check if the buffer is avalaible because HAProxy doesn't allocate |
| 1765 | * the request buffer if its not required. |
| 1766 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1767 | if (socket->s->req.buf->size == 0) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1768 | if (!stream_alloc_recv_buffer(&socket->s->req)) { |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 1769 | socket->s->si[0].flags |= SI_FL_WAIT_ROOM; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 1770 | goto hlua_socket_write_yield_return; |
| 1771 | } |
| 1772 | } |
| 1773 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1774 | /* Check for avalaible space. */ |
Willy Tarreau | 94aa617 | 2015-03-13 14:19:06 +0100 | [diff] [blame] | 1775 | len = buffer_total_space(socket->s->req.buf); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1776 | if (len <= 0) |
| 1777 | goto hlua_socket_write_yield_return; |
| 1778 | |
| 1779 | /* send data */ |
| 1780 | if (len < send_len) |
| 1781 | send_len = len; |
Willy Tarreau | 94aa617 | 2015-03-13 14:19:06 +0100 | [diff] [blame] | 1782 | len = bi_putblk(&socket->s->req, buf+sent, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1783 | |
| 1784 | /* "Not enough space" (-1), "Buffer too little to contain |
| 1785 | * the data" (-2) are not expected because the available length |
| 1786 | * is tested. |
| 1787 | * Other unknown error are also not expected. |
| 1788 | */ |
| 1789 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 1790 | if (len == -1) |
Willy Tarreau | 94aa617 | 2015-03-13 14:19:06 +0100 | [diff] [blame] | 1791 | socket->s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 1792 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1793 | MAY_LJMP(hlua_socket_close(L)); |
| 1794 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1795 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1796 | return 1; |
| 1797 | } |
| 1798 | |
| 1799 | /* update buffers. */ |
Willy Tarreau | 828824a | 2015-04-19 17:20:03 +0200 | [diff] [blame] | 1800 | si_applet_done(&socket->s->si[0]); |
Willy Tarreau | 94aa617 | 2015-03-13 14:19:06 +0100 | [diff] [blame] | 1801 | socket->s->req.rex = TICK_ETERNITY; |
| 1802 | socket->s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1803 | |
| 1804 | /* Update length sent. */ |
| 1805 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1806 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1807 | |
| 1808 | /* All the data buffer is sent ? */ |
| 1809 | if (sent + len >= buf_len) |
| 1810 | return 1; |
| 1811 | |
| 1812 | hlua_socket_write_yield_return: |
| 1813 | appctx = objt_appctx(socket->s->si[0].end); |
| 1814 | if (!hlua_com_new(hlua, &appctx->ctx.hlua.wake_on_write)) |
| 1815 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 1816 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1817 | return 0; |
| 1818 | } |
| 1819 | |
| 1820 | /* This function initiate the send of data. It just check the input |
| 1821 | * parameters and push an integer in the Lua stack that contain the |
| 1822 | * amount of data writed in the buffer. This is used by the function |
| 1823 | * "hlua_socket_write_yield" that can yield. |
| 1824 | * |
| 1825 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 1826 | * the associated object. The second is a string buffer. The third is |
| 1827 | * a facultative integer that represents where is the buffer position |
| 1828 | * of the start of the data that can send. The first byte is the |
| 1829 | * position "1". The default value is "1". The fourth argument is a |
| 1830 | * facultative integer that represents where is the buffer position |
| 1831 | * of the end of the data that can send. The default is the last byte. |
| 1832 | */ |
| 1833 | static int hlua_socket_send(struct lua_State *L) |
| 1834 | { |
| 1835 | int i; |
| 1836 | int j; |
| 1837 | const char *buf; |
| 1838 | size_t buf_len; |
| 1839 | |
| 1840 | /* Check number of arguments. */ |
| 1841 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 1842 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 1843 | |
| 1844 | /* Get the string. */ |
| 1845 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 1846 | |
| 1847 | /* Get and check j. */ |
| 1848 | if (lua_gettop(L) == 4) { |
| 1849 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 1850 | if (j < 0) |
| 1851 | j = buf_len + j + 1; |
| 1852 | if (j > buf_len) |
| 1853 | j = buf_len + 1; |
| 1854 | lua_pop(L, 1); |
| 1855 | } |
| 1856 | else |
| 1857 | j = buf_len; |
| 1858 | |
| 1859 | /* Get and check i. */ |
| 1860 | if (lua_gettop(L) == 3) { |
| 1861 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 1862 | if (i < 0) |
| 1863 | i = buf_len + i + 1; |
| 1864 | if (i > buf_len) |
| 1865 | i = buf_len + 1; |
| 1866 | lua_pop(L, 1); |
| 1867 | } else |
| 1868 | i = 1; |
| 1869 | |
| 1870 | /* Check bth i and j. */ |
| 1871 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1872 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1873 | return 1; |
| 1874 | } |
| 1875 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1876 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1877 | return 1; |
| 1878 | } |
| 1879 | if (i == 0) |
| 1880 | i = 1; |
| 1881 | if (j == 0) |
| 1882 | j = 1; |
| 1883 | |
| 1884 | /* Pop the string. */ |
| 1885 | lua_pop(L, 1); |
| 1886 | |
| 1887 | /* Update the buffer length. */ |
| 1888 | buf += i - 1; |
| 1889 | buf_len = j - i + 1; |
| 1890 | lua_pushlstring(L, buf, buf_len); |
| 1891 | |
| 1892 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1893 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1894 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1895 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1896 | } |
| 1897 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 1898 | #define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345") |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1899 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 1900 | { |
| 1901 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 1902 | int ret; |
| 1903 | int len; |
| 1904 | char *p; |
| 1905 | |
| 1906 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 1907 | if (ret <= 0) { |
| 1908 | lua_pushnil(L); |
| 1909 | return 1; |
| 1910 | } |
| 1911 | |
| 1912 | if (ret == AF_UNIX) { |
| 1913 | lua_pushstring(L, buffer+1); |
| 1914 | return 1; |
| 1915 | } |
| 1916 | else if (ret == AF_INET6) { |
| 1917 | buffer[0] = '['; |
| 1918 | len = strlen(buffer); |
| 1919 | buffer[len] = ']'; |
| 1920 | len++; |
| 1921 | buffer[len] = ':'; |
| 1922 | len++; |
| 1923 | p = buffer; |
| 1924 | } |
| 1925 | else if (ret == AF_INET) { |
| 1926 | p = buffer + 1; |
| 1927 | len = strlen(p); |
| 1928 | p[len] = ':'; |
| 1929 | len++; |
| 1930 | } |
| 1931 | else { |
| 1932 | lua_pushnil(L); |
| 1933 | return 1; |
| 1934 | } |
| 1935 | |
| 1936 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 1937 | lua_pushnil(L); |
| 1938 | return 1; |
| 1939 | } |
| 1940 | |
| 1941 | lua_pushstring(L, p); |
| 1942 | return 1; |
| 1943 | } |
| 1944 | |
| 1945 | /* Returns information about the peer of the connection. */ |
| 1946 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 1947 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1948 | struct hlua_socket *socket; |
| 1949 | struct connection *conn; |
| 1950 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1951 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 1952 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1953 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1954 | |
| 1955 | /* Check if the tcp object is avalaible. */ |
| 1956 | if (!socket->s) { |
| 1957 | lua_pushnil(L); |
| 1958 | return 1; |
| 1959 | } |
| 1960 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1961 | conn = objt_conn(socket->s->si[1].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1962 | if (!conn) { |
| 1963 | lua_pushnil(L); |
| 1964 | return 1; |
| 1965 | } |
| 1966 | |
| 1967 | if (!(conn->flags & CO_FL_ADDR_TO_SET)) { |
| 1968 | unsigned int salen = sizeof(conn->addr.to); |
| 1969 | if (getpeername(conn->t.sock.fd, (struct sockaddr *)&conn->addr.to, &salen) == -1) { |
| 1970 | lua_pushnil(L); |
| 1971 | return 1; |
| 1972 | } |
| 1973 | conn->flags |= CO_FL_ADDR_TO_SET; |
| 1974 | } |
| 1975 | |
| 1976 | return MAY_LJMP(hlua_socket_info(L, &conn->addr.to)); |
| 1977 | } |
| 1978 | |
| 1979 | /* Returns information about my connection side. */ |
| 1980 | static int hlua_socket_getsockname(struct lua_State *L) |
| 1981 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1982 | struct hlua_socket *socket; |
| 1983 | struct connection *conn; |
| 1984 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1985 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 1986 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1987 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1988 | |
| 1989 | /* Check if the tcp object is avalaible. */ |
| 1990 | if (!socket->s) { |
| 1991 | lua_pushnil(L); |
| 1992 | return 1; |
| 1993 | } |
| 1994 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1995 | conn = objt_conn(socket->s->si[1].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1996 | if (!conn) { |
| 1997 | lua_pushnil(L); |
| 1998 | return 1; |
| 1999 | } |
| 2000 | |
| 2001 | if (!(conn->flags & CO_FL_ADDR_FROM_SET)) { |
| 2002 | unsigned int salen = sizeof(conn->addr.from); |
| 2003 | if (getsockname(conn->t.sock.fd, (struct sockaddr *)&conn->addr.from, &salen) == -1) { |
| 2004 | lua_pushnil(L); |
| 2005 | return 1; |
| 2006 | } |
| 2007 | conn->flags |= CO_FL_ADDR_FROM_SET; |
| 2008 | } |
| 2009 | |
| 2010 | return hlua_socket_info(L, &conn->addr.from); |
| 2011 | } |
| 2012 | |
| 2013 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2014 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2015 | .obj_type = OBJ_TYPE_APPLET, |
| 2016 | .name = "<LUA_TCP>", |
| 2017 | .fct = hlua_socket_handler, |
| 2018 | .release = hlua_socket_release, |
| 2019 | }; |
| 2020 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2021 | __LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2022 | { |
| 2023 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2024 | struct hlua *hlua = hlua_gethlua(L); |
| 2025 | struct appctx *appctx; |
| 2026 | |
| 2027 | /* Check for connection close. */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2028 | if (!hlua || !socket->s || channel_output_closed(&socket->s->req)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2029 | lua_pushnil(L); |
| 2030 | lua_pushstring(L, "Can't connect"); |
| 2031 | return 2; |
| 2032 | } |
| 2033 | |
| 2034 | appctx = objt_appctx(socket->s->si[0].end); |
| 2035 | |
| 2036 | /* Check for connection established. */ |
| 2037 | if (appctx->ctx.hlua.connected) { |
| 2038 | lua_pushinteger(L, 1); |
| 2039 | return 1; |
| 2040 | } |
| 2041 | |
| 2042 | if (!hlua_com_new(hlua, &appctx->ctx.hlua.wake_on_write)) |
| 2043 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 2044 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2045 | return 0; |
| 2046 | } |
| 2047 | |
| 2048 | /* This function fail or initite the connection. */ |
| 2049 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2050 | { |
| 2051 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2052 | int port; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2053 | const char *ip; |
| 2054 | struct connection *conn; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2055 | struct hlua *hlua; |
| 2056 | struct appctx *appctx; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2057 | |
| 2058 | MAY_LJMP(check_args(L, 3, "connect")); |
| 2059 | |
| 2060 | /* Get args. */ |
| 2061 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2062 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2063 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2064 | |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 2065 | conn = si_alloc_conn(&socket->s->si[1], 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2066 | if (!conn) |
| 2067 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
| 2068 | |
| 2069 | /* Parse ip address. */ |
| 2070 | conn->addr.to.ss_family = AF_UNSPEC; |
| 2071 | if (!str2ip2(ip, &conn->addr.to, 0)) |
| 2072 | WILL_LJMP(luaL_error(L, "connect: cannot parse ip address '%s'", ip)); |
| 2073 | |
| 2074 | /* Set port. */ |
| 2075 | if (conn->addr.to.ss_family == AF_INET) |
| 2076 | ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(port); |
| 2077 | else if (conn->addr.to.ss_family == AF_INET6) |
| 2078 | ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(port); |
| 2079 | |
| 2080 | /* it is important not to call the wakeup function directly but to |
| 2081 | * pass through task_wakeup(), because this one knows how to apply |
| 2082 | * priorities to tasks. |
| 2083 | */ |
| 2084 | task_wakeup(socket->s->task, TASK_WOKEN_INIT); |
| 2085 | |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2086 | hlua = hlua_gethlua(L); |
| 2087 | appctx = objt_appctx(socket->s->si[0].end); |
| 2088 | if (!hlua_com_new(hlua, &appctx->ctx.hlua.wake_on_write)) |
| 2089 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 2090 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2091 | |
| 2092 | return 0; |
| 2093 | } |
| 2094 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2095 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2096 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2097 | { |
| 2098 | struct hlua_socket *socket; |
| 2099 | |
| 2100 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2101 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2102 | socket->s->target = &socket_ssl.obj_type; |
| 2103 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2104 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2105 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2106 | |
| 2107 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2108 | { |
| 2109 | return 0; |
| 2110 | } |
| 2111 | |
| 2112 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2113 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2114 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2115 | int tmout; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2116 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2117 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2118 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2119 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2120 | tmout = MAY_LJMP(luaL_checkinteger(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2121 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2122 | socket->s->req.rto = tmout; |
| 2123 | socket->s->req.wto = tmout; |
| 2124 | socket->s->res.rto = tmout; |
| 2125 | socket->s->res.wto = tmout; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2126 | |
| 2127 | return 0; |
| 2128 | } |
| 2129 | |
| 2130 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2131 | { |
| 2132 | struct hlua_socket *socket; |
| 2133 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2134 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2135 | struct stream *strm; |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2136 | struct task *task; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2137 | |
| 2138 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2139 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2140 | hlua_pusherror(L, "socket: full stack"); |
| 2141 | goto out_fail_conf; |
| 2142 | } |
| 2143 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2144 | /* Create the object: obj[0] = userdata. */ |
| 2145 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2146 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2147 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2148 | memset(socket, 0, sizeof(*socket)); |
| 2149 | |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2150 | /* Check if the various memory pools are intialized. */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2151 | if (!pool2_stream || !pool2_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2152 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2153 | goto out_fail_conf; |
| 2154 | } |
| 2155 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2156 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2157 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2158 | lua_setmetatable(L, -2); |
| 2159 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2160 | /* Create the applet context */ |
| 2161 | appctx = appctx_new(&update_applet); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2162 | if (!appctx) { |
| 2163 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2164 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2165 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2166 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2167 | appctx->ctx.hlua.socket = socket; |
| 2168 | appctx->ctx.hlua.connected = 0; |
| 2169 | LIST_INIT(&appctx->ctx.hlua.wake_on_write); |
| 2170 | LIST_INIT(&appctx->ctx.hlua.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2171 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2172 | /* Now create a session, task and stream for this applet */ |
| 2173 | sess = session_new(&socket_proxy, NULL, &appctx->obj_type); |
| 2174 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2175 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2176 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2177 | } |
| 2178 | |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2179 | task = task_new(); |
| 2180 | if (!task) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2181 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2182 | goto out_fail_task; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2183 | } |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2184 | task->nice = 0; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2185 | |
Willy Tarreau | 73b65ac | 2015-04-08 18:26:29 +0200 | [diff] [blame] | 2186 | strm = stream_new(sess, task, &appctx->obj_type); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2187 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2188 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2189 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2190 | } |
| 2191 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2192 | /* Configure an empty Lua for the stream. */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2193 | socket->s = strm; |
| 2194 | strm->hlua.T = NULL; |
| 2195 | strm->hlua.Tref = LUA_REFNIL; |
| 2196 | strm->hlua.Mref = LUA_REFNIL; |
| 2197 | strm->hlua.nargs = 0; |
| 2198 | strm->hlua.flags = 0; |
| 2199 | LIST_INIT(&strm->hlua.com); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2200 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2201 | /* Configure "right" stream interface. this "si" is used to connect |
| 2202 | * and retrieve data from the server. The connection is initialized |
| 2203 | * with the "struct server". |
| 2204 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2205 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2206 | |
| 2207 | /* Force destination server. */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2208 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET | SF_BE_ASSIGNED; |
| 2209 | strm->target = &socket_tcp.obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2210 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2211 | /* Update statistics counters. */ |
| 2212 | socket_proxy.feconn++; /* beconn will be increased later */ |
| 2213 | jobs++; |
| 2214 | totalconn++; |
| 2215 | |
| 2216 | /* Return yield waiting for connection. */ |
| 2217 | return 1; |
| 2218 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2219 | out_fail_stream: |
| 2220 | task_free(task); |
| 2221 | out_fail_task: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2222 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2223 | out_fail_sess: |
| 2224 | appctx_free(appctx); |
| 2225 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2226 | WILL_LJMP(lua_error(L)); |
| 2227 | return 0; |
| 2228 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2229 | |
| 2230 | /* |
| 2231 | * |
| 2232 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2233 | * Class Channel |
| 2234 | * |
| 2235 | * |
| 2236 | */ |
| 2237 | |
| 2238 | /* Returns the struct hlua_channel join to the class channel in the |
| 2239 | * stack entry "ud" or throws an argument error. |
| 2240 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2241 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2242 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2243 | return (struct channel *)MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2244 | } |
| 2245 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2246 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 2247 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2248 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 2249 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2250 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2251 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2252 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2253 | return 0; |
| 2254 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2255 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2256 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2257 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2258 | |
| 2259 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2260 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 2261 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2262 | return 1; |
| 2263 | } |
| 2264 | |
| 2265 | /* Duplicate all the data present in the input channel and put it |
| 2266 | * in a string LUA variables. Returns -1 and push a nil value in |
| 2267 | * the stack if the channel is closed and all the data are consumed, |
| 2268 | * returns 0 if no data are available, otherwise it returns the length |
| 2269 | * of the builded string. |
| 2270 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2271 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2272 | { |
| 2273 | char *blk1; |
| 2274 | char *blk2; |
| 2275 | int len1; |
| 2276 | int len2; |
| 2277 | int ret; |
| 2278 | luaL_Buffer b; |
| 2279 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2280 | ret = bi_getblk_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2281 | if (unlikely(ret == 0)) |
| 2282 | return 0; |
| 2283 | |
| 2284 | if (unlikely(ret < 0)) { |
| 2285 | lua_pushnil(L); |
| 2286 | return -1; |
| 2287 | } |
| 2288 | |
| 2289 | luaL_buffinit(L, &b); |
| 2290 | luaL_addlstring(&b, blk1, len1); |
| 2291 | if (unlikely(ret == 2)) |
| 2292 | luaL_addlstring(&b, blk2, len2); |
| 2293 | luaL_pushresult(&b); |
| 2294 | |
| 2295 | if (unlikely(ret == 2)) |
| 2296 | return len1 + len2; |
| 2297 | return len1; |
| 2298 | } |
| 2299 | |
| 2300 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2301 | * a yield. This function keep the data in the buffer. |
| 2302 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2303 | __LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2304 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2305 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2306 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2307 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2308 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2309 | if (_hlua_channel_dup(chn, L) == 0) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2310 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2311 | return 1; |
| 2312 | } |
| 2313 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2314 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 2315 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 2316 | { |
| 2317 | MAY_LJMP(check_args(L, 1, "dup")); |
| 2318 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2319 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 2320 | } |
| 2321 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2322 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2323 | * a yield. This function consumes the data in the buffer. It returns |
| 2324 | * a string containing the data or a nil pointer if no data are available |
| 2325 | * and the channel is closed. |
| 2326 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2327 | __LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2328 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2329 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2330 | int ret; |
| 2331 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2332 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2333 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2334 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2335 | if (unlikely(ret == 0)) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2336 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2337 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2338 | if (unlikely(ret == -1)) |
| 2339 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2340 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2341 | chn->buf->i -= ret; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2342 | return 1; |
| 2343 | } |
| 2344 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2345 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 2346 | __LJMP static int hlua_channel_get(lua_State *L) |
| 2347 | { |
| 2348 | MAY_LJMP(check_args(L, 1, "get")); |
| 2349 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2350 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 2351 | } |
| 2352 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2353 | /* This functions consumes and returns one line. If the channel is closed, |
| 2354 | * and the last data does not contains a final '\n', the data are returned |
| 2355 | * without the final '\n'. When no more data are avalaible, it returns nil |
| 2356 | * value. |
| 2357 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2358 | __LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2359 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2360 | char *blk1; |
| 2361 | char *blk2; |
| 2362 | int len1; |
| 2363 | int len2; |
| 2364 | int len; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2365 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2366 | int ret; |
| 2367 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2368 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2369 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2370 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2371 | ret = bi_getline_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2372 | if (ret == 0) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2373 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2374 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2375 | if (ret == -1) { |
| 2376 | lua_pushnil(L); |
| 2377 | return 1; |
| 2378 | } |
| 2379 | |
| 2380 | luaL_buffinit(L, &b); |
| 2381 | luaL_addlstring(&b, blk1, len1); |
| 2382 | len = len1; |
| 2383 | if (unlikely(ret == 2)) { |
| 2384 | luaL_addlstring(&b, blk2, len2); |
| 2385 | len += len2; |
| 2386 | } |
| 2387 | luaL_pushresult(&b); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2388 | buffer_replace2(chn->buf, chn->buf->p, chn->buf->p + len, NULL, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2389 | return 1; |
| 2390 | } |
| 2391 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2392 | /* Check arguments for the fucntion "hlua_channel_getline_yield". */ |
| 2393 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 2394 | { |
| 2395 | MAY_LJMP(check_args(L, 1, "getline")); |
| 2396 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2397 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 2398 | } |
| 2399 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2400 | /* This function takes a string as input, and append it at the |
| 2401 | * input side of channel. If the data is too big, but a space |
| 2402 | * is probably available after sending some data, the function |
| 2403 | * yield. If the data is bigger than the buffer, or if the |
| 2404 | * channel is closed, it returns -1. otherwise, it returns the |
| 2405 | * amount of data writed. |
| 2406 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2407 | __LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2408 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2409 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2410 | size_t len; |
| 2411 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2412 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2413 | int ret; |
| 2414 | int max; |
| 2415 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2416 | max = channel_recv_limit(chn) - buffer_len(chn->buf); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2417 | if (max > len - l) |
| 2418 | max = len - l; |
| 2419 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2420 | ret = bi_putblk(chn, str + l, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2421 | if (ret == -2 || ret == -3) { |
| 2422 | lua_pushinteger(L, -1); |
| 2423 | return 1; |
| 2424 | } |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2425 | if (ret == -1) { |
| 2426 | chn->flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2427 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0)); |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2428 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2429 | l += ret; |
| 2430 | lua_pop(L, 1); |
| 2431 | lua_pushinteger(L, l); |
| 2432 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2433 | max = channel_recv_limit(chn) - buffer_len(chn->buf); |
| 2434 | if (max == 0 && chn->buf->o == 0) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2435 | /* There are no space avalaible, and the output buffer is empty. |
| 2436 | * in this case, we cannot add more data, so we cannot yield, |
| 2437 | * we return the amount of copyied data. |
| 2438 | */ |
| 2439 | return 1; |
| 2440 | } |
| 2441 | if (l < len) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2442 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2443 | return 1; |
| 2444 | } |
| 2445 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2446 | /* just a wrapper of "hlua_channel_append_yield". It returns the length |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2447 | * of the writed string, or -1 if the channel is closed or if the |
| 2448 | * buffer size is too little for the data. |
| 2449 | */ |
| 2450 | __LJMP static int hlua_channel_append(lua_State *L) |
| 2451 | { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2452 | size_t len; |
| 2453 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2454 | MAY_LJMP(check_args(L, 2, "append")); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2455 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2456 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2457 | MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2458 | lua_pushinteger(L, 0); |
| 2459 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2460 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2461 | } |
| 2462 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2463 | /* just a wrapper of "hlua_channel_append_yield". This wrapper starts |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2464 | * his process by cleaning the buffer. The result is a replacement |
| 2465 | * of the current data. It returns the length of the writed string, |
| 2466 | * or -1 if the channel is closed or if the buffer size is too |
| 2467 | * little for the data. |
| 2468 | */ |
| 2469 | __LJMP static int hlua_channel_set(lua_State *L) |
| 2470 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2471 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2472 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2473 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2474 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2475 | lua_pushinteger(L, 0); |
| 2476 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2477 | chn->buf->i = 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2478 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2479 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2480 | } |
| 2481 | |
| 2482 | /* Append data in the output side of the buffer. This data is immediatly |
| 2483 | * sent. The fcuntion returns the ammount of data writed. If the buffer |
| 2484 | * cannot contains the data, the function yield. The function returns -1 |
| 2485 | * if the channel is closed. |
| 2486 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2487 | __LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2488 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2489 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2490 | size_t len; |
| 2491 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2492 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2493 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2494 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2495 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2496 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2497 | lua_pushinteger(L, -1); |
| 2498 | return 1; |
| 2499 | } |
| 2500 | |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 2501 | /* Check if the buffer is avalaible because HAProxy doesn't allocate |
| 2502 | * the request buffer if its not required. |
| 2503 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2504 | if (chn->buf->size == 0) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2505 | if (!stream_alloc_recv_buffer(chn)) { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2506 | chn_prod(chn)->flags |= SI_FL_WAIT_ROOM; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2507 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 2508 | } |
| 2509 | } |
| 2510 | |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2511 | /* the writed data will be immediatly sent, so we can check |
| 2512 | * the avalaible space without taking in account the reserve. |
| 2513 | * The reserve is guaranted for the processing of incoming |
| 2514 | * data, because the buffer will be flushed. |
| 2515 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2516 | max = chn->buf->size - buffer_len(chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2517 | |
| 2518 | /* If there are no space avalaible, and the output buffer is empty. |
| 2519 | * in this case, we cannot add more data, so we cannot yield, |
| 2520 | * we return the amount of copyied data. |
| 2521 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2522 | if (max == 0 && chn->buf->o == 0) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2523 | return 1; |
| 2524 | |
| 2525 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2526 | if (max > len - l) |
| 2527 | max = len - l; |
| 2528 | |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2529 | /* The buffer avalaible size may be not contiguous. This test |
| 2530 | * detects a non contiguous buffer and realign it. |
| 2531 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2532 | if (bi_space_for_replace(chn->buf) < max) |
| 2533 | buffer_slow_realign(chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2534 | |
| 2535 | /* Copy input data in the buffer. */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2536 | max = buffer_replace2(chn->buf, chn->buf->p, chn->buf->p, str + l, max); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2537 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2538 | /* buffer replace considers that the input part is filled. |
| 2539 | * so, I must forward these new data in the output part. |
| 2540 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2541 | b_adv(chn->buf, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2542 | |
| 2543 | l += max; |
| 2544 | lua_pop(L, 1); |
| 2545 | lua_pushinteger(L, l); |
| 2546 | |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2547 | /* If there are no space avalaible, and the output buffer is empty. |
| 2548 | * in this case, we cannot add more data, so we cannot yield, |
| 2549 | * we return the amount of copyied data. |
| 2550 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2551 | max = chn->buf->size - buffer_len(chn->buf); |
| 2552 | if (max == 0 && chn->buf->o == 0) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2553 | return 1; |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2554 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2555 | if (l < len) { |
| 2556 | /* If we are waiting for space in the response buffer, we |
| 2557 | * must set the flag WAKERESWR. This flag required the task |
| 2558 | * wake up if any activity is detected on the response buffer. |
| 2559 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2560 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2561 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 2562 | else |
| 2563 | HLUA_SET_WAKEREQWR(hlua); |
| 2564 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2565 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2566 | |
| 2567 | return 1; |
| 2568 | } |
| 2569 | |
| 2570 | /* Just a wraper of "_hlua_channel_send". This wrapper permits |
| 2571 | * yield the LUA process, and resume it without checking the |
| 2572 | * input arguments. |
| 2573 | */ |
| 2574 | __LJMP static int hlua_channel_send(lua_State *L) |
| 2575 | { |
| 2576 | MAY_LJMP(check_args(L, 2, "send")); |
| 2577 | lua_pushinteger(L, 0); |
| 2578 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2579 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2580 | } |
| 2581 | |
| 2582 | /* This function forward and amount of butes. The data pass from |
| 2583 | * the input side of the buffer to the output side, and can be |
| 2584 | * forwarded. This function never fails. |
| 2585 | * |
| 2586 | * The Lua function takes an amount of bytes to be forwarded in |
| 2587 | * imput. It returns the number of bytes forwarded. |
| 2588 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2589 | __LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2590 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2591 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2592 | int len; |
| 2593 | int l; |
| 2594 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2595 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2596 | |
| 2597 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2598 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 2599 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 2600 | |
| 2601 | max = len - l; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2602 | if (max > chn->buf->i) |
| 2603 | max = chn->buf->i; |
| 2604 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2605 | l += max; |
| 2606 | |
| 2607 | lua_pop(L, 1); |
| 2608 | lua_pushinteger(L, l); |
| 2609 | |
| 2610 | /* Check if it miss bytes to forward. */ |
| 2611 | if (l < len) { |
| 2612 | /* The the input channel or the output channel are closed, we |
| 2613 | * must return the amount of data forwarded. |
| 2614 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2615 | if (channel_input_closed(chn) || channel_output_closed(chn)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2616 | return 1; |
| 2617 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2618 | /* If we are waiting for space data in the response buffer, we |
| 2619 | * must set the flag WAKERESWR. This flag required the task |
| 2620 | * wake up if any activity is detected on the response buffer. |
| 2621 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2622 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2623 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 2624 | else |
| 2625 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2626 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2627 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 2628 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2629 | } |
| 2630 | |
| 2631 | return 1; |
| 2632 | } |
| 2633 | |
| 2634 | /* Just check the input and prepare the stack for the previous |
| 2635 | * function "hlua_channel_forward_yield" |
| 2636 | */ |
| 2637 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 2638 | { |
| 2639 | MAY_LJMP(check_args(L, 2, "forward")); |
| 2640 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2641 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 2642 | |
| 2643 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2644 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2645 | } |
| 2646 | |
| 2647 | /* Just returns the number of bytes available in the input |
| 2648 | * side of the buffer. This function never fails. |
| 2649 | */ |
| 2650 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 2651 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2652 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2653 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2654 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2655 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2656 | lua_pushinteger(L, chn->buf->i); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2657 | return 1; |
| 2658 | } |
| 2659 | |
| 2660 | /* Just returns the number of bytes available in the output |
| 2661 | * side of the buffer. This function never fails. |
| 2662 | */ |
| 2663 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 2664 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2665 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2666 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2667 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2668 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2669 | lua_pushinteger(L, chn->buf->o); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2670 | return 1; |
| 2671 | } |
| 2672 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2673 | /* |
| 2674 | * |
| 2675 | * |
| 2676 | * Class Fetches |
| 2677 | * |
| 2678 | * |
| 2679 | */ |
| 2680 | |
| 2681 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2682 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2683 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 2684 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2685 | { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 2686 | return (struct hlua_smp *)MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2687 | } |
| 2688 | |
| 2689 | /* This function creates and push in the stack a fetch object according |
| 2690 | * with a current TXN. |
| 2691 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 2692 | static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, int stringsafe) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2693 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 2694 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2695 | |
| 2696 | /* Check stack size. */ |
| 2697 | if (!lua_checkstack(L, 3)) |
| 2698 | return 0; |
| 2699 | |
| 2700 | /* Create the object: obj[0] = userdata. |
| 2701 | * Note that the base of the Fetches object is the |
| 2702 | * transaction object. |
| 2703 | */ |
| 2704 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 2705 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2706 | lua_rawseti(L, -2, 0); |
| 2707 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 2708 | hsmp->s = txn->s; |
| 2709 | hsmp->p = txn->p; |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 2710 | hsmp->stringsafe = stringsafe; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2711 | |
| 2712 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2713 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 2714 | lua_setmetatable(L, -2); |
| 2715 | |
| 2716 | return 1; |
| 2717 | } |
| 2718 | |
| 2719 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 2720 | * It uses closure argument to store the associated sample-fetch. It |
| 2721 | * returns only one argument or throws an error. An error is thrown |
| 2722 | * only if an error is encountered during the argument parsing. If |
| 2723 | * the "sample-fetch" function fails, nil is returned. |
| 2724 | */ |
| 2725 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 2726 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 2727 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 2728 | struct sample_fetch *f; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2729 | struct arg args[ARGM_NBARGS + 1]; |
| 2730 | int i; |
| 2731 | struct sample smp; |
| 2732 | |
| 2733 | /* Get closure arguments. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 2734 | f = (struct sample_fetch *)lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2735 | |
| 2736 | /* Get traditionnal arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 2737 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2738 | |
| 2739 | /* Get extra arguments. */ |
| 2740 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 2741 | if (i >= ARGM_NBARGS) |
| 2742 | break; |
| 2743 | hlua_lua2arg(L, i + 2, &args[i]); |
| 2744 | } |
| 2745 | args[i].type = ARGT_STOP; |
| 2746 | |
| 2747 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 2748 | MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2749 | |
| 2750 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 2751 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2752 | lua_pushfstring(L, "error in arguments"); |
| 2753 | WILL_LJMP(lua_error(L)); |
| 2754 | } |
| 2755 | |
| 2756 | /* Initialise the sample. */ |
| 2757 | memset(&smp, 0, sizeof(smp)); |
| 2758 | |
| 2759 | /* Run the sample fetch process. */ |
Thierry FOURNIER | 6879ad3 | 2015-05-11 11:54:58 +0200 | [diff] [blame] | 2760 | smp.px = hsmp->p; |
| 2761 | smp.sess = hsmp->s->sess; |
| 2762 | smp.strm = hsmp->s; |
Thierry FOURNIER | 1d33b88 | 2015-05-11 15:25:29 +0200 | [diff] [blame] | 2763 | smp.opt = 0; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 2764 | if (!f->process(args, &smp, f->kw, f->private)) { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 2765 | if (hsmp->stringsafe) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 2766 | lua_pushstring(L, ""); |
| 2767 | else |
| 2768 | lua_pushnil(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2769 | return 1; |
| 2770 | } |
| 2771 | |
| 2772 | /* Convert the returned sample in lua value. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 2773 | if (hsmp->stringsafe) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 2774 | hlua_smp2lua_str(L, &smp); |
| 2775 | else |
| 2776 | hlua_smp2lua(L, &smp); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2777 | return 1; |
| 2778 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2779 | |
| 2780 | /* |
| 2781 | * |
| 2782 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 2783 | * Class Converters |
| 2784 | * |
| 2785 | * |
| 2786 | */ |
| 2787 | |
| 2788 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2789 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 2790 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 2791 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 2792 | { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 2793 | return (struct hlua_smp *)MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 2794 | } |
| 2795 | |
| 2796 | /* This function creates and push in the stack a Converters object |
| 2797 | * according with a current TXN. |
| 2798 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 2799 | static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, int stringsafe) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 2800 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 2801 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 2802 | |
| 2803 | /* Check stack size. */ |
| 2804 | if (!lua_checkstack(L, 3)) |
| 2805 | return 0; |
| 2806 | |
| 2807 | /* Create the object: obj[0] = userdata. |
| 2808 | * Note that the base of the Converters object is the |
| 2809 | * same than the TXN object. |
| 2810 | */ |
| 2811 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 2812 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 2813 | lua_rawseti(L, -2, 0); |
| 2814 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 2815 | hsmp->s = txn->s; |
| 2816 | hsmp->p = txn->p; |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 2817 | hsmp->stringsafe = stringsafe; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 2818 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2819 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 2820 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 2821 | lua_setmetatable(L, -2); |
| 2822 | |
| 2823 | return 1; |
| 2824 | } |
| 2825 | |
| 2826 | /* This function is an LUA binding. It is called with each converter. |
| 2827 | * It uses closure argument to store the associated converter. It |
| 2828 | * returns only one argument or throws an error. An error is thrown |
| 2829 | * only if an error is encountered during the argument parsing. If |
| 2830 | * the converter function function fails, nil is returned. |
| 2831 | */ |
| 2832 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 2833 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 2834 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 2835 | struct sample_conv *conv; |
| 2836 | struct arg args[ARGM_NBARGS + 1]; |
| 2837 | int i; |
| 2838 | struct sample smp; |
| 2839 | |
| 2840 | /* Get closure arguments. */ |
| 2841 | conv = (struct sample_conv *)lua_touserdata(L, lua_upvalueindex(1)); |
| 2842 | |
| 2843 | /* Get traditionnal arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 2844 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 2845 | |
| 2846 | /* Get extra arguments. */ |
| 2847 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 2848 | if (i >= ARGM_NBARGS) |
| 2849 | break; |
| 2850 | hlua_lua2arg(L, i + 3, &args[i]); |
| 2851 | } |
| 2852 | args[i].type = ARGT_STOP; |
| 2853 | |
| 2854 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 2855 | MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 2856 | |
| 2857 | /* Run the special args checker. */ |
| 2858 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 2859 | hlua_pusherror(L, "error in arguments"); |
| 2860 | WILL_LJMP(lua_error(L)); |
| 2861 | } |
| 2862 | |
| 2863 | /* Initialise the sample. */ |
| 2864 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 2865 | hlua_pusherror(L, "error in the input argument"); |
| 2866 | WILL_LJMP(lua_error(L)); |
| 2867 | } |
| 2868 | |
| 2869 | /* Apply expected cast. */ |
| 2870 | if (!sample_casts[smp.type][conv->in_type]) { |
| 2871 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
| 2872 | smp_to_type[smp.type], smp_to_type[conv->in_type]); |
| 2873 | WILL_LJMP(lua_error(L)); |
| 2874 | } |
| 2875 | if (sample_casts[smp.type][conv->in_type] != c_none && |
| 2876 | !sample_casts[smp.type][conv->in_type](&smp)) { |
| 2877 | hlua_pusherror(L, "error during the input argument casting"); |
| 2878 | WILL_LJMP(lua_error(L)); |
| 2879 | } |
| 2880 | |
| 2881 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 6879ad3 | 2015-05-11 11:54:58 +0200 | [diff] [blame] | 2882 | smp.px = hsmp->p; |
| 2883 | smp.sess = hsmp->s->sess; |
| 2884 | smp.strm = hsmp->s; |
Thierry FOURNIER | 1d33b88 | 2015-05-11 15:25:29 +0200 | [diff] [blame] | 2885 | smp.opt = 0; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2886 | if (!conv->process(args, &smp, conv->private)) { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 2887 | if (hsmp->stringsafe) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 2888 | lua_pushstring(L, ""); |
| 2889 | else |
| 2890 | lua_pushnil(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 2891 | return 1; |
| 2892 | } |
| 2893 | |
| 2894 | /* Convert the returned sample in lua value. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 2895 | if (hsmp->stringsafe) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 2896 | hlua_smp2lua_str(L, &smp); |
| 2897 | else |
| 2898 | hlua_smp2lua(L, &smp); |
| 2899 | return 1; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 2900 | } |
| 2901 | |
| 2902 | /* |
| 2903 | * |
| 2904 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 2905 | * Class HTTP |
| 2906 | * |
| 2907 | * |
| 2908 | */ |
| 2909 | |
| 2910 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2911 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 2912 | */ |
| 2913 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
| 2914 | { |
| 2915 | return (struct hlua_txn *)MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
| 2916 | } |
| 2917 | |
| 2918 | /* This function creates and push in the stack a HTTP object |
| 2919 | * according with a current TXN. |
| 2920 | */ |
| 2921 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 2922 | { |
Willy Tarreau | 9a8ad86 | 2015-04-06 11:14:06 +0200 | [diff] [blame] | 2923 | struct hlua_txn *htxn; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 2924 | |
| 2925 | /* Check stack size. */ |
| 2926 | if (!lua_checkstack(L, 3)) |
| 2927 | return 0; |
| 2928 | |
| 2929 | /* Create the object: obj[0] = userdata. |
| 2930 | * Note that the base of the Converters object is the |
| 2931 | * same than the TXN object. |
| 2932 | */ |
| 2933 | lua_newtable(L); |
Willy Tarreau | 9a8ad86 | 2015-04-06 11:14:06 +0200 | [diff] [blame] | 2934 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 2935 | lua_rawseti(L, -2, 0); |
| 2936 | |
Willy Tarreau | 9a8ad86 | 2015-04-06 11:14:06 +0200 | [diff] [blame] | 2937 | htxn->s = txn->s; |
| 2938 | htxn->p = txn->p; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 2939 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2940 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 2941 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 2942 | lua_setmetatable(L, -2); |
| 2943 | |
| 2944 | return 1; |
| 2945 | } |
| 2946 | |
| 2947 | /* This function creates ans returns an array of HTTP headers. |
| 2948 | * This function does not fails. It is used as wrapper with the |
| 2949 | * 2 following functions. |
| 2950 | */ |
| 2951 | __LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 2952 | { |
| 2953 | const char *cur_ptr, *cur_next, *p; |
| 2954 | int old_idx, cur_idx; |
| 2955 | struct hdr_idx_elem *cur_hdr; |
| 2956 | const char *hn, *hv; |
| 2957 | int hnl, hvl; |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 2958 | int type; |
| 2959 | const char *in; |
| 2960 | char *out; |
| 2961 | int len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 2962 | |
| 2963 | /* Create the table. */ |
| 2964 | lua_newtable(L); |
| 2965 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 2966 | if (!htxn->s->txn) |
| 2967 | return 1; |
| 2968 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 2969 | /* Build array of headers. */ |
| 2970 | old_idx = 0; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 2971 | cur_next = msg->chn->buf->p + hdr_idx_first_pos(&htxn->s->txn->hdr_idx); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 2972 | |
| 2973 | while (1) { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 2974 | cur_idx = htxn->s->txn->hdr_idx.v[old_idx].next; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 2975 | if (!cur_idx) |
| 2976 | break; |
| 2977 | old_idx = cur_idx; |
| 2978 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 2979 | cur_hdr = &htxn->s->txn->hdr_idx.v[cur_idx]; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 2980 | cur_ptr = cur_next; |
| 2981 | cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1; |
| 2982 | |
| 2983 | /* Now we have one full header at cur_ptr of len cur_hdr->len, |
| 2984 | * and the next header starts at cur_next. We'll check |
| 2985 | * this header in the list as well as against the default |
| 2986 | * rule. |
| 2987 | */ |
| 2988 | |
| 2989 | /* look for ': *'. */ |
| 2990 | hn = cur_ptr; |
| 2991 | for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++); |
| 2992 | if (p >= cur_ptr+cur_hdr->len) |
| 2993 | continue; |
| 2994 | hnl = p - hn; |
| 2995 | p++; |
| 2996 | while (p < cur_ptr+cur_hdr->len && ( *p == ' ' || *p == '\t' )) |
| 2997 | p++; |
| 2998 | if (p >= cur_ptr+cur_hdr->len) |
| 2999 | continue; |
| 3000 | hv = p; |
| 3001 | hvl = cur_ptr+cur_hdr->len-p; |
| 3002 | |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3003 | /* Lowercase the key. Don't check the size of trash, it have |
| 3004 | * the size of one buffer and the input data contains in one |
| 3005 | * buffer. |
| 3006 | */ |
| 3007 | out = trash.str; |
| 3008 | for (in=hn; in<hn+hnl; in++, out++) |
| 3009 | *out = tolower(*in); |
| 3010 | *out = '\0'; |
| 3011 | |
| 3012 | /* Check for existing entry: |
| 3013 | * assume that the table is on the top of the stack, and |
| 3014 | * push the key in the stack, the function lua_gettable() |
| 3015 | * perform the lookup. |
| 3016 | */ |
| 3017 | lua_pushlstring(L, trash.str, hnl); |
| 3018 | lua_gettable(L, -2); |
| 3019 | type = lua_type(L, -1); |
| 3020 | |
| 3021 | switch (type) { |
| 3022 | case LUA_TNIL: |
| 3023 | /* Table not found, create it. */ |
| 3024 | lua_pop(L, 1); /* remove the nil value. */ |
| 3025 | lua_pushlstring(L, trash.str, hnl); /* push the header name as key. */ |
| 3026 | lua_newtable(L); /* create and push empty table. */ |
| 3027 | lua_pushlstring(L, hv, hvl); /* push header value. */ |
| 3028 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 3029 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
| 3030 | break; |
| 3031 | |
| 3032 | case LUA_TTABLE: |
| 3033 | /* Entry found: push the value in the table. */ |
| 3034 | len = lua_rawlen(L, -1); |
| 3035 | lua_pushlstring(L, hv, hvl); /* push header value. */ |
| 3036 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 3037 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 3038 | break; |
| 3039 | |
| 3040 | default: |
| 3041 | /* Other cases are errors. */ |
| 3042 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 3043 | WILL_LJMP(lua_error(L)); |
| 3044 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3045 | } |
| 3046 | |
| 3047 | return 1; |
| 3048 | } |
| 3049 | |
| 3050 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 3051 | { |
| 3052 | struct hlua_txn *htxn; |
| 3053 | |
| 3054 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 3055 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 3056 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3057 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3058 | } |
| 3059 | |
| 3060 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 3061 | { |
| 3062 | struct hlua_txn *htxn; |
| 3063 | |
| 3064 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 3065 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 3066 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3067 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3068 | } |
| 3069 | |
| 3070 | /* This function replace full header, or just a value in |
| 3071 | * the request or in the response. It is a wrapper fir the |
| 3072 | * 4 following functions. |
| 3073 | */ |
| 3074 | __LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn, |
| 3075 | struct http_msg *msg, int action) |
| 3076 | { |
| 3077 | size_t name_len; |
| 3078 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 3079 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 3080 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
| 3081 | struct my_regex re; |
| 3082 | |
| 3083 | if (!regex_comp(reg, &re, 1, 1, NULL)) |
| 3084 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 3085 | |
| 3086 | http_transform_header_str(htxn->s, msg, name, name_len, value, &re, action); |
| 3087 | regex_free(&re); |
| 3088 | return 0; |
| 3089 | } |
| 3090 | |
| 3091 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 3092 | { |
| 3093 | struct hlua_txn *htxn; |
| 3094 | |
| 3095 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 3096 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 3097 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3098 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, HTTP_REQ_ACT_REPLACE_HDR)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3099 | } |
| 3100 | |
| 3101 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 3102 | { |
| 3103 | struct hlua_txn *htxn; |
| 3104 | |
| 3105 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 3106 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 3107 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3108 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, HTTP_RES_ACT_REPLACE_HDR)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3109 | } |
| 3110 | |
| 3111 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 3112 | { |
| 3113 | struct hlua_txn *htxn; |
| 3114 | |
| 3115 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 3116 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 3117 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3118 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, HTTP_REQ_ACT_REPLACE_VAL)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3119 | } |
| 3120 | |
| 3121 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 3122 | { |
| 3123 | struct hlua_txn *htxn; |
| 3124 | |
| 3125 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 3126 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 3127 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3128 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, HTTP_RES_ACT_REPLACE_VAL)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3129 | } |
| 3130 | |
| 3131 | /* This function deletes all the occurences of an header. |
| 3132 | * It is a wrapper for the 2 following functions. |
| 3133 | */ |
| 3134 | __LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 3135 | { |
| 3136 | size_t len; |
| 3137 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3138 | struct hdr_ctx ctx; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3139 | struct http_txn *txn = htxn->s->txn; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3140 | |
| 3141 | ctx.idx = 0; |
| 3142 | while (http_find_header2(name, len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) |
| 3143 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 3144 | return 0; |
| 3145 | } |
| 3146 | |
| 3147 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 3148 | { |
| 3149 | struct hlua_txn *htxn; |
| 3150 | |
| 3151 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 3152 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 3153 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3154 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3155 | } |
| 3156 | |
| 3157 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 3158 | { |
| 3159 | struct hlua_txn *htxn; |
| 3160 | |
| 3161 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 3162 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 3163 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3164 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3165 | } |
| 3166 | |
| 3167 | /* This function adds an header. It is a wrapper used by |
| 3168 | * the 2 following functions. |
| 3169 | */ |
| 3170 | __LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 3171 | { |
| 3172 | size_t name_len; |
| 3173 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 3174 | size_t value_len; |
| 3175 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
| 3176 | char *p; |
| 3177 | |
| 3178 | /* Check length. */ |
| 3179 | trash.len = value_len + name_len + 2; |
| 3180 | if (trash.len > trash.size) |
| 3181 | return 0; |
| 3182 | |
| 3183 | /* Creates the header string. */ |
| 3184 | p = trash.str; |
| 3185 | memcpy(p, name, name_len); |
| 3186 | p += name_len; |
| 3187 | *p = ':'; |
| 3188 | p++; |
| 3189 | *p = ' '; |
| 3190 | p++; |
| 3191 | memcpy(p, value, value_len); |
| 3192 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3193 | lua_pushboolean(L, http_header_add_tail2(msg, &htxn->s->txn->hdr_idx, |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3194 | trash.str, trash.len) != 0); |
| 3195 | |
| 3196 | return 0; |
| 3197 | } |
| 3198 | |
| 3199 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 3200 | { |
| 3201 | struct hlua_txn *htxn; |
| 3202 | |
| 3203 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 3204 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 3205 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3206 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3207 | } |
| 3208 | |
| 3209 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 3210 | { |
| 3211 | struct hlua_txn *htxn; |
| 3212 | |
| 3213 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 3214 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 3215 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3216 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3217 | } |
| 3218 | |
| 3219 | static int hlua_http_req_set_hdr(lua_State *L) |
| 3220 | { |
| 3221 | struct hlua_txn *htxn; |
| 3222 | |
| 3223 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 3224 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 3225 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3226 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
| 3227 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3228 | } |
| 3229 | |
| 3230 | static int hlua_http_res_set_hdr(lua_State *L) |
| 3231 | { |
| 3232 | struct hlua_txn *htxn; |
| 3233 | |
| 3234 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 3235 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 3236 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3237 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
| 3238 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3239 | } |
| 3240 | |
| 3241 | /* This function set the method. */ |
| 3242 | static int hlua_http_req_set_meth(lua_State *L) |
| 3243 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 3244 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3245 | size_t name_len; |
| 3246 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3247 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3248 | lua_pushboolean(L, http_replace_req_line(0, name, name_len, htxn->p, htxn->s) != -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3249 | return 1; |
| 3250 | } |
| 3251 | |
| 3252 | /* This function set the method. */ |
| 3253 | static int hlua_http_req_set_path(lua_State *L) |
| 3254 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 3255 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3256 | size_t name_len; |
| 3257 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3258 | lua_pushboolean(L, http_replace_req_line(1, name, name_len, htxn->p, htxn->s) != -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3259 | return 1; |
| 3260 | } |
| 3261 | |
| 3262 | /* This function set the query-string. */ |
| 3263 | static int hlua_http_req_set_query(lua_State *L) |
| 3264 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 3265 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3266 | size_t name_len; |
| 3267 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3268 | |
| 3269 | /* Check length. */ |
| 3270 | if (name_len > trash.size - 1) { |
| 3271 | lua_pushboolean(L, 0); |
| 3272 | return 1; |
| 3273 | } |
| 3274 | |
| 3275 | /* Add the mark question as prefix. */ |
| 3276 | chunk_reset(&trash); |
| 3277 | trash.str[trash.len++] = '?'; |
| 3278 | memcpy(trash.str + trash.len, name, name_len); |
| 3279 | trash.len += name_len; |
| 3280 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3281 | lua_pushboolean(L, http_replace_req_line(2, trash.str, trash.len, htxn->p, htxn->s) != -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3282 | return 1; |
| 3283 | } |
| 3284 | |
| 3285 | /* This function set the uri. */ |
| 3286 | static int hlua_http_req_set_uri(lua_State *L) |
| 3287 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 3288 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3289 | size_t name_len; |
| 3290 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3291 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 3292 | lua_pushboolean(L, http_replace_req_line(3, name, name_len, htxn->p, htxn->s) != -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3293 | return 1; |
| 3294 | } |
| 3295 | |
| 3296 | /* |
| 3297 | * |
| 3298 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 3299 | * Class TXN |
| 3300 | * |
| 3301 | * |
| 3302 | */ |
| 3303 | |
| 3304 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3305 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 3306 | */ |
| 3307 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 3308 | { |
| 3309 | return (struct hlua_txn *)MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
| 3310 | } |
| 3311 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 3312 | __LJMP static int hlua_set_var(lua_State *L) |
| 3313 | { |
| 3314 | struct hlua_txn *htxn; |
| 3315 | const char *name; |
| 3316 | size_t len; |
| 3317 | struct sample smp; |
| 3318 | |
| 3319 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 3320 | |
| 3321 | /* It is useles to retrieve the stream, but this function |
| 3322 | * runs only in a stream context. |
| 3323 | */ |
| 3324 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 3325 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3326 | |
| 3327 | /* Converts the third argument in a sample. */ |
| 3328 | hlua_lua2smp(L, 3, &smp); |
| 3329 | |
| 3330 | /* Store the sample in a variable. */ |
| 3331 | vars_set_by_name(name, len, htxn->s, &smp); |
| 3332 | return 0; |
| 3333 | } |
| 3334 | |
| 3335 | __LJMP static int hlua_get_var(lua_State *L) |
| 3336 | { |
| 3337 | struct hlua_txn *htxn; |
| 3338 | const char *name; |
| 3339 | size_t len; |
| 3340 | struct sample smp; |
| 3341 | |
| 3342 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3343 | |
| 3344 | /* It is useles to retrieve the stream, but this function |
| 3345 | * runs only in a stream context. |
| 3346 | */ |
| 3347 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 3348 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3349 | |
| 3350 | if (!vars_get_by_name(name, len, htxn->s, &smp)) { |
| 3351 | lua_pushnil(L); |
| 3352 | return 1; |
| 3353 | } |
| 3354 | |
| 3355 | return hlua_smp2lua(L, &smp); |
| 3356 | } |
| 3357 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 3358 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 3359 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3360 | struct hlua *hlua; |
| 3361 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 3362 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3363 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3364 | /* It is useles to retrieve the stream, but this function |
| 3365 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 3366 | */ |
| 3367 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3368 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 3369 | |
| 3370 | /* Remove previous value. */ |
| 3371 | if (hlua->Mref != -1) |
| 3372 | luaL_unref(L, hlua->Mref, LUA_REGISTRYINDEX); |
| 3373 | |
| 3374 | /* Get and store new value. */ |
| 3375 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3376 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3377 | |
| 3378 | return 0; |
| 3379 | } |
| 3380 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 3381 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 3382 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3383 | struct hlua *hlua; |
| 3384 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 3385 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 3386 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3387 | /* It is useles to retrieve the stream, but this function |
| 3388 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 3389 | */ |
| 3390 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3391 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 3392 | |
| 3393 | /* Push configuration index in the stack. */ |
| 3394 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3395 | |
| 3396 | return 1; |
| 3397 | } |
| 3398 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 3399 | /* Create stack entry containing a class TXN. This function |
| 3400 | * return 0 if the stack does not contains free slots, |
| 3401 | * otherwise it returns 1. |
| 3402 | */ |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 3403 | static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 3404 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 3405 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 3406 | |
| 3407 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3408 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 3409 | return 0; |
| 3410 | |
| 3411 | /* NOTE: The allocation never fails. The failure |
| 3412 | * throw an error, and the function never returns. |
| 3413 | * if the throw is not avalaible, the process is aborted. |
| 3414 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3415 | /* Create the object: obj[0] = userdata. */ |
| 3416 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 3417 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3418 | lua_rawseti(L, -2, 0); |
| 3419 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 3420 | htxn->s = s; |
| 3421 | htxn->p = p; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 3422 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3423 | /* Create the "f" field that contains a list of fetches. */ |
| 3424 | lua_pushstring(L, "f"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 3425 | if (!hlua_fetches_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3426 | return 0; |
| 3427 | lua_settable(L, -3); |
| 3428 | |
| 3429 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3430 | lua_pushstring(L, "sf"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 3431 | if (!hlua_fetches_new(L, htxn, 1)) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3432 | return 0; |
| 3433 | lua_settable(L, -3); |
| 3434 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3435 | /* Create the "c" field that contains a list of converters. */ |
| 3436 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 3437 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3438 | return 0; |
| 3439 | lua_settable(L, -3); |
| 3440 | |
| 3441 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3442 | lua_pushstring(L, "sc"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 3443 | if (!hlua_converters_new(L, htxn, 1)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3444 | return 0; |
| 3445 | lua_settable(L, -3); |
| 3446 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 3447 | /* Create the "req" field that contains the request channel object. */ |
| 3448 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 3449 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 3450 | return 0; |
| 3451 | lua_settable(L, -3); |
| 3452 | |
| 3453 | /* Create the "res" field that contains the response channel object. */ |
| 3454 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 3455 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 3456 | return 0; |
| 3457 | lua_settable(L, -3); |
| 3458 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3459 | /* Creates the HTTP object is the current proxy allows http. */ |
| 3460 | lua_pushstring(L, "http"); |
| 3461 | if (p->mode == PR_MODE_HTTP) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 3462 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3463 | return 0; |
| 3464 | } |
| 3465 | else |
| 3466 | lua_pushnil(L); |
| 3467 | lua_settable(L, -3); |
| 3468 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 3469 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3470 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 3471 | lua_setmetatable(L, -2); |
| 3472 | |
| 3473 | return 1; |
| 3474 | } |
| 3475 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 3476 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 3477 | { |
| 3478 | const char *msg; |
| 3479 | struct hlua_txn *htxn; |
| 3480 | |
| 3481 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 3482 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 3483 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 3484 | |
| 3485 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 3486 | return 0; |
| 3487 | } |
| 3488 | |
| 3489 | __LJMP static int hlua_txn_log(lua_State *L) |
| 3490 | { |
| 3491 | int level; |
| 3492 | const char *msg; |
| 3493 | struct hlua_txn *htxn; |
| 3494 | |
| 3495 | MAY_LJMP(check_args(L, 3, "log")); |
| 3496 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 3497 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3498 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 3499 | |
| 3500 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 3501 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 3502 | |
| 3503 | hlua_sendlog(htxn->s->be, level, msg); |
| 3504 | return 0; |
| 3505 | } |
| 3506 | |
| 3507 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 3508 | { |
| 3509 | const char *msg; |
| 3510 | struct hlua_txn *htxn; |
| 3511 | |
| 3512 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 3513 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 3514 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 3515 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 3516 | return 0; |
| 3517 | } |
| 3518 | |
| 3519 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 3520 | { |
| 3521 | const char *msg; |
| 3522 | struct hlua_txn *htxn; |
| 3523 | |
| 3524 | MAY_LJMP(check_args(L, 2, "Info")); |
| 3525 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 3526 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 3527 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 3528 | return 0; |
| 3529 | } |
| 3530 | |
| 3531 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 3532 | { |
| 3533 | const char *msg; |
| 3534 | struct hlua_txn *htxn; |
| 3535 | |
| 3536 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 3537 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 3538 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 3539 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 3540 | return 0; |
| 3541 | } |
| 3542 | |
| 3543 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 3544 | { |
| 3545 | const char *msg; |
| 3546 | struct hlua_txn *htxn; |
| 3547 | |
| 3548 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 3549 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 3550 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 3551 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 3552 | return 0; |
| 3553 | } |
| 3554 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 3555 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 3556 | { |
| 3557 | struct hlua_txn *htxn; |
| 3558 | int ll; |
| 3559 | |
| 3560 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 3561 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 3562 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3563 | |
| 3564 | if (ll < 0 || ll > 7) |
| 3565 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 3566 | |
| 3567 | htxn->s->logs.level = ll; |
| 3568 | return 0; |
| 3569 | } |
| 3570 | |
| 3571 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 3572 | { |
| 3573 | struct hlua_txn *htxn; |
| 3574 | struct connection *cli_conn; |
| 3575 | int tos; |
| 3576 | |
| 3577 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 3578 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 3579 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3580 | |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3581 | if ((cli_conn = objt_conn(htxn->s->sess->origin)) && conn_ctrl_ready(cli_conn)) |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 3582 | inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, tos); |
| 3583 | |
| 3584 | return 0; |
| 3585 | } |
| 3586 | |
| 3587 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 3588 | { |
| 3589 | #ifdef SO_MARK |
| 3590 | struct hlua_txn *htxn; |
| 3591 | struct connection *cli_conn; |
| 3592 | int mark; |
| 3593 | |
| 3594 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 3595 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 3596 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3597 | |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 3598 | if ((cli_conn = objt_conn(htxn->s->sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | 07081fe | 2015-04-06 10:59:20 +0200 | [diff] [blame] | 3599 | setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 3600 | #endif |
| 3601 | return 0; |
| 3602 | } |
| 3603 | |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 3604 | /* This function is an Lua binding that send pending data |
| 3605 | * to the client, and close the stream interface. |
| 3606 | */ |
| 3607 | __LJMP static int hlua_txn_close(lua_State *L) |
| 3608 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3609 | struct hlua_txn *htxn; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 3610 | struct channel *ic, *oc; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 3611 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3612 | MAY_LJMP(check_args(L, 1, "close")); |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3613 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 3614 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3615 | ic = &htxn->s->req; |
| 3616 | oc = &htxn->s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 3617 | |
| 3618 | channel_abort(ic); |
| 3619 | channel_auto_close(ic); |
| 3620 | channel_erase(ic); |
| 3621 | channel_auto_read(oc); |
| 3622 | channel_auto_close(oc); |
| 3623 | channel_shutr_now(oc); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 3624 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 3625 | return 0; |
| 3626 | } |
| 3627 | |
| 3628 | __LJMP static int hlua_log(lua_State *L) |
| 3629 | { |
| 3630 | int level; |
| 3631 | const char *msg; |
| 3632 | |
| 3633 | MAY_LJMP(check_args(L, 2, "log")); |
| 3634 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 3635 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 3636 | |
| 3637 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 3638 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 3639 | |
| 3640 | hlua_sendlog(NULL, level, msg); |
| 3641 | return 0; |
| 3642 | } |
| 3643 | |
| 3644 | __LJMP static int hlua_log_debug(lua_State *L) |
| 3645 | { |
| 3646 | const char *msg; |
| 3647 | |
| 3648 | MAY_LJMP(check_args(L, 1, "debug")); |
| 3649 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 3650 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 3651 | return 0; |
| 3652 | } |
| 3653 | |
| 3654 | __LJMP static int hlua_log_info(lua_State *L) |
| 3655 | { |
| 3656 | const char *msg; |
| 3657 | |
| 3658 | MAY_LJMP(check_args(L, 1, "info")); |
| 3659 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 3660 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 3661 | return 0; |
| 3662 | } |
| 3663 | |
| 3664 | __LJMP static int hlua_log_warning(lua_State *L) |
| 3665 | { |
| 3666 | const char *msg; |
| 3667 | |
| 3668 | MAY_LJMP(check_args(L, 1, "warning")); |
| 3669 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 3670 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 3671 | return 0; |
| 3672 | } |
| 3673 | |
| 3674 | __LJMP static int hlua_log_alert(lua_State *L) |
| 3675 | { |
| 3676 | const char *msg; |
| 3677 | |
| 3678 | MAY_LJMP(check_args(L, 1, "alert")); |
| 3679 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 3680 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 3681 | return 0; |
| 3682 | } |
| 3683 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3684 | __LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 3685 | { |
| 3686 | int wakeup_ms = lua_tointeger(L, -1); |
| 3687 | if (now_ms < wakeup_ms) |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 3688 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0)); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 3689 | return 0; |
| 3690 | } |
| 3691 | |
| 3692 | __LJMP static int hlua_sleep(lua_State *L) |
| 3693 | { |
| 3694 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 3695 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 3696 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 3697 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 3698 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3699 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 3700 | wakeup_ms = tick_add(now_ms, delay); |
| 3701 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 3702 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 3703 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0)); |
| 3704 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 3705 | } |
| 3706 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 3707 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 3708 | { |
| 3709 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 3710 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 3711 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 3712 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 3713 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3714 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 3715 | wakeup_ms = tick_add(now_ms, delay); |
| 3716 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 3717 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 3718 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0)); |
| 3719 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 3720 | } |
| 3721 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 3722 | /* This functionis an LUA binding. it permits to give back |
| 3723 | * the hand at the HAProxy scheduler. It is used when the |
| 3724 | * LUA processing consumes a lot of time. |
| 3725 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3726 | __LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 3727 | { |
| 3728 | return 0; |
| 3729 | } |
| 3730 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 3731 | __LJMP static int hlua_yield(lua_State *L) |
| 3732 | { |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 3733 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD)); |
| 3734 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 3735 | } |
| 3736 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 3737 | /* This function change the nice of the currently executed |
| 3738 | * task. It is used set low or high priority at the current |
| 3739 | * task. |
| 3740 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 3741 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 3742 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3743 | struct hlua *hlua; |
| 3744 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 3745 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3746 | MAY_LJMP(check_args(L, 1, "set_nice")); |
| 3747 | hlua = hlua_gethlua(L); |
| 3748 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 3749 | |
| 3750 | /* If he task is not set, I'm in a start mode. */ |
| 3751 | if (!hlua || !hlua->task) |
| 3752 | return 0; |
| 3753 | |
| 3754 | if (nice < -1024) |
| 3755 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3756 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 3757 | nice = 1024; |
| 3758 | |
| 3759 | hlua->task->nice = nice; |
| 3760 | return 0; |
| 3761 | } |
| 3762 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 3763 | /* This function is used as a calback of a task. It is called by the |
| 3764 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 3765 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 3766 | * signal to wake the task. |
| 3767 | */ |
| 3768 | static struct task *hlua_process_task(struct task *task) |
| 3769 | { |
| 3770 | struct hlua *hlua = task->context; |
| 3771 | enum hlua_exec status; |
| 3772 | |
| 3773 | /* We need to remove the task from the wait queue before executing |
| 3774 | * the Lua code because we don't know if it needs to wait for |
| 3775 | * another timer or not in the case of E_AGAIN. |
| 3776 | */ |
| 3777 | task_delete(task); |
| 3778 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 3779 | /* If it is the first call to the task, we must initialize the |
| 3780 | * execution timeouts. |
| 3781 | */ |
| 3782 | if (!HLUA_IS_RUNNING(hlua)) |
| 3783 | hlua->expire = tick_add(now_ms, hlua_timeout_task); |
| 3784 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 3785 | /* Execute the Lua code. */ |
| 3786 | status = hlua_ctx_resume(hlua, 1); |
| 3787 | |
| 3788 | switch (status) { |
| 3789 | /* finished or yield */ |
| 3790 | case HLUA_E_OK: |
| 3791 | hlua_ctx_destroy(hlua); |
| 3792 | task_delete(task); |
| 3793 | task_free(task); |
| 3794 | break; |
| 3795 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 3796 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
| 3797 | if (hlua->wake_time != TICK_ETERNITY) |
| 3798 | task_schedule(task, hlua->wake_time); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 3799 | break; |
| 3800 | |
| 3801 | /* finished with error. */ |
| 3802 | case HLUA_E_ERRMSG: |
| 3803 | send_log(NULL, LOG_ERR, "Lua task: %s.", lua_tostring(hlua->T, -1)); |
| 3804 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) |
| 3805 | Alert("Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
| 3806 | hlua_ctx_destroy(hlua); |
| 3807 | task_delete(task); |
| 3808 | task_free(task); |
| 3809 | break; |
| 3810 | |
| 3811 | case HLUA_E_ERR: |
| 3812 | default: |
| 3813 | send_log(NULL, LOG_ERR, "Lua task: unknown error."); |
| 3814 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) |
| 3815 | Alert("Lua task: unknown error.\n"); |
| 3816 | hlua_ctx_destroy(hlua); |
| 3817 | task_delete(task); |
| 3818 | task_free(task); |
| 3819 | break; |
| 3820 | } |
| 3821 | return NULL; |
| 3822 | } |
| 3823 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 3824 | /* This function is an LUA binding that register LUA function to be |
| 3825 | * executed after the HAProxy configuration parsing and before the |
| 3826 | * HAProxy scheduler starts. This function expect only one LUA |
| 3827 | * argument that is a function. This function returns nothing, but |
| 3828 | * throws if an error is encountered. |
| 3829 | */ |
| 3830 | __LJMP static int hlua_register_init(lua_State *L) |
| 3831 | { |
| 3832 | struct hlua_init_function *init; |
| 3833 | int ref; |
| 3834 | |
| 3835 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 3836 | |
| 3837 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 3838 | |
| 3839 | init = malloc(sizeof(*init)); |
| 3840 | if (!init) |
| 3841 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 3842 | |
| 3843 | init->function_ref = ref; |
| 3844 | LIST_ADDQ(&hlua_init_functions, &init->l); |
| 3845 | return 0; |
| 3846 | } |
| 3847 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 3848 | /* This functio is an LUA binding. It permits to register a task |
| 3849 | * executed in parallel of the main HAroxy activity. The task is |
| 3850 | * created and it is set in the HAProxy scheduler. It can be called |
| 3851 | * from the "init" section, "post init" or during the runtime. |
| 3852 | * |
| 3853 | * Lua prototype: |
| 3854 | * |
| 3855 | * <none> core.register_task(<function>) |
| 3856 | */ |
| 3857 | static int hlua_register_task(lua_State *L) |
| 3858 | { |
| 3859 | struct hlua *hlua; |
| 3860 | struct task *task; |
| 3861 | int ref; |
| 3862 | |
| 3863 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 3864 | |
| 3865 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 3866 | |
| 3867 | hlua = malloc(sizeof(*hlua)); |
| 3868 | if (!hlua) |
| 3869 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 3870 | |
| 3871 | task = task_new(); |
| 3872 | task->context = hlua; |
| 3873 | task->process = hlua_process_task; |
| 3874 | |
| 3875 | if (!hlua_ctx_init(hlua, task)) |
| 3876 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 3877 | |
| 3878 | /* Restore the function in the stack. */ |
| 3879 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 3880 | hlua->nargs = 0; |
| 3881 | |
| 3882 | /* Schedule task. */ |
| 3883 | task_schedule(task, now_ms); |
| 3884 | |
| 3885 | return 0; |
| 3886 | } |
| 3887 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 3888 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 3889 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 3890 | * resume converters. |
| 3891 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3892 | static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private) |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 3893 | { |
| 3894 | struct hlua_function *fcn = (struct hlua_function *)private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3895 | struct stream *stream = smp->strm; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 3896 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3897 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 3898 | * Lua context can be not initialized. This behavior |
| 3899 | * permits to save performances because a systematic |
| 3900 | * Lua initialization cause 5% performances loss. |
| 3901 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3902 | if (!stream->hlua.T && !hlua_ctx_init(&stream->hlua, stream->task)) { |
| 3903 | send_log(stream->be, LOG_ERR, "Lua converter '%s': can't initialize Lua context.", fcn->name); |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 3904 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) |
| 3905 | Alert("Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 3906 | return 0; |
| 3907 | } |
| 3908 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 3909 | /* If it is the first run, initialize the data for the call. */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3910 | if (!HLUA_IS_RUNNING(&stream->hlua)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 3911 | /* Check stack available size. */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3912 | if (!lua_checkstack(stream->hlua.T, 1)) { |
| 3913 | send_log(stream->be, LOG_ERR, "Lua converter '%s': full stack.", fcn->name); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 3914 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) |
| 3915 | Alert("Lua converter '%s': full stack.\n", fcn->name); |
| 3916 | return 0; |
| 3917 | } |
| 3918 | |
| 3919 | /* Restore the function in the stack. */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3920 | lua_rawgeti(stream->hlua.T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 3921 | |
| 3922 | /* convert input sample and pust-it in the stack. */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3923 | if (!lua_checkstack(stream->hlua.T, 1)) { |
| 3924 | send_log(stream->be, LOG_ERR, "Lua converter '%s': full stack.", fcn->name); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 3925 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) |
| 3926 | Alert("Lua converter '%s': full stack.\n", fcn->name); |
| 3927 | return 0; |
| 3928 | } |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3929 | hlua_smp2lua(stream->hlua.T, smp); |
| 3930 | stream->hlua.nargs = 2; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 3931 | |
| 3932 | /* push keywords in the stack. */ |
| 3933 | if (arg_p) { |
| 3934 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3935 | if (!lua_checkstack(stream->hlua.T, 1)) { |
| 3936 | send_log(stream->be, LOG_ERR, "Lua converter '%s': full stack.", fcn->name); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 3937 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) |
| 3938 | Alert("Lua converter '%s': full stack.\n", fcn->name); |
| 3939 | return 0; |
| 3940 | } |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3941 | hlua_arg2lua(stream->hlua.T, arg_p); |
| 3942 | stream->hlua.nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 3943 | } |
| 3944 | } |
| 3945 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 3946 | /* We must initialize the execution timeouts. */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3947 | stream->hlua.expire = tick_add(now_ms, hlua_timeout_session); |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 3948 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 3949 | /* Set the currently running flag. */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3950 | HLUA_SET_RUN(&stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 3951 | } |
| 3952 | |
| 3953 | /* Execute the function. */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3954 | switch (hlua_ctx_resume(&stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 3955 | /* finished. */ |
| 3956 | case HLUA_E_OK: |
| 3957 | /* Convert the returned value in sample. */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3958 | hlua_lua2smp(stream->hlua.T, -1, smp); |
| 3959 | lua_pop(stream->hlua.T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 3960 | return 1; |
| 3961 | |
| 3962 | /* yield. */ |
| 3963 | case HLUA_E_AGAIN: |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3964 | send_log(stream->be, LOG_ERR, "Lua converter '%s': cannot use yielded functions.", fcn->name); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 3965 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) |
| 3966 | Alert("Lua converter '%s': cannot use yielded functions.\n", fcn->name); |
| 3967 | return 0; |
| 3968 | |
| 3969 | /* finished with error. */ |
| 3970 | case HLUA_E_ERRMSG: |
| 3971 | /* Display log. */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3972 | send_log(stream->be, LOG_ERR, "Lua converter '%s': %s.", fcn->name, lua_tostring(stream->hlua.T, -1)); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 3973 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3974 | Alert("Lua converter '%s': %s.\n", fcn->name, lua_tostring(stream->hlua.T, -1)); |
| 3975 | lua_pop(stream->hlua.T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 3976 | return 0; |
| 3977 | |
| 3978 | case HLUA_E_ERR: |
| 3979 | /* Display log. */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3980 | send_log(stream->be, LOG_ERR, "Lua converter '%s' returns an unknown error.", fcn->name); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 3981 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) |
| 3982 | Alert("Lua converter '%s' returns an unknown error.\n", fcn->name); |
| 3983 | |
| 3984 | default: |
| 3985 | return 0; |
| 3986 | } |
| 3987 | } |
| 3988 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 3989 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 3990 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 3991 | * resume sample-fetches. |
| 3992 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 3993 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 3994 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 3995 | { |
| 3996 | struct hlua_function *fcn = (struct hlua_function *)private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3997 | struct stream *stream = smp->strm; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 3998 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3999 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 4000 | * Lua context can be not initialized. This behavior |
| 4001 | * permits to save performances because a systematic |
| 4002 | * Lua initialization cause 5% performances loss. |
| 4003 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4004 | if (!stream->hlua.T && !hlua_ctx_init(&stream->hlua, stream->task)) { |
| 4005 | send_log(stream->be, LOG_ERR, "Lua sample-fetch '%s': can't initialize Lua context.", fcn->name); |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 4006 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) |
| 4007 | Alert("Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 4008 | return 0; |
| 4009 | } |
| 4010 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 4011 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4012 | if (!HLUA_IS_RUNNING(&stream->hlua)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 4013 | /* Check stack available size. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4014 | if (!lua_checkstack(stream->hlua.T, 2)) { |
| 4015 | send_log(smp->px, LOG_ERR, "Lua sample-fetch '%s': full stack.", fcn->name); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 4016 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) |
| 4017 | Alert("Lua sample-fetch '%s': full stack.\n", fcn->name); |
| 4018 | return 0; |
| 4019 | } |
| 4020 | |
| 4021 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4022 | lua_rawgeti(stream->hlua.T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 4023 | |
| 4024 | /* push arguments in the stack. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4025 | if (!hlua_txn_new(stream->hlua.T, stream, smp->px)) { |
| 4026 | send_log(smp->px, LOG_ERR, "Lua sample-fetch '%s': full stack.", fcn->name); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 4027 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) |
| 4028 | Alert("Lua sample-fetch '%s': full stack.\n", fcn->name); |
| 4029 | return 0; |
| 4030 | } |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4031 | stream->hlua.nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 4032 | |
| 4033 | /* push keywords in the stack. */ |
| 4034 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 4035 | /* Check stack available size. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4036 | if (!lua_checkstack(stream->hlua.T, 1)) { |
| 4037 | send_log(smp->px, LOG_ERR, "Lua sample-fetch '%s': full stack.", fcn->name); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 4038 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) |
| 4039 | Alert("Lua sample-fetch '%s': full stack.\n", fcn->name); |
| 4040 | return 0; |
| 4041 | } |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4042 | if (!lua_checkstack(stream->hlua.T, 1)) { |
| 4043 | send_log(smp->px, LOG_ERR, "Lua sample-fetch '%s': full stack.", fcn->name); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 4044 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) |
| 4045 | Alert("Lua sample-fetch '%s': full stack.\n", fcn->name); |
| 4046 | return 0; |
| 4047 | } |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4048 | hlua_arg2lua(stream->hlua.T, arg_p); |
| 4049 | stream->hlua.nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 4050 | } |
| 4051 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 4052 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4053 | stream->hlua.expire = tick_add(now_ms, hlua_timeout_session); |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 4054 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 4055 | /* Set the currently running flag. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4056 | HLUA_SET_RUN(&stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 4057 | } |
| 4058 | |
| 4059 | /* Execute the function. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4060 | switch (hlua_ctx_resume(&stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 4061 | /* finished. */ |
| 4062 | case HLUA_E_OK: |
| 4063 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4064 | hlua_lua2smp(stream->hlua.T, -1, smp); |
| 4065 | lua_pop(stream->hlua.T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 4066 | |
| 4067 | /* Set the end of execution flag. */ |
| 4068 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 4069 | return 1; |
| 4070 | |
| 4071 | /* yield. */ |
| 4072 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4073 | send_log(smp->px, LOG_ERR, "Lua sample-fetch '%s': cannot use yielded functions.", fcn->name); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 4074 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) |
| 4075 | Alert("Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name); |
| 4076 | return 0; |
| 4077 | |
| 4078 | /* finished with error. */ |
| 4079 | case HLUA_E_ERRMSG: |
| 4080 | /* Display log. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4081 | send_log(smp->px, LOG_ERR, "Lua sample-fetch '%s': %s.", fcn->name, lua_tostring(stream->hlua.T, -1)); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 4082 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4083 | Alert("Lua sample-fetch '%s': %s.\n", fcn->name, lua_tostring(stream->hlua.T, -1)); |
| 4084 | lua_pop(stream->hlua.T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 4085 | return 0; |
| 4086 | |
| 4087 | case HLUA_E_ERR: |
| 4088 | /* Display log. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4089 | send_log(smp->px, LOG_ERR, "Lua sample-fetch '%s' returns an unknown error.", fcn->name); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 4090 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) |
| 4091 | Alert("Lua sample-fetch '%s': returns an unknown error.\n", fcn->name); |
| 4092 | |
| 4093 | default: |
| 4094 | return 0; |
| 4095 | } |
| 4096 | } |
| 4097 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 4098 | /* This function is an LUA binding used for registering |
| 4099 | * "sample-conv" functions. It expects a converter name used |
| 4100 | * in the haproxy configuration file, and an LUA function. |
| 4101 | */ |
| 4102 | __LJMP static int hlua_register_converters(lua_State *L) |
| 4103 | { |
| 4104 | struct sample_conv_kw_list *sck; |
| 4105 | const char *name; |
| 4106 | int ref; |
| 4107 | int len; |
| 4108 | struct hlua_function *fcn; |
| 4109 | |
| 4110 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 4111 | |
| 4112 | /* First argument : converter name. */ |
| 4113 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 4114 | |
| 4115 | /* Second argument : lua function. */ |
| 4116 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 4117 | |
| 4118 | /* Allocate and fill the sample fetch keyword struct. */ |
Willy Tarreau | 07081fe | 2015-04-06 10:59:20 +0200 | [diff] [blame] | 4119 | sck = malloc(sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 4120 | if (!sck) |
| 4121 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 4122 | fcn = malloc(sizeof(*fcn)); |
| 4123 | if (!fcn) |
| 4124 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 4125 | |
| 4126 | /* Fill fcn. */ |
| 4127 | fcn->name = strdup(name); |
| 4128 | if (!fcn->name) |
| 4129 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 4130 | fcn->function_ref = ref; |
| 4131 | |
| 4132 | /* List head */ |
| 4133 | sck->list.n = sck->list.p = NULL; |
| 4134 | |
| 4135 | /* converter keyword. */ |
| 4136 | len = strlen("lua.") + strlen(name) + 1; |
| 4137 | sck->kw[0].kw = malloc(len); |
| 4138 | if (!sck->kw[0].kw) |
| 4139 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 4140 | |
| 4141 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 4142 | sck->kw[0].process = hlua_sample_conv_wrapper; |
| 4143 | sck->kw[0].arg_mask = ARG5(0,STR,STR,STR,STR,STR); |
| 4144 | sck->kw[0].val_args = NULL; |
| 4145 | sck->kw[0].in_type = SMP_T_STR; |
| 4146 | sck->kw[0].out_type = SMP_T_STR; |
| 4147 | sck->kw[0].private = fcn; |
| 4148 | |
| 4149 | /* End of array. */ |
| 4150 | memset(&sck->kw[1], 0, sizeof(struct sample_conv)); |
| 4151 | |
| 4152 | /* Register this new converter */ |
| 4153 | sample_register_convs(sck); |
| 4154 | |
| 4155 | return 0; |
| 4156 | } |
| 4157 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 4158 | /* This fucntion is an LUA binding used for registering |
| 4159 | * "sample-fetch" functions. It expects a converter name used |
| 4160 | * in the haproxy configuration file, and an LUA function. |
| 4161 | */ |
| 4162 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 4163 | { |
| 4164 | const char *name; |
| 4165 | int ref; |
| 4166 | int len; |
| 4167 | struct sample_fetch_kw_list *sfk; |
| 4168 | struct hlua_function *fcn; |
| 4169 | |
| 4170 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 4171 | |
| 4172 | /* First argument : sample-fetch name. */ |
| 4173 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 4174 | |
| 4175 | /* Second argument : lua function. */ |
| 4176 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 4177 | |
| 4178 | /* Allocate and fill the sample fetch keyword struct. */ |
Willy Tarreau | 07081fe | 2015-04-06 10:59:20 +0200 | [diff] [blame] | 4179 | sfk = malloc(sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 4180 | if (!sfk) |
| 4181 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 4182 | fcn = malloc(sizeof(*fcn)); |
| 4183 | if (!fcn) |
| 4184 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 4185 | |
| 4186 | /* Fill fcn. */ |
| 4187 | fcn->name = strdup(name); |
| 4188 | if (!fcn->name) |
| 4189 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 4190 | fcn->function_ref = ref; |
| 4191 | |
| 4192 | /* List head */ |
| 4193 | sfk->list.n = sfk->list.p = NULL; |
| 4194 | |
| 4195 | /* sample-fetch keyword. */ |
| 4196 | len = strlen("lua.") + strlen(name) + 1; |
| 4197 | sfk->kw[0].kw = malloc(len); |
| 4198 | if (!sfk->kw[0].kw) |
| 4199 | return luaL_error(L, "lua out of memory error."); |
| 4200 | |
| 4201 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 4202 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
| 4203 | sfk->kw[0].arg_mask = ARG5(0,STR,STR,STR,STR,STR); |
| 4204 | sfk->kw[0].val_args = NULL; |
| 4205 | sfk->kw[0].out_type = SMP_T_STR; |
| 4206 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 4207 | sfk->kw[0].val = 0; |
| 4208 | sfk->kw[0].private = fcn; |
| 4209 | |
| 4210 | /* End of array. */ |
| 4211 | memset(&sfk->kw[1], 0, sizeof(struct sample_fetch)); |
| 4212 | |
| 4213 | /* Register this new fetch. */ |
| 4214 | sample_register_fetches(sfk); |
| 4215 | |
| 4216 | return 0; |
| 4217 | } |
| 4218 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 4219 | /* global {tcp|http}-request parser. Return 1 in succes case, else return 0. */ |
| 4220 | static int hlua_parse_rule(const char **args, int *cur_arg, struct proxy *px, |
| 4221 | struct hlua_rule **rule_p, char **err) |
| 4222 | { |
| 4223 | struct hlua_rule *rule; |
| 4224 | |
| 4225 | /* Memory for the rule. */ |
| 4226 | rule = malloc(sizeof(*rule)); |
| 4227 | if (!rule) { |
| 4228 | memprintf(err, "out of memory error"); |
| 4229 | return 0; |
| 4230 | } |
| 4231 | *rule_p = rule; |
| 4232 | |
| 4233 | /* The requiered arg is a function name. */ |
| 4234 | if (!args[*cur_arg]) { |
| 4235 | memprintf(err, "expect Lua function name"); |
| 4236 | return 0; |
| 4237 | } |
| 4238 | |
| 4239 | /* Lookup for the symbol, and check if it is a function. */ |
| 4240 | lua_getglobal(gL.T, args[*cur_arg]); |
| 4241 | if (lua_isnil(gL.T, -1)) { |
| 4242 | lua_pop(gL.T, 1); |
| 4243 | memprintf(err, "Lua function '%s' not found", args[*cur_arg]); |
| 4244 | return 0; |
| 4245 | } |
| 4246 | if (!lua_isfunction(gL.T, -1)) { |
| 4247 | lua_pop(gL.T, 1); |
| 4248 | memprintf(err, "'%s' is not a function", args[*cur_arg]); |
| 4249 | return 0; |
| 4250 | } |
| 4251 | |
| 4252 | /* Reference the Lua function and store the reference. */ |
| 4253 | rule->fcn.function_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 4254 | rule->fcn.name = strdup(args[*cur_arg]); |
| 4255 | if (!rule->fcn.name) { |
| 4256 | memprintf(err, "out of memory error."); |
| 4257 | return 0; |
| 4258 | } |
| 4259 | (*cur_arg)++; |
| 4260 | |
| 4261 | /* TODO: later accept arguments. */ |
| 4262 | rule->args = NULL; |
| 4263 | |
| 4264 | return 1; |
| 4265 | } |
| 4266 | |
| 4267 | /* This function is a wrapper to execute each LUA function declared |
| 4268 | * as an action wrapper during the initialisation period. This function |
| 4269 | * return 1 if the processing is finished (with oe without error) and |
| 4270 | * return 0 if the function must be called again because the LUA |
| 4271 | * returns a yield. |
| 4272 | */ |
| 4273 | static int hlua_request_act_wrapper(struct hlua_rule *rule, struct proxy *px, |
Willy Tarreau | fdcd2ae | 2015-04-04 01:11:28 +0200 | [diff] [blame] | 4274 | struct stream *s, unsigned int analyzer) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 4275 | { |
| 4276 | char **arg; |
| 4277 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4278 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 4279 | * Lua context can be not initialized. This behavior |
| 4280 | * permits to save performances because a systematic |
| 4281 | * Lua initialization cause 5% performances loss. |
| 4282 | */ |
| 4283 | if (!s->hlua.T && !hlua_ctx_init(&s->hlua, s->task)) { |
| 4284 | send_log(px, LOG_ERR, "Lua action '%s': can't initialize Lua context.", rule->fcn.name); |
| 4285 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) |
| 4286 | Alert("Lua action '%s': can't initialize Lua context.\n", rule->fcn.name); |
| 4287 | return 0; |
| 4288 | } |
| 4289 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 4290 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 4291 | if (!HLUA_IS_RUNNING(&s->hlua)) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 4292 | /* Check stack available size. */ |
| 4293 | if (!lua_checkstack(s->hlua.T, 1)) { |
| 4294 | send_log(px, LOG_ERR, "Lua function '%s': full stack.", rule->fcn.name); |
| 4295 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) |
| 4296 | Alert("Lua function '%s': full stack.\n", rule->fcn.name); |
| 4297 | return 0; |
| 4298 | } |
| 4299 | |
| 4300 | /* Restore the function in the stack. */ |
| 4301 | lua_rawgeti(s->hlua.T, LUA_REGISTRYINDEX, rule->fcn.function_ref); |
| 4302 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4303 | /* Create and and push object stream in the stack. */ |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 4304 | if (!hlua_txn_new(s->hlua.T, s, px)) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 4305 | send_log(px, LOG_ERR, "Lua function '%s': full stack.", rule->fcn.name); |
| 4306 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) |
| 4307 | Alert("Lua function '%s': full stack.\n", rule->fcn.name); |
| 4308 | return 0; |
| 4309 | } |
| 4310 | s->hlua.nargs = 1; |
| 4311 | |
| 4312 | /* push keywords in the stack. */ |
| 4313 | for (arg = rule->args; arg && *arg; arg++) { |
| 4314 | if (!lua_checkstack(s->hlua.T, 1)) { |
| 4315 | send_log(px, LOG_ERR, "Lua function '%s': full stack.", rule->fcn.name); |
| 4316 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) |
| 4317 | Alert("Lua function '%s': full stack.\n", rule->fcn.name); |
| 4318 | return 0; |
| 4319 | } |
| 4320 | lua_pushstring(s->hlua.T, *arg); |
| 4321 | s->hlua.nargs++; |
| 4322 | } |
| 4323 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 4324 | /* We must initialize the execution timeouts. */ |
| 4325 | s->hlua.expire = tick_add(now_ms, hlua_timeout_session); |
| 4326 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 4327 | /* Set the currently running flag. */ |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 4328 | HLUA_SET_RUN(&s->hlua); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 4329 | } |
| 4330 | |
| 4331 | /* Execute the function. */ |
| 4332 | switch (hlua_ctx_resume(&s->hlua, 1)) { |
| 4333 | /* finished. */ |
| 4334 | case HLUA_E_OK: |
| 4335 | return 1; |
| 4336 | |
| 4337 | /* yield. */ |
| 4338 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 4339 | /* Set timeout in the required channel. */ |
| 4340 | if (s->hlua.wake_time != TICK_ETERNITY) { |
| 4341 | if (analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4342 | s->req.analyse_exp = s->hlua.wake_time; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 4343 | else if (analyzer & (AN_RES_INSPECT|AN_RES_HTTP_PROCESS_BE)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4344 | s->res.analyse_exp = s->hlua.wake_time; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 4345 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 4346 | /* Some actions can be wake up when a "write" event |
| 4347 | * is detected on a response channel. This is useful |
| 4348 | * only for actions targetted on the requests. |
| 4349 | */ |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 4350 | if (HLUA_IS_WAKERESWR(&s->hlua)) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4351 | s->res.flags |= CF_WAKE_WRITE; |
Willy Tarreau | 76bd97f | 2015-03-10 17:16:10 +0100 | [diff] [blame] | 4352 | if ((analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE))) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4353 | s->res.analysers |= analyzer; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 4354 | } |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 4355 | if (HLUA_IS_WAKEREQWR(&s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4356 | s->req.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 4357 | return 0; |
| 4358 | |
| 4359 | /* finished with error. */ |
| 4360 | case HLUA_E_ERRMSG: |
| 4361 | /* Display log. */ |
| 4362 | send_log(px, LOG_ERR, "Lua function '%s': %s.", rule->fcn.name, lua_tostring(s->hlua.T, -1)); |
| 4363 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) |
| 4364 | Alert("Lua function '%s': %s.\n", rule->fcn.name, lua_tostring(s->hlua.T, -1)); |
| 4365 | lua_pop(s->hlua.T, 1); |
| 4366 | return 1; |
| 4367 | |
| 4368 | case HLUA_E_ERR: |
| 4369 | /* Display log. */ |
| 4370 | send_log(px, LOG_ERR, "Lua function '%s' return an unknown error.", rule->fcn.name); |
| 4371 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) |
| 4372 | Alert("Lua function '%s' return an unknown error.\n", rule->fcn.name); |
| 4373 | |
| 4374 | default: |
| 4375 | return 1; |
| 4376 | } |
| 4377 | } |
| 4378 | |
| 4379 | /* Lua execution wrapper for "tcp-request". This function uses |
| 4380 | * "hlua_request_act_wrapper" for executing the LUA code. |
| 4381 | */ |
| 4382 | int hlua_tcp_req_act_wrapper(struct tcp_rule *tcp_rule, struct proxy *px, |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4383 | struct stream *s) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 4384 | { |
Thierry FOURNIER | fbdb775 | 2015-06-03 19:32:04 +0200 | [diff] [blame] | 4385 | return hlua_request_act_wrapper((struct hlua_rule *)tcp_rule->act_prm.data[0], |
Willy Tarreau | fdcd2ae | 2015-04-04 01:11:28 +0200 | [diff] [blame] | 4386 | px, s, AN_REQ_INSPECT_FE); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 4387 | } |
| 4388 | |
| 4389 | /* Lua execution wrapper for "tcp-response". This function uses |
| 4390 | * "hlua_request_act_wrapper" for executing the LUA code. |
| 4391 | */ |
| 4392 | int hlua_tcp_res_act_wrapper(struct tcp_rule *tcp_rule, struct proxy *px, |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4393 | struct stream *s) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 4394 | { |
Thierry FOURNIER | fbdb775 | 2015-06-03 19:32:04 +0200 | [diff] [blame] | 4395 | return hlua_request_act_wrapper((struct hlua_rule *)tcp_rule->act_prm.data[0], |
Willy Tarreau | fdcd2ae | 2015-04-04 01:11:28 +0200 | [diff] [blame] | 4396 | px, s, AN_RES_INSPECT); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 4397 | } |
| 4398 | |
| 4399 | /* Lua execution wrapper for http-request. |
| 4400 | * This function uses "hlua_request_act_wrapper" for executing |
| 4401 | * the LUA code. |
| 4402 | */ |
| 4403 | int hlua_http_req_act_wrapper(struct http_req_rule *rule, struct proxy *px, |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4404 | struct stream *s) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 4405 | { |
| 4406 | return hlua_request_act_wrapper((struct hlua_rule *)rule->arg.data, px, |
Willy Tarreau | fdcd2ae | 2015-04-04 01:11:28 +0200 | [diff] [blame] | 4407 | s, AN_REQ_HTTP_PROCESS_FE); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 4408 | } |
| 4409 | |
| 4410 | /* Lua execution wrapper for http-response. |
| 4411 | * This function uses "hlua_request_act_wrapper" for executing |
| 4412 | * the LUA code. |
| 4413 | */ |
| 4414 | int hlua_http_res_act_wrapper(struct http_res_rule *rule, struct proxy *px, |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4415 | struct stream *s) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 4416 | { |
| 4417 | return hlua_request_act_wrapper((struct hlua_rule *)rule->arg.data, px, |
Willy Tarreau | fdcd2ae | 2015-04-04 01:11:28 +0200 | [diff] [blame] | 4418 | s, AN_RES_HTTP_PROCESS_BE); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 4419 | } |
| 4420 | |
| 4421 | /* tcp-request <*> configuration wrapper. */ |
| 4422 | static int tcp_req_action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 4423 | struct tcp_rule *rule, char **err) |
| 4424 | { |
Thierry FOURNIER | fbdb775 | 2015-06-03 19:32:04 +0200 | [diff] [blame] | 4425 | if (!hlua_parse_rule(args, cur_arg, px, (struct hlua_rule **)&rule->act_prm.data[0], err)) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 4426 | return 0; |
Thierry FOURNIER | 79318d7 | 2015-05-29 17:31:12 +0200 | [diff] [blame] | 4427 | rule->action = TCP_ACT_CUSTOM_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 4428 | rule->action_ptr = hlua_tcp_req_act_wrapper; |
| 4429 | return 1; |
| 4430 | } |
| 4431 | |
| 4432 | /* tcp-response <*> configuration wrapper. */ |
| 4433 | static int tcp_res_action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 4434 | struct tcp_rule *rule, char **err) |
| 4435 | { |
Thierry FOURNIER | fbdb775 | 2015-06-03 19:32:04 +0200 | [diff] [blame] | 4436 | if (!hlua_parse_rule(args, cur_arg, px, (struct hlua_rule **)&rule->act_prm.data[0], err)) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 4437 | return 0; |
Thierry FOURNIER | 79318d7 | 2015-05-29 17:31:12 +0200 | [diff] [blame] | 4438 | rule->action = TCP_ACT_CUSTOM_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 4439 | rule->action_ptr = hlua_tcp_res_act_wrapper; |
| 4440 | return 1; |
| 4441 | } |
| 4442 | |
| 4443 | /* http-request <*> configuration wrapper. */ |
| 4444 | static int http_req_action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 4445 | struct http_req_rule *rule, char **err) |
| 4446 | { |
| 4447 | if (!hlua_parse_rule(args, cur_arg, px, (struct hlua_rule **)&rule->arg.data, err)) |
| 4448 | return -1; |
| 4449 | rule->action = HTTP_REQ_ACT_CUSTOM_CONT; |
| 4450 | rule->action_ptr = hlua_http_req_act_wrapper; |
| 4451 | return 1; |
| 4452 | } |
| 4453 | |
| 4454 | /* http-response <*> configuration wrapper. */ |
| 4455 | static int http_res_action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 4456 | struct http_res_rule *rule, char **err) |
| 4457 | { |
| 4458 | if (!hlua_parse_rule(args, cur_arg, px, (struct hlua_rule **)&rule->arg.data, err)) |
| 4459 | return -1; |
| 4460 | rule->action = HTTP_RES_ACT_CUSTOM_CONT; |
| 4461 | rule->action_ptr = hlua_http_res_act_wrapper; |
| 4462 | return 1; |
| 4463 | } |
| 4464 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 4465 | static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx, |
| 4466 | struct proxy *defpx, const char *file, int line, |
| 4467 | char **err, unsigned int *timeout) |
| 4468 | { |
| 4469 | const char *error; |
| 4470 | |
| 4471 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
| 4472 | if (error && *error != '\0') { |
| 4473 | memprintf(err, "%s: invalid timeout", args[0]); |
| 4474 | return -1; |
| 4475 | } |
| 4476 | return 0; |
| 4477 | } |
| 4478 | |
| 4479 | static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx, |
| 4480 | struct proxy *defpx, const char *file, int line, |
| 4481 | char **err) |
| 4482 | { |
| 4483 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 4484 | file, line, err, &hlua_timeout_session); |
| 4485 | } |
| 4486 | |
| 4487 | static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx, |
| 4488 | struct proxy *defpx, const char *file, int line, |
| 4489 | char **err) |
| 4490 | { |
| 4491 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 4492 | file, line, err, &hlua_timeout_task); |
| 4493 | } |
| 4494 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 4495 | static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx, |
| 4496 | struct proxy *defpx, const char *file, int line, |
| 4497 | char **err) |
| 4498 | { |
| 4499 | char *error; |
| 4500 | |
| 4501 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 4502 | if (*error != '\0') { |
| 4503 | memprintf(err, "%s: invalid number", args[0]); |
| 4504 | return -1; |
| 4505 | } |
| 4506 | return 0; |
| 4507 | } |
| 4508 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 4509 | static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx, |
| 4510 | struct proxy *defpx, const char *file, int line, |
| 4511 | char **err) |
| 4512 | { |
| 4513 | char *error; |
| 4514 | |
| 4515 | if (*(args[1]) == 0) { |
| 4516 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 4517 | return -1; |
| 4518 | } |
| 4519 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 4520 | if (*error != '\0') { |
| 4521 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 4522 | return -1; |
| 4523 | } |
| 4524 | return 0; |
| 4525 | } |
| 4526 | |
| 4527 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 4528 | /* This function is called by the main configuration key "lua-load". It loads and |
| 4529 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 4530 | * the main lua entry point. |
| 4531 | * |
| 4532 | * This funtion runs with the HAProxy keywords API. It returns -1 if an error is |
| 4533 | * occured, otherwise it returns 0. |
| 4534 | * |
| 4535 | * In some error case, LUA set an error message in top of the stack. This function |
| 4536 | * returns this error message in the HAProxy logs and pop it from the stack. |
| 4537 | */ |
| 4538 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
| 4539 | struct proxy *defpx, const char *file, int line, |
| 4540 | char **err) |
| 4541 | { |
| 4542 | int error; |
| 4543 | |
| 4544 | /* Just load and compile the file. */ |
| 4545 | error = luaL_loadfile(gL.T, args[1]); |
| 4546 | if (error) { |
| 4547 | memprintf(err, "error in lua file '%s': %s", args[1], lua_tostring(gL.T, -1)); |
| 4548 | lua_pop(gL.T, 1); |
| 4549 | return -1; |
| 4550 | } |
| 4551 | |
| 4552 | /* If no syntax error where detected, execute the code. */ |
| 4553 | error = lua_pcall(gL.T, 0, LUA_MULTRET, 0); |
| 4554 | switch (error) { |
| 4555 | case LUA_OK: |
| 4556 | break; |
| 4557 | case LUA_ERRRUN: |
| 4558 | memprintf(err, "lua runtime error: %s\n", lua_tostring(gL.T, -1)); |
| 4559 | lua_pop(gL.T, 1); |
| 4560 | return -1; |
| 4561 | case LUA_ERRMEM: |
| 4562 | memprintf(err, "lua out of memory error\n"); |
| 4563 | return -1; |
| 4564 | case LUA_ERRERR: |
| 4565 | memprintf(err, "lua message handler error: %s\n", lua_tostring(gL.T, -1)); |
| 4566 | lua_pop(gL.T, 1); |
| 4567 | return -1; |
| 4568 | case LUA_ERRGCMM: |
| 4569 | memprintf(err, "lua garbage collector error: %s\n", lua_tostring(gL.T, -1)); |
| 4570 | lua_pop(gL.T, 1); |
| 4571 | return -1; |
| 4572 | default: |
| 4573 | memprintf(err, "lua unknonwn error: %s\n", lua_tostring(gL.T, -1)); |
| 4574 | lua_pop(gL.T, 1); |
| 4575 | return -1; |
| 4576 | } |
| 4577 | |
| 4578 | return 0; |
| 4579 | } |
| 4580 | |
| 4581 | /* configuration keywords declaration */ |
| 4582 | static struct cfg_kw_list cfg_kws = {{ },{ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 4583 | { CFG_GLOBAL, "lua-load", hlua_load }, |
| 4584 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 4585 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 4586 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 4587 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 4588 | { 0, NULL, NULL }, |
| 4589 | }}; |
| 4590 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 4591 | static struct http_req_action_kw_list http_req_kws = {"lua", { }, { |
| 4592 | { "lua", http_req_action_register_lua }, |
| 4593 | { NULL, NULL } |
| 4594 | }}; |
| 4595 | |
| 4596 | static struct http_res_action_kw_list http_res_kws = {"lua", { }, { |
| 4597 | { "lua", http_res_action_register_lua }, |
| 4598 | { NULL, NULL } |
| 4599 | }}; |
| 4600 | |
| 4601 | static struct tcp_action_kw_list tcp_req_cont_kws = {"lua", { }, { |
| 4602 | { "lua", tcp_req_action_register_lua }, |
| 4603 | { NULL, NULL } |
| 4604 | }}; |
| 4605 | |
| 4606 | static struct tcp_action_kw_list tcp_res_cont_kws = {"lua", { }, { |
| 4607 | { "lua", tcp_res_action_register_lua }, |
| 4608 | { NULL, NULL } |
| 4609 | }}; |
| 4610 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 4611 | int hlua_post_init() |
| 4612 | { |
| 4613 | struct hlua_init_function *init; |
| 4614 | const char *msg; |
| 4615 | enum hlua_exec ret; |
| 4616 | |
| 4617 | list_for_each_entry(init, &hlua_init_functions, l) { |
| 4618 | lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref); |
| 4619 | ret = hlua_ctx_resume(&gL, 0); |
| 4620 | switch (ret) { |
| 4621 | case HLUA_E_OK: |
| 4622 | lua_pop(gL.T, -1); |
| 4623 | return 1; |
| 4624 | case HLUA_E_AGAIN: |
| 4625 | Alert("lua init: yield not allowed.\n"); |
| 4626 | return 0; |
| 4627 | case HLUA_E_ERRMSG: |
| 4628 | msg = lua_tostring(gL.T, -1); |
| 4629 | Alert("lua init: %s.\n", msg); |
| 4630 | return 0; |
| 4631 | case HLUA_E_ERR: |
| 4632 | default: |
| 4633 | Alert("lua init: unknown runtime error.\n"); |
| 4634 | return 0; |
| 4635 | } |
| 4636 | } |
| 4637 | return 1; |
| 4638 | } |
| 4639 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 4640 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 4641 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 4642 | * is the previously allocated size or the kind of object in case of a new |
| 4643 | * allocation. <nsize> is the requested new size. |
| 4644 | */ |
| 4645 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 4646 | { |
| 4647 | struct hlua_mem_allocator *zone = ud; |
| 4648 | |
| 4649 | if (nsize == 0) { |
| 4650 | /* it's a free */ |
| 4651 | if (ptr) |
| 4652 | zone->allocated -= osize; |
| 4653 | free(ptr); |
| 4654 | return NULL; |
| 4655 | } |
| 4656 | |
| 4657 | if (!ptr) { |
| 4658 | /* it's a new allocation */ |
| 4659 | if (zone->limit && zone->allocated + nsize > zone->limit) |
| 4660 | return NULL; |
| 4661 | |
| 4662 | ptr = malloc(nsize); |
| 4663 | if (ptr) |
| 4664 | zone->allocated += nsize; |
| 4665 | return ptr; |
| 4666 | } |
| 4667 | |
| 4668 | /* it's a realloc */ |
| 4669 | if (zone->limit && zone->allocated + nsize - osize > zone->limit) |
| 4670 | return NULL; |
| 4671 | |
| 4672 | ptr = realloc(ptr, nsize); |
| 4673 | if (ptr) |
| 4674 | zone->allocated += nsize - osize; |
| 4675 | return ptr; |
| 4676 | } |
| 4677 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 4678 | void hlua_init(void) |
| 4679 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 4680 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 4681 | int idx; |
| 4682 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4683 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 4684 | char *p; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4685 | #ifdef USE_OPENSSL |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4686 | struct srv_kw *kw; |
| 4687 | int tmp_error; |
| 4688 | char *error; |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 4689 | char *args[] = { /* SSL client configuration. */ |
| 4690 | "ssl", |
| 4691 | "verify", |
| 4692 | "none", |
| 4693 | "force-sslv3", |
| 4694 | NULL |
| 4695 | }; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4696 | #endif |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 4697 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4698 | /* Initialise com signals pool */ |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 4699 | pool2_hlua_com = create_pool("hlua_com", sizeof(struct hlua_com), MEM_F_SHARED); |
| 4700 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 4701 | /* Register configuration keywords. */ |
| 4702 | cfg_register_keywords(&cfg_kws); |
| 4703 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 4704 | /* Register custom HTTP rules. */ |
| 4705 | http_req_keywords_register(&http_req_kws); |
| 4706 | http_res_keywords_register(&http_res_kws); |
| 4707 | tcp_req_cont_keywords_register(&tcp_req_cont_kws); |
| 4708 | tcp_res_cont_keywords_register(&tcp_res_cont_kws); |
| 4709 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 4710 | /* Init main lua stack. */ |
| 4711 | gL.Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 4712 | gL.flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 4713 | LIST_INIT(&gL.com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 4714 | gL.T = luaL_newstate(); |
| 4715 | hlua_sethlua(&gL); |
| 4716 | gL.Tref = LUA_REFNIL; |
| 4717 | gL.task = NULL; |
| 4718 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 4719 | /* change the memory allocators to track memory usage */ |
| 4720 | lua_setallocf(gL.T, hlua_alloc, &hlua_global_allocator); |
| 4721 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 4722 | /* Initialise lua. */ |
| 4723 | luaL_openlibs(gL.T); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 4724 | |
| 4725 | /* |
| 4726 | * |
| 4727 | * Create "core" object. |
| 4728 | * |
| 4729 | */ |
| 4730 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 4731 | /* This table entry is the object "core" base. */ |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 4732 | lua_newtable(gL.T); |
| 4733 | |
| 4734 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4735 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 4736 | hlua_class_const_int(gL.T, log_levels[i], i); |
| 4737 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 4738 | /* Register special functions. */ |
| 4739 | hlua_class_function(gL.T, "register_init", hlua_register_init); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 4740 | hlua_class_function(gL.T, "register_task", hlua_register_task); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 4741 | hlua_class_function(gL.T, "register_fetches", hlua_register_fetches); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 4742 | hlua_class_function(gL.T, "register_converters", hlua_register_converters); |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 4743 | hlua_class_function(gL.T, "yield", hlua_yield); |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 4744 | hlua_class_function(gL.T, "set_nice", hlua_set_nice); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 4745 | hlua_class_function(gL.T, "sleep", hlua_sleep); |
| 4746 | hlua_class_function(gL.T, "msleep", hlua_msleep); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 4747 | hlua_class_function(gL.T, "add_acl", hlua_add_acl); |
| 4748 | hlua_class_function(gL.T, "del_acl", hlua_del_acl); |
| 4749 | hlua_class_function(gL.T, "set_map", hlua_set_map); |
| 4750 | hlua_class_function(gL.T, "del_map", hlua_del_map); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 4751 | hlua_class_function(gL.T, "tcp", hlua_socket_new); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 4752 | hlua_class_function(gL.T, "log", hlua_log); |
| 4753 | hlua_class_function(gL.T, "Debug", hlua_log_debug); |
| 4754 | hlua_class_function(gL.T, "Info", hlua_log_info); |
| 4755 | hlua_class_function(gL.T, "Warning", hlua_log_warning); |
| 4756 | hlua_class_function(gL.T, "Alert", hlua_log_alert); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 4757 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 4758 | lua_setglobal(gL.T, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4759 | |
| 4760 | /* |
| 4761 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 4762 | * Register class Map |
| 4763 | * |
| 4764 | */ |
| 4765 | |
| 4766 | /* This table entry is the object "Map" base. */ |
| 4767 | lua_newtable(gL.T); |
| 4768 | |
| 4769 | /* register pattern types. */ |
| 4770 | for (i=0; i<PAT_MATCH_NUM; i++) |
| 4771 | hlua_class_const_int(gL.T, pat_match_names[i], i); |
| 4772 | |
| 4773 | /* register constructor. */ |
| 4774 | hlua_class_function(gL.T, "new", hlua_map_new); |
| 4775 | |
| 4776 | /* Create and fill the metatable. */ |
| 4777 | lua_newtable(gL.T); |
| 4778 | |
| 4779 | /* Create and fille the __index entry. */ |
| 4780 | lua_pushstring(gL.T, "__index"); |
| 4781 | lua_newtable(gL.T); |
| 4782 | |
| 4783 | /* Register . */ |
| 4784 | hlua_class_function(gL.T, "lookup", hlua_map_lookup); |
| 4785 | hlua_class_function(gL.T, "slookup", hlua_map_slookup); |
| 4786 | |
| 4787 | lua_settable(gL.T, -3); |
| 4788 | |
| 4789 | /* Register previous table in the registry with reference and named entry. */ |
| 4790 | lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */ |
| 4791 | lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */ |
| 4792 | lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_MAP); /* register class session. */ |
| 4793 | class_map_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */ |
| 4794 | |
| 4795 | /* Assign the metatable to the mai Map object. */ |
| 4796 | lua_setmetatable(gL.T, -2); |
| 4797 | |
| 4798 | /* Set a name to the table. */ |
| 4799 | lua_setglobal(gL.T, "Map"); |
| 4800 | |
| 4801 | /* |
| 4802 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4803 | * Register class Channel |
| 4804 | * |
| 4805 | */ |
| 4806 | |
| 4807 | /* Create and fill the metatable. */ |
| 4808 | lua_newtable(gL.T); |
| 4809 | |
| 4810 | /* Create and fille the __index entry. */ |
| 4811 | lua_pushstring(gL.T, "__index"); |
| 4812 | lua_newtable(gL.T); |
| 4813 | |
| 4814 | /* Register . */ |
| 4815 | hlua_class_function(gL.T, "get", hlua_channel_get); |
| 4816 | hlua_class_function(gL.T, "dup", hlua_channel_dup); |
| 4817 | hlua_class_function(gL.T, "getline", hlua_channel_getline); |
| 4818 | hlua_class_function(gL.T, "set", hlua_channel_set); |
| 4819 | hlua_class_function(gL.T, "append", hlua_channel_append); |
| 4820 | hlua_class_function(gL.T, "send", hlua_channel_send); |
| 4821 | hlua_class_function(gL.T, "forward", hlua_channel_forward); |
| 4822 | hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len); |
| 4823 | hlua_class_function(gL.T, "get_out_len", hlua_channel_get_out_len); |
| 4824 | |
| 4825 | lua_settable(gL.T, -3); |
| 4826 | |
| 4827 | /* Register previous table in the registry with reference and named entry. */ |
| 4828 | lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */ |
| 4829 | lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_CHANNEL); /* register class session. */ |
| 4830 | class_channel_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */ |
| 4831 | |
| 4832 | /* |
| 4833 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4834 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4835 | * |
| 4836 | */ |
| 4837 | |
| 4838 | /* Create and fill the metatable. */ |
| 4839 | lua_newtable(gL.T); |
| 4840 | |
| 4841 | /* Create and fille the __index entry. */ |
| 4842 | lua_pushstring(gL.T, "__index"); |
| 4843 | lua_newtable(gL.T); |
| 4844 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 4845 | /* Browse existing fetches and create the associated |
| 4846 | * object method. |
| 4847 | */ |
| 4848 | sf = NULL; |
| 4849 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
| 4850 | |
| 4851 | /* Dont register the keywork if the arguments check function are |
| 4852 | * not safe during the runtime. |
| 4853 | */ |
| 4854 | if ((sf->val_args != NULL) && |
| 4855 | (sf->val_args != val_payload_lv) && |
| 4856 | (sf->val_args != val_hdr)) |
| 4857 | continue; |
| 4858 | |
| 4859 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 4860 | * by an underscore. |
| 4861 | */ |
| 4862 | strncpy(trash.str, sf->kw, trash.size); |
| 4863 | trash.str[trash.size - 1] = '\0'; |
| 4864 | for (p = trash.str; *p; p++) |
| 4865 | if (*p == '.' || *p == '-' || *p == '+') |
| 4866 | *p = '_'; |
| 4867 | |
| 4868 | /* Register the function. */ |
| 4869 | lua_pushstring(gL.T, trash.str); |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 4870 | lua_pushlightuserdata(gL.T, sf); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 4871 | lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1); |
| 4872 | lua_settable(gL.T, -3); |
| 4873 | } |
| 4874 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4875 | lua_settable(gL.T, -3); |
| 4876 | |
| 4877 | /* Register previous table in the registry with reference and named entry. */ |
| 4878 | lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */ |
| 4879 | lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_FETCHES); /* register class session. */ |
| 4880 | class_fetches_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */ |
| 4881 | |
| 4882 | /* |
| 4883 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4884 | * Register class Converters |
| 4885 | * |
| 4886 | */ |
| 4887 | |
| 4888 | /* Create and fill the metatable. */ |
| 4889 | lua_newtable(gL.T); |
| 4890 | |
| 4891 | /* Create and fill the __index entry. */ |
| 4892 | lua_pushstring(gL.T, "__index"); |
| 4893 | lua_newtable(gL.T); |
| 4894 | |
| 4895 | /* Browse existing converters and create the associated |
| 4896 | * object method. |
| 4897 | */ |
| 4898 | sc = NULL; |
| 4899 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
| 4900 | /* Dont register the keywork if the arguments check function are |
| 4901 | * not safe during the runtime. |
| 4902 | */ |
| 4903 | if (sc->val_args != NULL) |
| 4904 | continue; |
| 4905 | |
| 4906 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 4907 | * by an underscore. |
| 4908 | */ |
| 4909 | strncpy(trash.str, sc->kw, trash.size); |
| 4910 | trash.str[trash.size - 1] = '\0'; |
| 4911 | for (p = trash.str; *p; p++) |
| 4912 | if (*p == '.' || *p == '-' || *p == '+') |
| 4913 | *p = '_'; |
| 4914 | |
| 4915 | /* Register the function. */ |
| 4916 | lua_pushstring(gL.T, trash.str); |
| 4917 | lua_pushlightuserdata(gL.T, sc); |
| 4918 | lua_pushcclosure(gL.T, hlua_run_sample_conv, 1); |
| 4919 | lua_settable(gL.T, -3); |
| 4920 | } |
| 4921 | |
| 4922 | lua_settable(gL.T, -3); |
| 4923 | |
| 4924 | /* Register previous table in the registry with reference and named entry. */ |
| 4925 | lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */ |
| 4926 | lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_CONVERTERS); /* register class session. */ |
| 4927 | class_converters_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */ |
| 4928 | |
| 4929 | /* |
| 4930 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4931 | * Register class HTTP |
| 4932 | * |
| 4933 | */ |
| 4934 | |
| 4935 | /* Create and fill the metatable. */ |
| 4936 | lua_newtable(gL.T); |
| 4937 | |
| 4938 | /* Create and fille the __index entry. */ |
| 4939 | lua_pushstring(gL.T, "__index"); |
| 4940 | lua_newtable(gL.T); |
| 4941 | |
| 4942 | /* Register Lua functions. */ |
| 4943 | hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers); |
| 4944 | hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr); |
| 4945 | hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr); |
| 4946 | hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val); |
| 4947 | hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr); |
| 4948 | hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr); |
| 4949 | hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth); |
| 4950 | hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path); |
| 4951 | hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query); |
| 4952 | hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri); |
| 4953 | |
| 4954 | hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers); |
| 4955 | hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr); |
| 4956 | hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr); |
| 4957 | hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val); |
| 4958 | hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr); |
| 4959 | hlua_class_function(gL.T, "res_set_header", hlua_http_res_set_hdr); |
| 4960 | |
| 4961 | lua_settable(gL.T, -3); |
| 4962 | |
| 4963 | /* Register previous table in the registry with reference and named entry. */ |
| 4964 | lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */ |
| 4965 | lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_HTTP); /* register class session. */ |
| 4966 | class_http_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */ |
| 4967 | |
| 4968 | /* |
| 4969 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4970 | * Register class TXN |
| 4971 | * |
| 4972 | */ |
| 4973 | |
| 4974 | /* Create and fill the metatable. */ |
| 4975 | lua_newtable(gL.T); |
| 4976 | |
| 4977 | /* Create and fille the __index entry. */ |
| 4978 | lua_pushstring(gL.T, "__index"); |
| 4979 | lua_newtable(gL.T); |
| 4980 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 4981 | /* Register Lua functions. */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 4982 | hlua_class_function(gL.T, "set_priv", hlua_set_priv); |
| 4983 | hlua_class_function(gL.T, "get_priv", hlua_get_priv); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 4984 | hlua_class_function(gL.T, "set_var", hlua_set_var); |
| 4985 | hlua_class_function(gL.T, "get_var", hlua_get_var); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 4986 | hlua_class_function(gL.T, "close", hlua_txn_close); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 4987 | hlua_class_function(gL.T, "set_loglevel",hlua_txn_set_loglevel); |
| 4988 | hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos); |
| 4989 | hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 4990 | hlua_class_function(gL.T, "deflog", hlua_txn_deflog); |
| 4991 | hlua_class_function(gL.T, "log", hlua_txn_log); |
| 4992 | hlua_class_function(gL.T, "Debug", hlua_txn_log_debug); |
| 4993 | hlua_class_function(gL.T, "Info", hlua_txn_log_info); |
| 4994 | hlua_class_function(gL.T, "Warning", hlua_txn_log_warning); |
| 4995 | hlua_class_function(gL.T, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 4996 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4997 | lua_settable(gL.T, -3); |
| 4998 | |
| 4999 | /* Register previous table in the registry with reference and named entry. */ |
| 5000 | lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */ |
| 5001 | lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_TXN); /* register class session. */ |
| 5002 | class_txn_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 5003 | |
| 5004 | /* |
| 5005 | * |
| 5006 | * Register class Socket |
| 5007 | * |
| 5008 | */ |
| 5009 | |
| 5010 | /* Create and fill the metatable. */ |
| 5011 | lua_newtable(gL.T); |
| 5012 | |
| 5013 | /* Create and fille the __index entry. */ |
| 5014 | lua_pushstring(gL.T, "__index"); |
| 5015 | lua_newtable(gL.T); |
| 5016 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 5017 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 5018 | hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 5019 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 5020 | hlua_class_function(gL.T, "connect", hlua_socket_connect); |
| 5021 | hlua_class_function(gL.T, "send", hlua_socket_send); |
| 5022 | hlua_class_function(gL.T, "receive", hlua_socket_receive); |
| 5023 | hlua_class_function(gL.T, "close", hlua_socket_close); |
| 5024 | hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername); |
| 5025 | hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname); |
| 5026 | hlua_class_function(gL.T, "setoption", hlua_socket_setoption); |
| 5027 | hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout); |
| 5028 | |
| 5029 | lua_settable(gL.T, -3); /* Push the last 2 entries in the table at index -3 */ |
| 5030 | |
| 5031 | /* Register the garbage collector entry. */ |
| 5032 | lua_pushstring(gL.T, "__gc"); |
| 5033 | lua_pushcclosure(gL.T, hlua_socket_gc, 0); |
| 5034 | lua_settable(gL.T, -3); /* Push the last 2 entries in the table at index -3 */ |
| 5035 | |
| 5036 | /* Register previous table in the registry with reference and named entry. */ |
| 5037 | lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */ |
| 5038 | lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */ |
| 5039 | lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_SOCKET); /* register class socket. */ |
| 5040 | class_socket_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class socket. */ |
| 5041 | |
| 5042 | /* Proxy and server configuration initialisation. */ |
| 5043 | memset(&socket_proxy, 0, sizeof(socket_proxy)); |
| 5044 | init_new_proxy(&socket_proxy); |
| 5045 | socket_proxy.parent = NULL; |
| 5046 | socket_proxy.last_change = now.tv_sec; |
| 5047 | socket_proxy.id = "LUA-SOCKET"; |
| 5048 | socket_proxy.cap = PR_CAP_FE | PR_CAP_BE; |
| 5049 | socket_proxy.maxconn = 0; |
| 5050 | socket_proxy.accept = NULL; |
| 5051 | socket_proxy.options2 |= PR_O2_INDEPSTR; |
| 5052 | socket_proxy.srv = NULL; |
| 5053 | socket_proxy.conn_retries = 0; |
| 5054 | socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */ |
| 5055 | |
| 5056 | /* Init TCP server: unchanged parameters */ |
| 5057 | memset(&socket_tcp, 0, sizeof(socket_tcp)); |
| 5058 | socket_tcp.next = NULL; |
| 5059 | socket_tcp.proxy = &socket_proxy; |
| 5060 | socket_tcp.obj_type = OBJ_TYPE_SERVER; |
| 5061 | LIST_INIT(&socket_tcp.actconns); |
| 5062 | LIST_INIT(&socket_tcp.pendconns); |
| 5063 | socket_tcp.state = SRV_ST_RUNNING; /* early server setup */ |
| 5064 | socket_tcp.last_change = 0; |
| 5065 | socket_tcp.id = "LUA-TCP-CONN"; |
| 5066 | socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 5067 | socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 5068 | socket_tcp.pp_opts = 0; /* Remove proxy protocol. */ |
| 5069 | |
| 5070 | /* XXX: Copy default parameter from default server, |
| 5071 | * but the default server is not initialized. |
| 5072 | */ |
| 5073 | socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue; |
| 5074 | socket_tcp.minconn = socket_proxy.defsrv.minconn; |
| 5075 | socket_tcp.maxconn = socket_proxy.defsrv.maxconn; |
| 5076 | socket_tcp.slowstart = socket_proxy.defsrv.slowstart; |
| 5077 | socket_tcp.onerror = socket_proxy.defsrv.onerror; |
| 5078 | socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 5079 | socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 5080 | socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 5081 | socket_tcp.uweight = socket_proxy.defsrv.iweight; |
| 5082 | socket_tcp.iweight = socket_proxy.defsrv.iweight; |
| 5083 | |
| 5084 | socket_tcp.check.status = HCHK_STATUS_INI; |
| 5085 | socket_tcp.check.rise = socket_proxy.defsrv.check.rise; |
| 5086 | socket_tcp.check.fall = socket_proxy.defsrv.check.fall; |
| 5087 | socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */ |
| 5088 | socket_tcp.check.server = &socket_tcp; |
| 5089 | |
| 5090 | socket_tcp.agent.status = HCHK_STATUS_INI; |
| 5091 | socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise; |
| 5092 | socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall; |
| 5093 | socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */ |
| 5094 | socket_tcp.agent.server = &socket_tcp; |
| 5095 | |
| 5096 | socket_tcp.xprt = &raw_sock; |
| 5097 | |
| 5098 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 5099 | /* Init TCP server: unchanged parameters */ |
| 5100 | memset(&socket_ssl, 0, sizeof(socket_ssl)); |
| 5101 | socket_ssl.next = NULL; |
| 5102 | socket_ssl.proxy = &socket_proxy; |
| 5103 | socket_ssl.obj_type = OBJ_TYPE_SERVER; |
| 5104 | LIST_INIT(&socket_ssl.actconns); |
| 5105 | LIST_INIT(&socket_ssl.pendconns); |
| 5106 | socket_ssl.state = SRV_ST_RUNNING; /* early server setup */ |
| 5107 | socket_ssl.last_change = 0; |
| 5108 | socket_ssl.id = "LUA-SSL-CONN"; |
| 5109 | socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 5110 | socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 5111 | socket_ssl.pp_opts = 0; /* Remove proxy protocol. */ |
| 5112 | |
| 5113 | /* XXX: Copy default parameter from default server, |
| 5114 | * but the default server is not initialized. |
| 5115 | */ |
| 5116 | socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue; |
| 5117 | socket_ssl.minconn = socket_proxy.defsrv.minconn; |
| 5118 | socket_ssl.maxconn = socket_proxy.defsrv.maxconn; |
| 5119 | socket_ssl.slowstart = socket_proxy.defsrv.slowstart; |
| 5120 | socket_ssl.onerror = socket_proxy.defsrv.onerror; |
| 5121 | socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 5122 | socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 5123 | socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 5124 | socket_ssl.uweight = socket_proxy.defsrv.iweight; |
| 5125 | socket_ssl.iweight = socket_proxy.defsrv.iweight; |
| 5126 | |
| 5127 | socket_ssl.check.status = HCHK_STATUS_INI; |
| 5128 | socket_ssl.check.rise = socket_proxy.defsrv.check.rise; |
| 5129 | socket_ssl.check.fall = socket_proxy.defsrv.check.fall; |
| 5130 | socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */ |
| 5131 | socket_ssl.check.server = &socket_ssl; |
| 5132 | |
| 5133 | socket_ssl.agent.status = HCHK_STATUS_INI; |
| 5134 | socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise; |
| 5135 | socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall; |
| 5136 | socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */ |
| 5137 | socket_ssl.agent.server = &socket_ssl; |
| 5138 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 5139 | socket_ssl.use_ssl = 1; |
| 5140 | socket_ssl.xprt = &ssl_sock; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 5141 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 5142 | for (idx = 0; args[idx] != NULL; idx++) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 5143 | if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */ |
| 5144 | /* |
| 5145 | * |
| 5146 | * If the keyword is not known, we can search in the registered |
| 5147 | * server keywords. This is usefull to configure special SSL |
| 5148 | * features like client certificates and ssl_verify. |
| 5149 | * |
| 5150 | */ |
| 5151 | tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error); |
| 5152 | if (tmp_error != 0) { |
| 5153 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 5154 | abort(); /* This must be never arrives because the command line |
| 5155 | not editable by the user. */ |
| 5156 | } |
| 5157 | idx += kw->skip; |
| 5158 | } |
| 5159 | } |
| 5160 | |
| 5161 | /* Initialize SSL server. */ |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 5162 | ssl_sock_prepare_srv_ctx(&socket_ssl, &socket_proxy); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 5163 | #endif |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 5164 | } |