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