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