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 | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 112 | /* Applet status flags */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 113 | #define APPLET_DONE 0x01 /* applet processing is done. */ |
| 114 | #define APPLET_100C 0x02 /* 100 continue expected. */ |
| 115 | #define APPLET_HDR_SENT 0x04 /* Response header sent. */ |
| 116 | #define APPLET_CHUNKED 0x08 /* Use transfer encoding chunked. */ |
| 117 | #define APPLET_LAST_CHK 0x10 /* Last chunk sent. */ |
| 118 | |
| 119 | #define HTTP_100C "HTTP/1.1 100 Continue\r\n\r\n" |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 120 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 121 | /* The main Lua execution context. */ |
| 122 | struct hlua gL; |
| 123 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 124 | /* This is the memory pool containing all the signal structs. These |
| 125 | * struct are used to store each requiered signal between two tasks. |
| 126 | */ |
| 127 | struct pool_head *pool2_hlua_com; |
| 128 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 129 | /* Used for Socket connection. */ |
| 130 | static struct proxy socket_proxy; |
| 131 | static struct server socket_tcp; |
| 132 | #ifdef USE_OPENSSL |
| 133 | static struct server socket_ssl; |
| 134 | #endif |
| 135 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 136 | /* List head of the function called at the initialisation time. */ |
| 137 | struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions); |
| 138 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 139 | /* The following variables contains the reference of the different |
| 140 | * Lua classes. These references are useful for identify metadata |
| 141 | * associated with an object. |
| 142 | */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 143 | static int class_txn_ref; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 144 | static int class_socket_ref; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 145 | static int class_channel_ref; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 146 | static int class_fetches_ref; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 147 | static int class_converters_ref; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 148 | static int class_http_ref; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 149 | static int class_map_ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 150 | static int class_applet_tcp_ref; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 151 | static int class_applet_http_ref; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 152 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 153 | /* Global Lua execution timeout. By default Lua, execution linked |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 154 | * with stream (actions, sample-fetches and converters) have a |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 155 | * short timeout. Lua linked with tasks doesn't have a timeout |
| 156 | * because a task may remain alive during all the haproxy execution. |
| 157 | */ |
| 158 | static unsigned int hlua_timeout_session = 4000; /* session timeout. */ |
| 159 | static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 160 | static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 161 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 162 | /* Interrupts the Lua processing each "hlua_nb_instruction" instructions. |
| 163 | * it is used for preventing infinite loops. |
| 164 | * |
| 165 | * I test the scheer with an infinite loop containing one incrementation |
| 166 | * and one test. I run this loop between 10 seconds, I raise a ceil of |
| 167 | * 710M loops from one interrupt each 9000 instructions, so I fix the value |
| 168 | * to one interrupt each 10 000 instructions. |
| 169 | * |
| 170 | * configured | Number of |
| 171 | * instructions | loops executed |
| 172 | * between two | in milions |
| 173 | * forced yields | |
| 174 | * ---------------+--------------- |
| 175 | * 10 | 160 |
| 176 | * 500 | 670 |
| 177 | * 1000 | 680 |
| 178 | * 5000 | 700 |
| 179 | * 7000 | 700 |
| 180 | * 8000 | 700 |
| 181 | * 9000 | 710 <- ceil |
| 182 | * 10000 | 710 |
| 183 | * 100000 | 710 |
| 184 | * 1000000 | 710 |
| 185 | * |
| 186 | */ |
| 187 | static unsigned int hlua_nb_instruction = 10000; |
| 188 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 189 | /* Descriptor for the memory allocation state. If limit is not null, it will |
| 190 | * be enforced on any memory allocation. |
| 191 | */ |
| 192 | struct hlua_mem_allocator { |
| 193 | size_t allocated; |
| 194 | size_t limit; |
| 195 | }; |
| 196 | |
| 197 | static struct hlua_mem_allocator hlua_global_allocator; |
| 198 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 199 | static const char error_500[] = |
| 200 | "HTTP/1.0 500 Server Error\r\n" |
| 201 | "Cache-Control: no-cache\r\n" |
| 202 | "Connection: close\r\n" |
| 203 | "Content-Type: text/html\r\n" |
| 204 | "\r\n" |
| 205 | "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n"; |
| 206 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 207 | /* These functions converts types between HAProxy internal args or |
| 208 | * sample and LUA types. Another function permits to check if the |
| 209 | * LUA stack contains arguments according with an required ARG_T |
| 210 | * format. |
| 211 | */ |
| 212 | static int hlua_arg2lua(lua_State *L, const struct arg *arg); |
| 213 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 214 | __LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp, |
| 215 | unsigned int mask, struct proxy *p); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 216 | static int hlua_smp2lua(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 217 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 218 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp); |
| 219 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 220 | __LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg); |
| 221 | |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 222 | #define SEND_ERR(__be, __fmt, __args...) \ |
| 223 | do { \ |
| 224 | send_log(__be, LOG_ERR, __fmt, ## __args); \ |
| 225 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \ |
| 226 | Alert(__fmt, ## __args); \ |
| 227 | } while (0) |
| 228 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 229 | /* Used to check an Lua function type in the stack. It creates and |
| 230 | * returns a reference of the function. This function throws an |
| 231 | * error if the rgument is not a "function". |
| 232 | */ |
| 233 | __LJMP unsigned int hlua_checkfunction(lua_State *L, int argno) |
| 234 | { |
| 235 | if (!lua_isfunction(L, argno)) { |
| 236 | const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, -1)); |
| 237 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 238 | } |
| 239 | lua_pushvalue(L, argno); |
| 240 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 241 | } |
| 242 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 243 | /* Return the string that is of the top of the stack. */ |
| 244 | const char *hlua_get_top_error_string(lua_State *L) |
| 245 | { |
| 246 | if (lua_gettop(L) < 1) |
| 247 | return "unknown error"; |
| 248 | if (lua_type(L, -1) != LUA_TSTRING) |
| 249 | return "unknown error"; |
| 250 | return lua_tostring(L, -1); |
| 251 | } |
| 252 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 253 | /* The three following functions are useful for adding entries |
| 254 | * in a table. These functions takes a string and respectively an |
| 255 | * integer, a string or a function and add it to the table in the |
| 256 | * top of the stack. |
| 257 | * |
| 258 | * These functions throws an error if no more stack size is |
| 259 | * available. |
| 260 | */ |
| 261 | __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] | 262 | int value) |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 263 | { |
| 264 | if (!lua_checkstack(L, 2)) |
| 265 | WILL_LJMP(luaL_error(L, "full stack")); |
| 266 | lua_pushstring(L, name); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 267 | lua_pushinteger(L, value); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 268 | lua_rawset(L, -3); |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 269 | } |
| 270 | __LJMP static inline void hlua_class_const_str(lua_State *L, const char *name, |
| 271 | const char *value) |
| 272 | { |
| 273 | if (!lua_checkstack(L, 2)) |
| 274 | WILL_LJMP(luaL_error(L, "full stack")); |
| 275 | lua_pushstring(L, name); |
| 276 | lua_pushstring(L, value); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 277 | lua_rawset(L, -3); |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 278 | } |
| 279 | __LJMP static inline void hlua_class_function(lua_State *L, const char *name, |
| 280 | int (*function)(lua_State *L)) |
| 281 | { |
| 282 | if (!lua_checkstack(L, 2)) |
| 283 | WILL_LJMP(luaL_error(L, "full stack")); |
| 284 | lua_pushstring(L, name); |
| 285 | lua_pushcclosure(L, function, 0); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 286 | lua_rawset(L, -3); |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 287 | } |
| 288 | |
Thierry FOURNIER | d2a3dcc | 2015-09-18 07:35:06 +0200 | [diff] [blame] | 289 | __LJMP static int hlua_dump_object(struct lua_State *L) |
| 290 | { |
| 291 | const char *name = (const char *)lua_tostring(L, lua_upvalueindex(1)); |
| 292 | lua_pushfstring(L, "HAProxy class %s", name); |
| 293 | return 1; |
| 294 | } |
| 295 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 296 | /* This function check the number of arguments available in the |
| 297 | * stack. If the number of arguments available is not the same |
| 298 | * then <nb> an error is throwed. |
| 299 | */ |
| 300 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 301 | { |
| 302 | if (lua_gettop(L) == nb) |
| 303 | return; |
| 304 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 305 | } |
| 306 | |
| 307 | /* Return true if the data in stack[<ud>] is an object of |
| 308 | * type <class_ref>. |
| 309 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 310 | static int hlua_metaistype(lua_State *L, int ud, int class_ref) |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 311 | { |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 312 | if (!lua_getmetatable(L, ud)) |
| 313 | return 0; |
| 314 | |
| 315 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_ref); |
| 316 | if (!lua_rawequal(L, -1, -2)) { |
| 317 | lua_pop(L, 2); |
| 318 | return 0; |
| 319 | } |
| 320 | |
| 321 | lua_pop(L, 2); |
| 322 | return 1; |
| 323 | } |
| 324 | |
| 325 | /* Return an object of the expected type, or throws an error. */ |
| 326 | __LJMP static void *hlua_checkudata(lua_State *L, int ud, int class_ref) |
| 327 | { |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 328 | void *p; |
| 329 | |
| 330 | /* Check if the stack entry is an array. */ |
| 331 | if (!lua_istable(L, ud)) |
| 332 | WILL_LJMP(luaL_argerror(L, ud, NULL)); |
| 333 | /* Check if the metadata have the expected type. */ |
| 334 | if (!hlua_metaistype(L, ud, class_ref)) |
| 335 | WILL_LJMP(luaL_argerror(L, ud, NULL)); |
| 336 | /* Push on the stack at the entry [0] of the table. */ |
| 337 | lua_rawgeti(L, ud, 0); |
| 338 | /* Check if this entry is userdata. */ |
| 339 | p = lua_touserdata(L, -1); |
| 340 | if (!p) |
| 341 | WILL_LJMP(luaL_argerror(L, ud, NULL)); |
| 342 | /* Remove the entry returned by lua_rawgeti(). */ |
| 343 | lua_pop(L, 1); |
| 344 | /* Return the associated struct. */ |
| 345 | return p; |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | /* This fucntion push an error string prefixed by the file name |
| 349 | * and the line number where the error is encountered. |
| 350 | */ |
| 351 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 352 | { |
| 353 | va_list argp; |
| 354 | va_start(argp, fmt); |
| 355 | luaL_where(L, 1); |
| 356 | lua_pushvfstring(L, fmt, argp); |
| 357 | va_end(argp); |
| 358 | lua_concat(L, 2); |
| 359 | return 1; |
| 360 | } |
| 361 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 362 | /* This function register a new signal. "lua" is the current lua |
| 363 | * execution context. It contains a pointer to the associated task. |
| 364 | * "link" is a list head attached to an other task that must be wake |
| 365 | * the lua task if an event occurs. This is useful with external |
| 366 | * events like TCP I/O or sleep functions. This funcion allocate |
| 367 | * memory for the signal. |
| 368 | */ |
| 369 | static int hlua_com_new(struct hlua *lua, struct list *link) |
| 370 | { |
| 371 | struct hlua_com *com = pool_alloc2(pool2_hlua_com); |
| 372 | if (!com) |
| 373 | return 0; |
| 374 | LIST_ADDQ(&lua->com, &com->purge_me); |
| 375 | LIST_ADDQ(link, &com->wake_me); |
| 376 | com->task = lua->task; |
| 377 | return 1; |
| 378 | } |
| 379 | |
| 380 | /* This function purge all the pending signals when the LUA execution |
| 381 | * is finished. This prevent than a coprocess try to wake a deleted |
| 382 | * task. This function remove the memory associated to the signal. |
| 383 | */ |
| 384 | static void hlua_com_purge(struct hlua *lua) |
| 385 | { |
| 386 | struct hlua_com *com, *back; |
| 387 | |
| 388 | /* Delete all pending communication signals. */ |
| 389 | list_for_each_entry_safe(com, back, &lua->com, purge_me) { |
| 390 | LIST_DEL(&com->purge_me); |
| 391 | LIST_DEL(&com->wake_me); |
| 392 | pool_free2(pool2_hlua_com, com); |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | /* This function sends signals. It wakes all the tasks attached |
| 397 | * to a list head, and remove the signal, and free the used |
| 398 | * memory. |
| 399 | */ |
| 400 | static void hlua_com_wake(struct list *wake) |
| 401 | { |
| 402 | struct hlua_com *com, *back; |
| 403 | |
| 404 | /* Wake task and delete all pending communication signals. */ |
| 405 | list_for_each_entry_safe(com, back, wake, wake_me) { |
| 406 | LIST_DEL(&com->purge_me); |
| 407 | LIST_DEL(&com->wake_me); |
| 408 | task_wakeup(com->task, TASK_WOKEN_MSG); |
| 409 | pool_free2(pool2_hlua_com, com); |
| 410 | } |
| 411 | } |
| 412 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 413 | /* This functions is used with sample fetch and converters. It |
| 414 | * converts the HAProxy configuration argument in a lua stack |
| 415 | * values. |
| 416 | * |
| 417 | * It takes an array of "arg", and each entry of the array is |
| 418 | * converted and pushed in the LUA stack. |
| 419 | */ |
| 420 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 421 | { |
| 422 | switch (arg->type) { |
| 423 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 424 | case ARGT_TIME: |
| 425 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 426 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 427 | break; |
| 428 | |
| 429 | case ARGT_STR: |
| 430 | lua_pushlstring(L, arg->data.str.str, arg->data.str.len); |
| 431 | break; |
| 432 | |
| 433 | case ARGT_IPV4: |
| 434 | case ARGT_IPV6: |
| 435 | case ARGT_MSK4: |
| 436 | case ARGT_MSK6: |
| 437 | case ARGT_FE: |
| 438 | case ARGT_BE: |
| 439 | case ARGT_TAB: |
| 440 | case ARGT_SRV: |
| 441 | case ARGT_USR: |
| 442 | case ARGT_MAP: |
| 443 | default: |
| 444 | lua_pushnil(L); |
| 445 | break; |
| 446 | } |
| 447 | return 1; |
| 448 | } |
| 449 | |
| 450 | /* This function take one entrie in an LUA stack at the index "ud", |
| 451 | * and try to convert it in an HAProxy argument entry. This is useful |
| 452 | * with sample fetch wrappers. The input arguments are gived to the |
| 453 | * lua wrapper and converted as arg list by thi function. |
| 454 | */ |
| 455 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 456 | { |
| 457 | switch (lua_type(L, ud)) { |
| 458 | |
| 459 | case LUA_TNUMBER: |
| 460 | case LUA_TBOOLEAN: |
| 461 | arg->type = ARGT_SINT; |
| 462 | arg->data.sint = lua_tointeger(L, ud); |
| 463 | break; |
| 464 | |
| 465 | case LUA_TSTRING: |
| 466 | arg->type = ARGT_STR; |
| 467 | arg->data.str.str = (char *)lua_tolstring(L, ud, (size_t *)&arg->data.str.len); |
| 468 | break; |
| 469 | |
| 470 | case LUA_TUSERDATA: |
| 471 | case LUA_TNIL: |
| 472 | case LUA_TTABLE: |
| 473 | case LUA_TFUNCTION: |
| 474 | case LUA_TTHREAD: |
| 475 | case LUA_TLIGHTUSERDATA: |
| 476 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 477 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 478 | break; |
| 479 | } |
| 480 | return 1; |
| 481 | } |
| 482 | |
| 483 | /* the following functions are used to convert a struct sample |
| 484 | * in Lua type. This useful to convert the return of the |
| 485 | * fetchs or converters. |
| 486 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 487 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 488 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 489 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 490 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 491 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 492 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 493 | break; |
| 494 | |
| 495 | case SMP_T_BIN: |
| 496 | case SMP_T_STR: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 497 | 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] | 498 | break; |
| 499 | |
| 500 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 501 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 502 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 503 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 504 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 505 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 506 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 507 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 508 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 509 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 510 | case HTTP_METH_OTHER: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 511 | 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] | 512 | break; |
| 513 | default: |
| 514 | lua_pushnil(L); |
| 515 | break; |
| 516 | } |
| 517 | break; |
| 518 | |
| 519 | case SMP_T_IPV4: |
| 520 | case SMP_T_IPV6: |
| 521 | 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] | 522 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 523 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 524 | 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] | 525 | else |
| 526 | lua_pushnil(L); |
| 527 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 528 | default: |
| 529 | lua_pushnil(L); |
| 530 | break; |
| 531 | } |
| 532 | return 1; |
| 533 | } |
| 534 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 535 | /* the following functions are used to convert a struct sample |
| 536 | * in Lua strings. This is useful to convert the return of the |
| 537 | * fetchs or converters. |
| 538 | */ |
| 539 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 540 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 541 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 542 | |
| 543 | case SMP_T_BIN: |
| 544 | case SMP_T_STR: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 545 | 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] | 546 | break; |
| 547 | |
| 548 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 549 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 550 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 551 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 552 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 553 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 554 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 555 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 556 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 557 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 558 | case HTTP_METH_OTHER: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 559 | 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] | 560 | break; |
| 561 | default: |
| 562 | lua_pushstring(L, ""); |
| 563 | break; |
| 564 | } |
| 565 | break; |
| 566 | |
| 567 | case SMP_T_SINT: |
| 568 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 569 | case SMP_T_IPV4: |
| 570 | case SMP_T_IPV6: |
| 571 | 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] | 572 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 573 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 574 | 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] | 575 | else |
| 576 | lua_pushstring(L, ""); |
| 577 | break; |
| 578 | default: |
| 579 | lua_pushstring(L, ""); |
| 580 | break; |
| 581 | } |
| 582 | return 1; |
| 583 | } |
| 584 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 585 | /* the following functions are used to convert an Lua type in a |
| 586 | * struct sample. This is useful to provide data from a converter |
| 587 | * to the LUA code. |
| 588 | */ |
| 589 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 590 | { |
| 591 | switch (lua_type(L, ud)) { |
| 592 | |
| 593 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 594 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 595 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 596 | break; |
| 597 | |
| 598 | |
| 599 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 600 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 601 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 602 | break; |
| 603 | |
| 604 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 605 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 606 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 607 | 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] | 608 | break; |
| 609 | |
| 610 | case LUA_TUSERDATA: |
| 611 | case LUA_TNIL: |
| 612 | case LUA_TTABLE: |
| 613 | case LUA_TFUNCTION: |
| 614 | case LUA_TTHREAD: |
| 615 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 616 | case LUA_TNONE: |
| 617 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 618 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 619 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 620 | break; |
| 621 | } |
| 622 | return 1; |
| 623 | } |
| 624 | |
| 625 | /* This function check the "argp" builded by another conversion function |
| 626 | * is in accord with the expected argp defined by the "mask". The fucntion |
| 627 | * 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] | 628 | * |
| 629 | * This function assumes thant the argp argument contains ARGM_NBARGS + 1 |
| 630 | * entries. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 631 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 632 | __LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp, |
| 633 | unsigned int mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 634 | { |
| 635 | int min_arg; |
| 636 | int idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 637 | struct proxy *px; |
| 638 | char *sname, *pname; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 639 | |
| 640 | idx = 0; |
| 641 | min_arg = ARGM(mask); |
| 642 | mask >>= ARGM_BITS; |
| 643 | |
| 644 | while (1) { |
| 645 | |
| 646 | /* Check oversize. */ |
| 647 | if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) { |
Cyril Bonté | 577a36a | 2015-03-02 00:08:38 +0100 | [diff] [blame] | 648 | WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | /* Check for mandatory arguments. */ |
| 652 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 653 | if (idx < min_arg) { |
| 654 | |
| 655 | /* If miss other argument than the first one, we return an error. */ |
| 656 | if (idx > 0) |
| 657 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 658 | |
| 659 | /* If first argument have a certain type, some default values |
| 660 | * may be used. See the function smp_resolve_args(). |
| 661 | */ |
| 662 | switch (mask & ARGT_MASK) { |
| 663 | |
| 664 | case ARGT_FE: |
| 665 | if (!(p->cap & PR_CAP_FE)) |
| 666 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 667 | argp[idx].data.prx = p; |
| 668 | argp[idx].type = ARGT_FE; |
| 669 | argp[idx+1].type = ARGT_STOP; |
| 670 | break; |
| 671 | |
| 672 | case ARGT_BE: |
| 673 | if (!(p->cap & PR_CAP_BE)) |
| 674 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 675 | argp[idx].data.prx = p; |
| 676 | argp[idx].type = ARGT_BE; |
| 677 | argp[idx+1].type = ARGT_STOP; |
| 678 | break; |
| 679 | |
| 680 | case ARGT_TAB: |
| 681 | argp[idx].data.prx = p; |
| 682 | argp[idx].type = ARGT_TAB; |
| 683 | argp[idx+1].type = ARGT_STOP; |
| 684 | break; |
| 685 | |
| 686 | default: |
| 687 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 688 | break; |
| 689 | } |
| 690 | } |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 691 | return 0; |
| 692 | } |
| 693 | |
| 694 | /* Check for exceed the number of requiered argument. */ |
| 695 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 696 | argp[idx].type != ARGT_STOP) { |
| 697 | WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected")); |
| 698 | } |
| 699 | |
| 700 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 701 | argp[idx].type == ARGT_STOP) { |
| 702 | return 0; |
| 703 | } |
| 704 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 705 | /* Convert some argument types. */ |
| 706 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 707 | case ARGT_SINT: |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 708 | if (argp[idx].type != ARGT_SINT) |
| 709 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
| 710 | argp[idx].type = ARGT_SINT; |
| 711 | break; |
| 712 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 713 | case ARGT_TIME: |
| 714 | if (argp[idx].type != ARGT_SINT) |
| 715 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 716 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 717 | break; |
| 718 | |
| 719 | case ARGT_SIZE: |
| 720 | if (argp[idx].type != ARGT_SINT) |
| 721 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 722 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 723 | break; |
| 724 | |
| 725 | case ARGT_FE: |
| 726 | if (argp[idx].type != ARGT_STR) |
| 727 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
| 728 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 729 | trash.str[argp[idx].data.str.len] = 0; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 730 | argp[idx].data.prx = proxy_fe_by_name(trash.str); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 731 | if (!argp[idx].data.prx) |
| 732 | WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist")); |
| 733 | argp[idx].type = ARGT_FE; |
| 734 | break; |
| 735 | |
| 736 | case ARGT_BE: |
| 737 | if (argp[idx].type != ARGT_STR) |
| 738 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
| 739 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 740 | trash.str[argp[idx].data.str.len] = 0; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 741 | argp[idx].data.prx = proxy_be_by_name(trash.str); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 742 | if (!argp[idx].data.prx) |
| 743 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 744 | argp[idx].type = ARGT_BE; |
| 745 | break; |
| 746 | |
| 747 | case ARGT_TAB: |
| 748 | if (argp[idx].type != ARGT_STR) |
| 749 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
| 750 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 751 | trash.str[argp[idx].data.str.len] = 0; |
Willy Tarreau | e2dc1fa | 2015-05-26 12:08:07 +0200 | [diff] [blame] | 752 | argp[idx].data.prx = proxy_tbl_by_name(trash.str); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 753 | if (!argp[idx].data.prx) |
| 754 | WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist")); |
| 755 | argp[idx].type = ARGT_TAB; |
| 756 | break; |
| 757 | |
| 758 | case ARGT_SRV: |
| 759 | if (argp[idx].type != ARGT_STR) |
| 760 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
| 761 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 762 | trash.str[argp[idx].data.str.len] = 0; |
| 763 | sname = strrchr(trash.str, '/'); |
| 764 | if (sname) { |
| 765 | *sname++ = '\0'; |
| 766 | pname = trash.str; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 767 | px = proxy_be_by_name(pname); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 768 | if (!px) |
| 769 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 770 | } |
| 771 | else { |
| 772 | sname = trash.str; |
| 773 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 774 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 775 | argp[idx].data.srv = findserver(px, sname); |
| 776 | if (!argp[idx].data.srv) |
| 777 | WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist")); |
| 778 | argp[idx].type = ARGT_SRV; |
| 779 | break; |
| 780 | |
| 781 | case ARGT_IPV4: |
| 782 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 783 | trash.str[argp[idx].data.str.len] = 0; |
| 784 | if (inet_pton(AF_INET, trash.str, &argp[idx].data.ipv4)) |
| 785 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address")); |
| 786 | argp[idx].type = ARGT_IPV4; |
| 787 | break; |
| 788 | |
| 789 | case ARGT_MSK4: |
| 790 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 791 | trash.str[argp[idx].data.str.len] = 0; |
| 792 | if (!str2mask(trash.str, &argp[idx].data.ipv4)) |
| 793 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask")); |
| 794 | argp[idx].type = ARGT_MSK4; |
| 795 | break; |
| 796 | |
| 797 | case ARGT_IPV6: |
| 798 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 799 | trash.str[argp[idx].data.str.len] = 0; |
| 800 | if (inet_pton(AF_INET6, trash.str, &argp[idx].data.ipv6)) |
| 801 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address")); |
| 802 | argp[idx].type = ARGT_IPV6; |
| 803 | break; |
| 804 | |
| 805 | case ARGT_MSK6: |
| 806 | case ARGT_MAP: |
| 807 | case ARGT_REG: |
| 808 | case ARGT_USR: |
| 809 | WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 810 | break; |
| 811 | } |
| 812 | |
| 813 | /* Check for type of argument. */ |
| 814 | if ((mask & ARGT_MASK) != argp[idx].type) { |
| 815 | const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 816 | arg_type_names[(mask & ARGT_MASK)], |
| 817 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 818 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 819 | } |
| 820 | |
| 821 | /* Next argument. */ |
| 822 | mask >>= ARGT_BITS; |
| 823 | idx++; |
| 824 | } |
| 825 | } |
| 826 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 827 | /* |
| 828 | * The following functions are used to make correspondance between the the |
| 829 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 830 | * |
| 831 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 832 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 833 | */ |
| 834 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 835 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 836 | struct hlua **hlua = lua_getextraspace(L); |
| 837 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 838 | } |
| 839 | static inline void hlua_sethlua(struct hlua *hlua) |
| 840 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 841 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 842 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 843 | } |
| 844 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 845 | /* This function is used to send logs. It try to send on screen (stderr) |
| 846 | * and on the default syslog server. |
| 847 | */ |
| 848 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 849 | { |
| 850 | struct tm tm; |
| 851 | char *p; |
| 852 | |
| 853 | /* Cleanup the log message. */ |
| 854 | p = trash.str; |
| 855 | for (; *msg != '\0'; msg++, p++) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 856 | if (p >= trash.str + trash.size - 1) { |
| 857 | /* Break the message if exceed the buffer size. */ |
| 858 | *(p-4) = ' '; |
| 859 | *(p-3) = '.'; |
| 860 | *(p-2) = '.'; |
| 861 | *(p-1) = '.'; |
| 862 | break; |
| 863 | } |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 864 | if (isprint(*msg)) |
| 865 | *p = *msg; |
| 866 | else |
| 867 | *p = '.'; |
| 868 | } |
| 869 | *p = '\0'; |
| 870 | |
Thierry FOURNIER | 5554e29 | 2015-09-09 11:21:37 +0200 | [diff] [blame] | 871 | send_log(px, level, "%s\n", trash.str); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 872 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 873 | get_localtime(date.tv_sec, &tm); |
| 874 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 875 | log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, |
| 876 | (int)getpid(), trash.str); |
| 877 | fflush(stderr); |
| 878 | } |
| 879 | } |
| 880 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 881 | /* This function just ensure that the yield will be always |
| 882 | * returned with a timeout and permit to set some flags |
| 883 | */ |
| 884 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 885 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 886 | { |
| 887 | struct hlua *hlua = hlua_gethlua(L); |
| 888 | |
| 889 | /* Set the wake timeout. If timeout is required, we set |
| 890 | * the expiration time. |
| 891 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 892 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 893 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 894 | hlua->flags |= flags; |
| 895 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 896 | /* Process the yield. */ |
| 897 | WILL_LJMP(lua_yieldk(L, nresults, ctx, k)); |
| 898 | } |
| 899 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 900 | /* This function initialises the Lua environment stored in the stream. |
| 901 | * 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] | 902 | * 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] | 903 | * |
| 904 | * This function is particular. it initialises a new Lua thread. If the |
| 905 | * initialisation fails (example: out of memory error), the lua function |
| 906 | * throws an error (longjmp). |
| 907 | * |
| 908 | * This function manipulates two Lua stack: the main and the thread. Only |
| 909 | * the main stack can fail. The thread is not manipulated. This function |
| 910 | * MUST NOT manipulate the created thread stack state, because is not |
| 911 | * proctected agains error throwed by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 912 | */ |
| 913 | int hlua_ctx_init(struct hlua *lua, struct task *task) |
| 914 | { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 915 | if (!SET_SAFE_LJMP(gL.T)) { |
| 916 | lua->Tref = LUA_REFNIL; |
| 917 | return 0; |
| 918 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 919 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 920 | lua->flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 921 | LIST_INIT(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 922 | lua->T = lua_newthread(gL.T); |
| 923 | if (!lua->T) { |
| 924 | lua->Tref = LUA_REFNIL; |
| 925 | return 0; |
| 926 | } |
| 927 | hlua_sethlua(lua); |
| 928 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 929 | lua->task = task; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 930 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 931 | return 1; |
| 932 | } |
| 933 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 934 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 935 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 936 | * is not freed. |
| 937 | */ |
| 938 | void hlua_ctx_destroy(struct hlua *lua) |
| 939 | { |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 940 | if (!lua->T) |
| 941 | return; |
| 942 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 943 | /* Purge all the pending signals. */ |
| 944 | hlua_com_purge(lua); |
| 945 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 946 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 947 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 948 | |
| 949 | /* Forces a garbage collecting process. If the Lua program is finished |
| 950 | * without error, we run the GC on the thread pointer. Its freed all |
| 951 | * the unused memory. |
| 952 | * If the thread is finnish with an error or is currently yielded, |
| 953 | * it seems that the GC applied on the thread doesn't clean anything, |
| 954 | * so e run the GC on the main thread. |
| 955 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 956 | * the garbage collection. |
| 957 | */ |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 958 | if (lua->flags & HLUA_MUST_GC) { |
| 959 | lua_gc(lua->T, LUA_GCCOLLECT, 0); |
| 960 | if (lua_status(lua->T) != LUA_OK) |
| 961 | lua_gc(gL.T, LUA_GCCOLLECT, 0); |
| 962 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 963 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 964 | lua->T = NULL; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 965 | } |
| 966 | |
| 967 | /* This function is used to restore the Lua context when a coroutine |
| 968 | * fails. This function copy the common memory between old coroutine |
| 969 | * and the new coroutine. The old coroutine is destroyed, and its |
| 970 | * replaced by the new coroutine. |
| 971 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 972 | * as string error message and it is copied in the new stack. |
| 973 | */ |
| 974 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 975 | { |
| 976 | lua_State *T; |
| 977 | int new_ref; |
| 978 | |
| 979 | /* Renew the main LUA stack doesn't have sense. */ |
| 980 | if (lua == &gL) |
| 981 | return 0; |
| 982 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 983 | /* New Lua coroutine. */ |
| 984 | T = lua_newthread(gL.T); |
| 985 | if (!T) |
| 986 | return 0; |
| 987 | |
| 988 | /* Copy last error message. */ |
| 989 | if (keep_msg) |
| 990 | lua_xmove(lua->T, T, 1); |
| 991 | |
| 992 | /* Copy data between the coroutines. */ |
| 993 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 994 | lua_xmove(lua->T, T, 1); |
| 995 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */ |
| 996 | |
| 997 | /* Destroy old data. */ |
| 998 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 999 | |
| 1000 | /* The thread is garbage collected by Lua. */ |
| 1001 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 1002 | |
| 1003 | /* Fill the struct with the new coroutine values. */ |
| 1004 | lua->Mref = new_ref; |
| 1005 | lua->T = T; |
| 1006 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 1007 | |
| 1008 | /* Set context. */ |
| 1009 | hlua_sethlua(lua); |
| 1010 | |
| 1011 | return 1; |
| 1012 | } |
| 1013 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1014 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1015 | { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1016 | struct hlua *hlua = hlua_gethlua(L); |
| 1017 | |
| 1018 | /* Lua cannot yield when its returning from a function, |
| 1019 | * so, we can fix the interrupt hook to 1 instruction, |
| 1020 | * expecting that the function is finnished. |
| 1021 | */ |
| 1022 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1023 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1024 | return; |
| 1025 | } |
| 1026 | |
| 1027 | /* restore the interrupt condition. */ |
| 1028 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1029 | |
| 1030 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1031 | * If the state is not yieldable, trying yield causes an error. |
| 1032 | */ |
| 1033 | if (lua_isyieldable(L)) |
| 1034 | WILL_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
| 1035 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 1036 | /* If we cannot yield, update the clock and check the timeout. */ |
| 1037 | tv_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1038 | hlua->run_time += now_ms - hlua->start_time; |
| 1039 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1040 | lua_pushfstring(L, "execution timeout"); |
| 1041 | WILL_LJMP(lua_error(L)); |
| 1042 | } |
| 1043 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1044 | /* Update the start time. */ |
| 1045 | hlua->start_time = now_ms; |
| 1046 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1047 | /* Try to interrupt the process at the end of the current |
| 1048 | * unyieldable function. |
| 1049 | */ |
| 1050 | 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] | 1051 | } |
| 1052 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1053 | /* This function start or resumes the Lua stack execution. If the flag |
| 1054 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1055 | * The function return an error. |
| 1056 | * |
| 1057 | * The function can returns 4 values: |
| 1058 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1059 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1060 | * task wakeup. |
| 1061 | * - HLUA_E_ERRMSG : An error has occured, an error message is set in |
| 1062 | * the top of the stack. |
| 1063 | * - HLUA_E_ERR : An error has occured without error message. |
| 1064 | * |
| 1065 | * If an error occured, the stack is renewed and it is ready to run new |
| 1066 | * LUA code. |
| 1067 | */ |
| 1068 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1069 | { |
| 1070 | int ret; |
| 1071 | const char *msg; |
| 1072 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1073 | /* Initialise run time counter. */ |
| 1074 | if (!HLUA_IS_RUNNING(lua)) |
| 1075 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1076 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1077 | resume_execution: |
| 1078 | |
| 1079 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1080 | * instructions. it is used for preventing infinite loops. |
| 1081 | */ |
| 1082 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1083 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1084 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1085 | HLUA_SET_RUN(lua); |
| 1086 | HLUA_CLR_CTRLYIELD(lua); |
| 1087 | HLUA_CLR_WAKERESWR(lua); |
| 1088 | HLUA_CLR_WAKEREQWR(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1089 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1090 | /* Update the start time. */ |
| 1091 | lua->start_time = now_ms; |
| 1092 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1093 | /* Call the function. */ |
| 1094 | ret = lua_resume(lua->T, gL.T, lua->nargs); |
| 1095 | switch (ret) { |
| 1096 | |
| 1097 | case LUA_OK: |
| 1098 | ret = HLUA_E_OK; |
| 1099 | break; |
| 1100 | |
| 1101 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1102 | /* Check if the execution timeout is expired. It it is the case, we |
| 1103 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1104 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1105 | tv_update_date(0, 1); |
| 1106 | lua->run_time += now_ms - lua->start_time; |
| 1107 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1108 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1109 | if (!lua_checkstack(lua->T, 1)) { |
| 1110 | ret = HLUA_E_ERR; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1111 | break; |
| 1112 | } |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1113 | lua_pushfstring(lua->T, "execution timeout"); |
| 1114 | ret = HLUA_E_ERRMSG; |
| 1115 | break; |
| 1116 | } |
| 1117 | /* Process the forced yield. if the general yield is not allowed or |
| 1118 | * if no task were associated this the current Lua execution |
| 1119 | * coroutine, we resume the execution. Else we want to return in the |
| 1120 | * scheduler and we want to be waked up again, to continue the |
| 1121 | * current Lua execution. So we schedule our own task. |
| 1122 | */ |
| 1123 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1124 | if (!yield_allowed || !lua->task) |
| 1125 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1126 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1127 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1128 | if (!yield_allowed) { |
| 1129 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1130 | if (!lua_checkstack(lua->T, 1)) { |
| 1131 | ret = HLUA_E_ERR; |
| 1132 | break; |
| 1133 | } |
| 1134 | lua_pushfstring(lua->T, "yield not allowed"); |
| 1135 | ret = HLUA_E_ERRMSG; |
| 1136 | break; |
| 1137 | } |
| 1138 | ret = HLUA_E_AGAIN; |
| 1139 | break; |
| 1140 | |
| 1141 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1142 | |
| 1143 | /* Special exit case. The traditionnal exit is returned as an error |
| 1144 | * because the errors ares the only one mean to return immediately |
| 1145 | * from and lua execution. |
| 1146 | */ |
| 1147 | if (lua->flags & HLUA_EXIT) { |
| 1148 | ret = HLUA_E_OK; |
Thierry FOURNIER | e1587b3 | 2015-08-28 09:54:13 +0200 | [diff] [blame] | 1149 | hlua_ctx_renew(lua, 0); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1150 | break; |
| 1151 | } |
| 1152 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1153 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1154 | if (!lua_checkstack(lua->T, 1)) { |
| 1155 | ret = HLUA_E_ERR; |
| 1156 | break; |
| 1157 | } |
| 1158 | msg = lua_tostring(lua->T, -1); |
| 1159 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1160 | lua_pop(lua->T, 1); |
| 1161 | if (msg) |
| 1162 | lua_pushfstring(lua->T, "runtime error: %s", msg); |
| 1163 | else |
| 1164 | lua_pushfstring(lua->T, "unknown runtime error"); |
| 1165 | ret = HLUA_E_ERRMSG; |
| 1166 | break; |
| 1167 | |
| 1168 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1169 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1170 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1171 | if (!lua_checkstack(lua->T, 1)) { |
| 1172 | ret = HLUA_E_ERR; |
| 1173 | break; |
| 1174 | } |
| 1175 | lua_pushfstring(lua->T, "out of memory error"); |
| 1176 | ret = HLUA_E_ERRMSG; |
| 1177 | break; |
| 1178 | |
| 1179 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1180 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1181 | if (!lua_checkstack(lua->T, 1)) { |
| 1182 | ret = HLUA_E_ERR; |
| 1183 | break; |
| 1184 | } |
| 1185 | msg = lua_tostring(lua->T, -1); |
| 1186 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1187 | lua_pop(lua->T, 1); |
| 1188 | if (msg) |
| 1189 | lua_pushfstring(lua->T, "message handler error: %s", msg); |
| 1190 | else |
| 1191 | lua_pushfstring(lua->T, "message handler error"); |
| 1192 | ret = HLUA_E_ERRMSG; |
| 1193 | break; |
| 1194 | |
| 1195 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1196 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1197 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1198 | if (!lua_checkstack(lua->T, 1)) { |
| 1199 | ret = HLUA_E_ERR; |
| 1200 | break; |
| 1201 | } |
| 1202 | lua_pushfstring(lua->T, "unknonwn error"); |
| 1203 | ret = HLUA_E_ERRMSG; |
| 1204 | break; |
| 1205 | } |
| 1206 | |
Thierry FOURNIER | 6ab4d8e | 2015-09-27 22:17:19 +0200 | [diff] [blame] | 1207 | /* This GC permits to destroy some object when a Lua timeout strikes. */ |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1208 | if (lua->flags & HLUA_MUST_GC && |
| 1209 | ret != HLUA_E_AGAIN) |
Thierry FOURNIER | 6ab4d8e | 2015-09-27 22:17:19 +0200 | [diff] [blame] | 1210 | lua_gc(lua->T, LUA_GCCOLLECT, 0); |
| 1211 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1212 | switch (ret) { |
| 1213 | case HLUA_E_AGAIN: |
| 1214 | break; |
| 1215 | |
| 1216 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1217 | hlua_com_purge(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1218 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1219 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1220 | break; |
| 1221 | |
| 1222 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1223 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1224 | hlua_com_purge(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1225 | hlua_ctx_renew(lua, 0); |
| 1226 | break; |
| 1227 | |
| 1228 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1229 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1230 | hlua_com_purge(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1231 | break; |
| 1232 | } |
| 1233 | |
| 1234 | return ret; |
| 1235 | } |
| 1236 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1237 | /* This function exit the current code. */ |
| 1238 | __LJMP static int hlua_done(lua_State *L) |
| 1239 | { |
| 1240 | struct hlua *hlua = hlua_gethlua(L); |
| 1241 | |
| 1242 | hlua->flags |= HLUA_EXIT; |
| 1243 | WILL_LJMP(lua_error(L)); |
| 1244 | |
| 1245 | return 0; |
| 1246 | } |
| 1247 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1248 | /* This function is an LUA binding. It provides a function |
| 1249 | * for deleting ACL from a referenced ACL file. |
| 1250 | */ |
| 1251 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1252 | { |
| 1253 | const char *name; |
| 1254 | const char *key; |
| 1255 | struct pat_ref *ref; |
| 1256 | |
| 1257 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1258 | |
| 1259 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1260 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1261 | |
| 1262 | ref = pat_ref_lookup(name); |
| 1263 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1264 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1265 | |
| 1266 | pat_ref_delete(ref, key); |
| 1267 | return 0; |
| 1268 | } |
| 1269 | |
| 1270 | /* This function is an LUA binding. It provides a function |
| 1271 | * for deleting map entry from a referenced map file. |
| 1272 | */ |
| 1273 | static int hlua_del_map(lua_State *L) |
| 1274 | { |
| 1275 | const char *name; |
| 1276 | const char *key; |
| 1277 | struct pat_ref *ref; |
| 1278 | |
| 1279 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1280 | |
| 1281 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1282 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1283 | |
| 1284 | ref = pat_ref_lookup(name); |
| 1285 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1286 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1287 | |
| 1288 | pat_ref_delete(ref, key); |
| 1289 | return 0; |
| 1290 | } |
| 1291 | |
| 1292 | /* This function is an LUA binding. It provides a function |
| 1293 | * for adding ACL pattern from a referenced ACL file. |
| 1294 | */ |
| 1295 | static int hlua_add_acl(lua_State *L) |
| 1296 | { |
| 1297 | const char *name; |
| 1298 | const char *key; |
| 1299 | struct pat_ref *ref; |
| 1300 | |
| 1301 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1302 | |
| 1303 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1304 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1305 | |
| 1306 | ref = pat_ref_lookup(name); |
| 1307 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1308 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1309 | |
| 1310 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1311 | pat_ref_add(ref, key, NULL, NULL); |
| 1312 | return 0; |
| 1313 | } |
| 1314 | |
| 1315 | /* This function is an LUA binding. It provides a function |
| 1316 | * for setting map pattern and sample from a referenced map |
| 1317 | * file. |
| 1318 | */ |
| 1319 | static int hlua_set_map(lua_State *L) |
| 1320 | { |
| 1321 | const char *name; |
| 1322 | const char *key; |
| 1323 | const char *value; |
| 1324 | struct pat_ref *ref; |
| 1325 | |
| 1326 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1327 | |
| 1328 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1329 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1330 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1331 | |
| 1332 | ref = pat_ref_lookup(name); |
| 1333 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1334 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1335 | |
| 1336 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1337 | pat_ref_set(ref, key, value, NULL); |
| 1338 | else |
| 1339 | pat_ref_add(ref, key, value, NULL); |
| 1340 | return 0; |
| 1341 | } |
| 1342 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1343 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1344 | * an integer or user data. This data is associated with a metatable. This |
| 1345 | * metatable have an original version registred in the global context with |
| 1346 | * the name of the object (_G[<name>] = <metable> ). |
| 1347 | * |
| 1348 | * A metable is a table that modify the standard behavior of a standard |
| 1349 | * access to the associated data. The entries of this new metatable are |
| 1350 | * defined as is: |
| 1351 | * |
| 1352 | * http://lua-users.org/wiki/MetatableEvents |
| 1353 | * |
| 1354 | * __index |
| 1355 | * |
| 1356 | * we access an absent field in a table, the result is nil. This is |
| 1357 | * true, but it is not the whole truth. Actually, such access triggers |
| 1358 | * the interpreter to look for an __index metamethod: If there is no |
| 1359 | * such method, as usually happens, then the access results in nil; |
| 1360 | * otherwise, the metamethod will provide the result. |
| 1361 | * |
| 1362 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1363 | * the key does not appear in the table, but the metatable has an __index |
| 1364 | * property: |
| 1365 | * |
| 1366 | * - if the value is a function, the function is called, passing in the |
| 1367 | * table and the key; the return value of that function is returned as |
| 1368 | * the result. |
| 1369 | * |
| 1370 | * - if the value is another table, the value of the key in that table is |
| 1371 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1372 | * table's metatable has an __index property, then it continues on up) |
| 1373 | * |
| 1374 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1375 | * |
| 1376 | * http://www.lua.org/pil/13.4.1.html |
| 1377 | * |
| 1378 | * __newindex |
| 1379 | * |
| 1380 | * Like __index, but control property assignment. |
| 1381 | * |
| 1382 | * __mode - Control weak references. A string value with one or both |
| 1383 | * of the characters 'k' and 'v' which specifies that the the |
| 1384 | * keys and/or values in the table are weak references. |
| 1385 | * |
| 1386 | * __call - Treat a table like a function. When a table is followed by |
| 1387 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1388 | * a __call key pointing to a function, that function is invoked |
| 1389 | * (passing any specified arguments) and the return value is |
| 1390 | * returned. |
| 1391 | * |
| 1392 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1393 | * called, if the metatable for myTable has a __metatable |
| 1394 | * key, the value of that key is returned instead of the |
| 1395 | * actual metatable. |
| 1396 | * |
| 1397 | * __tostring - Control string representation. When the builtin |
| 1398 | * "tostring( myTable )" function is called, if the metatable |
| 1399 | * for myTable has a __tostring property set to a function, |
| 1400 | * that function is invoked (passing myTable to it) and the |
| 1401 | * return value is used as the string representation. |
| 1402 | * |
| 1403 | * __len - Control table length. When the table length is requested using |
| 1404 | * the length operator ( '#' ), if the metatable for myTable has |
| 1405 | * a __len key pointing to a function, that function is invoked |
| 1406 | * (passing myTable to it) and the return value used as the value |
| 1407 | * of "#myTable". |
| 1408 | * |
| 1409 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1410 | * collected, if the metatable has a __gc field pointing to a |
| 1411 | * function, that function is first invoked, passing the userdata |
| 1412 | * to it. The __gc metamethod is not called for tables. |
| 1413 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1414 | * |
| 1415 | * Special metamethods for redefining standard operators: |
| 1416 | * http://www.lua.org/pil/13.1.html |
| 1417 | * |
| 1418 | * __add "+" |
| 1419 | * __sub "-" |
| 1420 | * __mul "*" |
| 1421 | * __div "/" |
| 1422 | * __unm "!" |
| 1423 | * __pow "^" |
| 1424 | * __concat ".." |
| 1425 | * |
| 1426 | * Special methods for redfining standar relations |
| 1427 | * http://www.lua.org/pil/13.2.html |
| 1428 | * |
| 1429 | * __eq "==" |
| 1430 | * __lt "<" |
| 1431 | * __le "<=" |
| 1432 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1433 | |
| 1434 | /* |
| 1435 | * |
| 1436 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1437 | * Class Map |
| 1438 | * |
| 1439 | * |
| 1440 | */ |
| 1441 | |
| 1442 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1443 | * a class session, otherwise it throws an error. |
| 1444 | */ |
| 1445 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1446 | { |
| 1447 | return (struct map_descriptor *)MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
| 1448 | } |
| 1449 | |
| 1450 | /* This function is the map constructor. It don't need |
| 1451 | * the class Map object. It creates and return a new Map |
| 1452 | * object. It must be called only during "body" or "init" |
| 1453 | * context because it process some filesystem accesses. |
| 1454 | */ |
| 1455 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1456 | { |
| 1457 | const char *fn; |
| 1458 | int match = PAT_MATCH_STR; |
| 1459 | struct sample_conv conv; |
| 1460 | const char *file = ""; |
| 1461 | int line = 0; |
| 1462 | lua_Debug ar; |
| 1463 | char *err = NULL; |
| 1464 | struct arg args[2]; |
| 1465 | |
| 1466 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1467 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1468 | |
| 1469 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1470 | |
| 1471 | if (lua_gettop(L) >= 2) { |
| 1472 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1473 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1474 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1475 | } |
| 1476 | |
| 1477 | /* Get Lua filename and line number. */ |
| 1478 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1479 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1480 | if (ar.currentline > 0) { /* is there info? */ |
| 1481 | file = ar.short_src; |
| 1482 | line = ar.currentline; |
| 1483 | } |
| 1484 | } |
| 1485 | |
| 1486 | /* fill fake sample_conv struct. */ |
| 1487 | conv.kw = ""; /* unused. */ |
| 1488 | conv.process = NULL; /* unused. */ |
| 1489 | conv.arg_mask = 0; /* unused. */ |
| 1490 | conv.val_args = NULL; /* unused. */ |
| 1491 | conv.out_type = SMP_T_STR; |
| 1492 | conv.private = (void *)(long)match; |
| 1493 | switch (match) { |
| 1494 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1495 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1496 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1497 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1498 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1499 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1500 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1501 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1502 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1503 | default: |
| 1504 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1505 | } |
| 1506 | |
| 1507 | /* fill fake args. */ |
| 1508 | args[0].type = ARGT_STR; |
| 1509 | args[0].data.str.str = (char *)fn; |
| 1510 | args[1].type = ARGT_STOP; |
| 1511 | |
| 1512 | /* load the map. */ |
| 1513 | if (!sample_load_map(args, &conv, file, line, &err)) { |
| 1514 | /* error case: we cant use luaL_error because we must |
| 1515 | * free the err variable. |
| 1516 | */ |
| 1517 | luaL_where(L, 1); |
| 1518 | lua_pushfstring(L, "'new': %s.", err); |
| 1519 | lua_concat(L, 2); |
| 1520 | free(err); |
| 1521 | WILL_LJMP(lua_error(L)); |
| 1522 | } |
| 1523 | |
| 1524 | /* create the lua object. */ |
| 1525 | lua_newtable(L); |
| 1526 | lua_pushlightuserdata(L, args[0].data.map); |
| 1527 | lua_rawseti(L, -2, 0); |
| 1528 | |
| 1529 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1530 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1531 | lua_setmetatable(L, -2); |
| 1532 | |
| 1533 | |
| 1534 | return 1; |
| 1535 | } |
| 1536 | |
| 1537 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1538 | { |
| 1539 | struct map_descriptor *desc; |
| 1540 | struct pattern *pat; |
| 1541 | struct sample smp; |
| 1542 | |
| 1543 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1544 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1545 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1546 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1547 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1548 | } |
| 1549 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1550 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1551 | smp.flags = SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1552 | 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] | 1553 | } |
| 1554 | |
| 1555 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1556 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1557 | if (str) |
| 1558 | lua_pushstring(L, ""); |
| 1559 | else |
| 1560 | lua_pushnil(L); |
| 1561 | return 1; |
| 1562 | } |
| 1563 | |
| 1564 | /* 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] | 1565 | 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] | 1566 | return 1; |
| 1567 | } |
| 1568 | |
| 1569 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1570 | { |
| 1571 | return _hlua_map_lookup(L, 0); |
| 1572 | } |
| 1573 | |
| 1574 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1575 | { |
| 1576 | return _hlua_map_lookup(L, 1); |
| 1577 | } |
| 1578 | |
| 1579 | /* |
| 1580 | * |
| 1581 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1582 | * Class Socket |
| 1583 | * |
| 1584 | * |
| 1585 | */ |
| 1586 | |
| 1587 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1588 | { |
| 1589 | return (struct hlua_socket *)MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
| 1590 | } |
| 1591 | |
| 1592 | /* This function is the handler called for each I/O on the established |
| 1593 | * connection. It is used for notify space avalaible to send or data |
| 1594 | * received. |
| 1595 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1596 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1597 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1598 | struct stream_interface *si = appctx->owner; |
Willy Tarreau | 50fe03b | 2014-11-28 13:59:31 +0100 | [diff] [blame] | 1599 | struct connection *c = objt_conn(si_opposite(si)->end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1600 | |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1601 | /* If the connection object is not avalaible, close all the |
| 1602 | * streams and wakeup everithing waiting for. |
| 1603 | */ |
| 1604 | if (!c) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1605 | si_shutw(si); |
| 1606 | si_shutr(si); |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 1607 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1608 | hlua_com_wake(&appctx->ctx.hlua.wake_on_read); |
| 1609 | hlua_com_wake(&appctx->ctx.hlua.wake_on_write); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1610 | return; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1611 | } |
| 1612 | |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1613 | /* If we cant write, wakeup the pending write signals. */ |
| 1614 | if (channel_output_closed(si_ic(si))) |
| 1615 | hlua_com_wake(&appctx->ctx.hlua.wake_on_write); |
| 1616 | |
| 1617 | /* If we cant read, wakeup the pending read signals. */ |
| 1618 | if (channel_input_closed(si_oc(si))) |
| 1619 | hlua_com_wake(&appctx->ctx.hlua.wake_on_read); |
| 1620 | |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1621 | /* if the connection is not estabkished, inform the stream that we want |
| 1622 | * to be notified whenever the connection completes. |
| 1623 | */ |
| 1624 | if (!(c->flags & CO_FL_CONNECTED)) { |
| 1625 | si_applet_cant_get(si); |
| 1626 | si_applet_cant_put(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1627 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1628 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1629 | |
| 1630 | /* This function is called after the connect. */ |
| 1631 | appctx->ctx.hlua.connected = 1; |
| 1632 | |
| 1633 | /* Wake the tasks which wants to write if the buffer have avalaible space. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1634 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1635 | hlua_com_wake(&appctx->ctx.hlua.wake_on_write); |
| 1636 | |
| 1637 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1638 | if (!channel_is_empty(si_oc(si))) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1639 | hlua_com_wake(&appctx->ctx.hlua.wake_on_read); |
| 1640 | } |
| 1641 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1642 | /* This function is called when the "struct stream" is destroyed. |
| 1643 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1644 | * Wake all the pending signals. |
| 1645 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1646 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1647 | { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1648 | /* Remove my link in the original object. */ |
| 1649 | if (appctx->ctx.hlua.socket) |
| 1650 | appctx->ctx.hlua.socket->s = NULL; |
| 1651 | |
| 1652 | /* Wake all the task waiting for me. */ |
| 1653 | hlua_com_wake(&appctx->ctx.hlua.wake_on_read); |
| 1654 | hlua_com_wake(&appctx->ctx.hlua.wake_on_write); |
| 1655 | } |
| 1656 | |
| 1657 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1658 | * uses this object. If the stream does not exists, just quit. |
| 1659 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1660 | * pending signal can rest in the read and write lists. destroy |
| 1661 | * it. |
| 1662 | */ |
| 1663 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1664 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1665 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1666 | struct appctx *appctx; |
| 1667 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1668 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1669 | |
| 1670 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1671 | if (!socket->s) |
| 1672 | return 0; |
| 1673 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1674 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1675 | appctx = objt_appctx(socket->s->si[0].end); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1676 | stream_shutdown(socket->s, SF_ERR_KILLED); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1677 | socket->s = NULL; |
| 1678 | appctx->ctx.hlua.socket = NULL; |
| 1679 | |
| 1680 | return 0; |
| 1681 | } |
| 1682 | |
| 1683 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1684 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1685 | */ |
| 1686 | __LJMP static int hlua_socket_close(lua_State *L) |
| 1687 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1688 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1689 | struct appctx *appctx; |
| 1690 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1691 | MAY_LJMP(check_args(L, 1, "close")); |
| 1692 | |
| 1693 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1694 | if (!socket->s) |
| 1695 | return 0; |
| 1696 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1697 | /* Close the stream and remove the associated stop task. */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1698 | stream_shutdown(socket->s, SF_ERR_KILLED); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1699 | appctx = objt_appctx(socket->s->si[0].end); |
| 1700 | appctx->ctx.hlua.socket = NULL; |
| 1701 | socket->s = NULL; |
| 1702 | |
| 1703 | return 0; |
| 1704 | } |
| 1705 | |
| 1706 | /* This Lua function assumes that the stack contain three parameters. |
| 1707 | * 1 - USERDATA containing a struct socket |
| 1708 | * 2 - INTEGER with values of the macro defined below |
| 1709 | * If the integer is -1, we must read at most one line. |
| 1710 | * If the integer is -2, we ust read all the data until the |
| 1711 | * end of the stream. |
| 1712 | * If the integer is positive value, we must read a number of |
| 1713 | * bytes corresponding to this value. |
| 1714 | */ |
| 1715 | #define HLSR_READ_LINE (-1) |
| 1716 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1717 | __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] | 1718 | { |
| 1719 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1720 | int wanted = lua_tointeger(L, 2); |
| 1721 | struct hlua *hlua = hlua_gethlua(L); |
| 1722 | struct appctx *appctx; |
| 1723 | int len; |
| 1724 | int nblk; |
| 1725 | char *blk1; |
| 1726 | int len1; |
| 1727 | char *blk2; |
| 1728 | int len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1729 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1730 | struct channel *oc; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1731 | |
| 1732 | /* Check if this lua stack is schedulable. */ |
| 1733 | if (!hlua || !hlua->task) |
| 1734 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 1735 | "'frontend', 'backend' or 'task'")); |
| 1736 | |
| 1737 | /* check for connection closed. If some data where read, return it. */ |
| 1738 | if (!socket->s) |
| 1739 | goto connection_closed; |
| 1740 | |
Willy Tarreau | 94aa617 | 2015-03-13 14:19:06 +0100 | [diff] [blame] | 1741 | oc = &socket->s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1742 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1743 | /* Read line. */ |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1744 | nblk = bo_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1745 | if (nblk < 0) /* Connection close. */ |
| 1746 | goto connection_closed; |
| 1747 | if (nblk == 0) /* No data avalaible. */ |
| 1748 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1749 | |
| 1750 | /* remove final \r\n. */ |
| 1751 | if (nblk == 1) { |
| 1752 | if (blk1[len1-1] == '\n') { |
| 1753 | len1--; |
| 1754 | skip_at_end++; |
| 1755 | if (blk1[len1-1] == '\r') { |
| 1756 | len1--; |
| 1757 | skip_at_end++; |
| 1758 | } |
| 1759 | } |
| 1760 | } |
| 1761 | else { |
| 1762 | if (blk2[len2-1] == '\n') { |
| 1763 | len2--; |
| 1764 | skip_at_end++; |
| 1765 | if (blk2[len2-1] == '\r') { |
| 1766 | len2--; |
| 1767 | skip_at_end++; |
| 1768 | } |
| 1769 | } |
| 1770 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1771 | } |
| 1772 | |
| 1773 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1774 | /* Read all the available data. */ |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1775 | nblk = bo_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1776 | if (nblk < 0) /* Connection close. */ |
| 1777 | goto connection_closed; |
| 1778 | if (nblk == 0) /* No data avalaible. */ |
| 1779 | goto connection_empty; |
| 1780 | } |
| 1781 | |
| 1782 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1783 | /* Read a block of data. */ |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1784 | nblk = bo_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1785 | if (nblk < 0) /* Connection close. */ |
| 1786 | goto connection_closed; |
| 1787 | if (nblk == 0) /* No data avalaible. */ |
| 1788 | goto connection_empty; |
| 1789 | |
| 1790 | if (len1 > wanted) { |
| 1791 | nblk = 1; |
| 1792 | len1 = wanted; |
| 1793 | } if (nblk == 2 && len1 + len2 > wanted) |
| 1794 | len2 = wanted - len1; |
| 1795 | } |
| 1796 | |
| 1797 | len = len1; |
| 1798 | |
| 1799 | luaL_addlstring(&socket->b, blk1, len1); |
| 1800 | if (nblk == 2) { |
| 1801 | len += len2; |
| 1802 | luaL_addlstring(&socket->b, blk2, len2); |
| 1803 | } |
| 1804 | |
| 1805 | /* Consume data. */ |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1806 | bo_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1807 | |
| 1808 | /* Don't wait anything. */ |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 1809 | stream_int_notify(&socket->s->si[0]); |
| 1810 | stream_int_update_applet(&socket->s->si[0]); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1811 | |
| 1812 | /* If the pattern reclaim to read all the data |
| 1813 | * in the connection, got out. |
| 1814 | */ |
| 1815 | if (wanted == HLSR_READ_ALL) |
| 1816 | goto connection_empty; |
| 1817 | else if (wanted >= 0 && len < wanted) |
| 1818 | goto connection_empty; |
| 1819 | |
| 1820 | /* Return result. */ |
| 1821 | luaL_pushresult(&socket->b); |
| 1822 | return 1; |
| 1823 | |
| 1824 | connection_closed: |
| 1825 | |
| 1826 | /* If the buffer containds data. */ |
| 1827 | if (socket->b.n > 0) { |
| 1828 | luaL_pushresult(&socket->b); |
| 1829 | return 1; |
| 1830 | } |
| 1831 | lua_pushnil(L); |
| 1832 | lua_pushstring(L, "connection closed."); |
| 1833 | return 2; |
| 1834 | |
| 1835 | connection_empty: |
| 1836 | |
| 1837 | appctx = objt_appctx(socket->s->si[0].end); |
| 1838 | if (!hlua_com_new(hlua, &appctx->ctx.hlua.wake_on_read)) |
| 1839 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 1840 | 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] | 1841 | return 0; |
| 1842 | } |
| 1843 | |
| 1844 | /* This Lus function gets two parameters. The first one can be string |
| 1845 | * or a number. If the string is "*l", the user require one line. If |
| 1846 | * the string is "*a", the user require all the content of the stream. |
| 1847 | * If the value is a number, the user require a number of bytes equal |
| 1848 | * to the value. The default value is "*l" (a line). |
| 1849 | * |
| 1850 | * This paraeter with a variable type is converted in integer. This |
| 1851 | * integer takes this values: |
| 1852 | * -1 : read a line |
| 1853 | * -2 : read all the stream |
| 1854 | * >0 : amount if bytes. |
| 1855 | * |
| 1856 | * The second parameter is optinal. It contains a string that must be |
| 1857 | * concatenated with the read data. |
| 1858 | */ |
| 1859 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 1860 | { |
| 1861 | int wanted = HLSR_READ_LINE; |
| 1862 | const char *pattern; |
| 1863 | int type; |
| 1864 | char *error; |
| 1865 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1866 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1867 | |
| 1868 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 1869 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 1870 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1871 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1872 | |
| 1873 | /* check for pattern. */ |
| 1874 | if (lua_gettop(L) >= 2) { |
| 1875 | type = lua_type(L, 2); |
| 1876 | if (type == LUA_TSTRING) { |
| 1877 | pattern = lua_tostring(L, 2); |
| 1878 | if (strcmp(pattern, "*a") == 0) |
| 1879 | wanted = HLSR_READ_ALL; |
| 1880 | else if (strcmp(pattern, "*l") == 0) |
| 1881 | wanted = HLSR_READ_LINE; |
| 1882 | else { |
| 1883 | wanted = strtoll(pattern, &error, 10); |
| 1884 | if (*error != '\0') |
| 1885 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 1886 | } |
| 1887 | } |
| 1888 | else if (type == LUA_TNUMBER) { |
| 1889 | wanted = lua_tointeger(L, 2); |
| 1890 | if (wanted < 0) |
| 1891 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 1892 | } |
| 1893 | } |
| 1894 | |
| 1895 | /* Set pattern. */ |
| 1896 | lua_pushinteger(L, wanted); |
| 1897 | lua_replace(L, 2); |
| 1898 | |
| 1899 | /* init bufffer, and fiil it wih prefix. */ |
| 1900 | luaL_buffinit(L, &socket->b); |
| 1901 | |
| 1902 | /* Check prefix. */ |
| 1903 | if (lua_gettop(L) >= 3) { |
| 1904 | if (lua_type(L, 3) != LUA_TSTRING) |
| 1905 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 1906 | pattern = lua_tolstring(L, 3, &len); |
| 1907 | luaL_addlstring(&socket->b, pattern, len); |
| 1908 | } |
| 1909 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1910 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1911 | } |
| 1912 | |
| 1913 | /* Write the Lua input string in the output buffer. |
| 1914 | * This fucntion returns a yield if no space are available. |
| 1915 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1916 | 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] | 1917 | { |
| 1918 | struct hlua_socket *socket; |
| 1919 | struct hlua *hlua = hlua_gethlua(L); |
| 1920 | struct appctx *appctx; |
| 1921 | size_t buf_len; |
| 1922 | const char *buf; |
| 1923 | int len; |
| 1924 | int send_len; |
| 1925 | int sent; |
| 1926 | |
| 1927 | /* Check if this lua stack is schedulable. */ |
| 1928 | if (!hlua || !hlua->task) |
| 1929 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 1930 | "'frontend', 'backend' or 'task'")); |
| 1931 | |
| 1932 | /* Get object */ |
| 1933 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1934 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1935 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1936 | |
| 1937 | /* Check for connection close. */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1938 | if (!socket->s || channel_output_closed(&socket->s->req)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1939 | lua_pushinteger(L, -1); |
| 1940 | return 1; |
| 1941 | } |
| 1942 | |
| 1943 | /* Update the input buffer data. */ |
| 1944 | buf += sent; |
| 1945 | send_len = buf_len - sent; |
| 1946 | |
| 1947 | /* All the data are sent. */ |
| 1948 | if (sent >= buf_len) |
| 1949 | return 1; /* Implicitly return the length sent. */ |
| 1950 | |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 1951 | /* Check if the buffer is avalaible because HAProxy doesn't allocate |
| 1952 | * the request buffer if its not required. |
| 1953 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1954 | if (socket->s->req.buf->size == 0) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1955 | if (!stream_alloc_recv_buffer(&socket->s->req)) { |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 1956 | socket->s->si[0].flags |= SI_FL_WAIT_ROOM; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 1957 | goto hlua_socket_write_yield_return; |
| 1958 | } |
| 1959 | } |
| 1960 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1961 | /* Check for avalaible space. */ |
Willy Tarreau | 94aa617 | 2015-03-13 14:19:06 +0100 | [diff] [blame] | 1962 | len = buffer_total_space(socket->s->req.buf); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1963 | if (len <= 0) |
| 1964 | goto hlua_socket_write_yield_return; |
| 1965 | |
| 1966 | /* send data */ |
| 1967 | if (len < send_len) |
| 1968 | send_len = len; |
Willy Tarreau | 94aa617 | 2015-03-13 14:19:06 +0100 | [diff] [blame] | 1969 | len = bi_putblk(&socket->s->req, buf+sent, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1970 | |
| 1971 | /* "Not enough space" (-1), "Buffer too little to contain |
| 1972 | * the data" (-2) are not expected because the available length |
| 1973 | * is tested. |
| 1974 | * Other unknown error are also not expected. |
| 1975 | */ |
| 1976 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 1977 | if (len == -1) |
Willy Tarreau | 94aa617 | 2015-03-13 14:19:06 +0100 | [diff] [blame] | 1978 | socket->s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 1979 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1980 | MAY_LJMP(hlua_socket_close(L)); |
| 1981 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1982 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1983 | return 1; |
| 1984 | } |
| 1985 | |
| 1986 | /* update buffers. */ |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 1987 | stream_int_notify(&socket->s->si[0]); |
| 1988 | stream_int_update_applet(&socket->s->si[0]); |
| 1989 | |
Willy Tarreau | 94aa617 | 2015-03-13 14:19:06 +0100 | [diff] [blame] | 1990 | socket->s->req.rex = TICK_ETERNITY; |
| 1991 | socket->s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1992 | |
| 1993 | /* Update length sent. */ |
| 1994 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1995 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1996 | |
| 1997 | /* All the data buffer is sent ? */ |
| 1998 | if (sent + len >= buf_len) |
| 1999 | return 1; |
| 2000 | |
| 2001 | hlua_socket_write_yield_return: |
| 2002 | appctx = objt_appctx(socket->s->si[0].end); |
| 2003 | if (!hlua_com_new(hlua, &appctx->ctx.hlua.wake_on_write)) |
| 2004 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 2005 | 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] | 2006 | return 0; |
| 2007 | } |
| 2008 | |
| 2009 | /* This function initiate the send of data. It just check the input |
| 2010 | * parameters and push an integer in the Lua stack that contain the |
| 2011 | * amount of data writed in the buffer. This is used by the function |
| 2012 | * "hlua_socket_write_yield" that can yield. |
| 2013 | * |
| 2014 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2015 | * the associated object. The second is a string buffer. The third is |
| 2016 | * a facultative integer that represents where is the buffer position |
| 2017 | * of the start of the data that can send. The first byte is the |
| 2018 | * position "1". The default value is "1". The fourth argument is a |
| 2019 | * facultative integer that represents where is the buffer position |
| 2020 | * of the end of the data that can send. The default is the last byte. |
| 2021 | */ |
| 2022 | static int hlua_socket_send(struct lua_State *L) |
| 2023 | { |
| 2024 | int i; |
| 2025 | int j; |
| 2026 | const char *buf; |
| 2027 | size_t buf_len; |
| 2028 | |
| 2029 | /* Check number of arguments. */ |
| 2030 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2031 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2032 | |
| 2033 | /* Get the string. */ |
| 2034 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2035 | |
| 2036 | /* Get and check j. */ |
| 2037 | if (lua_gettop(L) == 4) { |
| 2038 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2039 | if (j < 0) |
| 2040 | j = buf_len + j + 1; |
| 2041 | if (j > buf_len) |
| 2042 | j = buf_len + 1; |
| 2043 | lua_pop(L, 1); |
| 2044 | } |
| 2045 | else |
| 2046 | j = buf_len; |
| 2047 | |
| 2048 | /* Get and check i. */ |
| 2049 | if (lua_gettop(L) == 3) { |
| 2050 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2051 | if (i < 0) |
| 2052 | i = buf_len + i + 1; |
| 2053 | if (i > buf_len) |
| 2054 | i = buf_len + 1; |
| 2055 | lua_pop(L, 1); |
| 2056 | } else |
| 2057 | i = 1; |
| 2058 | |
| 2059 | /* Check bth i and j. */ |
| 2060 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2061 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2062 | return 1; |
| 2063 | } |
| 2064 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2065 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2066 | return 1; |
| 2067 | } |
| 2068 | if (i == 0) |
| 2069 | i = 1; |
| 2070 | if (j == 0) |
| 2071 | j = 1; |
| 2072 | |
| 2073 | /* Pop the string. */ |
| 2074 | lua_pop(L, 1); |
| 2075 | |
| 2076 | /* Update the buffer length. */ |
| 2077 | buf += i - 1; |
| 2078 | buf_len = j - i + 1; |
| 2079 | lua_pushlstring(L, buf, buf_len); |
| 2080 | |
| 2081 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2082 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2083 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2084 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2085 | } |
| 2086 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2087 | #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] | 2088 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2089 | { |
| 2090 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2091 | int ret; |
| 2092 | int len; |
| 2093 | char *p; |
| 2094 | |
| 2095 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2096 | if (ret <= 0) { |
| 2097 | lua_pushnil(L); |
| 2098 | return 1; |
| 2099 | } |
| 2100 | |
| 2101 | if (ret == AF_UNIX) { |
| 2102 | lua_pushstring(L, buffer+1); |
| 2103 | return 1; |
| 2104 | } |
| 2105 | else if (ret == AF_INET6) { |
| 2106 | buffer[0] = '['; |
| 2107 | len = strlen(buffer); |
| 2108 | buffer[len] = ']'; |
| 2109 | len++; |
| 2110 | buffer[len] = ':'; |
| 2111 | len++; |
| 2112 | p = buffer; |
| 2113 | } |
| 2114 | else if (ret == AF_INET) { |
| 2115 | p = buffer + 1; |
| 2116 | len = strlen(p); |
| 2117 | p[len] = ':'; |
| 2118 | len++; |
| 2119 | } |
| 2120 | else { |
| 2121 | lua_pushnil(L); |
| 2122 | return 1; |
| 2123 | } |
| 2124 | |
| 2125 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2126 | lua_pushnil(L); |
| 2127 | return 1; |
| 2128 | } |
| 2129 | |
| 2130 | lua_pushstring(L, p); |
| 2131 | return 1; |
| 2132 | } |
| 2133 | |
| 2134 | /* Returns information about the peer of the connection. */ |
| 2135 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2136 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2137 | struct hlua_socket *socket; |
| 2138 | struct connection *conn; |
| 2139 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2140 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2141 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2142 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2143 | |
| 2144 | /* Check if the tcp object is avalaible. */ |
| 2145 | if (!socket->s) { |
| 2146 | lua_pushnil(L); |
| 2147 | return 1; |
| 2148 | } |
| 2149 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2150 | conn = objt_conn(socket->s->si[1].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2151 | if (!conn) { |
| 2152 | lua_pushnil(L); |
| 2153 | return 1; |
| 2154 | } |
| 2155 | |
| 2156 | if (!(conn->flags & CO_FL_ADDR_TO_SET)) { |
| 2157 | unsigned int salen = sizeof(conn->addr.to); |
| 2158 | if (getpeername(conn->t.sock.fd, (struct sockaddr *)&conn->addr.to, &salen) == -1) { |
| 2159 | lua_pushnil(L); |
| 2160 | return 1; |
| 2161 | } |
| 2162 | conn->flags |= CO_FL_ADDR_TO_SET; |
| 2163 | } |
| 2164 | |
| 2165 | return MAY_LJMP(hlua_socket_info(L, &conn->addr.to)); |
| 2166 | } |
| 2167 | |
| 2168 | /* Returns information about my connection side. */ |
| 2169 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2170 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2171 | struct hlua_socket *socket; |
| 2172 | struct connection *conn; |
| 2173 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2174 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2175 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2176 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2177 | |
| 2178 | /* Check if the tcp object is avalaible. */ |
| 2179 | if (!socket->s) { |
| 2180 | lua_pushnil(L); |
| 2181 | return 1; |
| 2182 | } |
| 2183 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2184 | conn = objt_conn(socket->s->si[1].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2185 | if (!conn) { |
| 2186 | lua_pushnil(L); |
| 2187 | return 1; |
| 2188 | } |
| 2189 | |
| 2190 | if (!(conn->flags & CO_FL_ADDR_FROM_SET)) { |
| 2191 | unsigned int salen = sizeof(conn->addr.from); |
| 2192 | if (getsockname(conn->t.sock.fd, (struct sockaddr *)&conn->addr.from, &salen) == -1) { |
| 2193 | lua_pushnil(L); |
| 2194 | return 1; |
| 2195 | } |
| 2196 | conn->flags |= CO_FL_ADDR_FROM_SET; |
| 2197 | } |
| 2198 | |
| 2199 | return hlua_socket_info(L, &conn->addr.from); |
| 2200 | } |
| 2201 | |
| 2202 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2203 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2204 | .obj_type = OBJ_TYPE_APPLET, |
| 2205 | .name = "<LUA_TCP>", |
| 2206 | .fct = hlua_socket_handler, |
| 2207 | .release = hlua_socket_release, |
| 2208 | }; |
| 2209 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2210 | __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] | 2211 | { |
| 2212 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2213 | struct hlua *hlua = hlua_gethlua(L); |
| 2214 | struct appctx *appctx; |
| 2215 | |
| 2216 | /* Check for connection close. */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2217 | if (!hlua || !socket->s || channel_output_closed(&socket->s->req)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2218 | lua_pushnil(L); |
| 2219 | lua_pushstring(L, "Can't connect"); |
| 2220 | return 2; |
| 2221 | } |
| 2222 | |
| 2223 | appctx = objt_appctx(socket->s->si[0].end); |
| 2224 | |
| 2225 | /* Check for connection established. */ |
| 2226 | if (appctx->ctx.hlua.connected) { |
| 2227 | lua_pushinteger(L, 1); |
| 2228 | return 1; |
| 2229 | } |
| 2230 | |
| 2231 | if (!hlua_com_new(hlua, &appctx->ctx.hlua.wake_on_write)) |
| 2232 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 2233 | 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] | 2234 | return 0; |
| 2235 | } |
| 2236 | |
| 2237 | /* This function fail or initite the connection. */ |
| 2238 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2239 | { |
| 2240 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2241 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2242 | const char *ip; |
| 2243 | struct connection *conn; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2244 | struct hlua *hlua; |
| 2245 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2246 | int low, high; |
| 2247 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2248 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2249 | if (lua_gettop(L) < 2) |
| 2250 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2251 | |
| 2252 | /* Get args. */ |
| 2253 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2254 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2255 | if (lua_gettop(L) >= 3) |
| 2256 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2257 | |
Willy Tarreau | 973a542 | 2015-08-05 21:47:23 +0200 | [diff] [blame] | 2258 | conn = si_alloc_conn(&socket->s->si[1]); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2259 | if (!conn) |
| 2260 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
| 2261 | |
Willy Tarreau | 3adac08 | 2015-09-26 17:51:09 +0200 | [diff] [blame] | 2262 | /* needed for the connection not to be closed */ |
| 2263 | conn->target = socket->s->target; |
| 2264 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2265 | /* Parse ip address. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2266 | addr = str2sa_range(ip, &low, &high, NULL, NULL, NULL, 0); |
| 2267 | if (!addr) |
| 2268 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
| 2269 | if (low != high) |
| 2270 | WILL_LJMP(luaL_error(L, "connect: port ranges not supported : address '%s'", ip)); |
| 2271 | memcpy(&conn->addr.to, addr, sizeof(struct sockaddr_storage)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2272 | |
| 2273 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2274 | if (low == 0) { |
| 2275 | if (conn->addr.to.ss_family == AF_INET) { |
| 2276 | if (port == -1) |
| 2277 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
| 2278 | ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(port); |
| 2279 | } else if (conn->addr.to.ss_family == AF_INET6) { |
| 2280 | if (port == -1) |
| 2281 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
| 2282 | ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(port); |
| 2283 | } |
| 2284 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2285 | |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2286 | hlua = hlua_gethlua(L); |
| 2287 | appctx = objt_appctx(socket->s->si[0].end); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2288 | |
| 2289 | /* inform the stream that we want to be notified whenever the |
| 2290 | * connection completes. |
| 2291 | */ |
| 2292 | si_applet_cant_get(&socket->s->si[0]); |
| 2293 | si_applet_cant_put(&socket->s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2294 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2295 | |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2296 | hlua->flags |= HLUA_MUST_GC; |
| 2297 | |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2298 | if (!hlua_com_new(hlua, &appctx->ctx.hlua.wake_on_write)) |
| 2299 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 2300 | 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] | 2301 | |
| 2302 | return 0; |
| 2303 | } |
| 2304 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2305 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2306 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2307 | { |
| 2308 | struct hlua_socket *socket; |
| 2309 | |
| 2310 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2311 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2312 | socket->s->target = &socket_ssl.obj_type; |
| 2313 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2314 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2315 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2316 | |
| 2317 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2318 | { |
| 2319 | return 0; |
| 2320 | } |
| 2321 | |
| 2322 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2323 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2324 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2325 | int tmout; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2326 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2327 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2328 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2329 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2330 | tmout = MAY_LJMP(luaL_checkinteger(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2331 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2332 | socket->s->req.rto = tmout; |
| 2333 | socket->s->req.wto = tmout; |
| 2334 | socket->s->res.rto = tmout; |
| 2335 | socket->s->res.wto = tmout; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2336 | |
| 2337 | return 0; |
| 2338 | } |
| 2339 | |
| 2340 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2341 | { |
| 2342 | struct hlua_socket *socket; |
| 2343 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2344 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2345 | struct stream *strm; |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2346 | struct task *task; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2347 | |
| 2348 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2349 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2350 | hlua_pusherror(L, "socket: full stack"); |
| 2351 | goto out_fail_conf; |
| 2352 | } |
| 2353 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2354 | /* Create the object: obj[0] = userdata. */ |
| 2355 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2356 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2357 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2358 | memset(socket, 0, sizeof(*socket)); |
| 2359 | |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2360 | /* Check if the various memory pools are intialized. */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2361 | if (!pool2_stream || !pool2_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2362 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2363 | goto out_fail_conf; |
| 2364 | } |
| 2365 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2366 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2367 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2368 | lua_setmetatable(L, -2); |
| 2369 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2370 | /* Create the applet context */ |
| 2371 | appctx = appctx_new(&update_applet); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2372 | if (!appctx) { |
| 2373 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2374 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2375 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2376 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2377 | appctx->ctx.hlua.socket = socket; |
| 2378 | appctx->ctx.hlua.connected = 0; |
| 2379 | LIST_INIT(&appctx->ctx.hlua.wake_on_write); |
| 2380 | LIST_INIT(&appctx->ctx.hlua.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2381 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2382 | /* Now create a session, task and stream for this applet */ |
| 2383 | sess = session_new(&socket_proxy, NULL, &appctx->obj_type); |
| 2384 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2385 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2386 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2387 | } |
| 2388 | |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2389 | task = task_new(); |
| 2390 | if (!task) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2391 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2392 | goto out_fail_task; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2393 | } |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2394 | task->nice = 0; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2395 | |
Willy Tarreau | 73b65ac | 2015-04-08 18:26:29 +0200 | [diff] [blame] | 2396 | strm = stream_new(sess, task, &appctx->obj_type); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2397 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2398 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2399 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2400 | } |
| 2401 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2402 | /* Configure an empty Lua for the stream. */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2403 | socket->s = strm; |
| 2404 | strm->hlua.T = NULL; |
| 2405 | strm->hlua.Tref = LUA_REFNIL; |
| 2406 | strm->hlua.Mref = LUA_REFNIL; |
| 2407 | strm->hlua.nargs = 0; |
| 2408 | strm->hlua.flags = 0; |
| 2409 | LIST_INIT(&strm->hlua.com); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2410 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2411 | /* Configure "right" stream interface. this "si" is used to connect |
| 2412 | * and retrieve data from the server. The connection is initialized |
| 2413 | * with the "struct server". |
| 2414 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2415 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2416 | |
| 2417 | /* Force destination server. */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2418 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET | SF_BE_ASSIGNED; |
| 2419 | strm->target = &socket_tcp.obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2420 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2421 | /* Update statistics counters. */ |
| 2422 | socket_proxy.feconn++; /* beconn will be increased later */ |
| 2423 | jobs++; |
| 2424 | totalconn++; |
| 2425 | |
| 2426 | /* Return yield waiting for connection. */ |
| 2427 | return 1; |
| 2428 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2429 | out_fail_stream: |
| 2430 | task_free(task); |
| 2431 | out_fail_task: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2432 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2433 | out_fail_sess: |
| 2434 | appctx_free(appctx); |
| 2435 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2436 | WILL_LJMP(lua_error(L)); |
| 2437 | return 0; |
| 2438 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2439 | |
| 2440 | /* |
| 2441 | * |
| 2442 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2443 | * Class Channel |
| 2444 | * |
| 2445 | * |
| 2446 | */ |
| 2447 | |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2448 | /* The state between the channel data and the HTTP parser state can be |
| 2449 | * unconsistent, so reset the parser and call it again. Warning, this |
| 2450 | * action not revalidate the request and not send a 400 if the modified |
| 2451 | * resuest is not valid. |
| 2452 | * |
| 2453 | * This function never fails. If dir is 0 we are a request, if it is 1 |
| 2454 | * its a response. |
| 2455 | */ |
| 2456 | static void hlua_resynchonize_proto(struct stream *stream, int dir) |
| 2457 | { |
| 2458 | /* Protocol HTTP. */ |
| 2459 | if (stream->be->mode == PR_MODE_HTTP) { |
| 2460 | |
| 2461 | if (dir == 0) |
| 2462 | http_txn_reset_req(stream->txn); |
| 2463 | else if (dir == 1) |
| 2464 | http_txn_reset_res(stream->txn); |
| 2465 | |
| 2466 | if (stream->txn->hdr_idx.v) |
| 2467 | hdr_idx_init(&stream->txn->hdr_idx); |
| 2468 | |
| 2469 | if (dir == 0) |
| 2470 | http_msg_analyzer(&stream->txn->req, &stream->txn->hdr_idx); |
| 2471 | else if (dir == 1) |
| 2472 | http_msg_analyzer(&stream->txn->rsp, &stream->txn->hdr_idx); |
| 2473 | } |
| 2474 | } |
| 2475 | |
| 2476 | /* Check the protocole integrity after the Lua manipulations. |
| 2477 | * Close the stream and returns 0 if fails, otherwise returns 1. |
| 2478 | */ |
| 2479 | static int hlua_check_proto(struct stream *stream, int dir) |
| 2480 | { |
| 2481 | const struct chunk msg = { .len = 0 }; |
| 2482 | |
Willy Tarreau | 9af89f7 | 2015-09-26 11:50:08 +0200 | [diff] [blame] | 2483 | /* Protocol HTTP. The message parsing state must match the request or |
| 2484 | * response state. The problem that may happen is that Lua modifies |
| 2485 | * the request or response message *after* it was parsed, and corrupted |
| 2486 | * it so that it could not be processed anymore. We just need to verify |
| 2487 | * if the parser is still expected to run or not. |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2488 | */ |
| 2489 | if (stream->be->mode == PR_MODE_HTTP) { |
Willy Tarreau | 9af89f7 | 2015-09-26 11:50:08 +0200 | [diff] [blame] | 2490 | if (dir == 0 && |
| 2491 | !(stream->req.analysers & AN_REQ_WAIT_HTTP) && |
| 2492 | stream->txn->req.msg_state < HTTP_MSG_BODY) { |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2493 | stream_int_retnclose(&stream->si[0], &msg); |
| 2494 | return 0; |
| 2495 | } |
Willy Tarreau | 9af89f7 | 2015-09-26 11:50:08 +0200 | [diff] [blame] | 2496 | else if (dir == 1 && |
| 2497 | !(stream->res.analysers & AN_RES_WAIT_HTTP) && |
| 2498 | stream->txn->rsp.msg_state < HTTP_MSG_BODY) { |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2499 | stream_int_retnclose(&stream->si[0], &msg); |
| 2500 | return 0; |
| 2501 | } |
| 2502 | } |
| 2503 | return 1; |
| 2504 | } |
| 2505 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2506 | /* Returns the struct hlua_channel join to the class channel in the |
| 2507 | * stack entry "ud" or throws an argument error. |
| 2508 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2509 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2510 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2511 | return (struct channel *)MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2512 | } |
| 2513 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2514 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 2515 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2516 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 2517 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2518 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2519 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2520 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2521 | return 0; |
| 2522 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2523 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2524 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2525 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2526 | |
| 2527 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2528 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 2529 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2530 | return 1; |
| 2531 | } |
| 2532 | |
| 2533 | /* Duplicate all the data present in the input channel and put it |
| 2534 | * in a string LUA variables. Returns -1 and push a nil value in |
| 2535 | * the stack if the channel is closed and all the data are consumed, |
| 2536 | * returns 0 if no data are available, otherwise it returns the length |
| 2537 | * of the builded string. |
| 2538 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2539 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2540 | { |
| 2541 | char *blk1; |
| 2542 | char *blk2; |
| 2543 | int len1; |
| 2544 | int len2; |
| 2545 | int ret; |
| 2546 | luaL_Buffer b; |
| 2547 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2548 | ret = bi_getblk_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2549 | if (unlikely(ret == 0)) |
| 2550 | return 0; |
| 2551 | |
| 2552 | if (unlikely(ret < 0)) { |
| 2553 | lua_pushnil(L); |
| 2554 | return -1; |
| 2555 | } |
| 2556 | |
| 2557 | luaL_buffinit(L, &b); |
| 2558 | luaL_addlstring(&b, blk1, len1); |
| 2559 | if (unlikely(ret == 2)) |
| 2560 | luaL_addlstring(&b, blk2, len2); |
| 2561 | luaL_pushresult(&b); |
| 2562 | |
| 2563 | if (unlikely(ret == 2)) |
| 2564 | return len1 + len2; |
| 2565 | return len1; |
| 2566 | } |
| 2567 | |
| 2568 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2569 | * a yield. This function keep the data in the buffer. |
| 2570 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2571 | __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] | 2572 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2573 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2574 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2575 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2576 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2577 | if (_hlua_channel_dup(chn, L) == 0) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2578 | 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] | 2579 | return 1; |
| 2580 | } |
| 2581 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2582 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 2583 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 2584 | { |
| 2585 | MAY_LJMP(check_args(L, 1, "dup")); |
| 2586 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2587 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 2588 | } |
| 2589 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2590 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2591 | * a yield. This function consumes the data in the buffer. It returns |
| 2592 | * a string containing the data or a nil pointer if no data are available |
| 2593 | * and the channel is closed. |
| 2594 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2595 | __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] | 2596 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2597 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2598 | int ret; |
| 2599 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2600 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2601 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2602 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2603 | if (unlikely(ret == 0)) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2604 | 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] | 2605 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2606 | if (unlikely(ret == -1)) |
| 2607 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2608 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2609 | chn->buf->i -= ret; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2610 | hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2611 | return 1; |
| 2612 | } |
| 2613 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2614 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 2615 | __LJMP static int hlua_channel_get(lua_State *L) |
| 2616 | { |
| 2617 | MAY_LJMP(check_args(L, 1, "get")); |
| 2618 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2619 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 2620 | } |
| 2621 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2622 | /* This functions consumes and returns one line. If the channel is closed, |
| 2623 | * and the last data does not contains a final '\n', the data are returned |
| 2624 | * without the final '\n'. When no more data are avalaible, it returns nil |
| 2625 | * value. |
| 2626 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2627 | __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] | 2628 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2629 | char *blk1; |
| 2630 | char *blk2; |
| 2631 | int len1; |
| 2632 | int len2; |
| 2633 | int len; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2634 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2635 | int ret; |
| 2636 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2637 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2638 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2639 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2640 | ret = bi_getline_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2641 | if (ret == 0) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2642 | 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] | 2643 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2644 | if (ret == -1) { |
| 2645 | lua_pushnil(L); |
| 2646 | return 1; |
| 2647 | } |
| 2648 | |
| 2649 | luaL_buffinit(L, &b); |
| 2650 | luaL_addlstring(&b, blk1, len1); |
| 2651 | len = len1; |
| 2652 | if (unlikely(ret == 2)) { |
| 2653 | luaL_addlstring(&b, blk2, len2); |
| 2654 | len += len2; |
| 2655 | } |
| 2656 | luaL_pushresult(&b); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2657 | 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] | 2658 | hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2659 | return 1; |
| 2660 | } |
| 2661 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2662 | /* Check arguments for the fucntion "hlua_channel_getline_yield". */ |
| 2663 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 2664 | { |
| 2665 | MAY_LJMP(check_args(L, 1, "getline")); |
| 2666 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2667 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 2668 | } |
| 2669 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2670 | /* This function takes a string as input, and append it at the |
| 2671 | * input side of channel. If the data is too big, but a space |
| 2672 | * is probably available after sending some data, the function |
| 2673 | * yield. If the data is bigger than the buffer, or if the |
| 2674 | * channel is closed, it returns -1. otherwise, it returns the |
| 2675 | * amount of data writed. |
| 2676 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2677 | __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] | 2678 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2679 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2680 | size_t len; |
| 2681 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2682 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2683 | int ret; |
| 2684 | int max; |
| 2685 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2686 | max = channel_recv_limit(chn) - buffer_len(chn->buf); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2687 | if (max > len - l) |
| 2688 | max = len - l; |
| 2689 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2690 | ret = bi_putblk(chn, str + l, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2691 | if (ret == -2 || ret == -3) { |
| 2692 | lua_pushinteger(L, -1); |
| 2693 | return 1; |
| 2694 | } |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2695 | if (ret == -1) { |
| 2696 | chn->flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2697 | 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] | 2698 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2699 | l += ret; |
| 2700 | lua_pop(L, 1); |
| 2701 | lua_pushinteger(L, l); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2702 | hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2703 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2704 | max = channel_recv_limit(chn) - buffer_len(chn->buf); |
| 2705 | if (max == 0 && chn->buf->o == 0) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2706 | /* There are no space avalaible, and the output buffer is empty. |
| 2707 | * in this case, we cannot add more data, so we cannot yield, |
| 2708 | * we return the amount of copyied data. |
| 2709 | */ |
| 2710 | return 1; |
| 2711 | } |
| 2712 | if (l < len) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2713 | 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] | 2714 | return 1; |
| 2715 | } |
| 2716 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2717 | /* just a wrapper of "hlua_channel_append_yield". It returns the length |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2718 | * of the writed string, or -1 if the channel is closed or if the |
| 2719 | * buffer size is too little for the data. |
| 2720 | */ |
| 2721 | __LJMP static int hlua_channel_append(lua_State *L) |
| 2722 | { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2723 | size_t len; |
| 2724 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2725 | MAY_LJMP(check_args(L, 2, "append")); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2726 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2727 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2728 | MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2729 | lua_pushinteger(L, 0); |
| 2730 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2731 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2732 | } |
| 2733 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2734 | /* just a wrapper of "hlua_channel_append_yield". This wrapper starts |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2735 | * his process by cleaning the buffer. The result is a replacement |
| 2736 | * of the current data. It returns the length of the writed string, |
| 2737 | * or -1 if the channel is closed or if the buffer size is too |
| 2738 | * little for the data. |
| 2739 | */ |
| 2740 | __LJMP static int hlua_channel_set(lua_State *L) |
| 2741 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2742 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2743 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2744 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2745 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2746 | lua_pushinteger(L, 0); |
| 2747 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2748 | chn->buf->i = 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2749 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2750 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2751 | } |
| 2752 | |
| 2753 | /* Append data in the output side of the buffer. This data is immediatly |
| 2754 | * sent. The fcuntion returns the ammount of data writed. If the buffer |
| 2755 | * cannot contains the data, the function yield. The function returns -1 |
| 2756 | * if the channel is closed. |
| 2757 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2758 | __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] | 2759 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2760 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2761 | size_t len; |
| 2762 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2763 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2764 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2765 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2766 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2767 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2768 | lua_pushinteger(L, -1); |
| 2769 | return 1; |
| 2770 | } |
| 2771 | |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 2772 | /* Check if the buffer is avalaible because HAProxy doesn't allocate |
| 2773 | * the request buffer if its not required. |
| 2774 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2775 | if (chn->buf->size == 0) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2776 | if (!stream_alloc_recv_buffer(chn)) { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2777 | chn_prod(chn)->flags |= SI_FL_WAIT_ROOM; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2778 | 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] | 2779 | } |
| 2780 | } |
| 2781 | |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2782 | /* the writed data will be immediatly sent, so we can check |
| 2783 | * the avalaible space without taking in account the reserve. |
| 2784 | * The reserve is guaranted for the processing of incoming |
| 2785 | * data, because the buffer will be flushed. |
| 2786 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2787 | max = chn->buf->size - buffer_len(chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2788 | |
| 2789 | /* If there are no space avalaible, and the output buffer is empty. |
| 2790 | * in this case, we cannot add more data, so we cannot yield, |
| 2791 | * we return the amount of copyied data. |
| 2792 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2793 | if (max == 0 && chn->buf->o == 0) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2794 | return 1; |
| 2795 | |
| 2796 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2797 | if (max > len - l) |
| 2798 | max = len - l; |
| 2799 | |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2800 | /* The buffer avalaible size may be not contiguous. This test |
| 2801 | * detects a non contiguous buffer and realign it. |
| 2802 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2803 | if (bi_space_for_replace(chn->buf) < max) |
| 2804 | buffer_slow_realign(chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2805 | |
| 2806 | /* Copy input data in the buffer. */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2807 | 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] | 2808 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2809 | /* buffer replace considers that the input part is filled. |
| 2810 | * so, I must forward these new data in the output part. |
| 2811 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2812 | b_adv(chn->buf, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2813 | |
| 2814 | l += max; |
| 2815 | lua_pop(L, 1); |
| 2816 | lua_pushinteger(L, l); |
| 2817 | |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2818 | /* If there are no space avalaible, and the output buffer is empty. |
| 2819 | * in this case, we cannot add more data, so we cannot yield, |
| 2820 | * we return the amount of copyied data. |
| 2821 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2822 | max = chn->buf->size - buffer_len(chn->buf); |
| 2823 | if (max == 0 && chn->buf->o == 0) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2824 | return 1; |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2825 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2826 | if (l < len) { |
| 2827 | /* If we are waiting for space in the response buffer, we |
| 2828 | * must set the flag WAKERESWR. This flag required the task |
| 2829 | * wake up if any activity is detected on the response buffer. |
| 2830 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2831 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2832 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 2833 | else |
| 2834 | HLUA_SET_WAKEREQWR(hlua); |
| 2835 | 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] | 2836 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2837 | |
| 2838 | return 1; |
| 2839 | } |
| 2840 | |
| 2841 | /* Just a wraper of "_hlua_channel_send". This wrapper permits |
| 2842 | * yield the LUA process, and resume it without checking the |
| 2843 | * input arguments. |
| 2844 | */ |
| 2845 | __LJMP static int hlua_channel_send(lua_State *L) |
| 2846 | { |
| 2847 | MAY_LJMP(check_args(L, 2, "send")); |
| 2848 | lua_pushinteger(L, 0); |
| 2849 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2850 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2851 | } |
| 2852 | |
| 2853 | /* This function forward and amount of butes. The data pass from |
| 2854 | * the input side of the buffer to the output side, and can be |
| 2855 | * forwarded. This function never fails. |
| 2856 | * |
| 2857 | * The Lua function takes an amount of bytes to be forwarded in |
| 2858 | * imput. It returns the number of bytes forwarded. |
| 2859 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2860 | __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] | 2861 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2862 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2863 | int len; |
| 2864 | int l; |
| 2865 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2866 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2867 | |
| 2868 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2869 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 2870 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 2871 | |
| 2872 | max = len - l; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2873 | if (max > chn->buf->i) |
| 2874 | max = chn->buf->i; |
| 2875 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2876 | l += max; |
| 2877 | |
| 2878 | lua_pop(L, 1); |
| 2879 | lua_pushinteger(L, l); |
| 2880 | |
| 2881 | /* Check if it miss bytes to forward. */ |
| 2882 | if (l < len) { |
| 2883 | /* The the input channel or the output channel are closed, we |
| 2884 | * must return the amount of data forwarded. |
| 2885 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2886 | if (channel_input_closed(chn) || channel_output_closed(chn)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2887 | return 1; |
| 2888 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2889 | /* If we are waiting for space data in the response buffer, we |
| 2890 | * must set the flag WAKERESWR. This flag required the task |
| 2891 | * wake up if any activity is detected on the response buffer. |
| 2892 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2893 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2894 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 2895 | else |
| 2896 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2897 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2898 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 2899 | 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] | 2900 | } |
| 2901 | |
| 2902 | return 1; |
| 2903 | } |
| 2904 | |
| 2905 | /* Just check the input and prepare the stack for the previous |
| 2906 | * function "hlua_channel_forward_yield" |
| 2907 | */ |
| 2908 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 2909 | { |
| 2910 | MAY_LJMP(check_args(L, 2, "forward")); |
| 2911 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2912 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 2913 | |
| 2914 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2915 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2916 | } |
| 2917 | |
| 2918 | /* Just returns the number of bytes available in the input |
| 2919 | * side of the buffer. This function never fails. |
| 2920 | */ |
| 2921 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 2922 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2923 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2924 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2925 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2926 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2927 | lua_pushinteger(L, chn->buf->i); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2928 | return 1; |
| 2929 | } |
| 2930 | |
| 2931 | /* Just returns the number of bytes available in the output |
| 2932 | * side of the buffer. This function never fails. |
| 2933 | */ |
| 2934 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 2935 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2936 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2937 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2938 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2939 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2940 | lua_pushinteger(L, chn->buf->o); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2941 | return 1; |
| 2942 | } |
| 2943 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2944 | /* |
| 2945 | * |
| 2946 | * |
| 2947 | * Class Fetches |
| 2948 | * |
| 2949 | * |
| 2950 | */ |
| 2951 | |
| 2952 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2953 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2954 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 2955 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2956 | { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 2957 | 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] | 2958 | } |
| 2959 | |
| 2960 | /* This function creates and push in the stack a fetch object according |
| 2961 | * with a current TXN. |
| 2962 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 2963 | 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] | 2964 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 2965 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2966 | |
| 2967 | /* Check stack size. */ |
| 2968 | if (!lua_checkstack(L, 3)) |
| 2969 | return 0; |
| 2970 | |
| 2971 | /* Create the object: obj[0] = userdata. |
| 2972 | * Note that the base of the Fetches object is the |
| 2973 | * transaction object. |
| 2974 | */ |
| 2975 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 2976 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2977 | lua_rawseti(L, -2, 0); |
| 2978 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 2979 | hsmp->s = txn->s; |
| 2980 | hsmp->p = txn->p; |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 2981 | hsmp->stringsafe = stringsafe; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2982 | |
| 2983 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2984 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 2985 | lua_setmetatable(L, -2); |
| 2986 | |
| 2987 | return 1; |
| 2988 | } |
| 2989 | |
| 2990 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 2991 | * It uses closure argument to store the associated sample-fetch. It |
| 2992 | * returns only one argument or throws an error. An error is thrown |
| 2993 | * only if an error is encountered during the argument parsing. If |
| 2994 | * the "sample-fetch" function fails, nil is returned. |
| 2995 | */ |
| 2996 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 2997 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 2998 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 2999 | struct sample_fetch *f; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3000 | struct arg args[ARGM_NBARGS + 1]; |
| 3001 | int i; |
| 3002 | struct sample smp; |
| 3003 | |
| 3004 | /* Get closure arguments. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3005 | f = (struct sample_fetch *)lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3006 | |
| 3007 | /* Get traditionnal arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3008 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3009 | |
| 3010 | /* Get extra arguments. */ |
| 3011 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 3012 | if (i >= ARGM_NBARGS) |
| 3013 | break; |
| 3014 | hlua_lua2arg(L, i + 2, &args[i]); |
| 3015 | } |
| 3016 | args[i].type = ARGT_STOP; |
| 3017 | |
| 3018 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3019 | 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] | 3020 | |
| 3021 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3022 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3023 | lua_pushfstring(L, "error in arguments"); |
| 3024 | WILL_LJMP(lua_error(L)); |
| 3025 | } |
| 3026 | |
| 3027 | /* Initialise the sample. */ |
| 3028 | memset(&smp, 0, sizeof(smp)); |
| 3029 | |
| 3030 | /* Run the sample fetch process. */ |
Thierry FOURNIER | 6879ad3 | 2015-05-11 11:54:58 +0200 | [diff] [blame] | 3031 | smp.px = hsmp->p; |
| 3032 | smp.sess = hsmp->s->sess; |
| 3033 | smp.strm = hsmp->s; |
Thierry FOURNIER | 1d33b88 | 2015-05-11 15:25:29 +0200 | [diff] [blame] | 3034 | smp.opt = 0; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 3035 | if (!f->process(args, &smp, f->kw, f->private)) { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3036 | if (hsmp->stringsafe) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3037 | lua_pushstring(L, ""); |
| 3038 | else |
| 3039 | lua_pushnil(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3040 | return 1; |
| 3041 | } |
| 3042 | |
| 3043 | /* Convert the returned sample in lua value. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3044 | if (hsmp->stringsafe) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3045 | hlua_smp2lua_str(L, &smp); |
| 3046 | else |
| 3047 | hlua_smp2lua(L, &smp); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3048 | return 1; |
| 3049 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3050 | |
| 3051 | /* |
| 3052 | * |
| 3053 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3054 | * Class Converters |
| 3055 | * |
| 3056 | * |
| 3057 | */ |
| 3058 | |
| 3059 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3060 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3061 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3062 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3063 | { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3064 | 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] | 3065 | } |
| 3066 | |
| 3067 | /* This function creates and push in the stack a Converters object |
| 3068 | * according with a current TXN. |
| 3069 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3070 | 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] | 3071 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3072 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3073 | |
| 3074 | /* Check stack size. */ |
| 3075 | if (!lua_checkstack(L, 3)) |
| 3076 | return 0; |
| 3077 | |
| 3078 | /* Create the object: obj[0] = userdata. |
| 3079 | * Note that the base of the Converters object is the |
| 3080 | * same than the TXN object. |
| 3081 | */ |
| 3082 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3083 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3084 | lua_rawseti(L, -2, 0); |
| 3085 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3086 | hsmp->s = txn->s; |
| 3087 | hsmp->p = txn->p; |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3088 | hsmp->stringsafe = stringsafe; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3089 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3090 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3091 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 3092 | lua_setmetatable(L, -2); |
| 3093 | |
| 3094 | return 1; |
| 3095 | } |
| 3096 | |
| 3097 | /* This function is an LUA binding. It is called with each converter. |
| 3098 | * It uses closure argument to store the associated converter. It |
| 3099 | * returns only one argument or throws an error. An error is thrown |
| 3100 | * only if an error is encountered during the argument parsing. If |
| 3101 | * the converter function function fails, nil is returned. |
| 3102 | */ |
| 3103 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 3104 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3105 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3106 | struct sample_conv *conv; |
| 3107 | struct arg args[ARGM_NBARGS + 1]; |
| 3108 | int i; |
| 3109 | struct sample smp; |
| 3110 | |
| 3111 | /* Get closure arguments. */ |
| 3112 | conv = (struct sample_conv *)lua_touserdata(L, lua_upvalueindex(1)); |
| 3113 | |
| 3114 | /* Get traditionnal arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3115 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3116 | |
| 3117 | /* Get extra arguments. */ |
| 3118 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 3119 | if (i >= ARGM_NBARGS) |
| 3120 | break; |
| 3121 | hlua_lua2arg(L, i + 3, &args[i]); |
| 3122 | } |
| 3123 | args[i].type = ARGT_STOP; |
| 3124 | |
| 3125 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3126 | 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] | 3127 | |
| 3128 | /* Run the special args checker. */ |
| 3129 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 3130 | hlua_pusherror(L, "error in arguments"); |
| 3131 | WILL_LJMP(lua_error(L)); |
| 3132 | } |
| 3133 | |
| 3134 | /* Initialise the sample. */ |
| 3135 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 3136 | hlua_pusherror(L, "error in the input argument"); |
| 3137 | WILL_LJMP(lua_error(L)); |
| 3138 | } |
| 3139 | |
| 3140 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3141 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3142 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3143 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3144 | WILL_LJMP(lua_error(L)); |
| 3145 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3146 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 3147 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3148 | hlua_pusherror(L, "error during the input argument casting"); |
| 3149 | WILL_LJMP(lua_error(L)); |
| 3150 | } |
| 3151 | |
| 3152 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 6879ad3 | 2015-05-11 11:54:58 +0200 | [diff] [blame] | 3153 | smp.px = hsmp->p; |
| 3154 | smp.sess = hsmp->s->sess; |
| 3155 | smp.strm = hsmp->s; |
Thierry FOURNIER | 1d33b88 | 2015-05-11 15:25:29 +0200 | [diff] [blame] | 3156 | smp.opt = 0; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3157 | if (!conv->process(args, &smp, conv->private)) { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3158 | if (hsmp->stringsafe) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3159 | lua_pushstring(L, ""); |
| 3160 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3161 | lua_pushnil(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3162 | return 1; |
| 3163 | } |
| 3164 | |
| 3165 | /* Convert the returned sample in lua value. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3166 | if (hsmp->stringsafe) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3167 | hlua_smp2lua_str(L, &smp); |
| 3168 | else |
| 3169 | hlua_smp2lua(L, &smp); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3170 | return 1; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3171 | } |
| 3172 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3173 | /* |
| 3174 | * |
| 3175 | * |
| 3176 | * Class AppletTCP |
| 3177 | * |
| 3178 | * |
| 3179 | */ |
| 3180 | |
| 3181 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 3182 | * a class stream, otherwise it throws an error. |
| 3183 | */ |
| 3184 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 3185 | { |
| 3186 | return (struct hlua_appctx *)MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
| 3187 | } |
| 3188 | |
| 3189 | /* This function creates and push in the stack an Applet object |
| 3190 | * according with a current TXN. |
| 3191 | */ |
| 3192 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 3193 | { |
| 3194 | struct hlua_appctx *appctx; |
| 3195 | struct stream_interface *si = ctx->owner; |
| 3196 | struct stream *s = si_strm(si); |
| 3197 | struct proxy *p = s->be; |
| 3198 | |
| 3199 | /* Check stack size. */ |
| 3200 | if (!lua_checkstack(L, 3)) |
| 3201 | return 0; |
| 3202 | |
| 3203 | /* Create the object: obj[0] = userdata. |
| 3204 | * Note that the base of the Converters object is the |
| 3205 | * same than the TXN object. |
| 3206 | */ |
| 3207 | lua_newtable(L); |
| 3208 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
| 3209 | lua_rawseti(L, -2, 0); |
| 3210 | appctx->appctx = ctx; |
| 3211 | appctx->htxn.s = s; |
| 3212 | appctx->htxn.p = p; |
| 3213 | |
| 3214 | /* Create the "f" field that contains a list of fetches. */ |
| 3215 | lua_pushstring(L, "f"); |
| 3216 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3217 | return 0; |
| 3218 | lua_settable(L, -3); |
| 3219 | |
| 3220 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3221 | lua_pushstring(L, "sf"); |
| 3222 | if (!hlua_fetches_new(L, &appctx->htxn, 1)) |
| 3223 | return 0; |
| 3224 | lua_settable(L, -3); |
| 3225 | |
| 3226 | /* Create the "c" field that contains a list of converters. */ |
| 3227 | lua_pushstring(L, "c"); |
| 3228 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3229 | return 0; |
| 3230 | lua_settable(L, -3); |
| 3231 | |
| 3232 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3233 | lua_pushstring(L, "sc"); |
| 3234 | if (!hlua_converters_new(L, &appctx->htxn, 1)) |
| 3235 | return 0; |
| 3236 | lua_settable(L, -3); |
| 3237 | |
| 3238 | /* Pop a class stream metatable and affect it to the table. */ |
| 3239 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 3240 | lua_setmetatable(L, -2); |
| 3241 | |
| 3242 | return 1; |
| 3243 | } |
| 3244 | |
| 3245 | /* If expected data not yet available, it returns a yield. This function |
| 3246 | * consumes the data in the buffer. It returns a string containing the |
| 3247 | * data. This string can be empty. |
| 3248 | */ |
| 3249 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 3250 | { |
| 3251 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3252 | struct stream_interface *si = appctx->appctx->owner; |
| 3253 | int ret; |
| 3254 | char *blk1; |
| 3255 | int len1; |
| 3256 | char *blk2; |
| 3257 | int len2; |
| 3258 | |
| 3259 | /* Read the maximum amount of data avalaible. */ |
| 3260 | ret = bo_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
| 3261 | |
| 3262 | /* Data not yet avalaible. return yield. */ |
| 3263 | if (ret == 0) { |
| 3264 | si_applet_cant_get(si); |
| 3265 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0)); |
| 3266 | } |
| 3267 | |
| 3268 | /* End of data: commit the total strings and return. */ |
| 3269 | if (ret < 0) { |
| 3270 | luaL_pushresult(&appctx->b); |
| 3271 | return 1; |
| 3272 | } |
| 3273 | |
| 3274 | /* Ensure that the block 2 length is usable. */ |
| 3275 | if (ret == 1) |
| 3276 | len2 = 0; |
| 3277 | |
| 3278 | /* dont check the max length read and dont check. */ |
| 3279 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3280 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3281 | |
| 3282 | /* Consume input channel output buffer data. */ |
| 3283 | bo_skip(si_oc(si), len1 + len2); |
| 3284 | luaL_pushresult(&appctx->b); |
| 3285 | return 1; |
| 3286 | } |
| 3287 | |
| 3288 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 3289 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 3290 | { |
| 3291 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3292 | |
| 3293 | /* Initialise the string catenation. */ |
| 3294 | luaL_buffinit(L, &appctx->b); |
| 3295 | |
| 3296 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 3297 | } |
| 3298 | |
| 3299 | /* If expected data not yet available, it returns a yield. This function |
| 3300 | * consumes the data in the buffer. It returns a string containing the |
| 3301 | * data. This string can be empty. |
| 3302 | */ |
| 3303 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 3304 | { |
| 3305 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3306 | struct stream_interface *si = appctx->appctx->owner; |
| 3307 | int len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3308 | int ret; |
| 3309 | char *blk1; |
| 3310 | int len1; |
| 3311 | char *blk2; |
| 3312 | int len2; |
| 3313 | |
| 3314 | /* Read the maximum amount of data avalaible. */ |
| 3315 | ret = bo_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
| 3316 | |
| 3317 | /* Data not yet avalaible. return yield. */ |
| 3318 | if (ret == 0) { |
| 3319 | si_applet_cant_get(si); |
| 3320 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0)); |
| 3321 | } |
| 3322 | |
| 3323 | /* End of data: commit the total strings and return. */ |
| 3324 | if (ret < 0) { |
| 3325 | luaL_pushresult(&appctx->b); |
| 3326 | return 1; |
| 3327 | } |
| 3328 | |
| 3329 | /* Ensure that the block 2 length is usable. */ |
| 3330 | if (ret == 1) |
| 3331 | len2 = 0; |
| 3332 | |
| 3333 | if (len == -1) { |
| 3334 | |
| 3335 | /* If len == -1, catenate all the data avalaile and |
| 3336 | * yield because we want to get all the data until |
| 3337 | * the end of data stream. |
| 3338 | */ |
| 3339 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3340 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3341 | bo_skip(si_oc(si), len1 + len2); |
| 3342 | si_applet_cant_get(si); |
| 3343 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0)); |
| 3344 | |
| 3345 | } else { |
| 3346 | |
| 3347 | /* Copy the fisrt block caping to the length required. */ |
| 3348 | if (len1 > len) |
| 3349 | len1 = len; |
| 3350 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3351 | len -= len1; |
| 3352 | |
| 3353 | /* Copy the second block. */ |
| 3354 | if (len2 > len) |
| 3355 | len2 = len; |
| 3356 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3357 | len -= len2; |
| 3358 | |
| 3359 | /* Consume input channel output buffer data. */ |
| 3360 | bo_skip(si_oc(si), len1 + len2); |
| 3361 | |
| 3362 | /* If we are no other data avalaible, yield waiting for new data. */ |
| 3363 | if (len > 0) { |
| 3364 | lua_pushinteger(L, len); |
| 3365 | lua_replace(L, 2); |
| 3366 | si_applet_cant_get(si); |
| 3367 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0)); |
| 3368 | } |
| 3369 | |
| 3370 | /* return the result. */ |
| 3371 | luaL_pushresult(&appctx->b); |
| 3372 | return 1; |
| 3373 | } |
| 3374 | |
| 3375 | /* we never executes this */ |
| 3376 | hlua_pusherror(L, "Lua: internal error"); |
| 3377 | WILL_LJMP(lua_error(L)); |
| 3378 | return 0; |
| 3379 | } |
| 3380 | |
| 3381 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 3382 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 3383 | { |
| 3384 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3385 | int len = -1; |
| 3386 | |
| 3387 | if (lua_gettop(L) > 2) |
| 3388 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 3389 | if (lua_gettop(L) >= 2) { |
| 3390 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3391 | lua_pop(L, 1); |
| 3392 | } |
| 3393 | |
| 3394 | /* Confirm or set the required length */ |
| 3395 | lua_pushinteger(L, len); |
| 3396 | |
| 3397 | /* Initialise the string catenation. */ |
| 3398 | luaL_buffinit(L, &appctx->b); |
| 3399 | |
| 3400 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 3401 | } |
| 3402 | |
| 3403 | /* Append data in the output side of the buffer. This data is immediatly |
| 3404 | * sent. The fcuntion returns the ammount of data writed. If the buffer |
| 3405 | * cannot contains the data, the function yield. The function returns -1 |
| 3406 | * if the channel is closed. |
| 3407 | */ |
| 3408 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 3409 | { |
| 3410 | size_t len; |
| 3411 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3412 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3413 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3414 | struct stream_interface *si = appctx->appctx->owner; |
| 3415 | struct channel *chn = si_ic(si); |
| 3416 | int max; |
| 3417 | |
| 3418 | /* Get the max amount of data which can write as input in the channel. */ |
| 3419 | max = channel_recv_max(chn); |
| 3420 | if (max > (len - l)) |
| 3421 | max = len - l; |
| 3422 | |
| 3423 | /* Copy data. */ |
| 3424 | bi_putblk(chn, str + l, max); |
| 3425 | |
| 3426 | /* update counters. */ |
| 3427 | l += max; |
| 3428 | lua_pop(L, 1); |
| 3429 | lua_pushinteger(L, l); |
| 3430 | |
| 3431 | /* If some data is not send, declares the situation to the |
| 3432 | * applet, and returns a yield. |
| 3433 | */ |
| 3434 | if (l < len) { |
| 3435 | si_applet_cant_put(si); |
| 3436 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0)); |
| 3437 | } |
| 3438 | |
| 3439 | return 1; |
| 3440 | } |
| 3441 | |
| 3442 | /* Just a wraper of "hlua_applet_tcp_send_yield". This wrapper permits |
| 3443 | * yield the LUA process, and resume it without checking the |
| 3444 | * input arguments. |
| 3445 | */ |
| 3446 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 3447 | { |
| 3448 | MAY_LJMP(check_args(L, 2, "send")); |
| 3449 | lua_pushinteger(L, 0); |
| 3450 | |
| 3451 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 3452 | } |
| 3453 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3454 | /* |
| 3455 | * |
| 3456 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3457 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3458 | * |
| 3459 | * |
| 3460 | */ |
| 3461 | |
| 3462 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3463 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3464 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3465 | __LJMP static struct hlua_appctx *hlua_checkapplet_http(lua_State *L, int ud) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3466 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3467 | return (struct hlua_appctx *)MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3468 | } |
| 3469 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3470 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3471 | * according with a current TXN. |
| 3472 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3473 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3474 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3475 | struct hlua_appctx *appctx; |
| 3476 | struct stream_interface *si = ctx->owner; |
| 3477 | struct stream *s = si_strm(si); |
| 3478 | struct proxy *px = s->be; |
| 3479 | struct http_txn *txn = s->txn; |
| 3480 | const char *path; |
| 3481 | const char *end; |
| 3482 | const char *p; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3483 | |
| 3484 | /* Check stack size. */ |
| 3485 | if (!lua_checkstack(L, 3)) |
| 3486 | return 0; |
| 3487 | |
| 3488 | /* Create the object: obj[0] = userdata. |
| 3489 | * Note that the base of the Converters object is the |
| 3490 | * same than the TXN object. |
| 3491 | */ |
| 3492 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3493 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3494 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3495 | appctx->appctx = ctx; |
| 3496 | appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
| 3497 | appctx->htxn.s = s; |
| 3498 | appctx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3499 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3500 | /* Create the "f" field that contains a list of fetches. */ |
| 3501 | lua_pushstring(L, "f"); |
| 3502 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3503 | return 0; |
| 3504 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3505 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3506 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3507 | lua_pushstring(L, "sf"); |
| 3508 | if (!hlua_fetches_new(L, &appctx->htxn, 1)) |
| 3509 | return 0; |
| 3510 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3511 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3512 | /* Create the "c" field that contains a list of converters. */ |
| 3513 | lua_pushstring(L, "c"); |
| 3514 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3515 | return 0; |
| 3516 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3517 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3518 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3519 | lua_pushstring(L, "sc"); |
| 3520 | if (!hlua_converters_new(L, &appctx->htxn, 1)) |
| 3521 | return 0; |
| 3522 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3523 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3524 | /* Stores the request method. */ |
| 3525 | lua_pushstring(L, "method"); |
| 3526 | lua_pushlstring(L, txn->req.chn->buf->p, txn->req.sl.rq.m_l); |
| 3527 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3528 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3529 | /* Stores the http version. */ |
| 3530 | lua_pushstring(L, "version"); |
| 3531 | lua_pushlstring(L, txn->req.chn->buf->p + txn->req.sl.rq.v, txn->req.sl.rq.v_l); |
| 3532 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3533 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3534 | /* Get path and qs */ |
| 3535 | path = http_get_path(txn); |
| 3536 | end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
| 3537 | p = path; |
| 3538 | while (p < end && *p != '?') |
| 3539 | p++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3540 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3541 | /* Stores the request path. */ |
| 3542 | lua_pushstring(L, "path"); |
| 3543 | lua_pushlstring(L, path, p - path); |
| 3544 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3545 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3546 | /* Stores the query string. */ |
| 3547 | lua_pushstring(L, "qs"); |
| 3548 | if (*p == '?') |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3549 | p++; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3550 | lua_pushlstring(L, p, end - p); |
| 3551 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3552 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3553 | /* Stores the request path. */ |
| 3554 | lua_pushstring(L, "length"); |
| 3555 | lua_pushinteger(L, txn->req.body_len); |
| 3556 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3557 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3558 | /* Create an array of HTTP request headers. */ |
| 3559 | lua_pushstring(L, "headers"); |
| 3560 | MAY_LJMP(hlua_http_get_headers(L, &appctx->htxn, &appctx->htxn.s->txn->req)); |
| 3561 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3562 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3563 | /* Create an empty array of HTTP request headers. */ |
| 3564 | lua_pushstring(L, "response"); |
| 3565 | lua_newtable(L); |
| 3566 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3567 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3568 | /* Pop a class stream metatable and affect it to the table. */ |
| 3569 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 3570 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3571 | |
| 3572 | return 1; |
| 3573 | } |
| 3574 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3575 | /* If expected data not yet available, it returns a yield. This function |
| 3576 | * consumes the data in the buffer. It returns a string containing the |
| 3577 | * data. This string can be empty. |
| 3578 | */ |
| 3579 | __LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3580 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3581 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3582 | struct stream_interface *si = appctx->appctx->owner; |
| 3583 | struct channel *chn = si_ic(si); |
| 3584 | int ret; |
| 3585 | char *blk1; |
| 3586 | int len1; |
| 3587 | char *blk2; |
| 3588 | int len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3589 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3590 | /* Maybe we cant send a 100-continue ? */ |
| 3591 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) { |
| 3592 | ret = bi_putblk(chn, HTTP_100C, strlen(HTTP_100C)); |
| 3593 | /* if ret == -2 or -3 the channel closed or the message si too |
| 3594 | * big for the buffers. We cant send anything. So, we ignoring |
| 3595 | * the error, considers that the 100-continue is sent, and try |
| 3596 | * to receive. |
| 3597 | * If ret is -1, we dont have room in the buffer, so we yield. |
| 3598 | */ |
| 3599 | if (ret == -1) { |
| 3600 | si_applet_cant_put(si); |
| 3601 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0)); |
| 3602 | } |
| 3603 | appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C; |
| 3604 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3605 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3606 | /* Check for the end of the data. */ |
| 3607 | if (appctx->appctx->ctx.hlua_apphttp.left_bytes <= 0) { |
| 3608 | luaL_pushresult(&appctx->b); |
| 3609 | return 1; |
| 3610 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3611 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3612 | /* Read the maximum amount of data avalaible. */ |
| 3613 | ret = bo_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3614 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3615 | /* Data not yet avalaible. return yield. */ |
| 3616 | if (ret == 0) { |
| 3617 | si_applet_cant_get(si); |
| 3618 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0)); |
| 3619 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3620 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3621 | /* End of data: commit the total strings and return. */ |
| 3622 | if (ret < 0) { |
| 3623 | luaL_pushresult(&appctx->b); |
| 3624 | return 1; |
| 3625 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3626 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3627 | /* Ensure that the block 2 length is usable. */ |
| 3628 | if (ret == 1) |
| 3629 | len2 = 0; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3630 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3631 | /* Copy the fisrt block caping to the length required. */ |
| 3632 | if (len1 > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 3633 | len1 = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 3634 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3635 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3636 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3637 | /* Copy the second block. */ |
| 3638 | if (len2 > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 3639 | len2 = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 3640 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3641 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3642 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3643 | /* Consume input channel output buffer data. */ |
| 3644 | bo_skip(si_oc(si), len1 + len2); |
| 3645 | luaL_pushresult(&appctx->b); |
| 3646 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3647 | } |
| 3648 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3649 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 3650 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3651 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3652 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3653 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3654 | /* Initialise the string catenation. */ |
| 3655 | luaL_buffinit(L, &appctx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3656 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3657 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3658 | } |
| 3659 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3660 | /* If expected data not yet available, it returns a yield. This function |
| 3661 | * consumes the data in the buffer. It returns a string containing the |
| 3662 | * data. This string can be empty. |
| 3663 | */ |
| 3664 | __LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3665 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3666 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3667 | struct stream_interface *si = appctx->appctx->owner; |
| 3668 | int len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3669 | struct channel *chn = si_ic(si); |
| 3670 | int ret; |
| 3671 | char *blk1; |
| 3672 | int len1; |
| 3673 | char *blk2; |
| 3674 | int len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3675 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3676 | /* Maybe we cant send a 100-continue ? */ |
| 3677 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) { |
| 3678 | ret = bi_putblk(chn, HTTP_100C, strlen(HTTP_100C)); |
| 3679 | /* if ret == -2 or -3 the channel closed or the message si too |
| 3680 | * big for the buffers. We cant send anything. So, we ignoring |
| 3681 | * the error, considers that the 100-continue is sent, and try |
| 3682 | * to receive. |
| 3683 | * If ret is -1, we dont have room in the buffer, so we yield. |
| 3684 | */ |
| 3685 | if (ret == -1) { |
| 3686 | si_applet_cant_put(si); |
| 3687 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0)); |
| 3688 | } |
| 3689 | appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C; |
| 3690 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3691 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3692 | /* Read the maximum amount of data avalaible. */ |
| 3693 | ret = bo_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3694 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3695 | /* Data not yet avalaible. return yield. */ |
| 3696 | if (ret == 0) { |
| 3697 | si_applet_cant_get(si); |
| 3698 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0)); |
| 3699 | } |
| 3700 | |
| 3701 | /* End of data: commit the total strings and return. */ |
| 3702 | if (ret < 0) { |
| 3703 | luaL_pushresult(&appctx->b); |
| 3704 | return 1; |
| 3705 | } |
| 3706 | |
| 3707 | /* Ensure that the block 2 length is usable. */ |
| 3708 | if (ret == 1) |
| 3709 | len2 = 0; |
| 3710 | |
| 3711 | /* Copy the fisrt block caping to the length required. */ |
| 3712 | if (len1 > len) |
| 3713 | len1 = len; |
| 3714 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3715 | len -= len1; |
| 3716 | |
| 3717 | /* Copy the second block. */ |
| 3718 | if (len2 > len) |
| 3719 | len2 = len; |
| 3720 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3721 | len -= len2; |
| 3722 | |
| 3723 | /* Consume input channel output buffer data. */ |
| 3724 | bo_skip(si_oc(si), len1 + len2); |
| 3725 | if (appctx->appctx->ctx.hlua_apphttp.left_bytes != -1) |
| 3726 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len; |
| 3727 | |
| 3728 | /* If we are no other data avalaible, yield waiting for new data. */ |
| 3729 | if (len > 0) { |
| 3730 | lua_pushinteger(L, len); |
| 3731 | lua_replace(L, 2); |
| 3732 | si_applet_cant_get(si); |
| 3733 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0)); |
| 3734 | } |
| 3735 | |
| 3736 | /* return the result. */ |
| 3737 | luaL_pushresult(&appctx->b); |
| 3738 | return 1; |
| 3739 | } |
| 3740 | |
| 3741 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 3742 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 3743 | { |
| 3744 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3745 | int len = -1; |
| 3746 | |
| 3747 | /* Check arguments. */ |
| 3748 | if (lua_gettop(L) > 2) |
| 3749 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 3750 | if (lua_gettop(L) >= 2) { |
| 3751 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3752 | lua_pop(L, 1); |
| 3753 | } |
| 3754 | |
| 3755 | /* Check the required length */ |
| 3756 | if (len == -1 || len > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 3757 | len = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 3758 | lua_pushinteger(L, len); |
| 3759 | |
| 3760 | /* Initialise the string catenation. */ |
| 3761 | luaL_buffinit(L, &appctx->b); |
| 3762 | |
| 3763 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
| 3764 | } |
| 3765 | |
| 3766 | /* Append data in the output side of the buffer. This data is immediatly |
| 3767 | * sent. The fcuntion returns the ammount of data writed. If the buffer |
| 3768 | * cannot contains the data, the function yield. The function returns -1 |
| 3769 | * if the channel is closed. |
| 3770 | */ |
| 3771 | __LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 3772 | { |
| 3773 | size_t len; |
| 3774 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3775 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3776 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3777 | struct stream_interface *si = appctx->appctx->owner; |
| 3778 | struct channel *chn = si_ic(si); |
| 3779 | int max; |
| 3780 | |
| 3781 | /* Get the max amount of data which can write as input in the channel. */ |
| 3782 | max = channel_recv_max(chn); |
| 3783 | if (max > (len - l)) |
| 3784 | max = len - l; |
| 3785 | |
| 3786 | /* Copy data. */ |
| 3787 | bi_putblk(chn, str + l, max); |
| 3788 | |
| 3789 | /* update counters. */ |
| 3790 | l += max; |
| 3791 | lua_pop(L, 1); |
| 3792 | lua_pushinteger(L, l); |
| 3793 | |
| 3794 | /* If some data is not send, declares the situation to the |
| 3795 | * applet, and returns a yield. |
| 3796 | */ |
| 3797 | if (l < len) { |
| 3798 | si_applet_cant_put(si); |
| 3799 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0)); |
| 3800 | } |
| 3801 | |
| 3802 | return 1; |
| 3803 | } |
| 3804 | |
| 3805 | /* Just a wraper of "hlua_applet_send_yield". This wrapper permits |
| 3806 | * yield the LUA process, and resume it without checking the |
| 3807 | * input arguments. |
| 3808 | */ |
| 3809 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 3810 | { |
| 3811 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3812 | size_t len; |
| 3813 | char hex[10]; |
| 3814 | |
| 3815 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3816 | |
| 3817 | /* If transfer encoding chunked is selected, we surround the data |
| 3818 | * by chunk data. |
| 3819 | */ |
| 3820 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED) { |
| 3821 | snprintf(hex, 9, "%x", (unsigned int)len); |
| 3822 | lua_pushfstring(L, "%s\r\n", hex); |
| 3823 | lua_insert(L, 2); /* swap the last 2 entries. */ |
| 3824 | lua_pushstring(L, "\r\n"); |
| 3825 | lua_concat(L, 3); |
| 3826 | } |
| 3827 | |
| 3828 | /* This interger is used for followinf the amount of data sent. */ |
| 3829 | lua_pushinteger(L, 0); |
| 3830 | |
| 3831 | /* We want to send some data. Headers must be sent. */ |
| 3832 | if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 3833 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 3834 | WILL_LJMP(lua_error(L)); |
| 3835 | } |
| 3836 | |
| 3837 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
| 3838 | } |
| 3839 | |
| 3840 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 3841 | { |
| 3842 | const char *name; |
| 3843 | int ret; |
| 3844 | |
| 3845 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3846 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 3847 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 3848 | |
| 3849 | /* Push in the stack the "response" entry. */ |
| 3850 | ret = lua_getfield(L, 1, "response"); |
| 3851 | if (ret != LUA_TTABLE) { |
| 3852 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 3853 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 3854 | WILL_LJMP(lua_error(L)); |
| 3855 | } |
| 3856 | |
| 3857 | /* check if the header is already registered if it is not |
| 3858 | * the case, register it. |
| 3859 | */ |
| 3860 | ret = lua_getfield(L, -1, name); |
| 3861 | if (ret == LUA_TNIL) { |
| 3862 | |
| 3863 | /* Entry not found. */ |
| 3864 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 3865 | |
| 3866 | /* Insert the new header name in the array in the top of the stack. |
| 3867 | * It left the new array in the top of the stack. |
| 3868 | */ |
| 3869 | lua_newtable(L); |
| 3870 | lua_pushvalue(L, 2); |
| 3871 | lua_pushvalue(L, -2); |
| 3872 | lua_settable(L, -4); |
| 3873 | |
| 3874 | } else if (ret != LUA_TTABLE) { |
| 3875 | |
| 3876 | /* corruption error. */ |
| 3877 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 3878 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 3879 | WILL_LJMP(lua_error(L)); |
| 3880 | } |
| 3881 | |
| 3882 | /* Now the top od thestack is an array of values. We push |
| 3883 | * the header value as new entry. |
| 3884 | */ |
| 3885 | lua_pushvalue(L, 3); |
| 3886 | ret = lua_rawlen(L, -2); |
| 3887 | lua_rawseti(L, -2, ret + 1); |
| 3888 | lua_pushboolean(L, 1); |
| 3889 | return 1; |
| 3890 | } |
| 3891 | |
| 3892 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 3893 | { |
| 3894 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3895 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3896 | |
| 3897 | if (status < 100 || status > 599) { |
| 3898 | lua_pushboolean(L, 0); |
| 3899 | return 1; |
| 3900 | } |
| 3901 | |
| 3902 | appctx->appctx->ctx.hlua_apphttp.status = status; |
| 3903 | lua_pushboolean(L, 1); |
| 3904 | return 1; |
| 3905 | } |
| 3906 | |
| 3907 | /* We will build the status line and the headers of the HTTP response. |
| 3908 | * We will try send at once if its not possible, we give back the hand |
| 3909 | * waiting for more room. |
| 3910 | */ |
| 3911 | __LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx) |
| 3912 | { |
| 3913 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3914 | struct stream_interface *si = appctx->appctx->owner; |
| 3915 | struct channel *chn = si_ic(si); |
| 3916 | int ret; |
| 3917 | size_t len; |
| 3918 | const char *msg; |
| 3919 | |
| 3920 | /* Get the message as the first argument on the stack. */ |
| 3921 | msg = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3922 | |
| 3923 | /* Send the message at once. */ |
| 3924 | ret = bi_putblk(chn, msg, len); |
| 3925 | |
| 3926 | /* if ret == -2 or -3 the channel closed or the message si too |
| 3927 | * big for the buffers. |
| 3928 | */ |
| 3929 | if (ret == -2 || ret == -3) { |
| 3930 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 3931 | WILL_LJMP(lua_error(L)); |
| 3932 | } |
| 3933 | |
| 3934 | /* If ret is -1, we dont have room in the buffer, so we yield. */ |
| 3935 | if (ret == -1) { |
| 3936 | si_applet_cant_put(si); |
| 3937 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0)); |
| 3938 | } |
| 3939 | |
| 3940 | /* Headers sent, set the flag. */ |
| 3941 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
| 3942 | return 0; |
| 3943 | } |
| 3944 | |
| 3945 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
| 3946 | { |
| 3947 | struct chunk *tmp = get_trash_chunk(); |
| 3948 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3949 | struct stream_interface *si = appctx->appctx->owner; |
| 3950 | struct stream *s = si_strm(si); |
| 3951 | struct http_txn *txn = s->txn; |
| 3952 | const char *name; |
| 3953 | const char *value; |
| 3954 | int id; |
| 3955 | int hdr_connection = 0; |
| 3956 | int hdr_contentlength = -1; |
| 3957 | int hdr_chunked = 0; |
| 3958 | |
| 3959 | /* Use the same http version than the request. */ |
| 3960 | chunk_appendf(tmp, "HTTP/1.%c %d %s\r\n", |
| 3961 | txn->req.flags & HTTP_MSGF_VER_11 ? '1' : '0', |
| 3962 | appctx->appctx->ctx.hlua_apphttp.status, |
| 3963 | get_reason(appctx->appctx->ctx.hlua_apphttp.status)); |
| 3964 | |
| 3965 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 3966 | lua_pushvalue(L, 0); |
| 3967 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 3968 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
| 3969 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 3970 | WILL_LJMP(lua_error(L)); |
| 3971 | } |
| 3972 | |
| 3973 | /* Browse the list of headers. */ |
| 3974 | lua_pushnil(L); |
| 3975 | while(lua_next(L, -2) != 0) { |
| 3976 | |
| 3977 | /* We expect a string as -2. */ |
| 3978 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 3979 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
| 3980 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 3981 | lua_typename(L, lua_type(L, -2))); |
| 3982 | WILL_LJMP(lua_error(L)); |
| 3983 | } |
| 3984 | name = lua_tostring(L, -2); |
| 3985 | |
| 3986 | /* We expect an array as -1. */ |
| 3987 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 3988 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
| 3989 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 3990 | name, |
| 3991 | lua_typename(L, lua_type(L, -1))); |
| 3992 | WILL_LJMP(lua_error(L)); |
| 3993 | } |
| 3994 | |
| 3995 | /* Browse the table who is on the top of the stack. */ |
| 3996 | lua_pushnil(L); |
| 3997 | while(lua_next(L, -2) != 0) { |
| 3998 | |
| 3999 | /* We expect a number as -2. */ |
| 4000 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 4001 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
| 4002 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4003 | name, |
| 4004 | lua_typename(L, lua_type(L, -2))); |
| 4005 | WILL_LJMP(lua_error(L)); |
| 4006 | } |
| 4007 | id = lua_tointeger(L, -2); |
| 4008 | |
| 4009 | /* We expect a string as -2. */ |
| 4010 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 4011 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
| 4012 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4013 | name, id, |
| 4014 | lua_typename(L, lua_type(L, -1))); |
| 4015 | WILL_LJMP(lua_error(L)); |
| 4016 | } |
| 4017 | value = lua_tostring(L, -1); |
| 4018 | |
| 4019 | /* Catenate a new header. */ |
| 4020 | chunk_appendf(tmp, "%s: %s\r\n", name, value); |
| 4021 | |
| 4022 | /* Protocol checks. */ |
| 4023 | |
| 4024 | /* Check if the header conneciton is present. */ |
| 4025 | if (strcasecmp("connection", name) == 0) |
| 4026 | hdr_connection = 1; |
| 4027 | |
| 4028 | /* Copy the header content length. The length conversion |
| 4029 | * is done without control. If it contains a ad value, this |
| 4030 | * is not our problem. |
| 4031 | */ |
| 4032 | if (strcasecmp("content-length", name) == 0) |
| 4033 | hdr_contentlength = atoi(value); |
| 4034 | |
| 4035 | /* Check if the client annouces a transfer-encoding chunked it self. */ |
| 4036 | if (strcasecmp("transfer-encoding", name) == 0 && |
| 4037 | strcasecmp("chunked", value) == 0) |
| 4038 | hdr_chunked = 1; |
| 4039 | |
| 4040 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4041 | lua_pop(L, 1); |
| 4042 | } |
| 4043 | |
| 4044 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4045 | lua_pop(L, 1); |
| 4046 | } |
| 4047 | |
| 4048 | /* If the http protocol version is 1.1, we expect an header "connection" set |
| 4049 | * to "close" to be HAProxy/keeplive compliant. Otherwise, we expect nothing. |
| 4050 | * If the header conneciton is present, don't change it, if it is not present, |
| 4051 | * we must set. |
| 4052 | * |
| 4053 | * we set a "connection: close" header for ensuring that the keepalive will be |
| 4054 | * respected by haproxy. HAProcy considers that the application cloe the connection |
| 4055 | * and it keep the connection from the client open. |
| 4056 | */ |
| 4057 | if (txn->req.flags & HTTP_MSGF_VER_11 && !hdr_connection) |
| 4058 | chunk_appendf(tmp, "Connection: close\r\n"); |
| 4059 | |
| 4060 | /* If we dont have a content-length set, we must announce a transfer enconding |
| 4061 | * chunked. This is required by haproxy for the keepalive compliance. |
| 4062 | * If the applet annouce a transfer-encoding chunked itslef, don't |
| 4063 | * do anything. |
| 4064 | */ |
| 4065 | if (hdr_contentlength == -1 && hdr_chunked == 0) { |
| 4066 | chunk_appendf(tmp, "Transfer-encoding: chunked\r\n"); |
| 4067 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_CHUNKED; |
| 4068 | } |
| 4069 | |
| 4070 | /* Finalize headers. */ |
| 4071 | chunk_appendf(tmp, "\r\n"); |
| 4072 | |
| 4073 | /* Remove the last entry and the array of headers */ |
| 4074 | lua_pop(L, 2); |
| 4075 | |
| 4076 | /* Push the headers block. */ |
| 4077 | lua_pushlstring(L, tmp->str, tmp->len); |
| 4078 | |
| 4079 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
| 4080 | } |
| 4081 | |
| 4082 | /* |
| 4083 | * |
| 4084 | * |
| 4085 | * Class HTTP |
| 4086 | * |
| 4087 | * |
| 4088 | */ |
| 4089 | |
| 4090 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4091 | * a class stream, otherwise it throws an error. |
| 4092 | */ |
| 4093 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
| 4094 | { |
| 4095 | return (struct hlua_txn *)MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
| 4096 | } |
| 4097 | |
| 4098 | /* This function creates and push in the stack a HTTP object |
| 4099 | * according with a current TXN. |
| 4100 | */ |
| 4101 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 4102 | { |
| 4103 | struct hlua_txn *htxn; |
| 4104 | |
| 4105 | /* Check stack size. */ |
| 4106 | if (!lua_checkstack(L, 3)) |
| 4107 | return 0; |
| 4108 | |
| 4109 | /* Create the object: obj[0] = userdata. |
| 4110 | * Note that the base of the Converters object is the |
| 4111 | * same than the TXN object. |
| 4112 | */ |
| 4113 | lua_newtable(L); |
| 4114 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 4115 | lua_rawseti(L, -2, 0); |
| 4116 | |
| 4117 | htxn->s = txn->s; |
| 4118 | htxn->p = txn->p; |
| 4119 | |
| 4120 | /* Pop a class stream metatable and affect it to the table. */ |
| 4121 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 4122 | lua_setmetatable(L, -2); |
| 4123 | |
| 4124 | return 1; |
| 4125 | } |
| 4126 | |
| 4127 | /* This function creates ans returns an array of HTTP headers. |
| 4128 | * This function does not fails. It is used as wrapper with the |
| 4129 | * 2 following functions. |
| 4130 | */ |
| 4131 | __LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4132 | { |
| 4133 | const char *cur_ptr, *cur_next, *p; |
| 4134 | int old_idx, cur_idx; |
| 4135 | struct hdr_idx_elem *cur_hdr; |
| 4136 | const char *hn, *hv; |
| 4137 | int hnl, hvl; |
| 4138 | int type; |
| 4139 | const char *in; |
| 4140 | char *out; |
| 4141 | int len; |
| 4142 | |
| 4143 | /* Create the table. */ |
| 4144 | lua_newtable(L); |
| 4145 | |
| 4146 | if (!htxn->s->txn) |
| 4147 | return 1; |
| 4148 | |
| 4149 | /* Build array of headers. */ |
| 4150 | old_idx = 0; |
| 4151 | cur_next = msg->chn->buf->p + hdr_idx_first_pos(&htxn->s->txn->hdr_idx); |
| 4152 | |
| 4153 | while (1) { |
| 4154 | cur_idx = htxn->s->txn->hdr_idx.v[old_idx].next; |
| 4155 | if (!cur_idx) |
| 4156 | break; |
| 4157 | old_idx = cur_idx; |
| 4158 | |
| 4159 | cur_hdr = &htxn->s->txn->hdr_idx.v[cur_idx]; |
| 4160 | cur_ptr = cur_next; |
| 4161 | cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1; |
| 4162 | |
| 4163 | /* Now we have one full header at cur_ptr of len cur_hdr->len, |
| 4164 | * and the next header starts at cur_next. We'll check |
| 4165 | * this header in the list as well as against the default |
| 4166 | * rule. |
| 4167 | */ |
| 4168 | |
| 4169 | /* look for ': *'. */ |
| 4170 | hn = cur_ptr; |
| 4171 | for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++); |
| 4172 | if (p >= cur_ptr+cur_hdr->len) |
| 4173 | continue; |
| 4174 | hnl = p - hn; |
| 4175 | p++; |
| 4176 | while (p < cur_ptr+cur_hdr->len && ( *p == ' ' || *p == '\t' )) |
| 4177 | p++; |
| 4178 | if (p >= cur_ptr+cur_hdr->len) |
| 4179 | continue; |
| 4180 | hv = p; |
| 4181 | hvl = cur_ptr+cur_hdr->len-p; |
| 4182 | |
| 4183 | /* Lowercase the key. Don't check the size of trash, it have |
| 4184 | * the size of one buffer and the input data contains in one |
| 4185 | * buffer. |
| 4186 | */ |
| 4187 | out = trash.str; |
| 4188 | for (in=hn; in<hn+hnl; in++, out++) |
| 4189 | *out = tolower(*in); |
| 4190 | *out = '\0'; |
| 4191 | |
| 4192 | /* Check for existing entry: |
| 4193 | * assume that the table is on the top of the stack, and |
| 4194 | * push the key in the stack, the function lua_gettable() |
| 4195 | * perform the lookup. |
| 4196 | */ |
| 4197 | lua_pushlstring(L, trash.str, hnl); |
| 4198 | lua_gettable(L, -2); |
| 4199 | type = lua_type(L, -1); |
| 4200 | |
| 4201 | switch (type) { |
| 4202 | case LUA_TNIL: |
| 4203 | /* Table not found, create it. */ |
| 4204 | lua_pop(L, 1); /* remove the nil value. */ |
| 4205 | lua_pushlstring(L, trash.str, hnl); /* push the header name as key. */ |
| 4206 | lua_newtable(L); /* create and push empty table. */ |
| 4207 | lua_pushlstring(L, hv, hvl); /* push header value. */ |
| 4208 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 4209 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
| 4210 | break; |
| 4211 | |
| 4212 | case LUA_TTABLE: |
| 4213 | /* Entry found: push the value in the table. */ |
| 4214 | len = lua_rawlen(L, -1); |
| 4215 | lua_pushlstring(L, hv, hvl); /* push header value. */ |
| 4216 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 4217 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 4218 | break; |
| 4219 | |
| 4220 | default: |
| 4221 | /* Other cases are errors. */ |
| 4222 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 4223 | WILL_LJMP(lua_error(L)); |
| 4224 | } |
| 4225 | } |
| 4226 | |
| 4227 | return 1; |
| 4228 | } |
| 4229 | |
| 4230 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 4231 | { |
| 4232 | struct hlua_txn *htxn; |
| 4233 | |
| 4234 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 4235 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4236 | |
| 4237 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->req); |
| 4238 | } |
| 4239 | |
| 4240 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 4241 | { |
| 4242 | struct hlua_txn *htxn; |
| 4243 | |
| 4244 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 4245 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4246 | |
| 4247 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp); |
| 4248 | } |
| 4249 | |
| 4250 | /* This function replace full header, or just a value in |
| 4251 | * the request or in the response. It is a wrapper fir the |
| 4252 | * 4 following functions. |
| 4253 | */ |
| 4254 | __LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn, |
| 4255 | struct http_msg *msg, int action) |
| 4256 | { |
| 4257 | size_t name_len; |
| 4258 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4259 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 4260 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
| 4261 | struct my_regex re; |
| 4262 | |
| 4263 | if (!regex_comp(reg, &re, 1, 1, NULL)) |
| 4264 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 4265 | |
| 4266 | http_transform_header_str(htxn->s, msg, name, name_len, value, &re, action); |
| 4267 | regex_free(&re); |
| 4268 | return 0; |
| 4269 | } |
| 4270 | |
| 4271 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 4272 | { |
| 4273 | struct hlua_txn *htxn; |
| 4274 | |
| 4275 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4276 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4277 | |
| 4278 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR)); |
| 4279 | } |
| 4280 | |
| 4281 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 4282 | { |
| 4283 | struct hlua_txn *htxn; |
| 4284 | |
| 4285 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 4286 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4287 | |
| 4288 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR)); |
| 4289 | } |
| 4290 | |
| 4291 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 4292 | { |
| 4293 | struct hlua_txn *htxn; |
| 4294 | |
| 4295 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4296 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4297 | |
| 4298 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL)); |
| 4299 | } |
| 4300 | |
| 4301 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 4302 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4303 | struct hlua_txn *htxn; |
| 4304 | |
| 4305 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 4306 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4307 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 4308 | 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] | 4309 | } |
| 4310 | |
| 4311 | /* This function deletes all the occurences of an header. |
| 4312 | * It is a wrapper for the 2 following functions. |
| 4313 | */ |
| 4314 | __LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4315 | { |
| 4316 | size_t len; |
| 4317 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4318 | struct hdr_ctx ctx; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4319 | struct http_txn *txn = htxn->s->txn; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4320 | |
| 4321 | ctx.idx = 0; |
| 4322 | while (http_find_header2(name, len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) |
| 4323 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 4324 | return 0; |
| 4325 | } |
| 4326 | |
| 4327 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 4328 | { |
| 4329 | struct hlua_txn *htxn; |
| 4330 | |
| 4331 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 4332 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4333 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4334 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4335 | } |
| 4336 | |
| 4337 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 4338 | { |
| 4339 | struct hlua_txn *htxn; |
| 4340 | |
| 4341 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 4342 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4343 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4344 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4345 | } |
| 4346 | |
| 4347 | /* This function adds an header. It is a wrapper used by |
| 4348 | * the 2 following functions. |
| 4349 | */ |
| 4350 | __LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4351 | { |
| 4352 | size_t name_len; |
| 4353 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4354 | size_t value_len; |
| 4355 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
| 4356 | char *p; |
| 4357 | |
| 4358 | /* Check length. */ |
| 4359 | trash.len = value_len + name_len + 2; |
| 4360 | if (trash.len > trash.size) |
| 4361 | return 0; |
| 4362 | |
| 4363 | /* Creates the header string. */ |
| 4364 | p = trash.str; |
| 4365 | memcpy(p, name, name_len); |
| 4366 | p += name_len; |
| 4367 | *p = ':'; |
| 4368 | p++; |
| 4369 | *p = ' '; |
| 4370 | p++; |
| 4371 | memcpy(p, value, value_len); |
| 4372 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4373 | 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] | 4374 | trash.str, trash.len) != 0); |
| 4375 | |
| 4376 | return 0; |
| 4377 | } |
| 4378 | |
| 4379 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 4380 | { |
| 4381 | struct hlua_txn *htxn; |
| 4382 | |
| 4383 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 4384 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4385 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4386 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4387 | } |
| 4388 | |
| 4389 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 4390 | { |
| 4391 | struct hlua_txn *htxn; |
| 4392 | |
| 4393 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 4394 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4395 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4396 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4397 | } |
| 4398 | |
| 4399 | static int hlua_http_req_set_hdr(lua_State *L) |
| 4400 | { |
| 4401 | struct hlua_txn *htxn; |
| 4402 | |
| 4403 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 4404 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4405 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4406 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
| 4407 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4408 | } |
| 4409 | |
| 4410 | static int hlua_http_res_set_hdr(lua_State *L) |
| 4411 | { |
| 4412 | struct hlua_txn *htxn; |
| 4413 | |
| 4414 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 4415 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4416 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4417 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
| 4418 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4419 | } |
| 4420 | |
| 4421 | /* This function set the method. */ |
| 4422 | static int hlua_http_req_set_meth(lua_State *L) |
| 4423 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4424 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4425 | size_t name_len; |
| 4426 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4427 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4428 | 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] | 4429 | return 1; |
| 4430 | } |
| 4431 | |
| 4432 | /* This function set the method. */ |
| 4433 | static int hlua_http_req_set_path(lua_State *L) |
| 4434 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4435 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4436 | size_t name_len; |
| 4437 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4438 | 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] | 4439 | return 1; |
| 4440 | } |
| 4441 | |
| 4442 | /* This function set the query-string. */ |
| 4443 | static int hlua_http_req_set_query(lua_State *L) |
| 4444 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4445 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4446 | size_t name_len; |
| 4447 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4448 | |
| 4449 | /* Check length. */ |
| 4450 | if (name_len > trash.size - 1) { |
| 4451 | lua_pushboolean(L, 0); |
| 4452 | return 1; |
| 4453 | } |
| 4454 | |
| 4455 | /* Add the mark question as prefix. */ |
| 4456 | chunk_reset(&trash); |
| 4457 | trash.str[trash.len++] = '?'; |
| 4458 | memcpy(trash.str + trash.len, name, name_len); |
| 4459 | trash.len += name_len; |
| 4460 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4461 | 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] | 4462 | return 1; |
| 4463 | } |
| 4464 | |
| 4465 | /* This function set the uri. */ |
| 4466 | static int hlua_http_req_set_uri(lua_State *L) |
| 4467 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4468 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4469 | size_t name_len; |
| 4470 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4471 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4472 | 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] | 4473 | return 1; |
| 4474 | } |
| 4475 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 4476 | /* This function set the response code. */ |
| 4477 | static int hlua_http_res_set_status(lua_State *L) |
| 4478 | { |
| 4479 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4480 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4481 | |
| 4482 | http_set_status(code, htxn->s); |
| 4483 | return 0; |
| 4484 | } |
| 4485 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4486 | /* |
| 4487 | * |
| 4488 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4489 | * Class TXN |
| 4490 | * |
| 4491 | * |
| 4492 | */ |
| 4493 | |
| 4494 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4495 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4496 | */ |
| 4497 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 4498 | { |
| 4499 | return (struct hlua_txn *)MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
| 4500 | } |
| 4501 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 4502 | __LJMP static int hlua_set_var(lua_State *L) |
| 4503 | { |
| 4504 | struct hlua_txn *htxn; |
| 4505 | const char *name; |
| 4506 | size_t len; |
| 4507 | struct sample smp; |
| 4508 | |
| 4509 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 4510 | |
| 4511 | /* It is useles to retrieve the stream, but this function |
| 4512 | * runs only in a stream context. |
| 4513 | */ |
| 4514 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 4515 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4516 | |
| 4517 | /* Converts the third argument in a sample. */ |
| 4518 | hlua_lua2smp(L, 3, &smp); |
| 4519 | |
| 4520 | /* Store the sample in a variable. */ |
| 4521 | vars_set_by_name(name, len, htxn->s, &smp); |
| 4522 | return 0; |
| 4523 | } |
| 4524 | |
| 4525 | __LJMP static int hlua_get_var(lua_State *L) |
| 4526 | { |
| 4527 | struct hlua_txn *htxn; |
| 4528 | const char *name; |
| 4529 | size_t len; |
| 4530 | struct sample smp; |
| 4531 | |
| 4532 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4533 | |
| 4534 | /* It is useles to retrieve the stream, but this function |
| 4535 | * runs only in a stream context. |
| 4536 | */ |
| 4537 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 4538 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4539 | |
| 4540 | if (!vars_get_by_name(name, len, htxn->s, &smp)) { |
| 4541 | lua_pushnil(L); |
| 4542 | return 1; |
| 4543 | } |
| 4544 | |
| 4545 | return hlua_smp2lua(L, &smp); |
| 4546 | } |
| 4547 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 4548 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 4549 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4550 | struct hlua *hlua; |
| 4551 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 4552 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4553 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4554 | /* It is useles to retrieve the stream, but this function |
| 4555 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 4556 | */ |
| 4557 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4558 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 4559 | |
| 4560 | /* Remove previous value. */ |
| 4561 | if (hlua->Mref != -1) |
| 4562 | luaL_unref(L, hlua->Mref, LUA_REGISTRYINDEX); |
| 4563 | |
| 4564 | /* Get and store new value. */ |
| 4565 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4566 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4567 | |
| 4568 | return 0; |
| 4569 | } |
| 4570 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 4571 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 4572 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4573 | struct hlua *hlua; |
| 4574 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 4575 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 4576 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4577 | /* It is useles to retrieve the stream, but this function |
| 4578 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 4579 | */ |
| 4580 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4581 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 4582 | |
| 4583 | /* Push configuration index in the stack. */ |
| 4584 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4585 | |
| 4586 | return 1; |
| 4587 | } |
| 4588 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4589 | /* Create stack entry containing a class TXN. This function |
| 4590 | * return 0 if the stack does not contains free slots, |
| 4591 | * otherwise it returns 1. |
| 4592 | */ |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 4593 | 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] | 4594 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 4595 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4596 | |
| 4597 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 4598 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4599 | return 0; |
| 4600 | |
| 4601 | /* NOTE: The allocation never fails. The failure |
| 4602 | * throw an error, and the function never returns. |
| 4603 | * if the throw is not avalaible, the process is aborted. |
| 4604 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 4605 | /* Create the object: obj[0] = userdata. */ |
| 4606 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 4607 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 4608 | lua_rawseti(L, -2, 0); |
| 4609 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 4610 | htxn->s = s; |
| 4611 | htxn->p = p; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4612 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4613 | /* Create the "f" field that contains a list of fetches. */ |
| 4614 | lua_pushstring(L, "f"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 4615 | if (!hlua_fetches_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4616 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 4617 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4618 | |
| 4619 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4620 | lua_pushstring(L, "sf"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 4621 | if (!hlua_fetches_new(L, htxn, 1)) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4622 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 4623 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4624 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4625 | /* Create the "c" field that contains a list of converters. */ |
| 4626 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 4627 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4628 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 4629 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4630 | |
| 4631 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4632 | lua_pushstring(L, "sc"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 4633 | if (!hlua_converters_new(L, htxn, 1)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4634 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 4635 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4636 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 4637 | /* Create the "req" field that contains the request channel object. */ |
| 4638 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 4639 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 4640 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 4641 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 4642 | |
| 4643 | /* Create the "res" field that contains the response channel object. */ |
| 4644 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 4645 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 4646 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 4647 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 4648 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4649 | /* Creates the HTTP object is the current proxy allows http. */ |
| 4650 | lua_pushstring(L, "http"); |
| 4651 | if (p->mode == PR_MODE_HTTP) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 4652 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4653 | return 0; |
| 4654 | } |
| 4655 | else |
| 4656 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 4657 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4658 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4659 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 4660 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 4661 | lua_setmetatable(L, -2); |
| 4662 | |
| 4663 | return 1; |
| 4664 | } |
| 4665 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 4666 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 4667 | { |
| 4668 | const char *msg; |
| 4669 | struct hlua_txn *htxn; |
| 4670 | |
| 4671 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 4672 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 4673 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4674 | |
| 4675 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 4676 | return 0; |
| 4677 | } |
| 4678 | |
| 4679 | __LJMP static int hlua_txn_log(lua_State *L) |
| 4680 | { |
| 4681 | int level; |
| 4682 | const char *msg; |
| 4683 | struct hlua_txn *htxn; |
| 4684 | |
| 4685 | MAY_LJMP(check_args(L, 3, "log")); |
| 4686 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 4687 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4688 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 4689 | |
| 4690 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 4691 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 4692 | |
| 4693 | hlua_sendlog(htxn->s->be, level, msg); |
| 4694 | return 0; |
| 4695 | } |
| 4696 | |
| 4697 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 4698 | { |
| 4699 | const char *msg; |
| 4700 | struct hlua_txn *htxn; |
| 4701 | |
| 4702 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 4703 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 4704 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4705 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 4706 | return 0; |
| 4707 | } |
| 4708 | |
| 4709 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 4710 | { |
| 4711 | const char *msg; |
| 4712 | struct hlua_txn *htxn; |
| 4713 | |
| 4714 | MAY_LJMP(check_args(L, 2, "Info")); |
| 4715 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 4716 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4717 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 4718 | return 0; |
| 4719 | } |
| 4720 | |
| 4721 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 4722 | { |
| 4723 | const char *msg; |
| 4724 | struct hlua_txn *htxn; |
| 4725 | |
| 4726 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 4727 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 4728 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4729 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 4730 | return 0; |
| 4731 | } |
| 4732 | |
| 4733 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 4734 | { |
| 4735 | const char *msg; |
| 4736 | struct hlua_txn *htxn; |
| 4737 | |
| 4738 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 4739 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 4740 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4741 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 4742 | return 0; |
| 4743 | } |
| 4744 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 4745 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 4746 | { |
| 4747 | struct hlua_txn *htxn; |
| 4748 | int ll; |
| 4749 | |
| 4750 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 4751 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 4752 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4753 | |
| 4754 | if (ll < 0 || ll > 7) |
| 4755 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 4756 | |
| 4757 | htxn->s->logs.level = ll; |
| 4758 | return 0; |
| 4759 | } |
| 4760 | |
| 4761 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 4762 | { |
| 4763 | struct hlua_txn *htxn; |
| 4764 | struct connection *cli_conn; |
| 4765 | int tos; |
| 4766 | |
| 4767 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 4768 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 4769 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4770 | |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 4771 | 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] | 4772 | inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, tos); |
| 4773 | |
| 4774 | return 0; |
| 4775 | } |
| 4776 | |
| 4777 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 4778 | { |
| 4779 | #ifdef SO_MARK |
| 4780 | struct hlua_txn *htxn; |
| 4781 | struct connection *cli_conn; |
| 4782 | int mark; |
| 4783 | |
| 4784 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 4785 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 4786 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4787 | |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 4788 | 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] | 4789 | 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] | 4790 | #endif |
| 4791 | return 0; |
| 4792 | } |
| 4793 | |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 4794 | /* This function is an Lua binding that send pending data |
| 4795 | * to the client, and close the stream interface. |
| 4796 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 4797 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 4798 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4799 | struct hlua_txn *htxn; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 4800 | struct channel *ic, *oc; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 4801 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4802 | MAY_LJMP(check_args(L, 1, "close")); |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4803 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 4804 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4805 | ic = &htxn->s->req; |
| 4806 | oc = &htxn->s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 4807 | |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 4808 | if (htxn->s->txn) { |
| 4809 | /* HTTP mode, let's stay in sync with the stream */ |
| 4810 | bi_fast_delete(ic->buf, htxn->s->txn->req.sov); |
| 4811 | htxn->s->txn->req.next -= htxn->s->txn->req.sov; |
| 4812 | htxn->s->txn->req.sov = 0; |
| 4813 | ic->analysers &= AN_REQ_HTTP_XFER_BODY; |
| 4814 | oc->analysers = AN_RES_HTTP_XFER_BODY; |
| 4815 | htxn->s->txn->req.msg_state = HTTP_MSG_CLOSED; |
| 4816 | htxn->s->txn->rsp.msg_state = HTTP_MSG_DONE; |
| 4817 | |
| 4818 | /* Trim any possible response */ |
| 4819 | oc->buf->i = 0; |
| 4820 | htxn->s->txn->rsp.next = htxn->s->txn->rsp.sov = 0; |
| 4821 | |
| 4822 | /* Note that if we want to support keep-alive, we need |
| 4823 | * to bypass the close/shutr_now calls below, but that |
| 4824 | * may only be done if the HTTP request was already |
| 4825 | * processed and the connection header is known (ie |
| 4826 | * not during TCP rules). |
| 4827 | */ |
| 4828 | } |
| 4829 | |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 4830 | channel_auto_read(ic); |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 4831 | channel_abort(ic); |
| 4832 | channel_auto_close(ic); |
| 4833 | channel_erase(ic); |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 4834 | |
| 4835 | oc->wex = tick_add_ifset(now_ms, oc->wto); |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 4836 | channel_auto_read(oc); |
| 4837 | channel_auto_close(oc); |
| 4838 | channel_shutr_now(oc); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 4839 | |
Willy Tarreau | 0458b08 | 2015-08-28 09:40:04 +0200 | [diff] [blame] | 4840 | ic->analysers = 0; |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 4841 | |
| 4842 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 4843 | return 0; |
| 4844 | } |
| 4845 | |
| 4846 | __LJMP static int hlua_log(lua_State *L) |
| 4847 | { |
| 4848 | int level; |
| 4849 | const char *msg; |
| 4850 | |
| 4851 | MAY_LJMP(check_args(L, 2, "log")); |
| 4852 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 4853 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4854 | |
| 4855 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 4856 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 4857 | |
| 4858 | hlua_sendlog(NULL, level, msg); |
| 4859 | return 0; |
| 4860 | } |
| 4861 | |
| 4862 | __LJMP static int hlua_log_debug(lua_State *L) |
| 4863 | { |
| 4864 | const char *msg; |
| 4865 | |
| 4866 | MAY_LJMP(check_args(L, 1, "debug")); |
| 4867 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 4868 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 4869 | return 0; |
| 4870 | } |
| 4871 | |
| 4872 | __LJMP static int hlua_log_info(lua_State *L) |
| 4873 | { |
| 4874 | const char *msg; |
| 4875 | |
| 4876 | MAY_LJMP(check_args(L, 1, "info")); |
| 4877 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 4878 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 4879 | return 0; |
| 4880 | } |
| 4881 | |
| 4882 | __LJMP static int hlua_log_warning(lua_State *L) |
| 4883 | { |
| 4884 | const char *msg; |
| 4885 | |
| 4886 | MAY_LJMP(check_args(L, 1, "warning")); |
| 4887 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 4888 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 4889 | return 0; |
| 4890 | } |
| 4891 | |
| 4892 | __LJMP static int hlua_log_alert(lua_State *L) |
| 4893 | { |
| 4894 | const char *msg; |
| 4895 | |
| 4896 | MAY_LJMP(check_args(L, 1, "alert")); |
| 4897 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 4898 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 4899 | return 0; |
| 4900 | } |
| 4901 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 4902 | __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] | 4903 | { |
| 4904 | int wakeup_ms = lua_tointeger(L, -1); |
| 4905 | if (now_ms < wakeup_ms) |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 4906 | 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] | 4907 | return 0; |
| 4908 | } |
| 4909 | |
| 4910 | __LJMP static int hlua_sleep(lua_State *L) |
| 4911 | { |
| 4912 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 4913 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 4914 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 4915 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 4916 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 4917 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 4918 | wakeup_ms = tick_add(now_ms, delay); |
| 4919 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 4920 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 4921 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0)); |
| 4922 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 4923 | } |
| 4924 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 4925 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 4926 | { |
| 4927 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 4928 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 4929 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 4930 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 4931 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 4932 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 4933 | wakeup_ms = tick_add(now_ms, delay); |
| 4934 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 4935 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 4936 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0)); |
| 4937 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 4938 | } |
| 4939 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 4940 | /* This functionis an LUA binding. it permits to give back |
| 4941 | * the hand at the HAProxy scheduler. It is used when the |
| 4942 | * LUA processing consumes a lot of time. |
| 4943 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 4944 | __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] | 4945 | { |
| 4946 | return 0; |
| 4947 | } |
| 4948 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 4949 | __LJMP static int hlua_yield(lua_State *L) |
| 4950 | { |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 4951 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD)); |
| 4952 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 4953 | } |
| 4954 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 4955 | /* This function change the nice of the currently executed |
| 4956 | * task. It is used set low or high priority at the current |
| 4957 | * task. |
| 4958 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 4959 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 4960 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4961 | struct hlua *hlua; |
| 4962 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 4963 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4964 | MAY_LJMP(check_args(L, 1, "set_nice")); |
| 4965 | hlua = hlua_gethlua(L); |
| 4966 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 4967 | |
| 4968 | /* If he task is not set, I'm in a start mode. */ |
| 4969 | if (!hlua || !hlua->task) |
| 4970 | return 0; |
| 4971 | |
| 4972 | if (nice < -1024) |
| 4973 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4974 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 4975 | nice = 1024; |
| 4976 | |
| 4977 | hlua->task->nice = nice; |
| 4978 | return 0; |
| 4979 | } |
| 4980 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 4981 | /* This function is used as a calback of a task. It is called by the |
| 4982 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 4983 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 4984 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 4985 | * |
| 4986 | * Task wrapper are longjmp safe because the only one Lua code |
| 4987 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 4988 | */ |
| 4989 | static struct task *hlua_process_task(struct task *task) |
| 4990 | { |
| 4991 | struct hlua *hlua = task->context; |
| 4992 | enum hlua_exec status; |
| 4993 | |
| 4994 | /* We need to remove the task from the wait queue before executing |
| 4995 | * the Lua code because we don't know if it needs to wait for |
| 4996 | * another timer or not in the case of E_AGAIN. |
| 4997 | */ |
| 4998 | task_delete(task); |
| 4999 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5000 | /* If it is the first call to the task, we must initialize the |
| 5001 | * execution timeouts. |
| 5002 | */ |
| 5003 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 5004 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5005 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5006 | /* Execute the Lua code. */ |
| 5007 | status = hlua_ctx_resume(hlua, 1); |
| 5008 | |
| 5009 | switch (status) { |
| 5010 | /* finished or yield */ |
| 5011 | case HLUA_E_OK: |
| 5012 | hlua_ctx_destroy(hlua); |
| 5013 | task_delete(task); |
| 5014 | task_free(task); |
| 5015 | break; |
| 5016 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 5017 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
| 5018 | if (hlua->wake_time != TICK_ETERNITY) |
| 5019 | task_schedule(task, hlua->wake_time); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5020 | break; |
| 5021 | |
| 5022 | /* finished with error. */ |
| 5023 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5024 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5025 | hlua_ctx_destroy(hlua); |
| 5026 | task_delete(task); |
| 5027 | task_free(task); |
| 5028 | break; |
| 5029 | |
| 5030 | case HLUA_E_ERR: |
| 5031 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5032 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5033 | hlua_ctx_destroy(hlua); |
| 5034 | task_delete(task); |
| 5035 | task_free(task); |
| 5036 | break; |
| 5037 | } |
| 5038 | return NULL; |
| 5039 | } |
| 5040 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 5041 | /* This function is an LUA binding that register LUA function to be |
| 5042 | * executed after the HAProxy configuration parsing and before the |
| 5043 | * HAProxy scheduler starts. This function expect only one LUA |
| 5044 | * argument that is a function. This function returns nothing, but |
| 5045 | * throws if an error is encountered. |
| 5046 | */ |
| 5047 | __LJMP static int hlua_register_init(lua_State *L) |
| 5048 | { |
| 5049 | struct hlua_init_function *init; |
| 5050 | int ref; |
| 5051 | |
| 5052 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 5053 | |
| 5054 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 5055 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5056 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 5057 | if (!init) |
| 5058 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5059 | |
| 5060 | init->function_ref = ref; |
| 5061 | LIST_ADDQ(&hlua_init_functions, &init->l); |
| 5062 | return 0; |
| 5063 | } |
| 5064 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5065 | /* This functio is an LUA binding. It permits to register a task |
| 5066 | * executed in parallel of the main HAroxy activity. The task is |
| 5067 | * created and it is set in the HAProxy scheduler. It can be called |
| 5068 | * from the "init" section, "post init" or during the runtime. |
| 5069 | * |
| 5070 | * Lua prototype: |
| 5071 | * |
| 5072 | * <none> core.register_task(<function>) |
| 5073 | */ |
| 5074 | static int hlua_register_task(lua_State *L) |
| 5075 | { |
| 5076 | struct hlua *hlua; |
| 5077 | struct task *task; |
| 5078 | int ref; |
| 5079 | |
| 5080 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 5081 | |
| 5082 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 5083 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5084 | hlua = calloc(1, sizeof(*hlua)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5085 | if (!hlua) |
| 5086 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5087 | |
| 5088 | task = task_new(); |
| 5089 | task->context = hlua; |
| 5090 | task->process = hlua_process_task; |
| 5091 | |
| 5092 | if (!hlua_ctx_init(hlua, task)) |
| 5093 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5094 | |
| 5095 | /* Restore the function in the stack. */ |
| 5096 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 5097 | hlua->nargs = 0; |
| 5098 | |
| 5099 | /* Schedule task. */ |
| 5100 | task_schedule(task, now_ms); |
| 5101 | |
| 5102 | return 0; |
| 5103 | } |
| 5104 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5105 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 5106 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 5107 | * resume converters. |
| 5108 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5109 | 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] | 5110 | { |
| 5111 | struct hlua_function *fcn = (struct hlua_function *)private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5112 | struct stream *stream = smp->strm; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5113 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5114 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5115 | * Lua context can be not initialized. This behavior |
| 5116 | * permits to save performances because a systematic |
| 5117 | * Lua initialization cause 5% performances loss. |
| 5118 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5119 | if (!stream->hlua.T && !hlua_ctx_init(&stream->hlua, stream->task)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5120 | 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] | 5121 | return 0; |
| 5122 | } |
| 5123 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5124 | /* If it is the first run, initialize the data for the call. */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5125 | if (!HLUA_IS_RUNNING(&stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5126 | |
| 5127 | /* The following Lua calls can fail. */ |
| 5128 | if (!SET_SAFE_LJMP(stream->hlua.T)) { |
| 5129 | SEND_ERR(stream->be, "Lua converter '%s': critical error.\n", fcn->name); |
| 5130 | return 0; |
| 5131 | } |
| 5132 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5133 | /* Check stack available size. */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5134 | if (!lua_checkstack(stream->hlua.T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5135 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 10e5bc7 | 2015-09-25 23:51:34 +0200 | [diff] [blame] | 5136 | RESET_SAFE_LJMP(stream->hlua.T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5137 | return 0; |
| 5138 | } |
| 5139 | |
| 5140 | /* Restore the function in the stack. */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5141 | lua_rawgeti(stream->hlua.T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5142 | |
| 5143 | /* convert input sample and pust-it in the stack. */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5144 | if (!lua_checkstack(stream->hlua.T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5145 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 10e5bc7 | 2015-09-25 23:51:34 +0200 | [diff] [blame] | 5146 | RESET_SAFE_LJMP(stream->hlua.T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5147 | return 0; |
| 5148 | } |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5149 | hlua_smp2lua(stream->hlua.T, smp); |
| 5150 | stream->hlua.nargs = 2; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5151 | |
| 5152 | /* push keywords in the stack. */ |
| 5153 | if (arg_p) { |
| 5154 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5155 | if (!lua_checkstack(stream->hlua.T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5156 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 10e5bc7 | 2015-09-25 23:51:34 +0200 | [diff] [blame] | 5157 | RESET_SAFE_LJMP(stream->hlua.T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5158 | return 0; |
| 5159 | } |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5160 | hlua_arg2lua(stream->hlua.T, arg_p); |
| 5161 | stream->hlua.nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5162 | } |
| 5163 | } |
| 5164 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5165 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 5166 | stream->hlua.max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5167 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5168 | /* At this point the execution is safe. */ |
| 5169 | RESET_SAFE_LJMP(stream->hlua.T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5170 | } |
| 5171 | |
| 5172 | /* Execute the function. */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5173 | switch (hlua_ctx_resume(&stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5174 | /* finished. */ |
| 5175 | case HLUA_E_OK: |
| 5176 | /* Convert the returned value in sample. */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5177 | hlua_lua2smp(stream->hlua.T, -1, smp); |
| 5178 | lua_pop(stream->hlua.T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5179 | return 1; |
| 5180 | |
| 5181 | /* yield. */ |
| 5182 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5183 | 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] | 5184 | return 0; |
| 5185 | |
| 5186 | /* finished with error. */ |
| 5187 | case HLUA_E_ERRMSG: |
| 5188 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5189 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
| 5190 | fcn->name, lua_tostring(stream->hlua.T, -1)); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5191 | lua_pop(stream->hlua.T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5192 | return 0; |
| 5193 | |
| 5194 | case HLUA_E_ERR: |
| 5195 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5196 | 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] | 5197 | |
| 5198 | default: |
| 5199 | return 0; |
| 5200 | } |
| 5201 | } |
| 5202 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5203 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 5204 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 5205 | * resume sample-fetches. |
| 5206 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5207 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 5208 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5209 | { |
| 5210 | struct hlua_function *fcn = (struct hlua_function *)private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5211 | struct stream *stream = smp->strm; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5212 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5213 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5214 | * Lua context can be not initialized. This behavior |
| 5215 | * permits to save performances because a systematic |
| 5216 | * Lua initialization cause 5% performances loss. |
| 5217 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5218 | if (!stream->hlua.T && !hlua_ctx_init(&stream->hlua, stream->task)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5219 | 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] | 5220 | return 0; |
| 5221 | } |
| 5222 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5223 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5224 | if (!HLUA_IS_RUNNING(&stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5225 | |
| 5226 | /* The following Lua calls can fail. */ |
| 5227 | if (!SET_SAFE_LJMP(stream->hlua.T)) { |
| 5228 | SEND_ERR(smp->px, "Lua sample-fetch '%s': critical error.\n", fcn->name); |
| 5229 | return 0; |
| 5230 | } |
| 5231 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5232 | /* Check stack available size. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5233 | if (!lua_checkstack(stream->hlua.T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5234 | 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] | 5235 | RESET_SAFE_LJMP(stream->hlua.T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5236 | return 0; |
| 5237 | } |
| 5238 | |
| 5239 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5240 | lua_rawgeti(stream->hlua.T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5241 | |
| 5242 | /* push arguments in the stack. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5243 | if (!hlua_txn_new(stream->hlua.T, stream, smp->px)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5244 | 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] | 5245 | RESET_SAFE_LJMP(stream->hlua.T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5246 | return 0; |
| 5247 | } |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5248 | stream->hlua.nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5249 | |
| 5250 | /* push keywords in the stack. */ |
| 5251 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 5252 | /* Check stack available size. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5253 | if (!lua_checkstack(stream->hlua.T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5254 | 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] | 5255 | RESET_SAFE_LJMP(stream->hlua.T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5256 | return 0; |
| 5257 | } |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5258 | if (!lua_checkstack(stream->hlua.T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5259 | 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] | 5260 | RESET_SAFE_LJMP(stream->hlua.T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5261 | return 0; |
| 5262 | } |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5263 | hlua_arg2lua(stream->hlua.T, arg_p); |
| 5264 | stream->hlua.nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5265 | } |
| 5266 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5267 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 5268 | stream->hlua.max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5269 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5270 | /* At this point the execution is safe. */ |
| 5271 | RESET_SAFE_LJMP(stream->hlua.T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5272 | } |
| 5273 | |
| 5274 | /* Execute the function. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5275 | switch (hlua_ctx_resume(&stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5276 | /* finished. */ |
| 5277 | case HLUA_E_OK: |
Thierry FOURNIER | 26a7aac | 2015-10-13 14:25:11 +0200 | [diff] [blame^] | 5278 | if (!hlua_check_proto(stream, (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES)) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 5279 | return 0; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5280 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5281 | hlua_lua2smp(stream->hlua.T, -1, smp); |
| 5282 | lua_pop(stream->hlua.T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5283 | |
| 5284 | /* Set the end of execution flag. */ |
| 5285 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 5286 | return 1; |
| 5287 | |
| 5288 | /* yield. */ |
| 5289 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 26a7aac | 2015-10-13 14:25:11 +0200 | [diff] [blame^] | 5290 | hlua_check_proto(stream, (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES); |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5291 | 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] | 5292 | return 0; |
| 5293 | |
| 5294 | /* finished with error. */ |
| 5295 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 26a7aac | 2015-10-13 14:25:11 +0200 | [diff] [blame^] | 5296 | hlua_check_proto(stream, (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5297 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5298 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
| 5299 | fcn->name, lua_tostring(stream->hlua.T, -1)); |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5300 | lua_pop(stream->hlua.T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5301 | return 0; |
| 5302 | |
| 5303 | case HLUA_E_ERR: |
Thierry FOURNIER | 26a7aac | 2015-10-13 14:25:11 +0200 | [diff] [blame^] | 5304 | hlua_check_proto(stream, (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5305 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5306 | 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] | 5307 | |
| 5308 | default: |
| 5309 | return 0; |
| 5310 | } |
| 5311 | } |
| 5312 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5313 | /* This function is an LUA binding used for registering |
| 5314 | * "sample-conv" functions. It expects a converter name used |
| 5315 | * in the haproxy configuration file, and an LUA function. |
| 5316 | */ |
| 5317 | __LJMP static int hlua_register_converters(lua_State *L) |
| 5318 | { |
| 5319 | struct sample_conv_kw_list *sck; |
| 5320 | const char *name; |
| 5321 | int ref; |
| 5322 | int len; |
| 5323 | struct hlua_function *fcn; |
| 5324 | |
| 5325 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 5326 | |
| 5327 | /* First argument : converter name. */ |
| 5328 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5329 | |
| 5330 | /* Second argument : lua function. */ |
| 5331 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 5332 | |
| 5333 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5334 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5335 | if (!sck) |
| 5336 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5337 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5338 | if (!fcn) |
| 5339 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5340 | |
| 5341 | /* Fill fcn. */ |
| 5342 | fcn->name = strdup(name); |
| 5343 | if (!fcn->name) |
| 5344 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5345 | fcn->function_ref = ref; |
| 5346 | |
| 5347 | /* List head */ |
| 5348 | sck->list.n = sck->list.p = NULL; |
| 5349 | |
| 5350 | /* converter keyword. */ |
| 5351 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5352 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5353 | if (!sck->kw[0].kw) |
| 5354 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5355 | |
| 5356 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 5357 | sck->kw[0].process = hlua_sample_conv_wrapper; |
| 5358 | sck->kw[0].arg_mask = ARG5(0,STR,STR,STR,STR,STR); |
| 5359 | sck->kw[0].val_args = NULL; |
| 5360 | sck->kw[0].in_type = SMP_T_STR; |
| 5361 | sck->kw[0].out_type = SMP_T_STR; |
| 5362 | sck->kw[0].private = fcn; |
| 5363 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5364 | /* Register this new converter */ |
| 5365 | sample_register_convs(sck); |
| 5366 | |
| 5367 | return 0; |
| 5368 | } |
| 5369 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5370 | /* This fucntion is an LUA binding used for registering |
| 5371 | * "sample-fetch" functions. It expects a converter name used |
| 5372 | * in the haproxy configuration file, and an LUA function. |
| 5373 | */ |
| 5374 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 5375 | { |
| 5376 | const char *name; |
| 5377 | int ref; |
| 5378 | int len; |
| 5379 | struct sample_fetch_kw_list *sfk; |
| 5380 | struct hlua_function *fcn; |
| 5381 | |
| 5382 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 5383 | |
| 5384 | /* First argument : sample-fetch name. */ |
| 5385 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5386 | |
| 5387 | /* Second argument : lua function. */ |
| 5388 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 5389 | |
| 5390 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5391 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5392 | if (!sfk) |
| 5393 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5394 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5395 | if (!fcn) |
| 5396 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5397 | |
| 5398 | /* Fill fcn. */ |
| 5399 | fcn->name = strdup(name); |
| 5400 | if (!fcn->name) |
| 5401 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5402 | fcn->function_ref = ref; |
| 5403 | |
| 5404 | /* List head */ |
| 5405 | sfk->list.n = sfk->list.p = NULL; |
| 5406 | |
| 5407 | /* sample-fetch keyword. */ |
| 5408 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5409 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5410 | if (!sfk->kw[0].kw) |
| 5411 | return luaL_error(L, "lua out of memory error."); |
| 5412 | |
| 5413 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 5414 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
| 5415 | sfk->kw[0].arg_mask = ARG5(0,STR,STR,STR,STR,STR); |
| 5416 | sfk->kw[0].val_args = NULL; |
| 5417 | sfk->kw[0].out_type = SMP_T_STR; |
| 5418 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 5419 | sfk->kw[0].val = 0; |
| 5420 | sfk->kw[0].private = fcn; |
| 5421 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5422 | /* Register this new fetch. */ |
| 5423 | sample_register_fetches(sfk); |
| 5424 | |
| 5425 | return 0; |
| 5426 | } |
| 5427 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5428 | /* This function is a wrapper to execute each LUA function declared |
| 5429 | * as an action wrapper during the initialisation period. This function |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5430 | * return ACT_RET_CONT if the processing is finished (with or without |
| 5431 | * error) and return ACT_RET_YIELD if the function must be called again |
| 5432 | * because the LUA returns a yield. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5433 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5434 | static enum act_return hlua_action(struct act_rule *rule, struct proxy *px, |
Willy Tarreau | 658b85b | 2015-09-27 10:00:49 +0200 | [diff] [blame] | 5435 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5436 | { |
| 5437 | char **arg; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5438 | unsigned int analyzer; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 5439 | int dir; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5440 | |
| 5441 | switch (rule->from) { |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 5442 | case ACT_F_TCP_REQ_CNT: analyzer = AN_REQ_INSPECT_FE ; dir = 0; break; |
| 5443 | case ACT_F_TCP_RES_CNT: analyzer = AN_RES_INSPECT ; dir = 1; break; |
| 5444 | case ACT_F_HTTP_REQ: analyzer = AN_REQ_HTTP_PROCESS_FE; dir = 0; break; |
| 5445 | 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] | 5446 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5447 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5448 | return ACT_RET_CONT; |
| 5449 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5450 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5451 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5452 | * Lua context can be not initialized. This behavior |
| 5453 | * permits to save performances because a systematic |
| 5454 | * Lua initialization cause 5% performances loss. |
| 5455 | */ |
| 5456 | if (!s->hlua.T && !hlua_ctx_init(&s->hlua, s->task)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5457 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5458 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 5459 | return ACT_RET_CONT; |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5460 | } |
| 5461 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5462 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 5463 | if (!HLUA_IS_RUNNING(&s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5464 | |
| 5465 | /* The following Lua calls can fail. */ |
| 5466 | if (!SET_SAFE_LJMP(s->hlua.T)) { |
| 5467 | SEND_ERR(px, "Lua function '%s': critical error.\n", |
| 5468 | rule->arg.hlua_rule->fcn.name); |
| 5469 | return ACT_RET_CONT; |
| 5470 | } |
| 5471 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5472 | /* Check stack available size. */ |
| 5473 | if (!lua_checkstack(s->hlua.T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5474 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5475 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 10e5bc7 | 2015-09-25 23:51:34 +0200 | [diff] [blame] | 5476 | RESET_SAFE_LJMP(s->hlua.T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 5477 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5478 | } |
| 5479 | |
| 5480 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5481 | 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] | 5482 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5483 | /* Create and and push object stream in the stack. */ |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 5484 | if (!hlua_txn_new(s->hlua.T, s, px)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5485 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5486 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 10e5bc7 | 2015-09-25 23:51:34 +0200 | [diff] [blame] | 5487 | RESET_SAFE_LJMP(s->hlua.T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 5488 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5489 | } |
| 5490 | s->hlua.nargs = 1; |
| 5491 | |
| 5492 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5493 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5494 | if (!lua_checkstack(s->hlua.T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5495 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5496 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 10e5bc7 | 2015-09-25 23:51:34 +0200 | [diff] [blame] | 5497 | RESET_SAFE_LJMP(s->hlua.T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 5498 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5499 | } |
| 5500 | lua_pushstring(s->hlua.T, *arg); |
| 5501 | s->hlua.nargs++; |
| 5502 | } |
| 5503 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5504 | /* Now the execution is safe. */ |
| 5505 | RESET_SAFE_LJMP(s->hlua.T); |
| 5506 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5507 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 5508 | s->hlua.max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5509 | } |
| 5510 | |
| 5511 | /* Execute the function. */ |
Willy Tarreau | 528192d | 2015-09-27 10:48:01 +0200 | [diff] [blame] | 5512 | switch (hlua_ctx_resume(&s->hlua, !(flags & ACT_FLAG_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5513 | /* finished. */ |
| 5514 | case HLUA_E_OK: |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 5515 | if (!hlua_check_proto(s, dir)) |
| 5516 | return ACT_RET_ERR; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 5517 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5518 | |
| 5519 | /* yield. */ |
| 5520 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 5521 | /* Set timeout in the required channel. */ |
| 5522 | if (s->hlua.wake_time != TICK_ETERNITY) { |
| 5523 | if (analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5524 | s->req.analyse_exp = s->hlua.wake_time; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 5525 | else if (analyzer & (AN_RES_INSPECT|AN_RES_HTTP_PROCESS_BE)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5526 | s->res.analyse_exp = s->hlua.wake_time; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 5527 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5528 | /* Some actions can be wake up when a "write" event |
| 5529 | * is detected on a response channel. This is useful |
| 5530 | * only for actions targetted on the requests. |
| 5531 | */ |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 5532 | if (HLUA_IS_WAKERESWR(&s->hlua)) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5533 | s->res.flags |= CF_WAKE_WRITE; |
Willy Tarreau | 76bd97f | 2015-03-10 17:16:10 +0100 | [diff] [blame] | 5534 | if ((analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE))) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5535 | s->res.analysers |= analyzer; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5536 | } |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 5537 | if (HLUA_IS_WAKEREQWR(&s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5538 | s->req.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 5539 | return ACT_RET_YIELD; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5540 | |
| 5541 | /* finished with error. */ |
| 5542 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 5543 | if (!hlua_check_proto(s, dir)) |
| 5544 | return ACT_RET_ERR; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5545 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5546 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5547 | rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua.T, -1)); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5548 | lua_pop(s->hlua.T, 1); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 5549 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5550 | |
| 5551 | case HLUA_E_ERR: |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 5552 | if (!hlua_check_proto(s, dir)) |
| 5553 | return ACT_RET_ERR; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5554 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5555 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5556 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5557 | |
| 5558 | default: |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 5559 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5560 | } |
| 5561 | } |
| 5562 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 5563 | struct task *hlua_applet_wakeup(struct task *t) |
| 5564 | { |
| 5565 | struct appctx *ctx = t->context; |
| 5566 | struct stream_interface *si = ctx->owner; |
| 5567 | |
| 5568 | /* If the applet is wake up without any expected work, the sheduler |
| 5569 | * remove it from the run queue. This flag indicate that the applet |
| 5570 | * is waiting for write. If the buffer is full, the main processing |
| 5571 | * will send some data and after call the applet, otherwise it call |
| 5572 | * the applet ASAP. |
| 5573 | */ |
| 5574 | si_applet_cant_put(si); |
| 5575 | appctx_wakeup(ctx); |
| 5576 | return NULL; |
| 5577 | } |
| 5578 | |
| 5579 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 5580 | { |
| 5581 | struct stream_interface *si = ctx->owner; |
| 5582 | struct hlua *hlua = &ctx->ctx.hlua_apptcp.hlua; |
| 5583 | struct task *task; |
| 5584 | char **arg; |
| 5585 | |
| 5586 | HLUA_INIT(hlua); |
| 5587 | ctx->ctx.hlua_apptcp.flags = 0; |
| 5588 | |
| 5589 | /* Create task used by signal to wakeup applets. */ |
| 5590 | task = task_new(); |
| 5591 | if (!task) { |
| 5592 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 5593 | ctx->rule->arg.hlua_rule->fcn.name); |
| 5594 | return 0; |
| 5595 | } |
| 5596 | task->nice = 0; |
| 5597 | task->context = ctx; |
| 5598 | task->process = hlua_applet_wakeup; |
| 5599 | ctx->ctx.hlua_apptcp.task = task; |
| 5600 | |
| 5601 | /* In the execution wrappers linked with a stream, the |
| 5602 | * Lua context can be not initialized. This behavior |
| 5603 | * permits to save performances because a systematic |
| 5604 | * Lua initialization cause 5% performances loss. |
| 5605 | */ |
| 5606 | if (!hlua_ctx_init(hlua, task)) { |
| 5607 | SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n", |
| 5608 | ctx->rule->arg.hlua_rule->fcn.name); |
| 5609 | return 0; |
| 5610 | } |
| 5611 | |
| 5612 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 5613 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 5614 | |
| 5615 | /* The following Lua calls can fail. */ |
| 5616 | if (!SET_SAFE_LJMP(hlua->T)) { |
| 5617 | SEND_ERR(px, "Lua applet tcp '%s': critical error.\n", |
| 5618 | ctx->rule->arg.hlua_rule->fcn.name); |
| 5619 | RESET_SAFE_LJMP(hlua->T); |
| 5620 | return 0; |
| 5621 | } |
| 5622 | |
| 5623 | /* Check stack available size. */ |
| 5624 | if (!lua_checkstack(hlua->T, 1)) { |
| 5625 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 5626 | ctx->rule->arg.hlua_rule->fcn.name); |
| 5627 | RESET_SAFE_LJMP(hlua->T); |
| 5628 | return 0; |
| 5629 | } |
| 5630 | |
| 5631 | /* Restore the function in the stack. */ |
| 5632 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 5633 | |
| 5634 | /* Create and and push object stream in the stack. */ |
| 5635 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 5636 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 5637 | ctx->rule->arg.hlua_rule->fcn.name); |
| 5638 | RESET_SAFE_LJMP(hlua->T); |
| 5639 | return 0; |
| 5640 | } |
| 5641 | hlua->nargs = 1; |
| 5642 | |
| 5643 | /* push keywords in the stack. */ |
| 5644 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 5645 | if (!lua_checkstack(hlua->T, 1)) { |
| 5646 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 5647 | ctx->rule->arg.hlua_rule->fcn.name); |
| 5648 | RESET_SAFE_LJMP(hlua->T); |
| 5649 | return 0; |
| 5650 | } |
| 5651 | lua_pushstring(hlua->T, *arg); |
| 5652 | hlua->nargs++; |
| 5653 | } |
| 5654 | |
| 5655 | RESET_SAFE_LJMP(hlua->T); |
| 5656 | |
| 5657 | /* Wakeup the applet ASAP. */ |
| 5658 | si_applet_cant_get(si); |
| 5659 | si_applet_cant_put(si); |
| 5660 | |
| 5661 | return 1; |
| 5662 | } |
| 5663 | |
| 5664 | static void hlua_applet_tcp_fct(struct appctx *ctx) |
| 5665 | { |
| 5666 | struct stream_interface *si = ctx->owner; |
| 5667 | struct stream *strm = si_strm(si); |
| 5668 | struct channel *res = si_ic(si); |
| 5669 | struct act_rule *rule = ctx->rule; |
| 5670 | struct proxy *px = strm->be; |
| 5671 | struct hlua *hlua = &ctx->ctx.hlua_apptcp.hlua; |
| 5672 | |
| 5673 | /* The applet execution is already done. */ |
| 5674 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) |
| 5675 | return; |
| 5676 | |
| 5677 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 5678 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 5679 | return; |
| 5680 | |
| 5681 | /* Execute the function. */ |
| 5682 | switch (hlua_ctx_resume(hlua, 1)) { |
| 5683 | /* finished. */ |
| 5684 | case HLUA_E_OK: |
| 5685 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 5686 | |
| 5687 | /* log time */ |
| 5688 | strm->logs.tv_request = now; |
| 5689 | |
| 5690 | /* eat the whole request */ |
| 5691 | bo_skip(si_oc(si), si_ob(si)->o); |
| 5692 | res->flags |= CF_READ_NULL; |
| 5693 | si_shutr(si); |
| 5694 | return; |
| 5695 | |
| 5696 | /* yield. */ |
| 5697 | case HLUA_E_AGAIN: |
| 5698 | return; |
| 5699 | |
| 5700 | /* finished with error. */ |
| 5701 | case HLUA_E_ERRMSG: |
| 5702 | /* Display log. */ |
| 5703 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 5704 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 5705 | lua_pop(hlua->T, 1); |
| 5706 | goto error; |
| 5707 | |
| 5708 | case HLUA_E_ERR: |
| 5709 | /* Display log. */ |
| 5710 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
| 5711 | rule->arg.hlua_rule->fcn.name); |
| 5712 | goto error; |
| 5713 | |
| 5714 | default: |
| 5715 | goto error; |
| 5716 | } |
| 5717 | |
| 5718 | error: |
| 5719 | |
| 5720 | /* For all other cases, just close the stream. */ |
| 5721 | si_shutw(si); |
| 5722 | si_shutr(si); |
| 5723 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 5724 | } |
| 5725 | |
| 5726 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 5727 | { |
| 5728 | task_free(ctx->ctx.hlua_apptcp.task); |
| 5729 | ctx->ctx.hlua_apptcp.task = NULL; |
| 5730 | hlua_ctx_destroy(&ctx->ctx.hlua_apptcp.hlua); |
| 5731 | } |
| 5732 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5733 | /* The function returns 1 if the initialisation is complete, 0 if |
| 5734 | * an errors occurs and -1 if more data are required for initializing |
| 5735 | * the applet. |
| 5736 | */ |
| 5737 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 5738 | { |
| 5739 | struct stream_interface *si = ctx->owner; |
| 5740 | struct channel *req = si_oc(si); |
| 5741 | struct http_msg *msg; |
| 5742 | struct http_txn *txn; |
| 5743 | struct hlua *hlua = &ctx->ctx.hlua_apphttp.hlua; |
| 5744 | char **arg; |
| 5745 | struct hdr_ctx hdr; |
| 5746 | struct task *task; |
| 5747 | struct sample smp; /* just used for a valid call to smp_prefetch_http. */ |
| 5748 | |
| 5749 | /* Wait for a full HTTP request. */ |
| 5750 | if (!smp_prefetch_http(px, strm, 0, NULL, &smp, 0)) { |
| 5751 | if (smp.flags & SMP_F_MAY_CHANGE) |
| 5752 | return -1; |
| 5753 | return 0; |
| 5754 | } |
| 5755 | txn = strm->txn; |
| 5756 | msg = &txn->req; |
| 5757 | |
Willy Tarreau | 0078bfc | 2015-10-07 20:20:28 +0200 | [diff] [blame] | 5758 | /* We want two things in HTTP mode : |
| 5759 | * - enforce server-close mode if we were in keep-alive, so that the |
| 5760 | * applet is released after each response ; |
| 5761 | * - enable request body transfer to the applet in order to resync |
| 5762 | * with the response body. |
| 5763 | */ |
| 5764 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) |
| 5765 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
| 5766 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
| 5767 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5768 | HLUA_INIT(hlua); |
| 5769 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 5770 | ctx->ctx.hlua_apphttp.flags = 0; |
| 5771 | |
| 5772 | /* Create task used by signal to wakeup applets. */ |
| 5773 | task = task_new(); |
| 5774 | if (!task) { |
| 5775 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 5776 | ctx->rule->arg.hlua_rule->fcn.name); |
| 5777 | return 0; |
| 5778 | } |
| 5779 | task->nice = 0; |
| 5780 | task->context = ctx; |
| 5781 | task->process = hlua_applet_wakeup; |
| 5782 | ctx->ctx.hlua_apphttp.task = task; |
| 5783 | |
| 5784 | /* In the execution wrappers linked with a stream, the |
| 5785 | * Lua context can be not initialized. This behavior |
| 5786 | * permits to save performances because a systematic |
| 5787 | * Lua initialization cause 5% performances loss. |
| 5788 | */ |
| 5789 | if (!hlua_ctx_init(hlua, task)) { |
| 5790 | SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n", |
| 5791 | ctx->rule->arg.hlua_rule->fcn.name); |
| 5792 | return 0; |
| 5793 | } |
| 5794 | |
| 5795 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 5796 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5797 | |
| 5798 | /* The following Lua calls can fail. */ |
| 5799 | if (!SET_SAFE_LJMP(hlua->T)) { |
| 5800 | SEND_ERR(px, "Lua applet http '%s': critical error.\n", |
| 5801 | ctx->rule->arg.hlua_rule->fcn.name); |
| 5802 | return 0; |
| 5803 | } |
| 5804 | |
| 5805 | /* Check stack available size. */ |
| 5806 | if (!lua_checkstack(hlua->T, 1)) { |
| 5807 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 5808 | ctx->rule->arg.hlua_rule->fcn.name); |
| 5809 | RESET_SAFE_LJMP(hlua->T); |
| 5810 | return 0; |
| 5811 | } |
| 5812 | |
| 5813 | /* Restore the function in the stack. */ |
| 5814 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 5815 | |
| 5816 | /* Create and and push object stream in the stack. */ |
| 5817 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 5818 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 5819 | ctx->rule->arg.hlua_rule->fcn.name); |
| 5820 | RESET_SAFE_LJMP(hlua->T); |
| 5821 | return 0; |
| 5822 | } |
| 5823 | hlua->nargs = 1; |
| 5824 | |
| 5825 | /* Look for a 100-continue expected. */ |
| 5826 | if (msg->flags & HTTP_MSGF_VER_11) { |
| 5827 | hdr.idx = 0; |
| 5828 | if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &hdr) && |
| 5829 | unlikely(hdr.vlen == 12 && strncasecmp(hdr.line+hdr.val, "100-continue", 12) == 0)) |
| 5830 | ctx->ctx.hlua_apphttp.flags |= APPLET_100C; |
| 5831 | } |
| 5832 | |
| 5833 | /* push keywords in the stack. */ |
| 5834 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 5835 | if (!lua_checkstack(hlua->T, 1)) { |
| 5836 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 5837 | ctx->rule->arg.hlua_rule->fcn.name); |
| 5838 | RESET_SAFE_LJMP(hlua->T); |
| 5839 | return 0; |
| 5840 | } |
| 5841 | lua_pushstring(hlua->T, *arg); |
| 5842 | hlua->nargs++; |
| 5843 | } |
| 5844 | |
| 5845 | RESET_SAFE_LJMP(hlua->T); |
| 5846 | |
| 5847 | /* Wakeup the applet when data is ready for read. */ |
| 5848 | si_applet_cant_get(si); |
| 5849 | |
| 5850 | return 1; |
| 5851 | } |
| 5852 | |
| 5853 | static void hlua_applet_http_fct(struct appctx *ctx) |
| 5854 | { |
| 5855 | struct stream_interface *si = ctx->owner; |
| 5856 | struct stream *strm = si_strm(si); |
| 5857 | struct channel *res = si_ic(si); |
| 5858 | struct channel *req = si_oc(si); |
| 5859 | struct act_rule *rule = ctx->rule; |
| 5860 | struct proxy *px = strm->be; |
| 5861 | struct hlua *hlua = &ctx->ctx.hlua_apphttp.hlua; |
| 5862 | char *blk1; |
| 5863 | int len1; |
| 5864 | char *blk2; |
| 5865 | int len2; |
| 5866 | int ret; |
| 5867 | |
| 5868 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 5869 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 5870 | return; |
| 5871 | |
| 5872 | /* Set the currently running flag. */ |
| 5873 | if (!HLUA_IS_RUNNING(hlua) && |
| 5874 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 5875 | |
| 5876 | /* enable the minimally required analyzers to handle keep-alive |
| 5877 | * and compression on the HTTP response |
| 5878 | */ |
| 5879 | req->analysers = (req->analysers & AN_REQ_HTTP_BODY) | |
| 5880 | AN_REQ_HTTP_XFER_BODY | AN_REQ_HTTP_INNER; |
| 5881 | |
| 5882 | /* Wait for full HTTP analysys. */ |
| 5883 | if (unlikely(strm->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 5884 | si_applet_cant_get(si); |
| 5885 | return; |
| 5886 | } |
| 5887 | |
| 5888 | /* Store the max amount of bytes that we can read. */ |
| 5889 | ctx->ctx.hlua_apphttp.left_bytes = strm->txn->req.body_len; |
| 5890 | |
| 5891 | /* We need to flush the request header. This left the body |
| 5892 | * for the Lua. |
| 5893 | */ |
| 5894 | |
| 5895 | /* Read the maximum amount of data avalaible. */ |
| 5896 | ret = bo_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
| 5897 | if (ret == -1) |
| 5898 | return; |
| 5899 | |
| 5900 | /* No data available, ask for more data. */ |
| 5901 | if (ret == 1) |
| 5902 | len2 = 0; |
| 5903 | if (ret == 0) |
| 5904 | len1 = 0; |
| 5905 | if (len1 + len2 < strm->txn->req.eoh + 2) { |
| 5906 | si_applet_cant_get(si); |
| 5907 | return; |
| 5908 | } |
| 5909 | |
| 5910 | /* skip the requests bytes. */ |
| 5911 | bo_skip(si_oc(si), strm->txn->req.eoh + 2); |
| 5912 | } |
| 5913 | |
| 5914 | /* Executes The applet if it is not done. */ |
| 5915 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 5916 | |
| 5917 | /* Execute the function. */ |
| 5918 | switch (hlua_ctx_resume(hlua, 1)) { |
| 5919 | /* finished. */ |
| 5920 | case HLUA_E_OK: |
| 5921 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 5922 | break; |
| 5923 | |
| 5924 | /* yield. */ |
| 5925 | case HLUA_E_AGAIN: |
| 5926 | return; |
| 5927 | |
| 5928 | /* finished with error. */ |
| 5929 | case HLUA_E_ERRMSG: |
| 5930 | /* Display log. */ |
| 5931 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 5932 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 5933 | lua_pop(hlua->T, 1); |
| 5934 | goto error; |
| 5935 | |
| 5936 | case HLUA_E_ERR: |
| 5937 | /* Display log. */ |
| 5938 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
| 5939 | rule->arg.hlua_rule->fcn.name); |
| 5940 | goto error; |
| 5941 | |
| 5942 | default: |
| 5943 | goto error; |
| 5944 | } |
| 5945 | } |
| 5946 | |
| 5947 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
| 5948 | |
| 5949 | /* We must send the final chunk. */ |
| 5950 | if (ctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED && |
| 5951 | !(ctx->ctx.hlua_apphttp.flags & APPLET_LAST_CHK)) { |
| 5952 | |
| 5953 | /* sent last chunk at once. */ |
| 5954 | ret = bi_putblk(res, "0\r\n\r\n", 5); |
| 5955 | |
| 5956 | /* critical error. */ |
| 5957 | if (ret == -2 || ret == -3) { |
| 5958 | SEND_ERR(px, "Lua applet http '%s'cannont send last chunk.\n", |
| 5959 | rule->arg.hlua_rule->fcn.name); |
| 5960 | goto error; |
| 5961 | } |
| 5962 | |
| 5963 | /* no enough space error. */ |
| 5964 | if (ret == -1) { |
| 5965 | si_applet_cant_put(si); |
| 5966 | return; |
| 5967 | } |
| 5968 | |
| 5969 | /* set the last chunk sent. */ |
| 5970 | ctx->ctx.hlua_apphttp.flags |= APPLET_LAST_CHK; |
| 5971 | } |
| 5972 | |
| 5973 | /* close the connection. */ |
| 5974 | |
| 5975 | /* status / log */ |
| 5976 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 5977 | strm->logs.tv_request = now; |
| 5978 | |
| 5979 | /* eat the whole request */ |
| 5980 | bo_skip(si_oc(si), si_ob(si)->o); |
| 5981 | res->flags |= CF_READ_NULL; |
| 5982 | si_shutr(si); |
| 5983 | |
| 5984 | return; |
| 5985 | } |
| 5986 | |
| 5987 | error: |
| 5988 | |
| 5989 | /* If we are in HTTP mode, and we are not send any |
| 5990 | * data, return a 500 server error in best effort: |
| 5991 | * if there are no room avalaible in the buffer, |
| 5992 | * just close the connection. |
| 5993 | */ |
| 5994 | bi_putblk(res, error_500, strlen(error_500)); |
| 5995 | if (!(strm->flags & SF_ERR_MASK)) |
| 5996 | strm->flags |= SF_ERR_RESOURCE; |
| 5997 | si_shutw(si); |
| 5998 | si_shutr(si); |
| 5999 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 6000 | } |
| 6001 | |
| 6002 | static void hlua_applet_http_release(struct appctx *ctx) |
| 6003 | { |
| 6004 | task_free(ctx->ctx.hlua_apphttp.task); |
| 6005 | ctx->ctx.hlua_apphttp.task = NULL; |
| 6006 | hlua_ctx_destroy(&ctx->ctx.hlua_apphttp.hlua); |
| 6007 | } |
| 6008 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6009 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
| 6010 | * succes case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6011 | * |
| 6012 | * This function can fail with an abort() due to an Lua critical error. |
| 6013 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 6014 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6015 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6016 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 6017 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6018 | { |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6019 | struct hlua_function *fcn = (struct hlua_function *)rule->kw->private; |
| 6020 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6021 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6022 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6023 | if (!rule->arg.hlua_rule) { |
| 6024 | memprintf(err, "out of memory error"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 6025 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6026 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6027 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6028 | /* Reference the Lua function and store the reference. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6029 | rule->arg.hlua_rule->fcn = *fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6030 | |
| 6031 | /* TODO: later accept arguments. */ |
| 6032 | rule->arg.hlua_rule->args = NULL; |
| 6033 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 6034 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6035 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 6036 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6037 | } |
| 6038 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6039 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 6040 | struct act_rule *rule, char **err) |
| 6041 | { |
| 6042 | struct hlua_function *fcn = (struct hlua_function *)rule->kw->private; |
| 6043 | |
| 6044 | /* Memory for the rule. */ |
| 6045 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 6046 | if (!rule->arg.hlua_rule) { |
| 6047 | memprintf(err, "out of memory error"); |
| 6048 | return ACT_RET_PRS_ERR; |
| 6049 | } |
| 6050 | |
| 6051 | /* Reference the Lua function and store the reference. */ |
| 6052 | rule->arg.hlua_rule->fcn = *fcn; |
| 6053 | |
| 6054 | /* TODO: later accept arguments. */ |
| 6055 | rule->arg.hlua_rule->args = NULL; |
| 6056 | |
| 6057 | /* Add applet pointer in the rule. */ |
| 6058 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 6059 | rule->applet.name = fcn->name; |
| 6060 | rule->applet.init = hlua_applet_http_init; |
| 6061 | rule->applet.fct = hlua_applet_http_fct; |
| 6062 | rule->applet.release = hlua_applet_http_release; |
| 6063 | rule->applet.timeout = hlua_timeout_applet; |
| 6064 | |
| 6065 | return ACT_RET_PRS_OK; |
| 6066 | } |
| 6067 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6068 | /* This function is an LUA binding used for registering |
| 6069 | * "sample-conv" functions. It expects a converter name used |
| 6070 | * in the haproxy configuration file, and an LUA function. |
| 6071 | */ |
| 6072 | __LJMP static int hlua_register_action(lua_State *L) |
| 6073 | { |
| 6074 | struct action_kw_list *akl; |
| 6075 | const char *name; |
| 6076 | int ref; |
| 6077 | int len; |
| 6078 | struct hlua_function *fcn; |
| 6079 | |
| 6080 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 6081 | |
| 6082 | /* First argument : converter name. */ |
| 6083 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6084 | |
| 6085 | /* Second argument : environment. */ |
| 6086 | if (lua_type(L, 2) != LUA_TTABLE) |
| 6087 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 6088 | |
| 6089 | /* Third argument : lua function. */ |
| 6090 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 6091 | |
| 6092 | /* browse the second argulent as an array. */ |
| 6093 | lua_pushnil(L); |
| 6094 | while (lua_next(L, 2) != 0) { |
| 6095 | if (lua_type(L, -1) != LUA_TSTRING) |
| 6096 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 6097 | |
| 6098 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 6099 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6100 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6101 | if (!akl) |
| 6102 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6103 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6104 | if (!fcn) |
| 6105 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6106 | |
| 6107 | /* Fill fcn. */ |
| 6108 | fcn->name = strdup(name); |
| 6109 | if (!fcn->name) |
| 6110 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6111 | fcn->function_ref = ref; |
| 6112 | |
| 6113 | /* List head */ |
| 6114 | akl->list.n = akl->list.p = NULL; |
| 6115 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6116 | /* action keyword. */ |
| 6117 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6118 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6119 | if (!akl->kw[0].kw) |
| 6120 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6121 | |
| 6122 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 6123 | |
| 6124 | akl->kw[0].match_pfx = 0; |
| 6125 | akl->kw[0].private = fcn; |
| 6126 | akl->kw[0].parse = action_register_lua; |
| 6127 | |
| 6128 | /* select the action registering point. */ |
| 6129 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 6130 | tcp_req_cont_keywords_register(akl); |
| 6131 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 6132 | tcp_res_cont_keywords_register(akl); |
| 6133 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 6134 | http_req_keywords_register(akl); |
| 6135 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 6136 | http_res_keywords_register(akl); |
| 6137 | else |
| 6138 | WILL_LJMP(luaL_error(L, "lua action environment '%s' is unknown. " |
| 6139 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 6140 | "are expected.", lua_tostring(L, -1))); |
| 6141 | |
| 6142 | /* pop the environment string. */ |
| 6143 | lua_pop(L, 1); |
| 6144 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6145 | return ACT_RET_PRS_OK; |
| 6146 | } |
| 6147 | |
| 6148 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 6149 | struct act_rule *rule, char **err) |
| 6150 | { |
| 6151 | struct hlua_function *fcn = (struct hlua_function *)rule->kw->private; |
| 6152 | |
| 6153 | /* Memory for the rule. */ |
| 6154 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 6155 | if (!rule->arg.hlua_rule) { |
| 6156 | memprintf(err, "out of memory error"); |
| 6157 | return ACT_RET_PRS_ERR; |
| 6158 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6159 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6160 | /* Reference the Lua function and store the reference. */ |
| 6161 | rule->arg.hlua_rule->fcn = *fcn; |
| 6162 | |
| 6163 | /* TODO: later accept arguments. */ |
| 6164 | rule->arg.hlua_rule->args = NULL; |
| 6165 | |
| 6166 | /* Add applet pointer in the rule. */ |
| 6167 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 6168 | rule->applet.name = fcn->name; |
| 6169 | rule->applet.init = hlua_applet_tcp_init; |
| 6170 | rule->applet.fct = hlua_applet_tcp_fct; |
| 6171 | rule->applet.release = hlua_applet_tcp_release; |
| 6172 | rule->applet.timeout = hlua_timeout_applet; |
| 6173 | |
| 6174 | return 0; |
| 6175 | } |
| 6176 | |
| 6177 | /* This function is an LUA binding used for registering |
| 6178 | * "sample-conv" functions. It expects a converter name used |
| 6179 | * in the haproxy configuration file, and an LUA function. |
| 6180 | */ |
| 6181 | __LJMP static int hlua_register_service(lua_State *L) |
| 6182 | { |
| 6183 | struct action_kw_list *akl; |
| 6184 | const char *name; |
| 6185 | const char *env; |
| 6186 | int ref; |
| 6187 | int len; |
| 6188 | struct hlua_function *fcn; |
| 6189 | |
| 6190 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 6191 | |
| 6192 | /* First argument : converter name. */ |
| 6193 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6194 | |
| 6195 | /* Second argument : environment. */ |
| 6196 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6197 | |
| 6198 | /* Third argument : lua function. */ |
| 6199 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 6200 | |
| 6201 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 6202 | /* Allocate and fill the sample fetch keyword struct. */ |
| 6203 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 6204 | if (!akl) |
| 6205 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6206 | fcn = calloc(1, sizeof(*fcn)); |
| 6207 | if (!fcn) |
| 6208 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6209 | |
| 6210 | /* Fill fcn. */ |
| 6211 | len = strlen("<lua.>") + strlen(name) + 1; |
| 6212 | fcn->name = calloc(1, len); |
| 6213 | if (!fcn->name) |
| 6214 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6215 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
| 6216 | fcn->function_ref = ref; |
| 6217 | |
| 6218 | /* List head */ |
| 6219 | akl->list.n = akl->list.p = NULL; |
| 6220 | |
| 6221 | /* converter keyword. */ |
| 6222 | len = strlen("lua.") + strlen(name) + 1; |
| 6223 | akl->kw[0].kw = calloc(1, len); |
| 6224 | if (!akl->kw[0].kw) |
| 6225 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6226 | |
| 6227 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 6228 | |
| 6229 | if (strcmp(env, "tcp") == 0) |
| 6230 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6231 | else if (strcmp(env, "http") == 0) |
| 6232 | akl->kw[0].parse = action_register_service_http; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6233 | else |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6234 | WILL_LJMP(luaL_error(L, "lua service environment '%s' is unknown. " |
| 6235 | "'tcp' or 'http' are expected.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6236 | |
| 6237 | akl->kw[0].match_pfx = 0; |
| 6238 | akl->kw[0].private = fcn; |
| 6239 | |
| 6240 | /* End of array. */ |
| 6241 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 6242 | |
| 6243 | /* Register this new converter */ |
| 6244 | service_keywords_register(akl); |
| 6245 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6246 | return 0; |
| 6247 | } |
| 6248 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6249 | static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx, |
| 6250 | struct proxy *defpx, const char *file, int line, |
| 6251 | char **err, unsigned int *timeout) |
| 6252 | { |
| 6253 | const char *error; |
| 6254 | |
| 6255 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
| 6256 | if (error && *error != '\0') { |
| 6257 | memprintf(err, "%s: invalid timeout", args[0]); |
| 6258 | return -1; |
| 6259 | } |
| 6260 | return 0; |
| 6261 | } |
| 6262 | |
| 6263 | static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx, |
| 6264 | struct proxy *defpx, const char *file, int line, |
| 6265 | char **err) |
| 6266 | { |
| 6267 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 6268 | file, line, err, &hlua_timeout_session); |
| 6269 | } |
| 6270 | |
| 6271 | static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx, |
| 6272 | struct proxy *defpx, const char *file, int line, |
| 6273 | char **err) |
| 6274 | { |
| 6275 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 6276 | file, line, err, &hlua_timeout_task); |
| 6277 | } |
| 6278 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6279 | static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx, |
| 6280 | struct proxy *defpx, const char *file, int line, |
| 6281 | char **err) |
| 6282 | { |
| 6283 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 6284 | file, line, err, &hlua_timeout_applet); |
| 6285 | } |
| 6286 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 6287 | static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx, |
| 6288 | struct proxy *defpx, const char *file, int line, |
| 6289 | char **err) |
| 6290 | { |
| 6291 | char *error; |
| 6292 | |
| 6293 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 6294 | if (*error != '\0') { |
| 6295 | memprintf(err, "%s: invalid number", args[0]); |
| 6296 | return -1; |
| 6297 | } |
| 6298 | return 0; |
| 6299 | } |
| 6300 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 6301 | static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx, |
| 6302 | struct proxy *defpx, const char *file, int line, |
| 6303 | char **err) |
| 6304 | { |
| 6305 | char *error; |
| 6306 | |
| 6307 | if (*(args[1]) == 0) { |
| 6308 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 6309 | return -1; |
| 6310 | } |
| 6311 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 6312 | if (*error != '\0') { |
| 6313 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 6314 | return -1; |
| 6315 | } |
| 6316 | return 0; |
| 6317 | } |
| 6318 | |
| 6319 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 6320 | /* This function is called by the main configuration key "lua-load". It loads and |
| 6321 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 6322 | * the main lua entry point. |
| 6323 | * |
| 6324 | * This funtion runs with the HAProxy keywords API. It returns -1 if an error is |
| 6325 | * occured, otherwise it returns 0. |
| 6326 | * |
| 6327 | * In some error case, LUA set an error message in top of the stack. This function |
| 6328 | * 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] | 6329 | * |
| 6330 | * This function can fail with an abort() due to an Lua critical error. |
| 6331 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 6332 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 6333 | */ |
| 6334 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
| 6335 | struct proxy *defpx, const char *file, int line, |
| 6336 | char **err) |
| 6337 | { |
| 6338 | int error; |
| 6339 | |
| 6340 | /* Just load and compile the file. */ |
| 6341 | error = luaL_loadfile(gL.T, args[1]); |
| 6342 | if (error) { |
| 6343 | memprintf(err, "error in lua file '%s': %s", args[1], lua_tostring(gL.T, -1)); |
| 6344 | lua_pop(gL.T, 1); |
| 6345 | return -1; |
| 6346 | } |
| 6347 | |
| 6348 | /* If no syntax error where detected, execute the code. */ |
| 6349 | error = lua_pcall(gL.T, 0, LUA_MULTRET, 0); |
| 6350 | switch (error) { |
| 6351 | case LUA_OK: |
| 6352 | break; |
| 6353 | case LUA_ERRRUN: |
| 6354 | memprintf(err, "lua runtime error: %s\n", lua_tostring(gL.T, -1)); |
| 6355 | lua_pop(gL.T, 1); |
| 6356 | return -1; |
| 6357 | case LUA_ERRMEM: |
| 6358 | memprintf(err, "lua out of memory error\n"); |
| 6359 | return -1; |
| 6360 | case LUA_ERRERR: |
| 6361 | memprintf(err, "lua message handler error: %s\n", lua_tostring(gL.T, -1)); |
| 6362 | lua_pop(gL.T, 1); |
| 6363 | return -1; |
| 6364 | case LUA_ERRGCMM: |
| 6365 | memprintf(err, "lua garbage collector error: %s\n", lua_tostring(gL.T, -1)); |
| 6366 | lua_pop(gL.T, 1); |
| 6367 | return -1; |
| 6368 | default: |
| 6369 | memprintf(err, "lua unknonwn error: %s\n", lua_tostring(gL.T, -1)); |
| 6370 | lua_pop(gL.T, 1); |
| 6371 | return -1; |
| 6372 | } |
| 6373 | |
| 6374 | return 0; |
| 6375 | } |
| 6376 | |
| 6377 | /* configuration keywords declaration */ |
| 6378 | static struct cfg_kw_list cfg_kws = {{ },{ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6379 | { CFG_GLOBAL, "lua-load", hlua_load }, |
| 6380 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 6381 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 6382 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 6383 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 6384 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 6385 | { 0, NULL, NULL }, |
| 6386 | }}; |
| 6387 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6388 | /* This function can fail with an abort() due to an Lua critical error. |
| 6389 | * We are in the initialisation process of HAProxy, this abort() is |
| 6390 | * tolerated. |
| 6391 | */ |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6392 | int hlua_post_init() |
| 6393 | { |
| 6394 | struct hlua_init_function *init; |
| 6395 | const char *msg; |
| 6396 | enum hlua_exec ret; |
| 6397 | |
| 6398 | list_for_each_entry(init, &hlua_init_functions, l) { |
| 6399 | lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref); |
| 6400 | ret = hlua_ctx_resume(&gL, 0); |
| 6401 | switch (ret) { |
| 6402 | case HLUA_E_OK: |
| 6403 | lua_pop(gL.T, -1); |
| 6404 | return 1; |
| 6405 | case HLUA_E_AGAIN: |
| 6406 | Alert("lua init: yield not allowed.\n"); |
| 6407 | return 0; |
| 6408 | case HLUA_E_ERRMSG: |
| 6409 | msg = lua_tostring(gL.T, -1); |
| 6410 | Alert("lua init: %s.\n", msg); |
| 6411 | return 0; |
| 6412 | case HLUA_E_ERR: |
| 6413 | default: |
| 6414 | Alert("lua init: unknown runtime error.\n"); |
| 6415 | return 0; |
| 6416 | } |
| 6417 | } |
| 6418 | return 1; |
| 6419 | } |
| 6420 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 6421 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 6422 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 6423 | * is the previously allocated size or the kind of object in case of a new |
| 6424 | * allocation. <nsize> is the requested new size. |
| 6425 | */ |
| 6426 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 6427 | { |
| 6428 | struct hlua_mem_allocator *zone = ud; |
| 6429 | |
| 6430 | if (nsize == 0) { |
| 6431 | /* it's a free */ |
| 6432 | if (ptr) |
| 6433 | zone->allocated -= osize; |
| 6434 | free(ptr); |
| 6435 | return NULL; |
| 6436 | } |
| 6437 | |
| 6438 | if (!ptr) { |
| 6439 | /* it's a new allocation */ |
| 6440 | if (zone->limit && zone->allocated + nsize > zone->limit) |
| 6441 | return NULL; |
| 6442 | |
| 6443 | ptr = malloc(nsize); |
| 6444 | if (ptr) |
| 6445 | zone->allocated += nsize; |
| 6446 | return ptr; |
| 6447 | } |
| 6448 | |
| 6449 | /* it's a realloc */ |
| 6450 | if (zone->limit && zone->allocated + nsize - osize > zone->limit) |
| 6451 | return NULL; |
| 6452 | |
| 6453 | ptr = realloc(ptr, nsize); |
| 6454 | if (ptr) |
| 6455 | zone->allocated += nsize - osize; |
| 6456 | return ptr; |
| 6457 | } |
| 6458 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6459 | /* Ithis function can fail with an abort() due to an Lua critical error. |
| 6460 | * We are in the initialisation process of HAProxy, this abort() is |
| 6461 | * tolerated. |
| 6462 | */ |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 6463 | void hlua_init(void) |
| 6464 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 6465 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 6466 | int idx; |
| 6467 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 6468 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 6469 | char *p; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6470 | #ifdef USE_OPENSSL |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6471 | struct srv_kw *kw; |
| 6472 | int tmp_error; |
| 6473 | char *error; |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 6474 | char *args[] = { /* SSL client configuration. */ |
| 6475 | "ssl", |
| 6476 | "verify", |
| 6477 | "none", |
| 6478 | "force-sslv3", |
| 6479 | NULL |
| 6480 | }; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6481 | #endif |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 6482 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6483 | /* Initialise com signals pool */ |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 6484 | pool2_hlua_com = create_pool("hlua_com", sizeof(struct hlua_com), MEM_F_SHARED); |
| 6485 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 6486 | /* Register configuration keywords. */ |
| 6487 | cfg_register_keywords(&cfg_kws); |
| 6488 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 6489 | /* Init main lua stack. */ |
| 6490 | gL.Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 6491 | gL.flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 6492 | LIST_INIT(&gL.com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 6493 | gL.T = luaL_newstate(); |
| 6494 | hlua_sethlua(&gL); |
| 6495 | gL.Tref = LUA_REFNIL; |
| 6496 | gL.task = NULL; |
| 6497 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6498 | /* From this point, until the end of the initialisation fucntion, |
| 6499 | * the Lua function can fail with an abort. We are in the initialisation |
| 6500 | * process of HAProxy, this abort() is tolerated. |
| 6501 | */ |
| 6502 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 6503 | /* change the memory allocators to track memory usage */ |
| 6504 | lua_setallocf(gL.T, hlua_alloc, &hlua_global_allocator); |
| 6505 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 6506 | /* Initialise lua. */ |
| 6507 | luaL_openlibs(gL.T); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 6508 | |
| 6509 | /* |
| 6510 | * |
| 6511 | * Create "core" object. |
| 6512 | * |
| 6513 | */ |
| 6514 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 6515 | /* This table entry is the object "core" base. */ |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 6516 | lua_newtable(gL.T); |
| 6517 | |
| 6518 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6519 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 6520 | hlua_class_const_int(gL.T, log_levels[i], i); |
| 6521 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6522 | /* Register special functions. */ |
| 6523 | hlua_class_function(gL.T, "register_init", hlua_register_init); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6524 | hlua_class_function(gL.T, "register_task", hlua_register_task); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6525 | hlua_class_function(gL.T, "register_fetches", hlua_register_fetches); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6526 | hlua_class_function(gL.T, "register_converters", hlua_register_converters); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6527 | hlua_class_function(gL.T, "register_action", hlua_register_action); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6528 | hlua_class_function(gL.T, "register_service", hlua_register_service); |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6529 | hlua_class_function(gL.T, "yield", hlua_yield); |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 6530 | hlua_class_function(gL.T, "set_nice", hlua_set_nice); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6531 | hlua_class_function(gL.T, "sleep", hlua_sleep); |
| 6532 | hlua_class_function(gL.T, "msleep", hlua_msleep); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 6533 | hlua_class_function(gL.T, "add_acl", hlua_add_acl); |
| 6534 | hlua_class_function(gL.T, "del_acl", hlua_del_acl); |
| 6535 | hlua_class_function(gL.T, "set_map", hlua_set_map); |
| 6536 | hlua_class_function(gL.T, "del_map", hlua_del_map); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 6537 | hlua_class_function(gL.T, "tcp", hlua_socket_new); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 6538 | hlua_class_function(gL.T, "log", hlua_log); |
| 6539 | hlua_class_function(gL.T, "Debug", hlua_log_debug); |
| 6540 | hlua_class_function(gL.T, "Info", hlua_log_info); |
| 6541 | hlua_class_function(gL.T, "Warning", hlua_log_warning); |
| 6542 | hlua_class_function(gL.T, "Alert", hlua_log_alert); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 6543 | hlua_class_function(gL.T, "done", hlua_done); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6544 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 6545 | lua_setglobal(gL.T, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 6546 | |
| 6547 | /* |
| 6548 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 6549 | * Register class Map |
| 6550 | * |
| 6551 | */ |
| 6552 | |
| 6553 | /* This table entry is the object "Map" base. */ |
| 6554 | lua_newtable(gL.T); |
| 6555 | |
| 6556 | /* register pattern types. */ |
| 6557 | for (i=0; i<PAT_MATCH_NUM; i++) |
| 6558 | hlua_class_const_int(gL.T, pat_match_names[i], i); |
| 6559 | |
| 6560 | /* register constructor. */ |
| 6561 | hlua_class_function(gL.T, "new", hlua_map_new); |
| 6562 | |
| 6563 | /* Create and fill the metatable. */ |
| 6564 | lua_newtable(gL.T); |
| 6565 | |
Thierry FOURNIER | d2a3dcc | 2015-09-18 07:35:06 +0200 | [diff] [blame] | 6566 | /* Create the __tostring identifier */ |
| 6567 | lua_pushstring(gL.T, "__tostring"); |
| 6568 | lua_pushstring(gL.T, CLASS_MAP); |
| 6569 | lua_pushcclosure(gL.T, hlua_dump_object, 1); |
| 6570 | lua_rawset(gL.T, -3); |
| 6571 | |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 6572 | /* Create and fille the __index entry. */ |
| 6573 | lua_pushstring(gL.T, "__index"); |
| 6574 | lua_newtable(gL.T); |
| 6575 | |
| 6576 | /* Register . */ |
| 6577 | hlua_class_function(gL.T, "lookup", hlua_map_lookup); |
| 6578 | hlua_class_function(gL.T, "slookup", hlua_map_slookup); |
| 6579 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 6580 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 6581 | |
| 6582 | /* Register previous table in the registry with reference and named entry. */ |
| 6583 | lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */ |
| 6584 | lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */ |
| 6585 | lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_MAP); /* register class session. */ |
| 6586 | class_map_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */ |
| 6587 | |
| 6588 | /* Assign the metatable to the mai Map object. */ |
| 6589 | lua_setmetatable(gL.T, -2); |
| 6590 | |
| 6591 | /* Set a name to the table. */ |
| 6592 | lua_setglobal(gL.T, "Map"); |
| 6593 | |
| 6594 | /* |
| 6595 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 6596 | * Register class Channel |
| 6597 | * |
| 6598 | */ |
| 6599 | |
| 6600 | /* Create and fill the metatable. */ |
| 6601 | lua_newtable(gL.T); |
| 6602 | |
Thierry FOURNIER | d2a3dcc | 2015-09-18 07:35:06 +0200 | [diff] [blame] | 6603 | /* Create the __tostring identifier */ |
| 6604 | lua_pushstring(gL.T, "__tostring"); |
| 6605 | lua_pushstring(gL.T, CLASS_CHANNEL); |
| 6606 | lua_pushcclosure(gL.T, hlua_dump_object, 1); |
| 6607 | lua_rawset(gL.T, -3); |
| 6608 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 6609 | /* Create and fille the __index entry. */ |
| 6610 | lua_pushstring(gL.T, "__index"); |
| 6611 | lua_newtable(gL.T); |
| 6612 | |
| 6613 | /* Register . */ |
| 6614 | hlua_class_function(gL.T, "get", hlua_channel_get); |
| 6615 | hlua_class_function(gL.T, "dup", hlua_channel_dup); |
| 6616 | hlua_class_function(gL.T, "getline", hlua_channel_getline); |
| 6617 | hlua_class_function(gL.T, "set", hlua_channel_set); |
| 6618 | hlua_class_function(gL.T, "append", hlua_channel_append); |
| 6619 | hlua_class_function(gL.T, "send", hlua_channel_send); |
| 6620 | hlua_class_function(gL.T, "forward", hlua_channel_forward); |
| 6621 | hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len); |
| 6622 | hlua_class_function(gL.T, "get_out_len", hlua_channel_get_out_len); |
| 6623 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 6624 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 6625 | |
| 6626 | /* Register previous table in the registry with reference and named entry. */ |
| 6627 | lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */ |
| 6628 | lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_CHANNEL); /* register class session. */ |
| 6629 | class_channel_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */ |
| 6630 | |
| 6631 | /* |
| 6632 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 6633 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 6634 | * |
| 6635 | */ |
| 6636 | |
| 6637 | /* Create and fill the metatable. */ |
| 6638 | lua_newtable(gL.T); |
| 6639 | |
Thierry FOURNIER | d2a3dcc | 2015-09-18 07:35:06 +0200 | [diff] [blame] | 6640 | /* Create the __tostring identifier */ |
| 6641 | lua_pushstring(gL.T, "__tostring"); |
| 6642 | lua_pushstring(gL.T, CLASS_FETCHES); |
| 6643 | lua_pushcclosure(gL.T, hlua_dump_object, 1); |
| 6644 | lua_rawset(gL.T, -3); |
| 6645 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 6646 | /* Create and fille the __index entry. */ |
| 6647 | lua_pushstring(gL.T, "__index"); |
| 6648 | lua_newtable(gL.T); |
| 6649 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 6650 | /* Browse existing fetches and create the associated |
| 6651 | * object method. |
| 6652 | */ |
| 6653 | sf = NULL; |
| 6654 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
| 6655 | |
| 6656 | /* Dont register the keywork if the arguments check function are |
| 6657 | * not safe during the runtime. |
| 6658 | */ |
| 6659 | if ((sf->val_args != NULL) && |
| 6660 | (sf->val_args != val_payload_lv) && |
| 6661 | (sf->val_args != val_hdr)) |
| 6662 | continue; |
| 6663 | |
| 6664 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 6665 | * by an underscore. |
| 6666 | */ |
| 6667 | strncpy(trash.str, sf->kw, trash.size); |
| 6668 | trash.str[trash.size - 1] = '\0'; |
| 6669 | for (p = trash.str; *p; p++) |
| 6670 | if (*p == '.' || *p == '-' || *p == '+') |
| 6671 | *p = '_'; |
| 6672 | |
| 6673 | /* Register the function. */ |
| 6674 | lua_pushstring(gL.T, trash.str); |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 6675 | lua_pushlightuserdata(gL.T, sf); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 6676 | lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 6677 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 6678 | } |
| 6679 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 6680 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 6681 | |
| 6682 | /* Register previous table in the registry with reference and named entry. */ |
| 6683 | lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */ |
| 6684 | lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_FETCHES); /* register class session. */ |
| 6685 | class_fetches_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */ |
| 6686 | |
| 6687 | /* |
| 6688 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 6689 | * Register class Converters |
| 6690 | * |
| 6691 | */ |
| 6692 | |
| 6693 | /* Create and fill the metatable. */ |
| 6694 | lua_newtable(gL.T); |
| 6695 | |
Thierry FOURNIER | d2a3dcc | 2015-09-18 07:35:06 +0200 | [diff] [blame] | 6696 | /* Create the __tostring identifier */ |
| 6697 | lua_pushstring(gL.T, "__tostring"); |
| 6698 | lua_pushstring(gL.T, CLASS_CONVERTERS); |
| 6699 | lua_pushcclosure(gL.T, hlua_dump_object, 1); |
| 6700 | lua_rawset(gL.T, -3); |
| 6701 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 6702 | /* Create and fill the __index entry. */ |
| 6703 | lua_pushstring(gL.T, "__index"); |
| 6704 | lua_newtable(gL.T); |
| 6705 | |
| 6706 | /* Browse existing converters and create the associated |
| 6707 | * object method. |
| 6708 | */ |
| 6709 | sc = NULL; |
| 6710 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
| 6711 | /* Dont register the keywork if the arguments check function are |
| 6712 | * not safe during the runtime. |
| 6713 | */ |
| 6714 | if (sc->val_args != NULL) |
| 6715 | continue; |
| 6716 | |
| 6717 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 6718 | * by an underscore. |
| 6719 | */ |
| 6720 | strncpy(trash.str, sc->kw, trash.size); |
| 6721 | trash.str[trash.size - 1] = '\0'; |
| 6722 | for (p = trash.str; *p; p++) |
| 6723 | if (*p == '.' || *p == '-' || *p == '+') |
| 6724 | *p = '_'; |
| 6725 | |
| 6726 | /* Register the function. */ |
| 6727 | lua_pushstring(gL.T, trash.str); |
| 6728 | lua_pushlightuserdata(gL.T, sc); |
| 6729 | lua_pushcclosure(gL.T, hlua_run_sample_conv, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 6730 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 6731 | } |
| 6732 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 6733 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 6734 | |
| 6735 | /* Register previous table in the registry with reference and named entry. */ |
| 6736 | lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */ |
| 6737 | lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_CONVERTERS); /* register class session. */ |
| 6738 | class_converters_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */ |
| 6739 | |
| 6740 | /* |
| 6741 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6742 | * Register class HTTP |
| 6743 | * |
| 6744 | */ |
| 6745 | |
| 6746 | /* Create and fill the metatable. */ |
| 6747 | lua_newtable(gL.T); |
| 6748 | |
Thierry FOURNIER | d2a3dcc | 2015-09-18 07:35:06 +0200 | [diff] [blame] | 6749 | /* Create the __tostring identifier */ |
| 6750 | lua_pushstring(gL.T, "__tostring"); |
| 6751 | lua_pushstring(gL.T, CLASS_HTTP); |
| 6752 | lua_pushcclosure(gL.T, hlua_dump_object, 1); |
| 6753 | lua_rawset(gL.T, -3); |
| 6754 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6755 | /* Create and fille the __index entry. */ |
| 6756 | lua_pushstring(gL.T, "__index"); |
| 6757 | lua_newtable(gL.T); |
| 6758 | |
| 6759 | /* Register Lua functions. */ |
| 6760 | hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers); |
| 6761 | hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr); |
| 6762 | hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr); |
| 6763 | hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val); |
| 6764 | hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr); |
| 6765 | hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr); |
| 6766 | hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth); |
| 6767 | hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path); |
| 6768 | hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query); |
| 6769 | hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri); |
| 6770 | |
| 6771 | hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers); |
| 6772 | hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr); |
| 6773 | hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr); |
| 6774 | hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val); |
| 6775 | hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr); |
| 6776 | 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] | 6777 | 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] | 6778 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 6779 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6780 | |
| 6781 | /* Register previous table in the registry with reference and named entry. */ |
| 6782 | lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */ |
| 6783 | lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_HTTP); /* register class session. */ |
| 6784 | class_http_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */ |
| 6785 | |
| 6786 | /* |
| 6787 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6788 | * Register class AppletTCP |
| 6789 | * |
| 6790 | */ |
| 6791 | |
| 6792 | /* Create and fill the metatable. */ |
| 6793 | lua_newtable(gL.T); |
| 6794 | |
| 6795 | /* Create the __tostring identifier */ |
| 6796 | lua_pushstring(gL.T, "__tostring"); |
| 6797 | lua_pushstring(gL.T, CLASS_APPLET_TCP); |
| 6798 | lua_pushcclosure(gL.T, hlua_dump_object, 1); |
| 6799 | lua_rawset(gL.T, -3); |
| 6800 | |
| 6801 | /* Create and fille the __index entry. */ |
| 6802 | lua_pushstring(gL.T, "__index"); |
| 6803 | lua_newtable(gL.T); |
| 6804 | |
| 6805 | /* Register Lua functions. */ |
| 6806 | hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline); |
| 6807 | hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv); |
| 6808 | hlua_class_function(gL.T, "send", hlua_applet_tcp_send); |
| 6809 | |
| 6810 | lua_settable(gL.T, -3); |
| 6811 | |
| 6812 | /* Register previous table in the registry with reference and named entry. */ |
| 6813 | lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */ |
| 6814 | lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_APPLET_TCP); /* register class session. */ |
| 6815 | class_applet_tcp_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */ |
| 6816 | |
| 6817 | /* |
| 6818 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6819 | * Register class AppletHTTP |
| 6820 | * |
| 6821 | */ |
| 6822 | |
| 6823 | /* Create and fill the metatable. */ |
| 6824 | lua_newtable(gL.T); |
| 6825 | |
| 6826 | /* Create the __tostring identifier */ |
| 6827 | lua_pushstring(gL.T, "__tostring"); |
| 6828 | lua_pushstring(gL.T, CLASS_APPLET_HTTP); |
| 6829 | lua_pushcclosure(gL.T, hlua_dump_object, 1); |
| 6830 | lua_rawset(gL.T, -3); |
| 6831 | |
| 6832 | /* Create and fille the __index entry. */ |
| 6833 | lua_pushstring(gL.T, "__index"); |
| 6834 | lua_newtable(gL.T); |
| 6835 | |
| 6836 | /* Register Lua functions. */ |
| 6837 | hlua_class_function(gL.T, "getline", hlua_applet_http_getline); |
| 6838 | hlua_class_function(gL.T, "receive", hlua_applet_http_recv); |
| 6839 | hlua_class_function(gL.T, "send", hlua_applet_http_send); |
| 6840 | hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader); |
| 6841 | hlua_class_function(gL.T, "set_status", hlua_applet_http_status); |
| 6842 | hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response); |
| 6843 | |
| 6844 | lua_settable(gL.T, -3); |
| 6845 | |
| 6846 | /* Register previous table in the registry with reference and named entry. */ |
| 6847 | lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */ |
| 6848 | lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_APPLET_HTTP); /* register class session. */ |
| 6849 | class_applet_http_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */ |
| 6850 | |
| 6851 | /* |
| 6852 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 6853 | * Register class TXN |
| 6854 | * |
| 6855 | */ |
| 6856 | |
| 6857 | /* Create and fill the metatable. */ |
| 6858 | lua_newtable(gL.T); |
| 6859 | |
Thierry FOURNIER | d2a3dcc | 2015-09-18 07:35:06 +0200 | [diff] [blame] | 6860 | /* Create the __tostring identifier */ |
| 6861 | lua_pushstring(gL.T, "__tostring"); |
| 6862 | lua_pushstring(gL.T, CLASS_TXN); |
| 6863 | lua_pushcclosure(gL.T, hlua_dump_object, 1); |
| 6864 | lua_rawset(gL.T, -3); |
| 6865 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 6866 | /* Create and fille the __index entry. */ |
| 6867 | lua_pushstring(gL.T, "__index"); |
| 6868 | lua_newtable(gL.T); |
| 6869 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 6870 | /* Register Lua functions. */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 6871 | hlua_class_function(gL.T, "set_priv", hlua_set_priv); |
| 6872 | hlua_class_function(gL.T, "get_priv", hlua_get_priv); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 6873 | hlua_class_function(gL.T, "set_var", hlua_set_var); |
| 6874 | hlua_class_function(gL.T, "get_var", hlua_get_var); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 6875 | hlua_class_function(gL.T, "done", hlua_txn_done); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 6876 | hlua_class_function(gL.T, "set_loglevel",hlua_txn_set_loglevel); |
| 6877 | hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos); |
| 6878 | hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 6879 | hlua_class_function(gL.T, "deflog", hlua_txn_deflog); |
| 6880 | hlua_class_function(gL.T, "log", hlua_txn_log); |
| 6881 | hlua_class_function(gL.T, "Debug", hlua_txn_log_debug); |
| 6882 | hlua_class_function(gL.T, "Info", hlua_txn_log_info); |
| 6883 | hlua_class_function(gL.T, "Warning", hlua_txn_log_warning); |
| 6884 | hlua_class_function(gL.T, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 6885 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 6886 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 6887 | |
| 6888 | /* Register previous table in the registry with reference and named entry. */ |
| 6889 | lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */ |
| 6890 | lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_TXN); /* register class session. */ |
| 6891 | class_txn_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 6892 | |
| 6893 | /* |
| 6894 | * |
| 6895 | * Register class Socket |
| 6896 | * |
| 6897 | */ |
| 6898 | |
| 6899 | /* Create and fill the metatable. */ |
| 6900 | lua_newtable(gL.T); |
| 6901 | |
Thierry FOURNIER | d2a3dcc | 2015-09-18 07:35:06 +0200 | [diff] [blame] | 6902 | /* Create the __tostring identifier */ |
| 6903 | lua_pushstring(gL.T, "__tostring"); |
| 6904 | lua_pushstring(gL.T, CLASS_SOCKET); |
| 6905 | lua_pushcclosure(gL.T, hlua_dump_object, 1); |
| 6906 | lua_rawset(gL.T, -3); |
| 6907 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 6908 | /* Create and fille the __index entry. */ |
| 6909 | lua_pushstring(gL.T, "__index"); |
| 6910 | lua_newtable(gL.T); |
| 6911 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 6912 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 6913 | hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 6914 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 6915 | hlua_class_function(gL.T, "connect", hlua_socket_connect); |
| 6916 | hlua_class_function(gL.T, "send", hlua_socket_send); |
| 6917 | hlua_class_function(gL.T, "receive", hlua_socket_receive); |
| 6918 | hlua_class_function(gL.T, "close", hlua_socket_close); |
| 6919 | hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername); |
| 6920 | hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname); |
| 6921 | hlua_class_function(gL.T, "setoption", hlua_socket_setoption); |
| 6922 | hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout); |
| 6923 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 6924 | 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] | 6925 | |
| 6926 | /* Register the garbage collector entry. */ |
| 6927 | lua_pushstring(gL.T, "__gc"); |
| 6928 | lua_pushcclosure(gL.T, hlua_socket_gc, 0); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 6929 | 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] | 6930 | |
| 6931 | /* Register previous table in the registry with reference and named entry. */ |
| 6932 | lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */ |
| 6933 | lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */ |
| 6934 | lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_SOCKET); /* register class socket. */ |
| 6935 | class_socket_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class socket. */ |
| 6936 | |
| 6937 | /* Proxy and server configuration initialisation. */ |
| 6938 | memset(&socket_proxy, 0, sizeof(socket_proxy)); |
| 6939 | init_new_proxy(&socket_proxy); |
| 6940 | socket_proxy.parent = NULL; |
| 6941 | socket_proxy.last_change = now.tv_sec; |
| 6942 | socket_proxy.id = "LUA-SOCKET"; |
| 6943 | socket_proxy.cap = PR_CAP_FE | PR_CAP_BE; |
| 6944 | socket_proxy.maxconn = 0; |
| 6945 | socket_proxy.accept = NULL; |
| 6946 | socket_proxy.options2 |= PR_O2_INDEPSTR; |
| 6947 | socket_proxy.srv = NULL; |
| 6948 | socket_proxy.conn_retries = 0; |
| 6949 | socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */ |
| 6950 | |
| 6951 | /* Init TCP server: unchanged parameters */ |
| 6952 | memset(&socket_tcp, 0, sizeof(socket_tcp)); |
| 6953 | socket_tcp.next = NULL; |
| 6954 | socket_tcp.proxy = &socket_proxy; |
| 6955 | socket_tcp.obj_type = OBJ_TYPE_SERVER; |
| 6956 | LIST_INIT(&socket_tcp.actconns); |
| 6957 | LIST_INIT(&socket_tcp.pendconns); |
Willy Tarreau | 600802a | 2015-08-04 17:19:06 +0200 | [diff] [blame] | 6958 | LIST_INIT(&socket_tcp.priv_conns); |
Willy Tarreau | 173a1c6 | 2015-08-05 10:31:57 +0200 | [diff] [blame] | 6959 | LIST_INIT(&socket_tcp.idle_conns); |
Willy Tarreau | 7017cb0 | 2015-08-05 16:35:23 +0200 | [diff] [blame] | 6960 | LIST_INIT(&socket_tcp.safe_conns); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 6961 | socket_tcp.state = SRV_ST_RUNNING; /* early server setup */ |
| 6962 | socket_tcp.last_change = 0; |
| 6963 | socket_tcp.id = "LUA-TCP-CONN"; |
| 6964 | socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 6965 | socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 6966 | socket_tcp.pp_opts = 0; /* Remove proxy protocol. */ |
| 6967 | |
| 6968 | /* XXX: Copy default parameter from default server, |
| 6969 | * but the default server is not initialized. |
| 6970 | */ |
| 6971 | socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue; |
| 6972 | socket_tcp.minconn = socket_proxy.defsrv.minconn; |
| 6973 | socket_tcp.maxconn = socket_proxy.defsrv.maxconn; |
| 6974 | socket_tcp.slowstart = socket_proxy.defsrv.slowstart; |
| 6975 | socket_tcp.onerror = socket_proxy.defsrv.onerror; |
| 6976 | socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 6977 | socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 6978 | socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 6979 | socket_tcp.uweight = socket_proxy.defsrv.iweight; |
| 6980 | socket_tcp.iweight = socket_proxy.defsrv.iweight; |
| 6981 | |
| 6982 | socket_tcp.check.status = HCHK_STATUS_INI; |
| 6983 | socket_tcp.check.rise = socket_proxy.defsrv.check.rise; |
| 6984 | socket_tcp.check.fall = socket_proxy.defsrv.check.fall; |
| 6985 | socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */ |
| 6986 | socket_tcp.check.server = &socket_tcp; |
| 6987 | |
| 6988 | socket_tcp.agent.status = HCHK_STATUS_INI; |
| 6989 | socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise; |
| 6990 | socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall; |
| 6991 | socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */ |
| 6992 | socket_tcp.agent.server = &socket_tcp; |
| 6993 | |
| 6994 | socket_tcp.xprt = &raw_sock; |
| 6995 | |
| 6996 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 6997 | /* Init TCP server: unchanged parameters */ |
| 6998 | memset(&socket_ssl, 0, sizeof(socket_ssl)); |
| 6999 | socket_ssl.next = NULL; |
| 7000 | socket_ssl.proxy = &socket_proxy; |
| 7001 | socket_ssl.obj_type = OBJ_TYPE_SERVER; |
| 7002 | LIST_INIT(&socket_ssl.actconns); |
| 7003 | LIST_INIT(&socket_ssl.pendconns); |
Willy Tarreau | 600802a | 2015-08-04 17:19:06 +0200 | [diff] [blame] | 7004 | LIST_INIT(&socket_ssl.priv_conns); |
Willy Tarreau | 173a1c6 | 2015-08-05 10:31:57 +0200 | [diff] [blame] | 7005 | LIST_INIT(&socket_ssl.idle_conns); |
Willy Tarreau | 7017cb0 | 2015-08-05 16:35:23 +0200 | [diff] [blame] | 7006 | LIST_INIT(&socket_ssl.safe_conns); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7007 | socket_ssl.state = SRV_ST_RUNNING; /* early server setup */ |
| 7008 | socket_ssl.last_change = 0; |
| 7009 | socket_ssl.id = "LUA-SSL-CONN"; |
| 7010 | socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 7011 | socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 7012 | socket_ssl.pp_opts = 0; /* Remove proxy protocol. */ |
| 7013 | |
| 7014 | /* XXX: Copy default parameter from default server, |
| 7015 | * but the default server is not initialized. |
| 7016 | */ |
| 7017 | socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue; |
| 7018 | socket_ssl.minconn = socket_proxy.defsrv.minconn; |
| 7019 | socket_ssl.maxconn = socket_proxy.defsrv.maxconn; |
| 7020 | socket_ssl.slowstart = socket_proxy.defsrv.slowstart; |
| 7021 | socket_ssl.onerror = socket_proxy.defsrv.onerror; |
| 7022 | socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 7023 | socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 7024 | socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 7025 | socket_ssl.uweight = socket_proxy.defsrv.iweight; |
| 7026 | socket_ssl.iweight = socket_proxy.defsrv.iweight; |
| 7027 | |
| 7028 | socket_ssl.check.status = HCHK_STATUS_INI; |
| 7029 | socket_ssl.check.rise = socket_proxy.defsrv.check.rise; |
| 7030 | socket_ssl.check.fall = socket_proxy.defsrv.check.fall; |
| 7031 | socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */ |
| 7032 | socket_ssl.check.server = &socket_ssl; |
| 7033 | |
| 7034 | socket_ssl.agent.status = HCHK_STATUS_INI; |
| 7035 | socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise; |
| 7036 | socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall; |
| 7037 | socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */ |
| 7038 | socket_ssl.agent.server = &socket_ssl; |
| 7039 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 7040 | socket_ssl.use_ssl = 1; |
| 7041 | socket_ssl.xprt = &ssl_sock; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7042 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 7043 | for (idx = 0; args[idx] != NULL; idx++) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7044 | if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */ |
| 7045 | /* |
| 7046 | * |
| 7047 | * If the keyword is not known, we can search in the registered |
| 7048 | * server keywords. This is usefull to configure special SSL |
| 7049 | * features like client certificates and ssl_verify. |
| 7050 | * |
| 7051 | */ |
| 7052 | tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error); |
| 7053 | if (tmp_error != 0) { |
| 7054 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 7055 | abort(); /* This must be never arrives because the command line |
| 7056 | not editable by the user. */ |
| 7057 | } |
| 7058 | idx += kw->skip; |
| 7059 | } |
| 7060 | } |
| 7061 | |
| 7062 | /* Initialize SSL server. */ |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 7063 | ssl_sock_prepare_srv_ctx(&socket_ssl, &socket_proxy); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7064 | #endif |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 7065 | } |