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