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