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 | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 13 | #include <ctype.h> |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 14 | #include <setjmp.h> |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 15 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 16 | #include <lauxlib.h> |
| 17 | #include <lua.h> |
| 18 | #include <lualib.h> |
| 19 | |
Thierry FOURNIER | 463119c | 2015-03-10 00:35:36 +0100 | [diff] [blame] | 20 | #if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 503 |
| 21 | #error "Requires Lua 5.3 or later." |
Cyril Bonté | dc0306e | 2015-03-02 00:08:40 +0100 | [diff] [blame] | 22 | #endif |
| 23 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 24 | #include <ebpttree.h> |
| 25 | |
| 26 | #include <common/cfgparse.h> |
| 27 | |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 28 | #include <types/cli.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 29 | #include <types/hlua.h> |
| 30 | #include <types/proxy.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 31 | #include <types/stats.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 32 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 33 | #include <proto/arg.h> |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 34 | #include <proto/applet.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 35 | #include <proto/channel.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 36 | #include <proto/cli.h> |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 37 | #include <proto/connection.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 38 | #include <proto/stats.h> |
Thierry FOURNIER | 9a819e7 | 2015-02-16 20:22:55 +0100 | [diff] [blame] | 39 | #include <proto/hdr_idx.h> |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 40 | #include <proto/hlua.h> |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 41 | #include <proto/hlua_fcn.h> |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 42 | #include <proto/map.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 43 | #include <proto/obj_type.h> |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 44 | #include <proto/pattern.h> |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 45 | #include <proto/payload.h> |
| 46 | #include <proto/proto_http.h> |
| 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> |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 54 | #include <proto/tcp_rules.h> |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 55 | #include <proto/vars.h> |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 56 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 57 | /* Lua uses longjmp to perform yield or throwing errors. This |
| 58 | * macro is used only for identifying the function that can |
| 59 | * not return because a longjmp is executed. |
| 60 | * __LJMP marks a prototype of hlua file that can use longjmp. |
| 61 | * WILL_LJMP() marks an lua function that will use longjmp. |
| 62 | * MAY_LJMP() marks an lua function that may use longjmp. |
| 63 | */ |
| 64 | #define __LJMP |
| 65 | #define WILL_LJMP(func) func |
| 66 | #define MAY_LJMP(func) func |
| 67 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 68 | /* This couple of function executes securely some Lua calls outside of |
| 69 | * the lua runtime environment. Each Lua call can return a longjmp |
| 70 | * if it encounter a memory error. |
| 71 | * |
| 72 | * Lua documentation extract: |
| 73 | * |
| 74 | * If an error happens outside any protected environment, Lua calls |
| 75 | * a panic function (see lua_atpanic) and then calls abort, thus |
| 76 | * exiting the host application. Your panic function can avoid this |
| 77 | * exit by never returning (e.g., doing a long jump to your own |
| 78 | * recovery point outside Lua). |
| 79 | * |
| 80 | * The panic function runs as if it were a message handler (see |
| 81 | * §2.3); in particular, the error message is at the top of the |
| 82 | * stack. However, there is no guarantee about stack space. To push |
| 83 | * anything on the stack, the panic function must first check the |
| 84 | * available space (see §4.2). |
| 85 | * |
| 86 | * We must check all the Lua entry point. This includes: |
| 87 | * - The include/proto/hlua.h exported functions |
| 88 | * - the task wrapper function |
| 89 | * - The action wrapper function |
| 90 | * - The converters wrapper function |
| 91 | * - The sample-fetch wrapper functions |
| 92 | * |
| 93 | * It is tolerated that the initilisation function returns an abort. |
| 94 | * Before each Lua abort, an error message is writed on stderr. |
| 95 | * |
| 96 | * The macro SET_SAFE_LJMP initialise the longjmp. The Macro |
| 97 | * RESET_SAFE_LJMP reset the longjmp. These function must be macro |
| 98 | * because they must be exists in the program stack when the longjmp |
| 99 | * is called. |
| 100 | */ |
| 101 | jmp_buf safe_ljmp_env; |
| 102 | static int hlua_panic_safe(lua_State *L) { return 0; } |
| 103 | static int hlua_panic_ljmp(lua_State *L) { longjmp(safe_ljmp_env, 1); } |
| 104 | |
| 105 | #define SET_SAFE_LJMP(__L) \ |
| 106 | ({ \ |
| 107 | int ret; \ |
| 108 | if (setjmp(safe_ljmp_env) != 0) { \ |
| 109 | lua_atpanic(__L, hlua_panic_safe); \ |
| 110 | ret = 0; \ |
| 111 | } else { \ |
| 112 | lua_atpanic(__L, hlua_panic_ljmp); \ |
| 113 | ret = 1; \ |
| 114 | } \ |
| 115 | ret; \ |
| 116 | }) |
| 117 | |
| 118 | /* If we are the last function catching Lua errors, we |
| 119 | * must reset the panic function. |
| 120 | */ |
| 121 | #define RESET_SAFE_LJMP(__L) \ |
| 122 | do { \ |
| 123 | lua_atpanic(__L, hlua_panic_safe); \ |
| 124 | } while(0) |
| 125 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 126 | /* Applet status flags */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 127 | #define APPLET_DONE 0x01 /* applet processing is done. */ |
| 128 | #define APPLET_100C 0x02 /* 100 continue expected. */ |
| 129 | #define APPLET_HDR_SENT 0x04 /* Response header sent. */ |
| 130 | #define APPLET_CHUNKED 0x08 /* Use transfer encoding chunked. */ |
| 131 | #define APPLET_LAST_CHK 0x10 /* Last chunk sent. */ |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 132 | #define APPLET_HTTP11 0x20 /* Last chunk sent. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 133 | |
| 134 | #define HTTP_100C "HTTP/1.1 100 Continue\r\n\r\n" |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 135 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 136 | /* The main Lua execution context. */ |
| 137 | struct hlua gL; |
| 138 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 139 | /* This is the memory pool containing struct lua for applets |
| 140 | * (including cli). |
| 141 | */ |
| 142 | struct pool_head *pool2_hlua; |
| 143 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 144 | /* This is the memory pool containing all the signal structs. These |
| 145 | * struct are used to store each requiered signal between two tasks. |
| 146 | */ |
| 147 | struct pool_head *pool2_hlua_com; |
| 148 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 149 | /* Used for Socket connection. */ |
| 150 | static struct proxy socket_proxy; |
| 151 | static struct server socket_tcp; |
| 152 | #ifdef USE_OPENSSL |
| 153 | static struct server socket_ssl; |
| 154 | #endif |
| 155 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 156 | /* List head of the function called at the initialisation time. */ |
| 157 | struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions); |
| 158 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 159 | /* The following variables contains the reference of the different |
| 160 | * Lua classes. These references are useful for identify metadata |
| 161 | * associated with an object. |
| 162 | */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 163 | static int class_txn_ref; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 164 | static int class_socket_ref; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 165 | static int class_channel_ref; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 166 | static int class_fetches_ref; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 167 | static int class_converters_ref; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 168 | static int class_http_ref; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 169 | static int class_map_ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 170 | static int class_applet_tcp_ref; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 171 | static int class_applet_http_ref; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 172 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 173 | /* Global Lua execution timeout. By default Lua, execution linked |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 174 | * with stream (actions, sample-fetches and converters) have a |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 175 | * short timeout. Lua linked with tasks doesn't have a timeout |
| 176 | * because a task may remain alive during all the haproxy execution. |
| 177 | */ |
| 178 | static unsigned int hlua_timeout_session = 4000; /* session timeout. */ |
| 179 | static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 180 | static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 181 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 182 | /* Interrupts the Lua processing each "hlua_nb_instruction" instructions. |
| 183 | * it is used for preventing infinite loops. |
| 184 | * |
| 185 | * I test the scheer with an infinite loop containing one incrementation |
| 186 | * and one test. I run this loop between 10 seconds, I raise a ceil of |
| 187 | * 710M loops from one interrupt each 9000 instructions, so I fix the value |
| 188 | * to one interrupt each 10 000 instructions. |
| 189 | * |
| 190 | * configured | Number of |
| 191 | * instructions | loops executed |
| 192 | * between two | in milions |
| 193 | * forced yields | |
| 194 | * ---------------+--------------- |
| 195 | * 10 | 160 |
| 196 | * 500 | 670 |
| 197 | * 1000 | 680 |
| 198 | * 5000 | 700 |
| 199 | * 7000 | 700 |
| 200 | * 8000 | 700 |
| 201 | * 9000 | 710 <- ceil |
| 202 | * 10000 | 710 |
| 203 | * 100000 | 710 |
| 204 | * 1000000 | 710 |
| 205 | * |
| 206 | */ |
| 207 | static unsigned int hlua_nb_instruction = 10000; |
| 208 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 209 | /* Descriptor for the memory allocation state. If limit is not null, it will |
| 210 | * be enforced on any memory allocation. |
| 211 | */ |
| 212 | struct hlua_mem_allocator { |
| 213 | size_t allocated; |
| 214 | size_t limit; |
| 215 | }; |
| 216 | |
| 217 | static struct hlua_mem_allocator hlua_global_allocator; |
| 218 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 219 | static const char error_500[] = |
| 220 | "HTTP/1.0 500 Server Error\r\n" |
| 221 | "Cache-Control: no-cache\r\n" |
| 222 | "Connection: close\r\n" |
| 223 | "Content-Type: text/html\r\n" |
| 224 | "\r\n" |
| 225 | "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n"; |
| 226 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 227 | /* These functions converts types between HAProxy internal args or |
| 228 | * sample and LUA types. Another function permits to check if the |
| 229 | * LUA stack contains arguments according with an required ARG_T |
| 230 | * format. |
| 231 | */ |
| 232 | static int hlua_arg2lua(lua_State *L, const struct arg *arg); |
| 233 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 234 | __LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp, |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 235 | uint64_t mask, struct proxy *p); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 236 | static int hlua_smp2lua(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 237 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 238 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp); |
| 239 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 240 | __LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg); |
| 241 | |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 242 | #define SEND_ERR(__be, __fmt, __args...) \ |
| 243 | do { \ |
| 244 | send_log(__be, LOG_ERR, __fmt, ## __args); \ |
| 245 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \ |
| 246 | Alert(__fmt, ## __args); \ |
| 247 | } while (0) |
| 248 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 249 | /* Used to check an Lua function type in the stack. It creates and |
| 250 | * returns a reference of the function. This function throws an |
| 251 | * error if the rgument is not a "function". |
| 252 | */ |
| 253 | __LJMP unsigned int hlua_checkfunction(lua_State *L, int argno) |
| 254 | { |
| 255 | if (!lua_isfunction(L, argno)) { |
| 256 | const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, -1)); |
| 257 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 258 | } |
| 259 | lua_pushvalue(L, argno); |
| 260 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 261 | } |
| 262 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 263 | /* Return the string that is of the top of the stack. */ |
| 264 | const char *hlua_get_top_error_string(lua_State *L) |
| 265 | { |
| 266 | if (lua_gettop(L) < 1) |
| 267 | return "unknown error"; |
| 268 | if (lua_type(L, -1) != LUA_TSTRING) |
| 269 | return "unknown error"; |
| 270 | return lua_tostring(L, -1); |
| 271 | } |
| 272 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 273 | /* This function check the number of arguments available in the |
| 274 | * stack. If the number of arguments available is not the same |
| 275 | * then <nb> an error is throwed. |
| 276 | */ |
| 277 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 278 | { |
| 279 | if (lua_gettop(L) == nb) |
| 280 | return; |
| 281 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 282 | } |
| 283 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 284 | /* This fucntion push an error string prefixed by the file name |
| 285 | * and the line number where the error is encountered. |
| 286 | */ |
| 287 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 288 | { |
| 289 | va_list argp; |
| 290 | va_start(argp, fmt); |
| 291 | luaL_where(L, 1); |
| 292 | lua_pushvfstring(L, fmt, argp); |
| 293 | va_end(argp); |
| 294 | lua_concat(L, 2); |
| 295 | return 1; |
| 296 | } |
| 297 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 298 | /* This function register a new signal. "lua" is the current lua |
| 299 | * execution context. It contains a pointer to the associated task. |
| 300 | * "link" is a list head attached to an other task that must be wake |
| 301 | * the lua task if an event occurs. This is useful with external |
| 302 | * events like TCP I/O or sleep functions. This funcion allocate |
| 303 | * memory for the signal. |
| 304 | */ |
Thierry FOURNIER | 847ca66 | 2016-12-16 13:07:22 +0100 | [diff] [blame] | 305 | static struct hlua_com *hlua_com_new(struct list *purge, struct list *event, struct task *wakeup) |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 306 | { |
| 307 | struct hlua_com *com = pool_alloc2(pool2_hlua_com); |
| 308 | if (!com) |
Thierry FOURNIER | 847ca66 | 2016-12-16 13:07:22 +0100 | [diff] [blame] | 309 | return NULL; |
| 310 | LIST_ADDQ(purge, &com->purge_me); |
| 311 | LIST_ADDQ(event, &com->wake_me); |
| 312 | com->task = wakeup; |
| 313 | return com; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | /* This function purge all the pending signals when the LUA execution |
| 317 | * is finished. This prevent than a coprocess try to wake a deleted |
| 318 | * task. This function remove the memory associated to the signal. |
| 319 | */ |
Thierry FOURNIER | 847ca66 | 2016-12-16 13:07:22 +0100 | [diff] [blame] | 320 | static void hlua_com_purge(struct list *purge) |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 321 | { |
| 322 | struct hlua_com *com, *back; |
| 323 | |
| 324 | /* Delete all pending communication signals. */ |
Thierry FOURNIER | 847ca66 | 2016-12-16 13:07:22 +0100 | [diff] [blame] | 325 | list_for_each_entry_safe(com, back, purge, purge_me) { |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 326 | LIST_DEL(&com->purge_me); |
| 327 | LIST_DEL(&com->wake_me); |
| 328 | pool_free2(pool2_hlua_com, com); |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | /* This function sends signals. It wakes all the tasks attached |
| 333 | * to a list head, and remove the signal, and free the used |
| 334 | * memory. |
| 335 | */ |
| 336 | static void hlua_com_wake(struct list *wake) |
| 337 | { |
| 338 | struct hlua_com *com, *back; |
| 339 | |
| 340 | /* Wake task and delete all pending communication signals. */ |
| 341 | list_for_each_entry_safe(com, back, wake, wake_me) { |
| 342 | LIST_DEL(&com->purge_me); |
| 343 | LIST_DEL(&com->wake_me); |
| 344 | task_wakeup(com->task, TASK_WOKEN_MSG); |
| 345 | pool_free2(pool2_hlua_com, com); |
| 346 | } |
| 347 | } |
| 348 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 349 | /* This functions is used with sample fetch and converters. It |
| 350 | * converts the HAProxy configuration argument in a lua stack |
| 351 | * values. |
| 352 | * |
| 353 | * It takes an array of "arg", and each entry of the array is |
| 354 | * converted and pushed in the LUA stack. |
| 355 | */ |
| 356 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 357 | { |
| 358 | switch (arg->type) { |
| 359 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 360 | case ARGT_TIME: |
| 361 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 362 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 363 | break; |
| 364 | |
| 365 | case ARGT_STR: |
| 366 | lua_pushlstring(L, arg->data.str.str, arg->data.str.len); |
| 367 | break; |
| 368 | |
| 369 | case ARGT_IPV4: |
| 370 | case ARGT_IPV6: |
| 371 | case ARGT_MSK4: |
| 372 | case ARGT_MSK6: |
| 373 | case ARGT_FE: |
| 374 | case ARGT_BE: |
| 375 | case ARGT_TAB: |
| 376 | case ARGT_SRV: |
| 377 | case ARGT_USR: |
| 378 | case ARGT_MAP: |
| 379 | default: |
| 380 | lua_pushnil(L); |
| 381 | break; |
| 382 | } |
| 383 | return 1; |
| 384 | } |
| 385 | |
| 386 | /* This function take one entrie in an LUA stack at the index "ud", |
| 387 | * and try to convert it in an HAProxy argument entry. This is useful |
| 388 | * with sample fetch wrappers. The input arguments are gived to the |
| 389 | * lua wrapper and converted as arg list by thi function. |
| 390 | */ |
| 391 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 392 | { |
| 393 | switch (lua_type(L, ud)) { |
| 394 | |
| 395 | case LUA_TNUMBER: |
| 396 | case LUA_TBOOLEAN: |
| 397 | arg->type = ARGT_SINT; |
| 398 | arg->data.sint = lua_tointeger(L, ud); |
| 399 | break; |
| 400 | |
| 401 | case LUA_TSTRING: |
| 402 | arg->type = ARGT_STR; |
| 403 | arg->data.str.str = (char *)lua_tolstring(L, ud, (size_t *)&arg->data.str.len); |
| 404 | break; |
| 405 | |
| 406 | case LUA_TUSERDATA: |
| 407 | case LUA_TNIL: |
| 408 | case LUA_TTABLE: |
| 409 | case LUA_TFUNCTION: |
| 410 | case LUA_TTHREAD: |
| 411 | case LUA_TLIGHTUSERDATA: |
| 412 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 413 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 414 | break; |
| 415 | } |
| 416 | return 1; |
| 417 | } |
| 418 | |
| 419 | /* the following functions are used to convert a struct sample |
| 420 | * in Lua type. This useful to convert the return of the |
| 421 | * fetchs or converters. |
| 422 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 423 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 424 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 425 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 426 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 427 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 428 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 429 | break; |
| 430 | |
| 431 | case SMP_T_BIN: |
| 432 | case SMP_T_STR: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 433 | 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] | 434 | break; |
| 435 | |
| 436 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 437 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 438 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 439 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 440 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 441 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 442 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 443 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 444 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 445 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 446 | case HTTP_METH_OTHER: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 447 | 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] | 448 | break; |
| 449 | default: |
| 450 | lua_pushnil(L); |
| 451 | break; |
| 452 | } |
| 453 | break; |
| 454 | |
| 455 | case SMP_T_IPV4: |
| 456 | case SMP_T_IPV6: |
| 457 | 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] | 458 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 459 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 460 | 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] | 461 | else |
| 462 | lua_pushnil(L); |
| 463 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 464 | default: |
| 465 | lua_pushnil(L); |
| 466 | break; |
| 467 | } |
| 468 | return 1; |
| 469 | } |
| 470 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 471 | /* the following functions are used to convert a struct sample |
| 472 | * in Lua strings. This is useful to convert the return of the |
| 473 | * fetchs or converters. |
| 474 | */ |
| 475 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 476 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 477 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 478 | |
| 479 | case SMP_T_BIN: |
| 480 | case SMP_T_STR: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 481 | 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] | 482 | break; |
| 483 | |
| 484 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 485 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 486 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 487 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 488 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 489 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 490 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 491 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 492 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 493 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 494 | case HTTP_METH_OTHER: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 495 | 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] | 496 | break; |
| 497 | default: |
| 498 | lua_pushstring(L, ""); |
| 499 | break; |
| 500 | } |
| 501 | break; |
| 502 | |
| 503 | case SMP_T_SINT: |
| 504 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 505 | case SMP_T_IPV4: |
| 506 | case SMP_T_IPV6: |
| 507 | 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] | 508 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 509 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 510 | 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] | 511 | else |
| 512 | lua_pushstring(L, ""); |
| 513 | break; |
| 514 | default: |
| 515 | lua_pushstring(L, ""); |
| 516 | break; |
| 517 | } |
| 518 | return 1; |
| 519 | } |
| 520 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 521 | /* the following functions are used to convert an Lua type in a |
| 522 | * struct sample. This is useful to provide data from a converter |
| 523 | * to the LUA code. |
| 524 | */ |
| 525 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 526 | { |
| 527 | switch (lua_type(L, ud)) { |
| 528 | |
| 529 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 530 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 531 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 532 | break; |
| 533 | |
| 534 | |
| 535 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 536 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 537 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 538 | break; |
| 539 | |
| 540 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 541 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 542 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 543 | 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] | 544 | break; |
| 545 | |
| 546 | case LUA_TUSERDATA: |
| 547 | case LUA_TNIL: |
| 548 | case LUA_TTABLE: |
| 549 | case LUA_TFUNCTION: |
| 550 | case LUA_TTHREAD: |
| 551 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 552 | case LUA_TNONE: |
| 553 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 554 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 555 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 556 | break; |
| 557 | } |
| 558 | return 1; |
| 559 | } |
| 560 | |
| 561 | /* This function check the "argp" builded by another conversion function |
| 562 | * is in accord with the expected argp defined by the "mask". The fucntion |
| 563 | * 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] | 564 | * |
| 565 | * This function assumes thant the argp argument contains ARGM_NBARGS + 1 |
| 566 | * entries. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 567 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 568 | __LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp, |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 569 | uint64_t mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 570 | { |
| 571 | int min_arg; |
| 572 | int idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 573 | struct proxy *px; |
| 574 | char *sname, *pname; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 575 | |
| 576 | idx = 0; |
| 577 | min_arg = ARGM(mask); |
| 578 | mask >>= ARGM_BITS; |
| 579 | |
| 580 | while (1) { |
| 581 | |
| 582 | /* Check oversize. */ |
| 583 | if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) { |
Cyril Bonté | 577a36a | 2015-03-02 00:08:38 +0100 | [diff] [blame] | 584 | WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | /* Check for mandatory arguments. */ |
| 588 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 589 | if (idx < min_arg) { |
| 590 | |
| 591 | /* If miss other argument than the first one, we return an error. */ |
| 592 | if (idx > 0) |
| 593 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 594 | |
| 595 | /* If first argument have a certain type, some default values |
| 596 | * may be used. See the function smp_resolve_args(). |
| 597 | */ |
| 598 | switch (mask & ARGT_MASK) { |
| 599 | |
| 600 | case ARGT_FE: |
| 601 | if (!(p->cap & PR_CAP_FE)) |
| 602 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 603 | argp[idx].data.prx = p; |
| 604 | argp[idx].type = ARGT_FE; |
| 605 | argp[idx+1].type = ARGT_STOP; |
| 606 | break; |
| 607 | |
| 608 | case ARGT_BE: |
| 609 | if (!(p->cap & PR_CAP_BE)) |
| 610 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 611 | argp[idx].data.prx = p; |
| 612 | argp[idx].type = ARGT_BE; |
| 613 | argp[idx+1].type = ARGT_STOP; |
| 614 | break; |
| 615 | |
| 616 | case ARGT_TAB: |
| 617 | argp[idx].data.prx = p; |
| 618 | argp[idx].type = ARGT_TAB; |
| 619 | argp[idx+1].type = ARGT_STOP; |
| 620 | break; |
| 621 | |
| 622 | default: |
| 623 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 624 | break; |
| 625 | } |
| 626 | } |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 627 | return 0; |
| 628 | } |
| 629 | |
| 630 | /* Check for exceed the number of requiered argument. */ |
| 631 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 632 | argp[idx].type != ARGT_STOP) { |
| 633 | WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected")); |
| 634 | } |
| 635 | |
| 636 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 637 | argp[idx].type == ARGT_STOP) { |
| 638 | return 0; |
| 639 | } |
| 640 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 641 | /* Convert some argument types. */ |
| 642 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 643 | case ARGT_SINT: |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 644 | if (argp[idx].type != ARGT_SINT) |
| 645 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
| 646 | argp[idx].type = ARGT_SINT; |
| 647 | break; |
| 648 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 649 | case ARGT_TIME: |
| 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_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 653 | break; |
| 654 | |
| 655 | case ARGT_SIZE: |
| 656 | if (argp[idx].type != ARGT_SINT) |
| 657 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 658 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 659 | break; |
| 660 | |
| 661 | case ARGT_FE: |
| 662 | if (argp[idx].type != ARGT_STR) |
| 663 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
| 664 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 665 | trash.str[argp[idx].data.str.len] = 0; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 666 | argp[idx].data.prx = proxy_fe_by_name(trash.str); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 667 | if (!argp[idx].data.prx) |
| 668 | WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist")); |
| 669 | argp[idx].type = ARGT_FE; |
| 670 | break; |
| 671 | |
| 672 | case ARGT_BE: |
| 673 | if (argp[idx].type != ARGT_STR) |
| 674 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
| 675 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 676 | trash.str[argp[idx].data.str.len] = 0; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 677 | argp[idx].data.prx = proxy_be_by_name(trash.str); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 678 | if (!argp[idx].data.prx) |
| 679 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 680 | argp[idx].type = ARGT_BE; |
| 681 | break; |
| 682 | |
| 683 | case ARGT_TAB: |
| 684 | if (argp[idx].type != ARGT_STR) |
| 685 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
| 686 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 687 | trash.str[argp[idx].data.str.len] = 0; |
Willy Tarreau | e2dc1fa | 2015-05-26 12:08:07 +0200 | [diff] [blame] | 688 | argp[idx].data.prx = proxy_tbl_by_name(trash.str); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 689 | if (!argp[idx].data.prx) |
| 690 | WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist")); |
| 691 | argp[idx].type = ARGT_TAB; |
| 692 | break; |
| 693 | |
| 694 | case ARGT_SRV: |
| 695 | if (argp[idx].type != ARGT_STR) |
| 696 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
| 697 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 698 | trash.str[argp[idx].data.str.len] = 0; |
| 699 | sname = strrchr(trash.str, '/'); |
| 700 | if (sname) { |
| 701 | *sname++ = '\0'; |
| 702 | pname = trash.str; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 703 | px = proxy_be_by_name(pname); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 704 | if (!px) |
| 705 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 706 | } |
| 707 | else { |
| 708 | sname = trash.str; |
| 709 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 710 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 711 | argp[idx].data.srv = findserver(px, sname); |
| 712 | if (!argp[idx].data.srv) |
| 713 | WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist")); |
| 714 | argp[idx].type = ARGT_SRV; |
| 715 | break; |
| 716 | |
| 717 | case ARGT_IPV4: |
| 718 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 719 | trash.str[argp[idx].data.str.len] = 0; |
| 720 | if (inet_pton(AF_INET, trash.str, &argp[idx].data.ipv4)) |
| 721 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address")); |
| 722 | argp[idx].type = ARGT_IPV4; |
| 723 | break; |
| 724 | |
| 725 | case ARGT_MSK4: |
| 726 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 727 | trash.str[argp[idx].data.str.len] = 0; |
| 728 | if (!str2mask(trash.str, &argp[idx].data.ipv4)) |
| 729 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask")); |
| 730 | argp[idx].type = ARGT_MSK4; |
| 731 | break; |
| 732 | |
| 733 | case ARGT_IPV6: |
| 734 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 735 | trash.str[argp[idx].data.str.len] = 0; |
| 736 | if (inet_pton(AF_INET6, trash.str, &argp[idx].data.ipv6)) |
| 737 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address")); |
| 738 | argp[idx].type = ARGT_IPV6; |
| 739 | break; |
| 740 | |
| 741 | case ARGT_MSK6: |
| 742 | case ARGT_MAP: |
| 743 | case ARGT_REG: |
| 744 | case ARGT_USR: |
| 745 | WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 746 | break; |
| 747 | } |
| 748 | |
| 749 | /* Check for type of argument. */ |
| 750 | if ((mask & ARGT_MASK) != argp[idx].type) { |
| 751 | const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 752 | arg_type_names[(mask & ARGT_MASK)], |
| 753 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 754 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 755 | } |
| 756 | |
| 757 | /* Next argument. */ |
| 758 | mask >>= ARGT_BITS; |
| 759 | idx++; |
| 760 | } |
| 761 | } |
| 762 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 763 | /* |
| 764 | * The following functions are used to make correspondance between the the |
| 765 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 766 | * |
| 767 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 768 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 769 | */ |
| 770 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 771 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 772 | struct hlua **hlua = lua_getextraspace(L); |
| 773 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 774 | } |
| 775 | static inline void hlua_sethlua(struct hlua *hlua) |
| 776 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 777 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 778 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 779 | } |
| 780 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 781 | /* This function is used to send logs. It try to send on screen (stderr) |
| 782 | * and on the default syslog server. |
| 783 | */ |
| 784 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 785 | { |
| 786 | struct tm tm; |
| 787 | char *p; |
| 788 | |
| 789 | /* Cleanup the log message. */ |
| 790 | p = trash.str; |
| 791 | for (; *msg != '\0'; msg++, p++) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 792 | if (p >= trash.str + trash.size - 1) { |
| 793 | /* Break the message if exceed the buffer size. */ |
| 794 | *(p-4) = ' '; |
| 795 | *(p-3) = '.'; |
| 796 | *(p-2) = '.'; |
| 797 | *(p-1) = '.'; |
| 798 | break; |
| 799 | } |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 800 | if (isprint(*msg)) |
| 801 | *p = *msg; |
| 802 | else |
| 803 | *p = '.'; |
| 804 | } |
| 805 | *p = '\0'; |
| 806 | |
Thierry FOURNIER | 5554e29 | 2015-09-09 11:21:37 +0200 | [diff] [blame] | 807 | send_log(px, level, "%s\n", trash.str); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 808 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 809 | get_localtime(date.tv_sec, &tm); |
| 810 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 811 | log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, |
| 812 | (int)getpid(), trash.str); |
| 813 | fflush(stderr); |
| 814 | } |
| 815 | } |
| 816 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 817 | /* This function just ensure that the yield will be always |
| 818 | * returned with a timeout and permit to set some flags |
| 819 | */ |
| 820 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 821 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 822 | { |
| 823 | struct hlua *hlua = hlua_gethlua(L); |
| 824 | |
| 825 | /* Set the wake timeout. If timeout is required, we set |
| 826 | * the expiration time. |
| 827 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 828 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 829 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 830 | hlua->flags |= flags; |
| 831 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 832 | /* Process the yield. */ |
| 833 | WILL_LJMP(lua_yieldk(L, nresults, ctx, k)); |
| 834 | } |
| 835 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 836 | /* This function initialises the Lua environment stored in the stream. |
| 837 | * 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] | 838 | * 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] | 839 | * |
| 840 | * This function is particular. it initialises a new Lua thread. If the |
| 841 | * initialisation fails (example: out of memory error), the lua function |
| 842 | * throws an error (longjmp). |
| 843 | * |
| 844 | * This function manipulates two Lua stack: the main and the thread. Only |
| 845 | * the main stack can fail. The thread is not manipulated. This function |
| 846 | * MUST NOT manipulate the created thread stack state, because is not |
| 847 | * proctected agains error throwed by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 848 | */ |
| 849 | int hlua_ctx_init(struct hlua *lua, struct task *task) |
| 850 | { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 851 | if (!SET_SAFE_LJMP(gL.T)) { |
| 852 | lua->Tref = LUA_REFNIL; |
| 853 | return 0; |
| 854 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 855 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 856 | lua->flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 857 | LIST_INIT(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 858 | lua->T = lua_newthread(gL.T); |
| 859 | if (!lua->T) { |
| 860 | lua->Tref = LUA_REFNIL; |
| 861 | return 0; |
| 862 | } |
| 863 | hlua_sethlua(lua); |
| 864 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 865 | lua->task = task; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 866 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 867 | return 1; |
| 868 | } |
| 869 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 870 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 871 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 872 | * is not freed. |
| 873 | */ |
| 874 | void hlua_ctx_destroy(struct hlua *lua) |
| 875 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 876 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 877 | return; |
| 878 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 879 | if (!lua->T) |
| 880 | goto end; |
| 881 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 882 | /* Purge all the pending signals. */ |
Thierry FOURNIER | 847ca66 | 2016-12-16 13:07:22 +0100 | [diff] [blame] | 883 | hlua_com_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 884 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 885 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 886 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 887 | |
| 888 | /* Forces a garbage collecting process. If the Lua program is finished |
| 889 | * without error, we run the GC on the thread pointer. Its freed all |
| 890 | * the unused memory. |
| 891 | * If the thread is finnish with an error or is currently yielded, |
| 892 | * it seems that the GC applied on the thread doesn't clean anything, |
| 893 | * so e run the GC on the main thread. |
| 894 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 895 | * the garbage collection. |
| 896 | */ |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 897 | if (lua->flags & HLUA_MUST_GC) { |
| 898 | lua_gc(lua->T, LUA_GCCOLLECT, 0); |
| 899 | if (lua_status(lua->T) != LUA_OK) |
| 900 | lua_gc(gL.T, LUA_GCCOLLECT, 0); |
| 901 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 902 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 903 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 904 | |
| 905 | end: |
| 906 | pool_free2(pool2_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | /* This function is used to restore the Lua context when a coroutine |
| 910 | * fails. This function copy the common memory between old coroutine |
| 911 | * and the new coroutine. The old coroutine is destroyed, and its |
| 912 | * replaced by the new coroutine. |
| 913 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 914 | * as string error message and it is copied in the new stack. |
| 915 | */ |
| 916 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 917 | { |
| 918 | lua_State *T; |
| 919 | int new_ref; |
| 920 | |
| 921 | /* Renew the main LUA stack doesn't have sense. */ |
| 922 | if (lua == &gL) |
| 923 | return 0; |
| 924 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 925 | /* New Lua coroutine. */ |
| 926 | T = lua_newthread(gL.T); |
| 927 | if (!T) |
| 928 | return 0; |
| 929 | |
| 930 | /* Copy last error message. */ |
| 931 | if (keep_msg) |
| 932 | lua_xmove(lua->T, T, 1); |
| 933 | |
| 934 | /* Copy data between the coroutines. */ |
| 935 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 936 | lua_xmove(lua->T, T, 1); |
| 937 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */ |
| 938 | |
| 939 | /* Destroy old data. */ |
| 940 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 941 | |
| 942 | /* The thread is garbage collected by Lua. */ |
| 943 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 944 | |
| 945 | /* Fill the struct with the new coroutine values. */ |
| 946 | lua->Mref = new_ref; |
| 947 | lua->T = T; |
| 948 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 949 | |
| 950 | /* Set context. */ |
| 951 | hlua_sethlua(lua); |
| 952 | |
| 953 | return 1; |
| 954 | } |
| 955 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 956 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 957 | { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 958 | struct hlua *hlua = hlua_gethlua(L); |
| 959 | |
| 960 | /* Lua cannot yield when its returning from a function, |
| 961 | * so, we can fix the interrupt hook to 1 instruction, |
| 962 | * expecting that the function is finnished. |
| 963 | */ |
| 964 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 965 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 966 | return; |
| 967 | } |
| 968 | |
| 969 | /* restore the interrupt condition. */ |
| 970 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 971 | |
| 972 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 973 | * If the state is not yieldable, trying yield causes an error. |
| 974 | */ |
| 975 | if (lua_isyieldable(L)) |
| 976 | WILL_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
| 977 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 978 | /* If we cannot yield, update the clock and check the timeout. */ |
| 979 | tv_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 980 | hlua->run_time += now_ms - hlua->start_time; |
| 981 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 982 | lua_pushfstring(L, "execution timeout"); |
| 983 | WILL_LJMP(lua_error(L)); |
| 984 | } |
| 985 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 986 | /* Update the start time. */ |
| 987 | hlua->start_time = now_ms; |
| 988 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 989 | /* Try to interrupt the process at the end of the current |
| 990 | * unyieldable function. |
| 991 | */ |
| 992 | 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] | 993 | } |
| 994 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 995 | /* This function start or resumes the Lua stack execution. If the flag |
| 996 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 997 | * The function return an error. |
| 998 | * |
| 999 | * The function can returns 4 values: |
| 1000 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1001 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1002 | * task wakeup. |
| 1003 | * - HLUA_E_ERRMSG : An error has occured, an error message is set in |
| 1004 | * the top of the stack. |
| 1005 | * - HLUA_E_ERR : An error has occured without error message. |
| 1006 | * |
| 1007 | * If an error occured, the stack is renewed and it is ready to run new |
| 1008 | * LUA code. |
| 1009 | */ |
| 1010 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1011 | { |
| 1012 | int ret; |
| 1013 | const char *msg; |
| 1014 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1015 | /* Initialise run time counter. */ |
| 1016 | if (!HLUA_IS_RUNNING(lua)) |
| 1017 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1018 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1019 | resume_execution: |
| 1020 | |
| 1021 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1022 | * instructions. it is used for preventing infinite loops. |
| 1023 | */ |
| 1024 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1025 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1026 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1027 | HLUA_SET_RUN(lua); |
| 1028 | HLUA_CLR_CTRLYIELD(lua); |
| 1029 | HLUA_CLR_WAKERESWR(lua); |
| 1030 | HLUA_CLR_WAKEREQWR(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1031 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1032 | /* Update the start time. */ |
| 1033 | lua->start_time = now_ms; |
| 1034 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1035 | /* Call the function. */ |
| 1036 | ret = lua_resume(lua->T, gL.T, lua->nargs); |
| 1037 | switch (ret) { |
| 1038 | |
| 1039 | case LUA_OK: |
| 1040 | ret = HLUA_E_OK; |
| 1041 | break; |
| 1042 | |
| 1043 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1044 | /* Check if the execution timeout is expired. It it is the case, we |
| 1045 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1046 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1047 | tv_update_date(0, 1); |
| 1048 | lua->run_time += now_ms - lua->start_time; |
| 1049 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1050 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1051 | if (!lua_checkstack(lua->T, 1)) { |
| 1052 | ret = HLUA_E_ERR; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1053 | break; |
| 1054 | } |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1055 | lua_pushfstring(lua->T, "execution timeout"); |
| 1056 | ret = HLUA_E_ERRMSG; |
| 1057 | break; |
| 1058 | } |
| 1059 | /* Process the forced yield. if the general yield is not allowed or |
| 1060 | * if no task were associated this the current Lua execution |
| 1061 | * coroutine, we resume the execution. Else we want to return in the |
| 1062 | * scheduler and we want to be waked up again, to continue the |
| 1063 | * current Lua execution. So we schedule our own task. |
| 1064 | */ |
| 1065 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1066 | if (!yield_allowed || !lua->task) |
| 1067 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1068 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1069 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1070 | if (!yield_allowed) { |
| 1071 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1072 | if (!lua_checkstack(lua->T, 1)) { |
| 1073 | ret = HLUA_E_ERR; |
| 1074 | break; |
| 1075 | } |
| 1076 | lua_pushfstring(lua->T, "yield not allowed"); |
| 1077 | ret = HLUA_E_ERRMSG; |
| 1078 | break; |
| 1079 | } |
| 1080 | ret = HLUA_E_AGAIN; |
| 1081 | break; |
| 1082 | |
| 1083 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1084 | |
| 1085 | /* Special exit case. The traditionnal exit is returned as an error |
| 1086 | * because the errors ares the only one mean to return immediately |
| 1087 | * from and lua execution. |
| 1088 | */ |
| 1089 | if (lua->flags & HLUA_EXIT) { |
| 1090 | ret = HLUA_E_OK; |
Thierry FOURNIER | e1587b3 | 2015-08-28 09:54:13 +0200 | [diff] [blame] | 1091 | hlua_ctx_renew(lua, 0); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1092 | break; |
| 1093 | } |
| 1094 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1095 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1096 | if (!lua_checkstack(lua->T, 1)) { |
| 1097 | ret = HLUA_E_ERR; |
| 1098 | break; |
| 1099 | } |
| 1100 | msg = lua_tostring(lua->T, -1); |
| 1101 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1102 | lua_pop(lua->T, 1); |
| 1103 | if (msg) |
| 1104 | lua_pushfstring(lua->T, "runtime error: %s", msg); |
| 1105 | else |
| 1106 | lua_pushfstring(lua->T, "unknown runtime error"); |
| 1107 | ret = HLUA_E_ERRMSG; |
| 1108 | break; |
| 1109 | |
| 1110 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1111 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1112 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1113 | if (!lua_checkstack(lua->T, 1)) { |
| 1114 | ret = HLUA_E_ERR; |
| 1115 | break; |
| 1116 | } |
| 1117 | lua_pushfstring(lua->T, "out of memory error"); |
| 1118 | ret = HLUA_E_ERRMSG; |
| 1119 | break; |
| 1120 | |
| 1121 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1122 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1123 | if (!lua_checkstack(lua->T, 1)) { |
| 1124 | ret = HLUA_E_ERR; |
| 1125 | break; |
| 1126 | } |
| 1127 | msg = lua_tostring(lua->T, -1); |
| 1128 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1129 | lua_pop(lua->T, 1); |
| 1130 | if (msg) |
| 1131 | lua_pushfstring(lua->T, "message handler error: %s", msg); |
| 1132 | else |
| 1133 | lua_pushfstring(lua->T, "message handler error"); |
| 1134 | ret = HLUA_E_ERRMSG; |
| 1135 | break; |
| 1136 | |
| 1137 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1138 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1139 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1140 | if (!lua_checkstack(lua->T, 1)) { |
| 1141 | ret = HLUA_E_ERR; |
| 1142 | break; |
| 1143 | } |
| 1144 | lua_pushfstring(lua->T, "unknonwn error"); |
| 1145 | ret = HLUA_E_ERRMSG; |
| 1146 | break; |
| 1147 | } |
| 1148 | |
Thierry FOURNIER | 6ab4d8e | 2015-09-27 22:17:19 +0200 | [diff] [blame] | 1149 | /* This GC permits to destroy some object when a Lua timeout strikes. */ |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1150 | if (lua->flags & HLUA_MUST_GC && |
| 1151 | ret != HLUA_E_AGAIN) |
Thierry FOURNIER | 6ab4d8e | 2015-09-27 22:17:19 +0200 | [diff] [blame] | 1152 | lua_gc(lua->T, LUA_GCCOLLECT, 0); |
| 1153 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1154 | switch (ret) { |
| 1155 | case HLUA_E_AGAIN: |
| 1156 | break; |
| 1157 | |
| 1158 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 847ca66 | 2016-12-16 13:07:22 +0100 | [diff] [blame] | 1159 | hlua_com_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1160 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1161 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1162 | break; |
| 1163 | |
| 1164 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1165 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 847ca66 | 2016-12-16 13:07:22 +0100 | [diff] [blame] | 1166 | hlua_com_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1167 | hlua_ctx_renew(lua, 0); |
| 1168 | break; |
| 1169 | |
| 1170 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1171 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 847ca66 | 2016-12-16 13:07:22 +0100 | [diff] [blame] | 1172 | hlua_com_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1173 | break; |
| 1174 | } |
| 1175 | |
| 1176 | return ret; |
| 1177 | } |
| 1178 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1179 | /* This function exit the current code. */ |
| 1180 | __LJMP static int hlua_done(lua_State *L) |
| 1181 | { |
| 1182 | struct hlua *hlua = hlua_gethlua(L); |
| 1183 | |
| 1184 | hlua->flags |= HLUA_EXIT; |
| 1185 | WILL_LJMP(lua_error(L)); |
| 1186 | |
| 1187 | return 0; |
| 1188 | } |
| 1189 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1190 | /* This function is an LUA binding. It provides a function |
| 1191 | * for deleting ACL from a referenced ACL file. |
| 1192 | */ |
| 1193 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1194 | { |
| 1195 | const char *name; |
| 1196 | const char *key; |
| 1197 | struct pat_ref *ref; |
| 1198 | |
| 1199 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1200 | |
| 1201 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1202 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1203 | |
| 1204 | ref = pat_ref_lookup(name); |
| 1205 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1206 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1207 | |
| 1208 | pat_ref_delete(ref, key); |
| 1209 | return 0; |
| 1210 | } |
| 1211 | |
| 1212 | /* This function is an LUA binding. It provides a function |
| 1213 | * for deleting map entry from a referenced map file. |
| 1214 | */ |
| 1215 | static int hlua_del_map(lua_State *L) |
| 1216 | { |
| 1217 | const char *name; |
| 1218 | const char *key; |
| 1219 | struct pat_ref *ref; |
| 1220 | |
| 1221 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1222 | |
| 1223 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1224 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1225 | |
| 1226 | ref = pat_ref_lookup(name); |
| 1227 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1228 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1229 | |
| 1230 | pat_ref_delete(ref, key); |
| 1231 | return 0; |
| 1232 | } |
| 1233 | |
| 1234 | /* This function is an LUA binding. It provides a function |
| 1235 | * for adding ACL pattern from a referenced ACL file. |
| 1236 | */ |
| 1237 | static int hlua_add_acl(lua_State *L) |
| 1238 | { |
| 1239 | const char *name; |
| 1240 | const char *key; |
| 1241 | struct pat_ref *ref; |
| 1242 | |
| 1243 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1244 | |
| 1245 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1246 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1247 | |
| 1248 | ref = pat_ref_lookup(name); |
| 1249 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1250 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1251 | |
| 1252 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1253 | pat_ref_add(ref, key, NULL, NULL); |
| 1254 | return 0; |
| 1255 | } |
| 1256 | |
| 1257 | /* This function is an LUA binding. It provides a function |
| 1258 | * for setting map pattern and sample from a referenced map |
| 1259 | * file. |
| 1260 | */ |
| 1261 | static int hlua_set_map(lua_State *L) |
| 1262 | { |
| 1263 | const char *name; |
| 1264 | const char *key; |
| 1265 | const char *value; |
| 1266 | struct pat_ref *ref; |
| 1267 | |
| 1268 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1269 | |
| 1270 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1271 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1272 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1273 | |
| 1274 | ref = pat_ref_lookup(name); |
| 1275 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1276 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1277 | |
| 1278 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1279 | pat_ref_set(ref, key, value, NULL); |
| 1280 | else |
| 1281 | pat_ref_add(ref, key, value, NULL); |
| 1282 | return 0; |
| 1283 | } |
| 1284 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1285 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1286 | * an integer or user data. This data is associated with a metatable. This |
| 1287 | * metatable have an original version registred in the global context with |
| 1288 | * the name of the object (_G[<name>] = <metable> ). |
| 1289 | * |
| 1290 | * A metable is a table that modify the standard behavior of a standard |
| 1291 | * access to the associated data. The entries of this new metatable are |
| 1292 | * defined as is: |
| 1293 | * |
| 1294 | * http://lua-users.org/wiki/MetatableEvents |
| 1295 | * |
| 1296 | * __index |
| 1297 | * |
| 1298 | * we access an absent field in a table, the result is nil. This is |
| 1299 | * true, but it is not the whole truth. Actually, such access triggers |
| 1300 | * the interpreter to look for an __index metamethod: If there is no |
| 1301 | * such method, as usually happens, then the access results in nil; |
| 1302 | * otherwise, the metamethod will provide the result. |
| 1303 | * |
| 1304 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1305 | * the key does not appear in the table, but the metatable has an __index |
| 1306 | * property: |
| 1307 | * |
| 1308 | * - if the value is a function, the function is called, passing in the |
| 1309 | * table and the key; the return value of that function is returned as |
| 1310 | * the result. |
| 1311 | * |
| 1312 | * - if the value is another table, the value of the key in that table is |
| 1313 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1314 | * table's metatable has an __index property, then it continues on up) |
| 1315 | * |
| 1316 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1317 | * |
| 1318 | * http://www.lua.org/pil/13.4.1.html |
| 1319 | * |
| 1320 | * __newindex |
| 1321 | * |
| 1322 | * Like __index, but control property assignment. |
| 1323 | * |
| 1324 | * __mode - Control weak references. A string value with one or both |
| 1325 | * of the characters 'k' and 'v' which specifies that the the |
| 1326 | * keys and/or values in the table are weak references. |
| 1327 | * |
| 1328 | * __call - Treat a table like a function. When a table is followed by |
| 1329 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1330 | * a __call key pointing to a function, that function is invoked |
| 1331 | * (passing any specified arguments) and the return value is |
| 1332 | * returned. |
| 1333 | * |
| 1334 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1335 | * called, if the metatable for myTable has a __metatable |
| 1336 | * key, the value of that key is returned instead of the |
| 1337 | * actual metatable. |
| 1338 | * |
| 1339 | * __tostring - Control string representation. When the builtin |
| 1340 | * "tostring( myTable )" function is called, if the metatable |
| 1341 | * for myTable has a __tostring property set to a function, |
| 1342 | * that function is invoked (passing myTable to it) and the |
| 1343 | * return value is used as the string representation. |
| 1344 | * |
| 1345 | * __len - Control table length. When the table length is requested using |
| 1346 | * the length operator ( '#' ), if the metatable for myTable has |
| 1347 | * a __len key pointing to a function, that function is invoked |
| 1348 | * (passing myTable to it) and the return value used as the value |
| 1349 | * of "#myTable". |
| 1350 | * |
| 1351 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1352 | * collected, if the metatable has a __gc field pointing to a |
| 1353 | * function, that function is first invoked, passing the userdata |
| 1354 | * to it. The __gc metamethod is not called for tables. |
| 1355 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1356 | * |
| 1357 | * Special metamethods for redefining standard operators: |
| 1358 | * http://www.lua.org/pil/13.1.html |
| 1359 | * |
| 1360 | * __add "+" |
| 1361 | * __sub "-" |
| 1362 | * __mul "*" |
| 1363 | * __div "/" |
| 1364 | * __unm "!" |
| 1365 | * __pow "^" |
| 1366 | * __concat ".." |
| 1367 | * |
| 1368 | * Special methods for redfining standar relations |
| 1369 | * http://www.lua.org/pil/13.2.html |
| 1370 | * |
| 1371 | * __eq "==" |
| 1372 | * __lt "<" |
| 1373 | * __le "<=" |
| 1374 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1375 | |
| 1376 | /* |
| 1377 | * |
| 1378 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1379 | * Class Map |
| 1380 | * |
| 1381 | * |
| 1382 | */ |
| 1383 | |
| 1384 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1385 | * a class session, otherwise it throws an error. |
| 1386 | */ |
| 1387 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1388 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1389 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1390 | } |
| 1391 | |
| 1392 | /* This function is the map constructor. It don't need |
| 1393 | * the class Map object. It creates and return a new Map |
| 1394 | * object. It must be called only during "body" or "init" |
| 1395 | * context because it process some filesystem accesses. |
| 1396 | */ |
| 1397 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1398 | { |
| 1399 | const char *fn; |
| 1400 | int match = PAT_MATCH_STR; |
| 1401 | struct sample_conv conv; |
| 1402 | const char *file = ""; |
| 1403 | int line = 0; |
| 1404 | lua_Debug ar; |
| 1405 | char *err = NULL; |
| 1406 | struct arg args[2]; |
| 1407 | |
| 1408 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1409 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1410 | |
| 1411 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1412 | |
| 1413 | if (lua_gettop(L) >= 2) { |
| 1414 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1415 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1416 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1417 | } |
| 1418 | |
| 1419 | /* Get Lua filename and line number. */ |
| 1420 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1421 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1422 | if (ar.currentline > 0) { /* is there info? */ |
| 1423 | file = ar.short_src; |
| 1424 | line = ar.currentline; |
| 1425 | } |
| 1426 | } |
| 1427 | |
| 1428 | /* fill fake sample_conv struct. */ |
| 1429 | conv.kw = ""; /* unused. */ |
| 1430 | conv.process = NULL; /* unused. */ |
| 1431 | conv.arg_mask = 0; /* unused. */ |
| 1432 | conv.val_args = NULL; /* unused. */ |
| 1433 | conv.out_type = SMP_T_STR; |
| 1434 | conv.private = (void *)(long)match; |
| 1435 | switch (match) { |
| 1436 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1437 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1438 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1439 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1440 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1441 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1442 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1443 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1444 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1445 | default: |
| 1446 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1447 | } |
| 1448 | |
| 1449 | /* fill fake args. */ |
| 1450 | args[0].type = ARGT_STR; |
| 1451 | args[0].data.str.str = (char *)fn; |
| 1452 | args[1].type = ARGT_STOP; |
| 1453 | |
| 1454 | /* load the map. */ |
| 1455 | if (!sample_load_map(args, &conv, file, line, &err)) { |
| 1456 | /* error case: we cant use luaL_error because we must |
| 1457 | * free the err variable. |
| 1458 | */ |
| 1459 | luaL_where(L, 1); |
| 1460 | lua_pushfstring(L, "'new': %s.", err); |
| 1461 | lua_concat(L, 2); |
| 1462 | free(err); |
| 1463 | WILL_LJMP(lua_error(L)); |
| 1464 | } |
| 1465 | |
| 1466 | /* create the lua object. */ |
| 1467 | lua_newtable(L); |
| 1468 | lua_pushlightuserdata(L, args[0].data.map); |
| 1469 | lua_rawseti(L, -2, 0); |
| 1470 | |
| 1471 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1472 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1473 | lua_setmetatable(L, -2); |
| 1474 | |
| 1475 | |
| 1476 | return 1; |
| 1477 | } |
| 1478 | |
| 1479 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1480 | { |
| 1481 | struct map_descriptor *desc; |
| 1482 | struct pattern *pat; |
| 1483 | struct sample smp; |
| 1484 | |
| 1485 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1486 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1487 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1488 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1489 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1490 | } |
| 1491 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1492 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1493 | smp.flags = SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1494 | 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] | 1495 | } |
| 1496 | |
| 1497 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1498 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1499 | if (str) |
| 1500 | lua_pushstring(L, ""); |
| 1501 | else |
| 1502 | lua_pushnil(L); |
| 1503 | return 1; |
| 1504 | } |
| 1505 | |
| 1506 | /* 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] | 1507 | 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] | 1508 | return 1; |
| 1509 | } |
| 1510 | |
| 1511 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1512 | { |
| 1513 | return _hlua_map_lookup(L, 0); |
| 1514 | } |
| 1515 | |
| 1516 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1517 | { |
| 1518 | return _hlua_map_lookup(L, 1); |
| 1519 | } |
| 1520 | |
| 1521 | /* |
| 1522 | * |
| 1523 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1524 | * Class Socket |
| 1525 | * |
| 1526 | * |
| 1527 | */ |
| 1528 | |
| 1529 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1530 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1531 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1532 | } |
| 1533 | |
| 1534 | /* This function is the handler called for each I/O on the established |
| 1535 | * connection. It is used for notify space avalaible to send or data |
| 1536 | * received. |
| 1537 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1538 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1539 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1540 | struct stream_interface *si = appctx->owner; |
Willy Tarreau | 50fe03b | 2014-11-28 13:59:31 +0100 | [diff] [blame] | 1541 | struct connection *c = objt_conn(si_opposite(si)->end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1542 | |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1543 | /* If the connection object is not avalaible, close all the |
| 1544 | * streams and wakeup everithing waiting for. |
| 1545 | */ |
| 1546 | if (!c) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1547 | si_shutw(si); |
| 1548 | si_shutr(si); |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 1549 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1550 | hlua_com_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1551 | hlua_com_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1552 | return; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1553 | } |
| 1554 | |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1555 | /* If we cant write, wakeup the pending write signals. */ |
| 1556 | if (channel_output_closed(si_ic(si))) |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1557 | hlua_com_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1558 | |
| 1559 | /* If we cant read, wakeup the pending read signals. */ |
| 1560 | if (channel_input_closed(si_oc(si))) |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1561 | hlua_com_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1562 | |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1563 | /* if the connection is not estabkished, inform the stream that we want |
| 1564 | * to be notified whenever the connection completes. |
| 1565 | */ |
| 1566 | if (!(c->flags & CO_FL_CONNECTED)) { |
| 1567 | si_applet_cant_get(si); |
| 1568 | si_applet_cant_put(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1569 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1570 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1571 | |
| 1572 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1573 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1574 | |
| 1575 | /* 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] | 1576 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1577 | hlua_com_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1578 | |
| 1579 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1580 | if (!channel_is_empty(si_oc(si))) |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1581 | hlua_com_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1582 | } |
| 1583 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1584 | /* This function is called when the "struct stream" is destroyed. |
| 1585 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1586 | * Wake all the pending signals. |
| 1587 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1588 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1589 | { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1590 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1591 | if (appctx->ctx.hlua_cosocket.socket) |
| 1592 | appctx->ctx.hlua_cosocket.socket->s = NULL; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1593 | |
| 1594 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1595 | hlua_com_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1596 | hlua_com_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1597 | } |
| 1598 | |
| 1599 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1600 | * uses this object. If the stream does not exists, just quit. |
| 1601 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1602 | * pending signal can rest in the read and write lists. destroy |
| 1603 | * it. |
| 1604 | */ |
| 1605 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1606 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1607 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1608 | struct appctx *appctx; |
| 1609 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1610 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1611 | |
| 1612 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1613 | if (!socket->s) |
| 1614 | return 0; |
| 1615 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1616 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1617 | appctx = objt_appctx(socket->s->si[0].end); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1618 | stream_shutdown(socket->s, SF_ERR_KILLED); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1619 | socket->s = NULL; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1620 | appctx->ctx.hlua_cosocket.socket = NULL; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1621 | |
| 1622 | return 0; |
| 1623 | } |
| 1624 | |
| 1625 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1626 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1627 | */ |
| 1628 | __LJMP static int hlua_socket_close(lua_State *L) |
| 1629 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1630 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1631 | struct appctx *appctx; |
| 1632 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1633 | MAY_LJMP(check_args(L, 1, "close")); |
| 1634 | |
| 1635 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1636 | if (!socket->s) |
| 1637 | return 0; |
| 1638 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1639 | /* Close the stream and remove the associated stop task. */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1640 | stream_shutdown(socket->s, SF_ERR_KILLED); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1641 | appctx = objt_appctx(socket->s->si[0].end); |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1642 | appctx->ctx.hlua_cosocket.socket = NULL; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1643 | socket->s = NULL; |
| 1644 | |
| 1645 | return 0; |
| 1646 | } |
| 1647 | |
| 1648 | /* This Lua function assumes that the stack contain three parameters. |
| 1649 | * 1 - USERDATA containing a struct socket |
| 1650 | * 2 - INTEGER with values of the macro defined below |
| 1651 | * If the integer is -1, we must read at most one line. |
| 1652 | * If the integer is -2, we ust read all the data until the |
| 1653 | * end of the stream. |
| 1654 | * If the integer is positive value, we must read a number of |
| 1655 | * bytes corresponding to this value. |
| 1656 | */ |
| 1657 | #define HLSR_READ_LINE (-1) |
| 1658 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1659 | __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] | 1660 | { |
| 1661 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1662 | int wanted = lua_tointeger(L, 2); |
| 1663 | struct hlua *hlua = hlua_gethlua(L); |
| 1664 | struct appctx *appctx; |
| 1665 | int len; |
| 1666 | int nblk; |
| 1667 | char *blk1; |
| 1668 | int len1; |
| 1669 | char *blk2; |
| 1670 | int len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1671 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1672 | struct channel *oc; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1673 | |
| 1674 | /* Check if this lua stack is schedulable. */ |
| 1675 | if (!hlua || !hlua->task) |
| 1676 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 1677 | "'frontend', 'backend' or 'task'")); |
| 1678 | |
| 1679 | /* check for connection closed. If some data where read, return it. */ |
| 1680 | if (!socket->s) |
| 1681 | goto connection_closed; |
| 1682 | |
Willy Tarreau | 94aa617 | 2015-03-13 14:19:06 +0100 | [diff] [blame] | 1683 | oc = &socket->s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1684 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1685 | /* Read line. */ |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1686 | nblk = bo_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1687 | if (nblk < 0) /* Connection close. */ |
| 1688 | goto connection_closed; |
| 1689 | if (nblk == 0) /* No data avalaible. */ |
| 1690 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1691 | |
| 1692 | /* remove final \r\n. */ |
| 1693 | if (nblk == 1) { |
| 1694 | if (blk1[len1-1] == '\n') { |
| 1695 | len1--; |
| 1696 | skip_at_end++; |
| 1697 | if (blk1[len1-1] == '\r') { |
| 1698 | len1--; |
| 1699 | skip_at_end++; |
| 1700 | } |
| 1701 | } |
| 1702 | } |
| 1703 | else { |
| 1704 | if (blk2[len2-1] == '\n') { |
| 1705 | len2--; |
| 1706 | skip_at_end++; |
| 1707 | if (blk2[len2-1] == '\r') { |
| 1708 | len2--; |
| 1709 | skip_at_end++; |
| 1710 | } |
| 1711 | } |
| 1712 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1713 | } |
| 1714 | |
| 1715 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1716 | /* Read all the available data. */ |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1717 | nblk = bo_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1718 | if (nblk < 0) /* Connection close. */ |
| 1719 | goto connection_closed; |
| 1720 | if (nblk == 0) /* No data avalaible. */ |
| 1721 | goto connection_empty; |
| 1722 | } |
| 1723 | |
| 1724 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1725 | /* Read a block of data. */ |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1726 | nblk = bo_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1727 | if (nblk < 0) /* Connection close. */ |
| 1728 | goto connection_closed; |
| 1729 | if (nblk == 0) /* No data avalaible. */ |
| 1730 | goto connection_empty; |
| 1731 | |
| 1732 | if (len1 > wanted) { |
| 1733 | nblk = 1; |
| 1734 | len1 = wanted; |
| 1735 | } if (nblk == 2 && len1 + len2 > wanted) |
| 1736 | len2 = wanted - len1; |
| 1737 | } |
| 1738 | |
| 1739 | len = len1; |
| 1740 | |
| 1741 | luaL_addlstring(&socket->b, blk1, len1); |
| 1742 | if (nblk == 2) { |
| 1743 | len += len2; |
| 1744 | luaL_addlstring(&socket->b, blk2, len2); |
| 1745 | } |
| 1746 | |
| 1747 | /* Consume data. */ |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1748 | bo_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1749 | |
| 1750 | /* Don't wait anything. */ |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 1751 | stream_int_notify(&socket->s->si[0]); |
| 1752 | stream_int_update_applet(&socket->s->si[0]); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1753 | |
| 1754 | /* If the pattern reclaim to read all the data |
| 1755 | * in the connection, got out. |
| 1756 | */ |
| 1757 | if (wanted == HLSR_READ_ALL) |
| 1758 | goto connection_empty; |
| 1759 | else if (wanted >= 0 && len < wanted) |
| 1760 | goto connection_empty; |
| 1761 | |
| 1762 | /* Return result. */ |
| 1763 | luaL_pushresult(&socket->b); |
| 1764 | return 1; |
| 1765 | |
| 1766 | connection_closed: |
| 1767 | |
| 1768 | /* If the buffer containds data. */ |
| 1769 | if (socket->b.n > 0) { |
| 1770 | luaL_pushresult(&socket->b); |
| 1771 | return 1; |
| 1772 | } |
| 1773 | lua_pushnil(L); |
| 1774 | lua_pushstring(L, "connection closed."); |
| 1775 | return 2; |
| 1776 | |
| 1777 | connection_empty: |
| 1778 | |
| 1779 | appctx = objt_appctx(socket->s->si[0].end); |
Thierry FOURNIER | 847ca66 | 2016-12-16 13:07:22 +0100 | [diff] [blame] | 1780 | if (!hlua_com_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1781 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 1782 | 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] | 1783 | return 0; |
| 1784 | } |
| 1785 | |
| 1786 | /* This Lus function gets two parameters. The first one can be string |
| 1787 | * or a number. If the string is "*l", the user require one line. If |
| 1788 | * the string is "*a", the user require all the content of the stream. |
| 1789 | * If the value is a number, the user require a number of bytes equal |
| 1790 | * to the value. The default value is "*l" (a line). |
| 1791 | * |
| 1792 | * This paraeter with a variable type is converted in integer. This |
| 1793 | * integer takes this values: |
| 1794 | * -1 : read a line |
| 1795 | * -2 : read all the stream |
| 1796 | * >0 : amount if bytes. |
| 1797 | * |
| 1798 | * The second parameter is optinal. It contains a string that must be |
| 1799 | * concatenated with the read data. |
| 1800 | */ |
| 1801 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 1802 | { |
| 1803 | int wanted = HLSR_READ_LINE; |
| 1804 | const char *pattern; |
| 1805 | int type; |
| 1806 | char *error; |
| 1807 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1808 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1809 | |
| 1810 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 1811 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 1812 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1813 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1814 | |
| 1815 | /* check for pattern. */ |
| 1816 | if (lua_gettop(L) >= 2) { |
| 1817 | type = lua_type(L, 2); |
| 1818 | if (type == LUA_TSTRING) { |
| 1819 | pattern = lua_tostring(L, 2); |
| 1820 | if (strcmp(pattern, "*a") == 0) |
| 1821 | wanted = HLSR_READ_ALL; |
| 1822 | else if (strcmp(pattern, "*l") == 0) |
| 1823 | wanted = HLSR_READ_LINE; |
| 1824 | else { |
| 1825 | wanted = strtoll(pattern, &error, 10); |
| 1826 | if (*error != '\0') |
| 1827 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 1828 | } |
| 1829 | } |
| 1830 | else if (type == LUA_TNUMBER) { |
| 1831 | wanted = lua_tointeger(L, 2); |
| 1832 | if (wanted < 0) |
| 1833 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 1834 | } |
| 1835 | } |
| 1836 | |
| 1837 | /* Set pattern. */ |
| 1838 | lua_pushinteger(L, wanted); |
| 1839 | lua_replace(L, 2); |
| 1840 | |
| 1841 | /* init bufffer, and fiil it wih prefix. */ |
| 1842 | luaL_buffinit(L, &socket->b); |
| 1843 | |
| 1844 | /* Check prefix. */ |
| 1845 | if (lua_gettop(L) >= 3) { |
| 1846 | if (lua_type(L, 3) != LUA_TSTRING) |
| 1847 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 1848 | pattern = lua_tolstring(L, 3, &len); |
| 1849 | luaL_addlstring(&socket->b, pattern, len); |
| 1850 | } |
| 1851 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1852 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1853 | } |
| 1854 | |
| 1855 | /* Write the Lua input string in the output buffer. |
| 1856 | * This fucntion returns a yield if no space are available. |
| 1857 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1858 | 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] | 1859 | { |
| 1860 | struct hlua_socket *socket; |
| 1861 | struct hlua *hlua = hlua_gethlua(L); |
| 1862 | struct appctx *appctx; |
| 1863 | size_t buf_len; |
| 1864 | const char *buf; |
| 1865 | int len; |
| 1866 | int send_len; |
| 1867 | int sent; |
| 1868 | |
| 1869 | /* Check if this lua stack is schedulable. */ |
| 1870 | if (!hlua || !hlua->task) |
| 1871 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 1872 | "'frontend', 'backend' or 'task'")); |
| 1873 | |
| 1874 | /* Get object */ |
| 1875 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1876 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1877 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1878 | |
| 1879 | /* Check for connection close. */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1880 | if (!socket->s || channel_output_closed(&socket->s->req)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1881 | lua_pushinteger(L, -1); |
| 1882 | return 1; |
| 1883 | } |
| 1884 | |
| 1885 | /* Update the input buffer data. */ |
| 1886 | buf += sent; |
| 1887 | send_len = buf_len - sent; |
| 1888 | |
| 1889 | /* All the data are sent. */ |
| 1890 | if (sent >= buf_len) |
| 1891 | return 1; /* Implicitly return the length sent. */ |
| 1892 | |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 1893 | /* Check if the buffer is avalaible because HAProxy doesn't allocate |
| 1894 | * the request buffer if its not required. |
| 1895 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1896 | if (socket->s->req.buf->size == 0) { |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 1897 | appctx = hlua->task->context; |
| 1898 | if (!channel_alloc_buffer(&socket->s->req, &appctx->buffer_wait)) |
| 1899 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 1900 | } |
| 1901 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1902 | /* Check for avalaible space. */ |
Willy Tarreau | 94aa617 | 2015-03-13 14:19:06 +0100 | [diff] [blame] | 1903 | len = buffer_total_space(socket->s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 1904 | if (len <= 0) { |
| 1905 | appctx = objt_appctx(socket->s->si[0].end); |
| 1906 | if (!hlua_com_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) |
| 1907 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1908 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 1909 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1910 | |
| 1911 | /* send data */ |
| 1912 | if (len < send_len) |
| 1913 | send_len = len; |
Willy Tarreau | 94aa617 | 2015-03-13 14:19:06 +0100 | [diff] [blame] | 1914 | len = bi_putblk(&socket->s->req, buf+sent, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1915 | |
| 1916 | /* "Not enough space" (-1), "Buffer too little to contain |
| 1917 | * the data" (-2) are not expected because the available length |
| 1918 | * is tested. |
| 1919 | * Other unknown error are also not expected. |
| 1920 | */ |
| 1921 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 1922 | if (len == -1) |
Willy Tarreau | 94aa617 | 2015-03-13 14:19:06 +0100 | [diff] [blame] | 1923 | socket->s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 1924 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1925 | MAY_LJMP(hlua_socket_close(L)); |
| 1926 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1927 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1928 | return 1; |
| 1929 | } |
| 1930 | |
| 1931 | /* update buffers. */ |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 1932 | stream_int_notify(&socket->s->si[0]); |
| 1933 | stream_int_update_applet(&socket->s->si[0]); |
| 1934 | |
Willy Tarreau | 94aa617 | 2015-03-13 14:19:06 +0100 | [diff] [blame] | 1935 | socket->s->req.rex = TICK_ETERNITY; |
| 1936 | socket->s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1937 | |
| 1938 | /* Update length sent. */ |
| 1939 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1940 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1941 | |
| 1942 | /* All the data buffer is sent ? */ |
| 1943 | if (sent + len >= buf_len) |
| 1944 | return 1; |
| 1945 | |
| 1946 | hlua_socket_write_yield_return: |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 1947 | 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] | 1948 | return 0; |
| 1949 | } |
| 1950 | |
| 1951 | /* This function initiate the send of data. It just check the input |
| 1952 | * parameters and push an integer in the Lua stack that contain the |
| 1953 | * amount of data writed in the buffer. This is used by the function |
| 1954 | * "hlua_socket_write_yield" that can yield. |
| 1955 | * |
| 1956 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 1957 | * the associated object. The second is a string buffer. The third is |
| 1958 | * a facultative integer that represents where is the buffer position |
| 1959 | * of the start of the data that can send. The first byte is the |
| 1960 | * position "1". The default value is "1". The fourth argument is a |
| 1961 | * facultative integer that represents where is the buffer position |
| 1962 | * of the end of the data that can send. The default is the last byte. |
| 1963 | */ |
| 1964 | static int hlua_socket_send(struct lua_State *L) |
| 1965 | { |
| 1966 | int i; |
| 1967 | int j; |
| 1968 | const char *buf; |
| 1969 | size_t buf_len; |
| 1970 | |
| 1971 | /* Check number of arguments. */ |
| 1972 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 1973 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 1974 | |
| 1975 | /* Get the string. */ |
| 1976 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 1977 | |
| 1978 | /* Get and check j. */ |
| 1979 | if (lua_gettop(L) == 4) { |
| 1980 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 1981 | if (j < 0) |
| 1982 | j = buf_len + j + 1; |
| 1983 | if (j > buf_len) |
| 1984 | j = buf_len + 1; |
| 1985 | lua_pop(L, 1); |
| 1986 | } |
| 1987 | else |
| 1988 | j = buf_len; |
| 1989 | |
| 1990 | /* Get and check i. */ |
| 1991 | if (lua_gettop(L) == 3) { |
| 1992 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 1993 | if (i < 0) |
| 1994 | i = buf_len + i + 1; |
| 1995 | if (i > buf_len) |
| 1996 | i = buf_len + 1; |
| 1997 | lua_pop(L, 1); |
| 1998 | } else |
| 1999 | i = 1; |
| 2000 | |
| 2001 | /* Check bth i and j. */ |
| 2002 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2003 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2004 | return 1; |
| 2005 | } |
| 2006 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2007 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2008 | return 1; |
| 2009 | } |
| 2010 | if (i == 0) |
| 2011 | i = 1; |
| 2012 | if (j == 0) |
| 2013 | j = 1; |
| 2014 | |
| 2015 | /* Pop the string. */ |
| 2016 | lua_pop(L, 1); |
| 2017 | |
| 2018 | /* Update the buffer length. */ |
| 2019 | buf += i - 1; |
| 2020 | buf_len = j - i + 1; |
| 2021 | lua_pushlstring(L, buf, buf_len); |
| 2022 | |
| 2023 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2024 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2025 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2026 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2027 | } |
| 2028 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2029 | #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] | 2030 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2031 | { |
| 2032 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2033 | int ret; |
| 2034 | int len; |
| 2035 | char *p; |
| 2036 | |
| 2037 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2038 | if (ret <= 0) { |
| 2039 | lua_pushnil(L); |
| 2040 | return 1; |
| 2041 | } |
| 2042 | |
| 2043 | if (ret == AF_UNIX) { |
| 2044 | lua_pushstring(L, buffer+1); |
| 2045 | return 1; |
| 2046 | } |
| 2047 | else if (ret == AF_INET6) { |
| 2048 | buffer[0] = '['; |
| 2049 | len = strlen(buffer); |
| 2050 | buffer[len] = ']'; |
| 2051 | len++; |
| 2052 | buffer[len] = ':'; |
| 2053 | len++; |
| 2054 | p = buffer; |
| 2055 | } |
| 2056 | else if (ret == AF_INET) { |
| 2057 | p = buffer + 1; |
| 2058 | len = strlen(p); |
| 2059 | p[len] = ':'; |
| 2060 | len++; |
| 2061 | } |
| 2062 | else { |
| 2063 | lua_pushnil(L); |
| 2064 | return 1; |
| 2065 | } |
| 2066 | |
| 2067 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2068 | lua_pushnil(L); |
| 2069 | return 1; |
| 2070 | } |
| 2071 | |
| 2072 | lua_pushstring(L, p); |
| 2073 | return 1; |
| 2074 | } |
| 2075 | |
| 2076 | /* Returns information about the peer of the connection. */ |
| 2077 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2078 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2079 | struct hlua_socket *socket; |
| 2080 | struct connection *conn; |
| 2081 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2082 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2083 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2084 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2085 | |
| 2086 | /* Check if the tcp object is avalaible. */ |
| 2087 | if (!socket->s) { |
| 2088 | lua_pushnil(L); |
| 2089 | return 1; |
| 2090 | } |
| 2091 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2092 | conn = objt_conn(socket->s->si[1].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2093 | if (!conn) { |
| 2094 | lua_pushnil(L); |
| 2095 | return 1; |
| 2096 | } |
| 2097 | |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2098 | conn_get_to_addr(conn); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2099 | if (!(conn->flags & CO_FL_ADDR_TO_SET)) { |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2100 | lua_pushnil(L); |
| 2101 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2102 | } |
| 2103 | |
| 2104 | return MAY_LJMP(hlua_socket_info(L, &conn->addr.to)); |
| 2105 | } |
| 2106 | |
| 2107 | /* Returns information about my connection side. */ |
| 2108 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2109 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2110 | struct hlua_socket *socket; |
| 2111 | struct connection *conn; |
| 2112 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2113 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2114 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2115 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2116 | |
| 2117 | /* Check if the tcp object is avalaible. */ |
| 2118 | if (!socket->s) { |
| 2119 | lua_pushnil(L); |
| 2120 | return 1; |
| 2121 | } |
| 2122 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2123 | conn = objt_conn(socket->s->si[1].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2124 | if (!conn) { |
| 2125 | lua_pushnil(L); |
| 2126 | return 1; |
| 2127 | } |
| 2128 | |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2129 | conn_get_from_addr(conn); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2130 | if (!(conn->flags & CO_FL_ADDR_FROM_SET)) { |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2131 | lua_pushnil(L); |
| 2132 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2133 | } |
| 2134 | |
| 2135 | return hlua_socket_info(L, &conn->addr.from); |
| 2136 | } |
| 2137 | |
| 2138 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2139 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2140 | .obj_type = OBJ_TYPE_APPLET, |
| 2141 | .name = "<LUA_TCP>", |
| 2142 | .fct = hlua_socket_handler, |
| 2143 | .release = hlua_socket_release, |
| 2144 | }; |
| 2145 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2146 | __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] | 2147 | { |
| 2148 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2149 | struct hlua *hlua = hlua_gethlua(L); |
| 2150 | struct appctx *appctx; |
| 2151 | |
| 2152 | /* Check for connection close. */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2153 | if (!hlua || !socket->s || channel_output_closed(&socket->s->req)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2154 | lua_pushnil(L); |
| 2155 | lua_pushstring(L, "Can't connect"); |
| 2156 | return 2; |
| 2157 | } |
| 2158 | |
| 2159 | appctx = objt_appctx(socket->s->si[0].end); |
| 2160 | |
| 2161 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2162 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2163 | lua_pushinteger(L, 1); |
| 2164 | return 1; |
| 2165 | } |
| 2166 | |
Thierry FOURNIER | 847ca66 | 2016-12-16 13:07:22 +0100 | [diff] [blame] | 2167 | if (!hlua_com_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2168 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 2169 | 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] | 2170 | return 0; |
| 2171 | } |
| 2172 | |
| 2173 | /* This function fail or initite the connection. */ |
| 2174 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2175 | { |
| 2176 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2177 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2178 | const char *ip; |
| 2179 | struct connection *conn; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2180 | struct hlua *hlua; |
| 2181 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2182 | int low, high; |
| 2183 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2184 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2185 | if (lua_gettop(L) < 2) |
| 2186 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2187 | |
| 2188 | /* Get args. */ |
| 2189 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2190 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2191 | if (lua_gettop(L) >= 3) |
| 2192 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2193 | |
Willy Tarreau | 973a542 | 2015-08-05 21:47:23 +0200 | [diff] [blame] | 2194 | conn = si_alloc_conn(&socket->s->si[1]); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2195 | if (!conn) |
| 2196 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
| 2197 | |
Willy Tarreau | 3adac08 | 2015-09-26 17:51:09 +0200 | [diff] [blame] | 2198 | /* needed for the connection not to be closed */ |
| 2199 | conn->target = socket->s->target; |
| 2200 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2201 | /* Parse ip address. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2202 | addr = str2sa_range(ip, &low, &high, NULL, NULL, NULL, 0); |
| 2203 | if (!addr) |
| 2204 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
| 2205 | if (low != high) |
| 2206 | WILL_LJMP(luaL_error(L, "connect: port ranges not supported : address '%s'", ip)); |
| 2207 | memcpy(&conn->addr.to, addr, sizeof(struct sockaddr_storage)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2208 | |
| 2209 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2210 | if (low == 0) { |
| 2211 | if (conn->addr.to.ss_family == AF_INET) { |
| 2212 | if (port == -1) |
| 2213 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
| 2214 | ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(port); |
| 2215 | } else if (conn->addr.to.ss_family == AF_INET6) { |
| 2216 | if (port == -1) |
| 2217 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
| 2218 | ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(port); |
| 2219 | } |
| 2220 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2221 | |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2222 | hlua = hlua_gethlua(L); |
| 2223 | appctx = objt_appctx(socket->s->si[0].end); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2224 | |
| 2225 | /* inform the stream that we want to be notified whenever the |
| 2226 | * connection completes. |
| 2227 | */ |
| 2228 | si_applet_cant_get(&socket->s->si[0]); |
| 2229 | si_applet_cant_put(&socket->s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2230 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2231 | |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2232 | hlua->flags |= HLUA_MUST_GC; |
| 2233 | |
Thierry FOURNIER | 847ca66 | 2016-12-16 13:07:22 +0100 | [diff] [blame] | 2234 | if (!hlua_com_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2235 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 2236 | 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] | 2237 | |
| 2238 | return 0; |
| 2239 | } |
| 2240 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2241 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2242 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2243 | { |
| 2244 | struct hlua_socket *socket; |
| 2245 | |
| 2246 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2247 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2248 | socket->s->target = &socket_ssl.obj_type; |
| 2249 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2250 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2251 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2252 | |
| 2253 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2254 | { |
| 2255 | return 0; |
| 2256 | } |
| 2257 | |
| 2258 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2259 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2260 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2261 | int tmout; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2262 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2263 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2264 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2265 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2266 | tmout = MAY_LJMP(luaL_checkinteger(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2267 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2268 | socket->s->req.rto = tmout; |
| 2269 | socket->s->req.wto = tmout; |
| 2270 | socket->s->res.rto = tmout; |
| 2271 | socket->s->res.wto = tmout; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2272 | |
| 2273 | return 0; |
| 2274 | } |
| 2275 | |
| 2276 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2277 | { |
| 2278 | struct hlua_socket *socket; |
| 2279 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2280 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2281 | struct stream *strm; |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2282 | struct task *task; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2283 | |
| 2284 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2285 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2286 | hlua_pusherror(L, "socket: full stack"); |
| 2287 | goto out_fail_conf; |
| 2288 | } |
| 2289 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2290 | /* Create the object: obj[0] = userdata. */ |
| 2291 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2292 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2293 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2294 | memset(socket, 0, sizeof(*socket)); |
| 2295 | |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2296 | /* Check if the various memory pools are intialized. */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2297 | if (!pool2_stream || !pool2_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2298 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2299 | goto out_fail_conf; |
| 2300 | } |
| 2301 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2302 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2303 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2304 | lua_setmetatable(L, -2); |
| 2305 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2306 | /* Create the applet context */ |
| 2307 | appctx = appctx_new(&update_applet); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2308 | if (!appctx) { |
| 2309 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2310 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2311 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2312 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2313 | appctx->ctx.hlua_cosocket.socket = socket; |
| 2314 | appctx->ctx.hlua_cosocket.connected = 0; |
| 2315 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 2316 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2317 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2318 | /* Now create a session, task and stream for this applet */ |
| 2319 | sess = session_new(&socket_proxy, NULL, &appctx->obj_type); |
| 2320 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2321 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2322 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2323 | } |
| 2324 | |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2325 | task = task_new(); |
| 2326 | if (!task) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2327 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2328 | goto out_fail_task; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2329 | } |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2330 | task->nice = 0; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2331 | |
Willy Tarreau | 73b65ac | 2015-04-08 18:26:29 +0200 | [diff] [blame] | 2332 | strm = stream_new(sess, task, &appctx->obj_type); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2333 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2334 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2335 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2336 | } |
| 2337 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2338 | /* Configure an empty Lua for the stream. */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2339 | socket->s = strm; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 2340 | strm->hlua = NULL; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2341 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2342 | /* Configure "right" stream interface. this "si" is used to connect |
| 2343 | * and retrieve data from the server. The connection is initialized |
| 2344 | * with the "struct server". |
| 2345 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2346 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2347 | |
| 2348 | /* Force destination server. */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2349 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET | SF_BE_ASSIGNED; |
| 2350 | strm->target = &socket_tcp.obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2351 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2352 | /* Update statistics counters. */ |
| 2353 | socket_proxy.feconn++; /* beconn will be increased later */ |
| 2354 | jobs++; |
| 2355 | totalconn++; |
| 2356 | |
| 2357 | /* Return yield waiting for connection. */ |
| 2358 | return 1; |
| 2359 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2360 | out_fail_stream: |
| 2361 | task_free(task); |
| 2362 | out_fail_task: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2363 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2364 | out_fail_sess: |
| 2365 | appctx_free(appctx); |
| 2366 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2367 | WILL_LJMP(lua_error(L)); |
| 2368 | return 0; |
| 2369 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2370 | |
| 2371 | /* |
| 2372 | * |
| 2373 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2374 | * Class Channel |
| 2375 | * |
| 2376 | * |
| 2377 | */ |
| 2378 | |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2379 | /* The state between the channel data and the HTTP parser state can be |
| 2380 | * unconsistent, so reset the parser and call it again. Warning, this |
| 2381 | * action not revalidate the request and not send a 400 if the modified |
| 2382 | * resuest is not valid. |
| 2383 | * |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2384 | * This function never fails. The direction is set using dir, which equals |
| 2385 | * either SMP_OPT_DIR_REQ or SMP_OPT_DIR_RES. |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2386 | */ |
| 2387 | static void hlua_resynchonize_proto(struct stream *stream, int dir) |
| 2388 | { |
| 2389 | /* Protocol HTTP. */ |
| 2390 | if (stream->be->mode == PR_MODE_HTTP) { |
| 2391 | |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2392 | if (dir == SMP_OPT_DIR_REQ) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2393 | http_txn_reset_req(stream->txn); |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2394 | else if (dir == SMP_OPT_DIR_RES) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2395 | http_txn_reset_res(stream->txn); |
| 2396 | |
| 2397 | if (stream->txn->hdr_idx.v) |
| 2398 | hdr_idx_init(&stream->txn->hdr_idx); |
| 2399 | |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2400 | if (dir == SMP_OPT_DIR_REQ) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2401 | http_msg_analyzer(&stream->txn->req, &stream->txn->hdr_idx); |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2402 | else if (dir == SMP_OPT_DIR_RES) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2403 | http_msg_analyzer(&stream->txn->rsp, &stream->txn->hdr_idx); |
| 2404 | } |
| 2405 | } |
| 2406 | |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2407 | /* This function is called before the Lua execution. It stores |
| 2408 | * the differents parsers state before executing some Lua code. |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2409 | */ |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2410 | static inline void consistency_set(struct stream *stream, int opt, struct hlua_consistency *c) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2411 | { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2412 | c->mode = stream->be->mode; |
| 2413 | switch (c->mode) { |
| 2414 | case PR_MODE_HTTP: |
| 2415 | c->data.http.dir = opt & SMP_OPT_DIR; |
| 2416 | if (c->data.http.dir == SMP_OPT_DIR_REQ) |
| 2417 | c->data.http.state = stream->txn->req.msg_state; |
| 2418 | else |
| 2419 | c->data.http.state = stream->txn->rsp.msg_state; |
| 2420 | break; |
| 2421 | default: |
| 2422 | break; |
| 2423 | } |
| 2424 | } |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2425 | |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2426 | /* This function is called after the Lua execution. it |
| 2427 | * returns true if the parser state is consistent, otherwise, |
| 2428 | * it return false. |
| 2429 | * |
| 2430 | * In HTTP mode, the parser state must be in the same state |
| 2431 | * or greater when we exit the function. Even if we do a |
| 2432 | * control yield. This prevent to break the HTTP message |
| 2433 | * from the Lua code. |
| 2434 | */ |
| 2435 | static inline int consistency_check(struct stream *stream, int opt, struct hlua_consistency *c) |
| 2436 | { |
| 2437 | if (c->mode != stream->be->mode) |
| 2438 | return 0; |
| 2439 | |
| 2440 | switch (c->mode) { |
| 2441 | case PR_MODE_HTTP: |
| 2442 | if (c->data.http.dir != (opt & SMP_OPT_DIR)) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2443 | return 0; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2444 | if (c->data.http.dir == SMP_OPT_DIR_REQ) |
| 2445 | return stream->txn->req.msg_state >= c->data.http.state; |
| 2446 | else |
| 2447 | return stream->txn->rsp.msg_state >= c->data.http.state; |
| 2448 | default: |
| 2449 | return 1; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2450 | } |
| 2451 | return 1; |
| 2452 | } |
| 2453 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2454 | /* Returns the struct hlua_channel join to the class channel in the |
| 2455 | * stack entry "ud" or throws an argument error. |
| 2456 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2457 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2458 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2459 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2460 | } |
| 2461 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2462 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 2463 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2464 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 2465 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2466 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2467 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2468 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2469 | return 0; |
| 2470 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2471 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2472 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2473 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2474 | |
| 2475 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2476 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 2477 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2478 | return 1; |
| 2479 | } |
| 2480 | |
| 2481 | /* Duplicate all the data present in the input channel and put it |
| 2482 | * in a string LUA variables. Returns -1 and push a nil value in |
| 2483 | * the stack if the channel is closed and all the data are consumed, |
| 2484 | * returns 0 if no data are available, otherwise it returns the length |
| 2485 | * of the builded string. |
| 2486 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2487 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2488 | { |
| 2489 | char *blk1; |
| 2490 | char *blk2; |
| 2491 | int len1; |
| 2492 | int len2; |
| 2493 | int ret; |
| 2494 | luaL_Buffer b; |
| 2495 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2496 | ret = bi_getblk_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2497 | if (unlikely(ret == 0)) |
| 2498 | return 0; |
| 2499 | |
| 2500 | if (unlikely(ret < 0)) { |
| 2501 | lua_pushnil(L); |
| 2502 | return -1; |
| 2503 | } |
| 2504 | |
| 2505 | luaL_buffinit(L, &b); |
| 2506 | luaL_addlstring(&b, blk1, len1); |
| 2507 | if (unlikely(ret == 2)) |
| 2508 | luaL_addlstring(&b, blk2, len2); |
| 2509 | luaL_pushresult(&b); |
| 2510 | |
| 2511 | if (unlikely(ret == 2)) |
| 2512 | return len1 + len2; |
| 2513 | return len1; |
| 2514 | } |
| 2515 | |
| 2516 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2517 | * a yield. This function keep the data in the buffer. |
| 2518 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2519 | __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] | 2520 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2521 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2522 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2523 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2524 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2525 | if (_hlua_channel_dup(chn, L) == 0) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2526 | 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] | 2527 | return 1; |
| 2528 | } |
| 2529 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2530 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 2531 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 2532 | { |
| 2533 | MAY_LJMP(check_args(L, 1, "dup")); |
| 2534 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2535 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 2536 | } |
| 2537 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2538 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2539 | * a yield. This function consumes the data in the buffer. It returns |
| 2540 | * a string containing the data or a nil pointer if no data are available |
| 2541 | * and the channel is closed. |
| 2542 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2543 | __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] | 2544 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2545 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2546 | int ret; |
| 2547 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2548 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2549 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2550 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2551 | if (unlikely(ret == 0)) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2552 | 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] | 2553 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2554 | if (unlikely(ret == -1)) |
| 2555 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2556 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2557 | chn->buf->i -= ret; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2558 | hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2559 | return 1; |
| 2560 | } |
| 2561 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2562 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 2563 | __LJMP static int hlua_channel_get(lua_State *L) |
| 2564 | { |
| 2565 | MAY_LJMP(check_args(L, 1, "get")); |
| 2566 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2567 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 2568 | } |
| 2569 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2570 | /* This functions consumes and returns one line. If the channel is closed, |
| 2571 | * and the last data does not contains a final '\n', the data are returned |
| 2572 | * without the final '\n'. When no more data are avalaible, it returns nil |
| 2573 | * value. |
| 2574 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2575 | __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] | 2576 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2577 | char *blk1; |
| 2578 | char *blk2; |
| 2579 | int len1; |
| 2580 | int len2; |
| 2581 | int len; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2582 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2583 | int ret; |
| 2584 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2585 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2586 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2587 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2588 | ret = bi_getline_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2589 | if (ret == 0) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2590 | 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] | 2591 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2592 | if (ret == -1) { |
| 2593 | lua_pushnil(L); |
| 2594 | return 1; |
| 2595 | } |
| 2596 | |
| 2597 | luaL_buffinit(L, &b); |
| 2598 | luaL_addlstring(&b, blk1, len1); |
| 2599 | len = len1; |
| 2600 | if (unlikely(ret == 2)) { |
| 2601 | luaL_addlstring(&b, blk2, len2); |
| 2602 | len += len2; |
| 2603 | } |
| 2604 | luaL_pushresult(&b); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2605 | 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] | 2606 | hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2607 | return 1; |
| 2608 | } |
| 2609 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2610 | /* Check arguments for the fucntion "hlua_channel_getline_yield". */ |
| 2611 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 2612 | { |
| 2613 | MAY_LJMP(check_args(L, 1, "getline")); |
| 2614 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2615 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 2616 | } |
| 2617 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2618 | /* This function takes a string as input, and append it at the |
| 2619 | * input side of channel. If the data is too big, but a space |
| 2620 | * is probably available after sending some data, the function |
| 2621 | * yield. If the data is bigger than the buffer, or if the |
| 2622 | * channel is closed, it returns -1. otherwise, it returns the |
| 2623 | * amount of data writed. |
| 2624 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2625 | __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] | 2626 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2627 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2628 | size_t len; |
| 2629 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2630 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2631 | int ret; |
| 2632 | int max; |
| 2633 | |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2634 | /* Check if the buffer is avalaible because HAProxy doesn't allocate |
| 2635 | * the request buffer if its not required. |
| 2636 | */ |
| 2637 | if (chn->buf->size == 0) { |
| 2638 | si_applet_cant_put(chn_prod(chn)); |
| 2639 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0)); |
| 2640 | } |
| 2641 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2642 | max = channel_recv_limit(chn) - buffer_len(chn->buf); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2643 | if (max > len - l) |
| 2644 | max = len - l; |
| 2645 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2646 | ret = bi_putblk(chn, str + l, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2647 | if (ret == -2 || ret == -3) { |
| 2648 | lua_pushinteger(L, -1); |
| 2649 | return 1; |
| 2650 | } |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2651 | if (ret == -1) { |
| 2652 | chn->flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2653 | 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] | 2654 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2655 | l += ret; |
| 2656 | lua_pop(L, 1); |
| 2657 | lua_pushinteger(L, l); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2658 | hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2659 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2660 | max = channel_recv_limit(chn) - buffer_len(chn->buf); |
| 2661 | if (max == 0 && chn->buf->o == 0) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2662 | /* There are no space avalaible, and the output buffer is empty. |
| 2663 | * in this case, we cannot add more data, so we cannot yield, |
| 2664 | * we return the amount of copyied data. |
| 2665 | */ |
| 2666 | return 1; |
| 2667 | } |
| 2668 | if (l < len) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2669 | 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] | 2670 | return 1; |
| 2671 | } |
| 2672 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2673 | /* just a wrapper of "hlua_channel_append_yield". It returns the length |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2674 | * of the writed string, or -1 if the channel is closed or if the |
| 2675 | * buffer size is too little for the data. |
| 2676 | */ |
| 2677 | __LJMP static int hlua_channel_append(lua_State *L) |
| 2678 | { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2679 | size_t len; |
| 2680 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2681 | MAY_LJMP(check_args(L, 2, "append")); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2682 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2683 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2684 | MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2685 | lua_pushinteger(L, 0); |
| 2686 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2687 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2688 | } |
| 2689 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2690 | /* just a wrapper of "hlua_channel_append_yield". This wrapper starts |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2691 | * his process by cleaning the buffer. The result is a replacement |
| 2692 | * of the current data. It returns the length of the writed string, |
| 2693 | * or -1 if the channel is closed or if the buffer size is too |
| 2694 | * little for the data. |
| 2695 | */ |
| 2696 | __LJMP static int hlua_channel_set(lua_State *L) |
| 2697 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2698 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2699 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2700 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2701 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2702 | lua_pushinteger(L, 0); |
| 2703 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2704 | chn->buf->i = 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2705 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2706 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2707 | } |
| 2708 | |
| 2709 | /* Append data in the output side of the buffer. This data is immediatly |
| 2710 | * sent. The fcuntion returns the ammount of data writed. If the buffer |
| 2711 | * cannot contains the data, the function yield. The function returns -1 |
| 2712 | * if the channel is closed. |
| 2713 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2714 | __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] | 2715 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2716 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2717 | size_t len; |
| 2718 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2719 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2720 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2721 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2722 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2723 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2724 | lua_pushinteger(L, -1); |
| 2725 | return 1; |
| 2726 | } |
| 2727 | |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 2728 | /* Check if the buffer is avalaible because HAProxy doesn't allocate |
| 2729 | * the request buffer if its not required. |
| 2730 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2731 | if (chn->buf->size == 0) { |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2732 | si_applet_cant_put(chn_prod(chn)); |
| 2733 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 2734 | } |
| 2735 | |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2736 | /* the writed data will be immediatly sent, so we can check |
| 2737 | * the avalaible space without taking in account the reserve. |
| 2738 | * The reserve is guaranted for the processing of incoming |
| 2739 | * data, because the buffer will be flushed. |
| 2740 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2741 | max = chn->buf->size - buffer_len(chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2742 | |
| 2743 | /* If there are no space avalaible, and the output buffer is empty. |
| 2744 | * in this case, we cannot add more data, so we cannot yield, |
| 2745 | * we return the amount of copyied data. |
| 2746 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2747 | if (max == 0 && chn->buf->o == 0) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2748 | return 1; |
| 2749 | |
| 2750 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2751 | if (max > len - l) |
| 2752 | max = len - l; |
| 2753 | |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2754 | /* The buffer avalaible size may be not contiguous. This test |
| 2755 | * detects a non contiguous buffer and realign it. |
| 2756 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2757 | if (bi_space_for_replace(chn->buf) < max) |
| 2758 | buffer_slow_realign(chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2759 | |
| 2760 | /* Copy input data in the buffer. */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2761 | 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] | 2762 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2763 | /* buffer replace considers that the input part is filled. |
| 2764 | * so, I must forward these new data in the output part. |
| 2765 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2766 | b_adv(chn->buf, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2767 | |
| 2768 | l += max; |
| 2769 | lua_pop(L, 1); |
| 2770 | lua_pushinteger(L, l); |
| 2771 | |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2772 | /* If there are no space avalaible, and the output buffer is empty. |
| 2773 | * in this case, we cannot add more data, so we cannot yield, |
| 2774 | * we return the amount of copyied data. |
| 2775 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2776 | max = chn->buf->size - buffer_len(chn->buf); |
| 2777 | if (max == 0 && chn->buf->o == 0) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2778 | return 1; |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2779 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2780 | if (l < len) { |
| 2781 | /* If we are waiting for space in the response buffer, we |
| 2782 | * must set the flag WAKERESWR. This flag required the task |
| 2783 | * wake up if any activity is detected on the response buffer. |
| 2784 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2785 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2786 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 2787 | else |
| 2788 | HLUA_SET_WAKEREQWR(hlua); |
| 2789 | 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] | 2790 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2791 | |
| 2792 | return 1; |
| 2793 | } |
| 2794 | |
| 2795 | /* Just a wraper of "_hlua_channel_send". This wrapper permits |
| 2796 | * yield the LUA process, and resume it without checking the |
| 2797 | * input arguments. |
| 2798 | */ |
| 2799 | __LJMP static int hlua_channel_send(lua_State *L) |
| 2800 | { |
| 2801 | MAY_LJMP(check_args(L, 2, "send")); |
| 2802 | lua_pushinteger(L, 0); |
| 2803 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2804 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2805 | } |
| 2806 | |
| 2807 | /* This function forward and amount of butes. The data pass from |
| 2808 | * the input side of the buffer to the output side, and can be |
| 2809 | * forwarded. This function never fails. |
| 2810 | * |
| 2811 | * The Lua function takes an amount of bytes to be forwarded in |
| 2812 | * imput. It returns the number of bytes forwarded. |
| 2813 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2814 | __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] | 2815 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2816 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2817 | int len; |
| 2818 | int l; |
| 2819 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2820 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2821 | |
| 2822 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2823 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 2824 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 2825 | |
| 2826 | max = len - l; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2827 | if (max > chn->buf->i) |
| 2828 | max = chn->buf->i; |
| 2829 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2830 | l += max; |
| 2831 | |
| 2832 | lua_pop(L, 1); |
| 2833 | lua_pushinteger(L, l); |
| 2834 | |
| 2835 | /* Check if it miss bytes to forward. */ |
| 2836 | if (l < len) { |
| 2837 | /* The the input channel or the output channel are closed, we |
| 2838 | * must return the amount of data forwarded. |
| 2839 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2840 | if (channel_input_closed(chn) || channel_output_closed(chn)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2841 | return 1; |
| 2842 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2843 | /* If we are waiting for space data in the response buffer, we |
| 2844 | * must set the flag WAKERESWR. This flag required the task |
| 2845 | * wake up if any activity is detected on the response buffer. |
| 2846 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2847 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2848 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 2849 | else |
| 2850 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2851 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2852 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 2853 | 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] | 2854 | } |
| 2855 | |
| 2856 | return 1; |
| 2857 | } |
| 2858 | |
| 2859 | /* Just check the input and prepare the stack for the previous |
| 2860 | * function "hlua_channel_forward_yield" |
| 2861 | */ |
| 2862 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 2863 | { |
| 2864 | MAY_LJMP(check_args(L, 2, "forward")); |
| 2865 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2866 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 2867 | |
| 2868 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2869 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2870 | } |
| 2871 | |
| 2872 | /* Just returns the number of bytes available in the input |
| 2873 | * side of the buffer. This function never fails. |
| 2874 | */ |
| 2875 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 2876 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2877 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2878 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2879 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2880 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2881 | lua_pushinteger(L, chn->buf->i); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2882 | return 1; |
| 2883 | } |
| 2884 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 2885 | /* Returns true if the channel is full. */ |
| 2886 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 2887 | { |
| 2888 | struct channel *chn; |
| 2889 | int rem; |
| 2890 | |
| 2891 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 2892 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2893 | |
| 2894 | rem = chn->buf->size; |
| 2895 | rem -= chn->buf->o; /* Output size */ |
| 2896 | rem -= chn->buf->i; /* Input size */ |
| 2897 | rem -= global.tune.maxrewrite; /* Rewrite reserved size */ |
| 2898 | |
| 2899 | lua_pushboolean(L, rem <= 0); |
| 2900 | return 1; |
| 2901 | } |
| 2902 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2903 | /* Just returns the number of bytes available in the output |
| 2904 | * side of the buffer. This function never fails. |
| 2905 | */ |
| 2906 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 2907 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2908 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2909 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2910 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2911 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2912 | lua_pushinteger(L, chn->buf->o); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2913 | return 1; |
| 2914 | } |
| 2915 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2916 | /* |
| 2917 | * |
| 2918 | * |
| 2919 | * Class Fetches |
| 2920 | * |
| 2921 | * |
| 2922 | */ |
| 2923 | |
| 2924 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2925 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2926 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 2927 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2928 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2929 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2930 | } |
| 2931 | |
| 2932 | /* This function creates and push in the stack a fetch object according |
| 2933 | * with a current TXN. |
| 2934 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 2935 | 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] | 2936 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 2937 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2938 | |
| 2939 | /* Check stack size. */ |
| 2940 | if (!lua_checkstack(L, 3)) |
| 2941 | return 0; |
| 2942 | |
| 2943 | /* Create the object: obj[0] = userdata. |
| 2944 | * Note that the base of the Fetches object is the |
| 2945 | * transaction object. |
| 2946 | */ |
| 2947 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 2948 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2949 | lua_rawseti(L, -2, 0); |
| 2950 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 2951 | hsmp->s = txn->s; |
| 2952 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 2953 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 2954 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2955 | |
| 2956 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2957 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 2958 | lua_setmetatable(L, -2); |
| 2959 | |
| 2960 | return 1; |
| 2961 | } |
| 2962 | |
| 2963 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 2964 | * It uses closure argument to store the associated sample-fetch. It |
| 2965 | * returns only one argument or throws an error. An error is thrown |
| 2966 | * only if an error is encountered during the argument parsing. If |
| 2967 | * the "sample-fetch" function fails, nil is returned. |
| 2968 | */ |
| 2969 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 2970 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 2971 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 2972 | struct sample_fetch *f; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2973 | struct arg args[ARGM_NBARGS + 1]; |
| 2974 | int i; |
| 2975 | struct sample smp; |
| 2976 | |
| 2977 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2978 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2979 | |
| 2980 | /* Get traditionnal arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 2981 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2982 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 2983 | /* Check execution authorization. */ |
| 2984 | if (f->use & SMP_USE_HTTP_ANY && |
| 2985 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 2986 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 2987 | "is not available in Lua services", f->kw); |
| 2988 | WILL_LJMP(lua_error(L)); |
| 2989 | } |
| 2990 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2991 | /* Get extra arguments. */ |
| 2992 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 2993 | if (i >= ARGM_NBARGS) |
| 2994 | break; |
| 2995 | hlua_lua2arg(L, i + 2, &args[i]); |
| 2996 | } |
| 2997 | args[i].type = ARGT_STOP; |
David Carlier | abdb00f | 2016-04-27 16:14:50 +0100 | [diff] [blame] | 2998 | args[i].data.str.str = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2999 | |
| 3000 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3001 | 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] | 3002 | |
| 3003 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3004 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3005 | lua_pushfstring(L, "error in arguments"); |
| 3006 | WILL_LJMP(lua_error(L)); |
| 3007 | } |
| 3008 | |
| 3009 | /* Initialise the sample. */ |
| 3010 | memset(&smp, 0, sizeof(smp)); |
| 3011 | |
| 3012 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3013 | 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] | 3014 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3015 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3016 | lua_pushstring(L, ""); |
| 3017 | else |
| 3018 | lua_pushnil(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3019 | return 1; |
| 3020 | } |
| 3021 | |
| 3022 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3023 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3024 | hlua_smp2lua_str(L, &smp); |
| 3025 | else |
| 3026 | hlua_smp2lua(L, &smp); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3027 | return 1; |
| 3028 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3029 | |
| 3030 | /* |
| 3031 | * |
| 3032 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3033 | * Class Converters |
| 3034 | * |
| 3035 | * |
| 3036 | */ |
| 3037 | |
| 3038 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3039 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3040 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3041 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3042 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3043 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3044 | } |
| 3045 | |
| 3046 | /* This function creates and push in the stack a Converters object |
| 3047 | * according with a current TXN. |
| 3048 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3049 | 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] | 3050 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3051 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3052 | |
| 3053 | /* Check stack size. */ |
| 3054 | if (!lua_checkstack(L, 3)) |
| 3055 | return 0; |
| 3056 | |
| 3057 | /* Create the object: obj[0] = userdata. |
| 3058 | * Note that the base of the Converters object is the |
| 3059 | * same than the TXN object. |
| 3060 | */ |
| 3061 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3062 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3063 | lua_rawseti(L, -2, 0); |
| 3064 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3065 | hsmp->s = txn->s; |
| 3066 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3067 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3068 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3069 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3070 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3071 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 3072 | lua_setmetatable(L, -2); |
| 3073 | |
| 3074 | return 1; |
| 3075 | } |
| 3076 | |
| 3077 | /* This function is an LUA binding. It is called with each converter. |
| 3078 | * It uses closure argument to store the associated converter. It |
| 3079 | * returns only one argument or throws an error. An error is thrown |
| 3080 | * only if an error is encountered during the argument parsing. If |
| 3081 | * the converter function function fails, nil is returned. |
| 3082 | */ |
| 3083 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 3084 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3085 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3086 | struct sample_conv *conv; |
| 3087 | struct arg args[ARGM_NBARGS + 1]; |
| 3088 | int i; |
| 3089 | struct sample smp; |
| 3090 | |
| 3091 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3092 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3093 | |
| 3094 | /* Get traditionnal arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3095 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3096 | |
| 3097 | /* Get extra arguments. */ |
| 3098 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 3099 | if (i >= ARGM_NBARGS) |
| 3100 | break; |
| 3101 | hlua_lua2arg(L, i + 3, &args[i]); |
| 3102 | } |
| 3103 | args[i].type = ARGT_STOP; |
David Carlier | abdb00f | 2016-04-27 16:14:50 +0100 | [diff] [blame] | 3104 | args[i].data.str.str = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3105 | |
| 3106 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3107 | 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] | 3108 | |
| 3109 | /* Run the special args checker. */ |
| 3110 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 3111 | hlua_pusherror(L, "error in arguments"); |
| 3112 | WILL_LJMP(lua_error(L)); |
| 3113 | } |
| 3114 | |
| 3115 | /* Initialise the sample. */ |
| 3116 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 3117 | hlua_pusherror(L, "error in the input argument"); |
| 3118 | WILL_LJMP(lua_error(L)); |
| 3119 | } |
| 3120 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3121 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 3122 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3123 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3124 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3125 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3126 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3127 | WILL_LJMP(lua_error(L)); |
| 3128 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3129 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 3130 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3131 | hlua_pusherror(L, "error during the input argument casting"); |
| 3132 | WILL_LJMP(lua_error(L)); |
| 3133 | } |
| 3134 | |
| 3135 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3136 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3137 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3138 | lua_pushstring(L, ""); |
| 3139 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3140 | lua_pushnil(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3141 | return 1; |
| 3142 | } |
| 3143 | |
| 3144 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3145 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3146 | hlua_smp2lua_str(L, &smp); |
| 3147 | else |
| 3148 | hlua_smp2lua(L, &smp); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3149 | return 1; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3150 | } |
| 3151 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3152 | /* |
| 3153 | * |
| 3154 | * |
| 3155 | * Class AppletTCP |
| 3156 | * |
| 3157 | * |
| 3158 | */ |
| 3159 | |
| 3160 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 3161 | * a class stream, otherwise it throws an error. |
| 3162 | */ |
| 3163 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 3164 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3165 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3166 | } |
| 3167 | |
| 3168 | /* This function creates and push in the stack an Applet object |
| 3169 | * according with a current TXN. |
| 3170 | */ |
| 3171 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 3172 | { |
| 3173 | struct hlua_appctx *appctx; |
| 3174 | struct stream_interface *si = ctx->owner; |
| 3175 | struct stream *s = si_strm(si); |
| 3176 | struct proxy *p = s->be; |
| 3177 | |
| 3178 | /* Check stack size. */ |
| 3179 | if (!lua_checkstack(L, 3)) |
| 3180 | return 0; |
| 3181 | |
| 3182 | /* Create the object: obj[0] = userdata. |
| 3183 | * Note that the base of the Converters object is the |
| 3184 | * same than the TXN object. |
| 3185 | */ |
| 3186 | lua_newtable(L); |
| 3187 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
| 3188 | lua_rawseti(L, -2, 0); |
| 3189 | appctx->appctx = ctx; |
| 3190 | appctx->htxn.s = s; |
| 3191 | appctx->htxn.p = p; |
| 3192 | |
| 3193 | /* Create the "f" field that contains a list of fetches. */ |
| 3194 | lua_pushstring(L, "f"); |
| 3195 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3196 | return 0; |
| 3197 | lua_settable(L, -3); |
| 3198 | |
| 3199 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3200 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3201 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3202 | return 0; |
| 3203 | lua_settable(L, -3); |
| 3204 | |
| 3205 | /* Create the "c" field that contains a list of converters. */ |
| 3206 | lua_pushstring(L, "c"); |
| 3207 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3208 | return 0; |
| 3209 | lua_settable(L, -3); |
| 3210 | |
| 3211 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3212 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3213 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3214 | return 0; |
| 3215 | lua_settable(L, -3); |
| 3216 | |
| 3217 | /* Pop a class stream metatable and affect it to the table. */ |
| 3218 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 3219 | lua_setmetatable(L, -2); |
| 3220 | |
| 3221 | return 1; |
| 3222 | } |
| 3223 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3224 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 3225 | { |
| 3226 | struct hlua_appctx *appctx; |
| 3227 | struct stream *s; |
| 3228 | const char *name; |
| 3229 | size_t len; |
| 3230 | struct sample smp; |
| 3231 | |
| 3232 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 3233 | |
| 3234 | /* It is useles to retrieve the stream, but this function |
| 3235 | * runs only in a stream context. |
| 3236 | */ |
| 3237 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3238 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3239 | s = appctx->htxn.s; |
| 3240 | |
| 3241 | /* Converts the third argument in a sample. */ |
| 3242 | hlua_lua2smp(L, 3, &smp); |
| 3243 | |
| 3244 | /* Store the sample in a variable. */ |
| 3245 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3246 | vars_set_by_name(name, len, &smp); |
| 3247 | return 0; |
| 3248 | } |
| 3249 | |
| 3250 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 3251 | { |
| 3252 | struct hlua_appctx *appctx; |
| 3253 | struct stream *s; |
| 3254 | const char *name; |
| 3255 | size_t len; |
| 3256 | struct sample smp; |
| 3257 | |
| 3258 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3259 | |
| 3260 | /* It is useles to retrieve the stream, but this function |
| 3261 | * runs only in a stream context. |
| 3262 | */ |
| 3263 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3264 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3265 | s = appctx->htxn.s; |
| 3266 | |
| 3267 | /* Unset the variable. */ |
| 3268 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3269 | vars_unset_by_name(name, len, &smp); |
| 3270 | return 0; |
| 3271 | } |
| 3272 | |
| 3273 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 3274 | { |
| 3275 | struct hlua_appctx *appctx; |
| 3276 | struct stream *s; |
| 3277 | const char *name; |
| 3278 | size_t len; |
| 3279 | struct sample smp; |
| 3280 | |
| 3281 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3282 | |
| 3283 | /* It is useles to retrieve the stream, but this function |
| 3284 | * runs only in a stream context. |
| 3285 | */ |
| 3286 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3287 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3288 | s = appctx->htxn.s; |
| 3289 | |
| 3290 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3291 | if (!vars_get_by_name(name, len, &smp)) { |
| 3292 | lua_pushnil(L); |
| 3293 | return 1; |
| 3294 | } |
| 3295 | |
| 3296 | return hlua_smp2lua(L, &smp); |
| 3297 | } |
| 3298 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3299 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 3300 | { |
| 3301 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3302 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3303 | struct hlua *hlua; |
| 3304 | |
| 3305 | /* Note that this hlua struct is from the session and not from the applet. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 3306 | if (!s->hlua) |
| 3307 | return 0; |
| 3308 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3309 | |
| 3310 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3311 | |
| 3312 | /* Remove previous value. */ |
| 3313 | if (hlua->Mref != -1) |
| 3314 | luaL_unref(L, hlua->Mref, LUA_REGISTRYINDEX); |
| 3315 | |
| 3316 | /* Get and store new value. */ |
| 3317 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3318 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3319 | |
| 3320 | return 0; |
| 3321 | } |
| 3322 | |
| 3323 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 3324 | { |
| 3325 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3326 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3327 | struct hlua *hlua; |
| 3328 | |
| 3329 | /* Note that this hlua struct is from the session and not from the applet. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 3330 | if (!s->hlua) { |
| 3331 | lua_pushnil(L); |
| 3332 | return 1; |
| 3333 | } |
| 3334 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3335 | |
| 3336 | /* Push configuration index in the stack. */ |
| 3337 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3338 | |
| 3339 | return 1; |
| 3340 | } |
| 3341 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3342 | /* If expected data not yet available, it returns a yield. This function |
| 3343 | * consumes the data in the buffer. It returns a string containing the |
| 3344 | * data. This string can be empty. |
| 3345 | */ |
| 3346 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 3347 | { |
| 3348 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3349 | struct stream_interface *si = appctx->appctx->owner; |
| 3350 | int ret; |
| 3351 | char *blk1; |
| 3352 | int len1; |
| 3353 | char *blk2; |
| 3354 | int len2; |
| 3355 | |
| 3356 | /* Read the maximum amount of data avalaible. */ |
| 3357 | ret = bo_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
| 3358 | |
| 3359 | /* Data not yet avalaible. return yield. */ |
| 3360 | if (ret == 0) { |
| 3361 | si_applet_cant_get(si); |
| 3362 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0)); |
| 3363 | } |
| 3364 | |
| 3365 | /* End of data: commit the total strings and return. */ |
| 3366 | if (ret < 0) { |
| 3367 | luaL_pushresult(&appctx->b); |
| 3368 | return 1; |
| 3369 | } |
| 3370 | |
| 3371 | /* Ensure that the block 2 length is usable. */ |
| 3372 | if (ret == 1) |
| 3373 | len2 = 0; |
| 3374 | |
| 3375 | /* dont check the max length read and dont check. */ |
| 3376 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3377 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3378 | |
| 3379 | /* Consume input channel output buffer data. */ |
| 3380 | bo_skip(si_oc(si), len1 + len2); |
| 3381 | luaL_pushresult(&appctx->b); |
| 3382 | return 1; |
| 3383 | } |
| 3384 | |
| 3385 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 3386 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 3387 | { |
| 3388 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3389 | |
| 3390 | /* Initialise the string catenation. */ |
| 3391 | luaL_buffinit(L, &appctx->b); |
| 3392 | |
| 3393 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 3394 | } |
| 3395 | |
| 3396 | /* If expected data not yet available, it returns a yield. This function |
| 3397 | * consumes the data in the buffer. It returns a string containing the |
| 3398 | * data. This string can be empty. |
| 3399 | */ |
| 3400 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 3401 | { |
| 3402 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3403 | struct stream_interface *si = appctx->appctx->owner; |
| 3404 | int len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3405 | int ret; |
| 3406 | char *blk1; |
| 3407 | int len1; |
| 3408 | char *blk2; |
| 3409 | int len2; |
| 3410 | |
| 3411 | /* Read the maximum amount of data avalaible. */ |
| 3412 | ret = bo_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
| 3413 | |
| 3414 | /* Data not yet avalaible. return yield. */ |
| 3415 | if (ret == 0) { |
| 3416 | si_applet_cant_get(si); |
| 3417 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0)); |
| 3418 | } |
| 3419 | |
| 3420 | /* End of data: commit the total strings and return. */ |
| 3421 | if (ret < 0) { |
| 3422 | luaL_pushresult(&appctx->b); |
| 3423 | return 1; |
| 3424 | } |
| 3425 | |
| 3426 | /* Ensure that the block 2 length is usable. */ |
| 3427 | if (ret == 1) |
| 3428 | len2 = 0; |
| 3429 | |
| 3430 | if (len == -1) { |
| 3431 | |
| 3432 | /* If len == -1, catenate all the data avalaile and |
| 3433 | * yield because we want to get all the data until |
| 3434 | * the end of data stream. |
| 3435 | */ |
| 3436 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3437 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3438 | bo_skip(si_oc(si), len1 + len2); |
| 3439 | si_applet_cant_get(si); |
| 3440 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0)); |
| 3441 | |
| 3442 | } else { |
| 3443 | |
| 3444 | /* Copy the fisrt block caping to the length required. */ |
| 3445 | if (len1 > len) |
| 3446 | len1 = len; |
| 3447 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3448 | len -= len1; |
| 3449 | |
| 3450 | /* Copy the second block. */ |
| 3451 | if (len2 > len) |
| 3452 | len2 = len; |
| 3453 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3454 | len -= len2; |
| 3455 | |
| 3456 | /* Consume input channel output buffer data. */ |
| 3457 | bo_skip(si_oc(si), len1 + len2); |
| 3458 | |
| 3459 | /* If we are no other data avalaible, yield waiting for new data. */ |
| 3460 | if (len > 0) { |
| 3461 | lua_pushinteger(L, len); |
| 3462 | lua_replace(L, 2); |
| 3463 | si_applet_cant_get(si); |
| 3464 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0)); |
| 3465 | } |
| 3466 | |
| 3467 | /* return the result. */ |
| 3468 | luaL_pushresult(&appctx->b); |
| 3469 | return 1; |
| 3470 | } |
| 3471 | |
| 3472 | /* we never executes this */ |
| 3473 | hlua_pusherror(L, "Lua: internal error"); |
| 3474 | WILL_LJMP(lua_error(L)); |
| 3475 | return 0; |
| 3476 | } |
| 3477 | |
| 3478 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 3479 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 3480 | { |
| 3481 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3482 | int len = -1; |
| 3483 | |
| 3484 | if (lua_gettop(L) > 2) |
| 3485 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 3486 | if (lua_gettop(L) >= 2) { |
| 3487 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3488 | lua_pop(L, 1); |
| 3489 | } |
| 3490 | |
| 3491 | /* Confirm or set the required length */ |
| 3492 | lua_pushinteger(L, len); |
| 3493 | |
| 3494 | /* Initialise the string catenation. */ |
| 3495 | luaL_buffinit(L, &appctx->b); |
| 3496 | |
| 3497 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 3498 | } |
| 3499 | |
| 3500 | /* Append data in the output side of the buffer. This data is immediatly |
| 3501 | * sent. The fcuntion returns the ammount of data writed. If the buffer |
| 3502 | * cannot contains the data, the function yield. The function returns -1 |
| 3503 | * if the channel is closed. |
| 3504 | */ |
| 3505 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 3506 | { |
| 3507 | size_t len; |
| 3508 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3509 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3510 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3511 | struct stream_interface *si = appctx->appctx->owner; |
| 3512 | struct channel *chn = si_ic(si); |
| 3513 | int max; |
| 3514 | |
| 3515 | /* Get the max amount of data which can write as input in the channel. */ |
| 3516 | max = channel_recv_max(chn); |
| 3517 | if (max > (len - l)) |
| 3518 | max = len - l; |
| 3519 | |
| 3520 | /* Copy data. */ |
| 3521 | bi_putblk(chn, str + l, max); |
| 3522 | |
| 3523 | /* update counters. */ |
| 3524 | l += max; |
| 3525 | lua_pop(L, 1); |
| 3526 | lua_pushinteger(L, l); |
| 3527 | |
| 3528 | /* If some data is not send, declares the situation to the |
| 3529 | * applet, and returns a yield. |
| 3530 | */ |
| 3531 | if (l < len) { |
| 3532 | si_applet_cant_put(si); |
| 3533 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0)); |
| 3534 | } |
| 3535 | |
| 3536 | return 1; |
| 3537 | } |
| 3538 | |
| 3539 | /* Just a wraper of "hlua_applet_tcp_send_yield". This wrapper permits |
| 3540 | * yield the LUA process, and resume it without checking the |
| 3541 | * input arguments. |
| 3542 | */ |
| 3543 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 3544 | { |
| 3545 | MAY_LJMP(check_args(L, 2, "send")); |
| 3546 | lua_pushinteger(L, 0); |
| 3547 | |
| 3548 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 3549 | } |
| 3550 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3551 | /* |
| 3552 | * |
| 3553 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3554 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3555 | * |
| 3556 | * |
| 3557 | */ |
| 3558 | |
| 3559 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3560 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3561 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3562 | __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] | 3563 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3564 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3565 | } |
| 3566 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3567 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3568 | * according with a current TXN. |
| 3569 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3570 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3571 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3572 | struct hlua_appctx *appctx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3573 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3574 | struct stream_interface *si = ctx->owner; |
| 3575 | struct stream *s = si_strm(si); |
| 3576 | struct proxy *px = s->be; |
| 3577 | struct http_txn *txn = s->txn; |
| 3578 | const char *path; |
| 3579 | const char *end; |
| 3580 | const char *p; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3581 | |
| 3582 | /* Check stack size. */ |
| 3583 | if (!lua_checkstack(L, 3)) |
| 3584 | return 0; |
| 3585 | |
| 3586 | /* Create the object: obj[0] = userdata. |
| 3587 | * Note that the base of the Converters object is the |
| 3588 | * same than the TXN object. |
| 3589 | */ |
| 3590 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3591 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3592 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3593 | appctx->appctx = ctx; |
| 3594 | appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
| 3595 | appctx->htxn.s = s; |
| 3596 | appctx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3597 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3598 | /* Create the "f" field that contains a list of fetches. */ |
| 3599 | lua_pushstring(L, "f"); |
| 3600 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3601 | return 0; |
| 3602 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3603 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3604 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3605 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3606 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3607 | return 0; |
| 3608 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3609 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3610 | /* Create the "c" field that contains a list of converters. */ |
| 3611 | lua_pushstring(L, "c"); |
| 3612 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3613 | return 0; |
| 3614 | lua_settable(L, -3); |
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 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3617 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3618 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3619 | return 0; |
| 3620 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3621 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3622 | /* Stores the request method. */ |
| 3623 | lua_pushstring(L, "method"); |
| 3624 | lua_pushlstring(L, txn->req.chn->buf->p, txn->req.sl.rq.m_l); |
| 3625 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3626 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3627 | /* Stores the http version. */ |
| 3628 | lua_pushstring(L, "version"); |
| 3629 | lua_pushlstring(L, txn->req.chn->buf->p + txn->req.sl.rq.v, txn->req.sl.rq.v_l); |
| 3630 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3631 | |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3632 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 3633 | * the array on the top of the stack. |
| 3634 | */ |
| 3635 | lua_pushstring(L, "headers"); |
| 3636 | htxn.s = s; |
| 3637 | htxn.p = px; |
| 3638 | htxn.dir = SMP_OPT_DIR_REQ; |
| 3639 | if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req)) |
| 3640 | return 0; |
| 3641 | lua_settable(L, -3); |
| 3642 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3643 | /* Get path and qs */ |
| 3644 | path = http_get_path(txn); |
| 3645 | end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
| 3646 | p = path; |
| 3647 | while (p < end && *p != '?') |
| 3648 | p++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3649 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3650 | /* Stores the request path. */ |
| 3651 | lua_pushstring(L, "path"); |
| 3652 | lua_pushlstring(L, path, p - path); |
| 3653 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3654 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3655 | /* Stores the query string. */ |
| 3656 | lua_pushstring(L, "qs"); |
| 3657 | if (*p == '?') |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3658 | p++; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3659 | lua_pushlstring(L, p, end - p); |
| 3660 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3661 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3662 | /* Stores the request path. */ |
| 3663 | lua_pushstring(L, "length"); |
| 3664 | lua_pushinteger(L, txn->req.body_len); |
| 3665 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3666 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3667 | /* Create an array of HTTP request headers. */ |
| 3668 | lua_pushstring(L, "headers"); |
| 3669 | MAY_LJMP(hlua_http_get_headers(L, &appctx->htxn, &appctx->htxn.s->txn->req)); |
| 3670 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3671 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3672 | /* Create an empty array of HTTP request headers. */ |
| 3673 | lua_pushstring(L, "response"); |
| 3674 | lua_newtable(L); |
| 3675 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3676 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3677 | /* Pop a class stream metatable and affect it to the table. */ |
| 3678 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 3679 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3680 | |
| 3681 | return 1; |
| 3682 | } |
| 3683 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3684 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 3685 | { |
| 3686 | struct hlua_appctx *appctx; |
| 3687 | struct stream *s; |
| 3688 | const char *name; |
| 3689 | size_t len; |
| 3690 | struct sample smp; |
| 3691 | |
| 3692 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 3693 | |
| 3694 | /* It is useles to retrieve the stream, but this function |
| 3695 | * runs only in a stream context. |
| 3696 | */ |
| 3697 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3698 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3699 | s = appctx->htxn.s; |
| 3700 | |
| 3701 | /* Converts the third argument in a sample. */ |
| 3702 | hlua_lua2smp(L, 3, &smp); |
| 3703 | |
| 3704 | /* Store the sample in a variable. */ |
| 3705 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3706 | vars_set_by_name(name, len, &smp); |
| 3707 | return 0; |
| 3708 | } |
| 3709 | |
| 3710 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 3711 | { |
| 3712 | struct hlua_appctx *appctx; |
| 3713 | struct stream *s; |
| 3714 | const char *name; |
| 3715 | size_t len; |
| 3716 | struct sample smp; |
| 3717 | |
| 3718 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3719 | |
| 3720 | /* It is useles to retrieve the stream, but this function |
| 3721 | * runs only in a stream context. |
| 3722 | */ |
| 3723 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3724 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3725 | s = appctx->htxn.s; |
| 3726 | |
| 3727 | /* Unset the variable. */ |
| 3728 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3729 | vars_unset_by_name(name, len, &smp); |
| 3730 | return 0; |
| 3731 | } |
| 3732 | |
| 3733 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 3734 | { |
| 3735 | struct hlua_appctx *appctx; |
| 3736 | struct stream *s; |
| 3737 | const char *name; |
| 3738 | size_t len; |
| 3739 | struct sample smp; |
| 3740 | |
| 3741 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3742 | |
| 3743 | /* It is useles to retrieve the stream, but this function |
| 3744 | * runs only in a stream context. |
| 3745 | */ |
| 3746 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3747 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3748 | s = appctx->htxn.s; |
| 3749 | |
| 3750 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3751 | if (!vars_get_by_name(name, len, &smp)) { |
| 3752 | lua_pushnil(L); |
| 3753 | return 1; |
| 3754 | } |
| 3755 | |
| 3756 | return hlua_smp2lua(L, &smp); |
| 3757 | } |
| 3758 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3759 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 3760 | { |
| 3761 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3762 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3763 | struct hlua *hlua; |
| 3764 | |
| 3765 | /* Note that this hlua struct is from the session and not from the applet. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 3766 | if (!s->hlua) |
| 3767 | return 0; |
| 3768 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3769 | |
| 3770 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3771 | |
| 3772 | /* Remove previous value. */ |
| 3773 | if (hlua->Mref != -1) |
| 3774 | luaL_unref(L, hlua->Mref, LUA_REGISTRYINDEX); |
| 3775 | |
| 3776 | /* Get and store new value. */ |
| 3777 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3778 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3779 | |
| 3780 | return 0; |
| 3781 | } |
| 3782 | |
| 3783 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 3784 | { |
| 3785 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3786 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3787 | struct hlua *hlua; |
| 3788 | |
| 3789 | /* Note that this hlua struct is from the session and not from the applet. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 3790 | if (!s->hlua) { |
| 3791 | lua_pushnil(L); |
| 3792 | return 1; |
| 3793 | } |
| 3794 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3795 | |
| 3796 | /* Push configuration index in the stack. */ |
| 3797 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3798 | |
| 3799 | return 1; |
| 3800 | } |
| 3801 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3802 | /* If expected data not yet available, it returns a yield. This function |
| 3803 | * consumes the data in the buffer. It returns a string containing the |
| 3804 | * data. This string can be empty. |
| 3805 | */ |
| 3806 | __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] | 3807 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3808 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3809 | struct stream_interface *si = appctx->appctx->owner; |
| 3810 | struct channel *chn = si_ic(si); |
| 3811 | int ret; |
| 3812 | char *blk1; |
| 3813 | int len1; |
| 3814 | char *blk2; |
| 3815 | int len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3816 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3817 | /* Maybe we cant send a 100-continue ? */ |
| 3818 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) { |
| 3819 | ret = bi_putblk(chn, HTTP_100C, strlen(HTTP_100C)); |
| 3820 | /* if ret == -2 or -3 the channel closed or the message si too |
| 3821 | * big for the buffers. We cant send anything. So, we ignoring |
| 3822 | * the error, considers that the 100-continue is sent, and try |
| 3823 | * to receive. |
| 3824 | * If ret is -1, we dont have room in the buffer, so we yield. |
| 3825 | */ |
| 3826 | if (ret == -1) { |
| 3827 | si_applet_cant_put(si); |
| 3828 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0)); |
| 3829 | } |
| 3830 | appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C; |
| 3831 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3832 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3833 | /* Check for the end of the data. */ |
| 3834 | if (appctx->appctx->ctx.hlua_apphttp.left_bytes <= 0) { |
| 3835 | luaL_pushresult(&appctx->b); |
| 3836 | return 1; |
| 3837 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3838 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3839 | /* Read the maximum amount of data avalaible. */ |
| 3840 | ret = bo_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3841 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3842 | /* Data not yet avalaible. return yield. */ |
| 3843 | if (ret == 0) { |
| 3844 | si_applet_cant_get(si); |
| 3845 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0)); |
| 3846 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3847 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3848 | /* End of data: commit the total strings and return. */ |
| 3849 | if (ret < 0) { |
| 3850 | luaL_pushresult(&appctx->b); |
| 3851 | return 1; |
| 3852 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3853 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3854 | /* Ensure that the block 2 length is usable. */ |
| 3855 | if (ret == 1) |
| 3856 | len2 = 0; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3857 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3858 | /* Copy the fisrt block caping to the length required. */ |
| 3859 | if (len1 > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 3860 | len1 = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 3861 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3862 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3863 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3864 | /* Copy the second block. */ |
| 3865 | if (len2 > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 3866 | len2 = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 3867 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3868 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3869 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3870 | /* Consume input channel output buffer data. */ |
| 3871 | bo_skip(si_oc(si), len1 + len2); |
| 3872 | luaL_pushresult(&appctx->b); |
| 3873 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3874 | } |
| 3875 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3876 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 3877 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3878 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3879 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3880 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3881 | /* Initialise the string catenation. */ |
| 3882 | luaL_buffinit(L, &appctx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3883 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3884 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3885 | } |
| 3886 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3887 | /* If expected data not yet available, it returns a yield. This function |
| 3888 | * consumes the data in the buffer. It returns a string containing the |
| 3889 | * data. This string can be empty. |
| 3890 | */ |
| 3891 | __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] | 3892 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3893 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3894 | struct stream_interface *si = appctx->appctx->owner; |
| 3895 | int len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3896 | struct channel *chn = si_ic(si); |
| 3897 | int ret; |
| 3898 | char *blk1; |
| 3899 | int len1; |
| 3900 | char *blk2; |
| 3901 | int len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3902 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3903 | /* Maybe we cant send a 100-continue ? */ |
| 3904 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) { |
| 3905 | ret = bi_putblk(chn, HTTP_100C, strlen(HTTP_100C)); |
| 3906 | /* if ret == -2 or -3 the channel closed or the message si too |
| 3907 | * big for the buffers. We cant send anything. So, we ignoring |
| 3908 | * the error, considers that the 100-continue is sent, and try |
| 3909 | * to receive. |
| 3910 | * If ret is -1, we dont have room in the buffer, so we yield. |
| 3911 | */ |
| 3912 | if (ret == -1) { |
| 3913 | si_applet_cant_put(si); |
| 3914 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0)); |
| 3915 | } |
| 3916 | appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C; |
| 3917 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3918 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3919 | /* Read the maximum amount of data avalaible. */ |
| 3920 | ret = bo_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3921 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3922 | /* Data not yet avalaible. return yield. */ |
| 3923 | if (ret == 0) { |
| 3924 | si_applet_cant_get(si); |
| 3925 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0)); |
| 3926 | } |
| 3927 | |
| 3928 | /* End of data: commit the total strings and return. */ |
| 3929 | if (ret < 0) { |
| 3930 | luaL_pushresult(&appctx->b); |
| 3931 | return 1; |
| 3932 | } |
| 3933 | |
| 3934 | /* Ensure that the block 2 length is usable. */ |
| 3935 | if (ret == 1) |
| 3936 | len2 = 0; |
| 3937 | |
| 3938 | /* Copy the fisrt block caping to the length required. */ |
| 3939 | if (len1 > len) |
| 3940 | len1 = len; |
| 3941 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3942 | len -= len1; |
| 3943 | |
| 3944 | /* Copy the second block. */ |
| 3945 | if (len2 > len) |
| 3946 | len2 = len; |
| 3947 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3948 | len -= len2; |
| 3949 | |
| 3950 | /* Consume input channel output buffer data. */ |
| 3951 | bo_skip(si_oc(si), len1 + len2); |
| 3952 | if (appctx->appctx->ctx.hlua_apphttp.left_bytes != -1) |
| 3953 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len; |
| 3954 | |
| 3955 | /* If we are no other data avalaible, yield waiting for new data. */ |
| 3956 | if (len > 0) { |
| 3957 | lua_pushinteger(L, len); |
| 3958 | lua_replace(L, 2); |
| 3959 | si_applet_cant_get(si); |
| 3960 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0)); |
| 3961 | } |
| 3962 | |
| 3963 | /* return the result. */ |
| 3964 | luaL_pushresult(&appctx->b); |
| 3965 | return 1; |
| 3966 | } |
| 3967 | |
| 3968 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 3969 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 3970 | { |
| 3971 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3972 | int len = -1; |
| 3973 | |
| 3974 | /* Check arguments. */ |
| 3975 | if (lua_gettop(L) > 2) |
| 3976 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 3977 | if (lua_gettop(L) >= 2) { |
| 3978 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3979 | lua_pop(L, 1); |
| 3980 | } |
| 3981 | |
| 3982 | /* Check the required length */ |
| 3983 | if (len == -1 || len > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 3984 | len = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 3985 | lua_pushinteger(L, len); |
| 3986 | |
| 3987 | /* Initialise the string catenation. */ |
| 3988 | luaL_buffinit(L, &appctx->b); |
| 3989 | |
| 3990 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
| 3991 | } |
| 3992 | |
| 3993 | /* Append data in the output side of the buffer. This data is immediatly |
| 3994 | * sent. The fcuntion returns the ammount of data writed. If the buffer |
| 3995 | * cannot contains the data, the function yield. The function returns -1 |
| 3996 | * if the channel is closed. |
| 3997 | */ |
| 3998 | __LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 3999 | { |
| 4000 | size_t len; |
| 4001 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4002 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4003 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4004 | struct stream_interface *si = appctx->appctx->owner; |
| 4005 | struct channel *chn = si_ic(si); |
| 4006 | int max; |
| 4007 | |
| 4008 | /* Get the max amount of data which can write as input in the channel. */ |
| 4009 | max = channel_recv_max(chn); |
| 4010 | if (max > (len - l)) |
| 4011 | max = len - l; |
| 4012 | |
| 4013 | /* Copy data. */ |
| 4014 | bi_putblk(chn, str + l, max); |
| 4015 | |
| 4016 | /* update counters. */ |
| 4017 | l += max; |
| 4018 | lua_pop(L, 1); |
| 4019 | lua_pushinteger(L, l); |
| 4020 | |
| 4021 | /* If some data is not send, declares the situation to the |
| 4022 | * applet, and returns a yield. |
| 4023 | */ |
| 4024 | if (l < len) { |
| 4025 | si_applet_cant_put(si); |
| 4026 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0)); |
| 4027 | } |
| 4028 | |
| 4029 | return 1; |
| 4030 | } |
| 4031 | |
| 4032 | /* Just a wraper of "hlua_applet_send_yield". This wrapper permits |
| 4033 | * yield the LUA process, and resume it without checking the |
| 4034 | * input arguments. |
| 4035 | */ |
| 4036 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 4037 | { |
| 4038 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4039 | size_t len; |
| 4040 | char hex[10]; |
| 4041 | |
| 4042 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4043 | |
| 4044 | /* If transfer encoding chunked is selected, we surround the data |
| 4045 | * by chunk data. |
| 4046 | */ |
| 4047 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED) { |
| 4048 | snprintf(hex, 9, "%x", (unsigned int)len); |
| 4049 | lua_pushfstring(L, "%s\r\n", hex); |
| 4050 | lua_insert(L, 2); /* swap the last 2 entries. */ |
| 4051 | lua_pushstring(L, "\r\n"); |
| 4052 | lua_concat(L, 3); |
| 4053 | } |
| 4054 | |
| 4055 | /* This interger is used for followinf the amount of data sent. */ |
| 4056 | lua_pushinteger(L, 0); |
| 4057 | |
| 4058 | /* We want to send some data. Headers must be sent. */ |
| 4059 | if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 4060 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 4061 | WILL_LJMP(lua_error(L)); |
| 4062 | } |
| 4063 | |
| 4064 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
| 4065 | } |
| 4066 | |
| 4067 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 4068 | { |
| 4069 | const char *name; |
| 4070 | int ret; |
| 4071 | |
| 4072 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4073 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4074 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 4075 | |
| 4076 | /* Push in the stack the "response" entry. */ |
| 4077 | ret = lua_getfield(L, 1, "response"); |
| 4078 | if (ret != LUA_TTABLE) { |
| 4079 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 4080 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 4081 | WILL_LJMP(lua_error(L)); |
| 4082 | } |
| 4083 | |
| 4084 | /* check if the header is already registered if it is not |
| 4085 | * the case, register it. |
| 4086 | */ |
| 4087 | ret = lua_getfield(L, -1, name); |
| 4088 | if (ret == LUA_TNIL) { |
| 4089 | |
| 4090 | /* Entry not found. */ |
| 4091 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 4092 | |
| 4093 | /* Insert the new header name in the array in the top of the stack. |
| 4094 | * It left the new array in the top of the stack. |
| 4095 | */ |
| 4096 | lua_newtable(L); |
| 4097 | lua_pushvalue(L, 2); |
| 4098 | lua_pushvalue(L, -2); |
| 4099 | lua_settable(L, -4); |
| 4100 | |
| 4101 | } else if (ret != LUA_TTABLE) { |
| 4102 | |
| 4103 | /* corruption error. */ |
| 4104 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 4105 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 4106 | WILL_LJMP(lua_error(L)); |
| 4107 | } |
| 4108 | |
| 4109 | /* Now the top od thestack is an array of values. We push |
| 4110 | * the header value as new entry. |
| 4111 | */ |
| 4112 | lua_pushvalue(L, 3); |
| 4113 | ret = lua_rawlen(L, -2); |
| 4114 | lua_rawseti(L, -2, ret + 1); |
| 4115 | lua_pushboolean(L, 1); |
| 4116 | return 1; |
| 4117 | } |
| 4118 | |
| 4119 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 4120 | { |
| 4121 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4122 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4123 | |
| 4124 | if (status < 100 || status > 599) { |
| 4125 | lua_pushboolean(L, 0); |
| 4126 | return 1; |
| 4127 | } |
| 4128 | |
| 4129 | appctx->appctx->ctx.hlua_apphttp.status = status; |
| 4130 | lua_pushboolean(L, 1); |
| 4131 | return 1; |
| 4132 | } |
| 4133 | |
| 4134 | /* We will build the status line and the headers of the HTTP response. |
| 4135 | * We will try send at once if its not possible, we give back the hand |
| 4136 | * waiting for more room. |
| 4137 | */ |
| 4138 | __LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx) |
| 4139 | { |
| 4140 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4141 | struct stream_interface *si = appctx->appctx->owner; |
| 4142 | struct channel *chn = si_ic(si); |
| 4143 | int ret; |
| 4144 | size_t len; |
| 4145 | const char *msg; |
| 4146 | |
| 4147 | /* Get the message as the first argument on the stack. */ |
| 4148 | msg = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4149 | |
| 4150 | /* Send the message at once. */ |
| 4151 | ret = bi_putblk(chn, msg, len); |
| 4152 | |
| 4153 | /* if ret == -2 or -3 the channel closed or the message si too |
| 4154 | * big for the buffers. |
| 4155 | */ |
| 4156 | if (ret == -2 || ret == -3) { |
| 4157 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 4158 | WILL_LJMP(lua_error(L)); |
| 4159 | } |
| 4160 | |
| 4161 | /* If ret is -1, we dont have room in the buffer, so we yield. */ |
| 4162 | if (ret == -1) { |
| 4163 | si_applet_cant_put(si); |
| 4164 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0)); |
| 4165 | } |
| 4166 | |
| 4167 | /* Headers sent, set the flag. */ |
| 4168 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
| 4169 | return 0; |
| 4170 | } |
| 4171 | |
| 4172 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
| 4173 | { |
| 4174 | struct chunk *tmp = get_trash_chunk(); |
| 4175 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4176 | const char *name; |
| 4177 | const char *value; |
| 4178 | int id; |
| 4179 | int hdr_connection = 0; |
| 4180 | int hdr_contentlength = -1; |
| 4181 | int hdr_chunked = 0; |
| 4182 | |
| 4183 | /* Use the same http version than the request. */ |
| 4184 | chunk_appendf(tmp, "HTTP/1.%c %d %s\r\n", |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 4185 | appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11 ? '1' : '0', |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4186 | appctx->appctx->ctx.hlua_apphttp.status, |
| 4187 | get_reason(appctx->appctx->ctx.hlua_apphttp.status)); |
| 4188 | |
| 4189 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 4190 | lua_pushvalue(L, 0); |
| 4191 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 4192 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
| 4193 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4194 | WILL_LJMP(lua_error(L)); |
| 4195 | } |
| 4196 | |
| 4197 | /* Browse the list of headers. */ |
| 4198 | lua_pushnil(L); |
| 4199 | while(lua_next(L, -2) != 0) { |
| 4200 | |
| 4201 | /* We expect a string as -2. */ |
| 4202 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 4203 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
| 4204 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4205 | lua_typename(L, lua_type(L, -2))); |
| 4206 | WILL_LJMP(lua_error(L)); |
| 4207 | } |
| 4208 | name = lua_tostring(L, -2); |
| 4209 | |
| 4210 | /* We expect an array as -1. */ |
| 4211 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 4212 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
| 4213 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4214 | name, |
| 4215 | lua_typename(L, lua_type(L, -1))); |
| 4216 | WILL_LJMP(lua_error(L)); |
| 4217 | } |
| 4218 | |
| 4219 | /* Browse the table who is on the top of the stack. */ |
| 4220 | lua_pushnil(L); |
| 4221 | while(lua_next(L, -2) != 0) { |
| 4222 | |
| 4223 | /* We expect a number as -2. */ |
| 4224 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 4225 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
| 4226 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4227 | name, |
| 4228 | lua_typename(L, lua_type(L, -2))); |
| 4229 | WILL_LJMP(lua_error(L)); |
| 4230 | } |
| 4231 | id = lua_tointeger(L, -2); |
| 4232 | |
| 4233 | /* We expect a string as -2. */ |
| 4234 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 4235 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
| 4236 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4237 | name, id, |
| 4238 | lua_typename(L, lua_type(L, -1))); |
| 4239 | WILL_LJMP(lua_error(L)); |
| 4240 | } |
| 4241 | value = lua_tostring(L, -1); |
| 4242 | |
| 4243 | /* Catenate a new header. */ |
| 4244 | chunk_appendf(tmp, "%s: %s\r\n", name, value); |
| 4245 | |
| 4246 | /* Protocol checks. */ |
| 4247 | |
| 4248 | /* Check if the header conneciton is present. */ |
| 4249 | if (strcasecmp("connection", name) == 0) |
| 4250 | hdr_connection = 1; |
| 4251 | |
| 4252 | /* Copy the header content length. The length conversion |
| 4253 | * is done without control. If it contains a ad value, this |
| 4254 | * is not our problem. |
| 4255 | */ |
| 4256 | if (strcasecmp("content-length", name) == 0) |
| 4257 | hdr_contentlength = atoi(value); |
| 4258 | |
| 4259 | /* Check if the client annouces a transfer-encoding chunked it self. */ |
| 4260 | if (strcasecmp("transfer-encoding", name) == 0 && |
| 4261 | strcasecmp("chunked", value) == 0) |
| 4262 | hdr_chunked = 1; |
| 4263 | |
| 4264 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4265 | lua_pop(L, 1); |
| 4266 | } |
| 4267 | |
| 4268 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4269 | lua_pop(L, 1); |
| 4270 | } |
| 4271 | |
| 4272 | /* If the http protocol version is 1.1, we expect an header "connection" set |
| 4273 | * to "close" to be HAProxy/keeplive compliant. Otherwise, we expect nothing. |
| 4274 | * If the header conneciton is present, don't change it, if it is not present, |
| 4275 | * we must set. |
| 4276 | * |
| 4277 | * we set a "connection: close" header for ensuring that the keepalive will be |
| 4278 | * respected by haproxy. HAProcy considers that the application cloe the connection |
| 4279 | * and it keep the connection from the client open. |
| 4280 | */ |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 4281 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11 && !hdr_connection) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4282 | chunk_appendf(tmp, "Connection: close\r\n"); |
| 4283 | |
| 4284 | /* If we dont have a content-length set, we must announce a transfer enconding |
| 4285 | * chunked. This is required by haproxy for the keepalive compliance. |
| 4286 | * If the applet annouce a transfer-encoding chunked itslef, don't |
| 4287 | * do anything. |
| 4288 | */ |
| 4289 | if (hdr_contentlength == -1 && hdr_chunked == 0) { |
| 4290 | chunk_appendf(tmp, "Transfer-encoding: chunked\r\n"); |
| 4291 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_CHUNKED; |
| 4292 | } |
| 4293 | |
| 4294 | /* Finalize headers. */ |
| 4295 | chunk_appendf(tmp, "\r\n"); |
| 4296 | |
| 4297 | /* Remove the last entry and the array of headers */ |
| 4298 | lua_pop(L, 2); |
| 4299 | |
| 4300 | /* Push the headers block. */ |
| 4301 | lua_pushlstring(L, tmp->str, tmp->len); |
| 4302 | |
| 4303 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
| 4304 | } |
| 4305 | |
| 4306 | /* |
| 4307 | * |
| 4308 | * |
| 4309 | * Class HTTP |
| 4310 | * |
| 4311 | * |
| 4312 | */ |
| 4313 | |
| 4314 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4315 | * a class stream, otherwise it throws an error. |
| 4316 | */ |
| 4317 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
| 4318 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4319 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4320 | } |
| 4321 | |
| 4322 | /* This function creates and push in the stack a HTTP object |
| 4323 | * according with a current TXN. |
| 4324 | */ |
| 4325 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 4326 | { |
| 4327 | struct hlua_txn *htxn; |
| 4328 | |
| 4329 | /* Check stack size. */ |
| 4330 | if (!lua_checkstack(L, 3)) |
| 4331 | return 0; |
| 4332 | |
| 4333 | /* Create the object: obj[0] = userdata. |
| 4334 | * Note that the base of the Converters object is the |
| 4335 | * same than the TXN object. |
| 4336 | */ |
| 4337 | lua_newtable(L); |
| 4338 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 4339 | lua_rawseti(L, -2, 0); |
| 4340 | |
| 4341 | htxn->s = txn->s; |
| 4342 | htxn->p = txn->p; |
| 4343 | |
| 4344 | /* Pop a class stream metatable and affect it to the table. */ |
| 4345 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 4346 | lua_setmetatable(L, -2); |
| 4347 | |
| 4348 | return 1; |
| 4349 | } |
| 4350 | |
| 4351 | /* This function creates ans returns an array of HTTP headers. |
| 4352 | * This function does not fails. It is used as wrapper with the |
| 4353 | * 2 following functions. |
| 4354 | */ |
| 4355 | __LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4356 | { |
| 4357 | const char *cur_ptr, *cur_next, *p; |
| 4358 | int old_idx, cur_idx; |
| 4359 | struct hdr_idx_elem *cur_hdr; |
| 4360 | const char *hn, *hv; |
| 4361 | int hnl, hvl; |
| 4362 | int type; |
| 4363 | const char *in; |
| 4364 | char *out; |
| 4365 | int len; |
| 4366 | |
| 4367 | /* Create the table. */ |
| 4368 | lua_newtable(L); |
| 4369 | |
| 4370 | if (!htxn->s->txn) |
| 4371 | return 1; |
| 4372 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4373 | /* Check if a valid response is parsed */ |
| 4374 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4375 | return 1; |
| 4376 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4377 | /* Build array of headers. */ |
| 4378 | old_idx = 0; |
| 4379 | cur_next = msg->chn->buf->p + hdr_idx_first_pos(&htxn->s->txn->hdr_idx); |
| 4380 | |
| 4381 | while (1) { |
| 4382 | cur_idx = htxn->s->txn->hdr_idx.v[old_idx].next; |
| 4383 | if (!cur_idx) |
| 4384 | break; |
| 4385 | old_idx = cur_idx; |
| 4386 | |
| 4387 | cur_hdr = &htxn->s->txn->hdr_idx.v[cur_idx]; |
| 4388 | cur_ptr = cur_next; |
| 4389 | cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1; |
| 4390 | |
| 4391 | /* Now we have one full header at cur_ptr of len cur_hdr->len, |
| 4392 | * and the next header starts at cur_next. We'll check |
| 4393 | * this header in the list as well as against the default |
| 4394 | * rule. |
| 4395 | */ |
| 4396 | |
| 4397 | /* look for ': *'. */ |
| 4398 | hn = cur_ptr; |
| 4399 | for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++); |
| 4400 | if (p >= cur_ptr+cur_hdr->len) |
| 4401 | continue; |
| 4402 | hnl = p - hn; |
| 4403 | p++; |
| 4404 | while (p < cur_ptr+cur_hdr->len && ( *p == ' ' || *p == '\t' )) |
| 4405 | p++; |
| 4406 | if (p >= cur_ptr+cur_hdr->len) |
| 4407 | continue; |
| 4408 | hv = p; |
| 4409 | hvl = cur_ptr+cur_hdr->len-p; |
| 4410 | |
| 4411 | /* Lowercase the key. Don't check the size of trash, it have |
| 4412 | * the size of one buffer and the input data contains in one |
| 4413 | * buffer. |
| 4414 | */ |
| 4415 | out = trash.str; |
| 4416 | for (in=hn; in<hn+hnl; in++, out++) |
| 4417 | *out = tolower(*in); |
| 4418 | *out = '\0'; |
| 4419 | |
| 4420 | /* Check for existing entry: |
| 4421 | * assume that the table is on the top of the stack, and |
| 4422 | * push the key in the stack, the function lua_gettable() |
| 4423 | * perform the lookup. |
| 4424 | */ |
| 4425 | lua_pushlstring(L, trash.str, hnl); |
| 4426 | lua_gettable(L, -2); |
| 4427 | type = lua_type(L, -1); |
| 4428 | |
| 4429 | switch (type) { |
| 4430 | case LUA_TNIL: |
| 4431 | /* Table not found, create it. */ |
| 4432 | lua_pop(L, 1); /* remove the nil value. */ |
| 4433 | lua_pushlstring(L, trash.str, hnl); /* push the header name as key. */ |
| 4434 | lua_newtable(L); /* create and push empty table. */ |
| 4435 | lua_pushlstring(L, hv, hvl); /* push header value. */ |
| 4436 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 4437 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
| 4438 | break; |
| 4439 | |
| 4440 | case LUA_TTABLE: |
| 4441 | /* Entry found: push the value in the table. */ |
| 4442 | len = lua_rawlen(L, -1); |
| 4443 | lua_pushlstring(L, hv, hvl); /* push header value. */ |
| 4444 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 4445 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 4446 | break; |
| 4447 | |
| 4448 | default: |
| 4449 | /* Other cases are errors. */ |
| 4450 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 4451 | WILL_LJMP(lua_error(L)); |
| 4452 | } |
| 4453 | } |
| 4454 | |
| 4455 | return 1; |
| 4456 | } |
| 4457 | |
| 4458 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 4459 | { |
| 4460 | struct hlua_txn *htxn; |
| 4461 | |
| 4462 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 4463 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4464 | |
| 4465 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->req); |
| 4466 | } |
| 4467 | |
| 4468 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 4469 | { |
| 4470 | struct hlua_txn *htxn; |
| 4471 | |
| 4472 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 4473 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4474 | |
| 4475 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp); |
| 4476 | } |
| 4477 | |
| 4478 | /* This function replace full header, or just a value in |
| 4479 | * the request or in the response. It is a wrapper fir the |
| 4480 | * 4 following functions. |
| 4481 | */ |
| 4482 | __LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn, |
| 4483 | struct http_msg *msg, int action) |
| 4484 | { |
| 4485 | size_t name_len; |
| 4486 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4487 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 4488 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
| 4489 | struct my_regex re; |
| 4490 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4491 | /* Check if a valid response is parsed */ |
| 4492 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4493 | return 0; |
| 4494 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4495 | if (!regex_comp(reg, &re, 1, 1, NULL)) |
| 4496 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 4497 | |
| 4498 | http_transform_header_str(htxn->s, msg, name, name_len, value, &re, action); |
| 4499 | regex_free(&re); |
| 4500 | return 0; |
| 4501 | } |
| 4502 | |
| 4503 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 4504 | { |
| 4505 | struct hlua_txn *htxn; |
| 4506 | |
| 4507 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4508 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4509 | |
| 4510 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR)); |
| 4511 | } |
| 4512 | |
| 4513 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 4514 | { |
| 4515 | struct hlua_txn *htxn; |
| 4516 | |
| 4517 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 4518 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4519 | |
| 4520 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR)); |
| 4521 | } |
| 4522 | |
| 4523 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 4524 | { |
| 4525 | struct hlua_txn *htxn; |
| 4526 | |
| 4527 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4528 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4529 | |
| 4530 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL)); |
| 4531 | } |
| 4532 | |
| 4533 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 4534 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4535 | struct hlua_txn *htxn; |
| 4536 | |
| 4537 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 4538 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4539 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 4540 | 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] | 4541 | } |
| 4542 | |
| 4543 | /* This function deletes all the occurences of an header. |
| 4544 | * It is a wrapper for the 2 following functions. |
| 4545 | */ |
| 4546 | __LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4547 | { |
| 4548 | size_t len; |
| 4549 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4550 | struct hdr_ctx ctx; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4551 | struct http_txn *txn = htxn->s->txn; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4552 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4553 | /* Check if a valid response is parsed */ |
| 4554 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4555 | return 0; |
| 4556 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4557 | ctx.idx = 0; |
| 4558 | while (http_find_header2(name, len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) |
| 4559 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 4560 | return 0; |
| 4561 | } |
| 4562 | |
| 4563 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 4564 | { |
| 4565 | struct hlua_txn *htxn; |
| 4566 | |
| 4567 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 4568 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4569 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4570 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4571 | } |
| 4572 | |
| 4573 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 4574 | { |
| 4575 | struct hlua_txn *htxn; |
| 4576 | |
| 4577 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 4578 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4579 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4580 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4581 | } |
| 4582 | |
| 4583 | /* This function adds an header. It is a wrapper used by |
| 4584 | * the 2 following functions. |
| 4585 | */ |
| 4586 | __LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4587 | { |
| 4588 | size_t name_len; |
| 4589 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4590 | size_t value_len; |
| 4591 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
| 4592 | char *p; |
| 4593 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4594 | /* Check if a valid message is parsed */ |
| 4595 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4596 | return 0; |
| 4597 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4598 | /* Check length. */ |
| 4599 | trash.len = value_len + name_len + 2; |
| 4600 | if (trash.len > trash.size) |
| 4601 | return 0; |
| 4602 | |
| 4603 | /* Creates the header string. */ |
| 4604 | p = trash.str; |
| 4605 | memcpy(p, name, name_len); |
| 4606 | p += name_len; |
| 4607 | *p = ':'; |
| 4608 | p++; |
| 4609 | *p = ' '; |
| 4610 | p++; |
| 4611 | memcpy(p, value, value_len); |
| 4612 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4613 | 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] | 4614 | trash.str, trash.len) != 0); |
| 4615 | |
| 4616 | return 0; |
| 4617 | } |
| 4618 | |
| 4619 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 4620 | { |
| 4621 | struct hlua_txn *htxn; |
| 4622 | |
| 4623 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 4624 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4625 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4626 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4627 | } |
| 4628 | |
| 4629 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 4630 | { |
| 4631 | struct hlua_txn *htxn; |
| 4632 | |
| 4633 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 4634 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4635 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4636 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4637 | } |
| 4638 | |
| 4639 | static int hlua_http_req_set_hdr(lua_State *L) |
| 4640 | { |
| 4641 | struct hlua_txn *htxn; |
| 4642 | |
| 4643 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 4644 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4645 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4646 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
| 4647 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4648 | } |
| 4649 | |
| 4650 | static int hlua_http_res_set_hdr(lua_State *L) |
| 4651 | { |
| 4652 | struct hlua_txn *htxn; |
| 4653 | |
| 4654 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 4655 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4656 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4657 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
| 4658 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4659 | } |
| 4660 | |
| 4661 | /* This function set the method. */ |
| 4662 | static int hlua_http_req_set_meth(lua_State *L) |
| 4663 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4664 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4665 | size_t name_len; |
| 4666 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4667 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4668 | /* Check if a valid request is parsed */ |
| 4669 | if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 4670 | lua_pushboolean(L, 0); |
| 4671 | return 1; |
| 4672 | } |
| 4673 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4674 | 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] | 4675 | return 1; |
| 4676 | } |
| 4677 | |
| 4678 | /* This function set the method. */ |
| 4679 | static int hlua_http_req_set_path(lua_State *L) |
| 4680 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4681 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4682 | size_t name_len; |
| 4683 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4684 | |
| 4685 | /* Check if a valid request is parsed */ |
| 4686 | if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 4687 | lua_pushboolean(L, 0); |
| 4688 | return 1; |
| 4689 | } |
| 4690 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4691 | 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] | 4692 | return 1; |
| 4693 | } |
| 4694 | |
| 4695 | /* This function set the query-string. */ |
| 4696 | static int hlua_http_req_set_query(lua_State *L) |
| 4697 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4698 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4699 | size_t name_len; |
| 4700 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4701 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4702 | /* Check if a valid request is parsed */ |
| 4703 | if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 4704 | lua_pushboolean(L, 0); |
| 4705 | return 1; |
| 4706 | } |
| 4707 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4708 | /* Check length. */ |
| 4709 | if (name_len > trash.size - 1) { |
| 4710 | lua_pushboolean(L, 0); |
| 4711 | return 1; |
| 4712 | } |
| 4713 | |
| 4714 | /* Add the mark question as prefix. */ |
| 4715 | chunk_reset(&trash); |
| 4716 | trash.str[trash.len++] = '?'; |
| 4717 | memcpy(trash.str + trash.len, name, name_len); |
| 4718 | trash.len += name_len; |
| 4719 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4720 | 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] | 4721 | return 1; |
| 4722 | } |
| 4723 | |
| 4724 | /* This function set the uri. */ |
| 4725 | static int hlua_http_req_set_uri(lua_State *L) |
| 4726 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4727 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4728 | size_t name_len; |
| 4729 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4730 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4731 | /* Check if a valid request is parsed */ |
| 4732 | if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 4733 | lua_pushboolean(L, 0); |
| 4734 | return 1; |
| 4735 | } |
| 4736 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4737 | 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] | 4738 | return 1; |
| 4739 | } |
| 4740 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 4741 | /* This function set the response code. */ |
| 4742 | static int hlua_http_res_set_status(lua_State *L) |
| 4743 | { |
| 4744 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4745 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4746 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4747 | /* Check if a valid response is parsed */ |
| 4748 | if (unlikely(htxn->s->txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 4749 | return 0; |
| 4750 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 4751 | http_set_status(code, htxn->s); |
| 4752 | return 0; |
| 4753 | } |
| 4754 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4755 | /* |
| 4756 | * |
| 4757 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4758 | * Class TXN |
| 4759 | * |
| 4760 | * |
| 4761 | */ |
| 4762 | |
| 4763 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4764 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4765 | */ |
| 4766 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 4767 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4768 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4769 | } |
| 4770 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 4771 | __LJMP static int hlua_set_var(lua_State *L) |
| 4772 | { |
| 4773 | struct hlua_txn *htxn; |
| 4774 | const char *name; |
| 4775 | size_t len; |
| 4776 | struct sample smp; |
| 4777 | |
| 4778 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 4779 | |
| 4780 | /* It is useles to retrieve the stream, but this function |
| 4781 | * runs only in a stream context. |
| 4782 | */ |
| 4783 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 4784 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4785 | |
| 4786 | /* Converts the third argument in a sample. */ |
| 4787 | hlua_lua2smp(L, 3, &smp); |
| 4788 | |
| 4789 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 4790 | 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] | 4791 | vars_set_by_name(name, len, &smp); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 4792 | return 0; |
| 4793 | } |
| 4794 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 4795 | __LJMP static int hlua_unset_var(lua_State *L) |
| 4796 | { |
| 4797 | struct hlua_txn *htxn; |
| 4798 | const char *name; |
| 4799 | size_t len; |
| 4800 | struct sample smp; |
| 4801 | |
| 4802 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4803 | |
| 4804 | /* It is useles to retrieve the stream, but this function |
| 4805 | * runs only in a stream context. |
| 4806 | */ |
| 4807 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 4808 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4809 | |
| 4810 | /* Unset the variable. */ |
| 4811 | smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR); |
| 4812 | vars_unset_by_name(name, len, &smp); |
| 4813 | return 0; |
| 4814 | } |
| 4815 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 4816 | __LJMP static int hlua_get_var(lua_State *L) |
| 4817 | { |
| 4818 | struct hlua_txn *htxn; |
| 4819 | const char *name; |
| 4820 | size_t len; |
| 4821 | struct sample smp; |
| 4822 | |
| 4823 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4824 | |
| 4825 | /* It is useles to retrieve the stream, but this function |
| 4826 | * runs only in a stream context. |
| 4827 | */ |
| 4828 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 4829 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4830 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 4831 | 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] | 4832 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 4833 | lua_pushnil(L); |
| 4834 | return 1; |
| 4835 | } |
| 4836 | |
| 4837 | return hlua_smp2lua(L, &smp); |
| 4838 | } |
| 4839 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 4840 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 4841 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4842 | struct hlua *hlua; |
| 4843 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 4844 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4845 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4846 | /* It is useles to retrieve the stream, but this function |
| 4847 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 4848 | */ |
| 4849 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4850 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 4851 | |
| 4852 | /* Remove previous value. */ |
| 4853 | if (hlua->Mref != -1) |
| 4854 | luaL_unref(L, hlua->Mref, LUA_REGISTRYINDEX); |
| 4855 | |
| 4856 | /* Get and store new value. */ |
| 4857 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4858 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4859 | |
| 4860 | return 0; |
| 4861 | } |
| 4862 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 4863 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 4864 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4865 | struct hlua *hlua; |
| 4866 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 4867 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 4868 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4869 | /* It is useles to retrieve the stream, but this function |
| 4870 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 4871 | */ |
| 4872 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4873 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 4874 | |
| 4875 | /* Push configuration index in the stack. */ |
| 4876 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4877 | |
| 4878 | return 1; |
| 4879 | } |
| 4880 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4881 | /* Create stack entry containing a class TXN. This function |
| 4882 | * return 0 if the stack does not contains free slots, |
| 4883 | * otherwise it returns 1. |
| 4884 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 4885 | static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir, int flags) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4886 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 4887 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4888 | |
| 4889 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 4890 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4891 | return 0; |
| 4892 | |
| 4893 | /* NOTE: The allocation never fails. The failure |
| 4894 | * throw an error, and the function never returns. |
| 4895 | * if the throw is not avalaible, the process is aborted. |
| 4896 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 4897 | /* Create the object: obj[0] = userdata. */ |
| 4898 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 4899 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 4900 | lua_rawseti(L, -2, 0); |
| 4901 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 4902 | htxn->s = s; |
| 4903 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 4904 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 4905 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4906 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4907 | /* Create the "f" field that contains a list of fetches. */ |
| 4908 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 4909 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4910 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 4911 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4912 | |
| 4913 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4914 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 4915 | 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] | 4916 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 4917 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4918 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4919 | /* Create the "c" field that contains a list of converters. */ |
| 4920 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 4921 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4922 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 4923 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4924 | |
| 4925 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4926 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4927 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4928 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 4929 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4930 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 4931 | /* Create the "req" field that contains the request channel object. */ |
| 4932 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 4933 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 4934 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 4935 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 4936 | |
| 4937 | /* Create the "res" field that contains the response channel object. */ |
| 4938 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 4939 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 4940 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 4941 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 4942 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4943 | /* Creates the HTTP object is the current proxy allows http. */ |
| 4944 | lua_pushstring(L, "http"); |
| 4945 | if (p->mode == PR_MODE_HTTP) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 4946 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4947 | return 0; |
| 4948 | } |
| 4949 | else |
| 4950 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 4951 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4952 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4953 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 4954 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 4955 | lua_setmetatable(L, -2); |
| 4956 | |
| 4957 | return 1; |
| 4958 | } |
| 4959 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 4960 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 4961 | { |
| 4962 | const char *msg; |
| 4963 | struct hlua_txn *htxn; |
| 4964 | |
| 4965 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 4966 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 4967 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4968 | |
| 4969 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 4970 | return 0; |
| 4971 | } |
| 4972 | |
| 4973 | __LJMP static int hlua_txn_log(lua_State *L) |
| 4974 | { |
| 4975 | int level; |
| 4976 | const char *msg; |
| 4977 | struct hlua_txn *htxn; |
| 4978 | |
| 4979 | MAY_LJMP(check_args(L, 3, "log")); |
| 4980 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 4981 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4982 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 4983 | |
| 4984 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 4985 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 4986 | |
| 4987 | hlua_sendlog(htxn->s->be, level, msg); |
| 4988 | return 0; |
| 4989 | } |
| 4990 | |
| 4991 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 4992 | { |
| 4993 | const char *msg; |
| 4994 | struct hlua_txn *htxn; |
| 4995 | |
| 4996 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 4997 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 4998 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4999 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 5000 | return 0; |
| 5001 | } |
| 5002 | |
| 5003 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 5004 | { |
| 5005 | const char *msg; |
| 5006 | struct hlua_txn *htxn; |
| 5007 | |
| 5008 | MAY_LJMP(check_args(L, 2, "Info")); |
| 5009 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5010 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5011 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 5012 | return 0; |
| 5013 | } |
| 5014 | |
| 5015 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 5016 | { |
| 5017 | const char *msg; |
| 5018 | struct hlua_txn *htxn; |
| 5019 | |
| 5020 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 5021 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5022 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5023 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 5024 | return 0; |
| 5025 | } |
| 5026 | |
| 5027 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 5028 | { |
| 5029 | const char *msg; |
| 5030 | struct hlua_txn *htxn; |
| 5031 | |
| 5032 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 5033 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5034 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5035 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 5036 | return 0; |
| 5037 | } |
| 5038 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5039 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 5040 | { |
| 5041 | struct hlua_txn *htxn; |
| 5042 | int ll; |
| 5043 | |
| 5044 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 5045 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5046 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5047 | |
| 5048 | if (ll < 0 || ll > 7) |
| 5049 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 5050 | |
| 5051 | htxn->s->logs.level = ll; |
| 5052 | return 0; |
| 5053 | } |
| 5054 | |
| 5055 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 5056 | { |
| 5057 | struct hlua_txn *htxn; |
| 5058 | struct connection *cli_conn; |
| 5059 | int tos; |
| 5060 | |
| 5061 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 5062 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5063 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5064 | |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 5065 | if ((cli_conn = objt_conn(htxn->s->sess->origin)) && conn_ctrl_ready(cli_conn)) |
Vincent Bernat | 6e61589 | 2016-05-18 16:17:44 +0200 | [diff] [blame] | 5066 | inet_set_tos(cli_conn->t.sock.fd, &cli_conn->addr.from, tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5067 | |
| 5068 | return 0; |
| 5069 | } |
| 5070 | |
| 5071 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 5072 | { |
| 5073 | #ifdef SO_MARK |
| 5074 | struct hlua_txn *htxn; |
| 5075 | struct connection *cli_conn; |
| 5076 | int mark; |
| 5077 | |
| 5078 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 5079 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5080 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5081 | |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 5082 | 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] | 5083 | 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] | 5084 | #endif |
| 5085 | return 0; |
| 5086 | } |
| 5087 | |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5088 | /* This function is an Lua binding that send pending data |
| 5089 | * to the client, and close the stream interface. |
| 5090 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5091 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5092 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5093 | struct hlua_txn *htxn; |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 5094 | struct hlua *hlua; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5095 | struct channel *ic, *oc; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5096 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5097 | MAY_LJMP(check_args(L, 1, "close")); |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5098 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 5099 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5100 | |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5101 | /* If the flags NOTERM is set, we cannot terminate the http |
| 5102 | * session, so we just end the execution of the current |
| 5103 | * lua code. |
| 5104 | */ |
| 5105 | if (htxn->flags & HLUA_TXN_NOTERM) { |
| 5106 | WILL_LJMP(hlua_done(L)); |
| 5107 | return 0; |
| 5108 | } |
| 5109 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5110 | ic = &htxn->s->req; |
| 5111 | oc = &htxn->s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5112 | |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 5113 | if (htxn->s->txn) { |
| 5114 | /* HTTP mode, let's stay in sync with the stream */ |
| 5115 | bi_fast_delete(ic->buf, htxn->s->txn->req.sov); |
| 5116 | htxn->s->txn->req.next -= htxn->s->txn->req.sov; |
| 5117 | htxn->s->txn->req.sov = 0; |
| 5118 | ic->analysers &= AN_REQ_HTTP_XFER_BODY; |
| 5119 | oc->analysers = AN_RES_HTTP_XFER_BODY; |
| 5120 | htxn->s->txn->req.msg_state = HTTP_MSG_CLOSED; |
| 5121 | htxn->s->txn->rsp.msg_state = HTTP_MSG_DONE; |
| 5122 | |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 5123 | /* Note that if we want to support keep-alive, we need |
| 5124 | * to bypass the close/shutr_now calls below, but that |
| 5125 | * may only be done if the HTTP request was already |
| 5126 | * processed and the connection header is known (ie |
| 5127 | * not during TCP rules). |
| 5128 | */ |
| 5129 | } |
| 5130 | |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 5131 | channel_auto_read(ic); |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5132 | channel_abort(ic); |
| 5133 | channel_auto_close(ic); |
| 5134 | channel_erase(ic); |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 5135 | |
| 5136 | oc->wex = tick_add_ifset(now_ms, oc->wto); |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5137 | channel_auto_read(oc); |
| 5138 | channel_auto_close(oc); |
| 5139 | channel_shutr_now(oc); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5140 | |
Willy Tarreau | 0458b08 | 2015-08-28 09:40:04 +0200 | [diff] [blame] | 5141 | ic->analysers = 0; |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5142 | |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 5143 | hlua->flags |= HLUA_STOP; |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5144 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5145 | return 0; |
| 5146 | } |
| 5147 | |
| 5148 | __LJMP static int hlua_log(lua_State *L) |
| 5149 | { |
| 5150 | int level; |
| 5151 | const char *msg; |
| 5152 | |
| 5153 | MAY_LJMP(check_args(L, 2, "log")); |
| 5154 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 5155 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5156 | |
| 5157 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5158 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5159 | |
| 5160 | hlua_sendlog(NULL, level, msg); |
| 5161 | return 0; |
| 5162 | } |
| 5163 | |
| 5164 | __LJMP static int hlua_log_debug(lua_State *L) |
| 5165 | { |
| 5166 | const char *msg; |
| 5167 | |
| 5168 | MAY_LJMP(check_args(L, 1, "debug")); |
| 5169 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5170 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 5171 | return 0; |
| 5172 | } |
| 5173 | |
| 5174 | __LJMP static int hlua_log_info(lua_State *L) |
| 5175 | { |
| 5176 | const char *msg; |
| 5177 | |
| 5178 | MAY_LJMP(check_args(L, 1, "info")); |
| 5179 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5180 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 5181 | return 0; |
| 5182 | } |
| 5183 | |
| 5184 | __LJMP static int hlua_log_warning(lua_State *L) |
| 5185 | { |
| 5186 | const char *msg; |
| 5187 | |
| 5188 | MAY_LJMP(check_args(L, 1, "warning")); |
| 5189 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5190 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 5191 | return 0; |
| 5192 | } |
| 5193 | |
| 5194 | __LJMP static int hlua_log_alert(lua_State *L) |
| 5195 | { |
| 5196 | const char *msg; |
| 5197 | |
| 5198 | MAY_LJMP(check_args(L, 1, "alert")); |
| 5199 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5200 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5201 | return 0; |
| 5202 | } |
| 5203 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5204 | __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] | 5205 | { |
| 5206 | int wakeup_ms = lua_tointeger(L, -1); |
| 5207 | if (now_ms < wakeup_ms) |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5208 | 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] | 5209 | return 0; |
| 5210 | } |
| 5211 | |
| 5212 | __LJMP static int hlua_sleep(lua_State *L) |
| 5213 | { |
| 5214 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5215 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5216 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5217 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5218 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5219 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5220 | wakeup_ms = tick_add(now_ms, delay); |
| 5221 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5222 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5223 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0)); |
| 5224 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5225 | } |
| 5226 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5227 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5228 | { |
| 5229 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5230 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5231 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5232 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5233 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5234 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5235 | wakeup_ms = tick_add(now_ms, delay); |
| 5236 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5237 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5238 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0)); |
| 5239 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5240 | } |
| 5241 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5242 | /* This functionis an LUA binding. it permits to give back |
| 5243 | * the hand at the HAProxy scheduler. It is used when the |
| 5244 | * LUA processing consumes a lot of time. |
| 5245 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5246 | __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] | 5247 | { |
| 5248 | return 0; |
| 5249 | } |
| 5250 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5251 | __LJMP static int hlua_yield(lua_State *L) |
| 5252 | { |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5253 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD)); |
| 5254 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5255 | } |
| 5256 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5257 | /* This function change the nice of the currently executed |
| 5258 | * task. It is used set low or high priority at the current |
| 5259 | * task. |
| 5260 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5261 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5262 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5263 | struct hlua *hlua; |
| 5264 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5265 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5266 | MAY_LJMP(check_args(L, 1, "set_nice")); |
| 5267 | hlua = hlua_gethlua(L); |
| 5268 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5269 | |
| 5270 | /* If he task is not set, I'm in a start mode. */ |
| 5271 | if (!hlua || !hlua->task) |
| 5272 | return 0; |
| 5273 | |
| 5274 | if (nice < -1024) |
| 5275 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5276 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5277 | nice = 1024; |
| 5278 | |
| 5279 | hlua->task->nice = nice; |
| 5280 | return 0; |
| 5281 | } |
| 5282 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5283 | /* This function is used as a calback of a task. It is called by the |
| 5284 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 5285 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 5286 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5287 | * |
| 5288 | * Task wrapper are longjmp safe because the only one Lua code |
| 5289 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5290 | */ |
| 5291 | static struct task *hlua_process_task(struct task *task) |
| 5292 | { |
| 5293 | struct hlua *hlua = task->context; |
| 5294 | enum hlua_exec status; |
| 5295 | |
| 5296 | /* We need to remove the task from the wait queue before executing |
| 5297 | * the Lua code because we don't know if it needs to wait for |
| 5298 | * another timer or not in the case of E_AGAIN. |
| 5299 | */ |
| 5300 | task_delete(task); |
| 5301 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5302 | /* If it is the first call to the task, we must initialize the |
| 5303 | * execution timeouts. |
| 5304 | */ |
| 5305 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 5306 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5307 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5308 | /* Execute the Lua code. */ |
| 5309 | status = hlua_ctx_resume(hlua, 1); |
| 5310 | |
| 5311 | switch (status) { |
| 5312 | /* finished or yield */ |
| 5313 | case HLUA_E_OK: |
| 5314 | hlua_ctx_destroy(hlua); |
| 5315 | task_delete(task); |
| 5316 | task_free(task); |
| 5317 | break; |
| 5318 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 5319 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
| 5320 | if (hlua->wake_time != TICK_ETERNITY) |
| 5321 | task_schedule(task, hlua->wake_time); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5322 | break; |
| 5323 | |
| 5324 | /* finished with error. */ |
| 5325 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5326 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5327 | hlua_ctx_destroy(hlua); |
| 5328 | task_delete(task); |
| 5329 | task_free(task); |
| 5330 | break; |
| 5331 | |
| 5332 | case HLUA_E_ERR: |
| 5333 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5334 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5335 | hlua_ctx_destroy(hlua); |
| 5336 | task_delete(task); |
| 5337 | task_free(task); |
| 5338 | break; |
| 5339 | } |
| 5340 | return NULL; |
| 5341 | } |
| 5342 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 5343 | /* This function is an LUA binding that register LUA function to be |
| 5344 | * executed after the HAProxy configuration parsing and before the |
| 5345 | * HAProxy scheduler starts. This function expect only one LUA |
| 5346 | * argument that is a function. This function returns nothing, but |
| 5347 | * throws if an error is encountered. |
| 5348 | */ |
| 5349 | __LJMP static int hlua_register_init(lua_State *L) |
| 5350 | { |
| 5351 | struct hlua_init_function *init; |
| 5352 | int ref; |
| 5353 | |
| 5354 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 5355 | |
| 5356 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 5357 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5358 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 5359 | if (!init) |
| 5360 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5361 | |
| 5362 | init->function_ref = ref; |
| 5363 | LIST_ADDQ(&hlua_init_functions, &init->l); |
| 5364 | return 0; |
| 5365 | } |
| 5366 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5367 | /* This functio is an LUA binding. It permits to register a task |
| 5368 | * executed in parallel of the main HAroxy activity. The task is |
| 5369 | * created and it is set in the HAProxy scheduler. It can be called |
| 5370 | * from the "init" section, "post init" or during the runtime. |
| 5371 | * |
| 5372 | * Lua prototype: |
| 5373 | * |
| 5374 | * <none> core.register_task(<function>) |
| 5375 | */ |
| 5376 | static int hlua_register_task(lua_State *L) |
| 5377 | { |
| 5378 | struct hlua *hlua; |
| 5379 | struct task *task; |
| 5380 | int ref; |
| 5381 | |
| 5382 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 5383 | |
| 5384 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 5385 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5386 | hlua = pool_alloc2(pool2_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5387 | if (!hlua) |
| 5388 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5389 | |
| 5390 | task = task_new(); |
| 5391 | task->context = hlua; |
| 5392 | task->process = hlua_process_task; |
| 5393 | |
| 5394 | if (!hlua_ctx_init(hlua, task)) |
| 5395 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5396 | |
| 5397 | /* Restore the function in the stack. */ |
| 5398 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 5399 | hlua->nargs = 0; |
| 5400 | |
| 5401 | /* Schedule task. */ |
| 5402 | task_schedule(task, now_ms); |
| 5403 | |
| 5404 | return 0; |
| 5405 | } |
| 5406 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5407 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 5408 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 5409 | * resume converters. |
| 5410 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5411 | 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] | 5412 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5413 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5414 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5415 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5416 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5417 | if (!stream) |
| 5418 | return 0; |
| 5419 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5420 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5421 | * Lua context can be not initialized. This behavior |
| 5422 | * permits to save performances because a systematic |
| 5423 | * Lua initialization cause 5% performances loss. |
| 5424 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5425 | if (!stream->hlua) { |
| 5426 | stream->hlua = pool_alloc2(pool2_hlua); |
| 5427 | if (!stream->hlua) { |
| 5428 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 5429 | return 0; |
| 5430 | } |
| 5431 | if (!hlua_ctx_init(stream->hlua, stream->task)) { |
| 5432 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 5433 | return 0; |
| 5434 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5435 | } |
| 5436 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5437 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5438 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5439 | |
| 5440 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5441 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 5442 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 5443 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5444 | else |
| 5445 | error = "critical error"; |
| 5446 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5447 | return 0; |
| 5448 | } |
| 5449 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5450 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5451 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5452 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5453 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5454 | return 0; |
| 5455 | } |
| 5456 | |
| 5457 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5458 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5459 | |
| 5460 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5461 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5462 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5463 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5464 | return 0; |
| 5465 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5466 | hlua_smp2lua(stream->hlua->T, smp); |
| 5467 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5468 | |
| 5469 | /* push keywords in the stack. */ |
| 5470 | if (arg_p) { |
| 5471 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5472 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5473 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5474 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5475 | return 0; |
| 5476 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5477 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 5478 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5479 | } |
| 5480 | } |
| 5481 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5482 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5483 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5484 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5485 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5486 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5487 | } |
| 5488 | |
| 5489 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5490 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5491 | /* finished. */ |
| 5492 | case HLUA_E_OK: |
| 5493 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5494 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 5495 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5496 | return 1; |
| 5497 | |
| 5498 | /* yield. */ |
| 5499 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5500 | 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] | 5501 | return 0; |
| 5502 | |
| 5503 | /* finished with error. */ |
| 5504 | case HLUA_E_ERRMSG: |
| 5505 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5506 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5507 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 5508 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5509 | return 0; |
| 5510 | |
| 5511 | case HLUA_E_ERR: |
| 5512 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5513 | 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] | 5514 | |
| 5515 | default: |
| 5516 | return 0; |
| 5517 | } |
| 5518 | } |
| 5519 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5520 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 5521 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5522 | * resume sample-fetches. This function will be called by the sample |
| 5523 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5524 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5525 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 5526 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5527 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5528 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5529 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5530 | const char *error; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5531 | const struct chunk msg = { .len = 0 }; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5532 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5533 | if (!stream) |
| 5534 | return 0; |
| 5535 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5536 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5537 | * Lua context can be not initialized. This behavior |
| 5538 | * permits to save performances because a systematic |
| 5539 | * Lua initialization cause 5% performances loss. |
| 5540 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5541 | if (!stream->hlua) { |
| 5542 | stream->hlua = pool_alloc2(pool2_hlua); |
| 5543 | if (!stream->hlua) { |
| 5544 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 5545 | return 0; |
| 5546 | } |
| 5547 | if (!hlua_ctx_init(stream->hlua, stream->task)) { |
| 5548 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 5549 | return 0; |
| 5550 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5551 | } |
| 5552 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5553 | consistency_set(stream, smp->opt, &stream->hlua->cons); |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5554 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5555 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5556 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5557 | |
| 5558 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5559 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 5560 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 5561 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5562 | else |
| 5563 | error = "critical error"; |
| 5564 | 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] | 5565 | return 0; |
| 5566 | } |
| 5567 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5568 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5569 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5570 | SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5571 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5572 | return 0; |
| 5573 | } |
| 5574 | |
| 5575 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5576 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5577 | |
| 5578 | /* push arguments in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5579 | if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR, |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5580 | HLUA_TXN_NOTERM)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5581 | SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5582 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5583 | return 0; |
| 5584 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5585 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5586 | |
| 5587 | /* push keywords in the stack. */ |
| 5588 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 5589 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5590 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5591 | SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5592 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5593 | return 0; |
| 5594 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5595 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 5596 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5597 | } |
| 5598 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5599 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5600 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5601 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5602 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5603 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5604 | } |
| 5605 | |
| 5606 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5607 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5608 | /* finished. */ |
| 5609 | case HLUA_E_OK: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5610 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5611 | stream_int_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 5612 | return 0; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5613 | } |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5614 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5615 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 5616 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5617 | |
| 5618 | /* Set the end of execution flag. */ |
| 5619 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 5620 | return 1; |
| 5621 | |
| 5622 | /* yield. */ |
| 5623 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5624 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5625 | stream_int_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5626 | 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] | 5627 | return 0; |
| 5628 | |
| 5629 | /* finished with error. */ |
| 5630 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5631 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5632 | stream_int_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5633 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5634 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5635 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 5636 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5637 | return 0; |
| 5638 | |
| 5639 | case HLUA_E_ERR: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5640 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5641 | stream_int_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5642 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5643 | 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] | 5644 | |
| 5645 | default: |
| 5646 | return 0; |
| 5647 | } |
| 5648 | } |
| 5649 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5650 | /* This function is an LUA binding used for registering |
| 5651 | * "sample-conv" functions. It expects a converter name used |
| 5652 | * in the haproxy configuration file, and an LUA function. |
| 5653 | */ |
| 5654 | __LJMP static int hlua_register_converters(lua_State *L) |
| 5655 | { |
| 5656 | struct sample_conv_kw_list *sck; |
| 5657 | const char *name; |
| 5658 | int ref; |
| 5659 | int len; |
| 5660 | struct hlua_function *fcn; |
| 5661 | |
| 5662 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 5663 | |
| 5664 | /* First argument : converter name. */ |
| 5665 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5666 | |
| 5667 | /* Second argument : lua function. */ |
| 5668 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 5669 | |
| 5670 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5671 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5672 | if (!sck) |
| 5673 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5674 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5675 | if (!fcn) |
| 5676 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5677 | |
| 5678 | /* Fill fcn. */ |
| 5679 | fcn->name = strdup(name); |
| 5680 | if (!fcn->name) |
| 5681 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5682 | fcn->function_ref = ref; |
| 5683 | |
| 5684 | /* List head */ |
| 5685 | sck->list.n = sck->list.p = NULL; |
| 5686 | |
| 5687 | /* converter keyword. */ |
| 5688 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5689 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5690 | if (!sck->kw[0].kw) |
| 5691 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5692 | |
| 5693 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 5694 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 5695 | sck->kw[0].arg_mask = ARG12(0,STR,STR,STR,STR,STR,STR,STR,STR,STR,STR,STR,STR); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5696 | sck->kw[0].val_args = NULL; |
| 5697 | sck->kw[0].in_type = SMP_T_STR; |
| 5698 | sck->kw[0].out_type = SMP_T_STR; |
| 5699 | sck->kw[0].private = fcn; |
| 5700 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5701 | /* Register this new converter */ |
| 5702 | sample_register_convs(sck); |
| 5703 | |
| 5704 | return 0; |
| 5705 | } |
| 5706 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5707 | /* This fucntion is an LUA binding used for registering |
| 5708 | * "sample-fetch" functions. It expects a converter name used |
| 5709 | * in the haproxy configuration file, and an LUA function. |
| 5710 | */ |
| 5711 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 5712 | { |
| 5713 | const char *name; |
| 5714 | int ref; |
| 5715 | int len; |
| 5716 | struct sample_fetch_kw_list *sfk; |
| 5717 | struct hlua_function *fcn; |
| 5718 | |
| 5719 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 5720 | |
| 5721 | /* First argument : sample-fetch name. */ |
| 5722 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5723 | |
| 5724 | /* Second argument : lua function. */ |
| 5725 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 5726 | |
| 5727 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5728 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5729 | if (!sfk) |
| 5730 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5731 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5732 | if (!fcn) |
| 5733 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5734 | |
| 5735 | /* Fill fcn. */ |
| 5736 | fcn->name = strdup(name); |
| 5737 | if (!fcn->name) |
| 5738 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5739 | fcn->function_ref = ref; |
| 5740 | |
| 5741 | /* List head */ |
| 5742 | sfk->list.n = sfk->list.p = NULL; |
| 5743 | |
| 5744 | /* sample-fetch keyword. */ |
| 5745 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5746 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5747 | if (!sfk->kw[0].kw) |
| 5748 | return luaL_error(L, "lua out of memory error."); |
| 5749 | |
| 5750 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 5751 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 5752 | sfk->kw[0].arg_mask = ARG12(0,STR,STR,STR,STR,STR,STR,STR,STR,STR,STR,STR,STR); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5753 | sfk->kw[0].val_args = NULL; |
| 5754 | sfk->kw[0].out_type = SMP_T_STR; |
| 5755 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 5756 | sfk->kw[0].val = 0; |
| 5757 | sfk->kw[0].private = fcn; |
| 5758 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5759 | /* Register this new fetch. */ |
| 5760 | sample_register_fetches(sfk); |
| 5761 | |
| 5762 | return 0; |
| 5763 | } |
| 5764 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5765 | /* This function is a wrapper to execute each LUA function declared |
| 5766 | * as an action wrapper during the initialisation period. This function |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5767 | * return ACT_RET_CONT if the processing is finished (with or without |
| 5768 | * error) and return ACT_RET_YIELD if the function must be called again |
| 5769 | * because the LUA returns a yield. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5770 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5771 | 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] | 5772 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5773 | { |
| 5774 | char **arg; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5775 | unsigned int analyzer; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 5776 | int dir; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5777 | const char *error; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5778 | const struct chunk msg = { .len = 0 }; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5779 | |
| 5780 | switch (rule->from) { |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 5781 | case ACT_F_TCP_REQ_CNT: analyzer = AN_REQ_INSPECT_FE ; dir = SMP_OPT_DIR_REQ; break; |
| 5782 | case ACT_F_TCP_RES_CNT: analyzer = AN_RES_INSPECT ; dir = SMP_OPT_DIR_RES; break; |
| 5783 | case ACT_F_HTTP_REQ: analyzer = AN_REQ_HTTP_PROCESS_FE; dir = SMP_OPT_DIR_REQ; break; |
| 5784 | 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] | 5785 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5786 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5787 | return ACT_RET_CONT; |
| 5788 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5789 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5790 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5791 | * Lua context can be not initialized. This behavior |
| 5792 | * permits to save performances because a systematic |
| 5793 | * Lua initialization cause 5% performances loss. |
| 5794 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5795 | if (!s->hlua) { |
| 5796 | s->hlua = pool_alloc2(pool2_hlua); |
| 5797 | if (!s->hlua) { |
| 5798 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 5799 | rule->arg.hlua_rule->fcn.name); |
| 5800 | return ACT_RET_CONT; |
| 5801 | } |
| 5802 | if (!hlua_ctx_init(s->hlua, s->task)) { |
| 5803 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 5804 | rule->arg.hlua_rule->fcn.name); |
| 5805 | return ACT_RET_CONT; |
| 5806 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5807 | } |
| 5808 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5809 | consistency_set(s, dir, &s->hlua->cons); |
| 5810 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5811 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5812 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5813 | |
| 5814 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5815 | if (!SET_SAFE_LJMP(s->hlua->T)) { |
| 5816 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 5817 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5818 | else |
| 5819 | error = "critical error"; |
| 5820 | SEND_ERR(px, "Lua function '%s': %s.\n", |
| 5821 | rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5822 | return ACT_RET_CONT; |
| 5823 | } |
| 5824 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5825 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5826 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5827 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5828 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5829 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 5830 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5831 | } |
| 5832 | |
| 5833 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5834 | 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] | 5835 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5836 | /* Create and and push object stream in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5837 | if (!hlua_txn_new(s->hlua->T, s, px, dir, 0)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5838 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5839 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5840 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 5841 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5842 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5843 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5844 | |
| 5845 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5846 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5847 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5848 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5849 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5850 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 5851 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5852 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5853 | lua_pushstring(s->hlua->T, *arg); |
| 5854 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5855 | } |
| 5856 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5857 | /* Now the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5858 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5859 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5860 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5861 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5862 | } |
| 5863 | |
| 5864 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5865 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_FLAG_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5866 | /* finished. */ |
| 5867 | case HLUA_E_OK: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5868 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5869 | stream_int_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 5870 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5871 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5872 | if (s->hlua->flags & HLUA_STOP) |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 5873 | return ACT_RET_STOP; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 5874 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5875 | |
| 5876 | /* yield. */ |
| 5877 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 5878 | /* Set timeout in the required channel. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5879 | if (s->hlua->wake_time != TICK_ETERNITY) { |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 5880 | if (analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE)) |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5881 | s->req.analyse_exp = s->hlua->wake_time; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 5882 | else if (analyzer & (AN_RES_INSPECT|AN_RES_HTTP_PROCESS_BE)) |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5883 | s->res.analyse_exp = s->hlua->wake_time; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 5884 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5885 | /* Some actions can be wake up when a "write" event |
| 5886 | * is detected on a response channel. This is useful |
| 5887 | * only for actions targetted on the requests. |
| 5888 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5889 | if (HLUA_IS_WAKERESWR(s->hlua)) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5890 | s->res.flags |= CF_WAKE_WRITE; |
Willy Tarreau | 76bd97f | 2015-03-10 17:16:10 +0100 | [diff] [blame] | 5891 | if ((analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE))) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5892 | s->res.analysers |= analyzer; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5893 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5894 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5895 | s->req.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5896 | /* We can quit the fcuntion without consistency check |
| 5897 | * because HAProxy is not able to manipulate data, it |
| 5898 | * is only allowed to call me again. */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 5899 | return ACT_RET_YIELD; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5900 | |
| 5901 | /* finished with error. */ |
| 5902 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5903 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5904 | stream_int_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 5905 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5906 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5907 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5908 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5909 | rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1)); |
| 5910 | lua_pop(s->hlua->T, 1); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 5911 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5912 | |
| 5913 | case HLUA_E_ERR: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5914 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5915 | stream_int_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 5916 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5917 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5918 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5919 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5920 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5921 | |
| 5922 | default: |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 5923 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5924 | } |
| 5925 | } |
| 5926 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 5927 | struct task *hlua_applet_wakeup(struct task *t) |
| 5928 | { |
| 5929 | struct appctx *ctx = t->context; |
| 5930 | struct stream_interface *si = ctx->owner; |
| 5931 | |
| 5932 | /* If the applet is wake up without any expected work, the sheduler |
| 5933 | * remove it from the run queue. This flag indicate that the applet |
| 5934 | * is waiting for write. If the buffer is full, the main processing |
| 5935 | * will send some data and after call the applet, otherwise it call |
| 5936 | * the applet ASAP. |
| 5937 | */ |
| 5938 | si_applet_cant_put(si); |
| 5939 | appctx_wakeup(ctx); |
| 5940 | return NULL; |
| 5941 | } |
| 5942 | |
| 5943 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 5944 | { |
| 5945 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 5946 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 5947 | struct task *task; |
| 5948 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5949 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 5950 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 5951 | hlua = pool_alloc2(pool2_hlua); |
| 5952 | if (!hlua) { |
| 5953 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 5954 | ctx->rule->arg.hlua_rule->fcn.name); |
| 5955 | return 0; |
| 5956 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 5957 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 5958 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 5959 | ctx->ctx.hlua_apptcp.flags = 0; |
| 5960 | |
| 5961 | /* Create task used by signal to wakeup applets. */ |
| 5962 | task = task_new(); |
| 5963 | if (!task) { |
| 5964 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 5965 | ctx->rule->arg.hlua_rule->fcn.name); |
| 5966 | return 0; |
| 5967 | } |
| 5968 | task->nice = 0; |
| 5969 | task->context = ctx; |
| 5970 | task->process = hlua_applet_wakeup; |
| 5971 | ctx->ctx.hlua_apptcp.task = task; |
| 5972 | |
| 5973 | /* In the execution wrappers linked with a stream, the |
| 5974 | * Lua context can be not initialized. This behavior |
| 5975 | * permits to save performances because a systematic |
| 5976 | * Lua initialization cause 5% performances loss. |
| 5977 | */ |
| 5978 | if (!hlua_ctx_init(hlua, task)) { |
| 5979 | SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n", |
| 5980 | ctx->rule->arg.hlua_rule->fcn.name); |
| 5981 | return 0; |
| 5982 | } |
| 5983 | |
| 5984 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 5985 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 5986 | |
| 5987 | /* The following Lua calls can fail. */ |
| 5988 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5989 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 5990 | error = lua_tostring(hlua->T, -1); |
| 5991 | else |
| 5992 | error = "critical error"; |
| 5993 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 5994 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 5995 | RESET_SAFE_LJMP(hlua->T); |
| 5996 | return 0; |
| 5997 | } |
| 5998 | |
| 5999 | /* Check stack available size. */ |
| 6000 | if (!lua_checkstack(hlua->T, 1)) { |
| 6001 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6002 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6003 | RESET_SAFE_LJMP(hlua->T); |
| 6004 | return 0; |
| 6005 | } |
| 6006 | |
| 6007 | /* Restore the function in the stack. */ |
| 6008 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 6009 | |
| 6010 | /* Create and and push object stream in the stack. */ |
| 6011 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 6012 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6013 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6014 | RESET_SAFE_LJMP(hlua->T); |
| 6015 | return 0; |
| 6016 | } |
| 6017 | hlua->nargs = 1; |
| 6018 | |
| 6019 | /* push keywords in the stack. */ |
| 6020 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 6021 | if (!lua_checkstack(hlua->T, 1)) { |
| 6022 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6023 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6024 | RESET_SAFE_LJMP(hlua->T); |
| 6025 | return 0; |
| 6026 | } |
| 6027 | lua_pushstring(hlua->T, *arg); |
| 6028 | hlua->nargs++; |
| 6029 | } |
| 6030 | |
| 6031 | RESET_SAFE_LJMP(hlua->T); |
| 6032 | |
| 6033 | /* Wakeup the applet ASAP. */ |
| 6034 | si_applet_cant_get(si); |
| 6035 | si_applet_cant_put(si); |
| 6036 | |
| 6037 | return 1; |
| 6038 | } |
| 6039 | |
| 6040 | static void hlua_applet_tcp_fct(struct appctx *ctx) |
| 6041 | { |
| 6042 | struct stream_interface *si = ctx->owner; |
| 6043 | struct stream *strm = si_strm(si); |
| 6044 | struct channel *res = si_ic(si); |
| 6045 | struct act_rule *rule = ctx->rule; |
| 6046 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6047 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6048 | |
| 6049 | /* The applet execution is already done. */ |
| 6050 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) |
| 6051 | return; |
| 6052 | |
| 6053 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 6054 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 6055 | return; |
| 6056 | |
| 6057 | /* Execute the function. */ |
| 6058 | switch (hlua_ctx_resume(hlua, 1)) { |
| 6059 | /* finished. */ |
| 6060 | case HLUA_E_OK: |
| 6061 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 6062 | |
| 6063 | /* log time */ |
| 6064 | strm->logs.tv_request = now; |
| 6065 | |
| 6066 | /* eat the whole request */ |
| 6067 | bo_skip(si_oc(si), si_ob(si)->o); |
| 6068 | res->flags |= CF_READ_NULL; |
| 6069 | si_shutr(si); |
| 6070 | return; |
| 6071 | |
| 6072 | /* yield. */ |
| 6073 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 6074 | if (hlua->wake_time != TICK_ETERNITY) |
| 6075 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6076 | return; |
| 6077 | |
| 6078 | /* finished with error. */ |
| 6079 | case HLUA_E_ERRMSG: |
| 6080 | /* Display log. */ |
| 6081 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 6082 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 6083 | lua_pop(hlua->T, 1); |
| 6084 | goto error; |
| 6085 | |
| 6086 | case HLUA_E_ERR: |
| 6087 | /* Display log. */ |
| 6088 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
| 6089 | rule->arg.hlua_rule->fcn.name); |
| 6090 | goto error; |
| 6091 | |
| 6092 | default: |
| 6093 | goto error; |
| 6094 | } |
| 6095 | |
| 6096 | error: |
| 6097 | |
| 6098 | /* For all other cases, just close the stream. */ |
| 6099 | si_shutw(si); |
| 6100 | si_shutr(si); |
| 6101 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 6102 | } |
| 6103 | |
| 6104 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 6105 | { |
| 6106 | task_free(ctx->ctx.hlua_apptcp.task); |
| 6107 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6108 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6109 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6110 | } |
| 6111 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6112 | /* The function returns 1 if the initialisation is complete, 0 if |
| 6113 | * an errors occurs and -1 if more data are required for initializing |
| 6114 | * the applet. |
| 6115 | */ |
| 6116 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6117 | { |
| 6118 | struct stream_interface *si = ctx->owner; |
| 6119 | struct channel *req = si_oc(si); |
| 6120 | struct http_msg *msg; |
| 6121 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6122 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6123 | char **arg; |
| 6124 | struct hdr_ctx hdr; |
| 6125 | struct task *task; |
| 6126 | 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] | 6127 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6128 | |
| 6129 | /* Wait for a full HTTP request. */ |
| 6130 | if (!smp_prefetch_http(px, strm, 0, NULL, &smp, 0)) { |
| 6131 | if (smp.flags & SMP_F_MAY_CHANGE) |
| 6132 | return -1; |
| 6133 | return 0; |
| 6134 | } |
| 6135 | txn = strm->txn; |
| 6136 | msg = &txn->req; |
| 6137 | |
Willy Tarreau | 0078bfc | 2015-10-07 20:20:28 +0200 | [diff] [blame] | 6138 | /* We want two things in HTTP mode : |
| 6139 | * - enforce server-close mode if we were in keep-alive, so that the |
| 6140 | * applet is released after each response ; |
| 6141 | * - enable request body transfer to the applet in order to resync |
| 6142 | * with the response body. |
| 6143 | */ |
| 6144 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) |
| 6145 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | 0078bfc | 2015-10-07 20:20:28 +0200 | [diff] [blame] | 6146 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6147 | hlua = pool_alloc2(pool2_hlua); |
| 6148 | if (!hlua) { |
| 6149 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 6150 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6151 | return 0; |
| 6152 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6153 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6154 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6155 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 6156 | ctx->ctx.hlua_apphttp.flags = 0; |
| 6157 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 6158 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 6159 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 6160 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6161 | /* Create task used by signal to wakeup applets. */ |
| 6162 | task = task_new(); |
| 6163 | if (!task) { |
| 6164 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 6165 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6166 | return 0; |
| 6167 | } |
| 6168 | task->nice = 0; |
| 6169 | task->context = ctx; |
| 6170 | task->process = hlua_applet_wakeup; |
| 6171 | ctx->ctx.hlua_apphttp.task = task; |
| 6172 | |
| 6173 | /* In the execution wrappers linked with a stream, the |
| 6174 | * Lua context can be not initialized. This behavior |
| 6175 | * permits to save performances because a systematic |
| 6176 | * Lua initialization cause 5% performances loss. |
| 6177 | */ |
| 6178 | if (!hlua_ctx_init(hlua, task)) { |
| 6179 | SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n", |
| 6180 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6181 | return 0; |
| 6182 | } |
| 6183 | |
| 6184 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6185 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6186 | |
| 6187 | /* The following Lua calls can fail. */ |
| 6188 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6189 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6190 | error = lua_tostring(hlua->T, -1); |
| 6191 | else |
| 6192 | error = "critical error"; |
| 6193 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 6194 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6195 | return 0; |
| 6196 | } |
| 6197 | |
| 6198 | /* Check stack available size. */ |
| 6199 | if (!lua_checkstack(hlua->T, 1)) { |
| 6200 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6201 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6202 | RESET_SAFE_LJMP(hlua->T); |
| 6203 | return 0; |
| 6204 | } |
| 6205 | |
| 6206 | /* Restore the function in the stack. */ |
| 6207 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 6208 | |
| 6209 | /* Create and and push object stream in the stack. */ |
| 6210 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 6211 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6212 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6213 | RESET_SAFE_LJMP(hlua->T); |
| 6214 | return 0; |
| 6215 | } |
| 6216 | hlua->nargs = 1; |
| 6217 | |
| 6218 | /* Look for a 100-continue expected. */ |
| 6219 | if (msg->flags & HTTP_MSGF_VER_11) { |
| 6220 | hdr.idx = 0; |
| 6221 | if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &hdr) && |
| 6222 | unlikely(hdr.vlen == 12 && strncasecmp(hdr.line+hdr.val, "100-continue", 12) == 0)) |
| 6223 | ctx->ctx.hlua_apphttp.flags |= APPLET_100C; |
| 6224 | } |
| 6225 | |
| 6226 | /* push keywords in the stack. */ |
| 6227 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 6228 | if (!lua_checkstack(hlua->T, 1)) { |
| 6229 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6230 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6231 | RESET_SAFE_LJMP(hlua->T); |
| 6232 | return 0; |
| 6233 | } |
| 6234 | lua_pushstring(hlua->T, *arg); |
| 6235 | hlua->nargs++; |
| 6236 | } |
| 6237 | |
| 6238 | RESET_SAFE_LJMP(hlua->T); |
| 6239 | |
| 6240 | /* Wakeup the applet when data is ready for read. */ |
| 6241 | si_applet_cant_get(si); |
| 6242 | |
| 6243 | return 1; |
| 6244 | } |
| 6245 | |
| 6246 | static void hlua_applet_http_fct(struct appctx *ctx) |
| 6247 | { |
| 6248 | struct stream_interface *si = ctx->owner; |
| 6249 | struct stream *strm = si_strm(si); |
| 6250 | struct channel *res = si_ic(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6251 | struct act_rule *rule = ctx->rule; |
| 6252 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6253 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6254 | char *blk1; |
| 6255 | int len1; |
| 6256 | char *blk2; |
| 6257 | int len2; |
| 6258 | int ret; |
| 6259 | |
| 6260 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 6261 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 6262 | return; |
| 6263 | |
| 6264 | /* Set the currently running flag. */ |
| 6265 | if (!HLUA_IS_RUNNING(hlua) && |
| 6266 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 6267 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6268 | /* Wait for full HTTP analysys. */ |
| 6269 | if (unlikely(strm->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 6270 | si_applet_cant_get(si); |
| 6271 | return; |
| 6272 | } |
| 6273 | |
| 6274 | /* Store the max amount of bytes that we can read. */ |
| 6275 | ctx->ctx.hlua_apphttp.left_bytes = strm->txn->req.body_len; |
| 6276 | |
| 6277 | /* We need to flush the request header. This left the body |
| 6278 | * for the Lua. |
| 6279 | */ |
| 6280 | |
| 6281 | /* Read the maximum amount of data avalaible. */ |
| 6282 | ret = bo_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
| 6283 | if (ret == -1) |
| 6284 | return; |
| 6285 | |
| 6286 | /* No data available, ask for more data. */ |
| 6287 | if (ret == 1) |
| 6288 | len2 = 0; |
| 6289 | if (ret == 0) |
| 6290 | len1 = 0; |
| 6291 | if (len1 + len2 < strm->txn->req.eoh + 2) { |
| 6292 | si_applet_cant_get(si); |
| 6293 | return; |
| 6294 | } |
| 6295 | |
| 6296 | /* skip the requests bytes. */ |
| 6297 | bo_skip(si_oc(si), strm->txn->req.eoh + 2); |
| 6298 | } |
| 6299 | |
| 6300 | /* Executes The applet if it is not done. */ |
| 6301 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 6302 | |
| 6303 | /* Execute the function. */ |
| 6304 | switch (hlua_ctx_resume(hlua, 1)) { |
| 6305 | /* finished. */ |
| 6306 | case HLUA_E_OK: |
| 6307 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 6308 | break; |
| 6309 | |
| 6310 | /* yield. */ |
| 6311 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 6312 | if (hlua->wake_time != TICK_ETERNITY) |
| 6313 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6314 | return; |
| 6315 | |
| 6316 | /* finished with error. */ |
| 6317 | case HLUA_E_ERRMSG: |
| 6318 | /* Display log. */ |
| 6319 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 6320 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 6321 | lua_pop(hlua->T, 1); |
| 6322 | goto error; |
| 6323 | |
| 6324 | case HLUA_E_ERR: |
| 6325 | /* Display log. */ |
| 6326 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
| 6327 | rule->arg.hlua_rule->fcn.name); |
| 6328 | goto error; |
| 6329 | |
| 6330 | default: |
| 6331 | goto error; |
| 6332 | } |
| 6333 | } |
| 6334 | |
| 6335 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
| 6336 | |
| 6337 | /* We must send the final chunk. */ |
| 6338 | if (ctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED && |
| 6339 | !(ctx->ctx.hlua_apphttp.flags & APPLET_LAST_CHK)) { |
| 6340 | |
| 6341 | /* sent last chunk at once. */ |
| 6342 | ret = bi_putblk(res, "0\r\n\r\n", 5); |
| 6343 | |
| 6344 | /* critical error. */ |
| 6345 | if (ret == -2 || ret == -3) { |
| 6346 | SEND_ERR(px, "Lua applet http '%s'cannont send last chunk.\n", |
| 6347 | rule->arg.hlua_rule->fcn.name); |
| 6348 | goto error; |
| 6349 | } |
| 6350 | |
| 6351 | /* no enough space error. */ |
| 6352 | if (ret == -1) { |
| 6353 | si_applet_cant_put(si); |
| 6354 | return; |
| 6355 | } |
| 6356 | |
| 6357 | /* set the last chunk sent. */ |
| 6358 | ctx->ctx.hlua_apphttp.flags |= APPLET_LAST_CHK; |
| 6359 | } |
| 6360 | |
| 6361 | /* close the connection. */ |
| 6362 | |
| 6363 | /* status / log */ |
| 6364 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 6365 | strm->logs.tv_request = now; |
| 6366 | |
| 6367 | /* eat the whole request */ |
| 6368 | bo_skip(si_oc(si), si_ob(si)->o); |
| 6369 | res->flags |= CF_READ_NULL; |
| 6370 | si_shutr(si); |
| 6371 | |
| 6372 | return; |
| 6373 | } |
| 6374 | |
| 6375 | error: |
| 6376 | |
| 6377 | /* If we are in HTTP mode, and we are not send any |
| 6378 | * data, return a 500 server error in best effort: |
| 6379 | * if there are no room avalaible in the buffer, |
| 6380 | * just close the connection. |
| 6381 | */ |
| 6382 | bi_putblk(res, error_500, strlen(error_500)); |
| 6383 | if (!(strm->flags & SF_ERR_MASK)) |
| 6384 | strm->flags |= SF_ERR_RESOURCE; |
| 6385 | si_shutw(si); |
| 6386 | si_shutr(si); |
| 6387 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 6388 | } |
| 6389 | |
| 6390 | static void hlua_applet_http_release(struct appctx *ctx) |
| 6391 | { |
| 6392 | task_free(ctx->ctx.hlua_apphttp.task); |
| 6393 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6394 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6395 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6396 | } |
| 6397 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6398 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
| 6399 | * succes case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6400 | * |
| 6401 | * This function can fail with an abort() due to an Lua critical error. |
| 6402 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 6403 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6404 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6405 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 6406 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6407 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6408 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6409 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6410 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6411 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6412 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6413 | if (!rule->arg.hlua_rule) { |
| 6414 | memprintf(err, "out of memory error"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 6415 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6416 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6417 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6418 | /* Memory for arguments. */ |
| 6419 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *)); |
| 6420 | if (!rule->arg.hlua_rule->args) { |
| 6421 | memprintf(err, "out of memory error"); |
| 6422 | return ACT_RET_PRS_ERR; |
| 6423 | } |
| 6424 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6425 | /* Reference the Lua function and store the reference. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6426 | rule->arg.hlua_rule->fcn = *fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6427 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6428 | /* Expect some arguments */ |
| 6429 | for (i = 0; i < fcn->nargs; i++) { |
| 6430 | if (*args[i+1] == '\0') { |
| 6431 | memprintf(err, "expect %d arguments", fcn->nargs); |
| 6432 | return ACT_RET_PRS_ERR; |
| 6433 | } |
| 6434 | rule->arg.hlua_rule->args[i] = strdup(args[i + 1]); |
| 6435 | if (!rule->arg.hlua_rule->args[i]) { |
| 6436 | memprintf(err, "out of memory error"); |
| 6437 | return ACT_RET_PRS_ERR; |
| 6438 | } |
| 6439 | (*cur_arg)++; |
| 6440 | } |
| 6441 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6442 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 6443 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6444 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 6445 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6446 | } |
| 6447 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6448 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 6449 | struct act_rule *rule, char **err) |
| 6450 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6451 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6452 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 6453 | /* HTTP applets are forbidden in tcp-request rules. |
| 6454 | * HTTP applet request requires everything initilized by |
| 6455 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
| 6456 | * The applet will be immediately initilized, but its before |
| 6457 | * the call of this analyzer. |
| 6458 | */ |
| 6459 | if (rule->from != ACT_F_HTTP_REQ) { |
| 6460 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 6461 | return ACT_RET_PRS_ERR; |
| 6462 | } |
| 6463 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6464 | /* Memory for the rule. */ |
| 6465 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 6466 | if (!rule->arg.hlua_rule) { |
| 6467 | memprintf(err, "out of memory error"); |
| 6468 | return ACT_RET_PRS_ERR; |
| 6469 | } |
| 6470 | |
| 6471 | /* Reference the Lua function and store the reference. */ |
| 6472 | rule->arg.hlua_rule->fcn = *fcn; |
| 6473 | |
| 6474 | /* TODO: later accept arguments. */ |
| 6475 | rule->arg.hlua_rule->args = NULL; |
| 6476 | |
| 6477 | /* Add applet pointer in the rule. */ |
| 6478 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 6479 | rule->applet.name = fcn->name; |
| 6480 | rule->applet.init = hlua_applet_http_init; |
| 6481 | rule->applet.fct = hlua_applet_http_fct; |
| 6482 | rule->applet.release = hlua_applet_http_release; |
| 6483 | rule->applet.timeout = hlua_timeout_applet; |
| 6484 | |
| 6485 | return ACT_RET_PRS_OK; |
| 6486 | } |
| 6487 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6488 | /* This function is an LUA binding used for registering |
| 6489 | * "sample-conv" functions. It expects a converter name used |
| 6490 | * in the haproxy configuration file, and an LUA function. |
| 6491 | */ |
| 6492 | __LJMP static int hlua_register_action(lua_State *L) |
| 6493 | { |
| 6494 | struct action_kw_list *akl; |
| 6495 | const char *name; |
| 6496 | int ref; |
| 6497 | int len; |
| 6498 | struct hlua_function *fcn; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6499 | int nargs; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6500 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6501 | /* Initialise the number of expected arguments at 0. */ |
| 6502 | nargs = 0; |
| 6503 | |
| 6504 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 6505 | WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6506 | |
| 6507 | /* First argument : converter name. */ |
| 6508 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6509 | |
| 6510 | /* Second argument : environment. */ |
| 6511 | if (lua_type(L, 2) != LUA_TTABLE) |
| 6512 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 6513 | |
| 6514 | /* Third argument : lua function. */ |
| 6515 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 6516 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6517 | /* Fouth argument : number of mandatories arguments expected on the configuration line. */ |
| 6518 | if (lua_gettop(L) >= 4) |
| 6519 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 6520 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6521 | /* browse the second argulent as an array. */ |
| 6522 | lua_pushnil(L); |
| 6523 | while (lua_next(L, 2) != 0) { |
| 6524 | if (lua_type(L, -1) != LUA_TSTRING) |
| 6525 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 6526 | |
| 6527 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 6528 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6529 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6530 | if (!akl) |
| 6531 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6532 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6533 | if (!fcn) |
| 6534 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6535 | |
| 6536 | /* Fill fcn. */ |
| 6537 | fcn->name = strdup(name); |
| 6538 | if (!fcn->name) |
| 6539 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6540 | fcn->function_ref = ref; |
| 6541 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6542 | /* Set the expected number od arguments. */ |
| 6543 | fcn->nargs = nargs; |
| 6544 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6545 | /* List head */ |
| 6546 | akl->list.n = akl->list.p = NULL; |
| 6547 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6548 | /* action keyword. */ |
| 6549 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6550 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6551 | if (!akl->kw[0].kw) |
| 6552 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6553 | |
| 6554 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 6555 | |
| 6556 | akl->kw[0].match_pfx = 0; |
| 6557 | akl->kw[0].private = fcn; |
| 6558 | akl->kw[0].parse = action_register_lua; |
| 6559 | |
| 6560 | /* select the action registering point. */ |
| 6561 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 6562 | tcp_req_cont_keywords_register(akl); |
| 6563 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 6564 | tcp_res_cont_keywords_register(akl); |
| 6565 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 6566 | http_req_keywords_register(akl); |
| 6567 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 6568 | http_res_keywords_register(akl); |
| 6569 | else |
| 6570 | WILL_LJMP(luaL_error(L, "lua action environment '%s' is unknown. " |
| 6571 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 6572 | "are expected.", lua_tostring(L, -1))); |
| 6573 | |
| 6574 | /* pop the environment string. */ |
| 6575 | lua_pop(L, 1); |
| 6576 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6577 | return ACT_RET_PRS_OK; |
| 6578 | } |
| 6579 | |
| 6580 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 6581 | struct act_rule *rule, char **err) |
| 6582 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6583 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6584 | |
| 6585 | /* Memory for the rule. */ |
| 6586 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 6587 | if (!rule->arg.hlua_rule) { |
| 6588 | memprintf(err, "out of memory error"); |
| 6589 | return ACT_RET_PRS_ERR; |
| 6590 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6591 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6592 | /* Reference the Lua function and store the reference. */ |
| 6593 | rule->arg.hlua_rule->fcn = *fcn; |
| 6594 | |
| 6595 | /* TODO: later accept arguments. */ |
| 6596 | rule->arg.hlua_rule->args = NULL; |
| 6597 | |
| 6598 | /* Add applet pointer in the rule. */ |
| 6599 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 6600 | rule->applet.name = fcn->name; |
| 6601 | rule->applet.init = hlua_applet_tcp_init; |
| 6602 | rule->applet.fct = hlua_applet_tcp_fct; |
| 6603 | rule->applet.release = hlua_applet_tcp_release; |
| 6604 | rule->applet.timeout = hlua_timeout_applet; |
| 6605 | |
| 6606 | return 0; |
| 6607 | } |
| 6608 | |
| 6609 | /* This function is an LUA binding used for registering |
| 6610 | * "sample-conv" functions. It expects a converter name used |
| 6611 | * in the haproxy configuration file, and an LUA function. |
| 6612 | */ |
| 6613 | __LJMP static int hlua_register_service(lua_State *L) |
| 6614 | { |
| 6615 | struct action_kw_list *akl; |
| 6616 | const char *name; |
| 6617 | const char *env; |
| 6618 | int ref; |
| 6619 | int len; |
| 6620 | struct hlua_function *fcn; |
| 6621 | |
| 6622 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 6623 | |
| 6624 | /* First argument : converter name. */ |
| 6625 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6626 | |
| 6627 | /* Second argument : environment. */ |
| 6628 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6629 | |
| 6630 | /* Third argument : lua function. */ |
| 6631 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 6632 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6633 | /* Allocate and fill the sample fetch keyword struct. */ |
| 6634 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 6635 | if (!akl) |
| 6636 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6637 | fcn = calloc(1, sizeof(*fcn)); |
| 6638 | if (!fcn) |
| 6639 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6640 | |
| 6641 | /* Fill fcn. */ |
| 6642 | len = strlen("<lua.>") + strlen(name) + 1; |
| 6643 | fcn->name = calloc(1, len); |
| 6644 | if (!fcn->name) |
| 6645 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6646 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
| 6647 | fcn->function_ref = ref; |
| 6648 | |
| 6649 | /* List head */ |
| 6650 | akl->list.n = akl->list.p = NULL; |
| 6651 | |
| 6652 | /* converter keyword. */ |
| 6653 | len = strlen("lua.") + strlen(name) + 1; |
| 6654 | akl->kw[0].kw = calloc(1, len); |
| 6655 | if (!akl->kw[0].kw) |
| 6656 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6657 | |
| 6658 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 6659 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 6660 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6661 | if (strcmp(env, "tcp") == 0) |
| 6662 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6663 | else if (strcmp(env, "http") == 0) |
| 6664 | akl->kw[0].parse = action_register_service_http; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6665 | else |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6666 | WILL_LJMP(luaL_error(L, "lua service environment '%s' is unknown. " |
| 6667 | "'tcp' or 'http' are expected.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6668 | |
| 6669 | akl->kw[0].match_pfx = 0; |
| 6670 | akl->kw[0].private = fcn; |
| 6671 | |
| 6672 | /* End of array. */ |
| 6673 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 6674 | |
| 6675 | /* Register this new converter */ |
| 6676 | service_keywords_register(akl); |
| 6677 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6678 | return 0; |
| 6679 | } |
| 6680 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6681 | /* This function initialises Lua cli handler. It copies the |
| 6682 | * arguments in the Lua stack and create channel IO objects. |
| 6683 | */ |
| 6684 | static int hlua_cli_parse_fct(char **args, struct appctx *appctx, void *private) |
| 6685 | { |
| 6686 | struct hlua *hlua; |
| 6687 | struct hlua_function *fcn; |
| 6688 | int i; |
| 6689 | const char *error; |
| 6690 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6691 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6692 | appctx->ctx.hlua_cli.fcn = private; |
| 6693 | |
| 6694 | hlua = pool_alloc2(pool2_hlua); |
| 6695 | if (!hlua) { |
| 6696 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 6697 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6698 | } |
| 6699 | HLUA_INIT(hlua); |
| 6700 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6701 | |
| 6702 | /* Create task used by signal to wakeup applets. |
| 6703 | * We use the same wakeup fonction than the Lua applet_tcp and |
| 6704 | * applet_http. It is absolutely compatible. |
| 6705 | */ |
| 6706 | appctx->ctx.hlua_cli.task = task_new(); |
| 6707 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 6708 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 6709 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6710 | } |
| 6711 | appctx->ctx.hlua_cli.task->nice = 0; |
| 6712 | appctx->ctx.hlua_cli.task->context = appctx; |
| 6713 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 6714 | |
| 6715 | /* Initialises the Lua context */ |
| 6716 | if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task)) { |
| 6717 | SEND_ERR(NULL, "Lua cli '%s': can't initialize Lua context.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 6718 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6719 | } |
| 6720 | |
| 6721 | /* The following Lua calls can fail. */ |
| 6722 | if (!SET_SAFE_LJMP(hlua->T)) { |
| 6723 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6724 | error = lua_tostring(hlua->T, -1); |
| 6725 | else |
| 6726 | error = "critical error"; |
| 6727 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 6728 | goto error; |
| 6729 | } |
| 6730 | |
| 6731 | /* Check stack available size. */ |
| 6732 | if (!lua_checkstack(hlua->T, 2)) { |
| 6733 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 6734 | goto error; |
| 6735 | } |
| 6736 | |
| 6737 | /* Restore the function in the stack. */ |
| 6738 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
| 6739 | |
| 6740 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 6741 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6742 | */ |
| 6743 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 6744 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 6745 | goto error; |
| 6746 | } |
| 6747 | hlua->nargs = 1; |
| 6748 | |
| 6749 | /* push keywords in the stack. */ |
| 6750 | for (i = 0; *args[i]; i++) { |
| 6751 | /* Check stack available size. */ |
| 6752 | if (!lua_checkstack(hlua->T, 1)) { |
| 6753 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 6754 | goto error; |
| 6755 | } |
| 6756 | lua_pushstring(hlua->T, args[i]); |
| 6757 | hlua->nargs++; |
| 6758 | } |
| 6759 | |
| 6760 | /* We must initialize the execution timeouts. */ |
| 6761 | hlua->max_time = hlua_timeout_session; |
| 6762 | |
| 6763 | /* At this point the execution is safe. */ |
| 6764 | RESET_SAFE_LJMP(hlua->T); |
| 6765 | |
| 6766 | /* It's ok */ |
| 6767 | return 0; |
| 6768 | |
| 6769 | /* It's not ok. */ |
| 6770 | error: |
| 6771 | RESET_SAFE_LJMP(hlua->T); |
| 6772 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 6773 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6774 | return 1; |
| 6775 | } |
| 6776 | |
| 6777 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 6778 | { |
| 6779 | struct hlua *hlua; |
| 6780 | struct stream_interface *si; |
| 6781 | struct hlua_function *fcn; |
| 6782 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6783 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6784 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 6785 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6786 | |
| 6787 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 6788 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 6789 | return 1; |
| 6790 | |
| 6791 | /* Execute the function. */ |
| 6792 | switch (hlua_ctx_resume(hlua, 1)) { |
| 6793 | |
| 6794 | /* finished. */ |
| 6795 | case HLUA_E_OK: |
| 6796 | return 1; |
| 6797 | |
| 6798 | /* yield. */ |
| 6799 | case HLUA_E_AGAIN: |
| 6800 | /* We want write. */ |
| 6801 | if (HLUA_IS_WAKERESWR(hlua)) |
| 6802 | si_applet_cant_put(si); |
| 6803 | /* Set the timeout. */ |
| 6804 | if (hlua->wake_time != TICK_ETERNITY) |
| 6805 | task_schedule(hlua->task, hlua->wake_time); |
| 6806 | return 0; |
| 6807 | |
| 6808 | /* finished with error. */ |
| 6809 | case HLUA_E_ERRMSG: |
| 6810 | /* Display log. */ |
| 6811 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 6812 | fcn->name, lua_tostring(hlua->T, -1)); |
| 6813 | lua_pop(hlua->T, 1); |
| 6814 | return 1; |
| 6815 | |
| 6816 | case HLUA_E_ERR: |
| 6817 | /* Display log. */ |
| 6818 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 6819 | fcn->name); |
| 6820 | return 1; |
| 6821 | |
| 6822 | default: |
| 6823 | return 1; |
| 6824 | } |
| 6825 | |
| 6826 | return 1; |
| 6827 | } |
| 6828 | |
| 6829 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 6830 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6831 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6832 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6833 | } |
| 6834 | |
| 6835 | /* This function is an LUA binding used for registering |
| 6836 | * new keywords in the cli. It expects a list of keywords |
| 6837 | * which are the "path". It is limited to 5 keywords. A |
| 6838 | * description of the command, a function to be executed |
| 6839 | * for the parsing and a function for io handlers. |
| 6840 | */ |
| 6841 | __LJMP static int hlua_register_cli(lua_State *L) |
| 6842 | { |
| 6843 | struct cli_kw_list *cli_kws; |
| 6844 | const char *message; |
| 6845 | int ref_io; |
| 6846 | int len; |
| 6847 | struct hlua_function *fcn; |
| 6848 | int index; |
| 6849 | int i; |
| 6850 | |
| 6851 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 6852 | |
| 6853 | /* First argument : an array of maximum 5 keywords. */ |
| 6854 | if (!lua_istable(L, 1)) |
| 6855 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 6856 | |
| 6857 | /* Second argument : string with contextual message. */ |
| 6858 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6859 | |
| 6860 | /* Third and fourth argument : lua function. */ |
| 6861 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 6862 | |
| 6863 | /* Allocate and fill the sample fetch keyword struct. */ |
| 6864 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
| 6865 | if (!cli_kws) |
| 6866 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6867 | fcn = calloc(1, sizeof(*fcn)); |
| 6868 | if (!fcn) |
| 6869 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6870 | |
| 6871 | /* Fill path. */ |
| 6872 | index = 0; |
| 6873 | lua_pushnil(L); |
| 6874 | while(lua_next(L, 1) != 0) { |
| 6875 | if (index >= 5) |
| 6876 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 6877 | if (lua_type(L, -1) != LUA_TSTRING) |
| 6878 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 6879 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
| 6880 | if (!cli_kws->kw[0].str_kw[index]) |
| 6881 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6882 | index++; |
| 6883 | lua_pop(L, 1); |
| 6884 | } |
| 6885 | |
| 6886 | /* Copy help message. */ |
| 6887 | cli_kws->kw[0].usage = strdup(message); |
| 6888 | if (!cli_kws->kw[0].usage) |
| 6889 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6890 | |
| 6891 | /* Fill fcn io handler. */ |
| 6892 | len = strlen("<lua.cli>") + 1; |
| 6893 | for (i = 0; i < index; i++) |
| 6894 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 6895 | fcn->name = calloc(1, len); |
| 6896 | if (!fcn->name) |
| 6897 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6898 | strncat((char *)fcn->name, "<lua.cli", len); |
| 6899 | for (i = 0; i < index; i++) { |
| 6900 | strncat((char *)fcn->name, ".", len); |
| 6901 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 6902 | } |
| 6903 | strncat((char *)fcn->name, ">", len); |
| 6904 | fcn->function_ref = ref_io; |
| 6905 | |
| 6906 | /* Fill last entries. */ |
| 6907 | cli_kws->kw[0].private = fcn; |
| 6908 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 6909 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 6910 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 6911 | |
| 6912 | /* Register this new converter */ |
| 6913 | cli_register_kw(cli_kws); |
| 6914 | |
| 6915 | return 0; |
| 6916 | } |
| 6917 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6918 | static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx, |
| 6919 | struct proxy *defpx, const char *file, int line, |
| 6920 | char **err, unsigned int *timeout) |
| 6921 | { |
| 6922 | const char *error; |
| 6923 | |
| 6924 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
| 6925 | if (error && *error != '\0') { |
| 6926 | memprintf(err, "%s: invalid timeout", args[0]); |
| 6927 | return -1; |
| 6928 | } |
| 6929 | return 0; |
| 6930 | } |
| 6931 | |
| 6932 | static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx, |
| 6933 | struct proxy *defpx, const char *file, int line, |
| 6934 | char **err) |
| 6935 | { |
| 6936 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 6937 | file, line, err, &hlua_timeout_session); |
| 6938 | } |
| 6939 | |
| 6940 | static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx, |
| 6941 | struct proxy *defpx, const char *file, int line, |
| 6942 | char **err) |
| 6943 | { |
| 6944 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 6945 | file, line, err, &hlua_timeout_task); |
| 6946 | } |
| 6947 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6948 | static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx, |
| 6949 | struct proxy *defpx, const char *file, int line, |
| 6950 | char **err) |
| 6951 | { |
| 6952 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 6953 | file, line, err, &hlua_timeout_applet); |
| 6954 | } |
| 6955 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 6956 | static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx, |
| 6957 | struct proxy *defpx, const char *file, int line, |
| 6958 | char **err) |
| 6959 | { |
| 6960 | char *error; |
| 6961 | |
| 6962 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 6963 | if (*error != '\0') { |
| 6964 | memprintf(err, "%s: invalid number", args[0]); |
| 6965 | return -1; |
| 6966 | } |
| 6967 | return 0; |
| 6968 | } |
| 6969 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 6970 | static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx, |
| 6971 | struct proxy *defpx, const char *file, int line, |
| 6972 | char **err) |
| 6973 | { |
| 6974 | char *error; |
| 6975 | |
| 6976 | if (*(args[1]) == 0) { |
| 6977 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 6978 | return -1; |
| 6979 | } |
| 6980 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 6981 | if (*error != '\0') { |
| 6982 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 6983 | return -1; |
| 6984 | } |
| 6985 | return 0; |
| 6986 | } |
| 6987 | |
| 6988 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 6989 | /* This function is called by the main configuration key "lua-load". It loads and |
| 6990 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 6991 | * the main lua entry point. |
| 6992 | * |
| 6993 | * This funtion runs with the HAProxy keywords API. It returns -1 if an error is |
| 6994 | * occured, otherwise it returns 0. |
| 6995 | * |
| 6996 | * In some error case, LUA set an error message in top of the stack. This function |
| 6997 | * 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] | 6998 | * |
| 6999 | * This function can fail with an abort() due to an Lua critical error. |
| 7000 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7001 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7002 | */ |
| 7003 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
| 7004 | struct proxy *defpx, const char *file, int line, |
| 7005 | char **err) |
| 7006 | { |
| 7007 | int error; |
| 7008 | |
| 7009 | /* Just load and compile the file. */ |
| 7010 | error = luaL_loadfile(gL.T, args[1]); |
| 7011 | if (error) { |
| 7012 | memprintf(err, "error in lua file '%s': %s", args[1], lua_tostring(gL.T, -1)); |
| 7013 | lua_pop(gL.T, 1); |
| 7014 | return -1; |
| 7015 | } |
| 7016 | |
| 7017 | /* If no syntax error where detected, execute the code. */ |
| 7018 | error = lua_pcall(gL.T, 0, LUA_MULTRET, 0); |
| 7019 | switch (error) { |
| 7020 | case LUA_OK: |
| 7021 | break; |
| 7022 | case LUA_ERRRUN: |
| 7023 | memprintf(err, "lua runtime error: %s\n", lua_tostring(gL.T, -1)); |
| 7024 | lua_pop(gL.T, 1); |
| 7025 | return -1; |
| 7026 | case LUA_ERRMEM: |
| 7027 | memprintf(err, "lua out of memory error\n"); |
| 7028 | return -1; |
| 7029 | case LUA_ERRERR: |
| 7030 | memprintf(err, "lua message handler error: %s\n", lua_tostring(gL.T, -1)); |
| 7031 | lua_pop(gL.T, 1); |
| 7032 | return -1; |
| 7033 | case LUA_ERRGCMM: |
| 7034 | memprintf(err, "lua garbage collector error: %s\n", lua_tostring(gL.T, -1)); |
| 7035 | lua_pop(gL.T, 1); |
| 7036 | return -1; |
| 7037 | default: |
| 7038 | memprintf(err, "lua unknonwn error: %s\n", lua_tostring(gL.T, -1)); |
| 7039 | lua_pop(gL.T, 1); |
| 7040 | return -1; |
| 7041 | } |
| 7042 | |
| 7043 | return 0; |
| 7044 | } |
| 7045 | |
| 7046 | /* configuration keywords declaration */ |
| 7047 | static struct cfg_kw_list cfg_kws = {{ },{ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7048 | { CFG_GLOBAL, "lua-load", hlua_load }, |
| 7049 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 7050 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 7051 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 7052 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7053 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7054 | { 0, NULL, NULL }, |
| 7055 | }}; |
| 7056 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7057 | /* This function can fail with an abort() due to an Lua critical error. |
| 7058 | * We are in the initialisation process of HAProxy, this abort() is |
| 7059 | * tolerated. |
| 7060 | */ |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7061 | int hlua_post_init() |
| 7062 | { |
| 7063 | struct hlua_init_function *init; |
| 7064 | const char *msg; |
| 7065 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7066 | const char *error; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7067 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 7068 | /* Call post initialisation function in safe environement. */ |
| 7069 | if (!SET_SAFE_LJMP(gL.T)) { |
| 7070 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 7071 | error = lua_tostring(gL.T, -1); |
| 7072 | else |
| 7073 | error = "critical error"; |
| 7074 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 7075 | exit(1); |
| 7076 | } |
| 7077 | hlua_fcn_post_init(gL.T); |
| 7078 | RESET_SAFE_LJMP(gL.T); |
| 7079 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7080 | list_for_each_entry(init, &hlua_init_functions, l) { |
| 7081 | lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref); |
| 7082 | ret = hlua_ctx_resume(&gL, 0); |
| 7083 | switch (ret) { |
| 7084 | case HLUA_E_OK: |
| 7085 | lua_pop(gL.T, -1); |
| 7086 | return 1; |
| 7087 | case HLUA_E_AGAIN: |
| 7088 | Alert("lua init: yield not allowed.\n"); |
| 7089 | return 0; |
| 7090 | case HLUA_E_ERRMSG: |
| 7091 | msg = lua_tostring(gL.T, -1); |
| 7092 | Alert("lua init: %s.\n", msg); |
| 7093 | return 0; |
| 7094 | case HLUA_E_ERR: |
| 7095 | default: |
| 7096 | Alert("lua init: unknown runtime error.\n"); |
| 7097 | return 0; |
| 7098 | } |
| 7099 | } |
| 7100 | return 1; |
| 7101 | } |
| 7102 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7103 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 7104 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 7105 | * is the previously allocated size or the kind of object in case of a new |
| 7106 | * allocation. <nsize> is the requested new size. |
| 7107 | */ |
| 7108 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 7109 | { |
| 7110 | struct hlua_mem_allocator *zone = ud; |
| 7111 | |
| 7112 | if (nsize == 0) { |
| 7113 | /* it's a free */ |
| 7114 | if (ptr) |
| 7115 | zone->allocated -= osize; |
| 7116 | free(ptr); |
| 7117 | return NULL; |
| 7118 | } |
| 7119 | |
| 7120 | if (!ptr) { |
| 7121 | /* it's a new allocation */ |
| 7122 | if (zone->limit && zone->allocated + nsize > zone->limit) |
| 7123 | return NULL; |
| 7124 | |
| 7125 | ptr = malloc(nsize); |
| 7126 | if (ptr) |
| 7127 | zone->allocated += nsize; |
| 7128 | return ptr; |
| 7129 | } |
| 7130 | |
| 7131 | /* it's a realloc */ |
| 7132 | if (zone->limit && zone->allocated + nsize - osize > zone->limit) |
| 7133 | return NULL; |
| 7134 | |
| 7135 | ptr = realloc(ptr, nsize); |
| 7136 | if (ptr) |
| 7137 | zone->allocated += nsize - osize; |
| 7138 | return ptr; |
| 7139 | } |
| 7140 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7141 | /* Ithis function can fail with an abort() due to an Lua critical error. |
| 7142 | * We are in the initialisation process of HAProxy, this abort() is |
| 7143 | * tolerated. |
| 7144 | */ |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 7145 | void hlua_init(void) |
| 7146 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7147 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7148 | int idx; |
| 7149 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7150 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7151 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7152 | const char *error_msg; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7153 | #ifdef USE_OPENSSL |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7154 | struct srv_kw *kw; |
| 7155 | int tmp_error; |
| 7156 | char *error; |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 7157 | char *args[] = { /* SSL client configuration. */ |
| 7158 | "ssl", |
| 7159 | "verify", |
| 7160 | "none", |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 7161 | NULL |
| 7162 | }; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7163 | #endif |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7164 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7165 | /* Initialise struct hlua and com signals pool */ |
| 7166 | pool2_hlua = create_pool("hlua", sizeof(struct hlua), MEM_F_SHARED); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 7167 | pool2_hlua_com = create_pool("hlua_com", sizeof(struct hlua_com), MEM_F_SHARED); |
| 7168 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7169 | /* Register configuration keywords. */ |
| 7170 | cfg_register_keywords(&cfg_kws); |
| 7171 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 7172 | /* Init main lua stack. */ |
| 7173 | gL.Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 7174 | gL.flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 7175 | LIST_INIT(&gL.com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 7176 | gL.T = luaL_newstate(); |
| 7177 | hlua_sethlua(&gL); |
| 7178 | gL.Tref = LUA_REFNIL; |
| 7179 | gL.task = NULL; |
| 7180 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7181 | /* From this point, until the end of the initialisation fucntion, |
| 7182 | * the Lua function can fail with an abort. We are in the initialisation |
| 7183 | * process of HAProxy, this abort() is tolerated. |
| 7184 | */ |
| 7185 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7186 | /* change the memory allocators to track memory usage */ |
| 7187 | lua_setallocf(gL.T, hlua_alloc, &hlua_global_allocator); |
| 7188 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 7189 | /* Initialise lua. */ |
| 7190 | luaL_openlibs(gL.T); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7191 | |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 7192 | /* Set safe environment for the initialisation. */ |
| 7193 | if (!SET_SAFE_LJMP(gL.T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7194 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 7195 | error_msg = lua_tostring(gL.T, -1); |
| 7196 | else |
| 7197 | error_msg = "critical error"; |
| 7198 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 7199 | exit(1); |
| 7200 | } |
| 7201 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7202 | /* |
| 7203 | * |
| 7204 | * Create "core" object. |
| 7205 | * |
| 7206 | */ |
| 7207 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 7208 | /* This table entry is the object "core" base. */ |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7209 | lua_newtable(gL.T); |
| 7210 | |
| 7211 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7212 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7213 | hlua_class_const_int(gL.T, log_levels[i], i); |
| 7214 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7215 | /* Register special functions. */ |
| 7216 | hlua_class_function(gL.T, "register_init", hlua_register_init); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 7217 | hlua_class_function(gL.T, "register_task", hlua_register_task); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7218 | hlua_class_function(gL.T, "register_fetches", hlua_register_fetches); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 7219 | hlua_class_function(gL.T, "register_converters", hlua_register_converters); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7220 | hlua_class_function(gL.T, "register_action", hlua_register_action); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7221 | hlua_class_function(gL.T, "register_service", hlua_register_service); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7222 | hlua_class_function(gL.T, "register_cli", hlua_register_cli); |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 7223 | hlua_class_function(gL.T, "yield", hlua_yield); |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7224 | hlua_class_function(gL.T, "set_nice", hlua_set_nice); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 7225 | hlua_class_function(gL.T, "sleep", hlua_sleep); |
| 7226 | hlua_class_function(gL.T, "msleep", hlua_msleep); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 7227 | hlua_class_function(gL.T, "add_acl", hlua_add_acl); |
| 7228 | hlua_class_function(gL.T, "del_acl", hlua_del_acl); |
| 7229 | hlua_class_function(gL.T, "set_map", hlua_set_map); |
| 7230 | hlua_class_function(gL.T, "del_map", hlua_del_map); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7231 | hlua_class_function(gL.T, "tcp", hlua_socket_new); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 7232 | hlua_class_function(gL.T, "log", hlua_log); |
| 7233 | hlua_class_function(gL.T, "Debug", hlua_log_debug); |
| 7234 | hlua_class_function(gL.T, "Info", hlua_log_info); |
| 7235 | hlua_class_function(gL.T, "Warning", hlua_log_warning); |
| 7236 | hlua_class_function(gL.T, "Alert", hlua_log_alert); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 7237 | hlua_class_function(gL.T, "done", hlua_done); |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 7238 | hlua_fcn_reg_core_fcn(gL.T); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7239 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7240 | lua_setglobal(gL.T, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7241 | |
| 7242 | /* |
| 7243 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7244 | * Register class Map |
| 7245 | * |
| 7246 | */ |
| 7247 | |
| 7248 | /* This table entry is the object "Map" base. */ |
| 7249 | lua_newtable(gL.T); |
| 7250 | |
| 7251 | /* register pattern types. */ |
| 7252 | for (i=0; i<PAT_MATCH_NUM; i++) |
| 7253 | hlua_class_const_int(gL.T, pat_match_names[i], i); |
| 7254 | |
| 7255 | /* register constructor. */ |
| 7256 | hlua_class_function(gL.T, "new", hlua_map_new); |
| 7257 | |
| 7258 | /* Create and fill the metatable. */ |
| 7259 | lua_newtable(gL.T); |
| 7260 | |
| 7261 | /* Create and fille the __index entry. */ |
| 7262 | lua_pushstring(gL.T, "__index"); |
| 7263 | lua_newtable(gL.T); |
| 7264 | |
| 7265 | /* Register . */ |
| 7266 | hlua_class_function(gL.T, "lookup", hlua_map_lookup); |
| 7267 | hlua_class_function(gL.T, "slookup", hlua_map_slookup); |
| 7268 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7269 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7270 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7271 | /* Register previous table in the registry with reference and named entry. |
| 7272 | * The function hlua_register_metatable() pops the stack, so we |
| 7273 | * previously create a copy of the table. |
| 7274 | */ |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7275 | 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] | 7276 | class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7277 | |
| 7278 | /* Assign the metatable to the mai Map object. */ |
| 7279 | lua_setmetatable(gL.T, -2); |
| 7280 | |
| 7281 | /* Set a name to the table. */ |
| 7282 | lua_setglobal(gL.T, "Map"); |
| 7283 | |
| 7284 | /* |
| 7285 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7286 | * Register class Channel |
| 7287 | * |
| 7288 | */ |
| 7289 | |
| 7290 | /* Create and fill the metatable. */ |
| 7291 | lua_newtable(gL.T); |
| 7292 | |
| 7293 | /* Create and fille the __index entry. */ |
| 7294 | lua_pushstring(gL.T, "__index"); |
| 7295 | lua_newtable(gL.T); |
| 7296 | |
| 7297 | /* Register . */ |
| 7298 | hlua_class_function(gL.T, "get", hlua_channel_get); |
| 7299 | hlua_class_function(gL.T, "dup", hlua_channel_dup); |
| 7300 | hlua_class_function(gL.T, "getline", hlua_channel_getline); |
| 7301 | hlua_class_function(gL.T, "set", hlua_channel_set); |
| 7302 | hlua_class_function(gL.T, "append", hlua_channel_append); |
| 7303 | hlua_class_function(gL.T, "send", hlua_channel_send); |
| 7304 | hlua_class_function(gL.T, "forward", hlua_channel_forward); |
| 7305 | hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len); |
| 7306 | hlua_class_function(gL.T, "get_out_len", hlua_channel_get_out_len); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 7307 | hlua_class_function(gL.T, "is_full", hlua_channel_is_full); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7308 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7309 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7310 | |
| 7311 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7312 | class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7313 | |
| 7314 | /* |
| 7315 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7316 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7317 | * |
| 7318 | */ |
| 7319 | |
| 7320 | /* Create and fill the metatable. */ |
| 7321 | lua_newtable(gL.T); |
| 7322 | |
| 7323 | /* Create and fille the __index entry. */ |
| 7324 | lua_pushstring(gL.T, "__index"); |
| 7325 | lua_newtable(gL.T); |
| 7326 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7327 | /* Browse existing fetches and create the associated |
| 7328 | * object method. |
| 7329 | */ |
| 7330 | sf = NULL; |
| 7331 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
| 7332 | |
| 7333 | /* Dont register the keywork if the arguments check function are |
| 7334 | * not safe during the runtime. |
| 7335 | */ |
| 7336 | if ((sf->val_args != NULL) && |
| 7337 | (sf->val_args != val_payload_lv) && |
| 7338 | (sf->val_args != val_hdr)) |
| 7339 | continue; |
| 7340 | |
| 7341 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 7342 | * by an underscore. |
| 7343 | */ |
| 7344 | strncpy(trash.str, sf->kw, trash.size); |
| 7345 | trash.str[trash.size - 1] = '\0'; |
| 7346 | for (p = trash.str; *p; p++) |
| 7347 | if (*p == '.' || *p == '-' || *p == '+') |
| 7348 | *p = '_'; |
| 7349 | |
| 7350 | /* Register the function. */ |
| 7351 | lua_pushstring(gL.T, trash.str); |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 7352 | lua_pushlightuserdata(gL.T, sf); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7353 | lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7354 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7355 | } |
| 7356 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7357 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7358 | |
| 7359 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7360 | class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7361 | |
| 7362 | /* |
| 7363 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7364 | * Register class Converters |
| 7365 | * |
| 7366 | */ |
| 7367 | |
| 7368 | /* Create and fill the metatable. */ |
| 7369 | lua_newtable(gL.T); |
| 7370 | |
| 7371 | /* Create and fill the __index entry. */ |
| 7372 | lua_pushstring(gL.T, "__index"); |
| 7373 | lua_newtable(gL.T); |
| 7374 | |
| 7375 | /* Browse existing converters and create the associated |
| 7376 | * object method. |
| 7377 | */ |
| 7378 | sc = NULL; |
| 7379 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
| 7380 | /* Dont register the keywork if the arguments check function are |
| 7381 | * not safe during the runtime. |
| 7382 | */ |
| 7383 | if (sc->val_args != NULL) |
| 7384 | continue; |
| 7385 | |
| 7386 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 7387 | * by an underscore. |
| 7388 | */ |
| 7389 | strncpy(trash.str, sc->kw, trash.size); |
| 7390 | trash.str[trash.size - 1] = '\0'; |
| 7391 | for (p = trash.str; *p; p++) |
| 7392 | if (*p == '.' || *p == '-' || *p == '+') |
| 7393 | *p = '_'; |
| 7394 | |
| 7395 | /* Register the function. */ |
| 7396 | lua_pushstring(gL.T, trash.str); |
| 7397 | lua_pushlightuserdata(gL.T, sc); |
| 7398 | lua_pushcclosure(gL.T, hlua_run_sample_conv, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7399 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7400 | } |
| 7401 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7402 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7403 | |
| 7404 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7405 | class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7406 | |
| 7407 | /* |
| 7408 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7409 | * Register class HTTP |
| 7410 | * |
| 7411 | */ |
| 7412 | |
| 7413 | /* Create and fill the metatable. */ |
| 7414 | lua_newtable(gL.T); |
| 7415 | |
| 7416 | /* Create and fille the __index entry. */ |
| 7417 | lua_pushstring(gL.T, "__index"); |
| 7418 | lua_newtable(gL.T); |
| 7419 | |
| 7420 | /* Register Lua functions. */ |
| 7421 | hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers); |
| 7422 | hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr); |
| 7423 | hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr); |
| 7424 | hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val); |
| 7425 | hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr); |
| 7426 | hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr); |
| 7427 | hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth); |
| 7428 | hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path); |
| 7429 | hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query); |
| 7430 | hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri); |
| 7431 | |
| 7432 | hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers); |
| 7433 | hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr); |
| 7434 | hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr); |
| 7435 | hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val); |
| 7436 | hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr); |
| 7437 | 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] | 7438 | 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] | 7439 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7440 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7441 | |
| 7442 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7443 | class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7444 | |
| 7445 | /* |
| 7446 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7447 | * Register class AppletTCP |
| 7448 | * |
| 7449 | */ |
| 7450 | |
| 7451 | /* Create and fill the metatable. */ |
| 7452 | lua_newtable(gL.T); |
| 7453 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7454 | /* Create and fille the __index entry. */ |
| 7455 | lua_pushstring(gL.T, "__index"); |
| 7456 | lua_newtable(gL.T); |
| 7457 | |
| 7458 | /* Register Lua functions. */ |
Thierry FOURNIER / OZON.IO | 3e1d791 | 2016-12-12 12:29:34 +0100 | [diff] [blame] | 7459 | hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline); |
| 7460 | hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv); |
| 7461 | hlua_class_function(gL.T, "send", hlua_applet_tcp_send); |
| 7462 | hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv); |
| 7463 | hlua_class_function(gL.T, "get_priv", hlua_applet_tcp_get_priv); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 7464 | hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var); |
| 7465 | hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var); |
| 7466 | hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7467 | |
| 7468 | lua_settable(gL.T, -3); |
| 7469 | |
| 7470 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7471 | class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7472 | |
| 7473 | /* |
| 7474 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7475 | * Register class AppletHTTP |
| 7476 | * |
| 7477 | */ |
| 7478 | |
| 7479 | /* Create and fill the metatable. */ |
| 7480 | lua_newtable(gL.T); |
| 7481 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7482 | /* Create and fille the __index entry. */ |
| 7483 | lua_pushstring(gL.T, "__index"); |
| 7484 | lua_newtable(gL.T); |
| 7485 | |
| 7486 | /* Register Lua functions. */ |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 7487 | hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv); |
| 7488 | hlua_class_function(gL.T, "get_priv", hlua_applet_http_get_priv); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 7489 | hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var); |
| 7490 | hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var); |
| 7491 | hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7492 | hlua_class_function(gL.T, "getline", hlua_applet_http_getline); |
| 7493 | hlua_class_function(gL.T, "receive", hlua_applet_http_recv); |
| 7494 | hlua_class_function(gL.T, "send", hlua_applet_http_send); |
| 7495 | hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader); |
| 7496 | hlua_class_function(gL.T, "set_status", hlua_applet_http_status); |
| 7497 | hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response); |
| 7498 | |
| 7499 | lua_settable(gL.T, -3); |
| 7500 | |
| 7501 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7502 | class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7503 | |
| 7504 | /* |
| 7505 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7506 | * Register class TXN |
| 7507 | * |
| 7508 | */ |
| 7509 | |
| 7510 | /* Create and fill the metatable. */ |
| 7511 | lua_newtable(gL.T); |
| 7512 | |
| 7513 | /* Create and fille the __index entry. */ |
| 7514 | lua_pushstring(gL.T, "__index"); |
| 7515 | lua_newtable(gL.T); |
| 7516 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7517 | /* Register Lua functions. */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7518 | hlua_class_function(gL.T, "set_priv", hlua_set_priv); |
| 7519 | hlua_class_function(gL.T, "get_priv", hlua_get_priv); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7520 | hlua_class_function(gL.T, "set_var", hlua_set_var); |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 7521 | hlua_class_function(gL.T, "unset_var", hlua_unset_var); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7522 | hlua_class_function(gL.T, "get_var", hlua_get_var); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 7523 | hlua_class_function(gL.T, "done", hlua_txn_done); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7524 | hlua_class_function(gL.T, "set_loglevel",hlua_txn_set_loglevel); |
| 7525 | hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos); |
| 7526 | hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 7527 | hlua_class_function(gL.T, "deflog", hlua_txn_deflog); |
| 7528 | hlua_class_function(gL.T, "log", hlua_txn_log); |
| 7529 | hlua_class_function(gL.T, "Debug", hlua_txn_log_debug); |
| 7530 | hlua_class_function(gL.T, "Info", hlua_txn_log_info); |
| 7531 | hlua_class_function(gL.T, "Warning", hlua_txn_log_warning); |
| 7532 | hlua_class_function(gL.T, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7533 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7534 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7535 | |
| 7536 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7537 | class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7538 | |
| 7539 | /* |
| 7540 | * |
| 7541 | * Register class Socket |
| 7542 | * |
| 7543 | */ |
| 7544 | |
| 7545 | /* Create and fill the metatable. */ |
| 7546 | lua_newtable(gL.T); |
| 7547 | |
| 7548 | /* Create and fille the __index entry. */ |
| 7549 | lua_pushstring(gL.T, "__index"); |
| 7550 | lua_newtable(gL.T); |
| 7551 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 7552 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7553 | hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 7554 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7555 | hlua_class_function(gL.T, "connect", hlua_socket_connect); |
| 7556 | hlua_class_function(gL.T, "send", hlua_socket_send); |
| 7557 | hlua_class_function(gL.T, "receive", hlua_socket_receive); |
| 7558 | hlua_class_function(gL.T, "close", hlua_socket_close); |
| 7559 | hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername); |
| 7560 | hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname); |
| 7561 | hlua_class_function(gL.T, "setoption", hlua_socket_setoption); |
| 7562 | hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout); |
| 7563 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7564 | 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] | 7565 | |
| 7566 | /* Register the garbage collector entry. */ |
| 7567 | lua_pushstring(gL.T, "__gc"); |
| 7568 | lua_pushcclosure(gL.T, hlua_socket_gc, 0); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7569 | 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] | 7570 | |
| 7571 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7572 | class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7573 | |
| 7574 | /* Proxy and server configuration initialisation. */ |
| 7575 | memset(&socket_proxy, 0, sizeof(socket_proxy)); |
| 7576 | init_new_proxy(&socket_proxy); |
| 7577 | socket_proxy.parent = NULL; |
| 7578 | socket_proxy.last_change = now.tv_sec; |
| 7579 | socket_proxy.id = "LUA-SOCKET"; |
| 7580 | socket_proxy.cap = PR_CAP_FE | PR_CAP_BE; |
| 7581 | socket_proxy.maxconn = 0; |
| 7582 | socket_proxy.accept = NULL; |
| 7583 | socket_proxy.options2 |= PR_O2_INDEPSTR; |
| 7584 | socket_proxy.srv = NULL; |
| 7585 | socket_proxy.conn_retries = 0; |
| 7586 | socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */ |
| 7587 | |
| 7588 | /* Init TCP server: unchanged parameters */ |
| 7589 | memset(&socket_tcp, 0, sizeof(socket_tcp)); |
| 7590 | socket_tcp.next = NULL; |
| 7591 | socket_tcp.proxy = &socket_proxy; |
| 7592 | socket_tcp.obj_type = OBJ_TYPE_SERVER; |
| 7593 | LIST_INIT(&socket_tcp.actconns); |
| 7594 | LIST_INIT(&socket_tcp.pendconns); |
Willy Tarreau | 600802a | 2015-08-04 17:19:06 +0200 | [diff] [blame] | 7595 | LIST_INIT(&socket_tcp.priv_conns); |
Willy Tarreau | 173a1c6 | 2015-08-05 10:31:57 +0200 | [diff] [blame] | 7596 | LIST_INIT(&socket_tcp.idle_conns); |
Willy Tarreau | 7017cb0 | 2015-08-05 16:35:23 +0200 | [diff] [blame] | 7597 | LIST_INIT(&socket_tcp.safe_conns); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7598 | socket_tcp.state = SRV_ST_RUNNING; /* early server setup */ |
| 7599 | socket_tcp.last_change = 0; |
| 7600 | socket_tcp.id = "LUA-TCP-CONN"; |
| 7601 | socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 7602 | socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 7603 | socket_tcp.pp_opts = 0; /* Remove proxy protocol. */ |
| 7604 | |
| 7605 | /* XXX: Copy default parameter from default server, |
| 7606 | * but the default server is not initialized. |
| 7607 | */ |
| 7608 | socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue; |
| 7609 | socket_tcp.minconn = socket_proxy.defsrv.minconn; |
| 7610 | socket_tcp.maxconn = socket_proxy.defsrv.maxconn; |
| 7611 | socket_tcp.slowstart = socket_proxy.defsrv.slowstart; |
| 7612 | socket_tcp.onerror = socket_proxy.defsrv.onerror; |
| 7613 | socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 7614 | socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 7615 | socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 7616 | socket_tcp.uweight = socket_proxy.defsrv.iweight; |
| 7617 | socket_tcp.iweight = socket_proxy.defsrv.iweight; |
| 7618 | |
| 7619 | socket_tcp.check.status = HCHK_STATUS_INI; |
| 7620 | socket_tcp.check.rise = socket_proxy.defsrv.check.rise; |
| 7621 | socket_tcp.check.fall = socket_proxy.defsrv.check.fall; |
| 7622 | socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */ |
| 7623 | socket_tcp.check.server = &socket_tcp; |
| 7624 | |
| 7625 | socket_tcp.agent.status = HCHK_STATUS_INI; |
| 7626 | socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise; |
| 7627 | socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall; |
| 7628 | socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */ |
| 7629 | socket_tcp.agent.server = &socket_tcp; |
| 7630 | |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 7631 | socket_tcp.xprt = xprt_get(XPRT_RAW); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7632 | |
| 7633 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7634 | /* Init TCP server: unchanged parameters */ |
| 7635 | memset(&socket_ssl, 0, sizeof(socket_ssl)); |
| 7636 | socket_ssl.next = NULL; |
| 7637 | socket_ssl.proxy = &socket_proxy; |
| 7638 | socket_ssl.obj_type = OBJ_TYPE_SERVER; |
| 7639 | LIST_INIT(&socket_ssl.actconns); |
| 7640 | LIST_INIT(&socket_ssl.pendconns); |
Willy Tarreau | 600802a | 2015-08-04 17:19:06 +0200 | [diff] [blame] | 7641 | LIST_INIT(&socket_ssl.priv_conns); |
Willy Tarreau | 173a1c6 | 2015-08-05 10:31:57 +0200 | [diff] [blame] | 7642 | LIST_INIT(&socket_ssl.idle_conns); |
Willy Tarreau | 7017cb0 | 2015-08-05 16:35:23 +0200 | [diff] [blame] | 7643 | LIST_INIT(&socket_ssl.safe_conns); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7644 | socket_ssl.state = SRV_ST_RUNNING; /* early server setup */ |
| 7645 | socket_ssl.last_change = 0; |
| 7646 | socket_ssl.id = "LUA-SSL-CONN"; |
| 7647 | socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 7648 | socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 7649 | socket_ssl.pp_opts = 0; /* Remove proxy protocol. */ |
| 7650 | |
| 7651 | /* XXX: Copy default parameter from default server, |
| 7652 | * but the default server is not initialized. |
| 7653 | */ |
| 7654 | socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue; |
| 7655 | socket_ssl.minconn = socket_proxy.defsrv.minconn; |
| 7656 | socket_ssl.maxconn = socket_proxy.defsrv.maxconn; |
| 7657 | socket_ssl.slowstart = socket_proxy.defsrv.slowstart; |
| 7658 | socket_ssl.onerror = socket_proxy.defsrv.onerror; |
| 7659 | socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 7660 | socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 7661 | socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 7662 | socket_ssl.uweight = socket_proxy.defsrv.iweight; |
| 7663 | socket_ssl.iweight = socket_proxy.defsrv.iweight; |
| 7664 | |
| 7665 | socket_ssl.check.status = HCHK_STATUS_INI; |
| 7666 | socket_ssl.check.rise = socket_proxy.defsrv.check.rise; |
| 7667 | socket_ssl.check.fall = socket_proxy.defsrv.check.fall; |
| 7668 | socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */ |
| 7669 | socket_ssl.check.server = &socket_ssl; |
| 7670 | |
| 7671 | socket_ssl.agent.status = HCHK_STATUS_INI; |
| 7672 | socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise; |
| 7673 | socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall; |
| 7674 | socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */ |
| 7675 | socket_ssl.agent.server = &socket_ssl; |
| 7676 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 7677 | socket_ssl.use_ssl = 1; |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 7678 | socket_ssl.xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7679 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 7680 | for (idx = 0; args[idx] != NULL; idx++) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7681 | if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */ |
| 7682 | /* |
| 7683 | * |
| 7684 | * If the keyword is not known, we can search in the registered |
| 7685 | * server keywords. This is usefull to configure special SSL |
| 7686 | * features like client certificates and ssl_verify. |
| 7687 | * |
| 7688 | */ |
| 7689 | tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error); |
| 7690 | if (tmp_error != 0) { |
| 7691 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 7692 | abort(); /* This must be never arrives because the command line |
| 7693 | not editable by the user. */ |
| 7694 | } |
| 7695 | idx += kw->skip; |
| 7696 | } |
| 7697 | } |
| 7698 | |
| 7699 | /* Initialize SSL server. */ |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 7700 | ssl_sock_prepare_srv_ctx(&socket_ssl); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7701 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 7702 | |
| 7703 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 7704 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 7705 | |
| 7706 | __attribute__((constructor)) |
| 7707 | static void __hlua_init(void) |
| 7708 | { |
| 7709 | char *ptr = NULL; |
| 7710 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 7711 | hap_register_build_opts(ptr, 1); |
| 7712 | } |