Thierry Fournier | e726b14 | 2016-02-11 17:57:57 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Lua unsafe core engine |
| 3 | * |
| 4 | * Copyright 2015-2016 Thierry Fournier <tfournier@arpalert.org> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13 | #include <sys/socket.h> |
| 14 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 15 | #include <ctype.h> |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 16 | #include <setjmp.h> |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 17 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 18 | #include <lauxlib.h> |
| 19 | #include <lua.h> |
| 20 | #include <lualib.h> |
| 21 | |
Thierry FOURNIER | 463119c | 2015-03-10 00:35:36 +0100 | [diff] [blame] | 22 | #if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 503 |
| 23 | #error "Requires Lua 5.3 or later." |
Cyril Bonté | dc0306e | 2015-03-02 00:08:40 +0100 | [diff] [blame] | 24 | #endif |
| 25 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 26 | #include <ebpttree.h> |
| 27 | |
| 28 | #include <common/cfgparse.h> |
| 29 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 30 | #include <types/connection.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 31 | #include <types/hlua.h> |
| 32 | #include <types/proxy.h> |
| 33 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 34 | #include <proto/arg.h> |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 35 | #include <proto/applet.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 36 | #include <proto/channel.h> |
Thierry FOURNIER | 9a819e7 | 2015-02-16 20:22:55 +0100 | [diff] [blame] | 37 | #include <proto/hdr_idx.h> |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 38 | #include <proto/hlua.h> |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 39 | #include <proto/hlua_fcn.h> |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 40 | #include <proto/map.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 41 | #include <proto/obj_type.h> |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 42 | #include <proto/pattern.h> |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 43 | #include <proto/payload.h> |
| 44 | #include <proto/proto_http.h> |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 45 | #include <proto/proto_tcp.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 46 | #include <proto/raw_sock.h> |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 47 | #include <proto/sample.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 48 | #include <proto/server.h> |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 49 | #include <proto/session.h> |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 50 | #include <proto/stream.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 51 | #include <proto/ssl_sock.h> |
| 52 | #include <proto/stream_interface.h> |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 53 | #include <proto/task.h> |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 54 | #include <proto/vars.h> |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 55 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 56 | /* Lua uses longjmp to perform yield or throwing errors. This |
| 57 | * macro is used only for identifying the function that can |
| 58 | * not return because a longjmp is executed. |
| 59 | * __LJMP marks a prototype of hlua file that can use longjmp. |
| 60 | * WILL_LJMP() marks an lua function that will use longjmp. |
| 61 | * MAY_LJMP() marks an lua function that may use longjmp. |
| 62 | */ |
| 63 | #define __LJMP |
| 64 | #define WILL_LJMP(func) func |
| 65 | #define MAY_LJMP(func) func |
| 66 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 67 | /* This couple of function executes securely some Lua calls outside of |
| 68 | * the lua runtime environment. Each Lua call can return a longjmp |
| 69 | * if it encounter a memory error. |
| 70 | * |
| 71 | * Lua documentation extract: |
| 72 | * |
| 73 | * If an error happens outside any protected environment, Lua calls |
| 74 | * a panic function (see lua_atpanic) and then calls abort, thus |
| 75 | * exiting the host application. Your panic function can avoid this |
| 76 | * exit by never returning (e.g., doing a long jump to your own |
| 77 | * recovery point outside Lua). |
| 78 | * |
| 79 | * The panic function runs as if it were a message handler (see |
| 80 | * §2.3); in particular, the error message is at the top of the |
| 81 | * stack. However, there is no guarantee about stack space. To push |
| 82 | * anything on the stack, the panic function must first check the |
| 83 | * available space (see §4.2). |
| 84 | * |
| 85 | * We must check all the Lua entry point. This includes: |
| 86 | * - The include/proto/hlua.h exported functions |
| 87 | * - the task wrapper function |
| 88 | * - The action wrapper function |
| 89 | * - The converters wrapper function |
| 90 | * - The sample-fetch wrapper functions |
| 91 | * |
| 92 | * It is tolerated that the initilisation function returns an abort. |
| 93 | * Before each Lua abort, an error message is writed on stderr. |
| 94 | * |
| 95 | * The macro SET_SAFE_LJMP initialise the longjmp. The Macro |
| 96 | * RESET_SAFE_LJMP reset the longjmp. These function must be macro |
| 97 | * because they must be exists in the program stack when the longjmp |
| 98 | * is called. |
| 99 | */ |
| 100 | jmp_buf safe_ljmp_env; |
| 101 | static int hlua_panic_safe(lua_State *L) { return 0; } |
| 102 | static int hlua_panic_ljmp(lua_State *L) { longjmp(safe_ljmp_env, 1); } |
| 103 | |
| 104 | #define SET_SAFE_LJMP(__L) \ |
| 105 | ({ \ |
| 106 | int ret; \ |
| 107 | if (setjmp(safe_ljmp_env) != 0) { \ |
| 108 | lua_atpanic(__L, hlua_panic_safe); \ |
| 109 | ret = 0; \ |
| 110 | } else { \ |
| 111 | lua_atpanic(__L, hlua_panic_ljmp); \ |
| 112 | ret = 1; \ |
| 113 | } \ |
| 114 | ret; \ |
| 115 | }) |
| 116 | |
| 117 | /* If we are the last function catching Lua errors, we |
| 118 | * must reset the panic function. |
| 119 | */ |
| 120 | #define RESET_SAFE_LJMP(__L) \ |
| 121 | do { \ |
| 122 | lua_atpanic(__L, hlua_panic_safe); \ |
| 123 | } while(0) |
| 124 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 125 | /* Applet status flags */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 126 | #define APPLET_DONE 0x01 /* applet processing is done. */ |
| 127 | #define APPLET_100C 0x02 /* 100 continue expected. */ |
| 128 | #define APPLET_HDR_SENT 0x04 /* Response header sent. */ |
| 129 | #define APPLET_CHUNKED 0x08 /* Use transfer encoding chunked. */ |
| 130 | #define APPLET_LAST_CHK 0x10 /* Last chunk sent. */ |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 131 | #define APPLET_HTTP11 0x20 /* Last chunk sent. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 132 | |
| 133 | #define HTTP_100C "HTTP/1.1 100 Continue\r\n\r\n" |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 134 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 135 | /* The main Lua execution context. */ |
| 136 | struct hlua gL; |
| 137 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 138 | /* This is the memory pool containing all the signal structs. These |
| 139 | * struct are used to store each requiered signal between two tasks. |
| 140 | */ |
| 141 | struct pool_head *pool2_hlua_com; |
| 142 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 143 | /* Used for Socket connection. */ |
| 144 | static struct proxy socket_proxy; |
| 145 | static struct server socket_tcp; |
| 146 | #ifdef USE_OPENSSL |
| 147 | static struct server socket_ssl; |
| 148 | #endif |
| 149 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 150 | /* List head of the function called at the initialisation time. */ |
| 151 | struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions); |
| 152 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 153 | /* The following variables contains the reference of the different |
| 154 | * Lua classes. These references are useful for identify metadata |
| 155 | * associated with an object. |
| 156 | */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 157 | static int class_txn_ref; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 158 | static int class_socket_ref; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 159 | static int class_channel_ref; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 160 | static int class_fetches_ref; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 161 | static int class_converters_ref; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 162 | static int class_http_ref; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 163 | static int class_map_ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 164 | static int class_applet_tcp_ref; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 165 | static int class_applet_http_ref; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 166 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 167 | /* Global Lua execution timeout. By default Lua, execution linked |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 168 | * with stream (actions, sample-fetches and converters) have a |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 169 | * short timeout. Lua linked with tasks doesn't have a timeout |
| 170 | * because a task may remain alive during all the haproxy execution. |
| 171 | */ |
| 172 | static unsigned int hlua_timeout_session = 4000; /* session timeout. */ |
| 173 | static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 174 | static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 175 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 176 | /* Interrupts the Lua processing each "hlua_nb_instruction" instructions. |
| 177 | * it is used for preventing infinite loops. |
| 178 | * |
| 179 | * I test the scheer with an infinite loop containing one incrementation |
| 180 | * and one test. I run this loop between 10 seconds, I raise a ceil of |
| 181 | * 710M loops from one interrupt each 9000 instructions, so I fix the value |
| 182 | * to one interrupt each 10 000 instructions. |
| 183 | * |
| 184 | * configured | Number of |
| 185 | * instructions | loops executed |
| 186 | * between two | in milions |
| 187 | * forced yields | |
| 188 | * ---------------+--------------- |
| 189 | * 10 | 160 |
| 190 | * 500 | 670 |
| 191 | * 1000 | 680 |
| 192 | * 5000 | 700 |
| 193 | * 7000 | 700 |
| 194 | * 8000 | 700 |
| 195 | * 9000 | 710 <- ceil |
| 196 | * 10000 | 710 |
| 197 | * 100000 | 710 |
| 198 | * 1000000 | 710 |
| 199 | * |
| 200 | */ |
| 201 | static unsigned int hlua_nb_instruction = 10000; |
| 202 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 203 | /* Descriptor for the memory allocation state. If limit is not null, it will |
| 204 | * be enforced on any memory allocation. |
| 205 | */ |
| 206 | struct hlua_mem_allocator { |
| 207 | size_t allocated; |
| 208 | size_t limit; |
| 209 | }; |
| 210 | |
| 211 | static struct hlua_mem_allocator hlua_global_allocator; |
| 212 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 213 | static const char error_500[] = |
| 214 | "HTTP/1.0 500 Server Error\r\n" |
| 215 | "Cache-Control: no-cache\r\n" |
| 216 | "Connection: close\r\n" |
| 217 | "Content-Type: text/html\r\n" |
| 218 | "\r\n" |
| 219 | "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n"; |
| 220 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 221 | /* These functions converts types between HAProxy internal args or |
| 222 | * sample and LUA types. Another function permits to check if the |
| 223 | * LUA stack contains arguments according with an required ARG_T |
| 224 | * format. |
| 225 | */ |
| 226 | static int hlua_arg2lua(lua_State *L, const struct arg *arg); |
| 227 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 228 | __LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp, |
| 229 | unsigned int mask, struct proxy *p); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 230 | static int hlua_smp2lua(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 231 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 232 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp); |
| 233 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 234 | __LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg); |
| 235 | |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 236 | #define SEND_ERR(__be, __fmt, __args...) \ |
| 237 | do { \ |
| 238 | send_log(__be, LOG_ERR, __fmt, ## __args); \ |
| 239 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \ |
| 240 | Alert(__fmt, ## __args); \ |
| 241 | } while (0) |
| 242 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 243 | /* Used to check an Lua function type in the stack. It creates and |
| 244 | * returns a reference of the function. This function throws an |
| 245 | * error if the rgument is not a "function". |
| 246 | */ |
| 247 | __LJMP unsigned int hlua_checkfunction(lua_State *L, int argno) |
| 248 | { |
| 249 | if (!lua_isfunction(L, argno)) { |
| 250 | const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, -1)); |
| 251 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 252 | } |
| 253 | lua_pushvalue(L, argno); |
| 254 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 255 | } |
| 256 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 257 | /* Return the string that is of the top of the stack. */ |
| 258 | const char *hlua_get_top_error_string(lua_State *L) |
| 259 | { |
| 260 | if (lua_gettop(L) < 1) |
| 261 | return "unknown error"; |
| 262 | if (lua_type(L, -1) != LUA_TSTRING) |
| 263 | return "unknown error"; |
| 264 | return lua_tostring(L, -1); |
| 265 | } |
| 266 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 267 | /* This function check the number of arguments available in the |
| 268 | * stack. If the number of arguments available is not the same |
| 269 | * then <nb> an error is throwed. |
| 270 | */ |
| 271 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 272 | { |
| 273 | if (lua_gettop(L) == nb) |
| 274 | return; |
| 275 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 276 | } |
| 277 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 278 | /* This fucntion push an error string prefixed by the file name |
| 279 | * and the line number where the error is encountered. |
| 280 | */ |
| 281 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 282 | { |
| 283 | va_list argp; |
| 284 | va_start(argp, fmt); |
| 285 | luaL_where(L, 1); |
| 286 | lua_pushvfstring(L, fmt, argp); |
| 287 | va_end(argp); |
| 288 | lua_concat(L, 2); |
| 289 | return 1; |
| 290 | } |
| 291 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 292 | /* This function register a new signal. "lua" is the current lua |
| 293 | * execution context. It contains a pointer to the associated task. |
| 294 | * "link" is a list head attached to an other task that must be wake |
| 295 | * the lua task if an event occurs. This is useful with external |
| 296 | * events like TCP I/O or sleep functions. This funcion allocate |
| 297 | * memory for the signal. |
| 298 | */ |
| 299 | static int hlua_com_new(struct hlua *lua, struct list *link) |
| 300 | { |
| 301 | struct hlua_com *com = pool_alloc2(pool2_hlua_com); |
| 302 | if (!com) |
| 303 | return 0; |
| 304 | LIST_ADDQ(&lua->com, &com->purge_me); |
| 305 | LIST_ADDQ(link, &com->wake_me); |
| 306 | com->task = lua->task; |
| 307 | return 1; |
| 308 | } |
| 309 | |
| 310 | /* This function purge all the pending signals when the LUA execution |
| 311 | * is finished. This prevent than a coprocess try to wake a deleted |
| 312 | * task. This function remove the memory associated to the signal. |
| 313 | */ |
| 314 | static void hlua_com_purge(struct hlua *lua) |
| 315 | { |
| 316 | struct hlua_com *com, *back; |
| 317 | |
| 318 | /* Delete all pending communication signals. */ |
| 319 | list_for_each_entry_safe(com, back, &lua->com, purge_me) { |
| 320 | LIST_DEL(&com->purge_me); |
| 321 | LIST_DEL(&com->wake_me); |
| 322 | pool_free2(pool2_hlua_com, com); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | /* This function sends signals. It wakes all the tasks attached |
| 327 | * to a list head, and remove the signal, and free the used |
| 328 | * memory. |
| 329 | */ |
| 330 | static void hlua_com_wake(struct list *wake) |
| 331 | { |
| 332 | struct hlua_com *com, *back; |
| 333 | |
| 334 | /* Wake task and delete all pending communication signals. */ |
| 335 | list_for_each_entry_safe(com, back, wake, wake_me) { |
| 336 | LIST_DEL(&com->purge_me); |
| 337 | LIST_DEL(&com->wake_me); |
| 338 | task_wakeup(com->task, TASK_WOKEN_MSG); |
| 339 | pool_free2(pool2_hlua_com, com); |
| 340 | } |
| 341 | } |
| 342 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 343 | /* This functions is used with sample fetch and converters. It |
| 344 | * converts the HAProxy configuration argument in a lua stack |
| 345 | * values. |
| 346 | * |
| 347 | * It takes an array of "arg", and each entry of the array is |
| 348 | * converted and pushed in the LUA stack. |
| 349 | */ |
| 350 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 351 | { |
| 352 | switch (arg->type) { |
| 353 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 354 | case ARGT_TIME: |
| 355 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 356 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 357 | break; |
| 358 | |
| 359 | case ARGT_STR: |
| 360 | lua_pushlstring(L, arg->data.str.str, arg->data.str.len); |
| 361 | break; |
| 362 | |
| 363 | case ARGT_IPV4: |
| 364 | case ARGT_IPV6: |
| 365 | case ARGT_MSK4: |
| 366 | case ARGT_MSK6: |
| 367 | case ARGT_FE: |
| 368 | case ARGT_BE: |
| 369 | case ARGT_TAB: |
| 370 | case ARGT_SRV: |
| 371 | case ARGT_USR: |
| 372 | case ARGT_MAP: |
| 373 | default: |
| 374 | lua_pushnil(L); |
| 375 | break; |
| 376 | } |
| 377 | return 1; |
| 378 | } |
| 379 | |
| 380 | /* This function take one entrie in an LUA stack at the index "ud", |
| 381 | * and try to convert it in an HAProxy argument entry. This is useful |
| 382 | * with sample fetch wrappers. The input arguments are gived to the |
| 383 | * lua wrapper and converted as arg list by thi function. |
| 384 | */ |
| 385 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 386 | { |
| 387 | switch (lua_type(L, ud)) { |
| 388 | |
| 389 | case LUA_TNUMBER: |
| 390 | case LUA_TBOOLEAN: |
| 391 | arg->type = ARGT_SINT; |
| 392 | arg->data.sint = lua_tointeger(L, ud); |
| 393 | break; |
| 394 | |
| 395 | case LUA_TSTRING: |
| 396 | arg->type = ARGT_STR; |
| 397 | arg->data.str.str = (char *)lua_tolstring(L, ud, (size_t *)&arg->data.str.len); |
| 398 | break; |
| 399 | |
| 400 | case LUA_TUSERDATA: |
| 401 | case LUA_TNIL: |
| 402 | case LUA_TTABLE: |
| 403 | case LUA_TFUNCTION: |
| 404 | case LUA_TTHREAD: |
| 405 | case LUA_TLIGHTUSERDATA: |
| 406 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 407 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 408 | break; |
| 409 | } |
| 410 | return 1; |
| 411 | } |
| 412 | |
| 413 | /* the following functions are used to convert a struct sample |
| 414 | * in Lua type. This useful to convert the return of the |
| 415 | * fetchs or converters. |
| 416 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 417 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 418 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 419 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 420 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 421 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 422 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 423 | break; |
| 424 | |
| 425 | case SMP_T_BIN: |
| 426 | case SMP_T_STR: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 427 | 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] | 428 | break; |
| 429 | |
| 430 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 431 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 432 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 433 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 434 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 435 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 436 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 437 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 438 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 439 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 440 | case HTTP_METH_OTHER: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 441 | 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] | 442 | break; |
| 443 | default: |
| 444 | lua_pushnil(L); |
| 445 | break; |
| 446 | } |
| 447 | break; |
| 448 | |
| 449 | case SMP_T_IPV4: |
| 450 | case SMP_T_IPV6: |
| 451 | 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] | 452 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 453 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 454 | 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] | 455 | else |
| 456 | lua_pushnil(L); |
| 457 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 458 | default: |
| 459 | lua_pushnil(L); |
| 460 | break; |
| 461 | } |
| 462 | return 1; |
| 463 | } |
| 464 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 465 | /* the following functions are used to convert a struct sample |
| 466 | * in Lua strings. This is useful to convert the return of the |
| 467 | * fetchs or converters. |
| 468 | */ |
| 469 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 470 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 471 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 472 | |
| 473 | case SMP_T_BIN: |
| 474 | case SMP_T_STR: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 475 | 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] | 476 | break; |
| 477 | |
| 478 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 479 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 480 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 481 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 482 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 483 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 484 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 485 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 486 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 487 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 488 | case HTTP_METH_OTHER: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 489 | 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] | 490 | break; |
| 491 | default: |
| 492 | lua_pushstring(L, ""); |
| 493 | break; |
| 494 | } |
| 495 | break; |
| 496 | |
| 497 | case SMP_T_SINT: |
| 498 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 499 | case SMP_T_IPV4: |
| 500 | case SMP_T_IPV6: |
| 501 | 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] | 502 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 503 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 504 | 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] | 505 | else |
| 506 | lua_pushstring(L, ""); |
| 507 | break; |
| 508 | default: |
| 509 | lua_pushstring(L, ""); |
| 510 | break; |
| 511 | } |
| 512 | return 1; |
| 513 | } |
| 514 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 515 | /* the following functions are used to convert an Lua type in a |
| 516 | * struct sample. This is useful to provide data from a converter |
| 517 | * to the LUA code. |
| 518 | */ |
| 519 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 520 | { |
| 521 | switch (lua_type(L, ud)) { |
| 522 | |
| 523 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 524 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 525 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 526 | break; |
| 527 | |
| 528 | |
| 529 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 530 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 531 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 532 | break; |
| 533 | |
| 534 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 535 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 536 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 537 | 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] | 538 | break; |
| 539 | |
| 540 | case LUA_TUSERDATA: |
| 541 | case LUA_TNIL: |
| 542 | case LUA_TTABLE: |
| 543 | case LUA_TFUNCTION: |
| 544 | case LUA_TTHREAD: |
| 545 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 546 | case LUA_TNONE: |
| 547 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 548 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 549 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 550 | break; |
| 551 | } |
| 552 | return 1; |
| 553 | } |
| 554 | |
| 555 | /* This function check the "argp" builded by another conversion function |
| 556 | * is in accord with the expected argp defined by the "mask". The fucntion |
| 557 | * 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] | 558 | * |
| 559 | * This function assumes thant the argp argument contains ARGM_NBARGS + 1 |
| 560 | * entries. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 561 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 562 | __LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp, |
| 563 | unsigned int mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 564 | { |
| 565 | int min_arg; |
| 566 | int idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 567 | struct proxy *px; |
| 568 | char *sname, *pname; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 569 | |
| 570 | idx = 0; |
| 571 | min_arg = ARGM(mask); |
| 572 | mask >>= ARGM_BITS; |
| 573 | |
| 574 | while (1) { |
| 575 | |
| 576 | /* Check oversize. */ |
| 577 | if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) { |
Cyril Bonté | 577a36a | 2015-03-02 00:08:38 +0100 | [diff] [blame] | 578 | WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | /* Check for mandatory arguments. */ |
| 582 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 583 | if (idx < min_arg) { |
| 584 | |
| 585 | /* If miss other argument than the first one, we return an error. */ |
| 586 | if (idx > 0) |
| 587 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 588 | |
| 589 | /* If first argument have a certain type, some default values |
| 590 | * may be used. See the function smp_resolve_args(). |
| 591 | */ |
| 592 | switch (mask & ARGT_MASK) { |
| 593 | |
| 594 | case ARGT_FE: |
| 595 | if (!(p->cap & PR_CAP_FE)) |
| 596 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 597 | argp[idx].data.prx = p; |
| 598 | argp[idx].type = ARGT_FE; |
| 599 | argp[idx+1].type = ARGT_STOP; |
| 600 | break; |
| 601 | |
| 602 | case ARGT_BE: |
| 603 | if (!(p->cap & PR_CAP_BE)) |
| 604 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 605 | argp[idx].data.prx = p; |
| 606 | argp[idx].type = ARGT_BE; |
| 607 | argp[idx+1].type = ARGT_STOP; |
| 608 | break; |
| 609 | |
| 610 | case ARGT_TAB: |
| 611 | argp[idx].data.prx = p; |
| 612 | argp[idx].type = ARGT_TAB; |
| 613 | argp[idx+1].type = ARGT_STOP; |
| 614 | break; |
| 615 | |
| 616 | default: |
| 617 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 618 | break; |
| 619 | } |
| 620 | } |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 621 | return 0; |
| 622 | } |
| 623 | |
| 624 | /* Check for exceed the number of requiered argument. */ |
| 625 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 626 | argp[idx].type != ARGT_STOP) { |
| 627 | WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected")); |
| 628 | } |
| 629 | |
| 630 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 631 | argp[idx].type == ARGT_STOP) { |
| 632 | return 0; |
| 633 | } |
| 634 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 635 | /* Convert some argument types. */ |
| 636 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 637 | case ARGT_SINT: |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 638 | if (argp[idx].type != ARGT_SINT) |
| 639 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
| 640 | argp[idx].type = ARGT_SINT; |
| 641 | break; |
| 642 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 643 | case ARGT_TIME: |
| 644 | if (argp[idx].type != ARGT_SINT) |
| 645 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 646 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 647 | break; |
| 648 | |
| 649 | case ARGT_SIZE: |
| 650 | if (argp[idx].type != ARGT_SINT) |
| 651 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 652 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 653 | break; |
| 654 | |
| 655 | case ARGT_FE: |
| 656 | if (argp[idx].type != ARGT_STR) |
| 657 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
| 658 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 659 | trash.str[argp[idx].data.str.len] = 0; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 660 | argp[idx].data.prx = proxy_fe_by_name(trash.str); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 661 | if (!argp[idx].data.prx) |
| 662 | WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist")); |
| 663 | argp[idx].type = ARGT_FE; |
| 664 | break; |
| 665 | |
| 666 | case ARGT_BE: |
| 667 | if (argp[idx].type != ARGT_STR) |
| 668 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
| 669 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 670 | trash.str[argp[idx].data.str.len] = 0; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 671 | argp[idx].data.prx = proxy_be_by_name(trash.str); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 672 | if (!argp[idx].data.prx) |
| 673 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 674 | argp[idx].type = ARGT_BE; |
| 675 | break; |
| 676 | |
| 677 | case ARGT_TAB: |
| 678 | if (argp[idx].type != ARGT_STR) |
| 679 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
| 680 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 681 | trash.str[argp[idx].data.str.len] = 0; |
Willy Tarreau | e2dc1fa | 2015-05-26 12:08:07 +0200 | [diff] [blame] | 682 | argp[idx].data.prx = proxy_tbl_by_name(trash.str); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 683 | if (!argp[idx].data.prx) |
| 684 | WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist")); |
| 685 | argp[idx].type = ARGT_TAB; |
| 686 | break; |
| 687 | |
| 688 | case ARGT_SRV: |
| 689 | if (argp[idx].type != ARGT_STR) |
| 690 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
| 691 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 692 | trash.str[argp[idx].data.str.len] = 0; |
| 693 | sname = strrchr(trash.str, '/'); |
| 694 | if (sname) { |
| 695 | *sname++ = '\0'; |
| 696 | pname = trash.str; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 697 | px = proxy_be_by_name(pname); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 698 | if (!px) |
| 699 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 700 | } |
| 701 | else { |
| 702 | sname = trash.str; |
| 703 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 704 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 705 | argp[idx].data.srv = findserver(px, sname); |
| 706 | if (!argp[idx].data.srv) |
| 707 | WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist")); |
| 708 | argp[idx].type = ARGT_SRV; |
| 709 | break; |
| 710 | |
| 711 | case ARGT_IPV4: |
| 712 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 713 | trash.str[argp[idx].data.str.len] = 0; |
| 714 | if (inet_pton(AF_INET, trash.str, &argp[idx].data.ipv4)) |
| 715 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address")); |
| 716 | argp[idx].type = ARGT_IPV4; |
| 717 | break; |
| 718 | |
| 719 | case ARGT_MSK4: |
| 720 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 721 | trash.str[argp[idx].data.str.len] = 0; |
| 722 | if (!str2mask(trash.str, &argp[idx].data.ipv4)) |
| 723 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask")); |
| 724 | argp[idx].type = ARGT_MSK4; |
| 725 | break; |
| 726 | |
| 727 | case ARGT_IPV6: |
| 728 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 729 | trash.str[argp[idx].data.str.len] = 0; |
| 730 | if (inet_pton(AF_INET6, trash.str, &argp[idx].data.ipv6)) |
| 731 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address")); |
| 732 | argp[idx].type = ARGT_IPV6; |
| 733 | break; |
| 734 | |
| 735 | case ARGT_MSK6: |
| 736 | case ARGT_MAP: |
| 737 | case ARGT_REG: |
| 738 | case ARGT_USR: |
| 739 | WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 740 | break; |
| 741 | } |
| 742 | |
| 743 | /* Check for type of argument. */ |
| 744 | if ((mask & ARGT_MASK) != argp[idx].type) { |
| 745 | const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 746 | arg_type_names[(mask & ARGT_MASK)], |
| 747 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 748 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 749 | } |
| 750 | |
| 751 | /* Next argument. */ |
| 752 | mask >>= ARGT_BITS; |
| 753 | idx++; |
| 754 | } |
| 755 | } |
| 756 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 757 | /* |
| 758 | * The following functions are used to make correspondance between the the |
| 759 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 760 | * |
| 761 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 762 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 763 | */ |
| 764 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 765 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 766 | struct hlua **hlua = lua_getextraspace(L); |
| 767 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 768 | } |
| 769 | static inline void hlua_sethlua(struct hlua *hlua) |
| 770 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 771 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 772 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 773 | } |
| 774 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 775 | /* This function is used to send logs. It try to send on screen (stderr) |
| 776 | * and on the default syslog server. |
| 777 | */ |
| 778 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 779 | { |
| 780 | struct tm tm; |
| 781 | char *p; |
| 782 | |
| 783 | /* Cleanup the log message. */ |
| 784 | p = trash.str; |
| 785 | for (; *msg != '\0'; msg++, p++) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 786 | if (p >= trash.str + trash.size - 1) { |
| 787 | /* Break the message if exceed the buffer size. */ |
| 788 | *(p-4) = ' '; |
| 789 | *(p-3) = '.'; |
| 790 | *(p-2) = '.'; |
| 791 | *(p-1) = '.'; |
| 792 | break; |
| 793 | } |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 794 | if (isprint(*msg)) |
| 795 | *p = *msg; |
| 796 | else |
| 797 | *p = '.'; |
| 798 | } |
| 799 | *p = '\0'; |
| 800 | |
Thierry FOURNIER | 5554e29 | 2015-09-09 11:21:37 +0200 | [diff] [blame] | 801 | send_log(px, level, "%s\n", trash.str); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 802 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 803 | get_localtime(date.tv_sec, &tm); |
| 804 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 805 | log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, |
| 806 | (int)getpid(), trash.str); |
| 807 | fflush(stderr); |
| 808 | } |
| 809 | } |
| 810 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 811 | /* This function just ensure that the yield will be always |
| 812 | * returned with a timeout and permit to set some flags |
| 813 | */ |
| 814 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 815 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 816 | { |
| 817 | struct hlua *hlua = hlua_gethlua(L); |
| 818 | |
| 819 | /* Set the wake timeout. If timeout is required, we set |
| 820 | * the expiration time. |
| 821 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 822 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 823 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 824 | hlua->flags |= flags; |
| 825 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 826 | /* Process the yield. */ |
| 827 | WILL_LJMP(lua_yieldk(L, nresults, ctx, k)); |
| 828 | } |
| 829 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 830 | /* This function initialises the Lua environment stored in the stream. |
| 831 | * 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] | 832 | * 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] | 833 | * |
| 834 | * This function is particular. it initialises a new Lua thread. If the |
| 835 | * initialisation fails (example: out of memory error), the lua function |
| 836 | * throws an error (longjmp). |
| 837 | * |
| 838 | * This function manipulates two Lua stack: the main and the thread. Only |
| 839 | * the main stack can fail. The thread is not manipulated. This function |
| 840 | * MUST NOT manipulate the created thread stack state, because is not |
| 841 | * proctected agains error throwed by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 842 | */ |
| 843 | int hlua_ctx_init(struct hlua *lua, struct task *task) |
| 844 | { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 845 | if (!SET_SAFE_LJMP(gL.T)) { |
| 846 | lua->Tref = LUA_REFNIL; |
| 847 | return 0; |
| 848 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 849 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 850 | lua->flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 851 | LIST_INIT(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 852 | lua->T = lua_newthread(gL.T); |
| 853 | if (!lua->T) { |
| 854 | lua->Tref = LUA_REFNIL; |
| 855 | return 0; |
| 856 | } |
| 857 | hlua_sethlua(lua); |
| 858 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 859 | lua->task = task; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 860 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 861 | return 1; |
| 862 | } |
| 863 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 864 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 865 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 866 | * is not freed. |
| 867 | */ |
| 868 | void hlua_ctx_destroy(struct hlua *lua) |
| 869 | { |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 870 | if (!lua->T) |
| 871 | return; |
| 872 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 873 | /* Purge all the pending signals. */ |
| 874 | hlua_com_purge(lua); |
| 875 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 876 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 877 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 878 | |
| 879 | /* Forces a garbage collecting process. If the Lua program is finished |
| 880 | * without error, we run the GC on the thread pointer. Its freed all |
| 881 | * the unused memory. |
| 882 | * If the thread is finnish with an error or is currently yielded, |
| 883 | * it seems that the GC applied on the thread doesn't clean anything, |
| 884 | * so e run the GC on the main thread. |
| 885 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 886 | * the garbage collection. |
| 887 | */ |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 888 | if (lua->flags & HLUA_MUST_GC) { |
| 889 | lua_gc(lua->T, LUA_GCCOLLECT, 0); |
| 890 | if (lua_status(lua->T) != LUA_OK) |
| 891 | lua_gc(gL.T, LUA_GCCOLLECT, 0); |
| 892 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 893 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 894 | lua->T = NULL; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 895 | } |
| 896 | |
| 897 | /* This function is used to restore the Lua context when a coroutine |
| 898 | * fails. This function copy the common memory between old coroutine |
| 899 | * and the new coroutine. The old coroutine is destroyed, and its |
| 900 | * replaced by the new coroutine. |
| 901 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 902 | * as string error message and it is copied in the new stack. |
| 903 | */ |
| 904 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 905 | { |
| 906 | lua_State *T; |
| 907 | int new_ref; |
| 908 | |
| 909 | /* Renew the main LUA stack doesn't have sense. */ |
| 910 | if (lua == &gL) |
| 911 | return 0; |
| 912 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 913 | /* New Lua coroutine. */ |
| 914 | T = lua_newthread(gL.T); |
| 915 | if (!T) |
| 916 | return 0; |
| 917 | |
| 918 | /* Copy last error message. */ |
| 919 | if (keep_msg) |
| 920 | lua_xmove(lua->T, T, 1); |
| 921 | |
| 922 | /* Copy data between the coroutines. */ |
| 923 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 924 | lua_xmove(lua->T, T, 1); |
| 925 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */ |
| 926 | |
| 927 | /* Destroy old data. */ |
| 928 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 929 | |
| 930 | /* The thread is garbage collected by Lua. */ |
| 931 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 932 | |
| 933 | /* Fill the struct with the new coroutine values. */ |
| 934 | lua->Mref = new_ref; |
| 935 | lua->T = T; |
| 936 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 937 | |
| 938 | /* Set context. */ |
| 939 | hlua_sethlua(lua); |
| 940 | |
| 941 | return 1; |
| 942 | } |
| 943 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 944 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 945 | { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 946 | struct hlua *hlua = hlua_gethlua(L); |
| 947 | |
| 948 | /* Lua cannot yield when its returning from a function, |
| 949 | * so, we can fix the interrupt hook to 1 instruction, |
| 950 | * expecting that the function is finnished. |
| 951 | */ |
| 952 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 953 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 954 | return; |
| 955 | } |
| 956 | |
| 957 | /* restore the interrupt condition. */ |
| 958 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 959 | |
| 960 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 961 | * If the state is not yieldable, trying yield causes an error. |
| 962 | */ |
| 963 | if (lua_isyieldable(L)) |
| 964 | WILL_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
| 965 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 966 | /* If we cannot yield, update the clock and check the timeout. */ |
| 967 | tv_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 968 | hlua->run_time += now_ms - hlua->start_time; |
| 969 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 970 | lua_pushfstring(L, "execution timeout"); |
| 971 | WILL_LJMP(lua_error(L)); |
| 972 | } |
| 973 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 974 | /* Update the start time. */ |
| 975 | hlua->start_time = now_ms; |
| 976 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 977 | /* Try to interrupt the process at the end of the current |
| 978 | * unyieldable function. |
| 979 | */ |
| 980 | 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] | 981 | } |
| 982 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 983 | /* This function start or resumes the Lua stack execution. If the flag |
| 984 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 985 | * The function return an error. |
| 986 | * |
| 987 | * The function can returns 4 values: |
| 988 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 989 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 990 | * task wakeup. |
| 991 | * - HLUA_E_ERRMSG : An error has occured, an error message is set in |
| 992 | * the top of the stack. |
| 993 | * - HLUA_E_ERR : An error has occured without error message. |
| 994 | * |
| 995 | * If an error occured, the stack is renewed and it is ready to run new |
| 996 | * LUA code. |
| 997 | */ |
| 998 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 999 | { |
| 1000 | int ret; |
| 1001 | const char *msg; |
| 1002 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1003 | /* Initialise run time counter. */ |
| 1004 | if (!HLUA_IS_RUNNING(lua)) |
| 1005 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1006 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1007 | resume_execution: |
| 1008 | |
| 1009 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1010 | * instructions. it is used for preventing infinite loops. |
| 1011 | */ |
| 1012 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1013 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1014 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1015 | HLUA_SET_RUN(lua); |
| 1016 | HLUA_CLR_CTRLYIELD(lua); |
| 1017 | HLUA_CLR_WAKERESWR(lua); |
| 1018 | HLUA_CLR_WAKEREQWR(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1019 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1020 | /* Update the start time. */ |
| 1021 | lua->start_time = now_ms; |
| 1022 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1023 | /* Call the function. */ |
| 1024 | ret = lua_resume(lua->T, gL.T, lua->nargs); |
| 1025 | switch (ret) { |
| 1026 | |
| 1027 | case LUA_OK: |
| 1028 | ret = HLUA_E_OK; |
| 1029 | break; |
| 1030 | |
| 1031 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1032 | /* Check if the execution timeout is expired. It it is the case, we |
| 1033 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1034 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1035 | tv_update_date(0, 1); |
| 1036 | lua->run_time += now_ms - lua->start_time; |
| 1037 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1038 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1039 | if (!lua_checkstack(lua->T, 1)) { |
| 1040 | ret = HLUA_E_ERR; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1041 | break; |
| 1042 | } |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1043 | lua_pushfstring(lua->T, "execution timeout"); |
| 1044 | ret = HLUA_E_ERRMSG; |
| 1045 | break; |
| 1046 | } |
| 1047 | /* Process the forced yield. if the general yield is not allowed or |
| 1048 | * if no task were associated this the current Lua execution |
| 1049 | * coroutine, we resume the execution. Else we want to return in the |
| 1050 | * scheduler and we want to be waked up again, to continue the |
| 1051 | * current Lua execution. So we schedule our own task. |
| 1052 | */ |
| 1053 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1054 | if (!yield_allowed || !lua->task) |
| 1055 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1056 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1057 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1058 | if (!yield_allowed) { |
| 1059 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1060 | if (!lua_checkstack(lua->T, 1)) { |
| 1061 | ret = HLUA_E_ERR; |
| 1062 | break; |
| 1063 | } |
| 1064 | lua_pushfstring(lua->T, "yield not allowed"); |
| 1065 | ret = HLUA_E_ERRMSG; |
| 1066 | break; |
| 1067 | } |
| 1068 | ret = HLUA_E_AGAIN; |
| 1069 | break; |
| 1070 | |
| 1071 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1072 | |
| 1073 | /* Special exit case. The traditionnal exit is returned as an error |
| 1074 | * because the errors ares the only one mean to return immediately |
| 1075 | * from and lua execution. |
| 1076 | */ |
| 1077 | if (lua->flags & HLUA_EXIT) { |
| 1078 | ret = HLUA_E_OK; |
Thierry FOURNIER | e1587b3 | 2015-08-28 09:54:13 +0200 | [diff] [blame] | 1079 | hlua_ctx_renew(lua, 0); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1080 | break; |
| 1081 | } |
| 1082 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1083 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1084 | if (!lua_checkstack(lua->T, 1)) { |
| 1085 | ret = HLUA_E_ERR; |
| 1086 | break; |
| 1087 | } |
| 1088 | msg = lua_tostring(lua->T, -1); |
| 1089 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1090 | lua_pop(lua->T, 1); |
| 1091 | if (msg) |
| 1092 | lua_pushfstring(lua->T, "runtime error: %s", msg); |
| 1093 | else |
| 1094 | lua_pushfstring(lua->T, "unknown runtime error"); |
| 1095 | ret = HLUA_E_ERRMSG; |
| 1096 | break; |
| 1097 | |
| 1098 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1099 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1100 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1101 | if (!lua_checkstack(lua->T, 1)) { |
| 1102 | ret = HLUA_E_ERR; |
| 1103 | break; |
| 1104 | } |
| 1105 | lua_pushfstring(lua->T, "out of memory error"); |
| 1106 | ret = HLUA_E_ERRMSG; |
| 1107 | break; |
| 1108 | |
| 1109 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1110 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1111 | if (!lua_checkstack(lua->T, 1)) { |
| 1112 | ret = HLUA_E_ERR; |
| 1113 | break; |
| 1114 | } |
| 1115 | msg = lua_tostring(lua->T, -1); |
| 1116 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1117 | lua_pop(lua->T, 1); |
| 1118 | if (msg) |
| 1119 | lua_pushfstring(lua->T, "message handler error: %s", msg); |
| 1120 | else |
| 1121 | lua_pushfstring(lua->T, "message handler error"); |
| 1122 | ret = HLUA_E_ERRMSG; |
| 1123 | break; |
| 1124 | |
| 1125 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1126 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1127 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1128 | if (!lua_checkstack(lua->T, 1)) { |
| 1129 | ret = HLUA_E_ERR; |
| 1130 | break; |
| 1131 | } |
| 1132 | lua_pushfstring(lua->T, "unknonwn error"); |
| 1133 | ret = HLUA_E_ERRMSG; |
| 1134 | break; |
| 1135 | } |
| 1136 | |
Thierry FOURNIER | 6ab4d8e | 2015-09-27 22:17:19 +0200 | [diff] [blame] | 1137 | /* This GC permits to destroy some object when a Lua timeout strikes. */ |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1138 | if (lua->flags & HLUA_MUST_GC && |
| 1139 | ret != HLUA_E_AGAIN) |
Thierry FOURNIER | 6ab4d8e | 2015-09-27 22:17:19 +0200 | [diff] [blame] | 1140 | lua_gc(lua->T, LUA_GCCOLLECT, 0); |
| 1141 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1142 | switch (ret) { |
| 1143 | case HLUA_E_AGAIN: |
| 1144 | break; |
| 1145 | |
| 1146 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1147 | hlua_com_purge(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1148 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1149 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1150 | break; |
| 1151 | |
| 1152 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1153 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1154 | hlua_com_purge(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1155 | hlua_ctx_renew(lua, 0); |
| 1156 | break; |
| 1157 | |
| 1158 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1159 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1160 | hlua_com_purge(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1161 | break; |
| 1162 | } |
| 1163 | |
| 1164 | return ret; |
| 1165 | } |
| 1166 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1167 | /* This function exit the current code. */ |
| 1168 | __LJMP static int hlua_done(lua_State *L) |
| 1169 | { |
| 1170 | struct hlua *hlua = hlua_gethlua(L); |
| 1171 | |
| 1172 | hlua->flags |= HLUA_EXIT; |
| 1173 | WILL_LJMP(lua_error(L)); |
| 1174 | |
| 1175 | return 0; |
| 1176 | } |
| 1177 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1178 | /* This function is an LUA binding. It provides a function |
| 1179 | * for deleting ACL from a referenced ACL file. |
| 1180 | */ |
| 1181 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1182 | { |
| 1183 | const char *name; |
| 1184 | const char *key; |
| 1185 | struct pat_ref *ref; |
| 1186 | |
| 1187 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1188 | |
| 1189 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1190 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1191 | |
| 1192 | ref = pat_ref_lookup(name); |
| 1193 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1194 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1195 | |
| 1196 | pat_ref_delete(ref, key); |
| 1197 | return 0; |
| 1198 | } |
| 1199 | |
| 1200 | /* This function is an LUA binding. It provides a function |
| 1201 | * for deleting map entry from a referenced map file. |
| 1202 | */ |
| 1203 | static int hlua_del_map(lua_State *L) |
| 1204 | { |
| 1205 | const char *name; |
| 1206 | const char *key; |
| 1207 | struct pat_ref *ref; |
| 1208 | |
| 1209 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1210 | |
| 1211 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1212 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1213 | |
| 1214 | ref = pat_ref_lookup(name); |
| 1215 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1216 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1217 | |
| 1218 | pat_ref_delete(ref, key); |
| 1219 | return 0; |
| 1220 | } |
| 1221 | |
| 1222 | /* This function is an LUA binding. It provides a function |
| 1223 | * for adding ACL pattern from a referenced ACL file. |
| 1224 | */ |
| 1225 | static int hlua_add_acl(lua_State *L) |
| 1226 | { |
| 1227 | const char *name; |
| 1228 | const char *key; |
| 1229 | struct pat_ref *ref; |
| 1230 | |
| 1231 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1232 | |
| 1233 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1234 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1235 | |
| 1236 | ref = pat_ref_lookup(name); |
| 1237 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1238 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1239 | |
| 1240 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1241 | pat_ref_add(ref, key, NULL, NULL); |
| 1242 | return 0; |
| 1243 | } |
| 1244 | |
| 1245 | /* This function is an LUA binding. It provides a function |
| 1246 | * for setting map pattern and sample from a referenced map |
| 1247 | * file. |
| 1248 | */ |
| 1249 | static int hlua_set_map(lua_State *L) |
| 1250 | { |
| 1251 | const char *name; |
| 1252 | const char *key; |
| 1253 | const char *value; |
| 1254 | struct pat_ref *ref; |
| 1255 | |
| 1256 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1257 | |
| 1258 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1259 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1260 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 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, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1265 | |
| 1266 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1267 | pat_ref_set(ref, key, value, NULL); |
| 1268 | else |
| 1269 | pat_ref_add(ref, key, value, NULL); |
| 1270 | return 0; |
| 1271 | } |
| 1272 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1273 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1274 | * an integer or user data. This data is associated with a metatable. This |
| 1275 | * metatable have an original version registred in the global context with |
| 1276 | * the name of the object (_G[<name>] = <metable> ). |
| 1277 | * |
| 1278 | * A metable is a table that modify the standard behavior of a standard |
| 1279 | * access to the associated data. The entries of this new metatable are |
| 1280 | * defined as is: |
| 1281 | * |
| 1282 | * http://lua-users.org/wiki/MetatableEvents |
| 1283 | * |
| 1284 | * __index |
| 1285 | * |
| 1286 | * we access an absent field in a table, the result is nil. This is |
| 1287 | * true, but it is not the whole truth. Actually, such access triggers |
| 1288 | * the interpreter to look for an __index metamethod: If there is no |
| 1289 | * such method, as usually happens, then the access results in nil; |
| 1290 | * otherwise, the metamethod will provide the result. |
| 1291 | * |
| 1292 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1293 | * the key does not appear in the table, but the metatable has an __index |
| 1294 | * property: |
| 1295 | * |
| 1296 | * - if the value is a function, the function is called, passing in the |
| 1297 | * table and the key; the return value of that function is returned as |
| 1298 | * the result. |
| 1299 | * |
| 1300 | * - if the value is another table, the value of the key in that table is |
| 1301 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1302 | * table's metatable has an __index property, then it continues on up) |
| 1303 | * |
| 1304 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1305 | * |
| 1306 | * http://www.lua.org/pil/13.4.1.html |
| 1307 | * |
| 1308 | * __newindex |
| 1309 | * |
| 1310 | * Like __index, but control property assignment. |
| 1311 | * |
| 1312 | * __mode - Control weak references. A string value with one or both |
| 1313 | * of the characters 'k' and 'v' which specifies that the the |
| 1314 | * keys and/or values in the table are weak references. |
| 1315 | * |
| 1316 | * __call - Treat a table like a function. When a table is followed by |
| 1317 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1318 | * a __call key pointing to a function, that function is invoked |
| 1319 | * (passing any specified arguments) and the return value is |
| 1320 | * returned. |
| 1321 | * |
| 1322 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1323 | * called, if the metatable for myTable has a __metatable |
| 1324 | * key, the value of that key is returned instead of the |
| 1325 | * actual metatable. |
| 1326 | * |
| 1327 | * __tostring - Control string representation. When the builtin |
| 1328 | * "tostring( myTable )" function is called, if the metatable |
| 1329 | * for myTable has a __tostring property set to a function, |
| 1330 | * that function is invoked (passing myTable to it) and the |
| 1331 | * return value is used as the string representation. |
| 1332 | * |
| 1333 | * __len - Control table length. When the table length is requested using |
| 1334 | * the length operator ( '#' ), if the metatable for myTable has |
| 1335 | * a __len key pointing to a function, that function is invoked |
| 1336 | * (passing myTable to it) and the return value used as the value |
| 1337 | * of "#myTable". |
| 1338 | * |
| 1339 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1340 | * collected, if the metatable has a __gc field pointing to a |
| 1341 | * function, that function is first invoked, passing the userdata |
| 1342 | * to it. The __gc metamethod is not called for tables. |
| 1343 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1344 | * |
| 1345 | * Special metamethods for redefining standard operators: |
| 1346 | * http://www.lua.org/pil/13.1.html |
| 1347 | * |
| 1348 | * __add "+" |
| 1349 | * __sub "-" |
| 1350 | * __mul "*" |
| 1351 | * __div "/" |
| 1352 | * __unm "!" |
| 1353 | * __pow "^" |
| 1354 | * __concat ".." |
| 1355 | * |
| 1356 | * Special methods for redfining standar relations |
| 1357 | * http://www.lua.org/pil/13.2.html |
| 1358 | * |
| 1359 | * __eq "==" |
| 1360 | * __lt "<" |
| 1361 | * __le "<=" |
| 1362 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1363 | |
| 1364 | /* |
| 1365 | * |
| 1366 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1367 | * Class Map |
| 1368 | * |
| 1369 | * |
| 1370 | */ |
| 1371 | |
| 1372 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1373 | * a class session, otherwise it throws an error. |
| 1374 | */ |
| 1375 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1376 | { |
| 1377 | return (struct map_descriptor *)MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
| 1378 | } |
| 1379 | |
| 1380 | /* This function is the map constructor. It don't need |
| 1381 | * the class Map object. It creates and return a new Map |
| 1382 | * object. It must be called only during "body" or "init" |
| 1383 | * context because it process some filesystem accesses. |
| 1384 | */ |
| 1385 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1386 | { |
| 1387 | const char *fn; |
| 1388 | int match = PAT_MATCH_STR; |
| 1389 | struct sample_conv conv; |
| 1390 | const char *file = ""; |
| 1391 | int line = 0; |
| 1392 | lua_Debug ar; |
| 1393 | char *err = NULL; |
| 1394 | struct arg args[2]; |
| 1395 | |
| 1396 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1397 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1398 | |
| 1399 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1400 | |
| 1401 | if (lua_gettop(L) >= 2) { |
| 1402 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1403 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1404 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1405 | } |
| 1406 | |
| 1407 | /* Get Lua filename and line number. */ |
| 1408 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1409 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1410 | if (ar.currentline > 0) { /* is there info? */ |
| 1411 | file = ar.short_src; |
| 1412 | line = ar.currentline; |
| 1413 | } |
| 1414 | } |
| 1415 | |
| 1416 | /* fill fake sample_conv struct. */ |
| 1417 | conv.kw = ""; /* unused. */ |
| 1418 | conv.process = NULL; /* unused. */ |
| 1419 | conv.arg_mask = 0; /* unused. */ |
| 1420 | conv.val_args = NULL; /* unused. */ |
| 1421 | conv.out_type = SMP_T_STR; |
| 1422 | conv.private = (void *)(long)match; |
| 1423 | switch (match) { |
| 1424 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1425 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1426 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1427 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1428 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1429 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1430 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1431 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1432 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1433 | default: |
| 1434 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1435 | } |
| 1436 | |
| 1437 | /* fill fake args. */ |
| 1438 | args[0].type = ARGT_STR; |
| 1439 | args[0].data.str.str = (char *)fn; |
| 1440 | args[1].type = ARGT_STOP; |
| 1441 | |
| 1442 | /* load the map. */ |
| 1443 | if (!sample_load_map(args, &conv, file, line, &err)) { |
| 1444 | /* error case: we cant use luaL_error because we must |
| 1445 | * free the err variable. |
| 1446 | */ |
| 1447 | luaL_where(L, 1); |
| 1448 | lua_pushfstring(L, "'new': %s.", err); |
| 1449 | lua_concat(L, 2); |
| 1450 | free(err); |
| 1451 | WILL_LJMP(lua_error(L)); |
| 1452 | } |
| 1453 | |
| 1454 | /* create the lua object. */ |
| 1455 | lua_newtable(L); |
| 1456 | lua_pushlightuserdata(L, args[0].data.map); |
| 1457 | lua_rawseti(L, -2, 0); |
| 1458 | |
| 1459 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1460 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1461 | lua_setmetatable(L, -2); |
| 1462 | |
| 1463 | |
| 1464 | return 1; |
| 1465 | } |
| 1466 | |
| 1467 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1468 | { |
| 1469 | struct map_descriptor *desc; |
| 1470 | struct pattern *pat; |
| 1471 | struct sample smp; |
| 1472 | |
| 1473 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1474 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1475 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1476 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1477 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1478 | } |
| 1479 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1480 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1481 | smp.flags = SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1482 | 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] | 1483 | } |
| 1484 | |
| 1485 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1486 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1487 | if (str) |
| 1488 | lua_pushstring(L, ""); |
| 1489 | else |
| 1490 | lua_pushnil(L); |
| 1491 | return 1; |
| 1492 | } |
| 1493 | |
| 1494 | /* 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] | 1495 | 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] | 1496 | return 1; |
| 1497 | } |
| 1498 | |
| 1499 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1500 | { |
| 1501 | return _hlua_map_lookup(L, 0); |
| 1502 | } |
| 1503 | |
| 1504 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1505 | { |
| 1506 | return _hlua_map_lookup(L, 1); |
| 1507 | } |
| 1508 | |
| 1509 | /* |
| 1510 | * |
| 1511 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1512 | * Class Socket |
| 1513 | * |
| 1514 | * |
| 1515 | */ |
| 1516 | |
| 1517 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1518 | { |
| 1519 | return (struct hlua_socket *)MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
| 1520 | } |
| 1521 | |
| 1522 | /* This function is the handler called for each I/O on the established |
| 1523 | * connection. It is used for notify space avalaible to send or data |
| 1524 | * received. |
| 1525 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1526 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1527 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1528 | struct stream_interface *si = appctx->owner; |
Willy Tarreau | 50fe03b | 2014-11-28 13:59:31 +0100 | [diff] [blame] | 1529 | struct connection *c = objt_conn(si_opposite(si)->end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1530 | |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1531 | /* If the connection object is not avalaible, close all the |
| 1532 | * streams and wakeup everithing waiting for. |
| 1533 | */ |
| 1534 | if (!c) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1535 | si_shutw(si); |
| 1536 | si_shutr(si); |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 1537 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1538 | hlua_com_wake(&appctx->ctx.hlua.wake_on_read); |
| 1539 | hlua_com_wake(&appctx->ctx.hlua.wake_on_write); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1540 | return; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1541 | } |
| 1542 | |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1543 | /* If we cant write, wakeup the pending write signals. */ |
| 1544 | if (channel_output_closed(si_ic(si))) |
| 1545 | hlua_com_wake(&appctx->ctx.hlua.wake_on_write); |
| 1546 | |
| 1547 | /* If we cant read, wakeup the pending read signals. */ |
| 1548 | if (channel_input_closed(si_oc(si))) |
| 1549 | hlua_com_wake(&appctx->ctx.hlua.wake_on_read); |
| 1550 | |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1551 | /* if the connection is not estabkished, inform the stream that we want |
| 1552 | * to be notified whenever the connection completes. |
| 1553 | */ |
| 1554 | if (!(c->flags & CO_FL_CONNECTED)) { |
| 1555 | si_applet_cant_get(si); |
| 1556 | si_applet_cant_put(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1557 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1558 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1559 | |
| 1560 | /* This function is called after the connect. */ |
| 1561 | appctx->ctx.hlua.connected = 1; |
| 1562 | |
| 1563 | /* 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] | 1564 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1565 | hlua_com_wake(&appctx->ctx.hlua.wake_on_write); |
| 1566 | |
| 1567 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1568 | if (!channel_is_empty(si_oc(si))) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1569 | hlua_com_wake(&appctx->ctx.hlua.wake_on_read); |
| 1570 | } |
| 1571 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1572 | /* This function is called when the "struct stream" is destroyed. |
| 1573 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1574 | * Wake all the pending signals. |
| 1575 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1576 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1577 | { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1578 | /* Remove my link in the original object. */ |
| 1579 | if (appctx->ctx.hlua.socket) |
| 1580 | appctx->ctx.hlua.socket->s = NULL; |
| 1581 | |
| 1582 | /* Wake all the task waiting for me. */ |
| 1583 | hlua_com_wake(&appctx->ctx.hlua.wake_on_read); |
| 1584 | hlua_com_wake(&appctx->ctx.hlua.wake_on_write); |
| 1585 | } |
| 1586 | |
| 1587 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1588 | * uses this object. If the stream does not exists, just quit. |
| 1589 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1590 | * pending signal can rest in the read and write lists. destroy |
| 1591 | * it. |
| 1592 | */ |
| 1593 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1594 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1595 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1596 | struct appctx *appctx; |
| 1597 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1598 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1599 | |
| 1600 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1601 | if (!socket->s) |
| 1602 | return 0; |
| 1603 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1604 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1605 | appctx = objt_appctx(socket->s->si[0].end); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1606 | stream_shutdown(socket->s, SF_ERR_KILLED); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1607 | socket->s = NULL; |
| 1608 | appctx->ctx.hlua.socket = NULL; |
| 1609 | |
| 1610 | return 0; |
| 1611 | } |
| 1612 | |
| 1613 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1614 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1615 | */ |
| 1616 | __LJMP static int hlua_socket_close(lua_State *L) |
| 1617 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1618 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1619 | struct appctx *appctx; |
| 1620 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1621 | MAY_LJMP(check_args(L, 1, "close")); |
| 1622 | |
| 1623 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1624 | if (!socket->s) |
| 1625 | return 0; |
| 1626 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1627 | /* Close the stream and remove the associated stop task. */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1628 | stream_shutdown(socket->s, SF_ERR_KILLED); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1629 | appctx = objt_appctx(socket->s->si[0].end); |
| 1630 | appctx->ctx.hlua.socket = NULL; |
| 1631 | socket->s = NULL; |
| 1632 | |
| 1633 | return 0; |
| 1634 | } |
| 1635 | |
| 1636 | /* This Lua function assumes that the stack contain three parameters. |
| 1637 | * 1 - USERDATA containing a struct socket |
| 1638 | * 2 - INTEGER with values of the macro defined below |
| 1639 | * If the integer is -1, we must read at most one line. |
| 1640 | * If the integer is -2, we ust read all the data until the |
| 1641 | * end of the stream. |
| 1642 | * If the integer is positive value, we must read a number of |
| 1643 | * bytes corresponding to this value. |
| 1644 | */ |
| 1645 | #define HLSR_READ_LINE (-1) |
| 1646 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1647 | __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] | 1648 | { |
| 1649 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1650 | int wanted = lua_tointeger(L, 2); |
| 1651 | struct hlua *hlua = hlua_gethlua(L); |
| 1652 | struct appctx *appctx; |
| 1653 | int len; |
| 1654 | int nblk; |
| 1655 | char *blk1; |
| 1656 | int len1; |
| 1657 | char *blk2; |
| 1658 | int len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1659 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1660 | struct channel *oc; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1661 | |
| 1662 | /* Check if this lua stack is schedulable. */ |
| 1663 | if (!hlua || !hlua->task) |
| 1664 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 1665 | "'frontend', 'backend' or 'task'")); |
| 1666 | |
| 1667 | /* check for connection closed. If some data where read, return it. */ |
| 1668 | if (!socket->s) |
| 1669 | goto connection_closed; |
| 1670 | |
Willy Tarreau | 94aa617 | 2015-03-13 14:19:06 +0100 | [diff] [blame] | 1671 | oc = &socket->s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1672 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1673 | /* Read line. */ |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1674 | nblk = bo_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1675 | if (nblk < 0) /* Connection close. */ |
| 1676 | goto connection_closed; |
| 1677 | if (nblk == 0) /* No data avalaible. */ |
| 1678 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1679 | |
| 1680 | /* remove final \r\n. */ |
| 1681 | if (nblk == 1) { |
| 1682 | if (blk1[len1-1] == '\n') { |
| 1683 | len1--; |
| 1684 | skip_at_end++; |
| 1685 | if (blk1[len1-1] == '\r') { |
| 1686 | len1--; |
| 1687 | skip_at_end++; |
| 1688 | } |
| 1689 | } |
| 1690 | } |
| 1691 | else { |
| 1692 | if (blk2[len2-1] == '\n') { |
| 1693 | len2--; |
| 1694 | skip_at_end++; |
| 1695 | if (blk2[len2-1] == '\r') { |
| 1696 | len2--; |
| 1697 | skip_at_end++; |
| 1698 | } |
| 1699 | } |
| 1700 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1701 | } |
| 1702 | |
| 1703 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1704 | /* Read all the available data. */ |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1705 | nblk = bo_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1706 | if (nblk < 0) /* Connection close. */ |
| 1707 | goto connection_closed; |
| 1708 | if (nblk == 0) /* No data avalaible. */ |
| 1709 | goto connection_empty; |
| 1710 | } |
| 1711 | |
| 1712 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1713 | /* Read a block of data. */ |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1714 | nblk = bo_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1715 | if (nblk < 0) /* Connection close. */ |
| 1716 | goto connection_closed; |
| 1717 | if (nblk == 0) /* No data avalaible. */ |
| 1718 | goto connection_empty; |
| 1719 | |
| 1720 | if (len1 > wanted) { |
| 1721 | nblk = 1; |
| 1722 | len1 = wanted; |
| 1723 | } if (nblk == 2 && len1 + len2 > wanted) |
| 1724 | len2 = wanted - len1; |
| 1725 | } |
| 1726 | |
| 1727 | len = len1; |
| 1728 | |
| 1729 | luaL_addlstring(&socket->b, blk1, len1); |
| 1730 | if (nblk == 2) { |
| 1731 | len += len2; |
| 1732 | luaL_addlstring(&socket->b, blk2, len2); |
| 1733 | } |
| 1734 | |
| 1735 | /* Consume data. */ |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1736 | bo_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1737 | |
| 1738 | /* Don't wait anything. */ |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 1739 | stream_int_notify(&socket->s->si[0]); |
| 1740 | stream_int_update_applet(&socket->s->si[0]); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1741 | |
| 1742 | /* If the pattern reclaim to read all the data |
| 1743 | * in the connection, got out. |
| 1744 | */ |
| 1745 | if (wanted == HLSR_READ_ALL) |
| 1746 | goto connection_empty; |
| 1747 | else if (wanted >= 0 && len < wanted) |
| 1748 | goto connection_empty; |
| 1749 | |
| 1750 | /* Return result. */ |
| 1751 | luaL_pushresult(&socket->b); |
| 1752 | return 1; |
| 1753 | |
| 1754 | connection_closed: |
| 1755 | |
| 1756 | /* If the buffer containds data. */ |
| 1757 | if (socket->b.n > 0) { |
| 1758 | luaL_pushresult(&socket->b); |
| 1759 | return 1; |
| 1760 | } |
| 1761 | lua_pushnil(L); |
| 1762 | lua_pushstring(L, "connection closed."); |
| 1763 | return 2; |
| 1764 | |
| 1765 | connection_empty: |
| 1766 | |
| 1767 | appctx = objt_appctx(socket->s->si[0].end); |
| 1768 | if (!hlua_com_new(hlua, &appctx->ctx.hlua.wake_on_read)) |
| 1769 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 1770 | 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] | 1771 | return 0; |
| 1772 | } |
| 1773 | |
| 1774 | /* This Lus function gets two parameters. The first one can be string |
| 1775 | * or a number. If the string is "*l", the user require one line. If |
| 1776 | * the string is "*a", the user require all the content of the stream. |
| 1777 | * If the value is a number, the user require a number of bytes equal |
| 1778 | * to the value. The default value is "*l" (a line). |
| 1779 | * |
| 1780 | * This paraeter with a variable type is converted in integer. This |
| 1781 | * integer takes this values: |
| 1782 | * -1 : read a line |
| 1783 | * -2 : read all the stream |
| 1784 | * >0 : amount if bytes. |
| 1785 | * |
| 1786 | * The second parameter is optinal. It contains a string that must be |
| 1787 | * concatenated with the read data. |
| 1788 | */ |
| 1789 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 1790 | { |
| 1791 | int wanted = HLSR_READ_LINE; |
| 1792 | const char *pattern; |
| 1793 | int type; |
| 1794 | char *error; |
| 1795 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1796 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1797 | |
| 1798 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 1799 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 1800 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1801 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1802 | |
| 1803 | /* check for pattern. */ |
| 1804 | if (lua_gettop(L) >= 2) { |
| 1805 | type = lua_type(L, 2); |
| 1806 | if (type == LUA_TSTRING) { |
| 1807 | pattern = lua_tostring(L, 2); |
| 1808 | if (strcmp(pattern, "*a") == 0) |
| 1809 | wanted = HLSR_READ_ALL; |
| 1810 | else if (strcmp(pattern, "*l") == 0) |
| 1811 | wanted = HLSR_READ_LINE; |
| 1812 | else { |
| 1813 | wanted = strtoll(pattern, &error, 10); |
| 1814 | if (*error != '\0') |
| 1815 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 1816 | } |
| 1817 | } |
| 1818 | else if (type == LUA_TNUMBER) { |
| 1819 | wanted = lua_tointeger(L, 2); |
| 1820 | if (wanted < 0) |
| 1821 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 1822 | } |
| 1823 | } |
| 1824 | |
| 1825 | /* Set pattern. */ |
| 1826 | lua_pushinteger(L, wanted); |
| 1827 | lua_replace(L, 2); |
| 1828 | |
| 1829 | /* init bufffer, and fiil it wih prefix. */ |
| 1830 | luaL_buffinit(L, &socket->b); |
| 1831 | |
| 1832 | /* Check prefix. */ |
| 1833 | if (lua_gettop(L) >= 3) { |
| 1834 | if (lua_type(L, 3) != LUA_TSTRING) |
| 1835 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 1836 | pattern = lua_tolstring(L, 3, &len); |
| 1837 | luaL_addlstring(&socket->b, pattern, len); |
| 1838 | } |
| 1839 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1840 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1841 | } |
| 1842 | |
| 1843 | /* Write the Lua input string in the output buffer. |
| 1844 | * This fucntion returns a yield if no space are available. |
| 1845 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1846 | 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] | 1847 | { |
| 1848 | struct hlua_socket *socket; |
| 1849 | struct hlua *hlua = hlua_gethlua(L); |
| 1850 | struct appctx *appctx; |
| 1851 | size_t buf_len; |
| 1852 | const char *buf; |
| 1853 | int len; |
| 1854 | int send_len; |
| 1855 | int sent; |
| 1856 | |
| 1857 | /* Check if this lua stack is schedulable. */ |
| 1858 | if (!hlua || !hlua->task) |
| 1859 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 1860 | "'frontend', 'backend' or 'task'")); |
| 1861 | |
| 1862 | /* Get object */ |
| 1863 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1864 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1865 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1866 | |
| 1867 | /* Check for connection close. */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1868 | if (!socket->s || channel_output_closed(&socket->s->req)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1869 | lua_pushinteger(L, -1); |
| 1870 | return 1; |
| 1871 | } |
| 1872 | |
| 1873 | /* Update the input buffer data. */ |
| 1874 | buf += sent; |
| 1875 | send_len = buf_len - sent; |
| 1876 | |
| 1877 | /* All the data are sent. */ |
| 1878 | if (sent >= buf_len) |
| 1879 | return 1; /* Implicitly return the length sent. */ |
| 1880 | |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 1881 | /* Check if the buffer is avalaible because HAProxy doesn't allocate |
| 1882 | * the request buffer if its not required. |
| 1883 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1884 | if (socket->s->req.buf->size == 0) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1885 | if (!stream_alloc_recv_buffer(&socket->s->req)) { |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 1886 | socket->s->si[0].flags |= SI_FL_WAIT_ROOM; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 1887 | goto hlua_socket_write_yield_return; |
| 1888 | } |
| 1889 | } |
| 1890 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1891 | /* Check for avalaible space. */ |
Willy Tarreau | 94aa617 | 2015-03-13 14:19:06 +0100 | [diff] [blame] | 1892 | len = buffer_total_space(socket->s->req.buf); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1893 | if (len <= 0) |
| 1894 | goto hlua_socket_write_yield_return; |
| 1895 | |
| 1896 | /* send data */ |
| 1897 | if (len < send_len) |
| 1898 | send_len = len; |
Willy Tarreau | 94aa617 | 2015-03-13 14:19:06 +0100 | [diff] [blame] | 1899 | len = bi_putblk(&socket->s->req, buf+sent, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1900 | |
| 1901 | /* "Not enough space" (-1), "Buffer too little to contain |
| 1902 | * the data" (-2) are not expected because the available length |
| 1903 | * is tested. |
| 1904 | * Other unknown error are also not expected. |
| 1905 | */ |
| 1906 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 1907 | if (len == -1) |
Willy Tarreau | 94aa617 | 2015-03-13 14:19:06 +0100 | [diff] [blame] | 1908 | socket->s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 1909 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1910 | MAY_LJMP(hlua_socket_close(L)); |
| 1911 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1912 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1913 | return 1; |
| 1914 | } |
| 1915 | |
| 1916 | /* update buffers. */ |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 1917 | stream_int_notify(&socket->s->si[0]); |
| 1918 | stream_int_update_applet(&socket->s->si[0]); |
| 1919 | |
Willy Tarreau | 94aa617 | 2015-03-13 14:19:06 +0100 | [diff] [blame] | 1920 | socket->s->req.rex = TICK_ETERNITY; |
| 1921 | socket->s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1922 | |
| 1923 | /* Update length sent. */ |
| 1924 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1925 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1926 | |
| 1927 | /* All the data buffer is sent ? */ |
| 1928 | if (sent + len >= buf_len) |
| 1929 | return 1; |
| 1930 | |
| 1931 | hlua_socket_write_yield_return: |
| 1932 | appctx = objt_appctx(socket->s->si[0].end); |
| 1933 | if (!hlua_com_new(hlua, &appctx->ctx.hlua.wake_on_write)) |
| 1934 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 1935 | 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] | 1936 | return 0; |
| 1937 | } |
| 1938 | |
| 1939 | /* This function initiate the send of data. It just check the input |
| 1940 | * parameters and push an integer in the Lua stack that contain the |
| 1941 | * amount of data writed in the buffer. This is used by the function |
| 1942 | * "hlua_socket_write_yield" that can yield. |
| 1943 | * |
| 1944 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 1945 | * the associated object. The second is a string buffer. The third is |
| 1946 | * a facultative integer that represents where is the buffer position |
| 1947 | * of the start of the data that can send. The first byte is the |
| 1948 | * position "1". The default value is "1". The fourth argument is a |
| 1949 | * facultative integer that represents where is the buffer position |
| 1950 | * of the end of the data that can send. The default is the last byte. |
| 1951 | */ |
| 1952 | static int hlua_socket_send(struct lua_State *L) |
| 1953 | { |
| 1954 | int i; |
| 1955 | int j; |
| 1956 | const char *buf; |
| 1957 | size_t buf_len; |
| 1958 | |
| 1959 | /* Check number of arguments. */ |
| 1960 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 1961 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 1962 | |
| 1963 | /* Get the string. */ |
| 1964 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 1965 | |
| 1966 | /* Get and check j. */ |
| 1967 | if (lua_gettop(L) == 4) { |
| 1968 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 1969 | if (j < 0) |
| 1970 | j = buf_len + j + 1; |
| 1971 | if (j > buf_len) |
| 1972 | j = buf_len + 1; |
| 1973 | lua_pop(L, 1); |
| 1974 | } |
| 1975 | else |
| 1976 | j = buf_len; |
| 1977 | |
| 1978 | /* Get and check i. */ |
| 1979 | if (lua_gettop(L) == 3) { |
| 1980 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 1981 | if (i < 0) |
| 1982 | i = buf_len + i + 1; |
| 1983 | if (i > buf_len) |
| 1984 | i = buf_len + 1; |
| 1985 | lua_pop(L, 1); |
| 1986 | } else |
| 1987 | i = 1; |
| 1988 | |
| 1989 | /* Check bth i and j. */ |
| 1990 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1991 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1992 | return 1; |
| 1993 | } |
| 1994 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1995 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1996 | return 1; |
| 1997 | } |
| 1998 | if (i == 0) |
| 1999 | i = 1; |
| 2000 | if (j == 0) |
| 2001 | j = 1; |
| 2002 | |
| 2003 | /* Pop the string. */ |
| 2004 | lua_pop(L, 1); |
| 2005 | |
| 2006 | /* Update the buffer length. */ |
| 2007 | buf += i - 1; |
| 2008 | buf_len = j - i + 1; |
| 2009 | lua_pushlstring(L, buf, buf_len); |
| 2010 | |
| 2011 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2012 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2013 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2014 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2015 | } |
| 2016 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2017 | #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] | 2018 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2019 | { |
| 2020 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2021 | int ret; |
| 2022 | int len; |
| 2023 | char *p; |
| 2024 | |
| 2025 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2026 | if (ret <= 0) { |
| 2027 | lua_pushnil(L); |
| 2028 | return 1; |
| 2029 | } |
| 2030 | |
| 2031 | if (ret == AF_UNIX) { |
| 2032 | lua_pushstring(L, buffer+1); |
| 2033 | return 1; |
| 2034 | } |
| 2035 | else if (ret == AF_INET6) { |
| 2036 | buffer[0] = '['; |
| 2037 | len = strlen(buffer); |
| 2038 | buffer[len] = ']'; |
| 2039 | len++; |
| 2040 | buffer[len] = ':'; |
| 2041 | len++; |
| 2042 | p = buffer; |
| 2043 | } |
| 2044 | else if (ret == AF_INET) { |
| 2045 | p = buffer + 1; |
| 2046 | len = strlen(p); |
| 2047 | p[len] = ':'; |
| 2048 | len++; |
| 2049 | } |
| 2050 | else { |
| 2051 | lua_pushnil(L); |
| 2052 | return 1; |
| 2053 | } |
| 2054 | |
| 2055 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2056 | lua_pushnil(L); |
| 2057 | return 1; |
| 2058 | } |
| 2059 | |
| 2060 | lua_pushstring(L, p); |
| 2061 | return 1; |
| 2062 | } |
| 2063 | |
| 2064 | /* Returns information about the peer of the connection. */ |
| 2065 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2066 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2067 | struct hlua_socket *socket; |
| 2068 | struct connection *conn; |
| 2069 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2070 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2071 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2072 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2073 | |
| 2074 | /* Check if the tcp object is avalaible. */ |
| 2075 | if (!socket->s) { |
| 2076 | lua_pushnil(L); |
| 2077 | return 1; |
| 2078 | } |
| 2079 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2080 | conn = objt_conn(socket->s->si[1].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2081 | if (!conn) { |
| 2082 | lua_pushnil(L); |
| 2083 | return 1; |
| 2084 | } |
| 2085 | |
| 2086 | if (!(conn->flags & CO_FL_ADDR_TO_SET)) { |
| 2087 | unsigned int salen = sizeof(conn->addr.to); |
| 2088 | if (getpeername(conn->t.sock.fd, (struct sockaddr *)&conn->addr.to, &salen) == -1) { |
| 2089 | lua_pushnil(L); |
| 2090 | return 1; |
| 2091 | } |
| 2092 | conn->flags |= CO_FL_ADDR_TO_SET; |
| 2093 | } |
| 2094 | |
| 2095 | return MAY_LJMP(hlua_socket_info(L, &conn->addr.to)); |
| 2096 | } |
| 2097 | |
| 2098 | /* Returns information about my connection side. */ |
| 2099 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2100 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2101 | struct hlua_socket *socket; |
| 2102 | struct connection *conn; |
| 2103 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2104 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2105 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2106 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2107 | |
| 2108 | /* Check if the tcp object is avalaible. */ |
| 2109 | if (!socket->s) { |
| 2110 | lua_pushnil(L); |
| 2111 | return 1; |
| 2112 | } |
| 2113 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2114 | conn = objt_conn(socket->s->si[1].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2115 | if (!conn) { |
| 2116 | lua_pushnil(L); |
| 2117 | return 1; |
| 2118 | } |
| 2119 | |
| 2120 | if (!(conn->flags & CO_FL_ADDR_FROM_SET)) { |
| 2121 | unsigned int salen = sizeof(conn->addr.from); |
| 2122 | if (getsockname(conn->t.sock.fd, (struct sockaddr *)&conn->addr.from, &salen) == -1) { |
| 2123 | lua_pushnil(L); |
| 2124 | return 1; |
| 2125 | } |
| 2126 | conn->flags |= CO_FL_ADDR_FROM_SET; |
| 2127 | } |
| 2128 | |
| 2129 | return hlua_socket_info(L, &conn->addr.from); |
| 2130 | } |
| 2131 | |
| 2132 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2133 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2134 | .obj_type = OBJ_TYPE_APPLET, |
| 2135 | .name = "<LUA_TCP>", |
| 2136 | .fct = hlua_socket_handler, |
| 2137 | .release = hlua_socket_release, |
| 2138 | }; |
| 2139 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2140 | __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] | 2141 | { |
| 2142 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2143 | struct hlua *hlua = hlua_gethlua(L); |
| 2144 | struct appctx *appctx; |
| 2145 | |
| 2146 | /* Check for connection close. */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2147 | if (!hlua || !socket->s || channel_output_closed(&socket->s->req)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2148 | lua_pushnil(L); |
| 2149 | lua_pushstring(L, "Can't connect"); |
| 2150 | return 2; |
| 2151 | } |
| 2152 | |
| 2153 | appctx = objt_appctx(socket->s->si[0].end); |
| 2154 | |
| 2155 | /* Check for connection established. */ |
| 2156 | if (appctx->ctx.hlua.connected) { |
| 2157 | lua_pushinteger(L, 1); |
| 2158 | return 1; |
| 2159 | } |
| 2160 | |
| 2161 | if (!hlua_com_new(hlua, &appctx->ctx.hlua.wake_on_write)) |
| 2162 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 2163 | 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] | 2164 | return 0; |
| 2165 | } |
| 2166 | |
| 2167 | /* This function fail or initite the connection. */ |
| 2168 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2169 | { |
| 2170 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2171 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2172 | const char *ip; |
| 2173 | struct connection *conn; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2174 | struct hlua *hlua; |
| 2175 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2176 | int low, high; |
| 2177 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2178 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2179 | if (lua_gettop(L) < 2) |
| 2180 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2181 | |
| 2182 | /* Get args. */ |
| 2183 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2184 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2185 | if (lua_gettop(L) >= 3) |
| 2186 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2187 | |
Willy Tarreau | 973a542 | 2015-08-05 21:47:23 +0200 | [diff] [blame] | 2188 | conn = si_alloc_conn(&socket->s->si[1]); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2189 | if (!conn) |
| 2190 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
| 2191 | |
Willy Tarreau | 3adac08 | 2015-09-26 17:51:09 +0200 | [diff] [blame] | 2192 | /* needed for the connection not to be closed */ |
| 2193 | conn->target = socket->s->target; |
| 2194 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2195 | /* Parse ip address. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2196 | addr = str2sa_range(ip, &low, &high, NULL, NULL, NULL, 0); |
| 2197 | if (!addr) |
| 2198 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
| 2199 | if (low != high) |
| 2200 | WILL_LJMP(luaL_error(L, "connect: port ranges not supported : address '%s'", ip)); |
| 2201 | memcpy(&conn->addr.to, addr, sizeof(struct sockaddr_storage)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2202 | |
| 2203 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2204 | if (low == 0) { |
| 2205 | if (conn->addr.to.ss_family == AF_INET) { |
| 2206 | if (port == -1) |
| 2207 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
| 2208 | ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(port); |
| 2209 | } else if (conn->addr.to.ss_family == AF_INET6) { |
| 2210 | if (port == -1) |
| 2211 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
| 2212 | ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(port); |
| 2213 | } |
| 2214 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2215 | |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2216 | hlua = hlua_gethlua(L); |
| 2217 | appctx = objt_appctx(socket->s->si[0].end); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2218 | |
| 2219 | /* inform the stream that we want to be notified whenever the |
| 2220 | * connection completes. |
| 2221 | */ |
| 2222 | si_applet_cant_get(&socket->s->si[0]); |
| 2223 | si_applet_cant_put(&socket->s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2224 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2225 | |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2226 | hlua->flags |= HLUA_MUST_GC; |
| 2227 | |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2228 | if (!hlua_com_new(hlua, &appctx->ctx.hlua.wake_on_write)) |
| 2229 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 2230 | 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] | 2231 | |
| 2232 | return 0; |
| 2233 | } |
| 2234 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2235 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2236 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2237 | { |
| 2238 | struct hlua_socket *socket; |
| 2239 | |
| 2240 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2241 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2242 | socket->s->target = &socket_ssl.obj_type; |
| 2243 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2244 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2245 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2246 | |
| 2247 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2248 | { |
| 2249 | return 0; |
| 2250 | } |
| 2251 | |
| 2252 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2253 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2254 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2255 | int tmout; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2256 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2257 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2258 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2259 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2260 | tmout = MAY_LJMP(luaL_checkinteger(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2261 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2262 | socket->s->req.rto = tmout; |
| 2263 | socket->s->req.wto = tmout; |
| 2264 | socket->s->res.rto = tmout; |
| 2265 | socket->s->res.wto = tmout; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2266 | |
| 2267 | return 0; |
| 2268 | } |
| 2269 | |
| 2270 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2271 | { |
| 2272 | struct hlua_socket *socket; |
| 2273 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2274 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2275 | struct stream *strm; |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2276 | struct task *task; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2277 | |
| 2278 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2279 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2280 | hlua_pusherror(L, "socket: full stack"); |
| 2281 | goto out_fail_conf; |
| 2282 | } |
| 2283 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2284 | /* Create the object: obj[0] = userdata. */ |
| 2285 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2286 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2287 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2288 | memset(socket, 0, sizeof(*socket)); |
| 2289 | |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2290 | /* Check if the various memory pools are intialized. */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2291 | if (!pool2_stream || !pool2_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2292 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2293 | goto out_fail_conf; |
| 2294 | } |
| 2295 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2296 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2297 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2298 | lua_setmetatable(L, -2); |
| 2299 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2300 | /* Create the applet context */ |
| 2301 | appctx = appctx_new(&update_applet); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2302 | if (!appctx) { |
| 2303 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2304 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2305 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2306 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2307 | appctx->ctx.hlua.socket = socket; |
| 2308 | appctx->ctx.hlua.connected = 0; |
| 2309 | LIST_INIT(&appctx->ctx.hlua.wake_on_write); |
| 2310 | LIST_INIT(&appctx->ctx.hlua.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2311 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2312 | /* Now create a session, task and stream for this applet */ |
| 2313 | sess = session_new(&socket_proxy, NULL, &appctx->obj_type); |
| 2314 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2315 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2316 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2317 | } |
| 2318 | |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2319 | task = task_new(); |
| 2320 | if (!task) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2321 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2322 | goto out_fail_task; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2323 | } |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2324 | task->nice = 0; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2325 | |
Willy Tarreau | 73b65ac | 2015-04-08 18:26:29 +0200 | [diff] [blame] | 2326 | strm = stream_new(sess, task, &appctx->obj_type); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2327 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2328 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2329 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2330 | } |
| 2331 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2332 | /* Configure an empty Lua for the stream. */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2333 | socket->s = strm; |
| 2334 | strm->hlua.T = NULL; |
| 2335 | strm->hlua.Tref = LUA_REFNIL; |
| 2336 | strm->hlua.Mref = LUA_REFNIL; |
| 2337 | strm->hlua.nargs = 0; |
| 2338 | strm->hlua.flags = 0; |
| 2339 | LIST_INIT(&strm->hlua.com); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2340 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2341 | /* Configure "right" stream interface. this "si" is used to connect |
| 2342 | * and retrieve data from the server. The connection is initialized |
| 2343 | * with the "struct server". |
| 2344 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2345 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2346 | |
| 2347 | /* Force destination server. */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2348 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET | SF_BE_ASSIGNED; |
| 2349 | strm->target = &socket_tcp.obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2350 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2351 | /* Update statistics counters. */ |
| 2352 | socket_proxy.feconn++; /* beconn will be increased later */ |
| 2353 | jobs++; |
| 2354 | totalconn++; |
| 2355 | |
| 2356 | /* Return yield waiting for connection. */ |
| 2357 | return 1; |
| 2358 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2359 | out_fail_stream: |
| 2360 | task_free(task); |
| 2361 | out_fail_task: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2362 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2363 | out_fail_sess: |
| 2364 | appctx_free(appctx); |
| 2365 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2366 | WILL_LJMP(lua_error(L)); |
| 2367 | return 0; |
| 2368 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2369 | |
| 2370 | /* |
| 2371 | * |
| 2372 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2373 | * Class Channel |
| 2374 | * |
| 2375 | * |
| 2376 | */ |
| 2377 | |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2378 | /* The state between the channel data and the HTTP parser state can be |
| 2379 | * unconsistent, so reset the parser and call it again. Warning, this |
| 2380 | * action not revalidate the request and not send a 400 if the modified |
| 2381 | * resuest is not valid. |
| 2382 | * |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2383 | * This function never fails. The direction is set using dir, which equals |
| 2384 | * either SMP_OPT_DIR_REQ or SMP_OPT_DIR_RES. |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2385 | */ |
| 2386 | static void hlua_resynchonize_proto(struct stream *stream, int dir) |
| 2387 | { |
| 2388 | /* Protocol HTTP. */ |
| 2389 | if (stream->be->mode == PR_MODE_HTTP) { |
| 2390 | |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2391 | if (dir == SMP_OPT_DIR_REQ) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2392 | http_txn_reset_req(stream->txn); |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2393 | else if (dir == SMP_OPT_DIR_RES) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2394 | http_txn_reset_res(stream->txn); |
| 2395 | |
| 2396 | if (stream->txn->hdr_idx.v) |
| 2397 | hdr_idx_init(&stream->txn->hdr_idx); |
| 2398 | |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2399 | if (dir == SMP_OPT_DIR_REQ) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2400 | http_msg_analyzer(&stream->txn->req, &stream->txn->hdr_idx); |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2401 | else if (dir == SMP_OPT_DIR_RES) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2402 | http_msg_analyzer(&stream->txn->rsp, &stream->txn->hdr_idx); |
| 2403 | } |
| 2404 | } |
| 2405 | |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2406 | /* Check the protocole integrity after the Lua manipulations. Close the stream |
| 2407 | * and returns 0 if fails, otherwise returns 1. The direction is set using dir, |
| 2408 | * which equals either SMP_OPT_DIR_REQ or SMP_OPT_DIR_RES. |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2409 | */ |
| 2410 | static int hlua_check_proto(struct stream *stream, int dir) |
| 2411 | { |
| 2412 | const struct chunk msg = { .len = 0 }; |
| 2413 | |
Willy Tarreau | 9af89f7 | 2015-09-26 11:50:08 +0200 | [diff] [blame] | 2414 | /* Protocol HTTP. The message parsing state must match the request or |
| 2415 | * response state. The problem that may happen is that Lua modifies |
| 2416 | * the request or response message *after* it was parsed, and corrupted |
| 2417 | * it so that it could not be processed anymore. We just need to verify |
| 2418 | * if the parser is still expected to run or not. |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2419 | */ |
| 2420 | if (stream->be->mode == PR_MODE_HTTP) { |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2421 | if (dir == SMP_OPT_DIR_REQ && |
Willy Tarreau | 9af89f7 | 2015-09-26 11:50:08 +0200 | [diff] [blame] | 2422 | !(stream->req.analysers & AN_REQ_WAIT_HTTP) && |
| 2423 | stream->txn->req.msg_state < HTTP_MSG_BODY) { |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2424 | stream_int_retnclose(&stream->si[0], &msg); |
| 2425 | return 0; |
| 2426 | } |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2427 | else if (dir == SMP_OPT_DIR_RES && |
Willy Tarreau | 9af89f7 | 2015-09-26 11:50:08 +0200 | [diff] [blame] | 2428 | !(stream->res.analysers & AN_RES_WAIT_HTTP) && |
| 2429 | stream->txn->rsp.msg_state < HTTP_MSG_BODY) { |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2430 | stream_int_retnclose(&stream->si[0], &msg); |
| 2431 | return 0; |
| 2432 | } |
| 2433 | } |
| 2434 | return 1; |
| 2435 | } |
| 2436 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2437 | /* Returns the struct hlua_channel join to the class channel in the |
| 2438 | * stack entry "ud" or throws an argument error. |
| 2439 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2440 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2441 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2442 | return (struct channel *)MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2443 | } |
| 2444 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2445 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 2446 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2447 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 2448 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2449 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2450 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2451 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2452 | return 0; |
| 2453 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2454 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2455 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2456 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2457 | |
| 2458 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2459 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 2460 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2461 | return 1; |
| 2462 | } |
| 2463 | |
| 2464 | /* Duplicate all the data present in the input channel and put it |
| 2465 | * in a string LUA variables. Returns -1 and push a nil value in |
| 2466 | * the stack if the channel is closed and all the data are consumed, |
| 2467 | * returns 0 if no data are available, otherwise it returns the length |
| 2468 | * of the builded string. |
| 2469 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2470 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2471 | { |
| 2472 | char *blk1; |
| 2473 | char *blk2; |
| 2474 | int len1; |
| 2475 | int len2; |
| 2476 | int ret; |
| 2477 | luaL_Buffer b; |
| 2478 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2479 | ret = bi_getblk_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2480 | if (unlikely(ret == 0)) |
| 2481 | return 0; |
| 2482 | |
| 2483 | if (unlikely(ret < 0)) { |
| 2484 | lua_pushnil(L); |
| 2485 | return -1; |
| 2486 | } |
| 2487 | |
| 2488 | luaL_buffinit(L, &b); |
| 2489 | luaL_addlstring(&b, blk1, len1); |
| 2490 | if (unlikely(ret == 2)) |
| 2491 | luaL_addlstring(&b, blk2, len2); |
| 2492 | luaL_pushresult(&b); |
| 2493 | |
| 2494 | if (unlikely(ret == 2)) |
| 2495 | return len1 + len2; |
| 2496 | return len1; |
| 2497 | } |
| 2498 | |
| 2499 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2500 | * a yield. This function keep the data in the buffer. |
| 2501 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2502 | __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] | 2503 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2504 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2505 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2506 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2507 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2508 | if (_hlua_channel_dup(chn, L) == 0) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2509 | 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] | 2510 | return 1; |
| 2511 | } |
| 2512 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2513 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 2514 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 2515 | { |
| 2516 | MAY_LJMP(check_args(L, 1, "dup")); |
| 2517 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2518 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 2519 | } |
| 2520 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2521 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2522 | * a yield. This function consumes the data in the buffer. It returns |
| 2523 | * a string containing the data or a nil pointer if no data are available |
| 2524 | * and the channel is closed. |
| 2525 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2526 | __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] | 2527 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2528 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2529 | int ret; |
| 2530 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2531 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2532 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2533 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2534 | if (unlikely(ret == 0)) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2535 | 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] | 2536 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2537 | if (unlikely(ret == -1)) |
| 2538 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2539 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2540 | chn->buf->i -= ret; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2541 | hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2542 | return 1; |
| 2543 | } |
| 2544 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2545 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 2546 | __LJMP static int hlua_channel_get(lua_State *L) |
| 2547 | { |
| 2548 | MAY_LJMP(check_args(L, 1, "get")); |
| 2549 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2550 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 2551 | } |
| 2552 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2553 | /* This functions consumes and returns one line. If the channel is closed, |
| 2554 | * and the last data does not contains a final '\n', the data are returned |
| 2555 | * without the final '\n'. When no more data are avalaible, it returns nil |
| 2556 | * value. |
| 2557 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2558 | __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] | 2559 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2560 | char *blk1; |
| 2561 | char *blk2; |
| 2562 | int len1; |
| 2563 | int len2; |
| 2564 | int len; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2565 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2566 | int ret; |
| 2567 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2568 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2569 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2570 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2571 | ret = bi_getline_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2572 | if (ret == 0) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2573 | 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] | 2574 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2575 | if (ret == -1) { |
| 2576 | lua_pushnil(L); |
| 2577 | return 1; |
| 2578 | } |
| 2579 | |
| 2580 | luaL_buffinit(L, &b); |
| 2581 | luaL_addlstring(&b, blk1, len1); |
| 2582 | len = len1; |
| 2583 | if (unlikely(ret == 2)) { |
| 2584 | luaL_addlstring(&b, blk2, len2); |
| 2585 | len += len2; |
| 2586 | } |
| 2587 | luaL_pushresult(&b); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2588 | 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] | 2589 | hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2590 | return 1; |
| 2591 | } |
| 2592 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2593 | /* Check arguments for the fucntion "hlua_channel_getline_yield". */ |
| 2594 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 2595 | { |
| 2596 | MAY_LJMP(check_args(L, 1, "getline")); |
| 2597 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2598 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 2599 | } |
| 2600 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2601 | /* This function takes a string as input, and append it at the |
| 2602 | * input side of channel. If the data is too big, but a space |
| 2603 | * is probably available after sending some data, the function |
| 2604 | * yield. If the data is bigger than the buffer, or if the |
| 2605 | * channel is closed, it returns -1. otherwise, it returns the |
| 2606 | * amount of data writed. |
| 2607 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2608 | __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] | 2609 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2610 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2611 | size_t len; |
| 2612 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2613 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2614 | int ret; |
| 2615 | int max; |
| 2616 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2617 | max = channel_recv_limit(chn) - buffer_len(chn->buf); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2618 | if (max > len - l) |
| 2619 | max = len - l; |
| 2620 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2621 | ret = bi_putblk(chn, str + l, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2622 | if (ret == -2 || ret == -3) { |
| 2623 | lua_pushinteger(L, -1); |
| 2624 | return 1; |
| 2625 | } |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2626 | if (ret == -1) { |
| 2627 | chn->flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2628 | 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] | 2629 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2630 | l += ret; |
| 2631 | lua_pop(L, 1); |
| 2632 | lua_pushinteger(L, l); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2633 | hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2634 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2635 | max = channel_recv_limit(chn) - buffer_len(chn->buf); |
| 2636 | if (max == 0 && chn->buf->o == 0) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2637 | /* There are no space avalaible, and the output buffer is empty. |
| 2638 | * in this case, we cannot add more data, so we cannot yield, |
| 2639 | * we return the amount of copyied data. |
| 2640 | */ |
| 2641 | return 1; |
| 2642 | } |
| 2643 | if (l < len) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2644 | 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] | 2645 | return 1; |
| 2646 | } |
| 2647 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2648 | /* just a wrapper of "hlua_channel_append_yield". It returns the length |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2649 | * of the writed string, or -1 if the channel is closed or if the |
| 2650 | * buffer size is too little for the data. |
| 2651 | */ |
| 2652 | __LJMP static int hlua_channel_append(lua_State *L) |
| 2653 | { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2654 | size_t len; |
| 2655 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2656 | MAY_LJMP(check_args(L, 2, "append")); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2657 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2658 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2659 | MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2660 | lua_pushinteger(L, 0); |
| 2661 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2662 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2663 | } |
| 2664 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2665 | /* just a wrapper of "hlua_channel_append_yield". This wrapper starts |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2666 | * his process by cleaning the buffer. The result is a replacement |
| 2667 | * of the current data. It returns the length of the writed string, |
| 2668 | * or -1 if the channel is closed or if the buffer size is too |
| 2669 | * little for the data. |
| 2670 | */ |
| 2671 | __LJMP static int hlua_channel_set(lua_State *L) |
| 2672 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2673 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2674 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2675 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2676 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2677 | lua_pushinteger(L, 0); |
| 2678 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2679 | chn->buf->i = 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2680 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2681 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2682 | } |
| 2683 | |
| 2684 | /* Append data in the output side of the buffer. This data is immediatly |
| 2685 | * sent. The fcuntion returns the ammount of data writed. If the buffer |
| 2686 | * cannot contains the data, the function yield. The function returns -1 |
| 2687 | * if the channel is closed. |
| 2688 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2689 | __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] | 2690 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2691 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2692 | size_t len; |
| 2693 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2694 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2695 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2696 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2697 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2698 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2699 | lua_pushinteger(L, -1); |
| 2700 | return 1; |
| 2701 | } |
| 2702 | |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 2703 | /* Check if the buffer is avalaible because HAProxy doesn't allocate |
| 2704 | * the request buffer if its not required. |
| 2705 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2706 | if (chn->buf->size == 0) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2707 | if (!stream_alloc_recv_buffer(chn)) { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2708 | chn_prod(chn)->flags |= SI_FL_WAIT_ROOM; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2709 | 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] | 2710 | } |
| 2711 | } |
| 2712 | |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2713 | /* the writed data will be immediatly sent, so we can check |
| 2714 | * the avalaible space without taking in account the reserve. |
| 2715 | * The reserve is guaranted for the processing of incoming |
| 2716 | * data, because the buffer will be flushed. |
| 2717 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2718 | max = chn->buf->size - buffer_len(chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2719 | |
| 2720 | /* If there are no space avalaible, and the output buffer is empty. |
| 2721 | * in this case, we cannot add more data, so we cannot yield, |
| 2722 | * we return the amount of copyied data. |
| 2723 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2724 | if (max == 0 && chn->buf->o == 0) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2725 | return 1; |
| 2726 | |
| 2727 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2728 | if (max > len - l) |
| 2729 | max = len - l; |
| 2730 | |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2731 | /* The buffer avalaible size may be not contiguous. This test |
| 2732 | * detects a non contiguous buffer and realign it. |
| 2733 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2734 | if (bi_space_for_replace(chn->buf) < max) |
| 2735 | buffer_slow_realign(chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2736 | |
| 2737 | /* Copy input data in the buffer. */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2738 | 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] | 2739 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2740 | /* buffer replace considers that the input part is filled. |
| 2741 | * so, I must forward these new data in the output part. |
| 2742 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2743 | b_adv(chn->buf, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2744 | |
| 2745 | l += max; |
| 2746 | lua_pop(L, 1); |
| 2747 | lua_pushinteger(L, l); |
| 2748 | |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2749 | /* If there are no space avalaible, and the output buffer is empty. |
| 2750 | * in this case, we cannot add more data, so we cannot yield, |
| 2751 | * we return the amount of copyied data. |
| 2752 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2753 | max = chn->buf->size - buffer_len(chn->buf); |
| 2754 | if (max == 0 && chn->buf->o == 0) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2755 | return 1; |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2756 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2757 | if (l < len) { |
| 2758 | /* If we are waiting for space in the response buffer, we |
| 2759 | * must set the flag WAKERESWR. This flag required the task |
| 2760 | * wake up if any activity is detected on the response buffer. |
| 2761 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2762 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2763 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 2764 | else |
| 2765 | HLUA_SET_WAKEREQWR(hlua); |
| 2766 | 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] | 2767 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2768 | |
| 2769 | return 1; |
| 2770 | } |
| 2771 | |
| 2772 | /* Just a wraper of "_hlua_channel_send". This wrapper permits |
| 2773 | * yield the LUA process, and resume it without checking the |
| 2774 | * input arguments. |
| 2775 | */ |
| 2776 | __LJMP static int hlua_channel_send(lua_State *L) |
| 2777 | { |
| 2778 | MAY_LJMP(check_args(L, 2, "send")); |
| 2779 | lua_pushinteger(L, 0); |
| 2780 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2781 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2782 | } |
| 2783 | |
| 2784 | /* This function forward and amount of butes. The data pass from |
| 2785 | * the input side of the buffer to the output side, and can be |
| 2786 | * forwarded. This function never fails. |
| 2787 | * |
| 2788 | * The Lua function takes an amount of bytes to be forwarded in |
| 2789 | * imput. It returns the number of bytes forwarded. |
| 2790 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2791 | __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] | 2792 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2793 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2794 | int len; |
| 2795 | int l; |
| 2796 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2797 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2798 | |
| 2799 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2800 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 2801 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 2802 | |
| 2803 | max = len - l; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2804 | if (max > chn->buf->i) |
| 2805 | max = chn->buf->i; |
| 2806 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2807 | l += max; |
| 2808 | |
| 2809 | lua_pop(L, 1); |
| 2810 | lua_pushinteger(L, l); |
| 2811 | |
| 2812 | /* Check if it miss bytes to forward. */ |
| 2813 | if (l < len) { |
| 2814 | /* The the input channel or the output channel are closed, we |
| 2815 | * must return the amount of data forwarded. |
| 2816 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2817 | if (channel_input_closed(chn) || channel_output_closed(chn)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2818 | return 1; |
| 2819 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2820 | /* If we are waiting for space data in the response buffer, we |
| 2821 | * must set the flag WAKERESWR. This flag required the task |
| 2822 | * wake up if any activity is detected on the response buffer. |
| 2823 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2824 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2825 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 2826 | else |
| 2827 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2828 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2829 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 2830 | 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] | 2831 | } |
| 2832 | |
| 2833 | return 1; |
| 2834 | } |
| 2835 | |
| 2836 | /* Just check the input and prepare the stack for the previous |
| 2837 | * function "hlua_channel_forward_yield" |
| 2838 | */ |
| 2839 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 2840 | { |
| 2841 | MAY_LJMP(check_args(L, 2, "forward")); |
| 2842 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2843 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 2844 | |
| 2845 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2846 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2847 | } |
| 2848 | |
| 2849 | /* Just returns the number of bytes available in the input |
| 2850 | * side of the buffer. This function never fails. |
| 2851 | */ |
| 2852 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 2853 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2854 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2855 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2856 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2857 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2858 | lua_pushinteger(L, chn->buf->i); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2859 | return 1; |
| 2860 | } |
| 2861 | |
| 2862 | /* Just returns the number of bytes available in the output |
| 2863 | * side of the buffer. This function never fails. |
| 2864 | */ |
| 2865 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 2866 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2867 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2868 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2869 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2870 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2871 | lua_pushinteger(L, chn->buf->o); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2872 | return 1; |
| 2873 | } |
| 2874 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2875 | /* |
| 2876 | * |
| 2877 | * |
| 2878 | * Class Fetches |
| 2879 | * |
| 2880 | * |
| 2881 | */ |
| 2882 | |
| 2883 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2884 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2885 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 2886 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2887 | { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 2888 | 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] | 2889 | } |
| 2890 | |
| 2891 | /* This function creates and push in the stack a fetch object according |
| 2892 | * with a current TXN. |
| 2893 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 2894 | static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, unsigned int flags) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2895 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 2896 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2897 | |
| 2898 | /* Check stack size. */ |
| 2899 | if (!lua_checkstack(L, 3)) |
| 2900 | return 0; |
| 2901 | |
| 2902 | /* Create the object: obj[0] = userdata. |
| 2903 | * Note that the base of the Fetches object is the |
| 2904 | * transaction object. |
| 2905 | */ |
| 2906 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 2907 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2908 | lua_rawseti(L, -2, 0); |
| 2909 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 2910 | hsmp->s = txn->s; |
| 2911 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 2912 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 2913 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2914 | |
| 2915 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2916 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 2917 | lua_setmetatable(L, -2); |
| 2918 | |
| 2919 | return 1; |
| 2920 | } |
| 2921 | |
| 2922 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 2923 | * It uses closure argument to store the associated sample-fetch. It |
| 2924 | * returns only one argument or throws an error. An error is thrown |
| 2925 | * only if an error is encountered during the argument parsing. If |
| 2926 | * the "sample-fetch" function fails, nil is returned. |
| 2927 | */ |
| 2928 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 2929 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 2930 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 2931 | struct sample_fetch *f; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2932 | struct arg args[ARGM_NBARGS + 1]; |
| 2933 | int i; |
| 2934 | struct sample smp; |
| 2935 | |
| 2936 | /* Get closure arguments. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 2937 | f = (struct sample_fetch *)lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2938 | |
| 2939 | /* Get traditionnal arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 2940 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2941 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 2942 | /* Check execution authorization. */ |
| 2943 | if (f->use & SMP_USE_HTTP_ANY && |
| 2944 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 2945 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 2946 | "is not available in Lua services", f->kw); |
| 2947 | WILL_LJMP(lua_error(L)); |
| 2948 | } |
| 2949 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2950 | /* Get extra arguments. */ |
| 2951 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 2952 | if (i >= ARGM_NBARGS) |
| 2953 | break; |
| 2954 | hlua_lua2arg(L, i + 2, &args[i]); |
| 2955 | } |
| 2956 | args[i].type = ARGT_STOP; |
| 2957 | |
| 2958 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 2959 | 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] | 2960 | |
| 2961 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 2962 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2963 | lua_pushfstring(L, "error in arguments"); |
| 2964 | WILL_LJMP(lua_error(L)); |
| 2965 | } |
| 2966 | |
| 2967 | /* Initialise the sample. */ |
| 2968 | memset(&smp, 0, sizeof(smp)); |
| 2969 | |
| 2970 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 2971 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 2972 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 2973 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 2974 | lua_pushstring(L, ""); |
| 2975 | else |
| 2976 | lua_pushnil(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2977 | return 1; |
| 2978 | } |
| 2979 | |
| 2980 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 2981 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 2982 | hlua_smp2lua_str(L, &smp); |
| 2983 | else |
| 2984 | hlua_smp2lua(L, &smp); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2985 | return 1; |
| 2986 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2987 | |
| 2988 | /* |
| 2989 | * |
| 2990 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 2991 | * Class Converters |
| 2992 | * |
| 2993 | * |
| 2994 | */ |
| 2995 | |
| 2996 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2997 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 2998 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 2999 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3000 | { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3001 | 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] | 3002 | } |
| 3003 | |
| 3004 | /* This function creates and push in the stack a Converters object |
| 3005 | * according with a current TXN. |
| 3006 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3007 | static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, unsigned int flags) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3008 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3009 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3010 | |
| 3011 | /* Check stack size. */ |
| 3012 | if (!lua_checkstack(L, 3)) |
| 3013 | return 0; |
| 3014 | |
| 3015 | /* Create the object: obj[0] = userdata. |
| 3016 | * Note that the base of the Converters object is the |
| 3017 | * same than the TXN object. |
| 3018 | */ |
| 3019 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3020 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3021 | lua_rawseti(L, -2, 0); |
| 3022 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3023 | hsmp->s = txn->s; |
| 3024 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3025 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3026 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3027 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3028 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3029 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 3030 | lua_setmetatable(L, -2); |
| 3031 | |
| 3032 | return 1; |
| 3033 | } |
| 3034 | |
| 3035 | /* This function is an LUA binding. It is called with each converter. |
| 3036 | * It uses closure argument to store the associated converter. It |
| 3037 | * returns only one argument or throws an error. An error is thrown |
| 3038 | * only if an error is encountered during the argument parsing. If |
| 3039 | * the converter function function fails, nil is returned. |
| 3040 | */ |
| 3041 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 3042 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3043 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3044 | struct sample_conv *conv; |
| 3045 | struct arg args[ARGM_NBARGS + 1]; |
| 3046 | int i; |
| 3047 | struct sample smp; |
| 3048 | |
| 3049 | /* Get closure arguments. */ |
| 3050 | conv = (struct sample_conv *)lua_touserdata(L, lua_upvalueindex(1)); |
| 3051 | |
| 3052 | /* Get traditionnal arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3053 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3054 | |
| 3055 | /* Get extra arguments. */ |
| 3056 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 3057 | if (i >= ARGM_NBARGS) |
| 3058 | break; |
| 3059 | hlua_lua2arg(L, i + 3, &args[i]); |
| 3060 | } |
| 3061 | args[i].type = ARGT_STOP; |
| 3062 | |
| 3063 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3064 | 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] | 3065 | |
| 3066 | /* Run the special args checker. */ |
| 3067 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 3068 | hlua_pusherror(L, "error in arguments"); |
| 3069 | WILL_LJMP(lua_error(L)); |
| 3070 | } |
| 3071 | |
| 3072 | /* Initialise the sample. */ |
| 3073 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 3074 | hlua_pusherror(L, "error in the input argument"); |
| 3075 | WILL_LJMP(lua_error(L)); |
| 3076 | } |
| 3077 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3078 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 3079 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3080 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3081 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3082 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3083 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3084 | WILL_LJMP(lua_error(L)); |
| 3085 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3086 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 3087 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3088 | hlua_pusherror(L, "error during the input argument casting"); |
| 3089 | WILL_LJMP(lua_error(L)); |
| 3090 | } |
| 3091 | |
| 3092 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3093 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3094 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3095 | lua_pushstring(L, ""); |
| 3096 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3097 | lua_pushnil(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3098 | return 1; |
| 3099 | } |
| 3100 | |
| 3101 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3102 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3103 | hlua_smp2lua_str(L, &smp); |
| 3104 | else |
| 3105 | hlua_smp2lua(L, &smp); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3106 | return 1; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3107 | } |
| 3108 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3109 | /* |
| 3110 | * |
| 3111 | * |
| 3112 | * Class AppletTCP |
| 3113 | * |
| 3114 | * |
| 3115 | */ |
| 3116 | |
| 3117 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 3118 | * a class stream, otherwise it throws an error. |
| 3119 | */ |
| 3120 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 3121 | { |
| 3122 | return (struct hlua_appctx *)MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
| 3123 | } |
| 3124 | |
| 3125 | /* This function creates and push in the stack an Applet object |
| 3126 | * according with a current TXN. |
| 3127 | */ |
| 3128 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 3129 | { |
| 3130 | struct hlua_appctx *appctx; |
| 3131 | struct stream_interface *si = ctx->owner; |
| 3132 | struct stream *s = si_strm(si); |
| 3133 | struct proxy *p = s->be; |
| 3134 | |
| 3135 | /* Check stack size. */ |
| 3136 | if (!lua_checkstack(L, 3)) |
| 3137 | return 0; |
| 3138 | |
| 3139 | /* Create the object: obj[0] = userdata. |
| 3140 | * Note that the base of the Converters object is the |
| 3141 | * same than the TXN object. |
| 3142 | */ |
| 3143 | lua_newtable(L); |
| 3144 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
| 3145 | lua_rawseti(L, -2, 0); |
| 3146 | appctx->appctx = ctx; |
| 3147 | appctx->htxn.s = s; |
| 3148 | appctx->htxn.p = p; |
| 3149 | |
| 3150 | /* Create the "f" field that contains a list of fetches. */ |
| 3151 | lua_pushstring(L, "f"); |
| 3152 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3153 | return 0; |
| 3154 | lua_settable(L, -3); |
| 3155 | |
| 3156 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3157 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3158 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3159 | return 0; |
| 3160 | lua_settable(L, -3); |
| 3161 | |
| 3162 | /* Create the "c" field that contains a list of converters. */ |
| 3163 | lua_pushstring(L, "c"); |
| 3164 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3165 | return 0; |
| 3166 | lua_settable(L, -3); |
| 3167 | |
| 3168 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3169 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3170 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3171 | return 0; |
| 3172 | lua_settable(L, -3); |
| 3173 | |
| 3174 | /* Pop a class stream metatable and affect it to the table. */ |
| 3175 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 3176 | lua_setmetatable(L, -2); |
| 3177 | |
| 3178 | return 1; |
| 3179 | } |
| 3180 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3181 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 3182 | { |
| 3183 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3184 | struct stream *s = appctx->htxn.s; |
| 3185 | struct hlua *hlua = &s->hlua; |
| 3186 | |
| 3187 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3188 | |
| 3189 | /* Remove previous value. */ |
| 3190 | if (hlua->Mref != -1) |
| 3191 | luaL_unref(L, hlua->Mref, LUA_REGISTRYINDEX); |
| 3192 | |
| 3193 | /* Get and store new value. */ |
| 3194 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3195 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3196 | |
| 3197 | return 0; |
| 3198 | } |
| 3199 | |
| 3200 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 3201 | { |
| 3202 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3203 | struct stream *s = appctx->htxn.s; |
| 3204 | struct hlua *hlua = &s->hlua; |
| 3205 | |
| 3206 | /* Push configuration index in the stack. */ |
| 3207 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3208 | |
| 3209 | return 1; |
| 3210 | } |
| 3211 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3212 | /* If expected data not yet available, it returns a yield. This function |
| 3213 | * consumes the data in the buffer. It returns a string containing the |
| 3214 | * data. This string can be empty. |
| 3215 | */ |
| 3216 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 3217 | { |
| 3218 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3219 | struct stream_interface *si = appctx->appctx->owner; |
| 3220 | int ret; |
| 3221 | char *blk1; |
| 3222 | int len1; |
| 3223 | char *blk2; |
| 3224 | int len2; |
| 3225 | |
| 3226 | /* Read the maximum amount of data avalaible. */ |
| 3227 | ret = bo_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
| 3228 | |
| 3229 | /* Data not yet avalaible. return yield. */ |
| 3230 | if (ret == 0) { |
| 3231 | si_applet_cant_get(si); |
| 3232 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0)); |
| 3233 | } |
| 3234 | |
| 3235 | /* End of data: commit the total strings and return. */ |
| 3236 | if (ret < 0) { |
| 3237 | luaL_pushresult(&appctx->b); |
| 3238 | return 1; |
| 3239 | } |
| 3240 | |
| 3241 | /* Ensure that the block 2 length is usable. */ |
| 3242 | if (ret == 1) |
| 3243 | len2 = 0; |
| 3244 | |
| 3245 | /* dont check the max length read and dont check. */ |
| 3246 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3247 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3248 | |
| 3249 | /* Consume input channel output buffer data. */ |
| 3250 | bo_skip(si_oc(si), len1 + len2); |
| 3251 | luaL_pushresult(&appctx->b); |
| 3252 | return 1; |
| 3253 | } |
| 3254 | |
| 3255 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 3256 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 3257 | { |
| 3258 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3259 | |
| 3260 | /* Initialise the string catenation. */ |
| 3261 | luaL_buffinit(L, &appctx->b); |
| 3262 | |
| 3263 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 3264 | } |
| 3265 | |
| 3266 | /* If expected data not yet available, it returns a yield. This function |
| 3267 | * consumes the data in the buffer. It returns a string containing the |
| 3268 | * data. This string can be empty. |
| 3269 | */ |
| 3270 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 3271 | { |
| 3272 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3273 | struct stream_interface *si = appctx->appctx->owner; |
| 3274 | int len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3275 | int ret; |
| 3276 | char *blk1; |
| 3277 | int len1; |
| 3278 | char *blk2; |
| 3279 | int len2; |
| 3280 | |
| 3281 | /* Read the maximum amount of data avalaible. */ |
| 3282 | ret = bo_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
| 3283 | |
| 3284 | /* Data not yet avalaible. return yield. */ |
| 3285 | if (ret == 0) { |
| 3286 | si_applet_cant_get(si); |
| 3287 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0)); |
| 3288 | } |
| 3289 | |
| 3290 | /* End of data: commit the total strings and return. */ |
| 3291 | if (ret < 0) { |
| 3292 | luaL_pushresult(&appctx->b); |
| 3293 | return 1; |
| 3294 | } |
| 3295 | |
| 3296 | /* Ensure that the block 2 length is usable. */ |
| 3297 | if (ret == 1) |
| 3298 | len2 = 0; |
| 3299 | |
| 3300 | if (len == -1) { |
| 3301 | |
| 3302 | /* If len == -1, catenate all the data avalaile and |
| 3303 | * yield because we want to get all the data until |
| 3304 | * the end of data stream. |
| 3305 | */ |
| 3306 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3307 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3308 | bo_skip(si_oc(si), len1 + len2); |
| 3309 | si_applet_cant_get(si); |
| 3310 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0)); |
| 3311 | |
| 3312 | } else { |
| 3313 | |
| 3314 | /* Copy the fisrt block caping to the length required. */ |
| 3315 | if (len1 > len) |
| 3316 | len1 = len; |
| 3317 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3318 | len -= len1; |
| 3319 | |
| 3320 | /* Copy the second block. */ |
| 3321 | if (len2 > len) |
| 3322 | len2 = len; |
| 3323 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3324 | len -= len2; |
| 3325 | |
| 3326 | /* Consume input channel output buffer data. */ |
| 3327 | bo_skip(si_oc(si), len1 + len2); |
| 3328 | |
| 3329 | /* If we are no other data avalaible, yield waiting for new data. */ |
| 3330 | if (len > 0) { |
| 3331 | lua_pushinteger(L, len); |
| 3332 | lua_replace(L, 2); |
| 3333 | si_applet_cant_get(si); |
| 3334 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0)); |
| 3335 | } |
| 3336 | |
| 3337 | /* return the result. */ |
| 3338 | luaL_pushresult(&appctx->b); |
| 3339 | return 1; |
| 3340 | } |
| 3341 | |
| 3342 | /* we never executes this */ |
| 3343 | hlua_pusherror(L, "Lua: internal error"); |
| 3344 | WILL_LJMP(lua_error(L)); |
| 3345 | return 0; |
| 3346 | } |
| 3347 | |
| 3348 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 3349 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 3350 | { |
| 3351 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3352 | int len = -1; |
| 3353 | |
| 3354 | if (lua_gettop(L) > 2) |
| 3355 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 3356 | if (lua_gettop(L) >= 2) { |
| 3357 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3358 | lua_pop(L, 1); |
| 3359 | } |
| 3360 | |
| 3361 | /* Confirm or set the required length */ |
| 3362 | lua_pushinteger(L, len); |
| 3363 | |
| 3364 | /* Initialise the string catenation. */ |
| 3365 | luaL_buffinit(L, &appctx->b); |
| 3366 | |
| 3367 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 3368 | } |
| 3369 | |
| 3370 | /* Append data in the output side of the buffer. This data is immediatly |
| 3371 | * sent. The fcuntion returns the ammount of data writed. If the buffer |
| 3372 | * cannot contains the data, the function yield. The function returns -1 |
| 3373 | * if the channel is closed. |
| 3374 | */ |
| 3375 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 3376 | { |
| 3377 | size_t len; |
| 3378 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3379 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3380 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3381 | struct stream_interface *si = appctx->appctx->owner; |
| 3382 | struct channel *chn = si_ic(si); |
| 3383 | int max; |
| 3384 | |
| 3385 | /* Get the max amount of data which can write as input in the channel. */ |
| 3386 | max = channel_recv_max(chn); |
| 3387 | if (max > (len - l)) |
| 3388 | max = len - l; |
| 3389 | |
| 3390 | /* Copy data. */ |
| 3391 | bi_putblk(chn, str + l, max); |
| 3392 | |
| 3393 | /* update counters. */ |
| 3394 | l += max; |
| 3395 | lua_pop(L, 1); |
| 3396 | lua_pushinteger(L, l); |
| 3397 | |
| 3398 | /* If some data is not send, declares the situation to the |
| 3399 | * applet, and returns a yield. |
| 3400 | */ |
| 3401 | if (l < len) { |
| 3402 | si_applet_cant_put(si); |
| 3403 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0)); |
| 3404 | } |
| 3405 | |
| 3406 | return 1; |
| 3407 | } |
| 3408 | |
| 3409 | /* Just a wraper of "hlua_applet_tcp_send_yield". This wrapper permits |
| 3410 | * yield the LUA process, and resume it without checking the |
| 3411 | * input arguments. |
| 3412 | */ |
| 3413 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 3414 | { |
| 3415 | MAY_LJMP(check_args(L, 2, "send")); |
| 3416 | lua_pushinteger(L, 0); |
| 3417 | |
| 3418 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 3419 | } |
| 3420 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3421 | /* |
| 3422 | * |
| 3423 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3424 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3425 | * |
| 3426 | * |
| 3427 | */ |
| 3428 | |
| 3429 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3430 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3431 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3432 | __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] | 3433 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3434 | 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] | 3435 | } |
| 3436 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3437 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3438 | * according with a current TXN. |
| 3439 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3440 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3441 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3442 | struct hlua_appctx *appctx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3443 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3444 | struct stream_interface *si = ctx->owner; |
| 3445 | struct stream *s = si_strm(si); |
| 3446 | struct proxy *px = s->be; |
| 3447 | struct http_txn *txn = s->txn; |
| 3448 | const char *path; |
| 3449 | const char *end; |
| 3450 | const char *p; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3451 | |
| 3452 | /* Check stack size. */ |
| 3453 | if (!lua_checkstack(L, 3)) |
| 3454 | return 0; |
| 3455 | |
| 3456 | /* Create the object: obj[0] = userdata. |
| 3457 | * Note that the base of the Converters object is the |
| 3458 | * same than the TXN object. |
| 3459 | */ |
| 3460 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3461 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3462 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3463 | appctx->appctx = ctx; |
| 3464 | appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
| 3465 | appctx->htxn.s = s; |
| 3466 | appctx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3467 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3468 | /* Create the "f" field that contains a list of fetches. */ |
| 3469 | lua_pushstring(L, "f"); |
| 3470 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3471 | return 0; |
| 3472 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3473 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3474 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3475 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3476 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3477 | return 0; |
| 3478 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3479 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3480 | /* Create the "c" field that contains a list of converters. */ |
| 3481 | lua_pushstring(L, "c"); |
| 3482 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3483 | return 0; |
| 3484 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3485 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3486 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3487 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3488 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3489 | return 0; |
| 3490 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3491 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3492 | /* Stores the request method. */ |
| 3493 | lua_pushstring(L, "method"); |
| 3494 | lua_pushlstring(L, txn->req.chn->buf->p, txn->req.sl.rq.m_l); |
| 3495 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3496 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3497 | /* Stores the http version. */ |
| 3498 | lua_pushstring(L, "version"); |
| 3499 | lua_pushlstring(L, txn->req.chn->buf->p + txn->req.sl.rq.v, txn->req.sl.rq.v_l); |
| 3500 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3501 | |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3502 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 3503 | * the array on the top of the stack. |
| 3504 | */ |
| 3505 | lua_pushstring(L, "headers"); |
| 3506 | htxn.s = s; |
| 3507 | htxn.p = px; |
| 3508 | htxn.dir = SMP_OPT_DIR_REQ; |
| 3509 | if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req)) |
| 3510 | return 0; |
| 3511 | lua_settable(L, -3); |
| 3512 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3513 | /* Get path and qs */ |
| 3514 | path = http_get_path(txn); |
| 3515 | end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
| 3516 | p = path; |
| 3517 | while (p < end && *p != '?') |
| 3518 | p++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3519 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3520 | /* Stores the request path. */ |
| 3521 | lua_pushstring(L, "path"); |
| 3522 | lua_pushlstring(L, path, p - path); |
| 3523 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3524 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3525 | /* Stores the query string. */ |
| 3526 | lua_pushstring(L, "qs"); |
| 3527 | if (*p == '?') |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3528 | p++; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3529 | lua_pushlstring(L, p, end - p); |
| 3530 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3531 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3532 | /* Stores the request path. */ |
| 3533 | lua_pushstring(L, "length"); |
| 3534 | lua_pushinteger(L, txn->req.body_len); |
| 3535 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3536 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3537 | /* Create an array of HTTP request headers. */ |
| 3538 | lua_pushstring(L, "headers"); |
| 3539 | MAY_LJMP(hlua_http_get_headers(L, &appctx->htxn, &appctx->htxn.s->txn->req)); |
| 3540 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3541 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3542 | /* Create an empty array of HTTP request headers. */ |
| 3543 | lua_pushstring(L, "response"); |
| 3544 | lua_newtable(L); |
| 3545 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3546 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3547 | /* Pop a class stream metatable and affect it to the table. */ |
| 3548 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 3549 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3550 | |
| 3551 | return 1; |
| 3552 | } |
| 3553 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3554 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 3555 | { |
| 3556 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3557 | struct stream *s = appctx->htxn.s; |
| 3558 | struct hlua *hlua = &s->hlua; |
| 3559 | |
| 3560 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3561 | |
| 3562 | /* Remove previous value. */ |
| 3563 | if (hlua->Mref != -1) |
| 3564 | luaL_unref(L, hlua->Mref, LUA_REGISTRYINDEX); |
| 3565 | |
| 3566 | /* Get and store new value. */ |
| 3567 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3568 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3569 | |
| 3570 | return 0; |
| 3571 | } |
| 3572 | |
| 3573 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 3574 | { |
| 3575 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3576 | struct stream *s = appctx->htxn.s; |
| 3577 | struct hlua *hlua = &s->hlua; |
| 3578 | |
| 3579 | /* Push configuration index in the stack. */ |
| 3580 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3581 | |
| 3582 | return 1; |
| 3583 | } |
| 3584 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3585 | /* If expected data not yet available, it returns a yield. This function |
| 3586 | * consumes the data in the buffer. It returns a string containing the |
| 3587 | * data. This string can be empty. |
| 3588 | */ |
| 3589 | __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] | 3590 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3591 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3592 | struct stream_interface *si = appctx->appctx->owner; |
| 3593 | struct channel *chn = si_ic(si); |
| 3594 | int ret; |
| 3595 | char *blk1; |
| 3596 | int len1; |
| 3597 | char *blk2; |
| 3598 | int len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3599 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3600 | /* Maybe we cant send a 100-continue ? */ |
| 3601 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) { |
| 3602 | ret = bi_putblk(chn, HTTP_100C, strlen(HTTP_100C)); |
| 3603 | /* if ret == -2 or -3 the channel closed or the message si too |
| 3604 | * big for the buffers. We cant send anything. So, we ignoring |
| 3605 | * the error, considers that the 100-continue is sent, and try |
| 3606 | * to receive. |
| 3607 | * If ret is -1, we dont have room in the buffer, so we yield. |
| 3608 | */ |
| 3609 | if (ret == -1) { |
| 3610 | si_applet_cant_put(si); |
| 3611 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0)); |
| 3612 | } |
| 3613 | appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C; |
| 3614 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3615 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3616 | /* Check for the end of the data. */ |
| 3617 | if (appctx->appctx->ctx.hlua_apphttp.left_bytes <= 0) { |
| 3618 | luaL_pushresult(&appctx->b); |
| 3619 | return 1; |
| 3620 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3621 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3622 | /* Read the maximum amount of data avalaible. */ |
| 3623 | ret = bo_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3624 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3625 | /* Data not yet avalaible. return yield. */ |
| 3626 | if (ret == 0) { |
| 3627 | si_applet_cant_get(si); |
| 3628 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0)); |
| 3629 | } |
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 | /* End of data: commit the total strings and return. */ |
| 3632 | if (ret < 0) { |
| 3633 | luaL_pushresult(&appctx->b); |
| 3634 | return 1; |
| 3635 | } |
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 | /* Ensure that the block 2 length is usable. */ |
| 3638 | if (ret == 1) |
| 3639 | len2 = 0; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3640 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3641 | /* Copy the fisrt block caping to the length required. */ |
| 3642 | if (len1 > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 3643 | len1 = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 3644 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3645 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3646 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3647 | /* Copy the second block. */ |
| 3648 | if (len2 > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 3649 | len2 = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 3650 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3651 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3652 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3653 | /* Consume input channel output buffer data. */ |
| 3654 | bo_skip(si_oc(si), len1 + len2); |
| 3655 | luaL_pushresult(&appctx->b); |
| 3656 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3657 | } |
| 3658 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3659 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 3660 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3661 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3662 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3663 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3664 | /* Initialise the string catenation. */ |
| 3665 | luaL_buffinit(L, &appctx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3666 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3667 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3668 | } |
| 3669 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3670 | /* If expected data not yet available, it returns a yield. This function |
| 3671 | * consumes the data in the buffer. It returns a string containing the |
| 3672 | * data. This string can be empty. |
| 3673 | */ |
| 3674 | __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] | 3675 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3676 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3677 | struct stream_interface *si = appctx->appctx->owner; |
| 3678 | int len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3679 | struct channel *chn = si_ic(si); |
| 3680 | int ret; |
| 3681 | char *blk1; |
| 3682 | int len1; |
| 3683 | char *blk2; |
| 3684 | int len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3685 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3686 | /* Maybe we cant send a 100-continue ? */ |
| 3687 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) { |
| 3688 | ret = bi_putblk(chn, HTTP_100C, strlen(HTTP_100C)); |
| 3689 | /* if ret == -2 or -3 the channel closed or the message si too |
| 3690 | * big for the buffers. We cant send anything. So, we ignoring |
| 3691 | * the error, considers that the 100-continue is sent, and try |
| 3692 | * to receive. |
| 3693 | * If ret is -1, we dont have room in the buffer, so we yield. |
| 3694 | */ |
| 3695 | if (ret == -1) { |
| 3696 | si_applet_cant_put(si); |
| 3697 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0)); |
| 3698 | } |
| 3699 | appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C; |
| 3700 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3701 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3702 | /* Read the maximum amount of data avalaible. */ |
| 3703 | ret = bo_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3704 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3705 | /* Data not yet avalaible. return yield. */ |
| 3706 | if (ret == 0) { |
| 3707 | si_applet_cant_get(si); |
| 3708 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0)); |
| 3709 | } |
| 3710 | |
| 3711 | /* End of data: commit the total strings and return. */ |
| 3712 | if (ret < 0) { |
| 3713 | luaL_pushresult(&appctx->b); |
| 3714 | return 1; |
| 3715 | } |
| 3716 | |
| 3717 | /* Ensure that the block 2 length is usable. */ |
| 3718 | if (ret == 1) |
| 3719 | len2 = 0; |
| 3720 | |
| 3721 | /* Copy the fisrt block caping to the length required. */ |
| 3722 | if (len1 > len) |
| 3723 | len1 = len; |
| 3724 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3725 | len -= len1; |
| 3726 | |
| 3727 | /* Copy the second block. */ |
| 3728 | if (len2 > len) |
| 3729 | len2 = len; |
| 3730 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3731 | len -= len2; |
| 3732 | |
| 3733 | /* Consume input channel output buffer data. */ |
| 3734 | bo_skip(si_oc(si), len1 + len2); |
| 3735 | if (appctx->appctx->ctx.hlua_apphttp.left_bytes != -1) |
| 3736 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len; |
| 3737 | |
| 3738 | /* If we are no other data avalaible, yield waiting for new data. */ |
| 3739 | if (len > 0) { |
| 3740 | lua_pushinteger(L, len); |
| 3741 | lua_replace(L, 2); |
| 3742 | si_applet_cant_get(si); |
| 3743 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0)); |
| 3744 | } |
| 3745 | |
| 3746 | /* return the result. */ |
| 3747 | luaL_pushresult(&appctx->b); |
| 3748 | return 1; |
| 3749 | } |
| 3750 | |
| 3751 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 3752 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 3753 | { |
| 3754 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3755 | int len = -1; |
| 3756 | |
| 3757 | /* Check arguments. */ |
| 3758 | if (lua_gettop(L) > 2) |
| 3759 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 3760 | if (lua_gettop(L) >= 2) { |
| 3761 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3762 | lua_pop(L, 1); |
| 3763 | } |
| 3764 | |
| 3765 | /* Check the required length */ |
| 3766 | if (len == -1 || len > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 3767 | len = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 3768 | lua_pushinteger(L, len); |
| 3769 | |
| 3770 | /* Initialise the string catenation. */ |
| 3771 | luaL_buffinit(L, &appctx->b); |
| 3772 | |
| 3773 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
| 3774 | } |
| 3775 | |
| 3776 | /* Append data in the output side of the buffer. This data is immediatly |
| 3777 | * sent. The fcuntion returns the ammount of data writed. If the buffer |
| 3778 | * cannot contains the data, the function yield. The function returns -1 |
| 3779 | * if the channel is closed. |
| 3780 | */ |
| 3781 | __LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 3782 | { |
| 3783 | size_t len; |
| 3784 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3785 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3786 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3787 | struct stream_interface *si = appctx->appctx->owner; |
| 3788 | struct channel *chn = si_ic(si); |
| 3789 | int max; |
| 3790 | |
| 3791 | /* Get the max amount of data which can write as input in the channel. */ |
| 3792 | max = channel_recv_max(chn); |
| 3793 | if (max > (len - l)) |
| 3794 | max = len - l; |
| 3795 | |
| 3796 | /* Copy data. */ |
| 3797 | bi_putblk(chn, str + l, max); |
| 3798 | |
| 3799 | /* update counters. */ |
| 3800 | l += max; |
| 3801 | lua_pop(L, 1); |
| 3802 | lua_pushinteger(L, l); |
| 3803 | |
| 3804 | /* If some data is not send, declares the situation to the |
| 3805 | * applet, and returns a yield. |
| 3806 | */ |
| 3807 | if (l < len) { |
| 3808 | si_applet_cant_put(si); |
| 3809 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0)); |
| 3810 | } |
| 3811 | |
| 3812 | return 1; |
| 3813 | } |
| 3814 | |
| 3815 | /* Just a wraper of "hlua_applet_send_yield". This wrapper permits |
| 3816 | * yield the LUA process, and resume it without checking the |
| 3817 | * input arguments. |
| 3818 | */ |
| 3819 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 3820 | { |
| 3821 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3822 | size_t len; |
| 3823 | char hex[10]; |
| 3824 | |
| 3825 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3826 | |
| 3827 | /* If transfer encoding chunked is selected, we surround the data |
| 3828 | * by chunk data. |
| 3829 | */ |
| 3830 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED) { |
| 3831 | snprintf(hex, 9, "%x", (unsigned int)len); |
| 3832 | lua_pushfstring(L, "%s\r\n", hex); |
| 3833 | lua_insert(L, 2); /* swap the last 2 entries. */ |
| 3834 | lua_pushstring(L, "\r\n"); |
| 3835 | lua_concat(L, 3); |
| 3836 | } |
| 3837 | |
| 3838 | /* This interger is used for followinf the amount of data sent. */ |
| 3839 | lua_pushinteger(L, 0); |
| 3840 | |
| 3841 | /* We want to send some data. Headers must be sent. */ |
| 3842 | if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 3843 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 3844 | WILL_LJMP(lua_error(L)); |
| 3845 | } |
| 3846 | |
| 3847 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
| 3848 | } |
| 3849 | |
| 3850 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 3851 | { |
| 3852 | const char *name; |
| 3853 | int ret; |
| 3854 | |
| 3855 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3856 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 3857 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 3858 | |
| 3859 | /* Push in the stack the "response" entry. */ |
| 3860 | ret = lua_getfield(L, 1, "response"); |
| 3861 | if (ret != LUA_TTABLE) { |
| 3862 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 3863 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 3864 | WILL_LJMP(lua_error(L)); |
| 3865 | } |
| 3866 | |
| 3867 | /* check if the header is already registered if it is not |
| 3868 | * the case, register it. |
| 3869 | */ |
| 3870 | ret = lua_getfield(L, -1, name); |
| 3871 | if (ret == LUA_TNIL) { |
| 3872 | |
| 3873 | /* Entry not found. */ |
| 3874 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 3875 | |
| 3876 | /* Insert the new header name in the array in the top of the stack. |
| 3877 | * It left the new array in the top of the stack. |
| 3878 | */ |
| 3879 | lua_newtable(L); |
| 3880 | lua_pushvalue(L, 2); |
| 3881 | lua_pushvalue(L, -2); |
| 3882 | lua_settable(L, -4); |
| 3883 | |
| 3884 | } else if (ret != LUA_TTABLE) { |
| 3885 | |
| 3886 | /* corruption error. */ |
| 3887 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 3888 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 3889 | WILL_LJMP(lua_error(L)); |
| 3890 | } |
| 3891 | |
| 3892 | /* Now the top od thestack is an array of values. We push |
| 3893 | * the header value as new entry. |
| 3894 | */ |
| 3895 | lua_pushvalue(L, 3); |
| 3896 | ret = lua_rawlen(L, -2); |
| 3897 | lua_rawseti(L, -2, ret + 1); |
| 3898 | lua_pushboolean(L, 1); |
| 3899 | return 1; |
| 3900 | } |
| 3901 | |
| 3902 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 3903 | { |
| 3904 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3905 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3906 | |
| 3907 | if (status < 100 || status > 599) { |
| 3908 | lua_pushboolean(L, 0); |
| 3909 | return 1; |
| 3910 | } |
| 3911 | |
| 3912 | appctx->appctx->ctx.hlua_apphttp.status = status; |
| 3913 | lua_pushboolean(L, 1); |
| 3914 | return 1; |
| 3915 | } |
| 3916 | |
| 3917 | /* We will build the status line and the headers of the HTTP response. |
| 3918 | * We will try send at once if its not possible, we give back the hand |
| 3919 | * waiting for more room. |
| 3920 | */ |
| 3921 | __LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx) |
| 3922 | { |
| 3923 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3924 | struct stream_interface *si = appctx->appctx->owner; |
| 3925 | struct channel *chn = si_ic(si); |
| 3926 | int ret; |
| 3927 | size_t len; |
| 3928 | const char *msg; |
| 3929 | |
| 3930 | /* Get the message as the first argument on the stack. */ |
| 3931 | msg = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3932 | |
| 3933 | /* Send the message at once. */ |
| 3934 | ret = bi_putblk(chn, msg, len); |
| 3935 | |
| 3936 | /* if ret == -2 or -3 the channel closed or the message si too |
| 3937 | * big for the buffers. |
| 3938 | */ |
| 3939 | if (ret == -2 || ret == -3) { |
| 3940 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 3941 | WILL_LJMP(lua_error(L)); |
| 3942 | } |
| 3943 | |
| 3944 | /* If ret is -1, we dont have room in the buffer, so we yield. */ |
| 3945 | if (ret == -1) { |
| 3946 | si_applet_cant_put(si); |
| 3947 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0)); |
| 3948 | } |
| 3949 | |
| 3950 | /* Headers sent, set the flag. */ |
| 3951 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
| 3952 | return 0; |
| 3953 | } |
| 3954 | |
| 3955 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
| 3956 | { |
| 3957 | struct chunk *tmp = get_trash_chunk(); |
| 3958 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3959 | const char *name; |
| 3960 | const char *value; |
| 3961 | int id; |
| 3962 | int hdr_connection = 0; |
| 3963 | int hdr_contentlength = -1; |
| 3964 | int hdr_chunked = 0; |
| 3965 | |
| 3966 | /* Use the same http version than the request. */ |
| 3967 | chunk_appendf(tmp, "HTTP/1.%c %d %s\r\n", |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 3968 | appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11 ? '1' : '0', |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3969 | appctx->appctx->ctx.hlua_apphttp.status, |
| 3970 | get_reason(appctx->appctx->ctx.hlua_apphttp.status)); |
| 3971 | |
| 3972 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 3973 | lua_pushvalue(L, 0); |
| 3974 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 3975 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
| 3976 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 3977 | WILL_LJMP(lua_error(L)); |
| 3978 | } |
| 3979 | |
| 3980 | /* Browse the list of headers. */ |
| 3981 | lua_pushnil(L); |
| 3982 | while(lua_next(L, -2) != 0) { |
| 3983 | |
| 3984 | /* We expect a string as -2. */ |
| 3985 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 3986 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
| 3987 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 3988 | lua_typename(L, lua_type(L, -2))); |
| 3989 | WILL_LJMP(lua_error(L)); |
| 3990 | } |
| 3991 | name = lua_tostring(L, -2); |
| 3992 | |
| 3993 | /* We expect an array as -1. */ |
| 3994 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 3995 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
| 3996 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 3997 | name, |
| 3998 | lua_typename(L, lua_type(L, -1))); |
| 3999 | WILL_LJMP(lua_error(L)); |
| 4000 | } |
| 4001 | |
| 4002 | /* Browse the table who is on the top of the stack. */ |
| 4003 | lua_pushnil(L); |
| 4004 | while(lua_next(L, -2) != 0) { |
| 4005 | |
| 4006 | /* We expect a number as -2. */ |
| 4007 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 4008 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
| 4009 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4010 | name, |
| 4011 | lua_typename(L, lua_type(L, -2))); |
| 4012 | WILL_LJMP(lua_error(L)); |
| 4013 | } |
| 4014 | id = lua_tointeger(L, -2); |
| 4015 | |
| 4016 | /* We expect a string as -2. */ |
| 4017 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 4018 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
| 4019 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4020 | name, id, |
| 4021 | lua_typename(L, lua_type(L, -1))); |
| 4022 | WILL_LJMP(lua_error(L)); |
| 4023 | } |
| 4024 | value = lua_tostring(L, -1); |
| 4025 | |
| 4026 | /* Catenate a new header. */ |
| 4027 | chunk_appendf(tmp, "%s: %s\r\n", name, value); |
| 4028 | |
| 4029 | /* Protocol checks. */ |
| 4030 | |
| 4031 | /* Check if the header conneciton is present. */ |
| 4032 | if (strcasecmp("connection", name) == 0) |
| 4033 | hdr_connection = 1; |
| 4034 | |
| 4035 | /* Copy the header content length. The length conversion |
| 4036 | * is done without control. If it contains a ad value, this |
| 4037 | * is not our problem. |
| 4038 | */ |
| 4039 | if (strcasecmp("content-length", name) == 0) |
| 4040 | hdr_contentlength = atoi(value); |
| 4041 | |
| 4042 | /* Check if the client annouces a transfer-encoding chunked it self. */ |
| 4043 | if (strcasecmp("transfer-encoding", name) == 0 && |
| 4044 | strcasecmp("chunked", value) == 0) |
| 4045 | hdr_chunked = 1; |
| 4046 | |
| 4047 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4048 | lua_pop(L, 1); |
| 4049 | } |
| 4050 | |
| 4051 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4052 | lua_pop(L, 1); |
| 4053 | } |
| 4054 | |
| 4055 | /* If the http protocol version is 1.1, we expect an header "connection" set |
| 4056 | * to "close" to be HAProxy/keeplive compliant. Otherwise, we expect nothing. |
| 4057 | * If the header conneciton is present, don't change it, if it is not present, |
| 4058 | * we must set. |
| 4059 | * |
| 4060 | * we set a "connection: close" header for ensuring that the keepalive will be |
| 4061 | * respected by haproxy. HAProcy considers that the application cloe the connection |
| 4062 | * and it keep the connection from the client open. |
| 4063 | */ |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 4064 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11 && !hdr_connection) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4065 | chunk_appendf(tmp, "Connection: close\r\n"); |
| 4066 | |
| 4067 | /* If we dont have a content-length set, we must announce a transfer enconding |
| 4068 | * chunked. This is required by haproxy for the keepalive compliance. |
| 4069 | * If the applet annouce a transfer-encoding chunked itslef, don't |
| 4070 | * do anything. |
| 4071 | */ |
| 4072 | if (hdr_contentlength == -1 && hdr_chunked == 0) { |
| 4073 | chunk_appendf(tmp, "Transfer-encoding: chunked\r\n"); |
| 4074 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_CHUNKED; |
| 4075 | } |
| 4076 | |
| 4077 | /* Finalize headers. */ |
| 4078 | chunk_appendf(tmp, "\r\n"); |
| 4079 | |
| 4080 | /* Remove the last entry and the array of headers */ |
| 4081 | lua_pop(L, 2); |
| 4082 | |
| 4083 | /* Push the headers block. */ |
| 4084 | lua_pushlstring(L, tmp->str, tmp->len); |
| 4085 | |
| 4086 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
| 4087 | } |
| 4088 | |
| 4089 | /* |
| 4090 | * |
| 4091 | * |
| 4092 | * Class HTTP |
| 4093 | * |
| 4094 | * |
| 4095 | */ |
| 4096 | |
| 4097 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4098 | * a class stream, otherwise it throws an error. |
| 4099 | */ |
| 4100 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
| 4101 | { |
| 4102 | return (struct hlua_txn *)MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
| 4103 | } |
| 4104 | |
| 4105 | /* This function creates and push in the stack a HTTP object |
| 4106 | * according with a current TXN. |
| 4107 | */ |
| 4108 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 4109 | { |
| 4110 | struct hlua_txn *htxn; |
| 4111 | |
| 4112 | /* Check stack size. */ |
| 4113 | if (!lua_checkstack(L, 3)) |
| 4114 | return 0; |
| 4115 | |
| 4116 | /* Create the object: obj[0] = userdata. |
| 4117 | * Note that the base of the Converters object is the |
| 4118 | * same than the TXN object. |
| 4119 | */ |
| 4120 | lua_newtable(L); |
| 4121 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 4122 | lua_rawseti(L, -2, 0); |
| 4123 | |
| 4124 | htxn->s = txn->s; |
| 4125 | htxn->p = txn->p; |
| 4126 | |
| 4127 | /* Pop a class stream metatable and affect it to the table. */ |
| 4128 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 4129 | lua_setmetatable(L, -2); |
| 4130 | |
| 4131 | return 1; |
| 4132 | } |
| 4133 | |
| 4134 | /* This function creates ans returns an array of HTTP headers. |
| 4135 | * This function does not fails. It is used as wrapper with the |
| 4136 | * 2 following functions. |
| 4137 | */ |
| 4138 | __LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4139 | { |
| 4140 | const char *cur_ptr, *cur_next, *p; |
| 4141 | int old_idx, cur_idx; |
| 4142 | struct hdr_idx_elem *cur_hdr; |
| 4143 | const char *hn, *hv; |
| 4144 | int hnl, hvl; |
| 4145 | int type; |
| 4146 | const char *in; |
| 4147 | char *out; |
| 4148 | int len; |
| 4149 | |
| 4150 | /* Create the table. */ |
| 4151 | lua_newtable(L); |
| 4152 | |
| 4153 | if (!htxn->s->txn) |
| 4154 | return 1; |
| 4155 | |
| 4156 | /* Build array of headers. */ |
| 4157 | old_idx = 0; |
| 4158 | cur_next = msg->chn->buf->p + hdr_idx_first_pos(&htxn->s->txn->hdr_idx); |
| 4159 | |
| 4160 | while (1) { |
| 4161 | cur_idx = htxn->s->txn->hdr_idx.v[old_idx].next; |
| 4162 | if (!cur_idx) |
| 4163 | break; |
| 4164 | old_idx = cur_idx; |
| 4165 | |
| 4166 | cur_hdr = &htxn->s->txn->hdr_idx.v[cur_idx]; |
| 4167 | cur_ptr = cur_next; |
| 4168 | cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1; |
| 4169 | |
| 4170 | /* Now we have one full header at cur_ptr of len cur_hdr->len, |
| 4171 | * and the next header starts at cur_next. We'll check |
| 4172 | * this header in the list as well as against the default |
| 4173 | * rule. |
| 4174 | */ |
| 4175 | |
| 4176 | /* look for ': *'. */ |
| 4177 | hn = cur_ptr; |
| 4178 | for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++); |
| 4179 | if (p >= cur_ptr+cur_hdr->len) |
| 4180 | continue; |
| 4181 | hnl = p - hn; |
| 4182 | p++; |
| 4183 | while (p < cur_ptr+cur_hdr->len && ( *p == ' ' || *p == '\t' )) |
| 4184 | p++; |
| 4185 | if (p >= cur_ptr+cur_hdr->len) |
| 4186 | continue; |
| 4187 | hv = p; |
| 4188 | hvl = cur_ptr+cur_hdr->len-p; |
| 4189 | |
| 4190 | /* Lowercase the key. Don't check the size of trash, it have |
| 4191 | * the size of one buffer and the input data contains in one |
| 4192 | * buffer. |
| 4193 | */ |
| 4194 | out = trash.str; |
| 4195 | for (in=hn; in<hn+hnl; in++, out++) |
| 4196 | *out = tolower(*in); |
| 4197 | *out = '\0'; |
| 4198 | |
| 4199 | /* Check for existing entry: |
| 4200 | * assume that the table is on the top of the stack, and |
| 4201 | * push the key in the stack, the function lua_gettable() |
| 4202 | * perform the lookup. |
| 4203 | */ |
| 4204 | lua_pushlstring(L, trash.str, hnl); |
| 4205 | lua_gettable(L, -2); |
| 4206 | type = lua_type(L, -1); |
| 4207 | |
| 4208 | switch (type) { |
| 4209 | case LUA_TNIL: |
| 4210 | /* Table not found, create it. */ |
| 4211 | lua_pop(L, 1); /* remove the nil value. */ |
| 4212 | lua_pushlstring(L, trash.str, hnl); /* push the header name as key. */ |
| 4213 | lua_newtable(L); /* create and push empty table. */ |
| 4214 | lua_pushlstring(L, hv, hvl); /* push header value. */ |
| 4215 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 4216 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
| 4217 | break; |
| 4218 | |
| 4219 | case LUA_TTABLE: |
| 4220 | /* Entry found: push the value in the table. */ |
| 4221 | len = lua_rawlen(L, -1); |
| 4222 | lua_pushlstring(L, hv, hvl); /* push header value. */ |
| 4223 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 4224 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 4225 | break; |
| 4226 | |
| 4227 | default: |
| 4228 | /* Other cases are errors. */ |
| 4229 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 4230 | WILL_LJMP(lua_error(L)); |
| 4231 | } |
| 4232 | } |
| 4233 | |
| 4234 | return 1; |
| 4235 | } |
| 4236 | |
| 4237 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 4238 | { |
| 4239 | struct hlua_txn *htxn; |
| 4240 | |
| 4241 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 4242 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4243 | |
| 4244 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->req); |
| 4245 | } |
| 4246 | |
| 4247 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 4248 | { |
| 4249 | struct hlua_txn *htxn; |
| 4250 | |
| 4251 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 4252 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4253 | |
| 4254 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp); |
| 4255 | } |
| 4256 | |
| 4257 | /* This function replace full header, or just a value in |
| 4258 | * the request or in the response. It is a wrapper fir the |
| 4259 | * 4 following functions. |
| 4260 | */ |
| 4261 | __LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn, |
| 4262 | struct http_msg *msg, int action) |
| 4263 | { |
| 4264 | size_t name_len; |
| 4265 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4266 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 4267 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
| 4268 | struct my_regex re; |
| 4269 | |
| 4270 | if (!regex_comp(reg, &re, 1, 1, NULL)) |
| 4271 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 4272 | |
| 4273 | http_transform_header_str(htxn->s, msg, name, name_len, value, &re, action); |
| 4274 | regex_free(&re); |
| 4275 | return 0; |
| 4276 | } |
| 4277 | |
| 4278 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 4279 | { |
| 4280 | struct hlua_txn *htxn; |
| 4281 | |
| 4282 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4283 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4284 | |
| 4285 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR)); |
| 4286 | } |
| 4287 | |
| 4288 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 4289 | { |
| 4290 | struct hlua_txn *htxn; |
| 4291 | |
| 4292 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 4293 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4294 | |
| 4295 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR)); |
| 4296 | } |
| 4297 | |
| 4298 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 4299 | { |
| 4300 | struct hlua_txn *htxn; |
| 4301 | |
| 4302 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4303 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4304 | |
| 4305 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL)); |
| 4306 | } |
| 4307 | |
| 4308 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 4309 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4310 | struct hlua_txn *htxn; |
| 4311 | |
| 4312 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 4313 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4314 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 4315 | 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] | 4316 | } |
| 4317 | |
| 4318 | /* This function deletes all the occurences of an header. |
| 4319 | * It is a wrapper for the 2 following functions. |
| 4320 | */ |
| 4321 | __LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4322 | { |
| 4323 | size_t len; |
| 4324 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4325 | struct hdr_ctx ctx; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4326 | struct http_txn *txn = htxn->s->txn; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4327 | |
| 4328 | ctx.idx = 0; |
| 4329 | while (http_find_header2(name, len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) |
| 4330 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 4331 | return 0; |
| 4332 | } |
| 4333 | |
| 4334 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 4335 | { |
| 4336 | struct hlua_txn *htxn; |
| 4337 | |
| 4338 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 4339 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4340 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4341 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4342 | } |
| 4343 | |
| 4344 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 4345 | { |
| 4346 | struct hlua_txn *htxn; |
| 4347 | |
| 4348 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 4349 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4350 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4351 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4352 | } |
| 4353 | |
| 4354 | /* This function adds an header. It is a wrapper used by |
| 4355 | * the 2 following functions. |
| 4356 | */ |
| 4357 | __LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4358 | { |
| 4359 | size_t name_len; |
| 4360 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4361 | size_t value_len; |
| 4362 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
| 4363 | char *p; |
| 4364 | |
| 4365 | /* Check length. */ |
| 4366 | trash.len = value_len + name_len + 2; |
| 4367 | if (trash.len > trash.size) |
| 4368 | return 0; |
| 4369 | |
| 4370 | /* Creates the header string. */ |
| 4371 | p = trash.str; |
| 4372 | memcpy(p, name, name_len); |
| 4373 | p += name_len; |
| 4374 | *p = ':'; |
| 4375 | p++; |
| 4376 | *p = ' '; |
| 4377 | p++; |
| 4378 | memcpy(p, value, value_len); |
| 4379 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4380 | 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] | 4381 | trash.str, trash.len) != 0); |
| 4382 | |
| 4383 | return 0; |
| 4384 | } |
| 4385 | |
| 4386 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 4387 | { |
| 4388 | struct hlua_txn *htxn; |
| 4389 | |
| 4390 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 4391 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4392 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4393 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4394 | } |
| 4395 | |
| 4396 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 4397 | { |
| 4398 | struct hlua_txn *htxn; |
| 4399 | |
| 4400 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 4401 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4402 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4403 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4404 | } |
| 4405 | |
| 4406 | static int hlua_http_req_set_hdr(lua_State *L) |
| 4407 | { |
| 4408 | struct hlua_txn *htxn; |
| 4409 | |
| 4410 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 4411 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4412 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4413 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
| 4414 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4415 | } |
| 4416 | |
| 4417 | static int hlua_http_res_set_hdr(lua_State *L) |
| 4418 | { |
| 4419 | struct hlua_txn *htxn; |
| 4420 | |
| 4421 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 4422 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4423 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4424 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
| 4425 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4426 | } |
| 4427 | |
| 4428 | /* This function set the method. */ |
| 4429 | static int hlua_http_req_set_meth(lua_State *L) |
| 4430 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4431 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4432 | size_t name_len; |
| 4433 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4434 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4435 | 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] | 4436 | return 1; |
| 4437 | } |
| 4438 | |
| 4439 | /* This function set the method. */ |
| 4440 | static int hlua_http_req_set_path(lua_State *L) |
| 4441 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4442 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4443 | size_t name_len; |
| 4444 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4445 | 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] | 4446 | return 1; |
| 4447 | } |
| 4448 | |
| 4449 | /* This function set the query-string. */ |
| 4450 | static int hlua_http_req_set_query(lua_State *L) |
| 4451 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4452 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4453 | size_t name_len; |
| 4454 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4455 | |
| 4456 | /* Check length. */ |
| 4457 | if (name_len > trash.size - 1) { |
| 4458 | lua_pushboolean(L, 0); |
| 4459 | return 1; |
| 4460 | } |
| 4461 | |
| 4462 | /* Add the mark question as prefix. */ |
| 4463 | chunk_reset(&trash); |
| 4464 | trash.str[trash.len++] = '?'; |
| 4465 | memcpy(trash.str + trash.len, name, name_len); |
| 4466 | trash.len += name_len; |
| 4467 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4468 | 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] | 4469 | return 1; |
| 4470 | } |
| 4471 | |
| 4472 | /* This function set the uri. */ |
| 4473 | static int hlua_http_req_set_uri(lua_State *L) |
| 4474 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4475 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4476 | size_t name_len; |
| 4477 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4478 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4479 | 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] | 4480 | return 1; |
| 4481 | } |
| 4482 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 4483 | /* This function set the response code. */ |
| 4484 | static int hlua_http_res_set_status(lua_State *L) |
| 4485 | { |
| 4486 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4487 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4488 | |
| 4489 | http_set_status(code, htxn->s); |
| 4490 | return 0; |
| 4491 | } |
| 4492 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4493 | /* |
| 4494 | * |
| 4495 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4496 | * Class TXN |
| 4497 | * |
| 4498 | * |
| 4499 | */ |
| 4500 | |
| 4501 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4502 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4503 | */ |
| 4504 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 4505 | { |
| 4506 | return (struct hlua_txn *)MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
| 4507 | } |
| 4508 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 4509 | __LJMP static int hlua_set_var(lua_State *L) |
| 4510 | { |
| 4511 | struct hlua_txn *htxn; |
| 4512 | const char *name; |
| 4513 | size_t len; |
| 4514 | struct sample smp; |
| 4515 | |
| 4516 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 4517 | |
| 4518 | /* It is useles to retrieve the stream, but this function |
| 4519 | * runs only in a stream context. |
| 4520 | */ |
| 4521 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 4522 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4523 | |
| 4524 | /* Converts the third argument in a sample. */ |
| 4525 | hlua_lua2smp(L, 3, &smp); |
| 4526 | |
| 4527 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 4528 | smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR); |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 4529 | vars_set_by_name(name, len, &smp); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 4530 | return 0; |
| 4531 | } |
| 4532 | |
| 4533 | __LJMP static int hlua_get_var(lua_State *L) |
| 4534 | { |
| 4535 | struct hlua_txn *htxn; |
| 4536 | const char *name; |
| 4537 | size_t len; |
| 4538 | struct sample smp; |
| 4539 | |
| 4540 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4541 | |
| 4542 | /* It is useles to retrieve the stream, but this function |
| 4543 | * runs only in a stream context. |
| 4544 | */ |
| 4545 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 4546 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4547 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 4548 | smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR); |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 4549 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 4550 | lua_pushnil(L); |
| 4551 | return 1; |
| 4552 | } |
| 4553 | |
| 4554 | return hlua_smp2lua(L, &smp); |
| 4555 | } |
| 4556 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 4557 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 4558 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4559 | struct hlua *hlua; |
| 4560 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 4561 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4562 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4563 | /* It is useles to retrieve the stream, but this function |
| 4564 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 4565 | */ |
| 4566 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4567 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 4568 | |
| 4569 | /* Remove previous value. */ |
| 4570 | if (hlua->Mref != -1) |
| 4571 | luaL_unref(L, hlua->Mref, LUA_REGISTRYINDEX); |
| 4572 | |
| 4573 | /* Get and store new value. */ |
| 4574 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4575 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4576 | |
| 4577 | return 0; |
| 4578 | } |
| 4579 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 4580 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 4581 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4582 | struct hlua *hlua; |
| 4583 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 4584 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 4585 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4586 | /* It is useles to retrieve the stream, but this function |
| 4587 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 4588 | */ |
| 4589 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4590 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 4591 | |
| 4592 | /* Push configuration index in the stack. */ |
| 4593 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4594 | |
| 4595 | return 1; |
| 4596 | } |
| 4597 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4598 | /* Create stack entry containing a class TXN. This function |
| 4599 | * return 0 if the stack does not contains free slots, |
| 4600 | * otherwise it returns 1. |
| 4601 | */ |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 4602 | static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4603 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 4604 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4605 | |
| 4606 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 4607 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4608 | return 0; |
| 4609 | |
| 4610 | /* NOTE: The allocation never fails. The failure |
| 4611 | * throw an error, and the function never returns. |
| 4612 | * if the throw is not avalaible, the process is aborted. |
| 4613 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 4614 | /* Create the object: obj[0] = userdata. */ |
| 4615 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 4616 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 4617 | lua_rawseti(L, -2, 0); |
| 4618 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 4619 | htxn->s = s; |
| 4620 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 4621 | htxn->dir = dir; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4622 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4623 | /* Create the "f" field that contains a list of fetches. */ |
| 4624 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 4625 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4626 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 4627 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4628 | |
| 4629 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4630 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 4631 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP | HLUA_F_AS_STRING)) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4632 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 4633 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4634 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4635 | /* Create the "c" field that contains a list of converters. */ |
| 4636 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 4637 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4638 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 4639 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4640 | |
| 4641 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4642 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4643 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4644 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 4645 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4646 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 4647 | /* Create the "req" field that contains the request channel object. */ |
| 4648 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 4649 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 4650 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 4651 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 4652 | |
| 4653 | /* Create the "res" field that contains the response channel object. */ |
| 4654 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 4655 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 4656 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 4657 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 4658 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4659 | /* Creates the HTTP object is the current proxy allows http. */ |
| 4660 | lua_pushstring(L, "http"); |
| 4661 | if (p->mode == PR_MODE_HTTP) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 4662 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4663 | return 0; |
| 4664 | } |
| 4665 | else |
| 4666 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 4667 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4668 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4669 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 4670 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 4671 | lua_setmetatable(L, -2); |
| 4672 | |
| 4673 | return 1; |
| 4674 | } |
| 4675 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 4676 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 4677 | { |
| 4678 | const char *msg; |
| 4679 | struct hlua_txn *htxn; |
| 4680 | |
| 4681 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 4682 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 4683 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4684 | |
| 4685 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 4686 | return 0; |
| 4687 | } |
| 4688 | |
| 4689 | __LJMP static int hlua_txn_log(lua_State *L) |
| 4690 | { |
| 4691 | int level; |
| 4692 | const char *msg; |
| 4693 | struct hlua_txn *htxn; |
| 4694 | |
| 4695 | MAY_LJMP(check_args(L, 3, "log")); |
| 4696 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 4697 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4698 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 4699 | |
| 4700 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 4701 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 4702 | |
| 4703 | hlua_sendlog(htxn->s->be, level, msg); |
| 4704 | return 0; |
| 4705 | } |
| 4706 | |
| 4707 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 4708 | { |
| 4709 | const char *msg; |
| 4710 | struct hlua_txn *htxn; |
| 4711 | |
| 4712 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 4713 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 4714 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4715 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 4716 | return 0; |
| 4717 | } |
| 4718 | |
| 4719 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 4720 | { |
| 4721 | const char *msg; |
| 4722 | struct hlua_txn *htxn; |
| 4723 | |
| 4724 | MAY_LJMP(check_args(L, 2, "Info")); |
| 4725 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 4726 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4727 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 4728 | return 0; |
| 4729 | } |
| 4730 | |
| 4731 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 4732 | { |
| 4733 | const char *msg; |
| 4734 | struct hlua_txn *htxn; |
| 4735 | |
| 4736 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 4737 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 4738 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4739 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 4740 | return 0; |
| 4741 | } |
| 4742 | |
| 4743 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 4744 | { |
| 4745 | const char *msg; |
| 4746 | struct hlua_txn *htxn; |
| 4747 | |
| 4748 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 4749 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 4750 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4751 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 4752 | return 0; |
| 4753 | } |
| 4754 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 4755 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 4756 | { |
| 4757 | struct hlua_txn *htxn; |
| 4758 | int ll; |
| 4759 | |
| 4760 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 4761 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 4762 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4763 | |
| 4764 | if (ll < 0 || ll > 7) |
| 4765 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 4766 | |
| 4767 | htxn->s->logs.level = ll; |
| 4768 | return 0; |
| 4769 | } |
| 4770 | |
| 4771 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 4772 | { |
| 4773 | struct hlua_txn *htxn; |
| 4774 | struct connection *cli_conn; |
| 4775 | int tos; |
| 4776 | |
| 4777 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 4778 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 4779 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4780 | |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 4781 | 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] | 4782 | inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, tos); |
| 4783 | |
| 4784 | return 0; |
| 4785 | } |
| 4786 | |
| 4787 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 4788 | { |
| 4789 | #ifdef SO_MARK |
| 4790 | struct hlua_txn *htxn; |
| 4791 | struct connection *cli_conn; |
| 4792 | int mark; |
| 4793 | |
| 4794 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 4795 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 4796 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4797 | |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 4798 | 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] | 4799 | 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] | 4800 | #endif |
| 4801 | return 0; |
| 4802 | } |
| 4803 | |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 4804 | /* This function is an Lua binding that send pending data |
| 4805 | * to the client, and close the stream interface. |
| 4806 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 4807 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 4808 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4809 | struct hlua_txn *htxn; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 4810 | struct channel *ic, *oc; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 4811 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4812 | MAY_LJMP(check_args(L, 1, "close")); |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4813 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 4814 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4815 | ic = &htxn->s->req; |
| 4816 | oc = &htxn->s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 4817 | |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 4818 | if (htxn->s->txn) { |
| 4819 | /* HTTP mode, let's stay in sync with the stream */ |
| 4820 | bi_fast_delete(ic->buf, htxn->s->txn->req.sov); |
| 4821 | htxn->s->txn->req.next -= htxn->s->txn->req.sov; |
| 4822 | htxn->s->txn->req.sov = 0; |
| 4823 | ic->analysers &= AN_REQ_HTTP_XFER_BODY; |
| 4824 | oc->analysers = AN_RES_HTTP_XFER_BODY; |
| 4825 | htxn->s->txn->req.msg_state = HTTP_MSG_CLOSED; |
| 4826 | htxn->s->txn->rsp.msg_state = HTTP_MSG_DONE; |
| 4827 | |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 4828 | /* Note that if we want to support keep-alive, we need |
| 4829 | * to bypass the close/shutr_now calls below, but that |
| 4830 | * may only be done if the HTTP request was already |
| 4831 | * processed and the connection header is known (ie |
| 4832 | * not during TCP rules). |
| 4833 | */ |
| 4834 | } |
| 4835 | |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 4836 | channel_auto_read(ic); |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 4837 | channel_abort(ic); |
| 4838 | channel_auto_close(ic); |
| 4839 | channel_erase(ic); |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 4840 | |
| 4841 | oc->wex = tick_add_ifset(now_ms, oc->wto); |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 4842 | channel_auto_read(oc); |
| 4843 | channel_auto_close(oc); |
| 4844 | channel_shutr_now(oc); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 4845 | |
Willy Tarreau | 0458b08 | 2015-08-28 09:40:04 +0200 | [diff] [blame] | 4846 | ic->analysers = 0; |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 4847 | |
| 4848 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 4849 | return 0; |
| 4850 | } |
| 4851 | |
| 4852 | __LJMP static int hlua_log(lua_State *L) |
| 4853 | { |
| 4854 | int level; |
| 4855 | const char *msg; |
| 4856 | |
| 4857 | MAY_LJMP(check_args(L, 2, "log")); |
| 4858 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 4859 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4860 | |
| 4861 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 4862 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 4863 | |
| 4864 | hlua_sendlog(NULL, level, msg); |
| 4865 | return 0; |
| 4866 | } |
| 4867 | |
| 4868 | __LJMP static int hlua_log_debug(lua_State *L) |
| 4869 | { |
| 4870 | const char *msg; |
| 4871 | |
| 4872 | MAY_LJMP(check_args(L, 1, "debug")); |
| 4873 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 4874 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 4875 | return 0; |
| 4876 | } |
| 4877 | |
| 4878 | __LJMP static int hlua_log_info(lua_State *L) |
| 4879 | { |
| 4880 | const char *msg; |
| 4881 | |
| 4882 | MAY_LJMP(check_args(L, 1, "info")); |
| 4883 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 4884 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 4885 | return 0; |
| 4886 | } |
| 4887 | |
| 4888 | __LJMP static int hlua_log_warning(lua_State *L) |
| 4889 | { |
| 4890 | const char *msg; |
| 4891 | |
| 4892 | MAY_LJMP(check_args(L, 1, "warning")); |
| 4893 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 4894 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 4895 | return 0; |
| 4896 | } |
| 4897 | |
| 4898 | __LJMP static int hlua_log_alert(lua_State *L) |
| 4899 | { |
| 4900 | const char *msg; |
| 4901 | |
| 4902 | MAY_LJMP(check_args(L, 1, "alert")); |
| 4903 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 4904 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 4905 | return 0; |
| 4906 | } |
| 4907 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 4908 | __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] | 4909 | { |
| 4910 | int wakeup_ms = lua_tointeger(L, -1); |
| 4911 | if (now_ms < wakeup_ms) |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 4912 | 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] | 4913 | return 0; |
| 4914 | } |
| 4915 | |
| 4916 | __LJMP static int hlua_sleep(lua_State *L) |
| 4917 | { |
| 4918 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 4919 | unsigned int 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 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 4922 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 4923 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 4924 | wakeup_ms = tick_add(now_ms, delay); |
| 4925 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 4926 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 4927 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0)); |
| 4928 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 4929 | } |
| 4930 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 4931 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 4932 | { |
| 4933 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 4934 | unsigned int 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 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 4937 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 4938 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 4939 | wakeup_ms = tick_add(now_ms, delay); |
| 4940 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 4941 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 4942 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0)); |
| 4943 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 4944 | } |
| 4945 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 4946 | /* This functionis an LUA binding. it permits to give back |
| 4947 | * the hand at the HAProxy scheduler. It is used when the |
| 4948 | * LUA processing consumes a lot of time. |
| 4949 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 4950 | __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] | 4951 | { |
| 4952 | return 0; |
| 4953 | } |
| 4954 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 4955 | __LJMP static int hlua_yield(lua_State *L) |
| 4956 | { |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 4957 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD)); |
| 4958 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 4959 | } |
| 4960 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 4961 | /* This function change the nice of the currently executed |
| 4962 | * task. It is used set low or high priority at the current |
| 4963 | * task. |
| 4964 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 4965 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 4966 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4967 | struct hlua *hlua; |
| 4968 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 4969 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4970 | MAY_LJMP(check_args(L, 1, "set_nice")); |
| 4971 | hlua = hlua_gethlua(L); |
| 4972 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 4973 | |
| 4974 | /* If he task is not set, I'm in a start mode. */ |
| 4975 | if (!hlua || !hlua->task) |
| 4976 | return 0; |
| 4977 | |
| 4978 | if (nice < -1024) |
| 4979 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4980 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 4981 | nice = 1024; |
| 4982 | |
| 4983 | hlua->task->nice = nice; |
| 4984 | return 0; |
| 4985 | } |
| 4986 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 4987 | /* This function is used as a calback of a task. It is called by the |
| 4988 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 4989 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 4990 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 4991 | * |
| 4992 | * Task wrapper are longjmp safe because the only one Lua code |
| 4993 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 4994 | */ |
| 4995 | static struct task *hlua_process_task(struct task *task) |
| 4996 | { |
| 4997 | struct hlua *hlua = task->context; |
| 4998 | enum hlua_exec status; |
| 4999 | |
| 5000 | /* We need to remove the task from the wait queue before executing |
| 5001 | * the Lua code because we don't know if it needs to wait for |
| 5002 | * another timer or not in the case of E_AGAIN. |
| 5003 | */ |
| 5004 | task_delete(task); |
| 5005 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5006 | /* If it is the first call to the task, we must initialize the |
| 5007 | * execution timeouts. |
| 5008 | */ |
| 5009 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 5010 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5011 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5012 | /* Execute the Lua code. */ |
| 5013 | status = hlua_ctx_resume(hlua, 1); |
| 5014 | |
| 5015 | switch (status) { |
| 5016 | /* finished or yield */ |
| 5017 | case HLUA_E_OK: |
| 5018 | hlua_ctx_destroy(hlua); |
| 5019 | task_delete(task); |
| 5020 | task_free(task); |
| 5021 | break; |
| 5022 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 5023 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
| 5024 | if (hlua->wake_time != TICK_ETERNITY) |
| 5025 | task_schedule(task, hlua->wake_time); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5026 | break; |
| 5027 | |
| 5028 | /* finished with error. */ |
| 5029 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5030 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5031 | hlua_ctx_destroy(hlua); |
| 5032 | task_delete(task); |
| 5033 | task_free(task); |
| 5034 | break; |
| 5035 | |
| 5036 | case HLUA_E_ERR: |
| 5037 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5038 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5039 | hlua_ctx_destroy(hlua); |
| 5040 | task_delete(task); |
| 5041 | task_free(task); |
| 5042 | break; |
| 5043 | } |
| 5044 | return NULL; |
| 5045 | } |
| 5046 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 5047 | /* This function is an LUA binding that register LUA function to be |
| 5048 | * executed after the HAProxy configuration parsing and before the |
| 5049 | * HAProxy scheduler starts. This function expect only one LUA |
| 5050 | * argument that is a function. This function returns nothing, but |
| 5051 | * throws if an error is encountered. |
| 5052 | */ |
| 5053 | __LJMP static int hlua_register_init(lua_State *L) |
| 5054 | { |
| 5055 | struct hlua_init_function *init; |
| 5056 | int ref; |
| 5057 | |
| 5058 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 5059 | |
| 5060 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 5061 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5062 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 5063 | if (!init) |
| 5064 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5065 | |
| 5066 | init->function_ref = ref; |
| 5067 | LIST_ADDQ(&hlua_init_functions, &init->l); |
| 5068 | return 0; |
| 5069 | } |
| 5070 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5071 | /* This functio is an LUA binding. It permits to register a task |
| 5072 | * executed in parallel of the main HAroxy activity. The task is |
| 5073 | * created and it is set in the HAProxy scheduler. It can be called |
| 5074 | * from the "init" section, "post init" or during the runtime. |
| 5075 | * |
| 5076 | * Lua prototype: |
| 5077 | * |
| 5078 | * <none> core.register_task(<function>) |
| 5079 | */ |
| 5080 | static int hlua_register_task(lua_State *L) |
| 5081 | { |
| 5082 | struct hlua *hlua; |
| 5083 | struct task *task; |
| 5084 | int ref; |
| 5085 | |
| 5086 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 5087 | |
| 5088 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 5089 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5090 | hlua = calloc(1, sizeof(*hlua)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5091 | if (!hlua) |
| 5092 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5093 | |
| 5094 | task = task_new(); |
| 5095 | task->context = hlua; |
| 5096 | task->process = hlua_process_task; |
| 5097 | |
| 5098 | if (!hlua_ctx_init(hlua, task)) |
| 5099 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5100 | |
| 5101 | /* Restore the function in the stack. */ |
| 5102 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 5103 | hlua->nargs = 0; |
| 5104 | |
| 5105 | /* Schedule task. */ |
| 5106 | task_schedule(task, now_ms); |
| 5107 | |
| 5108 | return 0; |
| 5109 | } |
| 5110 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5111 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 5112 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 5113 | * resume converters. |
| 5114 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5115 | 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] | 5116 | { |
| 5117 | struct hlua_function *fcn = (struct hlua_function *)private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5118 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5119 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5120 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5121 | if (!stream) |
| 5122 | return 0; |
| 5123 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5124 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5125 | * Lua context can be not initialized. This behavior |
| 5126 | * permits to save performances because a systematic |
| 5127 | * Lua initialization cause 5% performances loss. |
| 5128 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5129 | if (!stream->hlua.T && !hlua_ctx_init(&stream->hlua, stream->task)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5130 | 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] | 5131 | return 0; |
| 5132 | } |
| 5133 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5134 | /* If it is the first run, initialize the data for the call. */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5135 | if (!HLUA_IS_RUNNING(&stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5136 | |
| 5137 | /* The following Lua calls can fail. */ |
| 5138 | if (!SET_SAFE_LJMP(stream->hlua.T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5139 | if (lua_type(stream->hlua.T, -1) == LUA_TSTRING) |
| 5140 | error = lua_tostring(stream->hlua.T, -1); |
| 5141 | else |
| 5142 | error = "critical error"; |
| 5143 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5144 | return 0; |
| 5145 | } |
| 5146 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5147 | /* Check stack available size. */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5148 | if (!lua_checkstack(stream->hlua.T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5149 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 10e5bc7 | 2015-09-25 23:51:34 +0200 | [diff] [blame] | 5150 | RESET_SAFE_LJMP(stream->hlua.T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5151 | return 0; |
| 5152 | } |
| 5153 | |
| 5154 | /* Restore the function in the stack. */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5155 | lua_rawgeti(stream->hlua.T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5156 | |
| 5157 | /* convert input sample and pust-it in the stack. */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5158 | if (!lua_checkstack(stream->hlua.T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5159 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 10e5bc7 | 2015-09-25 23:51:34 +0200 | [diff] [blame] | 5160 | RESET_SAFE_LJMP(stream->hlua.T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5161 | return 0; |
| 5162 | } |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5163 | hlua_smp2lua(stream->hlua.T, smp); |
| 5164 | stream->hlua.nargs = 2; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5165 | |
| 5166 | /* push keywords in the stack. */ |
| 5167 | if (arg_p) { |
| 5168 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5169 | if (!lua_checkstack(stream->hlua.T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5170 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 10e5bc7 | 2015-09-25 23:51:34 +0200 | [diff] [blame] | 5171 | RESET_SAFE_LJMP(stream->hlua.T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5172 | return 0; |
| 5173 | } |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5174 | hlua_arg2lua(stream->hlua.T, arg_p); |
| 5175 | stream->hlua.nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5176 | } |
| 5177 | } |
| 5178 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5179 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 5180 | stream->hlua.max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5181 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5182 | /* At this point the execution is safe. */ |
| 5183 | RESET_SAFE_LJMP(stream->hlua.T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5184 | } |
| 5185 | |
| 5186 | /* Execute the function. */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5187 | switch (hlua_ctx_resume(&stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5188 | /* finished. */ |
| 5189 | case HLUA_E_OK: |
| 5190 | /* Convert the returned value in sample. */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5191 | hlua_lua2smp(stream->hlua.T, -1, smp); |
| 5192 | lua_pop(stream->hlua.T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5193 | return 1; |
| 5194 | |
| 5195 | /* yield. */ |
| 5196 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5197 | 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] | 5198 | return 0; |
| 5199 | |
| 5200 | /* finished with error. */ |
| 5201 | case HLUA_E_ERRMSG: |
| 5202 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5203 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
| 5204 | fcn->name, lua_tostring(stream->hlua.T, -1)); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5205 | lua_pop(stream->hlua.T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5206 | return 0; |
| 5207 | |
| 5208 | case HLUA_E_ERR: |
| 5209 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5210 | 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] | 5211 | |
| 5212 | default: |
| 5213 | return 0; |
| 5214 | } |
| 5215 | } |
| 5216 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5217 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 5218 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5219 | * resume sample-fetches. This function will be called by the sample |
| 5220 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5221 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5222 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 5223 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5224 | { |
| 5225 | struct hlua_function *fcn = (struct hlua_function *)private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5226 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5227 | const char *error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5228 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5229 | if (!stream) |
| 5230 | return 0; |
| 5231 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5232 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5233 | * Lua context can be not initialized. This behavior |
| 5234 | * permits to save performances because a systematic |
| 5235 | * Lua initialization cause 5% performances loss. |
| 5236 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5237 | if (!stream->hlua.T && !hlua_ctx_init(&stream->hlua, stream->task)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5238 | 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] | 5239 | return 0; |
| 5240 | } |
| 5241 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5242 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5243 | if (!HLUA_IS_RUNNING(&stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5244 | |
| 5245 | /* The following Lua calls can fail. */ |
| 5246 | if (!SET_SAFE_LJMP(stream->hlua.T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5247 | if (lua_type(stream->hlua.T, -1) == LUA_TSTRING) |
| 5248 | error = lua_tostring(stream->hlua.T, -1); |
| 5249 | else |
| 5250 | error = "critical error"; |
| 5251 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5252 | return 0; |
| 5253 | } |
| 5254 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5255 | /* Check stack available size. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5256 | if (!lua_checkstack(stream->hlua.T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5257 | 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] | 5258 | RESET_SAFE_LJMP(stream->hlua.T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5259 | return 0; |
| 5260 | } |
| 5261 | |
| 5262 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5263 | lua_rawgeti(stream->hlua.T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5264 | |
| 5265 | /* push arguments in the stack. */ |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 5266 | if (!hlua_txn_new(stream->hlua.T, stream, smp->px, smp->opt & SMP_OPT_DIR)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5267 | 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] | 5268 | RESET_SAFE_LJMP(stream->hlua.T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5269 | return 0; |
| 5270 | } |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5271 | stream->hlua.nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5272 | |
| 5273 | /* push keywords in the stack. */ |
| 5274 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 5275 | /* Check stack available size. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5276 | if (!lua_checkstack(stream->hlua.T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5277 | 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] | 5278 | RESET_SAFE_LJMP(stream->hlua.T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5279 | return 0; |
| 5280 | } |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5281 | if (!lua_checkstack(stream->hlua.T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5282 | 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] | 5283 | RESET_SAFE_LJMP(stream->hlua.T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5284 | return 0; |
| 5285 | } |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5286 | hlua_arg2lua(stream->hlua.T, arg_p); |
| 5287 | stream->hlua.nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5288 | } |
| 5289 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5290 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 5291 | stream->hlua.max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5292 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5293 | /* At this point the execution is safe. */ |
| 5294 | RESET_SAFE_LJMP(stream->hlua.T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5295 | } |
| 5296 | |
| 5297 | /* Execute the function. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5298 | switch (hlua_ctx_resume(&stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5299 | /* finished. */ |
| 5300 | case HLUA_E_OK: |
Thierry FOURNIER | 26a7aac | 2015-10-13 14:25:11 +0200 | [diff] [blame] | 5301 | 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] | 5302 | return 0; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5303 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5304 | hlua_lua2smp(stream->hlua.T, -1, smp); |
| 5305 | lua_pop(stream->hlua.T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5306 | |
| 5307 | /* Set the end of execution flag. */ |
| 5308 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 5309 | return 1; |
| 5310 | |
| 5311 | /* yield. */ |
| 5312 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 26a7aac | 2015-10-13 14:25:11 +0200 | [diff] [blame] | 5313 | 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] | 5314 | 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] | 5315 | return 0; |
| 5316 | |
| 5317 | /* finished with error. */ |
| 5318 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 26a7aac | 2015-10-13 14:25:11 +0200 | [diff] [blame] | 5319 | 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] | 5320 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5321 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
| 5322 | fcn->name, lua_tostring(stream->hlua.T, -1)); |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5323 | lua_pop(stream->hlua.T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5324 | return 0; |
| 5325 | |
| 5326 | case HLUA_E_ERR: |
Thierry FOURNIER | 26a7aac | 2015-10-13 14:25:11 +0200 | [diff] [blame] | 5327 | 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] | 5328 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5329 | 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] | 5330 | |
| 5331 | default: |
| 5332 | return 0; |
| 5333 | } |
| 5334 | } |
| 5335 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5336 | /* This function is an LUA binding used for registering |
| 5337 | * "sample-conv" functions. It expects a converter name used |
| 5338 | * in the haproxy configuration file, and an LUA function. |
| 5339 | */ |
| 5340 | __LJMP static int hlua_register_converters(lua_State *L) |
| 5341 | { |
| 5342 | struct sample_conv_kw_list *sck; |
| 5343 | const char *name; |
| 5344 | int ref; |
| 5345 | int len; |
| 5346 | struct hlua_function *fcn; |
| 5347 | |
| 5348 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 5349 | |
| 5350 | /* First argument : converter name. */ |
| 5351 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5352 | |
| 5353 | /* Second argument : lua function. */ |
| 5354 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 5355 | |
| 5356 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5357 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5358 | if (!sck) |
| 5359 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5360 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5361 | if (!fcn) |
| 5362 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5363 | |
| 5364 | /* Fill fcn. */ |
| 5365 | fcn->name = strdup(name); |
| 5366 | if (!fcn->name) |
| 5367 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5368 | fcn->function_ref = ref; |
| 5369 | |
| 5370 | /* List head */ |
| 5371 | sck->list.n = sck->list.p = NULL; |
| 5372 | |
| 5373 | /* converter keyword. */ |
| 5374 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5375 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5376 | if (!sck->kw[0].kw) |
| 5377 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5378 | |
| 5379 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 5380 | sck->kw[0].process = hlua_sample_conv_wrapper; |
| 5381 | sck->kw[0].arg_mask = ARG5(0,STR,STR,STR,STR,STR); |
| 5382 | sck->kw[0].val_args = NULL; |
| 5383 | sck->kw[0].in_type = SMP_T_STR; |
| 5384 | sck->kw[0].out_type = SMP_T_STR; |
| 5385 | sck->kw[0].private = fcn; |
| 5386 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5387 | /* Register this new converter */ |
| 5388 | sample_register_convs(sck); |
| 5389 | |
| 5390 | return 0; |
| 5391 | } |
| 5392 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5393 | /* This fucntion is an LUA binding used for registering |
| 5394 | * "sample-fetch" functions. It expects a converter name used |
| 5395 | * in the haproxy configuration file, and an LUA function. |
| 5396 | */ |
| 5397 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 5398 | { |
| 5399 | const char *name; |
| 5400 | int ref; |
| 5401 | int len; |
| 5402 | struct sample_fetch_kw_list *sfk; |
| 5403 | struct hlua_function *fcn; |
| 5404 | |
| 5405 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 5406 | |
| 5407 | /* First argument : sample-fetch name. */ |
| 5408 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5409 | |
| 5410 | /* Second argument : lua function. */ |
| 5411 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 5412 | |
| 5413 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5414 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5415 | if (!sfk) |
| 5416 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5417 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5418 | if (!fcn) |
| 5419 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5420 | |
| 5421 | /* Fill fcn. */ |
| 5422 | fcn->name = strdup(name); |
| 5423 | if (!fcn->name) |
| 5424 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5425 | fcn->function_ref = ref; |
| 5426 | |
| 5427 | /* List head */ |
| 5428 | sfk->list.n = sfk->list.p = NULL; |
| 5429 | |
| 5430 | /* sample-fetch keyword. */ |
| 5431 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5432 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5433 | if (!sfk->kw[0].kw) |
| 5434 | return luaL_error(L, "lua out of memory error."); |
| 5435 | |
| 5436 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 5437 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
| 5438 | sfk->kw[0].arg_mask = ARG5(0,STR,STR,STR,STR,STR); |
| 5439 | sfk->kw[0].val_args = NULL; |
| 5440 | sfk->kw[0].out_type = SMP_T_STR; |
| 5441 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 5442 | sfk->kw[0].val = 0; |
| 5443 | sfk->kw[0].private = fcn; |
| 5444 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5445 | /* Register this new fetch. */ |
| 5446 | sample_register_fetches(sfk); |
| 5447 | |
| 5448 | return 0; |
| 5449 | } |
| 5450 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5451 | /* This function is a wrapper to execute each LUA function declared |
| 5452 | * as an action wrapper during the initialisation period. This function |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5453 | * return ACT_RET_CONT if the processing is finished (with or without |
| 5454 | * error) and return ACT_RET_YIELD if the function must be called again |
| 5455 | * because the LUA returns a yield. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5456 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5457 | 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] | 5458 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5459 | { |
| 5460 | char **arg; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5461 | unsigned int analyzer; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 5462 | int dir; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5463 | const char *error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5464 | |
| 5465 | switch (rule->from) { |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 5466 | case ACT_F_TCP_REQ_CNT: analyzer = AN_REQ_INSPECT_FE ; dir = SMP_OPT_DIR_REQ; break; |
| 5467 | case ACT_F_TCP_RES_CNT: analyzer = AN_RES_INSPECT ; dir = SMP_OPT_DIR_RES; break; |
| 5468 | case ACT_F_HTTP_REQ: analyzer = AN_REQ_HTTP_PROCESS_FE; dir = SMP_OPT_DIR_REQ; break; |
| 5469 | case ACT_F_HTTP_RES: analyzer = AN_RES_HTTP_PROCESS_BE; dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5470 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5471 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5472 | return ACT_RET_CONT; |
| 5473 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5474 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5475 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5476 | * Lua context can be not initialized. This behavior |
| 5477 | * permits to save performances because a systematic |
| 5478 | * Lua initialization cause 5% performances loss. |
| 5479 | */ |
| 5480 | if (!s->hlua.T && !hlua_ctx_init(&s->hlua, s->task)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5481 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5482 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 5483 | return ACT_RET_CONT; |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5484 | } |
| 5485 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5486 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 5487 | if (!HLUA_IS_RUNNING(&s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5488 | |
| 5489 | /* The following Lua calls can fail. */ |
| 5490 | if (!SET_SAFE_LJMP(s->hlua.T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5491 | if (lua_type(s->hlua.T, -1) == LUA_TSTRING) |
| 5492 | error = lua_tostring(s->hlua.T, -1); |
| 5493 | else |
| 5494 | error = "critical error"; |
| 5495 | SEND_ERR(px, "Lua function '%s': %s.\n", |
| 5496 | rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5497 | return ACT_RET_CONT; |
| 5498 | } |
| 5499 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5500 | /* Check stack available size. */ |
| 5501 | if (!lua_checkstack(s->hlua.T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5502 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5503 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 10e5bc7 | 2015-09-25 23:51:34 +0200 | [diff] [blame] | 5504 | RESET_SAFE_LJMP(s->hlua.T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 5505 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5506 | } |
| 5507 | |
| 5508 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5509 | 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] | 5510 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5511 | /* Create and and push object stream in the stack. */ |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 5512 | if (!hlua_txn_new(s->hlua.T, s, px, dir)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5513 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5514 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 10e5bc7 | 2015-09-25 23:51:34 +0200 | [diff] [blame] | 5515 | RESET_SAFE_LJMP(s->hlua.T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 5516 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5517 | } |
| 5518 | s->hlua.nargs = 1; |
| 5519 | |
| 5520 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5521 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5522 | if (!lua_checkstack(s->hlua.T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5523 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5524 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 10e5bc7 | 2015-09-25 23:51:34 +0200 | [diff] [blame] | 5525 | RESET_SAFE_LJMP(s->hlua.T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 5526 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5527 | } |
| 5528 | lua_pushstring(s->hlua.T, *arg); |
| 5529 | s->hlua.nargs++; |
| 5530 | } |
| 5531 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5532 | /* Now the execution is safe. */ |
| 5533 | RESET_SAFE_LJMP(s->hlua.T); |
| 5534 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5535 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 5536 | s->hlua.max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5537 | } |
| 5538 | |
| 5539 | /* Execute the function. */ |
Willy Tarreau | 528192d | 2015-09-27 10:48:01 +0200 | [diff] [blame] | 5540 | switch (hlua_ctx_resume(&s->hlua, !(flags & ACT_FLAG_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5541 | /* finished. */ |
| 5542 | case HLUA_E_OK: |
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 | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 5545 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5546 | |
| 5547 | /* yield. */ |
| 5548 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 5549 | /* Set timeout in the required channel. */ |
| 5550 | if (s->hlua.wake_time != TICK_ETERNITY) { |
| 5551 | if (analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5552 | s->req.analyse_exp = s->hlua.wake_time; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 5553 | else if (analyzer & (AN_RES_INSPECT|AN_RES_HTTP_PROCESS_BE)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5554 | s->res.analyse_exp = s->hlua.wake_time; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 5555 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5556 | /* Some actions can be wake up when a "write" event |
| 5557 | * is detected on a response channel. This is useful |
| 5558 | * only for actions targetted on the requests. |
| 5559 | */ |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 5560 | if (HLUA_IS_WAKERESWR(&s->hlua)) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5561 | s->res.flags |= CF_WAKE_WRITE; |
Willy Tarreau | 76bd97f | 2015-03-10 17:16:10 +0100 | [diff] [blame] | 5562 | if ((analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE))) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5563 | s->res.analysers |= analyzer; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5564 | } |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 5565 | if (HLUA_IS_WAKEREQWR(&s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5566 | s->req.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 5567 | return ACT_RET_YIELD; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5568 | |
| 5569 | /* finished with error. */ |
| 5570 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 5571 | if (!hlua_check_proto(s, dir)) |
| 5572 | return ACT_RET_ERR; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5573 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5574 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5575 | rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua.T, -1)); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5576 | lua_pop(s->hlua.T, 1); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 5577 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5578 | |
| 5579 | case HLUA_E_ERR: |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 5580 | if (!hlua_check_proto(s, dir)) |
| 5581 | return ACT_RET_ERR; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5582 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5583 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5584 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5585 | |
| 5586 | default: |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 5587 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5588 | } |
| 5589 | } |
| 5590 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 5591 | struct task *hlua_applet_wakeup(struct task *t) |
| 5592 | { |
| 5593 | struct appctx *ctx = t->context; |
| 5594 | struct stream_interface *si = ctx->owner; |
| 5595 | |
| 5596 | /* If the applet is wake up without any expected work, the sheduler |
| 5597 | * remove it from the run queue. This flag indicate that the applet |
| 5598 | * is waiting for write. If the buffer is full, the main processing |
| 5599 | * will send some data and after call the applet, otherwise it call |
| 5600 | * the applet ASAP. |
| 5601 | */ |
| 5602 | si_applet_cant_put(si); |
| 5603 | appctx_wakeup(ctx); |
| 5604 | return NULL; |
| 5605 | } |
| 5606 | |
| 5607 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 5608 | { |
| 5609 | struct stream_interface *si = ctx->owner; |
| 5610 | struct hlua *hlua = &ctx->ctx.hlua_apptcp.hlua; |
| 5611 | struct task *task; |
| 5612 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5613 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 5614 | |
| 5615 | HLUA_INIT(hlua); |
| 5616 | ctx->ctx.hlua_apptcp.flags = 0; |
| 5617 | |
| 5618 | /* Create task used by signal to wakeup applets. */ |
| 5619 | task = task_new(); |
| 5620 | if (!task) { |
| 5621 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 5622 | ctx->rule->arg.hlua_rule->fcn.name); |
| 5623 | return 0; |
| 5624 | } |
| 5625 | task->nice = 0; |
| 5626 | task->context = ctx; |
| 5627 | task->process = hlua_applet_wakeup; |
| 5628 | ctx->ctx.hlua_apptcp.task = task; |
| 5629 | |
| 5630 | /* In the execution wrappers linked with a stream, the |
| 5631 | * Lua context can be not initialized. This behavior |
| 5632 | * permits to save performances because a systematic |
| 5633 | * Lua initialization cause 5% performances loss. |
| 5634 | */ |
| 5635 | if (!hlua_ctx_init(hlua, task)) { |
| 5636 | SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n", |
| 5637 | ctx->rule->arg.hlua_rule->fcn.name); |
| 5638 | return 0; |
| 5639 | } |
| 5640 | |
| 5641 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 5642 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 5643 | |
| 5644 | /* The following Lua calls can fail. */ |
| 5645 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5646 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 5647 | error = lua_tostring(hlua->T, -1); |
| 5648 | else |
| 5649 | error = "critical error"; |
| 5650 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 5651 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 5652 | RESET_SAFE_LJMP(hlua->T); |
| 5653 | return 0; |
| 5654 | } |
| 5655 | |
| 5656 | /* Check stack available size. */ |
| 5657 | if (!lua_checkstack(hlua->T, 1)) { |
| 5658 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 5659 | ctx->rule->arg.hlua_rule->fcn.name); |
| 5660 | RESET_SAFE_LJMP(hlua->T); |
| 5661 | return 0; |
| 5662 | } |
| 5663 | |
| 5664 | /* Restore the function in the stack. */ |
| 5665 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 5666 | |
| 5667 | /* Create and and push object stream in the stack. */ |
| 5668 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 5669 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 5670 | ctx->rule->arg.hlua_rule->fcn.name); |
| 5671 | RESET_SAFE_LJMP(hlua->T); |
| 5672 | return 0; |
| 5673 | } |
| 5674 | hlua->nargs = 1; |
| 5675 | |
| 5676 | /* push keywords in the stack. */ |
| 5677 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 5678 | if (!lua_checkstack(hlua->T, 1)) { |
| 5679 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 5680 | ctx->rule->arg.hlua_rule->fcn.name); |
| 5681 | RESET_SAFE_LJMP(hlua->T); |
| 5682 | return 0; |
| 5683 | } |
| 5684 | lua_pushstring(hlua->T, *arg); |
| 5685 | hlua->nargs++; |
| 5686 | } |
| 5687 | |
| 5688 | RESET_SAFE_LJMP(hlua->T); |
| 5689 | |
| 5690 | /* Wakeup the applet ASAP. */ |
| 5691 | si_applet_cant_get(si); |
| 5692 | si_applet_cant_put(si); |
| 5693 | |
| 5694 | return 1; |
| 5695 | } |
| 5696 | |
| 5697 | static void hlua_applet_tcp_fct(struct appctx *ctx) |
| 5698 | { |
| 5699 | struct stream_interface *si = ctx->owner; |
| 5700 | struct stream *strm = si_strm(si); |
| 5701 | struct channel *res = si_ic(si); |
| 5702 | struct act_rule *rule = ctx->rule; |
| 5703 | struct proxy *px = strm->be; |
| 5704 | struct hlua *hlua = &ctx->ctx.hlua_apptcp.hlua; |
| 5705 | |
| 5706 | /* The applet execution is already done. */ |
| 5707 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) |
| 5708 | return; |
| 5709 | |
| 5710 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 5711 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 5712 | return; |
| 5713 | |
| 5714 | /* Execute the function. */ |
| 5715 | switch (hlua_ctx_resume(hlua, 1)) { |
| 5716 | /* finished. */ |
| 5717 | case HLUA_E_OK: |
| 5718 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 5719 | |
| 5720 | /* log time */ |
| 5721 | strm->logs.tv_request = now; |
| 5722 | |
| 5723 | /* eat the whole request */ |
| 5724 | bo_skip(si_oc(si), si_ob(si)->o); |
| 5725 | res->flags |= CF_READ_NULL; |
| 5726 | si_shutr(si); |
| 5727 | return; |
| 5728 | |
| 5729 | /* yield. */ |
| 5730 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 5731 | if (hlua->wake_time != TICK_ETERNITY) |
| 5732 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 5733 | return; |
| 5734 | |
| 5735 | /* finished with error. */ |
| 5736 | case HLUA_E_ERRMSG: |
| 5737 | /* Display log. */ |
| 5738 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 5739 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 5740 | lua_pop(hlua->T, 1); |
| 5741 | goto error; |
| 5742 | |
| 5743 | case HLUA_E_ERR: |
| 5744 | /* Display log. */ |
| 5745 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
| 5746 | rule->arg.hlua_rule->fcn.name); |
| 5747 | goto error; |
| 5748 | |
| 5749 | default: |
| 5750 | goto error; |
| 5751 | } |
| 5752 | |
| 5753 | error: |
| 5754 | |
| 5755 | /* For all other cases, just close the stream. */ |
| 5756 | si_shutw(si); |
| 5757 | si_shutr(si); |
| 5758 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 5759 | } |
| 5760 | |
| 5761 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 5762 | { |
| 5763 | task_free(ctx->ctx.hlua_apptcp.task); |
| 5764 | ctx->ctx.hlua_apptcp.task = NULL; |
| 5765 | hlua_ctx_destroy(&ctx->ctx.hlua_apptcp.hlua); |
| 5766 | } |
| 5767 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5768 | /* The function returns 1 if the initialisation is complete, 0 if |
| 5769 | * an errors occurs and -1 if more data are required for initializing |
| 5770 | * the applet. |
| 5771 | */ |
| 5772 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 5773 | { |
| 5774 | struct stream_interface *si = ctx->owner; |
| 5775 | struct channel *req = si_oc(si); |
| 5776 | struct http_msg *msg; |
| 5777 | struct http_txn *txn; |
| 5778 | struct hlua *hlua = &ctx->ctx.hlua_apphttp.hlua; |
| 5779 | char **arg; |
| 5780 | struct hdr_ctx hdr; |
| 5781 | struct task *task; |
| 5782 | struct sample smp; /* just used for a valid call to smp_prefetch_http. */ |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5783 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5784 | |
| 5785 | /* Wait for a full HTTP request. */ |
| 5786 | if (!smp_prefetch_http(px, strm, 0, NULL, &smp, 0)) { |
| 5787 | if (smp.flags & SMP_F_MAY_CHANGE) |
| 5788 | return -1; |
| 5789 | return 0; |
| 5790 | } |
| 5791 | txn = strm->txn; |
| 5792 | msg = &txn->req; |
| 5793 | |
Willy Tarreau | 0078bfc | 2015-10-07 20:20:28 +0200 | [diff] [blame] | 5794 | /* We want two things in HTTP mode : |
| 5795 | * - enforce server-close mode if we were in keep-alive, so that the |
| 5796 | * applet is released after each response ; |
| 5797 | * - enable request body transfer to the applet in order to resync |
| 5798 | * with the response body. |
| 5799 | */ |
| 5800 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) |
| 5801 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | 0078bfc | 2015-10-07 20:20:28 +0200 | [diff] [blame] | 5802 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5803 | HLUA_INIT(hlua); |
| 5804 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 5805 | ctx->ctx.hlua_apphttp.flags = 0; |
| 5806 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 5807 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 5808 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 5809 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5810 | /* Create task used by signal to wakeup applets. */ |
| 5811 | task = task_new(); |
| 5812 | if (!task) { |
| 5813 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 5814 | ctx->rule->arg.hlua_rule->fcn.name); |
| 5815 | return 0; |
| 5816 | } |
| 5817 | task->nice = 0; |
| 5818 | task->context = ctx; |
| 5819 | task->process = hlua_applet_wakeup; |
| 5820 | ctx->ctx.hlua_apphttp.task = task; |
| 5821 | |
| 5822 | /* In the execution wrappers linked with a stream, the |
| 5823 | * Lua context can be not initialized. This behavior |
| 5824 | * permits to save performances because a systematic |
| 5825 | * Lua initialization cause 5% performances loss. |
| 5826 | */ |
| 5827 | if (!hlua_ctx_init(hlua, task)) { |
| 5828 | SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n", |
| 5829 | ctx->rule->arg.hlua_rule->fcn.name); |
| 5830 | return 0; |
| 5831 | } |
| 5832 | |
| 5833 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 5834 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5835 | |
| 5836 | /* The following Lua calls can fail. */ |
| 5837 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5838 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 5839 | error = lua_tostring(hlua->T, -1); |
| 5840 | else |
| 5841 | error = "critical error"; |
| 5842 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 5843 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5844 | return 0; |
| 5845 | } |
| 5846 | |
| 5847 | /* Check stack available size. */ |
| 5848 | if (!lua_checkstack(hlua->T, 1)) { |
| 5849 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 5850 | ctx->rule->arg.hlua_rule->fcn.name); |
| 5851 | RESET_SAFE_LJMP(hlua->T); |
| 5852 | return 0; |
| 5853 | } |
| 5854 | |
| 5855 | /* Restore the function in the stack. */ |
| 5856 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 5857 | |
| 5858 | /* Create and and push object stream in the stack. */ |
| 5859 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 5860 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 5861 | ctx->rule->arg.hlua_rule->fcn.name); |
| 5862 | RESET_SAFE_LJMP(hlua->T); |
| 5863 | return 0; |
| 5864 | } |
| 5865 | hlua->nargs = 1; |
| 5866 | |
| 5867 | /* Look for a 100-continue expected. */ |
| 5868 | if (msg->flags & HTTP_MSGF_VER_11) { |
| 5869 | hdr.idx = 0; |
| 5870 | if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &hdr) && |
| 5871 | unlikely(hdr.vlen == 12 && strncasecmp(hdr.line+hdr.val, "100-continue", 12) == 0)) |
| 5872 | ctx->ctx.hlua_apphttp.flags |= APPLET_100C; |
| 5873 | } |
| 5874 | |
| 5875 | /* push keywords in the stack. */ |
| 5876 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 5877 | if (!lua_checkstack(hlua->T, 1)) { |
| 5878 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 5879 | ctx->rule->arg.hlua_rule->fcn.name); |
| 5880 | RESET_SAFE_LJMP(hlua->T); |
| 5881 | return 0; |
| 5882 | } |
| 5883 | lua_pushstring(hlua->T, *arg); |
| 5884 | hlua->nargs++; |
| 5885 | } |
| 5886 | |
| 5887 | RESET_SAFE_LJMP(hlua->T); |
| 5888 | |
| 5889 | /* Wakeup the applet when data is ready for read. */ |
| 5890 | si_applet_cant_get(si); |
| 5891 | |
| 5892 | return 1; |
| 5893 | } |
| 5894 | |
| 5895 | static void hlua_applet_http_fct(struct appctx *ctx) |
| 5896 | { |
| 5897 | struct stream_interface *si = ctx->owner; |
| 5898 | struct stream *strm = si_strm(si); |
| 5899 | struct channel *res = si_ic(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5900 | struct act_rule *rule = ctx->rule; |
| 5901 | struct proxy *px = strm->be; |
| 5902 | struct hlua *hlua = &ctx->ctx.hlua_apphttp.hlua; |
| 5903 | char *blk1; |
| 5904 | int len1; |
| 5905 | char *blk2; |
| 5906 | int len2; |
| 5907 | int ret; |
| 5908 | |
| 5909 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 5910 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 5911 | return; |
| 5912 | |
| 5913 | /* Set the currently running flag. */ |
| 5914 | if (!HLUA_IS_RUNNING(hlua) && |
| 5915 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 5916 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5917 | /* Wait for full HTTP analysys. */ |
| 5918 | if (unlikely(strm->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 5919 | si_applet_cant_get(si); |
| 5920 | return; |
| 5921 | } |
| 5922 | |
| 5923 | /* Store the max amount of bytes that we can read. */ |
| 5924 | ctx->ctx.hlua_apphttp.left_bytes = strm->txn->req.body_len; |
| 5925 | |
| 5926 | /* We need to flush the request header. This left the body |
| 5927 | * for the Lua. |
| 5928 | */ |
| 5929 | |
| 5930 | /* Read the maximum amount of data avalaible. */ |
| 5931 | ret = bo_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
| 5932 | if (ret == -1) |
| 5933 | return; |
| 5934 | |
| 5935 | /* No data available, ask for more data. */ |
| 5936 | if (ret == 1) |
| 5937 | len2 = 0; |
| 5938 | if (ret == 0) |
| 5939 | len1 = 0; |
| 5940 | if (len1 + len2 < strm->txn->req.eoh + 2) { |
| 5941 | si_applet_cant_get(si); |
| 5942 | return; |
| 5943 | } |
| 5944 | |
| 5945 | /* skip the requests bytes. */ |
| 5946 | bo_skip(si_oc(si), strm->txn->req.eoh + 2); |
| 5947 | } |
| 5948 | |
| 5949 | /* Executes The applet if it is not done. */ |
| 5950 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 5951 | |
| 5952 | /* Execute the function. */ |
| 5953 | switch (hlua_ctx_resume(hlua, 1)) { |
| 5954 | /* finished. */ |
| 5955 | case HLUA_E_OK: |
| 5956 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 5957 | break; |
| 5958 | |
| 5959 | /* yield. */ |
| 5960 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 5961 | if (hlua->wake_time != TICK_ETERNITY) |
| 5962 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5963 | return; |
| 5964 | |
| 5965 | /* finished with error. */ |
| 5966 | case HLUA_E_ERRMSG: |
| 5967 | /* Display log. */ |
| 5968 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 5969 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 5970 | lua_pop(hlua->T, 1); |
| 5971 | goto error; |
| 5972 | |
| 5973 | case HLUA_E_ERR: |
| 5974 | /* Display log. */ |
| 5975 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
| 5976 | rule->arg.hlua_rule->fcn.name); |
| 5977 | goto error; |
| 5978 | |
| 5979 | default: |
| 5980 | goto error; |
| 5981 | } |
| 5982 | } |
| 5983 | |
| 5984 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
| 5985 | |
| 5986 | /* We must send the final chunk. */ |
| 5987 | if (ctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED && |
| 5988 | !(ctx->ctx.hlua_apphttp.flags & APPLET_LAST_CHK)) { |
| 5989 | |
| 5990 | /* sent last chunk at once. */ |
| 5991 | ret = bi_putblk(res, "0\r\n\r\n", 5); |
| 5992 | |
| 5993 | /* critical error. */ |
| 5994 | if (ret == -2 || ret == -3) { |
| 5995 | SEND_ERR(px, "Lua applet http '%s'cannont send last chunk.\n", |
| 5996 | rule->arg.hlua_rule->fcn.name); |
| 5997 | goto error; |
| 5998 | } |
| 5999 | |
| 6000 | /* no enough space error. */ |
| 6001 | if (ret == -1) { |
| 6002 | si_applet_cant_put(si); |
| 6003 | return; |
| 6004 | } |
| 6005 | |
| 6006 | /* set the last chunk sent. */ |
| 6007 | ctx->ctx.hlua_apphttp.flags |= APPLET_LAST_CHK; |
| 6008 | } |
| 6009 | |
| 6010 | /* close the connection. */ |
| 6011 | |
| 6012 | /* status / log */ |
| 6013 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 6014 | strm->logs.tv_request = now; |
| 6015 | |
| 6016 | /* eat the whole request */ |
| 6017 | bo_skip(si_oc(si), si_ob(si)->o); |
| 6018 | res->flags |= CF_READ_NULL; |
| 6019 | si_shutr(si); |
| 6020 | |
| 6021 | return; |
| 6022 | } |
| 6023 | |
| 6024 | error: |
| 6025 | |
| 6026 | /* If we are in HTTP mode, and we are not send any |
| 6027 | * data, return a 500 server error in best effort: |
| 6028 | * if there are no room avalaible in the buffer, |
| 6029 | * just close the connection. |
| 6030 | */ |
| 6031 | bi_putblk(res, error_500, strlen(error_500)); |
| 6032 | if (!(strm->flags & SF_ERR_MASK)) |
| 6033 | strm->flags |= SF_ERR_RESOURCE; |
| 6034 | si_shutw(si); |
| 6035 | si_shutr(si); |
| 6036 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 6037 | } |
| 6038 | |
| 6039 | static void hlua_applet_http_release(struct appctx *ctx) |
| 6040 | { |
| 6041 | task_free(ctx->ctx.hlua_apphttp.task); |
| 6042 | ctx->ctx.hlua_apphttp.task = NULL; |
| 6043 | hlua_ctx_destroy(&ctx->ctx.hlua_apphttp.hlua); |
| 6044 | } |
| 6045 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6046 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
| 6047 | * succes case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6048 | * |
| 6049 | * This function can fail with an abort() due to an Lua critical error. |
| 6050 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 6051 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6052 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6053 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 6054 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6055 | { |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6056 | struct hlua_function *fcn = (struct hlua_function *)rule->kw->private; |
| 6057 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6058 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6059 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6060 | if (!rule->arg.hlua_rule) { |
| 6061 | memprintf(err, "out of memory error"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 6062 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6063 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6064 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6065 | /* Reference the Lua function and store the reference. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6066 | rule->arg.hlua_rule->fcn = *fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6067 | |
| 6068 | /* TODO: later accept arguments. */ |
| 6069 | rule->arg.hlua_rule->args = NULL; |
| 6070 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 6071 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6072 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 6073 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6074 | } |
| 6075 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6076 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 6077 | struct act_rule *rule, char **err) |
| 6078 | { |
| 6079 | struct hlua_function *fcn = (struct hlua_function *)rule->kw->private; |
| 6080 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 6081 | /* HTTP applets are forbidden in tcp-request rules. |
| 6082 | * HTTP applet request requires everything initilized by |
| 6083 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
| 6084 | * The applet will be immediately initilized, but its before |
| 6085 | * the call of this analyzer. |
| 6086 | */ |
| 6087 | if (rule->from != ACT_F_HTTP_REQ) { |
| 6088 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 6089 | return ACT_RET_PRS_ERR; |
| 6090 | } |
| 6091 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6092 | /* Memory for the rule. */ |
| 6093 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 6094 | if (!rule->arg.hlua_rule) { |
| 6095 | memprintf(err, "out of memory error"); |
| 6096 | return ACT_RET_PRS_ERR; |
| 6097 | } |
| 6098 | |
| 6099 | /* Reference the Lua function and store the reference. */ |
| 6100 | rule->arg.hlua_rule->fcn = *fcn; |
| 6101 | |
| 6102 | /* TODO: later accept arguments. */ |
| 6103 | rule->arg.hlua_rule->args = NULL; |
| 6104 | |
| 6105 | /* Add applet pointer in the rule. */ |
| 6106 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 6107 | rule->applet.name = fcn->name; |
| 6108 | rule->applet.init = hlua_applet_http_init; |
| 6109 | rule->applet.fct = hlua_applet_http_fct; |
| 6110 | rule->applet.release = hlua_applet_http_release; |
| 6111 | rule->applet.timeout = hlua_timeout_applet; |
| 6112 | |
| 6113 | return ACT_RET_PRS_OK; |
| 6114 | } |
| 6115 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6116 | /* This function is an LUA binding used for registering |
| 6117 | * "sample-conv" functions. It expects a converter name used |
| 6118 | * in the haproxy configuration file, and an LUA function. |
| 6119 | */ |
| 6120 | __LJMP static int hlua_register_action(lua_State *L) |
| 6121 | { |
| 6122 | struct action_kw_list *akl; |
| 6123 | const char *name; |
| 6124 | int ref; |
| 6125 | int len; |
| 6126 | struct hlua_function *fcn; |
| 6127 | |
Thierry FOURNIER | ed0bdaa | 2015-12-20 19:51:06 +0100 | [diff] [blame] | 6128 | MAY_LJMP(check_args(L, 3, "register_action")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6129 | |
| 6130 | /* First argument : converter name. */ |
| 6131 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6132 | |
| 6133 | /* Second argument : environment. */ |
| 6134 | if (lua_type(L, 2) != LUA_TTABLE) |
| 6135 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 6136 | |
| 6137 | /* Third argument : lua function. */ |
| 6138 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 6139 | |
| 6140 | /* browse the second argulent as an array. */ |
| 6141 | lua_pushnil(L); |
| 6142 | while (lua_next(L, 2) != 0) { |
| 6143 | if (lua_type(L, -1) != LUA_TSTRING) |
| 6144 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 6145 | |
| 6146 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 6147 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6148 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6149 | if (!akl) |
| 6150 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6151 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6152 | if (!fcn) |
| 6153 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6154 | |
| 6155 | /* Fill fcn. */ |
| 6156 | fcn->name = strdup(name); |
| 6157 | if (!fcn->name) |
| 6158 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6159 | fcn->function_ref = ref; |
| 6160 | |
| 6161 | /* List head */ |
| 6162 | akl->list.n = akl->list.p = NULL; |
| 6163 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6164 | /* action keyword. */ |
| 6165 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6166 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6167 | if (!akl->kw[0].kw) |
| 6168 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6169 | |
| 6170 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 6171 | |
| 6172 | akl->kw[0].match_pfx = 0; |
| 6173 | akl->kw[0].private = fcn; |
| 6174 | akl->kw[0].parse = action_register_lua; |
| 6175 | |
| 6176 | /* select the action registering point. */ |
| 6177 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 6178 | tcp_req_cont_keywords_register(akl); |
| 6179 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 6180 | tcp_res_cont_keywords_register(akl); |
| 6181 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 6182 | http_req_keywords_register(akl); |
| 6183 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 6184 | http_res_keywords_register(akl); |
| 6185 | else |
| 6186 | WILL_LJMP(luaL_error(L, "lua action environment '%s' is unknown. " |
| 6187 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 6188 | "are expected.", lua_tostring(L, -1))); |
| 6189 | |
| 6190 | /* pop the environment string. */ |
| 6191 | lua_pop(L, 1); |
| 6192 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6193 | return ACT_RET_PRS_OK; |
| 6194 | } |
| 6195 | |
| 6196 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 6197 | struct act_rule *rule, char **err) |
| 6198 | { |
| 6199 | struct hlua_function *fcn = (struct hlua_function *)rule->kw->private; |
| 6200 | |
| 6201 | /* Memory for the rule. */ |
| 6202 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 6203 | if (!rule->arg.hlua_rule) { |
| 6204 | memprintf(err, "out of memory error"); |
| 6205 | return ACT_RET_PRS_ERR; |
| 6206 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6207 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6208 | /* Reference the Lua function and store the reference. */ |
| 6209 | rule->arg.hlua_rule->fcn = *fcn; |
| 6210 | |
| 6211 | /* TODO: later accept arguments. */ |
| 6212 | rule->arg.hlua_rule->args = NULL; |
| 6213 | |
| 6214 | /* Add applet pointer in the rule. */ |
| 6215 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 6216 | rule->applet.name = fcn->name; |
| 6217 | rule->applet.init = hlua_applet_tcp_init; |
| 6218 | rule->applet.fct = hlua_applet_tcp_fct; |
| 6219 | rule->applet.release = hlua_applet_tcp_release; |
| 6220 | rule->applet.timeout = hlua_timeout_applet; |
| 6221 | |
| 6222 | return 0; |
| 6223 | } |
| 6224 | |
| 6225 | /* This function is an LUA binding used for registering |
| 6226 | * "sample-conv" functions. It expects a converter name used |
| 6227 | * in the haproxy configuration file, and an LUA function. |
| 6228 | */ |
| 6229 | __LJMP static int hlua_register_service(lua_State *L) |
| 6230 | { |
| 6231 | struct action_kw_list *akl; |
| 6232 | const char *name; |
| 6233 | const char *env; |
| 6234 | int ref; |
| 6235 | int len; |
| 6236 | struct hlua_function *fcn; |
| 6237 | |
| 6238 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 6239 | |
| 6240 | /* First argument : converter name. */ |
| 6241 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6242 | |
| 6243 | /* Second argument : environment. */ |
| 6244 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6245 | |
| 6246 | /* Third argument : lua function. */ |
| 6247 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 6248 | |
| 6249 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 6250 | /* Allocate and fill the sample fetch keyword struct. */ |
| 6251 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 6252 | if (!akl) |
| 6253 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6254 | fcn = calloc(1, sizeof(*fcn)); |
| 6255 | if (!fcn) |
| 6256 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6257 | |
| 6258 | /* Fill fcn. */ |
| 6259 | len = strlen("<lua.>") + strlen(name) + 1; |
| 6260 | fcn->name = calloc(1, len); |
| 6261 | if (!fcn->name) |
| 6262 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6263 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
| 6264 | fcn->function_ref = ref; |
| 6265 | |
| 6266 | /* List head */ |
| 6267 | akl->list.n = akl->list.p = NULL; |
| 6268 | |
| 6269 | /* converter keyword. */ |
| 6270 | len = strlen("lua.") + strlen(name) + 1; |
| 6271 | akl->kw[0].kw = calloc(1, len); |
| 6272 | if (!akl->kw[0].kw) |
| 6273 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6274 | |
| 6275 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 6276 | |
| 6277 | if (strcmp(env, "tcp") == 0) |
| 6278 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6279 | else if (strcmp(env, "http") == 0) |
| 6280 | akl->kw[0].parse = action_register_service_http; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6281 | else |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6282 | WILL_LJMP(luaL_error(L, "lua service environment '%s' is unknown. " |
| 6283 | "'tcp' or 'http' are expected.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6284 | |
| 6285 | akl->kw[0].match_pfx = 0; |
| 6286 | akl->kw[0].private = fcn; |
| 6287 | |
| 6288 | /* End of array. */ |
| 6289 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 6290 | |
| 6291 | /* Register this new converter */ |
| 6292 | service_keywords_register(akl); |
| 6293 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6294 | return 0; |
| 6295 | } |
| 6296 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6297 | static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx, |
| 6298 | struct proxy *defpx, const char *file, int line, |
| 6299 | char **err, unsigned int *timeout) |
| 6300 | { |
| 6301 | const char *error; |
| 6302 | |
| 6303 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
| 6304 | if (error && *error != '\0') { |
| 6305 | memprintf(err, "%s: invalid timeout", args[0]); |
| 6306 | return -1; |
| 6307 | } |
| 6308 | return 0; |
| 6309 | } |
| 6310 | |
| 6311 | static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx, |
| 6312 | struct proxy *defpx, const char *file, int line, |
| 6313 | char **err) |
| 6314 | { |
| 6315 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 6316 | file, line, err, &hlua_timeout_session); |
| 6317 | } |
| 6318 | |
| 6319 | static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx, |
| 6320 | struct proxy *defpx, const char *file, int line, |
| 6321 | char **err) |
| 6322 | { |
| 6323 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 6324 | file, line, err, &hlua_timeout_task); |
| 6325 | } |
| 6326 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6327 | static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx, |
| 6328 | struct proxy *defpx, const char *file, int line, |
| 6329 | char **err) |
| 6330 | { |
| 6331 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 6332 | file, line, err, &hlua_timeout_applet); |
| 6333 | } |
| 6334 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 6335 | static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx, |
| 6336 | struct proxy *defpx, const char *file, int line, |
| 6337 | char **err) |
| 6338 | { |
| 6339 | char *error; |
| 6340 | |
| 6341 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 6342 | if (*error != '\0') { |
| 6343 | memprintf(err, "%s: invalid number", args[0]); |
| 6344 | return -1; |
| 6345 | } |
| 6346 | return 0; |
| 6347 | } |
| 6348 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 6349 | static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx, |
| 6350 | struct proxy *defpx, const char *file, int line, |
| 6351 | char **err) |
| 6352 | { |
| 6353 | char *error; |
| 6354 | |
| 6355 | if (*(args[1]) == 0) { |
| 6356 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 6357 | return -1; |
| 6358 | } |
| 6359 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 6360 | if (*error != '\0') { |
| 6361 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 6362 | return -1; |
| 6363 | } |
| 6364 | return 0; |
| 6365 | } |
| 6366 | |
| 6367 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 6368 | /* This function is called by the main configuration key "lua-load". It loads and |
| 6369 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 6370 | * the main lua entry point. |
| 6371 | * |
| 6372 | * This funtion runs with the HAProxy keywords API. It returns -1 if an error is |
| 6373 | * occured, otherwise it returns 0. |
| 6374 | * |
| 6375 | * In some error case, LUA set an error message in top of the stack. This function |
| 6376 | * 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] | 6377 | * |
| 6378 | * This function can fail with an abort() due to an Lua critical error. |
| 6379 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 6380 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 6381 | */ |
| 6382 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
| 6383 | struct proxy *defpx, const char *file, int line, |
| 6384 | char **err) |
| 6385 | { |
| 6386 | int error; |
| 6387 | |
| 6388 | /* Just load and compile the file. */ |
| 6389 | error = luaL_loadfile(gL.T, args[1]); |
| 6390 | if (error) { |
| 6391 | memprintf(err, "error in lua file '%s': %s", args[1], lua_tostring(gL.T, -1)); |
| 6392 | lua_pop(gL.T, 1); |
| 6393 | return -1; |
| 6394 | } |
| 6395 | |
| 6396 | /* If no syntax error where detected, execute the code. */ |
| 6397 | error = lua_pcall(gL.T, 0, LUA_MULTRET, 0); |
| 6398 | switch (error) { |
| 6399 | case LUA_OK: |
| 6400 | break; |
| 6401 | case LUA_ERRRUN: |
| 6402 | memprintf(err, "lua runtime error: %s\n", lua_tostring(gL.T, -1)); |
| 6403 | lua_pop(gL.T, 1); |
| 6404 | return -1; |
| 6405 | case LUA_ERRMEM: |
| 6406 | memprintf(err, "lua out of memory error\n"); |
| 6407 | return -1; |
| 6408 | case LUA_ERRERR: |
| 6409 | memprintf(err, "lua message handler error: %s\n", lua_tostring(gL.T, -1)); |
| 6410 | lua_pop(gL.T, 1); |
| 6411 | return -1; |
| 6412 | case LUA_ERRGCMM: |
| 6413 | memprintf(err, "lua garbage collector error: %s\n", lua_tostring(gL.T, -1)); |
| 6414 | lua_pop(gL.T, 1); |
| 6415 | return -1; |
| 6416 | default: |
| 6417 | memprintf(err, "lua unknonwn error: %s\n", lua_tostring(gL.T, -1)); |
| 6418 | lua_pop(gL.T, 1); |
| 6419 | return -1; |
| 6420 | } |
| 6421 | |
| 6422 | return 0; |
| 6423 | } |
| 6424 | |
| 6425 | /* configuration keywords declaration */ |
| 6426 | static struct cfg_kw_list cfg_kws = {{ },{ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6427 | { CFG_GLOBAL, "lua-load", hlua_load }, |
| 6428 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 6429 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 6430 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 6431 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 6432 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 6433 | { 0, NULL, NULL }, |
| 6434 | }}; |
| 6435 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6436 | /* This function can fail with an abort() due to an Lua critical error. |
| 6437 | * We are in the initialisation process of HAProxy, this abort() is |
| 6438 | * tolerated. |
| 6439 | */ |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6440 | int hlua_post_init() |
| 6441 | { |
| 6442 | struct hlua_init_function *init; |
| 6443 | const char *msg; |
| 6444 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6445 | const char *error; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6446 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 6447 | /* Call post initialisation function in safe environement. */ |
| 6448 | if (!SET_SAFE_LJMP(gL.T)) { |
| 6449 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 6450 | error = lua_tostring(gL.T, -1); |
| 6451 | else |
| 6452 | error = "critical error"; |
| 6453 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 6454 | exit(1); |
| 6455 | } |
| 6456 | hlua_fcn_post_init(gL.T); |
| 6457 | RESET_SAFE_LJMP(gL.T); |
| 6458 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6459 | list_for_each_entry(init, &hlua_init_functions, l) { |
| 6460 | lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref); |
| 6461 | ret = hlua_ctx_resume(&gL, 0); |
| 6462 | switch (ret) { |
| 6463 | case HLUA_E_OK: |
| 6464 | lua_pop(gL.T, -1); |
| 6465 | return 1; |
| 6466 | case HLUA_E_AGAIN: |
| 6467 | Alert("lua init: yield not allowed.\n"); |
| 6468 | return 0; |
| 6469 | case HLUA_E_ERRMSG: |
| 6470 | msg = lua_tostring(gL.T, -1); |
| 6471 | Alert("lua init: %s.\n", msg); |
| 6472 | return 0; |
| 6473 | case HLUA_E_ERR: |
| 6474 | default: |
| 6475 | Alert("lua init: unknown runtime error.\n"); |
| 6476 | return 0; |
| 6477 | } |
| 6478 | } |
| 6479 | return 1; |
| 6480 | } |
| 6481 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 6482 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 6483 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 6484 | * is the previously allocated size or the kind of object in case of a new |
| 6485 | * allocation. <nsize> is the requested new size. |
| 6486 | */ |
| 6487 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 6488 | { |
| 6489 | struct hlua_mem_allocator *zone = ud; |
| 6490 | |
| 6491 | if (nsize == 0) { |
| 6492 | /* it's a free */ |
| 6493 | if (ptr) |
| 6494 | zone->allocated -= osize; |
| 6495 | free(ptr); |
| 6496 | return NULL; |
| 6497 | } |
| 6498 | |
| 6499 | if (!ptr) { |
| 6500 | /* it's a new allocation */ |
| 6501 | if (zone->limit && zone->allocated + nsize > zone->limit) |
| 6502 | return NULL; |
| 6503 | |
| 6504 | ptr = malloc(nsize); |
| 6505 | if (ptr) |
| 6506 | zone->allocated += nsize; |
| 6507 | return ptr; |
| 6508 | } |
| 6509 | |
| 6510 | /* it's a realloc */ |
| 6511 | if (zone->limit && zone->allocated + nsize - osize > zone->limit) |
| 6512 | return NULL; |
| 6513 | |
| 6514 | ptr = realloc(ptr, nsize); |
| 6515 | if (ptr) |
| 6516 | zone->allocated += nsize - osize; |
| 6517 | return ptr; |
| 6518 | } |
| 6519 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6520 | /* Ithis function can fail with an abort() due to an Lua critical error. |
| 6521 | * We are in the initialisation process of HAProxy, this abort() is |
| 6522 | * tolerated. |
| 6523 | */ |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 6524 | void hlua_init(void) |
| 6525 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 6526 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 6527 | int idx; |
| 6528 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 6529 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 6530 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6531 | const char *error_msg; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6532 | #ifdef USE_OPENSSL |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6533 | struct srv_kw *kw; |
| 6534 | int tmp_error; |
| 6535 | char *error; |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 6536 | char *args[] = { /* SSL client configuration. */ |
| 6537 | "ssl", |
| 6538 | "verify", |
| 6539 | "none", |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 6540 | NULL |
| 6541 | }; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6542 | #endif |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 6543 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6544 | /* Initialise com signals pool */ |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 6545 | pool2_hlua_com = create_pool("hlua_com", sizeof(struct hlua_com), MEM_F_SHARED); |
| 6546 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 6547 | /* Register configuration keywords. */ |
| 6548 | cfg_register_keywords(&cfg_kws); |
| 6549 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 6550 | /* Init main lua stack. */ |
| 6551 | gL.Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 6552 | gL.flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 6553 | LIST_INIT(&gL.com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 6554 | gL.T = luaL_newstate(); |
| 6555 | hlua_sethlua(&gL); |
| 6556 | gL.Tref = LUA_REFNIL; |
| 6557 | gL.task = NULL; |
| 6558 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6559 | /* From this point, until the end of the initialisation fucntion, |
| 6560 | * the Lua function can fail with an abort. We are in the initialisation |
| 6561 | * process of HAProxy, this abort() is tolerated. |
| 6562 | */ |
| 6563 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 6564 | /* change the memory allocators to track memory usage */ |
| 6565 | lua_setallocf(gL.T, hlua_alloc, &hlua_global_allocator); |
| 6566 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 6567 | /* Initialise lua. */ |
| 6568 | luaL_openlibs(gL.T); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 6569 | |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 6570 | /* Set safe environment for the initialisation. */ |
| 6571 | if (!SET_SAFE_LJMP(gL.T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6572 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 6573 | error_msg = lua_tostring(gL.T, -1); |
| 6574 | else |
| 6575 | error_msg = "critical error"; |
| 6576 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 6577 | exit(1); |
| 6578 | } |
| 6579 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 6580 | /* |
| 6581 | * |
| 6582 | * Create "core" object. |
| 6583 | * |
| 6584 | */ |
| 6585 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 6586 | /* This table entry is the object "core" base. */ |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 6587 | lua_newtable(gL.T); |
| 6588 | |
| 6589 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6590 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 6591 | hlua_class_const_int(gL.T, log_levels[i], i); |
| 6592 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6593 | /* Register special functions. */ |
| 6594 | hlua_class_function(gL.T, "register_init", hlua_register_init); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6595 | hlua_class_function(gL.T, "register_task", hlua_register_task); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6596 | hlua_class_function(gL.T, "register_fetches", hlua_register_fetches); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6597 | hlua_class_function(gL.T, "register_converters", hlua_register_converters); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6598 | hlua_class_function(gL.T, "register_action", hlua_register_action); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6599 | hlua_class_function(gL.T, "register_service", hlua_register_service); |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6600 | hlua_class_function(gL.T, "yield", hlua_yield); |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 6601 | hlua_class_function(gL.T, "set_nice", hlua_set_nice); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6602 | hlua_class_function(gL.T, "sleep", hlua_sleep); |
| 6603 | hlua_class_function(gL.T, "msleep", hlua_msleep); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 6604 | hlua_class_function(gL.T, "add_acl", hlua_add_acl); |
| 6605 | hlua_class_function(gL.T, "del_acl", hlua_del_acl); |
| 6606 | hlua_class_function(gL.T, "set_map", hlua_set_map); |
| 6607 | hlua_class_function(gL.T, "del_map", hlua_del_map); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 6608 | hlua_class_function(gL.T, "tcp", hlua_socket_new); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 6609 | hlua_class_function(gL.T, "log", hlua_log); |
| 6610 | hlua_class_function(gL.T, "Debug", hlua_log_debug); |
| 6611 | hlua_class_function(gL.T, "Info", hlua_log_info); |
| 6612 | hlua_class_function(gL.T, "Warning", hlua_log_warning); |
| 6613 | hlua_class_function(gL.T, "Alert", hlua_log_alert); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 6614 | hlua_class_function(gL.T, "done", hlua_done); |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 6615 | hlua_fcn_reg_core_fcn(gL.T); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6616 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 6617 | lua_setglobal(gL.T, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 6618 | |
| 6619 | /* |
| 6620 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 6621 | * Register class Map |
| 6622 | * |
| 6623 | */ |
| 6624 | |
| 6625 | /* This table entry is the object "Map" base. */ |
| 6626 | lua_newtable(gL.T); |
| 6627 | |
| 6628 | /* register pattern types. */ |
| 6629 | for (i=0; i<PAT_MATCH_NUM; i++) |
| 6630 | hlua_class_const_int(gL.T, pat_match_names[i], i); |
| 6631 | |
| 6632 | /* register constructor. */ |
| 6633 | hlua_class_function(gL.T, "new", hlua_map_new); |
| 6634 | |
| 6635 | /* Create and fill the metatable. */ |
| 6636 | lua_newtable(gL.T); |
| 6637 | |
| 6638 | /* Create and fille the __index entry. */ |
| 6639 | lua_pushstring(gL.T, "__index"); |
| 6640 | lua_newtable(gL.T); |
| 6641 | |
| 6642 | /* Register . */ |
| 6643 | hlua_class_function(gL.T, "lookup", hlua_map_lookup); |
| 6644 | hlua_class_function(gL.T, "slookup", hlua_map_slookup); |
| 6645 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 6646 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 6647 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 6648 | /* Register previous table in the registry with reference and named entry. |
| 6649 | * The function hlua_register_metatable() pops the stack, so we |
| 6650 | * previously create a copy of the table. |
| 6651 | */ |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 6652 | lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 6653 | class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 6654 | |
| 6655 | /* Assign the metatable to the mai Map object. */ |
| 6656 | lua_setmetatable(gL.T, -2); |
| 6657 | |
| 6658 | /* Set a name to the table. */ |
| 6659 | lua_setglobal(gL.T, "Map"); |
| 6660 | |
| 6661 | /* |
| 6662 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 6663 | * Register class Channel |
| 6664 | * |
| 6665 | */ |
| 6666 | |
| 6667 | /* Create and fill the metatable. */ |
| 6668 | lua_newtable(gL.T); |
| 6669 | |
| 6670 | /* Create and fille the __index entry. */ |
| 6671 | lua_pushstring(gL.T, "__index"); |
| 6672 | lua_newtable(gL.T); |
| 6673 | |
| 6674 | /* Register . */ |
| 6675 | hlua_class_function(gL.T, "get", hlua_channel_get); |
| 6676 | hlua_class_function(gL.T, "dup", hlua_channel_dup); |
| 6677 | hlua_class_function(gL.T, "getline", hlua_channel_getline); |
| 6678 | hlua_class_function(gL.T, "set", hlua_channel_set); |
| 6679 | hlua_class_function(gL.T, "append", hlua_channel_append); |
| 6680 | hlua_class_function(gL.T, "send", hlua_channel_send); |
| 6681 | hlua_class_function(gL.T, "forward", hlua_channel_forward); |
| 6682 | hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len); |
| 6683 | hlua_class_function(gL.T, "get_out_len", hlua_channel_get_out_len); |
| 6684 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 6685 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 6686 | |
| 6687 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 6688 | class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 6689 | |
| 6690 | /* |
| 6691 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 6692 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 6693 | * |
| 6694 | */ |
| 6695 | |
| 6696 | /* Create and fill the metatable. */ |
| 6697 | lua_newtable(gL.T); |
| 6698 | |
| 6699 | /* Create and fille the __index entry. */ |
| 6700 | lua_pushstring(gL.T, "__index"); |
| 6701 | lua_newtable(gL.T); |
| 6702 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 6703 | /* Browse existing fetches and create the associated |
| 6704 | * object method. |
| 6705 | */ |
| 6706 | sf = NULL; |
| 6707 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
| 6708 | |
| 6709 | /* Dont register the keywork if the arguments check function are |
| 6710 | * not safe during the runtime. |
| 6711 | */ |
| 6712 | if ((sf->val_args != NULL) && |
| 6713 | (sf->val_args != val_payload_lv) && |
| 6714 | (sf->val_args != val_hdr)) |
| 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, sf->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); |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 6728 | lua_pushlightuserdata(gL.T, sf); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 6729 | lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 6730 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +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 | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 6734 | |
| 6735 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 6736 | class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 6737 | |
| 6738 | /* |
| 6739 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 6740 | * Register class Converters |
| 6741 | * |
| 6742 | */ |
| 6743 | |
| 6744 | /* Create and fill the metatable. */ |
| 6745 | lua_newtable(gL.T); |
| 6746 | |
| 6747 | /* Create and fill the __index entry. */ |
| 6748 | lua_pushstring(gL.T, "__index"); |
| 6749 | lua_newtable(gL.T); |
| 6750 | |
| 6751 | /* Browse existing converters and create the associated |
| 6752 | * object method. |
| 6753 | */ |
| 6754 | sc = NULL; |
| 6755 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
| 6756 | /* Dont register the keywork if the arguments check function are |
| 6757 | * not safe during the runtime. |
| 6758 | */ |
| 6759 | if (sc->val_args != NULL) |
| 6760 | continue; |
| 6761 | |
| 6762 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 6763 | * by an underscore. |
| 6764 | */ |
| 6765 | strncpy(trash.str, sc->kw, trash.size); |
| 6766 | trash.str[trash.size - 1] = '\0'; |
| 6767 | for (p = trash.str; *p; p++) |
| 6768 | if (*p == '.' || *p == '-' || *p == '+') |
| 6769 | *p = '_'; |
| 6770 | |
| 6771 | /* Register the function. */ |
| 6772 | lua_pushstring(gL.T, trash.str); |
| 6773 | lua_pushlightuserdata(gL.T, sc); |
| 6774 | lua_pushcclosure(gL.T, hlua_run_sample_conv, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 6775 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 6776 | } |
| 6777 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 6778 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 6779 | |
| 6780 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 6781 | class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 6782 | |
| 6783 | /* |
| 6784 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6785 | * Register class HTTP |
| 6786 | * |
| 6787 | */ |
| 6788 | |
| 6789 | /* Create and fill the metatable. */ |
| 6790 | lua_newtable(gL.T); |
| 6791 | |
| 6792 | /* Create and fille the __index entry. */ |
| 6793 | lua_pushstring(gL.T, "__index"); |
| 6794 | lua_newtable(gL.T); |
| 6795 | |
| 6796 | /* Register Lua functions. */ |
| 6797 | hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers); |
| 6798 | hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr); |
| 6799 | hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr); |
| 6800 | hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val); |
| 6801 | hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr); |
| 6802 | hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr); |
| 6803 | hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth); |
| 6804 | hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path); |
| 6805 | hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query); |
| 6806 | hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri); |
| 6807 | |
| 6808 | hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers); |
| 6809 | hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr); |
| 6810 | hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr); |
| 6811 | hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val); |
| 6812 | hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr); |
| 6813 | 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] | 6814 | 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] | 6815 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 6816 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6817 | |
| 6818 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 6819 | class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6820 | |
| 6821 | /* |
| 6822 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6823 | * Register class AppletTCP |
| 6824 | * |
| 6825 | */ |
| 6826 | |
| 6827 | /* Create and fill the metatable. */ |
| 6828 | lua_newtable(gL.T); |
| 6829 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6830 | /* Create and fille the __index entry. */ |
| 6831 | lua_pushstring(gL.T, "__index"); |
| 6832 | lua_newtable(gL.T); |
| 6833 | |
| 6834 | /* Register Lua functions. */ |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 6835 | hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline); |
| 6836 | hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv); |
| 6837 | hlua_class_function(gL.T, "send", hlua_applet_tcp_send); |
| 6838 | hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv); |
| 6839 | hlua_class_function(gL.T, "get_priv", hlua_applet_tcp_get_priv); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6840 | |
| 6841 | lua_settable(gL.T, -3); |
| 6842 | |
| 6843 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 6844 | class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6845 | |
| 6846 | /* |
| 6847 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6848 | * Register class AppletHTTP |
| 6849 | * |
| 6850 | */ |
| 6851 | |
| 6852 | /* Create and fill the metatable. */ |
| 6853 | lua_newtable(gL.T); |
| 6854 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6855 | /* Create and fille the __index entry. */ |
| 6856 | lua_pushstring(gL.T, "__index"); |
| 6857 | lua_newtable(gL.T); |
| 6858 | |
| 6859 | /* Register Lua functions. */ |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 6860 | hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv); |
| 6861 | hlua_class_function(gL.T, "get_priv", hlua_applet_http_get_priv); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6862 | hlua_class_function(gL.T, "getline", hlua_applet_http_getline); |
| 6863 | hlua_class_function(gL.T, "receive", hlua_applet_http_recv); |
| 6864 | hlua_class_function(gL.T, "send", hlua_applet_http_send); |
| 6865 | hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader); |
| 6866 | hlua_class_function(gL.T, "set_status", hlua_applet_http_status); |
| 6867 | hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response); |
| 6868 | |
| 6869 | lua_settable(gL.T, -3); |
| 6870 | |
| 6871 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 6872 | class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6873 | |
| 6874 | /* |
| 6875 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 6876 | * Register class TXN |
| 6877 | * |
| 6878 | */ |
| 6879 | |
| 6880 | /* Create and fill the metatable. */ |
| 6881 | lua_newtable(gL.T); |
| 6882 | |
| 6883 | /* Create and fille the __index entry. */ |
| 6884 | lua_pushstring(gL.T, "__index"); |
| 6885 | lua_newtable(gL.T); |
| 6886 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 6887 | /* Register Lua functions. */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 6888 | hlua_class_function(gL.T, "set_priv", hlua_set_priv); |
| 6889 | hlua_class_function(gL.T, "get_priv", hlua_get_priv); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 6890 | hlua_class_function(gL.T, "set_var", hlua_set_var); |
| 6891 | hlua_class_function(gL.T, "get_var", hlua_get_var); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 6892 | hlua_class_function(gL.T, "done", hlua_txn_done); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 6893 | hlua_class_function(gL.T, "set_loglevel",hlua_txn_set_loglevel); |
| 6894 | hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos); |
| 6895 | hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 6896 | hlua_class_function(gL.T, "deflog", hlua_txn_deflog); |
| 6897 | hlua_class_function(gL.T, "log", hlua_txn_log); |
| 6898 | hlua_class_function(gL.T, "Debug", hlua_txn_log_debug); |
| 6899 | hlua_class_function(gL.T, "Info", hlua_txn_log_info); |
| 6900 | hlua_class_function(gL.T, "Warning", hlua_txn_log_warning); |
| 6901 | hlua_class_function(gL.T, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 6902 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 6903 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 6904 | |
| 6905 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 6906 | class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 6907 | |
| 6908 | /* |
| 6909 | * |
| 6910 | * Register class Socket |
| 6911 | * |
| 6912 | */ |
| 6913 | |
| 6914 | /* Create and fill the metatable. */ |
| 6915 | lua_newtable(gL.T); |
| 6916 | |
| 6917 | /* Create and fille the __index entry. */ |
| 6918 | lua_pushstring(gL.T, "__index"); |
| 6919 | lua_newtable(gL.T); |
| 6920 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 6921 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 6922 | hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 6923 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 6924 | hlua_class_function(gL.T, "connect", hlua_socket_connect); |
| 6925 | hlua_class_function(gL.T, "send", hlua_socket_send); |
| 6926 | hlua_class_function(gL.T, "receive", hlua_socket_receive); |
| 6927 | hlua_class_function(gL.T, "close", hlua_socket_close); |
| 6928 | hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername); |
| 6929 | hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname); |
| 6930 | hlua_class_function(gL.T, "setoption", hlua_socket_setoption); |
| 6931 | hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout); |
| 6932 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 6933 | 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] | 6934 | |
| 6935 | /* Register the garbage collector entry. */ |
| 6936 | lua_pushstring(gL.T, "__gc"); |
| 6937 | lua_pushcclosure(gL.T, hlua_socket_gc, 0); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 6938 | 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] | 6939 | |
| 6940 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 6941 | class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 6942 | |
| 6943 | /* Proxy and server configuration initialisation. */ |
| 6944 | memset(&socket_proxy, 0, sizeof(socket_proxy)); |
| 6945 | init_new_proxy(&socket_proxy); |
| 6946 | socket_proxy.parent = NULL; |
| 6947 | socket_proxy.last_change = now.tv_sec; |
| 6948 | socket_proxy.id = "LUA-SOCKET"; |
| 6949 | socket_proxy.cap = PR_CAP_FE | PR_CAP_BE; |
| 6950 | socket_proxy.maxconn = 0; |
| 6951 | socket_proxy.accept = NULL; |
| 6952 | socket_proxy.options2 |= PR_O2_INDEPSTR; |
| 6953 | socket_proxy.srv = NULL; |
| 6954 | socket_proxy.conn_retries = 0; |
| 6955 | socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */ |
| 6956 | |
| 6957 | /* Init TCP server: unchanged parameters */ |
| 6958 | memset(&socket_tcp, 0, sizeof(socket_tcp)); |
| 6959 | socket_tcp.next = NULL; |
| 6960 | socket_tcp.proxy = &socket_proxy; |
| 6961 | socket_tcp.obj_type = OBJ_TYPE_SERVER; |
| 6962 | LIST_INIT(&socket_tcp.actconns); |
| 6963 | LIST_INIT(&socket_tcp.pendconns); |
Willy Tarreau | 600802a | 2015-08-04 17:19:06 +0200 | [diff] [blame] | 6964 | LIST_INIT(&socket_tcp.priv_conns); |
Willy Tarreau | 173a1c6 | 2015-08-05 10:31:57 +0200 | [diff] [blame] | 6965 | LIST_INIT(&socket_tcp.idle_conns); |
Willy Tarreau | 7017cb0 | 2015-08-05 16:35:23 +0200 | [diff] [blame] | 6966 | LIST_INIT(&socket_tcp.safe_conns); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 6967 | socket_tcp.state = SRV_ST_RUNNING; /* early server setup */ |
| 6968 | socket_tcp.last_change = 0; |
| 6969 | socket_tcp.id = "LUA-TCP-CONN"; |
| 6970 | socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 6971 | socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 6972 | socket_tcp.pp_opts = 0; /* Remove proxy protocol. */ |
| 6973 | |
| 6974 | /* XXX: Copy default parameter from default server, |
| 6975 | * but the default server is not initialized. |
| 6976 | */ |
| 6977 | socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue; |
| 6978 | socket_tcp.minconn = socket_proxy.defsrv.minconn; |
| 6979 | socket_tcp.maxconn = socket_proxy.defsrv.maxconn; |
| 6980 | socket_tcp.slowstart = socket_proxy.defsrv.slowstart; |
| 6981 | socket_tcp.onerror = socket_proxy.defsrv.onerror; |
| 6982 | socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 6983 | socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 6984 | socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 6985 | socket_tcp.uweight = socket_proxy.defsrv.iweight; |
| 6986 | socket_tcp.iweight = socket_proxy.defsrv.iweight; |
| 6987 | |
| 6988 | socket_tcp.check.status = HCHK_STATUS_INI; |
| 6989 | socket_tcp.check.rise = socket_proxy.defsrv.check.rise; |
| 6990 | socket_tcp.check.fall = socket_proxy.defsrv.check.fall; |
| 6991 | socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */ |
| 6992 | socket_tcp.check.server = &socket_tcp; |
| 6993 | |
| 6994 | socket_tcp.agent.status = HCHK_STATUS_INI; |
| 6995 | socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise; |
| 6996 | socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall; |
| 6997 | socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */ |
| 6998 | socket_tcp.agent.server = &socket_tcp; |
| 6999 | |
| 7000 | socket_tcp.xprt = &raw_sock; |
| 7001 | |
| 7002 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7003 | /* Init TCP server: unchanged parameters */ |
| 7004 | memset(&socket_ssl, 0, sizeof(socket_ssl)); |
| 7005 | socket_ssl.next = NULL; |
| 7006 | socket_ssl.proxy = &socket_proxy; |
| 7007 | socket_ssl.obj_type = OBJ_TYPE_SERVER; |
| 7008 | LIST_INIT(&socket_ssl.actconns); |
| 7009 | LIST_INIT(&socket_ssl.pendconns); |
Willy Tarreau | 600802a | 2015-08-04 17:19:06 +0200 | [diff] [blame] | 7010 | LIST_INIT(&socket_ssl.priv_conns); |
Willy Tarreau | 173a1c6 | 2015-08-05 10:31:57 +0200 | [diff] [blame] | 7011 | LIST_INIT(&socket_ssl.idle_conns); |
Willy Tarreau | 7017cb0 | 2015-08-05 16:35:23 +0200 | [diff] [blame] | 7012 | LIST_INIT(&socket_ssl.safe_conns); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7013 | socket_ssl.state = SRV_ST_RUNNING; /* early server setup */ |
| 7014 | socket_ssl.last_change = 0; |
| 7015 | socket_ssl.id = "LUA-SSL-CONN"; |
| 7016 | socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 7017 | socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 7018 | socket_ssl.pp_opts = 0; /* Remove proxy protocol. */ |
| 7019 | |
| 7020 | /* XXX: Copy default parameter from default server, |
| 7021 | * but the default server is not initialized. |
| 7022 | */ |
| 7023 | socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue; |
| 7024 | socket_ssl.minconn = socket_proxy.defsrv.minconn; |
| 7025 | socket_ssl.maxconn = socket_proxy.defsrv.maxconn; |
| 7026 | socket_ssl.slowstart = socket_proxy.defsrv.slowstart; |
| 7027 | socket_ssl.onerror = socket_proxy.defsrv.onerror; |
| 7028 | socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 7029 | socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 7030 | socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 7031 | socket_ssl.uweight = socket_proxy.defsrv.iweight; |
| 7032 | socket_ssl.iweight = socket_proxy.defsrv.iweight; |
| 7033 | |
| 7034 | socket_ssl.check.status = HCHK_STATUS_INI; |
| 7035 | socket_ssl.check.rise = socket_proxy.defsrv.check.rise; |
| 7036 | socket_ssl.check.fall = socket_proxy.defsrv.check.fall; |
| 7037 | socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */ |
| 7038 | socket_ssl.check.server = &socket_ssl; |
| 7039 | |
| 7040 | socket_ssl.agent.status = HCHK_STATUS_INI; |
| 7041 | socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise; |
| 7042 | socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall; |
| 7043 | socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */ |
| 7044 | socket_ssl.agent.server = &socket_ssl; |
| 7045 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 7046 | socket_ssl.use_ssl = 1; |
| 7047 | socket_ssl.xprt = &ssl_sock; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7048 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 7049 | for (idx = 0; args[idx] != NULL; idx++) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7050 | if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */ |
| 7051 | /* |
| 7052 | * |
| 7053 | * If the keyword is not known, we can search in the registered |
| 7054 | * server keywords. This is usefull to configure special SSL |
| 7055 | * features like client certificates and ssl_verify. |
| 7056 | * |
| 7057 | */ |
| 7058 | tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error); |
| 7059 | if (tmp_error != 0) { |
| 7060 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 7061 | abort(); /* This must be never arrives because the command line |
| 7062 | not editable by the user. */ |
| 7063 | } |
| 7064 | idx += kw->skip; |
| 7065 | } |
| 7066 | } |
| 7067 | |
| 7068 | /* Initialize SSL server. */ |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 7069 | ssl_sock_prepare_srv_ctx(&socket_ssl, &socket_proxy); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7070 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 7071 | |
| 7072 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 7073 | } |