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/stream_interface.h> |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 52 | #include <proto/task.h> |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 53 | #include <proto/tcp_rules.h> |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 54 | #include <proto/vars.h> |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 55 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 56 | /* Lua uses longjmp to perform yield or throwing errors. This |
| 57 | * macro is used only for identifying the function that can |
| 58 | * not return because a longjmp is executed. |
| 59 | * __LJMP marks a prototype of hlua file that can use longjmp. |
| 60 | * WILL_LJMP() marks an lua function that will use longjmp. |
| 61 | * MAY_LJMP() marks an lua function that may use longjmp. |
| 62 | */ |
| 63 | #define __LJMP |
| 64 | #define WILL_LJMP(func) func |
| 65 | #define MAY_LJMP(func) func |
| 66 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 67 | /* This couple of function executes securely some Lua calls outside of |
| 68 | * the lua runtime environment. Each Lua call can return a longjmp |
| 69 | * if it encounter a memory error. |
| 70 | * |
| 71 | * Lua documentation extract: |
| 72 | * |
| 73 | * If an error happens outside any protected environment, Lua calls |
| 74 | * a panic function (see lua_atpanic) and then calls abort, thus |
| 75 | * exiting the host application. Your panic function can avoid this |
| 76 | * exit by never returning (e.g., doing a long jump to your own |
| 77 | * recovery point outside Lua). |
| 78 | * |
| 79 | * The panic function runs as if it were a message handler (see |
| 80 | * §2.3); in particular, the error message is at the top of the |
| 81 | * stack. However, there is no guarantee about stack space. To push |
| 82 | * anything on the stack, the panic function must first check the |
| 83 | * available space (see §4.2). |
| 84 | * |
| 85 | * We must check all the Lua entry point. This includes: |
| 86 | * - The include/proto/hlua.h exported functions |
| 87 | * - the task wrapper function |
| 88 | * - The action wrapper function |
| 89 | * - The converters wrapper function |
| 90 | * - The sample-fetch wrapper functions |
| 91 | * |
| 92 | * It is tolerated that the initilisation function returns an abort. |
| 93 | * Before each Lua abort, an error message is writed on stderr. |
| 94 | * |
| 95 | * The macro SET_SAFE_LJMP initialise the longjmp. The Macro |
| 96 | * RESET_SAFE_LJMP reset the longjmp. These function must be macro |
| 97 | * because they must be exists in the program stack when the longjmp |
| 98 | * is called. |
| 99 | */ |
| 100 | jmp_buf safe_ljmp_env; |
| 101 | static int hlua_panic_safe(lua_State *L) { return 0; } |
| 102 | static int hlua_panic_ljmp(lua_State *L) { longjmp(safe_ljmp_env, 1); } |
| 103 | |
| 104 | #define SET_SAFE_LJMP(__L) \ |
| 105 | ({ \ |
| 106 | int ret; \ |
| 107 | if (setjmp(safe_ljmp_env) != 0) { \ |
| 108 | lua_atpanic(__L, hlua_panic_safe); \ |
| 109 | ret = 0; \ |
| 110 | } else { \ |
| 111 | lua_atpanic(__L, hlua_panic_ljmp); \ |
| 112 | ret = 1; \ |
| 113 | } \ |
| 114 | ret; \ |
| 115 | }) |
| 116 | |
| 117 | /* If we are the last function catching Lua errors, we |
| 118 | * must reset the panic function. |
| 119 | */ |
| 120 | #define RESET_SAFE_LJMP(__L) \ |
| 121 | do { \ |
| 122 | lua_atpanic(__L, hlua_panic_safe); \ |
| 123 | } while(0) |
| 124 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 125 | /* Applet status flags */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 126 | #define APPLET_DONE 0x01 /* applet processing is done. */ |
| 127 | #define APPLET_100C 0x02 /* 100 continue expected. */ |
| 128 | #define APPLET_HDR_SENT 0x04 /* Response header sent. */ |
| 129 | #define APPLET_CHUNKED 0x08 /* Use transfer encoding chunked. */ |
| 130 | #define APPLET_LAST_CHK 0x10 /* Last chunk sent. */ |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 131 | #define APPLET_HTTP11 0x20 /* Last chunk sent. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 132 | |
| 133 | #define HTTP_100C "HTTP/1.1 100 Continue\r\n\r\n" |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 134 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 135 | /* The main Lua execution context. */ |
| 136 | struct hlua gL; |
| 137 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 138 | /* This is the memory pool containing struct lua for applets |
| 139 | * (including cli). |
| 140 | */ |
| 141 | struct pool_head *pool2_hlua; |
| 142 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 143 | /* This is the memory pool containing all the signal structs. These |
| 144 | * struct are used to store each requiered signal between two tasks. |
| 145 | */ |
| 146 | struct pool_head *pool2_hlua_com; |
| 147 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 148 | /* Used for Socket connection. */ |
| 149 | static struct proxy socket_proxy; |
| 150 | static struct server socket_tcp; |
| 151 | #ifdef USE_OPENSSL |
| 152 | static struct server socket_ssl; |
| 153 | #endif |
| 154 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 155 | /* List head of the function called at the initialisation time. */ |
| 156 | struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions); |
| 157 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 158 | /* The following variables contains the reference of the different |
| 159 | * Lua classes. These references are useful for identify metadata |
| 160 | * associated with an object. |
| 161 | */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 162 | static int class_txn_ref; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 163 | static int class_socket_ref; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 164 | static int class_channel_ref; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 165 | static int class_fetches_ref; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 166 | static int class_converters_ref; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 167 | static int class_http_ref; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 168 | static int class_map_ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 169 | static int class_applet_tcp_ref; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 170 | static int class_applet_http_ref; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 171 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 172 | /* Global Lua execution timeout. By default Lua, execution linked |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 173 | * with stream (actions, sample-fetches and converters) have a |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 174 | * short timeout. Lua linked with tasks doesn't have a timeout |
| 175 | * because a task may remain alive during all the haproxy execution. |
| 176 | */ |
| 177 | static unsigned int hlua_timeout_session = 4000; /* session timeout. */ |
| 178 | static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 179 | static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 180 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 181 | /* Interrupts the Lua processing each "hlua_nb_instruction" instructions. |
| 182 | * it is used for preventing infinite loops. |
| 183 | * |
| 184 | * I test the scheer with an infinite loop containing one incrementation |
| 185 | * and one test. I run this loop between 10 seconds, I raise a ceil of |
| 186 | * 710M loops from one interrupt each 9000 instructions, so I fix the value |
| 187 | * to one interrupt each 10 000 instructions. |
| 188 | * |
| 189 | * configured | Number of |
| 190 | * instructions | loops executed |
| 191 | * between two | in milions |
| 192 | * forced yields | |
| 193 | * ---------------+--------------- |
| 194 | * 10 | 160 |
| 195 | * 500 | 670 |
| 196 | * 1000 | 680 |
| 197 | * 5000 | 700 |
| 198 | * 7000 | 700 |
| 199 | * 8000 | 700 |
| 200 | * 9000 | 710 <- ceil |
| 201 | * 10000 | 710 |
| 202 | * 100000 | 710 |
| 203 | * 1000000 | 710 |
| 204 | * |
| 205 | */ |
| 206 | static unsigned int hlua_nb_instruction = 10000; |
| 207 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 208 | /* Descriptor for the memory allocation state. If limit is not null, it will |
| 209 | * be enforced on any memory allocation. |
| 210 | */ |
| 211 | struct hlua_mem_allocator { |
| 212 | size_t allocated; |
| 213 | size_t limit; |
| 214 | }; |
| 215 | |
| 216 | static struct hlua_mem_allocator hlua_global_allocator; |
| 217 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 218 | static const char error_500[] = |
Jarno Huuskonen | 16ad94a | 2017-01-09 14:17:10 +0200 | [diff] [blame] | 219 | "HTTP/1.0 500 Internal Server Error\r\n" |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 220 | "Cache-Control: no-cache\r\n" |
| 221 | "Connection: close\r\n" |
| 222 | "Content-Type: text/html\r\n" |
| 223 | "\r\n" |
Jarno Huuskonen | 16ad94a | 2017-01-09 14:17:10 +0200 | [diff] [blame] | 224 | "<html><body><h1>500 Internal Server Error</h1>\nAn internal server error occured.\n</body></html>\n"; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 225 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 226 | /* These functions converts types between HAProxy internal args or |
| 227 | * sample and LUA types. Another function permits to check if the |
| 228 | * LUA stack contains arguments according with an required ARG_T |
| 229 | * format. |
| 230 | */ |
| 231 | static int hlua_arg2lua(lua_State *L, const struct arg *arg); |
| 232 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 233 | __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] | 234 | uint64_t mask, struct proxy *p); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 235 | static int hlua_smp2lua(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 236 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 237 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp); |
| 238 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 239 | __LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg); |
| 240 | |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 241 | #define SEND_ERR(__be, __fmt, __args...) \ |
| 242 | do { \ |
| 243 | send_log(__be, LOG_ERR, __fmt, ## __args); \ |
| 244 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \ |
| 245 | Alert(__fmt, ## __args); \ |
| 246 | } while (0) |
| 247 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 248 | /* Used to check an Lua function type in the stack. It creates and |
| 249 | * returns a reference of the function. This function throws an |
| 250 | * error if the rgument is not a "function". |
| 251 | */ |
| 252 | __LJMP unsigned int hlua_checkfunction(lua_State *L, int argno) |
| 253 | { |
| 254 | if (!lua_isfunction(L, argno)) { |
| 255 | const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, -1)); |
| 256 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 257 | } |
| 258 | lua_pushvalue(L, argno); |
| 259 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 260 | } |
| 261 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 262 | /* Return the string that is of the top of the stack. */ |
| 263 | const char *hlua_get_top_error_string(lua_State *L) |
| 264 | { |
| 265 | if (lua_gettop(L) < 1) |
| 266 | return "unknown error"; |
| 267 | if (lua_type(L, -1) != LUA_TSTRING) |
| 268 | return "unknown error"; |
| 269 | return lua_tostring(L, -1); |
| 270 | } |
| 271 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 272 | /* This function check the number of arguments available in the |
| 273 | * stack. If the number of arguments available is not the same |
| 274 | * then <nb> an error is throwed. |
| 275 | */ |
| 276 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 277 | { |
| 278 | if (lua_gettop(L) == nb) |
| 279 | return; |
| 280 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 281 | } |
| 282 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 283 | /* This fucntion push an error string prefixed by the file name |
| 284 | * and the line number where the error is encountered. |
| 285 | */ |
| 286 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 287 | { |
| 288 | va_list argp; |
| 289 | va_start(argp, fmt); |
| 290 | luaL_where(L, 1); |
| 291 | lua_pushvfstring(L, fmt, argp); |
| 292 | va_end(argp); |
| 293 | lua_concat(L, 2); |
| 294 | return 1; |
| 295 | } |
| 296 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 297 | /* This function register a new signal. "lua" is the current lua |
| 298 | * execution context. It contains a pointer to the associated task. |
| 299 | * "link" is a list head attached to an other task that must be wake |
| 300 | * the lua task if an event occurs. This is useful with external |
| 301 | * events like TCP I/O or sleep functions. This funcion allocate |
| 302 | * memory for the signal. |
| 303 | */ |
Thierry FOURNIER | 847ca66 | 2016-12-16 13:07:22 +0100 | [diff] [blame] | 304 | 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] | 305 | { |
| 306 | struct hlua_com *com = pool_alloc2(pool2_hlua_com); |
| 307 | if (!com) |
Thierry FOURNIER | 847ca66 | 2016-12-16 13:07:22 +0100 | [diff] [blame] | 308 | return NULL; |
| 309 | LIST_ADDQ(purge, &com->purge_me); |
| 310 | LIST_ADDQ(event, &com->wake_me); |
| 311 | com->task = wakeup; |
| 312 | return com; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | /* This function purge all the pending signals when the LUA execution |
| 316 | * is finished. This prevent than a coprocess try to wake a deleted |
| 317 | * task. This function remove the memory associated to the signal. |
| 318 | */ |
Thierry FOURNIER | 847ca66 | 2016-12-16 13:07:22 +0100 | [diff] [blame] | 319 | static void hlua_com_purge(struct list *purge) |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 320 | { |
| 321 | struct hlua_com *com, *back; |
| 322 | |
| 323 | /* Delete all pending communication signals. */ |
Thierry FOURNIER | 847ca66 | 2016-12-16 13:07:22 +0100 | [diff] [blame] | 324 | list_for_each_entry_safe(com, back, purge, purge_me) { |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 325 | LIST_DEL(&com->purge_me); |
| 326 | LIST_DEL(&com->wake_me); |
| 327 | pool_free2(pool2_hlua_com, com); |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | /* This function sends signals. It wakes all the tasks attached |
| 332 | * to a list head, and remove the signal, and free the used |
| 333 | * memory. |
| 334 | */ |
| 335 | static void hlua_com_wake(struct list *wake) |
| 336 | { |
| 337 | struct hlua_com *com, *back; |
| 338 | |
| 339 | /* Wake task and delete all pending communication signals. */ |
| 340 | list_for_each_entry_safe(com, back, wake, wake_me) { |
| 341 | LIST_DEL(&com->purge_me); |
| 342 | LIST_DEL(&com->wake_me); |
| 343 | task_wakeup(com->task, TASK_WOKEN_MSG); |
| 344 | pool_free2(pool2_hlua_com, com); |
| 345 | } |
| 346 | } |
| 347 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 348 | /* This functions is used with sample fetch and converters. It |
| 349 | * converts the HAProxy configuration argument in a lua stack |
| 350 | * values. |
| 351 | * |
| 352 | * It takes an array of "arg", and each entry of the array is |
| 353 | * converted and pushed in the LUA stack. |
| 354 | */ |
| 355 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 356 | { |
| 357 | switch (arg->type) { |
| 358 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 359 | case ARGT_TIME: |
| 360 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 361 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 362 | break; |
| 363 | |
| 364 | case ARGT_STR: |
| 365 | lua_pushlstring(L, arg->data.str.str, arg->data.str.len); |
| 366 | break; |
| 367 | |
| 368 | case ARGT_IPV4: |
| 369 | case ARGT_IPV6: |
| 370 | case ARGT_MSK4: |
| 371 | case ARGT_MSK6: |
| 372 | case ARGT_FE: |
| 373 | case ARGT_BE: |
| 374 | case ARGT_TAB: |
| 375 | case ARGT_SRV: |
| 376 | case ARGT_USR: |
| 377 | case ARGT_MAP: |
| 378 | default: |
| 379 | lua_pushnil(L); |
| 380 | break; |
| 381 | } |
| 382 | return 1; |
| 383 | } |
| 384 | |
| 385 | /* This function take one entrie in an LUA stack at the index "ud", |
| 386 | * and try to convert it in an HAProxy argument entry. This is useful |
| 387 | * with sample fetch wrappers. The input arguments are gived to the |
| 388 | * lua wrapper and converted as arg list by thi function. |
| 389 | */ |
| 390 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 391 | { |
| 392 | switch (lua_type(L, ud)) { |
| 393 | |
| 394 | case LUA_TNUMBER: |
| 395 | case LUA_TBOOLEAN: |
| 396 | arg->type = ARGT_SINT; |
| 397 | arg->data.sint = lua_tointeger(L, ud); |
| 398 | break; |
| 399 | |
| 400 | case LUA_TSTRING: |
| 401 | arg->type = ARGT_STR; |
| 402 | arg->data.str.str = (char *)lua_tolstring(L, ud, (size_t *)&arg->data.str.len); |
| 403 | break; |
| 404 | |
| 405 | case LUA_TUSERDATA: |
| 406 | case LUA_TNIL: |
| 407 | case LUA_TTABLE: |
| 408 | case LUA_TFUNCTION: |
| 409 | case LUA_TTHREAD: |
| 410 | case LUA_TLIGHTUSERDATA: |
| 411 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 412 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 413 | break; |
| 414 | } |
| 415 | return 1; |
| 416 | } |
| 417 | |
| 418 | /* the following functions are used to convert a struct sample |
| 419 | * in Lua type. This useful to convert the return of the |
| 420 | * fetchs or converters. |
| 421 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 422 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 423 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 424 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 425 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 426 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 427 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 428 | break; |
| 429 | |
| 430 | case SMP_T_BIN: |
| 431 | case SMP_T_STR: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 432 | 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] | 433 | break; |
| 434 | |
| 435 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 436 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 437 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 438 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 439 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 440 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 441 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 442 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 443 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 444 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 445 | case HTTP_METH_OTHER: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 446 | 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] | 447 | break; |
| 448 | default: |
| 449 | lua_pushnil(L); |
| 450 | break; |
| 451 | } |
| 452 | break; |
| 453 | |
| 454 | case SMP_T_IPV4: |
| 455 | case SMP_T_IPV6: |
| 456 | 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] | 457 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 458 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 459 | 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] | 460 | else |
| 461 | lua_pushnil(L); |
| 462 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 463 | default: |
| 464 | lua_pushnil(L); |
| 465 | break; |
| 466 | } |
| 467 | return 1; |
| 468 | } |
| 469 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 470 | /* the following functions are used to convert a struct sample |
| 471 | * in Lua strings. This is useful to convert the return of the |
| 472 | * fetchs or converters. |
| 473 | */ |
| 474 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 475 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 476 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 477 | |
| 478 | case SMP_T_BIN: |
| 479 | case SMP_T_STR: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 480 | 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] | 481 | break; |
| 482 | |
| 483 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 484 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 485 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 486 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 487 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 488 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 489 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 490 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 491 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 492 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 493 | case HTTP_METH_OTHER: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 494 | 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] | 495 | break; |
| 496 | default: |
| 497 | lua_pushstring(L, ""); |
| 498 | break; |
| 499 | } |
| 500 | break; |
| 501 | |
| 502 | case SMP_T_SINT: |
| 503 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 504 | case SMP_T_IPV4: |
| 505 | case SMP_T_IPV6: |
| 506 | 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] | 507 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 508 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 509 | 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] | 510 | else |
| 511 | lua_pushstring(L, ""); |
| 512 | break; |
| 513 | default: |
| 514 | lua_pushstring(L, ""); |
| 515 | break; |
| 516 | } |
| 517 | return 1; |
| 518 | } |
| 519 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 520 | /* the following functions are used to convert an Lua type in a |
| 521 | * struct sample. This is useful to provide data from a converter |
| 522 | * to the LUA code. |
| 523 | */ |
| 524 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 525 | { |
| 526 | switch (lua_type(L, ud)) { |
| 527 | |
| 528 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 529 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 530 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 531 | break; |
| 532 | |
| 533 | |
| 534 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 535 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 536 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 537 | break; |
| 538 | |
| 539 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 540 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 541 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 542 | 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] | 543 | break; |
| 544 | |
| 545 | case LUA_TUSERDATA: |
| 546 | case LUA_TNIL: |
| 547 | case LUA_TTABLE: |
| 548 | case LUA_TFUNCTION: |
| 549 | case LUA_TTHREAD: |
| 550 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 551 | case LUA_TNONE: |
| 552 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 553 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 554 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 555 | break; |
| 556 | } |
| 557 | return 1; |
| 558 | } |
| 559 | |
| 560 | /* This function check the "argp" builded by another conversion function |
| 561 | * is in accord with the expected argp defined by the "mask". The fucntion |
| 562 | * 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] | 563 | * |
| 564 | * This function assumes thant the argp argument contains ARGM_NBARGS + 1 |
| 565 | * entries. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 566 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 567 | __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] | 568 | uint64_t mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 569 | { |
| 570 | int min_arg; |
| 571 | int idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 572 | struct proxy *px; |
| 573 | char *sname, *pname; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 574 | |
| 575 | idx = 0; |
| 576 | min_arg = ARGM(mask); |
| 577 | mask >>= ARGM_BITS; |
| 578 | |
| 579 | while (1) { |
| 580 | |
| 581 | /* Check oversize. */ |
| 582 | if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) { |
Cyril Bonté | 577a36a | 2015-03-02 00:08:38 +0100 | [diff] [blame] | 583 | WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | /* Check for mandatory arguments. */ |
| 587 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 588 | if (idx < min_arg) { |
| 589 | |
| 590 | /* If miss other argument than the first one, we return an error. */ |
| 591 | if (idx > 0) |
| 592 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 593 | |
| 594 | /* If first argument have a certain type, some default values |
| 595 | * may be used. See the function smp_resolve_args(). |
| 596 | */ |
| 597 | switch (mask & ARGT_MASK) { |
| 598 | |
| 599 | case ARGT_FE: |
| 600 | if (!(p->cap & PR_CAP_FE)) |
| 601 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 602 | argp[idx].data.prx = p; |
| 603 | argp[idx].type = ARGT_FE; |
| 604 | argp[idx+1].type = ARGT_STOP; |
| 605 | break; |
| 606 | |
| 607 | case ARGT_BE: |
| 608 | if (!(p->cap & PR_CAP_BE)) |
| 609 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 610 | argp[idx].data.prx = p; |
| 611 | argp[idx].type = ARGT_BE; |
| 612 | argp[idx+1].type = ARGT_STOP; |
| 613 | break; |
| 614 | |
| 615 | case ARGT_TAB: |
| 616 | argp[idx].data.prx = p; |
| 617 | argp[idx].type = ARGT_TAB; |
| 618 | argp[idx+1].type = ARGT_STOP; |
| 619 | break; |
| 620 | |
| 621 | default: |
| 622 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 623 | break; |
| 624 | } |
| 625 | } |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 626 | return 0; |
| 627 | } |
| 628 | |
| 629 | /* Check for exceed the number of requiered argument. */ |
| 630 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 631 | argp[idx].type != ARGT_STOP) { |
| 632 | WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected")); |
| 633 | } |
| 634 | |
| 635 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 636 | argp[idx].type == ARGT_STOP) { |
| 637 | return 0; |
| 638 | } |
| 639 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 640 | /* Convert some argument types. */ |
| 641 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 642 | case ARGT_SINT: |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 643 | if (argp[idx].type != ARGT_SINT) |
| 644 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
| 645 | argp[idx].type = ARGT_SINT; |
| 646 | break; |
| 647 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 648 | case ARGT_TIME: |
| 649 | if (argp[idx].type != ARGT_SINT) |
| 650 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 651 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 652 | break; |
| 653 | |
| 654 | case ARGT_SIZE: |
| 655 | if (argp[idx].type != ARGT_SINT) |
| 656 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 657 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 658 | break; |
| 659 | |
| 660 | case ARGT_FE: |
| 661 | if (argp[idx].type != ARGT_STR) |
| 662 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
| 663 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 664 | trash.str[argp[idx].data.str.len] = 0; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 665 | argp[idx].data.prx = proxy_fe_by_name(trash.str); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 666 | if (!argp[idx].data.prx) |
| 667 | WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist")); |
| 668 | argp[idx].type = ARGT_FE; |
| 669 | break; |
| 670 | |
| 671 | case ARGT_BE: |
| 672 | if (argp[idx].type != ARGT_STR) |
| 673 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
| 674 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 675 | trash.str[argp[idx].data.str.len] = 0; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 676 | argp[idx].data.prx = proxy_be_by_name(trash.str); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 677 | if (!argp[idx].data.prx) |
| 678 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 679 | argp[idx].type = ARGT_BE; |
| 680 | break; |
| 681 | |
| 682 | case ARGT_TAB: |
| 683 | if (argp[idx].type != ARGT_STR) |
| 684 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
| 685 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 686 | trash.str[argp[idx].data.str.len] = 0; |
Willy Tarreau | e2dc1fa | 2015-05-26 12:08:07 +0200 | [diff] [blame] | 687 | argp[idx].data.prx = proxy_tbl_by_name(trash.str); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 688 | if (!argp[idx].data.prx) |
| 689 | WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist")); |
| 690 | argp[idx].type = ARGT_TAB; |
| 691 | break; |
| 692 | |
| 693 | case ARGT_SRV: |
| 694 | if (argp[idx].type != ARGT_STR) |
| 695 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
| 696 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 697 | trash.str[argp[idx].data.str.len] = 0; |
| 698 | sname = strrchr(trash.str, '/'); |
| 699 | if (sname) { |
| 700 | *sname++ = '\0'; |
| 701 | pname = trash.str; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 702 | px = proxy_be_by_name(pname); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 703 | if (!px) |
| 704 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 705 | } |
| 706 | else { |
| 707 | sname = trash.str; |
| 708 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 709 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 710 | argp[idx].data.srv = findserver(px, sname); |
| 711 | if (!argp[idx].data.srv) |
| 712 | WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist")); |
| 713 | argp[idx].type = ARGT_SRV; |
| 714 | break; |
| 715 | |
| 716 | case ARGT_IPV4: |
| 717 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 718 | trash.str[argp[idx].data.str.len] = 0; |
| 719 | if (inet_pton(AF_INET, trash.str, &argp[idx].data.ipv4)) |
| 720 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address")); |
| 721 | argp[idx].type = ARGT_IPV4; |
| 722 | break; |
| 723 | |
| 724 | case ARGT_MSK4: |
| 725 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 726 | trash.str[argp[idx].data.str.len] = 0; |
| 727 | if (!str2mask(trash.str, &argp[idx].data.ipv4)) |
| 728 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask")); |
| 729 | argp[idx].type = ARGT_MSK4; |
| 730 | break; |
| 731 | |
| 732 | case ARGT_IPV6: |
| 733 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 734 | trash.str[argp[idx].data.str.len] = 0; |
| 735 | if (inet_pton(AF_INET6, trash.str, &argp[idx].data.ipv6)) |
| 736 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address")); |
| 737 | argp[idx].type = ARGT_IPV6; |
| 738 | break; |
| 739 | |
| 740 | case ARGT_MSK6: |
| 741 | case ARGT_MAP: |
| 742 | case ARGT_REG: |
| 743 | case ARGT_USR: |
| 744 | WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 745 | break; |
| 746 | } |
| 747 | |
| 748 | /* Check for type of argument. */ |
| 749 | if ((mask & ARGT_MASK) != argp[idx].type) { |
| 750 | const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 751 | arg_type_names[(mask & ARGT_MASK)], |
| 752 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 753 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 754 | } |
| 755 | |
| 756 | /* Next argument. */ |
| 757 | mask >>= ARGT_BITS; |
| 758 | idx++; |
| 759 | } |
| 760 | } |
| 761 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 762 | /* |
| 763 | * The following functions are used to make correspondance between the the |
| 764 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 765 | * |
| 766 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 767 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 768 | */ |
| 769 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 770 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 771 | struct hlua **hlua = lua_getextraspace(L); |
| 772 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 773 | } |
| 774 | static inline void hlua_sethlua(struct hlua *hlua) |
| 775 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 776 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 777 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 778 | } |
| 779 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 780 | /* This function is used to send logs. It try to send on screen (stderr) |
| 781 | * and on the default syslog server. |
| 782 | */ |
| 783 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 784 | { |
| 785 | struct tm tm; |
| 786 | char *p; |
| 787 | |
| 788 | /* Cleanup the log message. */ |
| 789 | p = trash.str; |
| 790 | for (; *msg != '\0'; msg++, p++) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 791 | if (p >= trash.str + trash.size - 1) { |
| 792 | /* Break the message if exceed the buffer size. */ |
| 793 | *(p-4) = ' '; |
| 794 | *(p-3) = '.'; |
| 795 | *(p-2) = '.'; |
| 796 | *(p-1) = '.'; |
| 797 | break; |
| 798 | } |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 799 | if (isprint(*msg)) |
| 800 | *p = *msg; |
| 801 | else |
| 802 | *p = '.'; |
| 803 | } |
| 804 | *p = '\0'; |
| 805 | |
Thierry FOURNIER | 5554e29 | 2015-09-09 11:21:37 +0200 | [diff] [blame] | 806 | send_log(px, level, "%s\n", trash.str); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 807 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 808 | get_localtime(date.tv_sec, &tm); |
| 809 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 810 | log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, |
| 811 | (int)getpid(), trash.str); |
| 812 | fflush(stderr); |
| 813 | } |
| 814 | } |
| 815 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 816 | /* This function just ensure that the yield will be always |
| 817 | * returned with a timeout and permit to set some flags |
| 818 | */ |
| 819 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 820 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 821 | { |
| 822 | struct hlua *hlua = hlua_gethlua(L); |
| 823 | |
| 824 | /* Set the wake timeout. If timeout is required, we set |
| 825 | * the expiration time. |
| 826 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 827 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 828 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 829 | hlua->flags |= flags; |
| 830 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 831 | /* Process the yield. */ |
| 832 | WILL_LJMP(lua_yieldk(L, nresults, ctx, k)); |
| 833 | } |
| 834 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 835 | /* This function initialises the Lua environment stored in the stream. |
| 836 | * 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] | 837 | * 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] | 838 | * |
| 839 | * This function is particular. it initialises a new Lua thread. If the |
| 840 | * initialisation fails (example: out of memory error), the lua function |
| 841 | * throws an error (longjmp). |
| 842 | * |
| 843 | * This function manipulates two Lua stack: the main and the thread. Only |
| 844 | * the main stack can fail. The thread is not manipulated. This function |
| 845 | * MUST NOT manipulate the created thread stack state, because is not |
| 846 | * proctected agains error throwed by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 847 | */ |
| 848 | int hlua_ctx_init(struct hlua *lua, struct task *task) |
| 849 | { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 850 | if (!SET_SAFE_LJMP(gL.T)) { |
| 851 | lua->Tref = LUA_REFNIL; |
| 852 | return 0; |
| 853 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 854 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 855 | lua->flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 856 | LIST_INIT(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 857 | lua->T = lua_newthread(gL.T); |
| 858 | if (!lua->T) { |
| 859 | lua->Tref = LUA_REFNIL; |
| 860 | return 0; |
| 861 | } |
| 862 | hlua_sethlua(lua); |
| 863 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 864 | lua->task = task; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 865 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 866 | return 1; |
| 867 | } |
| 868 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 869 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 870 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 871 | * is not freed. |
| 872 | */ |
| 873 | void hlua_ctx_destroy(struct hlua *lua) |
| 874 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 875 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 876 | return; |
| 877 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 878 | if (!lua->T) |
| 879 | goto end; |
| 880 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 881 | /* Purge all the pending signals. */ |
Thierry FOURNIER | 847ca66 | 2016-12-16 13:07:22 +0100 | [diff] [blame] | 882 | hlua_com_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 883 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 884 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 885 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 886 | |
| 887 | /* Forces a garbage collecting process. If the Lua program is finished |
| 888 | * without error, we run the GC on the thread pointer. Its freed all |
| 889 | * the unused memory. |
| 890 | * If the thread is finnish with an error or is currently yielded, |
| 891 | * it seems that the GC applied on the thread doesn't clean anything, |
| 892 | * so e run the GC on the main thread. |
| 893 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 894 | * the garbage collection. |
| 895 | */ |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 896 | if (lua->flags & HLUA_MUST_GC) { |
| 897 | lua_gc(lua->T, LUA_GCCOLLECT, 0); |
| 898 | if (lua_status(lua->T) != LUA_OK) |
| 899 | lua_gc(gL.T, LUA_GCCOLLECT, 0); |
| 900 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 901 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 902 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 903 | |
| 904 | end: |
| 905 | pool_free2(pool2_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | /* This function is used to restore the Lua context when a coroutine |
| 909 | * fails. This function copy the common memory between old coroutine |
| 910 | * and the new coroutine. The old coroutine is destroyed, and its |
| 911 | * replaced by the new coroutine. |
| 912 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 913 | * as string error message and it is copied in the new stack. |
| 914 | */ |
| 915 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 916 | { |
| 917 | lua_State *T; |
| 918 | int new_ref; |
| 919 | |
| 920 | /* Renew the main LUA stack doesn't have sense. */ |
| 921 | if (lua == &gL) |
| 922 | return 0; |
| 923 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 924 | /* New Lua coroutine. */ |
| 925 | T = lua_newthread(gL.T); |
| 926 | if (!T) |
| 927 | return 0; |
| 928 | |
| 929 | /* Copy last error message. */ |
| 930 | if (keep_msg) |
| 931 | lua_xmove(lua->T, T, 1); |
| 932 | |
| 933 | /* Copy data between the coroutines. */ |
| 934 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 935 | lua_xmove(lua->T, T, 1); |
| 936 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */ |
| 937 | |
| 938 | /* Destroy old data. */ |
| 939 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 940 | |
| 941 | /* The thread is garbage collected by Lua. */ |
| 942 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 943 | |
| 944 | /* Fill the struct with the new coroutine values. */ |
| 945 | lua->Mref = new_ref; |
| 946 | lua->T = T; |
| 947 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 948 | |
| 949 | /* Set context. */ |
| 950 | hlua_sethlua(lua); |
| 951 | |
| 952 | return 1; |
| 953 | } |
| 954 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 955 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 956 | { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 957 | struct hlua *hlua = hlua_gethlua(L); |
| 958 | |
| 959 | /* Lua cannot yield when its returning from a function, |
| 960 | * so, we can fix the interrupt hook to 1 instruction, |
| 961 | * expecting that the function is finnished. |
| 962 | */ |
| 963 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 964 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 965 | return; |
| 966 | } |
| 967 | |
| 968 | /* restore the interrupt condition. */ |
| 969 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 970 | |
| 971 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 972 | * If the state is not yieldable, trying yield causes an error. |
| 973 | */ |
| 974 | if (lua_isyieldable(L)) |
| 975 | WILL_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
| 976 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 977 | /* If we cannot yield, update the clock and check the timeout. */ |
| 978 | tv_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 979 | hlua->run_time += now_ms - hlua->start_time; |
| 980 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 981 | lua_pushfstring(L, "execution timeout"); |
| 982 | WILL_LJMP(lua_error(L)); |
| 983 | } |
| 984 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 985 | /* Update the start time. */ |
| 986 | hlua->start_time = now_ms; |
| 987 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 988 | /* Try to interrupt the process at the end of the current |
| 989 | * unyieldable function. |
| 990 | */ |
| 991 | 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] | 992 | } |
| 993 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 994 | /* This function start or resumes the Lua stack execution. If the flag |
| 995 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 996 | * The function return an error. |
| 997 | * |
| 998 | * The function can returns 4 values: |
| 999 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1000 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1001 | * task wakeup. |
| 1002 | * - HLUA_E_ERRMSG : An error has occured, an error message is set in |
| 1003 | * the top of the stack. |
| 1004 | * - HLUA_E_ERR : An error has occured without error message. |
| 1005 | * |
| 1006 | * If an error occured, the stack is renewed and it is ready to run new |
| 1007 | * LUA code. |
| 1008 | */ |
| 1009 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1010 | { |
| 1011 | int ret; |
| 1012 | const char *msg; |
| 1013 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1014 | /* Initialise run time counter. */ |
| 1015 | if (!HLUA_IS_RUNNING(lua)) |
| 1016 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1017 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1018 | resume_execution: |
| 1019 | |
| 1020 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1021 | * instructions. it is used for preventing infinite loops. |
| 1022 | */ |
| 1023 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1024 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1025 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1026 | HLUA_SET_RUN(lua); |
| 1027 | HLUA_CLR_CTRLYIELD(lua); |
| 1028 | HLUA_CLR_WAKERESWR(lua); |
| 1029 | HLUA_CLR_WAKEREQWR(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1030 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1031 | /* Update the start time. */ |
| 1032 | lua->start_time = now_ms; |
| 1033 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1034 | /* Call the function. */ |
| 1035 | ret = lua_resume(lua->T, gL.T, lua->nargs); |
| 1036 | switch (ret) { |
| 1037 | |
| 1038 | case LUA_OK: |
| 1039 | ret = HLUA_E_OK; |
| 1040 | break; |
| 1041 | |
| 1042 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1043 | /* Check if the execution timeout is expired. It it is the case, we |
| 1044 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1045 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1046 | tv_update_date(0, 1); |
| 1047 | lua->run_time += now_ms - lua->start_time; |
| 1048 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1049 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1050 | if (!lua_checkstack(lua->T, 1)) { |
| 1051 | ret = HLUA_E_ERR; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1052 | break; |
| 1053 | } |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1054 | lua_pushfstring(lua->T, "execution timeout"); |
| 1055 | ret = HLUA_E_ERRMSG; |
| 1056 | break; |
| 1057 | } |
| 1058 | /* Process the forced yield. if the general yield is not allowed or |
| 1059 | * if no task were associated this the current Lua execution |
| 1060 | * coroutine, we resume the execution. Else we want to return in the |
| 1061 | * scheduler and we want to be waked up again, to continue the |
| 1062 | * current Lua execution. So we schedule our own task. |
| 1063 | */ |
| 1064 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1065 | if (!yield_allowed || !lua->task) |
| 1066 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1067 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1068 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1069 | if (!yield_allowed) { |
| 1070 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1071 | if (!lua_checkstack(lua->T, 1)) { |
| 1072 | ret = HLUA_E_ERR; |
| 1073 | break; |
| 1074 | } |
| 1075 | lua_pushfstring(lua->T, "yield not allowed"); |
| 1076 | ret = HLUA_E_ERRMSG; |
| 1077 | break; |
| 1078 | } |
| 1079 | ret = HLUA_E_AGAIN; |
| 1080 | break; |
| 1081 | |
| 1082 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1083 | |
| 1084 | /* Special exit case. The traditionnal exit is returned as an error |
| 1085 | * because the errors ares the only one mean to return immediately |
| 1086 | * from and lua execution. |
| 1087 | */ |
| 1088 | if (lua->flags & HLUA_EXIT) { |
| 1089 | ret = HLUA_E_OK; |
Thierry FOURNIER | e1587b3 | 2015-08-28 09:54:13 +0200 | [diff] [blame] | 1090 | hlua_ctx_renew(lua, 0); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1091 | break; |
| 1092 | } |
| 1093 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1094 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1095 | if (!lua_checkstack(lua->T, 1)) { |
| 1096 | ret = HLUA_E_ERR; |
| 1097 | break; |
| 1098 | } |
| 1099 | msg = lua_tostring(lua->T, -1); |
| 1100 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1101 | lua_pop(lua->T, 1); |
| 1102 | if (msg) |
| 1103 | lua_pushfstring(lua->T, "runtime error: %s", msg); |
| 1104 | else |
| 1105 | lua_pushfstring(lua->T, "unknown runtime error"); |
| 1106 | ret = HLUA_E_ERRMSG; |
| 1107 | break; |
| 1108 | |
| 1109 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1110 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1111 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1112 | if (!lua_checkstack(lua->T, 1)) { |
| 1113 | ret = HLUA_E_ERR; |
| 1114 | break; |
| 1115 | } |
| 1116 | lua_pushfstring(lua->T, "out of memory error"); |
| 1117 | ret = HLUA_E_ERRMSG; |
| 1118 | break; |
| 1119 | |
| 1120 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1121 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1122 | if (!lua_checkstack(lua->T, 1)) { |
| 1123 | ret = HLUA_E_ERR; |
| 1124 | break; |
| 1125 | } |
| 1126 | msg = lua_tostring(lua->T, -1); |
| 1127 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1128 | lua_pop(lua->T, 1); |
| 1129 | if (msg) |
| 1130 | lua_pushfstring(lua->T, "message handler error: %s", msg); |
| 1131 | else |
| 1132 | lua_pushfstring(lua->T, "message handler error"); |
| 1133 | ret = HLUA_E_ERRMSG; |
| 1134 | break; |
| 1135 | |
| 1136 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1137 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1138 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1139 | if (!lua_checkstack(lua->T, 1)) { |
| 1140 | ret = HLUA_E_ERR; |
| 1141 | break; |
| 1142 | } |
| 1143 | lua_pushfstring(lua->T, "unknonwn error"); |
| 1144 | ret = HLUA_E_ERRMSG; |
| 1145 | break; |
| 1146 | } |
| 1147 | |
Thierry FOURNIER | 6ab4d8e | 2015-09-27 22:17:19 +0200 | [diff] [blame] | 1148 | /* This GC permits to destroy some object when a Lua timeout strikes. */ |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1149 | if (lua->flags & HLUA_MUST_GC && |
| 1150 | ret != HLUA_E_AGAIN) |
Thierry FOURNIER | 6ab4d8e | 2015-09-27 22:17:19 +0200 | [diff] [blame] | 1151 | lua_gc(lua->T, LUA_GCCOLLECT, 0); |
| 1152 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1153 | switch (ret) { |
| 1154 | case HLUA_E_AGAIN: |
| 1155 | break; |
| 1156 | |
| 1157 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 847ca66 | 2016-12-16 13:07:22 +0100 | [diff] [blame] | 1158 | hlua_com_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1159 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1160 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1161 | break; |
| 1162 | |
| 1163 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1164 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 847ca66 | 2016-12-16 13:07:22 +0100 | [diff] [blame] | 1165 | hlua_com_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1166 | hlua_ctx_renew(lua, 0); |
| 1167 | break; |
| 1168 | |
| 1169 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1170 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 847ca66 | 2016-12-16 13:07:22 +0100 | [diff] [blame] | 1171 | hlua_com_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1172 | break; |
| 1173 | } |
| 1174 | |
| 1175 | return ret; |
| 1176 | } |
| 1177 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1178 | /* This function exit the current code. */ |
| 1179 | __LJMP static int hlua_done(lua_State *L) |
| 1180 | { |
| 1181 | struct hlua *hlua = hlua_gethlua(L); |
| 1182 | |
| 1183 | hlua->flags |= HLUA_EXIT; |
| 1184 | WILL_LJMP(lua_error(L)); |
| 1185 | |
| 1186 | return 0; |
| 1187 | } |
| 1188 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1189 | /* This function is an LUA binding. It provides a function |
| 1190 | * for deleting ACL from a referenced ACL file. |
| 1191 | */ |
| 1192 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1193 | { |
| 1194 | const char *name; |
| 1195 | const char *key; |
| 1196 | struct pat_ref *ref; |
| 1197 | |
| 1198 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1199 | |
| 1200 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1201 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1202 | |
| 1203 | ref = pat_ref_lookup(name); |
| 1204 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1205 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1206 | |
| 1207 | pat_ref_delete(ref, key); |
| 1208 | return 0; |
| 1209 | } |
| 1210 | |
| 1211 | /* This function is an LUA binding. It provides a function |
| 1212 | * for deleting map entry from a referenced map file. |
| 1213 | */ |
| 1214 | static int hlua_del_map(lua_State *L) |
| 1215 | { |
| 1216 | const char *name; |
| 1217 | const char *key; |
| 1218 | struct pat_ref *ref; |
| 1219 | |
| 1220 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1221 | |
| 1222 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1223 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1224 | |
| 1225 | ref = pat_ref_lookup(name); |
| 1226 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1227 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1228 | |
| 1229 | pat_ref_delete(ref, key); |
| 1230 | return 0; |
| 1231 | } |
| 1232 | |
| 1233 | /* This function is an LUA binding. It provides a function |
| 1234 | * for adding ACL pattern from a referenced ACL file. |
| 1235 | */ |
| 1236 | static int hlua_add_acl(lua_State *L) |
| 1237 | { |
| 1238 | const char *name; |
| 1239 | const char *key; |
| 1240 | struct pat_ref *ref; |
| 1241 | |
| 1242 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1243 | |
| 1244 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1245 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1246 | |
| 1247 | ref = pat_ref_lookup(name); |
| 1248 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1249 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1250 | |
| 1251 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1252 | pat_ref_add(ref, key, NULL, NULL); |
| 1253 | return 0; |
| 1254 | } |
| 1255 | |
| 1256 | /* This function is an LUA binding. It provides a function |
| 1257 | * for setting map pattern and sample from a referenced map |
| 1258 | * file. |
| 1259 | */ |
| 1260 | static int hlua_set_map(lua_State *L) |
| 1261 | { |
| 1262 | const char *name; |
| 1263 | const char *key; |
| 1264 | const char *value; |
| 1265 | struct pat_ref *ref; |
| 1266 | |
| 1267 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1268 | |
| 1269 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1270 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1271 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1272 | |
| 1273 | ref = pat_ref_lookup(name); |
| 1274 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1275 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1276 | |
| 1277 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1278 | pat_ref_set(ref, key, value, NULL); |
| 1279 | else |
| 1280 | pat_ref_add(ref, key, value, NULL); |
| 1281 | return 0; |
| 1282 | } |
| 1283 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1284 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1285 | * an integer or user data. This data is associated with a metatable. This |
| 1286 | * metatable have an original version registred in the global context with |
| 1287 | * the name of the object (_G[<name>] = <metable> ). |
| 1288 | * |
| 1289 | * A metable is a table that modify the standard behavior of a standard |
| 1290 | * access to the associated data. The entries of this new metatable are |
| 1291 | * defined as is: |
| 1292 | * |
| 1293 | * http://lua-users.org/wiki/MetatableEvents |
| 1294 | * |
| 1295 | * __index |
| 1296 | * |
| 1297 | * we access an absent field in a table, the result is nil. This is |
| 1298 | * true, but it is not the whole truth. Actually, such access triggers |
| 1299 | * the interpreter to look for an __index metamethod: If there is no |
| 1300 | * such method, as usually happens, then the access results in nil; |
| 1301 | * otherwise, the metamethod will provide the result. |
| 1302 | * |
| 1303 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1304 | * the key does not appear in the table, but the metatable has an __index |
| 1305 | * property: |
| 1306 | * |
| 1307 | * - if the value is a function, the function is called, passing in the |
| 1308 | * table and the key; the return value of that function is returned as |
| 1309 | * the result. |
| 1310 | * |
| 1311 | * - if the value is another table, the value of the key in that table is |
| 1312 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1313 | * table's metatable has an __index property, then it continues on up) |
| 1314 | * |
| 1315 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1316 | * |
| 1317 | * http://www.lua.org/pil/13.4.1.html |
| 1318 | * |
| 1319 | * __newindex |
| 1320 | * |
| 1321 | * Like __index, but control property assignment. |
| 1322 | * |
| 1323 | * __mode - Control weak references. A string value with one or both |
| 1324 | * of the characters 'k' and 'v' which specifies that the the |
| 1325 | * keys and/or values in the table are weak references. |
| 1326 | * |
| 1327 | * __call - Treat a table like a function. When a table is followed by |
| 1328 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1329 | * a __call key pointing to a function, that function is invoked |
| 1330 | * (passing any specified arguments) and the return value is |
| 1331 | * returned. |
| 1332 | * |
| 1333 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1334 | * called, if the metatable for myTable has a __metatable |
| 1335 | * key, the value of that key is returned instead of the |
| 1336 | * actual metatable. |
| 1337 | * |
| 1338 | * __tostring - Control string representation. When the builtin |
| 1339 | * "tostring( myTable )" function is called, if the metatable |
| 1340 | * for myTable has a __tostring property set to a function, |
| 1341 | * that function is invoked (passing myTable to it) and the |
| 1342 | * return value is used as the string representation. |
| 1343 | * |
| 1344 | * __len - Control table length. When the table length is requested using |
| 1345 | * the length operator ( '#' ), if the metatable for myTable has |
| 1346 | * a __len key pointing to a function, that function is invoked |
| 1347 | * (passing myTable to it) and the return value used as the value |
| 1348 | * of "#myTable". |
| 1349 | * |
| 1350 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1351 | * collected, if the metatable has a __gc field pointing to a |
| 1352 | * function, that function is first invoked, passing the userdata |
| 1353 | * to it. The __gc metamethod is not called for tables. |
| 1354 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1355 | * |
| 1356 | * Special metamethods for redefining standard operators: |
| 1357 | * http://www.lua.org/pil/13.1.html |
| 1358 | * |
| 1359 | * __add "+" |
| 1360 | * __sub "-" |
| 1361 | * __mul "*" |
| 1362 | * __div "/" |
| 1363 | * __unm "!" |
| 1364 | * __pow "^" |
| 1365 | * __concat ".." |
| 1366 | * |
| 1367 | * Special methods for redfining standar relations |
| 1368 | * http://www.lua.org/pil/13.2.html |
| 1369 | * |
| 1370 | * __eq "==" |
| 1371 | * __lt "<" |
| 1372 | * __le "<=" |
| 1373 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1374 | |
| 1375 | /* |
| 1376 | * |
| 1377 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1378 | * Class Map |
| 1379 | * |
| 1380 | * |
| 1381 | */ |
| 1382 | |
| 1383 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1384 | * a class session, otherwise it throws an error. |
| 1385 | */ |
| 1386 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1387 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1388 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1389 | } |
| 1390 | |
| 1391 | /* This function is the map constructor. It don't need |
| 1392 | * the class Map object. It creates and return a new Map |
| 1393 | * object. It must be called only during "body" or "init" |
| 1394 | * context because it process some filesystem accesses. |
| 1395 | */ |
| 1396 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1397 | { |
| 1398 | const char *fn; |
| 1399 | int match = PAT_MATCH_STR; |
| 1400 | struct sample_conv conv; |
| 1401 | const char *file = ""; |
| 1402 | int line = 0; |
| 1403 | lua_Debug ar; |
| 1404 | char *err = NULL; |
| 1405 | struct arg args[2]; |
| 1406 | |
| 1407 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1408 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1409 | |
| 1410 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1411 | |
| 1412 | if (lua_gettop(L) >= 2) { |
| 1413 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1414 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1415 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1416 | } |
| 1417 | |
| 1418 | /* Get Lua filename and line number. */ |
| 1419 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1420 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1421 | if (ar.currentline > 0) { /* is there info? */ |
| 1422 | file = ar.short_src; |
| 1423 | line = ar.currentline; |
| 1424 | } |
| 1425 | } |
| 1426 | |
| 1427 | /* fill fake sample_conv struct. */ |
| 1428 | conv.kw = ""; /* unused. */ |
| 1429 | conv.process = NULL; /* unused. */ |
| 1430 | conv.arg_mask = 0; /* unused. */ |
| 1431 | conv.val_args = NULL; /* unused. */ |
| 1432 | conv.out_type = SMP_T_STR; |
| 1433 | conv.private = (void *)(long)match; |
| 1434 | switch (match) { |
| 1435 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1436 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1437 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1438 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1439 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1440 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1441 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1442 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1443 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1444 | default: |
| 1445 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1446 | } |
| 1447 | |
| 1448 | /* fill fake args. */ |
| 1449 | args[0].type = ARGT_STR; |
| 1450 | args[0].data.str.str = (char *)fn; |
| 1451 | args[1].type = ARGT_STOP; |
| 1452 | |
| 1453 | /* load the map. */ |
| 1454 | if (!sample_load_map(args, &conv, file, line, &err)) { |
| 1455 | /* error case: we cant use luaL_error because we must |
| 1456 | * free the err variable. |
| 1457 | */ |
| 1458 | luaL_where(L, 1); |
| 1459 | lua_pushfstring(L, "'new': %s.", err); |
| 1460 | lua_concat(L, 2); |
| 1461 | free(err); |
| 1462 | WILL_LJMP(lua_error(L)); |
| 1463 | } |
| 1464 | |
| 1465 | /* create the lua object. */ |
| 1466 | lua_newtable(L); |
| 1467 | lua_pushlightuserdata(L, args[0].data.map); |
| 1468 | lua_rawseti(L, -2, 0); |
| 1469 | |
| 1470 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1471 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1472 | lua_setmetatable(L, -2); |
| 1473 | |
| 1474 | |
| 1475 | return 1; |
| 1476 | } |
| 1477 | |
| 1478 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1479 | { |
| 1480 | struct map_descriptor *desc; |
| 1481 | struct pattern *pat; |
| 1482 | struct sample smp; |
| 1483 | |
| 1484 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1485 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1486 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1487 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1488 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1489 | } |
| 1490 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1491 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1492 | smp.flags = SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1493 | 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] | 1494 | } |
| 1495 | |
| 1496 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1497 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1498 | if (str) |
| 1499 | lua_pushstring(L, ""); |
| 1500 | else |
| 1501 | lua_pushnil(L); |
| 1502 | return 1; |
| 1503 | } |
| 1504 | |
| 1505 | /* 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] | 1506 | 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] | 1507 | return 1; |
| 1508 | } |
| 1509 | |
| 1510 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1511 | { |
| 1512 | return _hlua_map_lookup(L, 0); |
| 1513 | } |
| 1514 | |
| 1515 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1516 | { |
| 1517 | return _hlua_map_lookup(L, 1); |
| 1518 | } |
| 1519 | |
| 1520 | /* |
| 1521 | * |
| 1522 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1523 | * Class Socket |
| 1524 | * |
| 1525 | * |
| 1526 | */ |
| 1527 | |
| 1528 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1529 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1530 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1531 | } |
| 1532 | |
| 1533 | /* This function is the handler called for each I/O on the established |
| 1534 | * connection. It is used for notify space avalaible to send or data |
| 1535 | * received. |
| 1536 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1537 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1538 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1539 | struct stream_interface *si = appctx->owner; |
Willy Tarreau | 50fe03b | 2014-11-28 13:59:31 +0100 | [diff] [blame] | 1540 | struct connection *c = objt_conn(si_opposite(si)->end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1541 | |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1542 | /* If the connection object is not avalaible, close all the |
| 1543 | * streams and wakeup everithing waiting for. |
| 1544 | */ |
| 1545 | if (!c) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1546 | si_shutw(si); |
| 1547 | si_shutr(si); |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 1548 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1549 | hlua_com_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1550 | hlua_com_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1551 | return; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1552 | } |
| 1553 | |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1554 | /* If we cant write, wakeup the pending write signals. */ |
| 1555 | if (channel_output_closed(si_ic(si))) |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1556 | hlua_com_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1557 | |
| 1558 | /* If we cant read, wakeup the pending read signals. */ |
| 1559 | if (channel_input_closed(si_oc(si))) |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1560 | hlua_com_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1561 | |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1562 | /* if the connection is not estabkished, inform the stream that we want |
| 1563 | * to be notified whenever the connection completes. |
| 1564 | */ |
| 1565 | if (!(c->flags & CO_FL_CONNECTED)) { |
| 1566 | si_applet_cant_get(si); |
| 1567 | si_applet_cant_put(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1568 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1569 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1570 | |
| 1571 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1572 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1573 | |
| 1574 | /* 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] | 1575 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1576 | hlua_com_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1577 | |
| 1578 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1579 | if (!channel_is_empty(si_oc(si))) |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1580 | hlua_com_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1581 | } |
| 1582 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1583 | /* This function is called when the "struct stream" is destroyed. |
| 1584 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1585 | * Wake all the pending signals. |
| 1586 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1587 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1588 | { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1589 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1590 | if (appctx->ctx.hlua_cosocket.socket) |
| 1591 | appctx->ctx.hlua_cosocket.socket->s = NULL; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1592 | |
| 1593 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1594 | hlua_com_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1595 | hlua_com_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1596 | } |
| 1597 | |
| 1598 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1599 | * uses this object. If the stream does not exists, just quit. |
| 1600 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1601 | * pending signal can rest in the read and write lists. destroy |
| 1602 | * it. |
| 1603 | */ |
| 1604 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1605 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1606 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1607 | struct appctx *appctx; |
| 1608 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1609 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1610 | |
| 1611 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1612 | if (!socket->s) |
| 1613 | return 0; |
| 1614 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1615 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1616 | appctx = objt_appctx(socket->s->si[0].end); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1617 | stream_shutdown(socket->s, SF_ERR_KILLED); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1618 | socket->s = NULL; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1619 | appctx->ctx.hlua_cosocket.socket = NULL; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1620 | |
| 1621 | return 0; |
| 1622 | } |
| 1623 | |
| 1624 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1625 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1626 | */ |
| 1627 | __LJMP static int hlua_socket_close(lua_State *L) |
| 1628 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1629 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1630 | struct appctx *appctx; |
| 1631 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1632 | MAY_LJMP(check_args(L, 1, "close")); |
| 1633 | |
| 1634 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1635 | if (!socket->s) |
| 1636 | return 0; |
| 1637 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1638 | /* Close the stream and remove the associated stop task. */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1639 | stream_shutdown(socket->s, SF_ERR_KILLED); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1640 | appctx = objt_appctx(socket->s->si[0].end); |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1641 | appctx->ctx.hlua_cosocket.socket = NULL; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1642 | socket->s = NULL; |
| 1643 | |
| 1644 | return 0; |
| 1645 | } |
| 1646 | |
| 1647 | /* This Lua function assumes that the stack contain three parameters. |
| 1648 | * 1 - USERDATA containing a struct socket |
| 1649 | * 2 - INTEGER with values of the macro defined below |
| 1650 | * If the integer is -1, we must read at most one line. |
| 1651 | * If the integer is -2, we ust read all the data until the |
| 1652 | * end of the stream. |
| 1653 | * If the integer is positive value, we must read a number of |
| 1654 | * bytes corresponding to this value. |
| 1655 | */ |
| 1656 | #define HLSR_READ_LINE (-1) |
| 1657 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1658 | __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] | 1659 | { |
| 1660 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1661 | int wanted = lua_tointeger(L, 2); |
| 1662 | struct hlua *hlua = hlua_gethlua(L); |
| 1663 | struct appctx *appctx; |
| 1664 | int len; |
| 1665 | int nblk; |
| 1666 | char *blk1; |
| 1667 | int len1; |
| 1668 | char *blk2; |
| 1669 | int len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1670 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1671 | struct channel *oc; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1672 | |
| 1673 | /* Check if this lua stack is schedulable. */ |
| 1674 | if (!hlua || !hlua->task) |
| 1675 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 1676 | "'frontend', 'backend' or 'task'")); |
| 1677 | |
| 1678 | /* check for connection closed. If some data where read, return it. */ |
| 1679 | if (!socket->s) |
| 1680 | goto connection_closed; |
| 1681 | |
Willy Tarreau | 94aa617 | 2015-03-13 14:19:06 +0100 | [diff] [blame] | 1682 | oc = &socket->s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1683 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1684 | /* Read line. */ |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1685 | nblk = bo_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1686 | if (nblk < 0) /* Connection close. */ |
| 1687 | goto connection_closed; |
| 1688 | if (nblk == 0) /* No data avalaible. */ |
| 1689 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1690 | |
| 1691 | /* remove final \r\n. */ |
| 1692 | if (nblk == 1) { |
| 1693 | if (blk1[len1-1] == '\n') { |
| 1694 | len1--; |
| 1695 | skip_at_end++; |
| 1696 | if (blk1[len1-1] == '\r') { |
| 1697 | len1--; |
| 1698 | skip_at_end++; |
| 1699 | } |
| 1700 | } |
| 1701 | } |
| 1702 | else { |
| 1703 | if (blk2[len2-1] == '\n') { |
| 1704 | len2--; |
| 1705 | skip_at_end++; |
| 1706 | if (blk2[len2-1] == '\r') { |
| 1707 | len2--; |
| 1708 | skip_at_end++; |
| 1709 | } |
| 1710 | } |
| 1711 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1712 | } |
| 1713 | |
| 1714 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1715 | /* Read all the available data. */ |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1716 | nblk = bo_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1717 | if (nblk < 0) /* Connection close. */ |
| 1718 | goto connection_closed; |
| 1719 | if (nblk == 0) /* No data avalaible. */ |
| 1720 | goto connection_empty; |
| 1721 | } |
| 1722 | |
| 1723 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1724 | /* Read a block of data. */ |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1725 | nblk = bo_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1726 | if (nblk < 0) /* Connection close. */ |
| 1727 | goto connection_closed; |
| 1728 | if (nblk == 0) /* No data avalaible. */ |
| 1729 | goto connection_empty; |
| 1730 | |
| 1731 | if (len1 > wanted) { |
| 1732 | nblk = 1; |
| 1733 | len1 = wanted; |
| 1734 | } if (nblk == 2 && len1 + len2 > wanted) |
| 1735 | len2 = wanted - len1; |
| 1736 | } |
| 1737 | |
| 1738 | len = len1; |
| 1739 | |
| 1740 | luaL_addlstring(&socket->b, blk1, len1); |
| 1741 | if (nblk == 2) { |
| 1742 | len += len2; |
| 1743 | luaL_addlstring(&socket->b, blk2, len2); |
| 1744 | } |
| 1745 | |
| 1746 | /* Consume data. */ |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1747 | bo_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1748 | |
| 1749 | /* Don't wait anything. */ |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 1750 | stream_int_notify(&socket->s->si[0]); |
| 1751 | stream_int_update_applet(&socket->s->si[0]); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1752 | |
| 1753 | /* If the pattern reclaim to read all the data |
| 1754 | * in the connection, got out. |
| 1755 | */ |
| 1756 | if (wanted == HLSR_READ_ALL) |
| 1757 | goto connection_empty; |
| 1758 | else if (wanted >= 0 && len < wanted) |
| 1759 | goto connection_empty; |
| 1760 | |
| 1761 | /* Return result. */ |
| 1762 | luaL_pushresult(&socket->b); |
| 1763 | return 1; |
| 1764 | |
| 1765 | connection_closed: |
| 1766 | |
| 1767 | /* If the buffer containds data. */ |
| 1768 | if (socket->b.n > 0) { |
| 1769 | luaL_pushresult(&socket->b); |
| 1770 | return 1; |
| 1771 | } |
| 1772 | lua_pushnil(L); |
| 1773 | lua_pushstring(L, "connection closed."); |
| 1774 | return 2; |
| 1775 | |
| 1776 | connection_empty: |
| 1777 | |
| 1778 | appctx = objt_appctx(socket->s->si[0].end); |
Thierry FOURNIER | 847ca66 | 2016-12-16 13:07:22 +0100 | [diff] [blame] | 1779 | 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] | 1780 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 1781 | 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] | 1782 | return 0; |
| 1783 | } |
| 1784 | |
| 1785 | /* This Lus function gets two parameters. The first one can be string |
| 1786 | * or a number. If the string is "*l", the user require one line. If |
| 1787 | * the string is "*a", the user require all the content of the stream. |
| 1788 | * If the value is a number, the user require a number of bytes equal |
| 1789 | * to the value. The default value is "*l" (a line). |
| 1790 | * |
| 1791 | * This paraeter with a variable type is converted in integer. This |
| 1792 | * integer takes this values: |
| 1793 | * -1 : read a line |
| 1794 | * -2 : read all the stream |
| 1795 | * >0 : amount if bytes. |
| 1796 | * |
| 1797 | * The second parameter is optinal. It contains a string that must be |
| 1798 | * concatenated with the read data. |
| 1799 | */ |
| 1800 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 1801 | { |
| 1802 | int wanted = HLSR_READ_LINE; |
| 1803 | const char *pattern; |
| 1804 | int type; |
| 1805 | char *error; |
| 1806 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1807 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1808 | |
| 1809 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 1810 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 1811 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1812 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1813 | |
| 1814 | /* check for pattern. */ |
| 1815 | if (lua_gettop(L) >= 2) { |
| 1816 | type = lua_type(L, 2); |
| 1817 | if (type == LUA_TSTRING) { |
| 1818 | pattern = lua_tostring(L, 2); |
| 1819 | if (strcmp(pattern, "*a") == 0) |
| 1820 | wanted = HLSR_READ_ALL; |
| 1821 | else if (strcmp(pattern, "*l") == 0) |
| 1822 | wanted = HLSR_READ_LINE; |
| 1823 | else { |
| 1824 | wanted = strtoll(pattern, &error, 10); |
| 1825 | if (*error != '\0') |
| 1826 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 1827 | } |
| 1828 | } |
| 1829 | else if (type == LUA_TNUMBER) { |
| 1830 | wanted = lua_tointeger(L, 2); |
| 1831 | if (wanted < 0) |
| 1832 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 1833 | } |
| 1834 | } |
| 1835 | |
| 1836 | /* Set pattern. */ |
| 1837 | lua_pushinteger(L, wanted); |
| 1838 | lua_replace(L, 2); |
| 1839 | |
| 1840 | /* init bufffer, and fiil it wih prefix. */ |
| 1841 | luaL_buffinit(L, &socket->b); |
| 1842 | |
| 1843 | /* Check prefix. */ |
| 1844 | if (lua_gettop(L) >= 3) { |
| 1845 | if (lua_type(L, 3) != LUA_TSTRING) |
| 1846 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 1847 | pattern = lua_tolstring(L, 3, &len); |
| 1848 | luaL_addlstring(&socket->b, pattern, len); |
| 1849 | } |
| 1850 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1851 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1852 | } |
| 1853 | |
| 1854 | /* Write the Lua input string in the output buffer. |
| 1855 | * This fucntion returns a yield if no space are available. |
| 1856 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1857 | 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] | 1858 | { |
| 1859 | struct hlua_socket *socket; |
| 1860 | struct hlua *hlua = hlua_gethlua(L); |
| 1861 | struct appctx *appctx; |
| 1862 | size_t buf_len; |
| 1863 | const char *buf; |
| 1864 | int len; |
| 1865 | int send_len; |
| 1866 | int sent; |
| 1867 | |
| 1868 | /* Check if this lua stack is schedulable. */ |
| 1869 | if (!hlua || !hlua->task) |
| 1870 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 1871 | "'frontend', 'backend' or 'task'")); |
| 1872 | |
| 1873 | /* Get object */ |
| 1874 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1875 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1876 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1877 | |
| 1878 | /* Check for connection close. */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1879 | if (!socket->s || channel_output_closed(&socket->s->req)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1880 | lua_pushinteger(L, -1); |
| 1881 | return 1; |
| 1882 | } |
| 1883 | |
| 1884 | /* Update the input buffer data. */ |
| 1885 | buf += sent; |
| 1886 | send_len = buf_len - sent; |
| 1887 | |
| 1888 | /* All the data are sent. */ |
| 1889 | if (sent >= buf_len) |
| 1890 | return 1; /* Implicitly return the length sent. */ |
| 1891 | |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 1892 | /* Check if the buffer is avalaible because HAProxy doesn't allocate |
| 1893 | * the request buffer if its not required. |
| 1894 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1895 | if (socket->s->req.buf->size == 0) { |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 1896 | appctx = hlua->task->context; |
| 1897 | if (!channel_alloc_buffer(&socket->s->req, &appctx->buffer_wait)) |
| 1898 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 1899 | } |
| 1900 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1901 | /* Check for avalaible space. */ |
Willy Tarreau | 94aa617 | 2015-03-13 14:19:06 +0100 | [diff] [blame] | 1902 | len = buffer_total_space(socket->s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 1903 | if (len <= 0) { |
| 1904 | appctx = objt_appctx(socket->s->si[0].end); |
| 1905 | if (!hlua_com_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) |
| 1906 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1907 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 1908 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1909 | |
| 1910 | /* send data */ |
| 1911 | if (len < send_len) |
| 1912 | send_len = len; |
Willy Tarreau | 94aa617 | 2015-03-13 14:19:06 +0100 | [diff] [blame] | 1913 | len = bi_putblk(&socket->s->req, buf+sent, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1914 | |
| 1915 | /* "Not enough space" (-1), "Buffer too little to contain |
| 1916 | * the data" (-2) are not expected because the available length |
| 1917 | * is tested. |
| 1918 | * Other unknown error are also not expected. |
| 1919 | */ |
| 1920 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 1921 | if (len == -1) |
Willy Tarreau | 94aa617 | 2015-03-13 14:19:06 +0100 | [diff] [blame] | 1922 | socket->s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 1923 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1924 | MAY_LJMP(hlua_socket_close(L)); |
| 1925 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1926 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1927 | return 1; |
| 1928 | } |
| 1929 | |
| 1930 | /* update buffers. */ |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 1931 | stream_int_notify(&socket->s->si[0]); |
| 1932 | stream_int_update_applet(&socket->s->si[0]); |
| 1933 | |
Willy Tarreau | 94aa617 | 2015-03-13 14:19:06 +0100 | [diff] [blame] | 1934 | socket->s->req.rex = TICK_ETERNITY; |
| 1935 | socket->s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1936 | |
| 1937 | /* Update length sent. */ |
| 1938 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1939 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1940 | |
| 1941 | /* All the data buffer is sent ? */ |
| 1942 | if (sent + len >= buf_len) |
| 1943 | return 1; |
| 1944 | |
| 1945 | hlua_socket_write_yield_return: |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 1946 | 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] | 1947 | return 0; |
| 1948 | } |
| 1949 | |
| 1950 | /* This function initiate the send of data. It just check the input |
| 1951 | * parameters and push an integer in the Lua stack that contain the |
| 1952 | * amount of data writed in the buffer. This is used by the function |
| 1953 | * "hlua_socket_write_yield" that can yield. |
| 1954 | * |
| 1955 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 1956 | * the associated object. The second is a string buffer. The third is |
| 1957 | * a facultative integer that represents where is the buffer position |
| 1958 | * of the start of the data that can send. The first byte is the |
| 1959 | * position "1". The default value is "1". The fourth argument is a |
| 1960 | * facultative integer that represents where is the buffer position |
| 1961 | * of the end of the data that can send. The default is the last byte. |
| 1962 | */ |
| 1963 | static int hlua_socket_send(struct lua_State *L) |
| 1964 | { |
| 1965 | int i; |
| 1966 | int j; |
| 1967 | const char *buf; |
| 1968 | size_t buf_len; |
| 1969 | |
| 1970 | /* Check number of arguments. */ |
| 1971 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 1972 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 1973 | |
| 1974 | /* Get the string. */ |
| 1975 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 1976 | |
| 1977 | /* Get and check j. */ |
| 1978 | if (lua_gettop(L) == 4) { |
| 1979 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 1980 | if (j < 0) |
| 1981 | j = buf_len + j + 1; |
| 1982 | if (j > buf_len) |
| 1983 | j = buf_len + 1; |
| 1984 | lua_pop(L, 1); |
| 1985 | } |
| 1986 | else |
| 1987 | j = buf_len; |
| 1988 | |
| 1989 | /* Get and check i. */ |
| 1990 | if (lua_gettop(L) == 3) { |
| 1991 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 1992 | if (i < 0) |
| 1993 | i = buf_len + i + 1; |
| 1994 | if (i > buf_len) |
| 1995 | i = buf_len + 1; |
| 1996 | lua_pop(L, 1); |
| 1997 | } else |
| 1998 | i = 1; |
| 1999 | |
| 2000 | /* Check bth i and j. */ |
| 2001 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2002 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2003 | return 1; |
| 2004 | } |
| 2005 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2006 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2007 | return 1; |
| 2008 | } |
| 2009 | if (i == 0) |
| 2010 | i = 1; |
| 2011 | if (j == 0) |
| 2012 | j = 1; |
| 2013 | |
| 2014 | /* Pop the string. */ |
| 2015 | lua_pop(L, 1); |
| 2016 | |
| 2017 | /* Update the buffer length. */ |
| 2018 | buf += i - 1; |
| 2019 | buf_len = j - i + 1; |
| 2020 | lua_pushlstring(L, buf, buf_len); |
| 2021 | |
| 2022 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2023 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2024 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2025 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2026 | } |
| 2027 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2028 | #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] | 2029 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2030 | { |
| 2031 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2032 | int ret; |
| 2033 | int len; |
| 2034 | char *p; |
| 2035 | |
| 2036 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2037 | if (ret <= 0) { |
| 2038 | lua_pushnil(L); |
| 2039 | return 1; |
| 2040 | } |
| 2041 | |
| 2042 | if (ret == AF_UNIX) { |
| 2043 | lua_pushstring(L, buffer+1); |
| 2044 | return 1; |
| 2045 | } |
| 2046 | else if (ret == AF_INET6) { |
| 2047 | buffer[0] = '['; |
| 2048 | len = strlen(buffer); |
| 2049 | buffer[len] = ']'; |
| 2050 | len++; |
| 2051 | buffer[len] = ':'; |
| 2052 | len++; |
| 2053 | p = buffer; |
| 2054 | } |
| 2055 | else if (ret == AF_INET) { |
| 2056 | p = buffer + 1; |
| 2057 | len = strlen(p); |
| 2058 | p[len] = ':'; |
| 2059 | len++; |
| 2060 | } |
| 2061 | else { |
| 2062 | lua_pushnil(L); |
| 2063 | return 1; |
| 2064 | } |
| 2065 | |
| 2066 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2067 | lua_pushnil(L); |
| 2068 | return 1; |
| 2069 | } |
| 2070 | |
| 2071 | lua_pushstring(L, p); |
| 2072 | return 1; |
| 2073 | } |
| 2074 | |
| 2075 | /* Returns information about the peer of the connection. */ |
| 2076 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2077 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2078 | struct hlua_socket *socket; |
| 2079 | struct connection *conn; |
| 2080 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2081 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2082 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2083 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2084 | |
| 2085 | /* Check if the tcp object is avalaible. */ |
| 2086 | if (!socket->s) { |
| 2087 | lua_pushnil(L); |
| 2088 | return 1; |
| 2089 | } |
| 2090 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2091 | conn = objt_conn(socket->s->si[1].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2092 | if (!conn) { |
| 2093 | lua_pushnil(L); |
| 2094 | return 1; |
| 2095 | } |
| 2096 | |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2097 | conn_get_to_addr(conn); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2098 | if (!(conn->flags & CO_FL_ADDR_TO_SET)) { |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2099 | lua_pushnil(L); |
| 2100 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2101 | } |
| 2102 | |
| 2103 | return MAY_LJMP(hlua_socket_info(L, &conn->addr.to)); |
| 2104 | } |
| 2105 | |
| 2106 | /* Returns information about my connection side. */ |
| 2107 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2108 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2109 | struct hlua_socket *socket; |
| 2110 | struct connection *conn; |
| 2111 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2112 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2113 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2114 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2115 | |
| 2116 | /* Check if the tcp object is avalaible. */ |
| 2117 | if (!socket->s) { |
| 2118 | lua_pushnil(L); |
| 2119 | return 1; |
| 2120 | } |
| 2121 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2122 | conn = objt_conn(socket->s->si[1].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2123 | if (!conn) { |
| 2124 | lua_pushnil(L); |
| 2125 | return 1; |
| 2126 | } |
| 2127 | |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2128 | conn_get_from_addr(conn); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2129 | if (!(conn->flags & CO_FL_ADDR_FROM_SET)) { |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2130 | lua_pushnil(L); |
| 2131 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2132 | } |
| 2133 | |
| 2134 | return hlua_socket_info(L, &conn->addr.from); |
| 2135 | } |
| 2136 | |
| 2137 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2138 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2139 | .obj_type = OBJ_TYPE_APPLET, |
| 2140 | .name = "<LUA_TCP>", |
| 2141 | .fct = hlua_socket_handler, |
| 2142 | .release = hlua_socket_release, |
| 2143 | }; |
| 2144 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2145 | __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] | 2146 | { |
| 2147 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2148 | struct hlua *hlua = hlua_gethlua(L); |
| 2149 | struct appctx *appctx; |
| 2150 | |
| 2151 | /* Check for connection close. */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2152 | if (!hlua || !socket->s || channel_output_closed(&socket->s->req)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2153 | lua_pushnil(L); |
| 2154 | lua_pushstring(L, "Can't connect"); |
| 2155 | return 2; |
| 2156 | } |
| 2157 | |
| 2158 | appctx = objt_appctx(socket->s->si[0].end); |
| 2159 | |
| 2160 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2161 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2162 | lua_pushinteger(L, 1); |
| 2163 | return 1; |
| 2164 | } |
| 2165 | |
Thierry FOURNIER | 847ca66 | 2016-12-16 13:07:22 +0100 | [diff] [blame] | 2166 | 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] | 2167 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 2168 | 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] | 2169 | return 0; |
| 2170 | } |
| 2171 | |
| 2172 | /* This function fail or initite the connection. */ |
| 2173 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2174 | { |
| 2175 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2176 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2177 | const char *ip; |
| 2178 | struct connection *conn; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2179 | struct hlua *hlua; |
| 2180 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2181 | int low, high; |
| 2182 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2183 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2184 | if (lua_gettop(L) < 2) |
| 2185 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2186 | |
| 2187 | /* Get args. */ |
| 2188 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2189 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2190 | if (lua_gettop(L) >= 3) |
| 2191 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2192 | |
Willy Tarreau | 973a542 | 2015-08-05 21:47:23 +0200 | [diff] [blame] | 2193 | conn = si_alloc_conn(&socket->s->si[1]); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2194 | if (!conn) |
| 2195 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
| 2196 | |
Willy Tarreau | 3adac08 | 2015-09-26 17:51:09 +0200 | [diff] [blame] | 2197 | /* needed for the connection not to be closed */ |
| 2198 | conn->target = socket->s->target; |
| 2199 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2200 | /* Parse ip address. */ |
Willy Tarreau | 48ef4c9 | 2017-01-06 18:32:38 +0100 | [diff] [blame] | 2201 | addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2202 | if (!addr) |
| 2203 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
| 2204 | if (low != high) |
| 2205 | WILL_LJMP(luaL_error(L, "connect: port ranges not supported : address '%s'", ip)); |
| 2206 | memcpy(&conn->addr.to, addr, sizeof(struct sockaddr_storage)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2207 | |
| 2208 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2209 | if (low == 0) { |
| 2210 | if (conn->addr.to.ss_family == AF_INET) { |
| 2211 | if (port == -1) |
| 2212 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
| 2213 | ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(port); |
| 2214 | } else if (conn->addr.to.ss_family == AF_INET6) { |
| 2215 | if (port == -1) |
| 2216 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
| 2217 | ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(port); |
| 2218 | } |
| 2219 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2220 | |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2221 | hlua = hlua_gethlua(L); |
| 2222 | appctx = objt_appctx(socket->s->si[0].end); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2223 | |
| 2224 | /* inform the stream that we want to be notified whenever the |
| 2225 | * connection completes. |
| 2226 | */ |
| 2227 | si_applet_cant_get(&socket->s->si[0]); |
| 2228 | si_applet_cant_put(&socket->s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2229 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2230 | |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2231 | hlua->flags |= HLUA_MUST_GC; |
| 2232 | |
Thierry FOURNIER | 847ca66 | 2016-12-16 13:07:22 +0100 | [diff] [blame] | 2233 | 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] | 2234 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 2235 | 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] | 2236 | |
| 2237 | return 0; |
| 2238 | } |
| 2239 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2240 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2241 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2242 | { |
| 2243 | struct hlua_socket *socket; |
| 2244 | |
| 2245 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2246 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2247 | socket->s->target = &socket_ssl.obj_type; |
| 2248 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2249 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2250 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2251 | |
| 2252 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2253 | { |
| 2254 | return 0; |
| 2255 | } |
| 2256 | |
| 2257 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2258 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2259 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2260 | int tmout; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2261 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2262 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2263 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2264 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2265 | tmout = MAY_LJMP(luaL_checkinteger(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2266 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2267 | socket->s->req.rto = tmout; |
| 2268 | socket->s->req.wto = tmout; |
| 2269 | socket->s->res.rto = tmout; |
| 2270 | socket->s->res.wto = tmout; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2271 | |
| 2272 | return 0; |
| 2273 | } |
| 2274 | |
| 2275 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2276 | { |
| 2277 | struct hlua_socket *socket; |
| 2278 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2279 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2280 | struct stream *strm; |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2281 | struct task *task; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2282 | |
| 2283 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2284 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2285 | hlua_pusherror(L, "socket: full stack"); |
| 2286 | goto out_fail_conf; |
| 2287 | } |
| 2288 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2289 | /* Create the object: obj[0] = userdata. */ |
| 2290 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2291 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2292 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2293 | memset(socket, 0, sizeof(*socket)); |
| 2294 | |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2295 | /* Check if the various memory pools are intialized. */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2296 | if (!pool2_stream || !pool2_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2297 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2298 | goto out_fail_conf; |
| 2299 | } |
| 2300 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2301 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2302 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2303 | lua_setmetatable(L, -2); |
| 2304 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2305 | /* Create the applet context */ |
| 2306 | appctx = appctx_new(&update_applet); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2307 | if (!appctx) { |
| 2308 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2309 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2310 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2311 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2312 | appctx->ctx.hlua_cosocket.socket = socket; |
| 2313 | appctx->ctx.hlua_cosocket.connected = 0; |
| 2314 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 2315 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2316 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2317 | /* Now create a session, task and stream for this applet */ |
| 2318 | sess = session_new(&socket_proxy, NULL, &appctx->obj_type); |
| 2319 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2320 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2321 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2322 | } |
| 2323 | |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2324 | task = task_new(); |
| 2325 | if (!task) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2326 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2327 | goto out_fail_task; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2328 | } |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2329 | task->nice = 0; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2330 | |
Willy Tarreau | 73b65ac | 2015-04-08 18:26:29 +0200 | [diff] [blame] | 2331 | strm = stream_new(sess, task, &appctx->obj_type); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2332 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2333 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2334 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2335 | } |
| 2336 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2337 | /* Configure an empty Lua for the stream. */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2338 | socket->s = strm; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 2339 | strm->hlua = NULL; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2340 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2341 | /* Configure "right" stream interface. this "si" is used to connect |
| 2342 | * and retrieve data from the server. The connection is initialized |
| 2343 | * with the "struct server". |
| 2344 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2345 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2346 | |
| 2347 | /* Force destination server. */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2348 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET | SF_BE_ASSIGNED; |
| 2349 | strm->target = &socket_tcp.obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2350 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2351 | /* Update statistics counters. */ |
| 2352 | socket_proxy.feconn++; /* beconn will be increased later */ |
| 2353 | jobs++; |
| 2354 | totalconn++; |
| 2355 | |
| 2356 | /* Return yield waiting for connection. */ |
| 2357 | return 1; |
| 2358 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2359 | out_fail_stream: |
| 2360 | task_free(task); |
| 2361 | out_fail_task: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2362 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2363 | out_fail_sess: |
| 2364 | appctx_free(appctx); |
| 2365 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2366 | WILL_LJMP(lua_error(L)); |
| 2367 | return 0; |
| 2368 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2369 | |
| 2370 | /* |
| 2371 | * |
| 2372 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2373 | * Class Channel |
| 2374 | * |
| 2375 | * |
| 2376 | */ |
| 2377 | |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2378 | /* The state between the channel data and the HTTP parser state can be |
| 2379 | * unconsistent, so reset the parser and call it again. Warning, this |
| 2380 | * action not revalidate the request and not send a 400 if the modified |
| 2381 | * resuest is not valid. |
| 2382 | * |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2383 | * This function never fails. The direction is set using dir, which equals |
| 2384 | * either SMP_OPT_DIR_REQ or SMP_OPT_DIR_RES. |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2385 | */ |
| 2386 | static void hlua_resynchonize_proto(struct stream *stream, int dir) |
| 2387 | { |
| 2388 | /* Protocol HTTP. */ |
| 2389 | if (stream->be->mode == PR_MODE_HTTP) { |
| 2390 | |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2391 | if (dir == SMP_OPT_DIR_REQ) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2392 | http_txn_reset_req(stream->txn); |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2393 | else if (dir == SMP_OPT_DIR_RES) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2394 | http_txn_reset_res(stream->txn); |
| 2395 | |
| 2396 | if (stream->txn->hdr_idx.v) |
| 2397 | hdr_idx_init(&stream->txn->hdr_idx); |
| 2398 | |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2399 | if (dir == SMP_OPT_DIR_REQ) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2400 | http_msg_analyzer(&stream->txn->req, &stream->txn->hdr_idx); |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2401 | else if (dir == SMP_OPT_DIR_RES) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2402 | http_msg_analyzer(&stream->txn->rsp, &stream->txn->hdr_idx); |
| 2403 | } |
| 2404 | } |
| 2405 | |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2406 | /* This function is called before the Lua execution. It stores |
| 2407 | * the differents parsers state before executing some Lua code. |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2408 | */ |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2409 | 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] | 2410 | { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2411 | c->mode = stream->be->mode; |
| 2412 | switch (c->mode) { |
| 2413 | case PR_MODE_HTTP: |
| 2414 | c->data.http.dir = opt & SMP_OPT_DIR; |
| 2415 | if (c->data.http.dir == SMP_OPT_DIR_REQ) |
| 2416 | c->data.http.state = stream->txn->req.msg_state; |
| 2417 | else |
| 2418 | c->data.http.state = stream->txn->rsp.msg_state; |
| 2419 | break; |
| 2420 | default: |
| 2421 | break; |
| 2422 | } |
| 2423 | } |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2424 | |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2425 | /* This function is called after the Lua execution. it |
| 2426 | * returns true if the parser state is consistent, otherwise, |
| 2427 | * it return false. |
| 2428 | * |
| 2429 | * In HTTP mode, the parser state must be in the same state |
| 2430 | * or greater when we exit the function. Even if we do a |
| 2431 | * control yield. This prevent to break the HTTP message |
| 2432 | * from the Lua code. |
| 2433 | */ |
| 2434 | static inline int consistency_check(struct stream *stream, int opt, struct hlua_consistency *c) |
| 2435 | { |
| 2436 | if (c->mode != stream->be->mode) |
| 2437 | return 0; |
| 2438 | |
| 2439 | switch (c->mode) { |
| 2440 | case PR_MODE_HTTP: |
| 2441 | if (c->data.http.dir != (opt & SMP_OPT_DIR)) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2442 | return 0; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2443 | if (c->data.http.dir == SMP_OPT_DIR_REQ) |
| 2444 | return stream->txn->req.msg_state >= c->data.http.state; |
| 2445 | else |
| 2446 | return stream->txn->rsp.msg_state >= c->data.http.state; |
| 2447 | default: |
| 2448 | return 1; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2449 | } |
| 2450 | return 1; |
| 2451 | } |
| 2452 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2453 | /* Returns the struct hlua_channel join to the class channel in the |
| 2454 | * stack entry "ud" or throws an argument error. |
| 2455 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2456 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2457 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2458 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2459 | } |
| 2460 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2461 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 2462 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2463 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 2464 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2465 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2466 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2467 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2468 | return 0; |
| 2469 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2470 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2471 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2472 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2473 | |
| 2474 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2475 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 2476 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2477 | return 1; |
| 2478 | } |
| 2479 | |
| 2480 | /* Duplicate all the data present in the input channel and put it |
| 2481 | * in a string LUA variables. Returns -1 and push a nil value in |
| 2482 | * the stack if the channel is closed and all the data are consumed, |
| 2483 | * returns 0 if no data are available, otherwise it returns the length |
| 2484 | * of the builded string. |
| 2485 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2486 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2487 | { |
| 2488 | char *blk1; |
| 2489 | char *blk2; |
| 2490 | int len1; |
| 2491 | int len2; |
| 2492 | int ret; |
| 2493 | luaL_Buffer b; |
| 2494 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2495 | ret = bi_getblk_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2496 | if (unlikely(ret == 0)) |
| 2497 | return 0; |
| 2498 | |
| 2499 | if (unlikely(ret < 0)) { |
| 2500 | lua_pushnil(L); |
| 2501 | return -1; |
| 2502 | } |
| 2503 | |
| 2504 | luaL_buffinit(L, &b); |
| 2505 | luaL_addlstring(&b, blk1, len1); |
| 2506 | if (unlikely(ret == 2)) |
| 2507 | luaL_addlstring(&b, blk2, len2); |
| 2508 | luaL_pushresult(&b); |
| 2509 | |
| 2510 | if (unlikely(ret == 2)) |
| 2511 | return len1 + len2; |
| 2512 | return len1; |
| 2513 | } |
| 2514 | |
| 2515 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2516 | * a yield. This function keep the data in the buffer. |
| 2517 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2518 | __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] | 2519 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2520 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2521 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2522 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2523 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2524 | if (_hlua_channel_dup(chn, L) == 0) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2525 | 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] | 2526 | return 1; |
| 2527 | } |
| 2528 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2529 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 2530 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 2531 | { |
| 2532 | MAY_LJMP(check_args(L, 1, "dup")); |
| 2533 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2534 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 2535 | } |
| 2536 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2537 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2538 | * a yield. This function consumes the data in the buffer. It returns |
| 2539 | * a string containing the data or a nil pointer if no data are available |
| 2540 | * and the channel is closed. |
| 2541 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2542 | __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] | 2543 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2544 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2545 | int ret; |
| 2546 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2547 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2548 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2549 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2550 | if (unlikely(ret == 0)) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2551 | 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] | 2552 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2553 | if (unlikely(ret == -1)) |
| 2554 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2555 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2556 | chn->buf->i -= ret; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2557 | hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2558 | return 1; |
| 2559 | } |
| 2560 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2561 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 2562 | __LJMP static int hlua_channel_get(lua_State *L) |
| 2563 | { |
| 2564 | MAY_LJMP(check_args(L, 1, "get")); |
| 2565 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2566 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 2567 | } |
| 2568 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2569 | /* This functions consumes and returns one line. If the channel is closed, |
| 2570 | * and the last data does not contains a final '\n', the data are returned |
| 2571 | * without the final '\n'. When no more data are avalaible, it returns nil |
| 2572 | * value. |
| 2573 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2574 | __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] | 2575 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2576 | char *blk1; |
| 2577 | char *blk2; |
| 2578 | int len1; |
| 2579 | int len2; |
| 2580 | int len; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2581 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2582 | int ret; |
| 2583 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2584 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2585 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2586 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2587 | ret = bi_getline_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2588 | if (ret == 0) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2589 | 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] | 2590 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2591 | if (ret == -1) { |
| 2592 | lua_pushnil(L); |
| 2593 | return 1; |
| 2594 | } |
| 2595 | |
| 2596 | luaL_buffinit(L, &b); |
| 2597 | luaL_addlstring(&b, blk1, len1); |
| 2598 | len = len1; |
| 2599 | if (unlikely(ret == 2)) { |
| 2600 | luaL_addlstring(&b, blk2, len2); |
| 2601 | len += len2; |
| 2602 | } |
| 2603 | luaL_pushresult(&b); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2604 | 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] | 2605 | hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2606 | return 1; |
| 2607 | } |
| 2608 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2609 | /* Check arguments for the fucntion "hlua_channel_getline_yield". */ |
| 2610 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 2611 | { |
| 2612 | MAY_LJMP(check_args(L, 1, "getline")); |
| 2613 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2614 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 2615 | } |
| 2616 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2617 | /* This function takes a string as input, and append it at the |
| 2618 | * input side of channel. If the data is too big, but a space |
| 2619 | * is probably available after sending some data, the function |
| 2620 | * yield. If the data is bigger than the buffer, or if the |
| 2621 | * channel is closed, it returns -1. otherwise, it returns the |
| 2622 | * amount of data writed. |
| 2623 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2624 | __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] | 2625 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2626 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2627 | size_t len; |
| 2628 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2629 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2630 | int ret; |
| 2631 | int max; |
| 2632 | |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2633 | /* Check if the buffer is avalaible because HAProxy doesn't allocate |
| 2634 | * the request buffer if its not required. |
| 2635 | */ |
| 2636 | if (chn->buf->size == 0) { |
| 2637 | si_applet_cant_put(chn_prod(chn)); |
| 2638 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0)); |
| 2639 | } |
| 2640 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2641 | max = channel_recv_limit(chn) - buffer_len(chn->buf); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2642 | if (max > len - l) |
| 2643 | max = len - l; |
| 2644 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2645 | ret = bi_putblk(chn, str + l, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2646 | if (ret == -2 || ret == -3) { |
| 2647 | lua_pushinteger(L, -1); |
| 2648 | return 1; |
| 2649 | } |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2650 | if (ret == -1) { |
| 2651 | chn->flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2652 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0)); |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2653 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2654 | l += ret; |
| 2655 | lua_pop(L, 1); |
| 2656 | lua_pushinteger(L, l); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2657 | hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2658 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2659 | max = channel_recv_limit(chn) - buffer_len(chn->buf); |
| 2660 | if (max == 0 && chn->buf->o == 0) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2661 | /* There are no space avalaible, and the output buffer is empty. |
| 2662 | * in this case, we cannot add more data, so we cannot yield, |
| 2663 | * we return the amount of copyied data. |
| 2664 | */ |
| 2665 | return 1; |
| 2666 | } |
| 2667 | if (l < len) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2668 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2669 | return 1; |
| 2670 | } |
| 2671 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2672 | /* just a wrapper of "hlua_channel_append_yield". It returns the length |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2673 | * of the writed string, or -1 if the channel is closed or if the |
| 2674 | * buffer size is too little for the data. |
| 2675 | */ |
| 2676 | __LJMP static int hlua_channel_append(lua_State *L) |
| 2677 | { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2678 | size_t len; |
| 2679 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2680 | MAY_LJMP(check_args(L, 2, "append")); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2681 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2682 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2683 | MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2684 | lua_pushinteger(L, 0); |
| 2685 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2686 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2687 | } |
| 2688 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2689 | /* just a wrapper of "hlua_channel_append_yield". This wrapper starts |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2690 | * his process by cleaning the buffer. The result is a replacement |
| 2691 | * of the current data. It returns the length of the writed string, |
| 2692 | * or -1 if the channel is closed or if the buffer size is too |
| 2693 | * little for the data. |
| 2694 | */ |
| 2695 | __LJMP static int hlua_channel_set(lua_State *L) |
| 2696 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2697 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2698 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2699 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2700 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2701 | lua_pushinteger(L, 0); |
| 2702 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2703 | chn->buf->i = 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2704 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2705 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2706 | } |
| 2707 | |
| 2708 | /* Append data in the output side of the buffer. This data is immediatly |
| 2709 | * sent. The fcuntion returns the ammount of data writed. If the buffer |
| 2710 | * cannot contains the data, the function yield. The function returns -1 |
| 2711 | * if the channel is closed. |
| 2712 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2713 | __LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2714 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2715 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2716 | size_t len; |
| 2717 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2718 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2719 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2720 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2721 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2722 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2723 | lua_pushinteger(L, -1); |
| 2724 | return 1; |
| 2725 | } |
| 2726 | |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 2727 | /* Check if the buffer is avalaible because HAProxy doesn't allocate |
| 2728 | * the request buffer if its not required. |
| 2729 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2730 | if (chn->buf->size == 0) { |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2731 | si_applet_cant_put(chn_prod(chn)); |
| 2732 | 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] | 2733 | } |
| 2734 | |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2735 | /* the writed data will be immediatly sent, so we can check |
| 2736 | * the avalaible space without taking in account the reserve. |
| 2737 | * The reserve is guaranted for the processing of incoming |
| 2738 | * data, because the buffer will be flushed. |
| 2739 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2740 | max = chn->buf->size - buffer_len(chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2741 | |
| 2742 | /* If there are no space avalaible, and the output buffer is empty. |
| 2743 | * in this case, we cannot add more data, so we cannot yield, |
| 2744 | * we return the amount of copyied data. |
| 2745 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2746 | if (max == 0 && chn->buf->o == 0) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2747 | return 1; |
| 2748 | |
| 2749 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2750 | if (max > len - l) |
| 2751 | max = len - l; |
| 2752 | |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2753 | /* The buffer avalaible size may be not contiguous. This test |
| 2754 | * detects a non contiguous buffer and realign it. |
| 2755 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2756 | if (bi_space_for_replace(chn->buf) < max) |
| 2757 | buffer_slow_realign(chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2758 | |
| 2759 | /* Copy input data in the buffer. */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2760 | 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] | 2761 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2762 | /* buffer replace considers that the input part is filled. |
| 2763 | * so, I must forward these new data in the output part. |
| 2764 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2765 | b_adv(chn->buf, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2766 | |
| 2767 | l += max; |
| 2768 | lua_pop(L, 1); |
| 2769 | lua_pushinteger(L, l); |
| 2770 | |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2771 | /* If there are no space avalaible, and the output buffer is empty. |
| 2772 | * in this case, we cannot add more data, so we cannot yield, |
| 2773 | * we return the amount of copyied data. |
| 2774 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2775 | max = chn->buf->size - buffer_len(chn->buf); |
| 2776 | if (max == 0 && chn->buf->o == 0) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2777 | return 1; |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2778 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2779 | if (l < len) { |
| 2780 | /* If we are waiting for space in the response buffer, we |
| 2781 | * must set the flag WAKERESWR. This flag required the task |
| 2782 | * wake up if any activity is detected on the response buffer. |
| 2783 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2784 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2785 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 2786 | else |
| 2787 | HLUA_SET_WAKEREQWR(hlua); |
| 2788 | 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] | 2789 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2790 | |
| 2791 | return 1; |
| 2792 | } |
| 2793 | |
| 2794 | /* Just a wraper of "_hlua_channel_send". This wrapper permits |
| 2795 | * yield the LUA process, and resume it without checking the |
| 2796 | * input arguments. |
| 2797 | */ |
| 2798 | __LJMP static int hlua_channel_send(lua_State *L) |
| 2799 | { |
| 2800 | MAY_LJMP(check_args(L, 2, "send")); |
| 2801 | lua_pushinteger(L, 0); |
| 2802 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2803 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2804 | } |
| 2805 | |
| 2806 | /* This function forward and amount of butes. The data pass from |
| 2807 | * the input side of the buffer to the output side, and can be |
| 2808 | * forwarded. This function never fails. |
| 2809 | * |
| 2810 | * The Lua function takes an amount of bytes to be forwarded in |
| 2811 | * imput. It returns the number of bytes forwarded. |
| 2812 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2813 | __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] | 2814 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2815 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2816 | int len; |
| 2817 | int l; |
| 2818 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2819 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2820 | |
| 2821 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2822 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 2823 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 2824 | |
| 2825 | max = len - l; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2826 | if (max > chn->buf->i) |
| 2827 | max = chn->buf->i; |
| 2828 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2829 | l += max; |
| 2830 | |
| 2831 | lua_pop(L, 1); |
| 2832 | lua_pushinteger(L, l); |
| 2833 | |
| 2834 | /* Check if it miss bytes to forward. */ |
| 2835 | if (l < len) { |
| 2836 | /* The the input channel or the output channel are closed, we |
| 2837 | * must return the amount of data forwarded. |
| 2838 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2839 | if (channel_input_closed(chn) || channel_output_closed(chn)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2840 | return 1; |
| 2841 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2842 | /* If we are waiting for space data in the response buffer, we |
| 2843 | * must set the flag WAKERESWR. This flag required the task |
| 2844 | * wake up if any activity is detected on the response buffer. |
| 2845 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2846 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2847 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 2848 | else |
| 2849 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2850 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2851 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 2852 | 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] | 2853 | } |
| 2854 | |
| 2855 | return 1; |
| 2856 | } |
| 2857 | |
| 2858 | /* Just check the input and prepare the stack for the previous |
| 2859 | * function "hlua_channel_forward_yield" |
| 2860 | */ |
| 2861 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 2862 | { |
| 2863 | MAY_LJMP(check_args(L, 2, "forward")); |
| 2864 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2865 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 2866 | |
| 2867 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2868 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2869 | } |
| 2870 | |
| 2871 | /* Just returns the number of bytes available in the input |
| 2872 | * side of the buffer. This function never fails. |
| 2873 | */ |
| 2874 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 2875 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2876 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2877 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2878 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2879 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2880 | lua_pushinteger(L, chn->buf->i); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2881 | return 1; |
| 2882 | } |
| 2883 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 2884 | /* Returns true if the channel is full. */ |
| 2885 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 2886 | { |
| 2887 | struct channel *chn; |
| 2888 | int rem; |
| 2889 | |
| 2890 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 2891 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2892 | |
| 2893 | rem = chn->buf->size; |
| 2894 | rem -= chn->buf->o; /* Output size */ |
| 2895 | rem -= chn->buf->i; /* Input size */ |
| 2896 | rem -= global.tune.maxrewrite; /* Rewrite reserved size */ |
| 2897 | |
| 2898 | lua_pushboolean(L, rem <= 0); |
| 2899 | return 1; |
| 2900 | } |
| 2901 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2902 | /* Just returns the number of bytes available in the output |
| 2903 | * side of the buffer. This function never fails. |
| 2904 | */ |
| 2905 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 2906 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2907 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2908 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2909 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2910 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2911 | lua_pushinteger(L, chn->buf->o); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2912 | return 1; |
| 2913 | } |
| 2914 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2915 | /* |
| 2916 | * |
| 2917 | * |
| 2918 | * Class Fetches |
| 2919 | * |
| 2920 | * |
| 2921 | */ |
| 2922 | |
| 2923 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2924 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2925 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 2926 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2927 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2928 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2929 | } |
| 2930 | |
| 2931 | /* This function creates and push in the stack a fetch object according |
| 2932 | * with a current TXN. |
| 2933 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 2934 | 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] | 2935 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 2936 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2937 | |
| 2938 | /* Check stack size. */ |
| 2939 | if (!lua_checkstack(L, 3)) |
| 2940 | return 0; |
| 2941 | |
| 2942 | /* Create the object: obj[0] = userdata. |
| 2943 | * Note that the base of the Fetches object is the |
| 2944 | * transaction object. |
| 2945 | */ |
| 2946 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 2947 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2948 | lua_rawseti(L, -2, 0); |
| 2949 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 2950 | hsmp->s = txn->s; |
| 2951 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 2952 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 2953 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2954 | |
| 2955 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2956 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 2957 | lua_setmetatable(L, -2); |
| 2958 | |
| 2959 | return 1; |
| 2960 | } |
| 2961 | |
| 2962 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 2963 | * It uses closure argument to store the associated sample-fetch. It |
| 2964 | * returns only one argument or throws an error. An error is thrown |
| 2965 | * only if an error is encountered during the argument parsing. If |
| 2966 | * the "sample-fetch" function fails, nil is returned. |
| 2967 | */ |
| 2968 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 2969 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 2970 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 2971 | struct sample_fetch *f; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2972 | struct arg args[ARGM_NBARGS + 1]; |
| 2973 | int i; |
| 2974 | struct sample smp; |
| 2975 | |
| 2976 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2977 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2978 | |
| 2979 | /* Get traditionnal arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 2980 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2981 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 2982 | /* Check execution authorization. */ |
| 2983 | if (f->use & SMP_USE_HTTP_ANY && |
| 2984 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 2985 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 2986 | "is not available in Lua services", f->kw); |
| 2987 | WILL_LJMP(lua_error(L)); |
| 2988 | } |
| 2989 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2990 | /* Get extra arguments. */ |
| 2991 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 2992 | if (i >= ARGM_NBARGS) |
| 2993 | break; |
| 2994 | hlua_lua2arg(L, i + 2, &args[i]); |
| 2995 | } |
| 2996 | args[i].type = ARGT_STOP; |
David Carlier | abdb00f | 2016-04-27 16:14:50 +0100 | [diff] [blame] | 2997 | args[i].data.str.str = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2998 | |
| 2999 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3000 | 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] | 3001 | |
| 3002 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3003 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3004 | lua_pushfstring(L, "error in arguments"); |
| 3005 | WILL_LJMP(lua_error(L)); |
| 3006 | } |
| 3007 | |
| 3008 | /* Initialise the sample. */ |
| 3009 | memset(&smp, 0, sizeof(smp)); |
| 3010 | |
| 3011 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3012 | 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] | 3013 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3014 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3015 | lua_pushstring(L, ""); |
| 3016 | else |
| 3017 | lua_pushnil(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3018 | return 1; |
| 3019 | } |
| 3020 | |
| 3021 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3022 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3023 | hlua_smp2lua_str(L, &smp); |
| 3024 | else |
| 3025 | hlua_smp2lua(L, &smp); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3026 | return 1; |
| 3027 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3028 | |
| 3029 | /* |
| 3030 | * |
| 3031 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3032 | * Class Converters |
| 3033 | * |
| 3034 | * |
| 3035 | */ |
| 3036 | |
| 3037 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3038 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3039 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3040 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3041 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3042 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3043 | } |
| 3044 | |
| 3045 | /* This function creates and push in the stack a Converters object |
| 3046 | * according with a current TXN. |
| 3047 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3048 | 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] | 3049 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3050 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3051 | |
| 3052 | /* Check stack size. */ |
| 3053 | if (!lua_checkstack(L, 3)) |
| 3054 | return 0; |
| 3055 | |
| 3056 | /* Create the object: obj[0] = userdata. |
| 3057 | * Note that the base of the Converters object is the |
| 3058 | * same than the TXN object. |
| 3059 | */ |
| 3060 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3061 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3062 | lua_rawseti(L, -2, 0); |
| 3063 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3064 | hsmp->s = txn->s; |
| 3065 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3066 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3067 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3068 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3069 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3070 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 3071 | lua_setmetatable(L, -2); |
| 3072 | |
| 3073 | return 1; |
| 3074 | } |
| 3075 | |
| 3076 | /* This function is an LUA binding. It is called with each converter. |
| 3077 | * It uses closure argument to store the associated converter. It |
| 3078 | * returns only one argument or throws an error. An error is thrown |
| 3079 | * only if an error is encountered during the argument parsing. If |
| 3080 | * the converter function function fails, nil is returned. |
| 3081 | */ |
| 3082 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 3083 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3084 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3085 | struct sample_conv *conv; |
| 3086 | struct arg args[ARGM_NBARGS + 1]; |
| 3087 | int i; |
| 3088 | struct sample smp; |
| 3089 | |
| 3090 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3091 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3092 | |
| 3093 | /* Get traditionnal arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3094 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3095 | |
| 3096 | /* Get extra arguments. */ |
| 3097 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 3098 | if (i >= ARGM_NBARGS) |
| 3099 | break; |
| 3100 | hlua_lua2arg(L, i + 3, &args[i]); |
| 3101 | } |
| 3102 | args[i].type = ARGT_STOP; |
David Carlier | abdb00f | 2016-04-27 16:14:50 +0100 | [diff] [blame] | 3103 | args[i].data.str.str = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3104 | |
| 3105 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3106 | 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] | 3107 | |
| 3108 | /* Run the special args checker. */ |
| 3109 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 3110 | hlua_pusherror(L, "error in arguments"); |
| 3111 | WILL_LJMP(lua_error(L)); |
| 3112 | } |
| 3113 | |
| 3114 | /* Initialise the sample. */ |
| 3115 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 3116 | hlua_pusherror(L, "error in the input argument"); |
| 3117 | WILL_LJMP(lua_error(L)); |
| 3118 | } |
| 3119 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3120 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 3121 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3122 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3123 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3124 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3125 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3126 | WILL_LJMP(lua_error(L)); |
| 3127 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3128 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 3129 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3130 | hlua_pusherror(L, "error during the input argument casting"); |
| 3131 | WILL_LJMP(lua_error(L)); |
| 3132 | } |
| 3133 | |
| 3134 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3135 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3136 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3137 | lua_pushstring(L, ""); |
| 3138 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3139 | lua_pushnil(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3140 | return 1; |
| 3141 | } |
| 3142 | |
| 3143 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3144 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3145 | hlua_smp2lua_str(L, &smp); |
| 3146 | else |
| 3147 | hlua_smp2lua(L, &smp); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3148 | return 1; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3149 | } |
| 3150 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3151 | /* |
| 3152 | * |
| 3153 | * |
| 3154 | * Class AppletTCP |
| 3155 | * |
| 3156 | * |
| 3157 | */ |
| 3158 | |
| 3159 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 3160 | * a class stream, otherwise it throws an error. |
| 3161 | */ |
| 3162 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 3163 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3164 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3165 | } |
| 3166 | |
| 3167 | /* This function creates and push in the stack an Applet object |
| 3168 | * according with a current TXN. |
| 3169 | */ |
| 3170 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 3171 | { |
| 3172 | struct hlua_appctx *appctx; |
| 3173 | struct stream_interface *si = ctx->owner; |
| 3174 | struct stream *s = si_strm(si); |
| 3175 | struct proxy *p = s->be; |
| 3176 | |
| 3177 | /* Check stack size. */ |
| 3178 | if (!lua_checkstack(L, 3)) |
| 3179 | return 0; |
| 3180 | |
| 3181 | /* Create the object: obj[0] = userdata. |
| 3182 | * Note that the base of the Converters object is the |
| 3183 | * same than the TXN object. |
| 3184 | */ |
| 3185 | lua_newtable(L); |
| 3186 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
| 3187 | lua_rawseti(L, -2, 0); |
| 3188 | appctx->appctx = ctx; |
| 3189 | appctx->htxn.s = s; |
| 3190 | appctx->htxn.p = p; |
| 3191 | |
| 3192 | /* Create the "f" field that contains a list of fetches. */ |
| 3193 | lua_pushstring(L, "f"); |
| 3194 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3195 | return 0; |
| 3196 | lua_settable(L, -3); |
| 3197 | |
| 3198 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3199 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3200 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3201 | return 0; |
| 3202 | lua_settable(L, -3); |
| 3203 | |
| 3204 | /* Create the "c" field that contains a list of converters. */ |
| 3205 | lua_pushstring(L, "c"); |
| 3206 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3207 | return 0; |
| 3208 | lua_settable(L, -3); |
| 3209 | |
| 3210 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3211 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3212 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3213 | return 0; |
| 3214 | lua_settable(L, -3); |
| 3215 | |
| 3216 | /* Pop a class stream metatable and affect it to the table. */ |
| 3217 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 3218 | lua_setmetatable(L, -2); |
| 3219 | |
| 3220 | return 1; |
| 3221 | } |
| 3222 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3223 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 3224 | { |
| 3225 | struct hlua_appctx *appctx; |
| 3226 | struct stream *s; |
| 3227 | const char *name; |
| 3228 | size_t len; |
| 3229 | struct sample smp; |
| 3230 | |
| 3231 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 3232 | |
| 3233 | /* It is useles to retrieve the stream, but this function |
| 3234 | * runs only in a stream context. |
| 3235 | */ |
| 3236 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3237 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3238 | s = appctx->htxn.s; |
| 3239 | |
| 3240 | /* Converts the third argument in a sample. */ |
| 3241 | hlua_lua2smp(L, 3, &smp); |
| 3242 | |
| 3243 | /* Store the sample in a variable. */ |
| 3244 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3245 | vars_set_by_name(name, len, &smp); |
| 3246 | return 0; |
| 3247 | } |
| 3248 | |
| 3249 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 3250 | { |
| 3251 | struct hlua_appctx *appctx; |
| 3252 | struct stream *s; |
| 3253 | const char *name; |
| 3254 | size_t len; |
| 3255 | struct sample smp; |
| 3256 | |
| 3257 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3258 | |
| 3259 | /* It is useles to retrieve the stream, but this function |
| 3260 | * runs only in a stream context. |
| 3261 | */ |
| 3262 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3263 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3264 | s = appctx->htxn.s; |
| 3265 | |
| 3266 | /* Unset the variable. */ |
| 3267 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3268 | vars_unset_by_name(name, len, &smp); |
| 3269 | return 0; |
| 3270 | } |
| 3271 | |
| 3272 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 3273 | { |
| 3274 | struct hlua_appctx *appctx; |
| 3275 | struct stream *s; |
| 3276 | const char *name; |
| 3277 | size_t len; |
| 3278 | struct sample smp; |
| 3279 | |
| 3280 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3281 | |
| 3282 | /* It is useles to retrieve the stream, but this function |
| 3283 | * runs only in a stream context. |
| 3284 | */ |
| 3285 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3286 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3287 | s = appctx->htxn.s; |
| 3288 | |
| 3289 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3290 | if (!vars_get_by_name(name, len, &smp)) { |
| 3291 | lua_pushnil(L); |
| 3292 | return 1; |
| 3293 | } |
| 3294 | |
| 3295 | return hlua_smp2lua(L, &smp); |
| 3296 | } |
| 3297 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3298 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 3299 | { |
| 3300 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3301 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3302 | struct hlua *hlua; |
| 3303 | |
| 3304 | /* 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] | 3305 | if (!s->hlua) |
| 3306 | return 0; |
| 3307 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3308 | |
| 3309 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3310 | |
| 3311 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 3312 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3313 | |
| 3314 | /* Get and store new value. */ |
| 3315 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3316 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3317 | |
| 3318 | return 0; |
| 3319 | } |
| 3320 | |
| 3321 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 3322 | { |
| 3323 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3324 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3325 | struct hlua *hlua; |
| 3326 | |
| 3327 | /* 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] | 3328 | if (!s->hlua) { |
| 3329 | lua_pushnil(L); |
| 3330 | return 1; |
| 3331 | } |
| 3332 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3333 | |
| 3334 | /* Push configuration index in the stack. */ |
| 3335 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3336 | |
| 3337 | return 1; |
| 3338 | } |
| 3339 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3340 | /* If expected data not yet available, it returns a yield. This function |
| 3341 | * consumes the data in the buffer. It returns a string containing the |
| 3342 | * data. This string can be empty. |
| 3343 | */ |
| 3344 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 3345 | { |
| 3346 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3347 | struct stream_interface *si = appctx->appctx->owner; |
| 3348 | int ret; |
| 3349 | char *blk1; |
| 3350 | int len1; |
| 3351 | char *blk2; |
| 3352 | int len2; |
| 3353 | |
| 3354 | /* Read the maximum amount of data avalaible. */ |
| 3355 | ret = bo_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
| 3356 | |
| 3357 | /* Data not yet avalaible. return yield. */ |
| 3358 | if (ret == 0) { |
| 3359 | si_applet_cant_get(si); |
| 3360 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0)); |
| 3361 | } |
| 3362 | |
| 3363 | /* End of data: commit the total strings and return. */ |
| 3364 | if (ret < 0) { |
| 3365 | luaL_pushresult(&appctx->b); |
| 3366 | return 1; |
| 3367 | } |
| 3368 | |
| 3369 | /* Ensure that the block 2 length is usable. */ |
| 3370 | if (ret == 1) |
| 3371 | len2 = 0; |
| 3372 | |
| 3373 | /* dont check the max length read and dont check. */ |
| 3374 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3375 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3376 | |
| 3377 | /* Consume input channel output buffer data. */ |
| 3378 | bo_skip(si_oc(si), len1 + len2); |
| 3379 | luaL_pushresult(&appctx->b); |
| 3380 | return 1; |
| 3381 | } |
| 3382 | |
| 3383 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 3384 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 3385 | { |
| 3386 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3387 | |
| 3388 | /* Initialise the string catenation. */ |
| 3389 | luaL_buffinit(L, &appctx->b); |
| 3390 | |
| 3391 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 3392 | } |
| 3393 | |
| 3394 | /* If expected data not yet available, it returns a yield. This function |
| 3395 | * consumes the data in the buffer. It returns a string containing the |
| 3396 | * data. This string can be empty. |
| 3397 | */ |
| 3398 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 3399 | { |
| 3400 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3401 | struct stream_interface *si = appctx->appctx->owner; |
| 3402 | int len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3403 | int ret; |
| 3404 | char *blk1; |
| 3405 | int len1; |
| 3406 | char *blk2; |
| 3407 | int len2; |
| 3408 | |
| 3409 | /* Read the maximum amount of data avalaible. */ |
| 3410 | ret = bo_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
| 3411 | |
| 3412 | /* Data not yet avalaible. return yield. */ |
| 3413 | if (ret == 0) { |
| 3414 | si_applet_cant_get(si); |
| 3415 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0)); |
| 3416 | } |
| 3417 | |
| 3418 | /* End of data: commit the total strings and return. */ |
| 3419 | if (ret < 0) { |
| 3420 | luaL_pushresult(&appctx->b); |
| 3421 | return 1; |
| 3422 | } |
| 3423 | |
| 3424 | /* Ensure that the block 2 length is usable. */ |
| 3425 | if (ret == 1) |
| 3426 | len2 = 0; |
| 3427 | |
| 3428 | if (len == -1) { |
| 3429 | |
| 3430 | /* If len == -1, catenate all the data avalaile and |
| 3431 | * yield because we want to get all the data until |
| 3432 | * the end of data stream. |
| 3433 | */ |
| 3434 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3435 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3436 | bo_skip(si_oc(si), len1 + len2); |
| 3437 | si_applet_cant_get(si); |
| 3438 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0)); |
| 3439 | |
| 3440 | } else { |
| 3441 | |
| 3442 | /* Copy the fisrt block caping to the length required. */ |
| 3443 | if (len1 > len) |
| 3444 | len1 = len; |
| 3445 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3446 | len -= len1; |
| 3447 | |
| 3448 | /* Copy the second block. */ |
| 3449 | if (len2 > len) |
| 3450 | len2 = len; |
| 3451 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3452 | len -= len2; |
| 3453 | |
| 3454 | /* Consume input channel output buffer data. */ |
| 3455 | bo_skip(si_oc(si), len1 + len2); |
| 3456 | |
| 3457 | /* If we are no other data avalaible, yield waiting for new data. */ |
| 3458 | if (len > 0) { |
| 3459 | lua_pushinteger(L, len); |
| 3460 | lua_replace(L, 2); |
| 3461 | si_applet_cant_get(si); |
| 3462 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0)); |
| 3463 | } |
| 3464 | |
| 3465 | /* return the result. */ |
| 3466 | luaL_pushresult(&appctx->b); |
| 3467 | return 1; |
| 3468 | } |
| 3469 | |
| 3470 | /* we never executes this */ |
| 3471 | hlua_pusherror(L, "Lua: internal error"); |
| 3472 | WILL_LJMP(lua_error(L)); |
| 3473 | return 0; |
| 3474 | } |
| 3475 | |
| 3476 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 3477 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 3478 | { |
| 3479 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3480 | int len = -1; |
| 3481 | |
| 3482 | if (lua_gettop(L) > 2) |
| 3483 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 3484 | if (lua_gettop(L) >= 2) { |
| 3485 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3486 | lua_pop(L, 1); |
| 3487 | } |
| 3488 | |
| 3489 | /* Confirm or set the required length */ |
| 3490 | lua_pushinteger(L, len); |
| 3491 | |
| 3492 | /* Initialise the string catenation. */ |
| 3493 | luaL_buffinit(L, &appctx->b); |
| 3494 | |
| 3495 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 3496 | } |
| 3497 | |
| 3498 | /* Append data in the output side of the buffer. This data is immediatly |
| 3499 | * sent. The fcuntion returns the ammount of data writed. If the buffer |
| 3500 | * cannot contains the data, the function yield. The function returns -1 |
| 3501 | * if the channel is closed. |
| 3502 | */ |
| 3503 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 3504 | { |
| 3505 | size_t len; |
| 3506 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3507 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3508 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3509 | struct stream_interface *si = appctx->appctx->owner; |
| 3510 | struct channel *chn = si_ic(si); |
| 3511 | int max; |
| 3512 | |
| 3513 | /* Get the max amount of data which can write as input in the channel. */ |
| 3514 | max = channel_recv_max(chn); |
| 3515 | if (max > (len - l)) |
| 3516 | max = len - l; |
| 3517 | |
| 3518 | /* Copy data. */ |
| 3519 | bi_putblk(chn, str + l, max); |
| 3520 | |
| 3521 | /* update counters. */ |
| 3522 | l += max; |
| 3523 | lua_pop(L, 1); |
| 3524 | lua_pushinteger(L, l); |
| 3525 | |
| 3526 | /* If some data is not send, declares the situation to the |
| 3527 | * applet, and returns a yield. |
| 3528 | */ |
| 3529 | if (l < len) { |
| 3530 | si_applet_cant_put(si); |
| 3531 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0)); |
| 3532 | } |
| 3533 | |
| 3534 | return 1; |
| 3535 | } |
| 3536 | |
| 3537 | /* Just a wraper of "hlua_applet_tcp_send_yield". This wrapper permits |
| 3538 | * yield the LUA process, and resume it without checking the |
| 3539 | * input arguments. |
| 3540 | */ |
| 3541 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 3542 | { |
| 3543 | MAY_LJMP(check_args(L, 2, "send")); |
| 3544 | lua_pushinteger(L, 0); |
| 3545 | |
| 3546 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 3547 | } |
| 3548 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3549 | /* |
| 3550 | * |
| 3551 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3552 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3553 | * |
| 3554 | * |
| 3555 | */ |
| 3556 | |
| 3557 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3558 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3559 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3560 | __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] | 3561 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3562 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3563 | } |
| 3564 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3565 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3566 | * according with a current TXN. |
| 3567 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3568 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3569 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3570 | struct hlua_appctx *appctx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3571 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3572 | struct stream_interface *si = ctx->owner; |
| 3573 | struct stream *s = si_strm(si); |
| 3574 | struct proxy *px = s->be; |
| 3575 | struct http_txn *txn = s->txn; |
| 3576 | const char *path; |
| 3577 | const char *end; |
| 3578 | const char *p; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3579 | |
| 3580 | /* Check stack size. */ |
| 3581 | if (!lua_checkstack(L, 3)) |
| 3582 | return 0; |
| 3583 | |
| 3584 | /* Create the object: obj[0] = userdata. |
| 3585 | * Note that the base of the Converters object is the |
| 3586 | * same than the TXN object. |
| 3587 | */ |
| 3588 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3589 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3590 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3591 | appctx->appctx = ctx; |
| 3592 | appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 3593 | appctx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3594 | appctx->htxn.s = s; |
| 3595 | appctx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3596 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3597 | /* Create the "f" field that contains a list of fetches. */ |
| 3598 | lua_pushstring(L, "f"); |
| 3599 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3600 | return 0; |
| 3601 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3602 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3603 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3604 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3605 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3606 | return 0; |
| 3607 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3608 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3609 | /* Create the "c" field that contains a list of converters. */ |
| 3610 | lua_pushstring(L, "c"); |
| 3611 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3612 | return 0; |
| 3613 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3614 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3615 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3616 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3617 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3618 | return 0; |
| 3619 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3620 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3621 | /* Stores the request method. */ |
| 3622 | lua_pushstring(L, "method"); |
| 3623 | lua_pushlstring(L, txn->req.chn->buf->p, txn->req.sl.rq.m_l); |
| 3624 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3625 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3626 | /* Stores the http version. */ |
| 3627 | lua_pushstring(L, "version"); |
| 3628 | lua_pushlstring(L, txn->req.chn->buf->p + txn->req.sl.rq.v, txn->req.sl.rq.v_l); |
| 3629 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3630 | |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3631 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 3632 | * the array on the top of the stack. |
| 3633 | */ |
| 3634 | lua_pushstring(L, "headers"); |
| 3635 | htxn.s = s; |
| 3636 | htxn.p = px; |
| 3637 | htxn.dir = SMP_OPT_DIR_REQ; |
| 3638 | if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req)) |
| 3639 | return 0; |
| 3640 | lua_settable(L, -3); |
| 3641 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3642 | /* Get path and qs */ |
| 3643 | path = http_get_path(txn); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3644 | if (path) { |
| 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 | 7d38863 | 2017-02-22 02:06:16 +0100 | [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 | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3655 | /* Stores the query string. */ |
| 3656 | lua_pushstring(L, "qs"); |
| 3657 | if (*p == '?') |
| 3658 | p++; |
| 3659 | lua_pushlstring(L, p, end - p); |
| 3660 | lua_settable(L, -3); |
| 3661 | } |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3662 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3663 | /* Stores the request path. */ |
| 3664 | lua_pushstring(L, "length"); |
| 3665 | lua_pushinteger(L, txn->req.body_len); |
| 3666 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3667 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3668 | /* Create an array of HTTP request headers. */ |
| 3669 | lua_pushstring(L, "headers"); |
| 3670 | MAY_LJMP(hlua_http_get_headers(L, &appctx->htxn, &appctx->htxn.s->txn->req)); |
| 3671 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3672 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3673 | /* Create an empty array of HTTP request headers. */ |
| 3674 | lua_pushstring(L, "response"); |
| 3675 | lua_newtable(L); |
| 3676 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3677 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3678 | /* Pop a class stream metatable and affect it to the table. */ |
| 3679 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 3680 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3681 | |
| 3682 | return 1; |
| 3683 | } |
| 3684 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3685 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 3686 | { |
| 3687 | struct hlua_appctx *appctx; |
| 3688 | struct stream *s; |
| 3689 | const char *name; |
| 3690 | size_t len; |
| 3691 | struct sample smp; |
| 3692 | |
| 3693 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 3694 | |
| 3695 | /* It is useles to retrieve the stream, but this function |
| 3696 | * runs only in a stream context. |
| 3697 | */ |
| 3698 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3699 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3700 | s = appctx->htxn.s; |
| 3701 | |
| 3702 | /* Converts the third argument in a sample. */ |
| 3703 | hlua_lua2smp(L, 3, &smp); |
| 3704 | |
| 3705 | /* Store the sample in a variable. */ |
| 3706 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3707 | vars_set_by_name(name, len, &smp); |
| 3708 | return 0; |
| 3709 | } |
| 3710 | |
| 3711 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 3712 | { |
| 3713 | struct hlua_appctx *appctx; |
| 3714 | struct stream *s; |
| 3715 | const char *name; |
| 3716 | size_t len; |
| 3717 | struct sample smp; |
| 3718 | |
| 3719 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3720 | |
| 3721 | /* It is useles to retrieve the stream, but this function |
| 3722 | * runs only in a stream context. |
| 3723 | */ |
| 3724 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3725 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3726 | s = appctx->htxn.s; |
| 3727 | |
| 3728 | /* Unset the variable. */ |
| 3729 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3730 | vars_unset_by_name(name, len, &smp); |
| 3731 | return 0; |
| 3732 | } |
| 3733 | |
| 3734 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 3735 | { |
| 3736 | struct hlua_appctx *appctx; |
| 3737 | struct stream *s; |
| 3738 | const char *name; |
| 3739 | size_t len; |
| 3740 | struct sample smp; |
| 3741 | |
| 3742 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3743 | |
| 3744 | /* It is useles to retrieve the stream, but this function |
| 3745 | * runs only in a stream context. |
| 3746 | */ |
| 3747 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3748 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3749 | s = appctx->htxn.s; |
| 3750 | |
| 3751 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3752 | if (!vars_get_by_name(name, len, &smp)) { |
| 3753 | lua_pushnil(L); |
| 3754 | return 1; |
| 3755 | } |
| 3756 | |
| 3757 | return hlua_smp2lua(L, &smp); |
| 3758 | } |
| 3759 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3760 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 3761 | { |
| 3762 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3763 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3764 | struct hlua *hlua; |
| 3765 | |
| 3766 | /* 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] | 3767 | if (!s->hlua) |
| 3768 | return 0; |
| 3769 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3770 | |
| 3771 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3772 | |
| 3773 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 3774 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 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)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4123 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4124 | |
| 4125 | if (status < 100 || status > 599) { |
| 4126 | lua_pushboolean(L, 0); |
| 4127 | return 1; |
| 4128 | } |
| 4129 | |
| 4130 | appctx->appctx->ctx.hlua_apphttp.status = status; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4131 | appctx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4132 | lua_pushboolean(L, 1); |
| 4133 | return 1; |
| 4134 | } |
| 4135 | |
| 4136 | /* We will build the status line and the headers of the HTTP response. |
| 4137 | * We will try send at once if its not possible, we give back the hand |
| 4138 | * waiting for more room. |
| 4139 | */ |
| 4140 | __LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx) |
| 4141 | { |
| 4142 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4143 | struct stream_interface *si = appctx->appctx->owner; |
| 4144 | struct channel *chn = si_ic(si); |
| 4145 | int ret; |
| 4146 | size_t len; |
| 4147 | const char *msg; |
| 4148 | |
| 4149 | /* Get the message as the first argument on the stack. */ |
| 4150 | msg = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4151 | |
| 4152 | /* Send the message at once. */ |
| 4153 | ret = bi_putblk(chn, msg, len); |
| 4154 | |
| 4155 | /* if ret == -2 or -3 the channel closed or the message si too |
| 4156 | * big for the buffers. |
| 4157 | */ |
| 4158 | if (ret == -2 || ret == -3) { |
| 4159 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 4160 | WILL_LJMP(lua_error(L)); |
| 4161 | } |
| 4162 | |
| 4163 | /* If ret is -1, we dont have room in the buffer, so we yield. */ |
| 4164 | if (ret == -1) { |
| 4165 | si_applet_cant_put(si); |
| 4166 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0)); |
| 4167 | } |
| 4168 | |
| 4169 | /* Headers sent, set the flag. */ |
| 4170 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
| 4171 | return 0; |
| 4172 | } |
| 4173 | |
| 4174 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
| 4175 | { |
| 4176 | struct chunk *tmp = get_trash_chunk(); |
| 4177 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4178 | const char *name; |
| 4179 | const char *value; |
| 4180 | int id; |
| 4181 | int hdr_connection = 0; |
| 4182 | int hdr_contentlength = -1; |
| 4183 | int hdr_chunked = 0; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4184 | const char *reason = appctx->appctx->ctx.hlua_apphttp.reason; |
| 4185 | |
| 4186 | if (reason == NULL) |
| 4187 | reason = get_reason(appctx->appctx->ctx.hlua_apphttp.status); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4188 | |
| 4189 | /* Use the same http version than the request. */ |
| 4190 | chunk_appendf(tmp, "HTTP/1.%c %d %s\r\n", |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 4191 | appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11 ? '1' : '0', |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4192 | appctx->appctx->ctx.hlua_apphttp.status, |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4193 | reason); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4194 | |
| 4195 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 4196 | lua_pushvalue(L, 0); |
| 4197 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 4198 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
| 4199 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4200 | WILL_LJMP(lua_error(L)); |
| 4201 | } |
| 4202 | |
| 4203 | /* Browse the list of headers. */ |
| 4204 | lua_pushnil(L); |
| 4205 | while(lua_next(L, -2) != 0) { |
| 4206 | |
| 4207 | /* We expect a string as -2. */ |
| 4208 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 4209 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
| 4210 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4211 | lua_typename(L, lua_type(L, -2))); |
| 4212 | WILL_LJMP(lua_error(L)); |
| 4213 | } |
| 4214 | name = lua_tostring(L, -2); |
| 4215 | |
| 4216 | /* We expect an array as -1. */ |
| 4217 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 4218 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
| 4219 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4220 | name, |
| 4221 | lua_typename(L, lua_type(L, -1))); |
| 4222 | WILL_LJMP(lua_error(L)); |
| 4223 | } |
| 4224 | |
| 4225 | /* Browse the table who is on the top of the stack. */ |
| 4226 | lua_pushnil(L); |
| 4227 | while(lua_next(L, -2) != 0) { |
| 4228 | |
| 4229 | /* We expect a number as -2. */ |
| 4230 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 4231 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
| 4232 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4233 | name, |
| 4234 | lua_typename(L, lua_type(L, -2))); |
| 4235 | WILL_LJMP(lua_error(L)); |
| 4236 | } |
| 4237 | id = lua_tointeger(L, -2); |
| 4238 | |
| 4239 | /* We expect a string as -2. */ |
| 4240 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 4241 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
| 4242 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4243 | name, id, |
| 4244 | lua_typename(L, lua_type(L, -1))); |
| 4245 | WILL_LJMP(lua_error(L)); |
| 4246 | } |
| 4247 | value = lua_tostring(L, -1); |
| 4248 | |
| 4249 | /* Catenate a new header. */ |
| 4250 | chunk_appendf(tmp, "%s: %s\r\n", name, value); |
| 4251 | |
| 4252 | /* Protocol checks. */ |
| 4253 | |
| 4254 | /* Check if the header conneciton is present. */ |
| 4255 | if (strcasecmp("connection", name) == 0) |
| 4256 | hdr_connection = 1; |
| 4257 | |
| 4258 | /* Copy the header content length. The length conversion |
| 4259 | * is done without control. If it contains a ad value, this |
| 4260 | * is not our problem. |
| 4261 | */ |
| 4262 | if (strcasecmp("content-length", name) == 0) |
| 4263 | hdr_contentlength = atoi(value); |
| 4264 | |
| 4265 | /* Check if the client annouces a transfer-encoding chunked it self. */ |
| 4266 | if (strcasecmp("transfer-encoding", name) == 0 && |
| 4267 | strcasecmp("chunked", value) == 0) |
| 4268 | hdr_chunked = 1; |
| 4269 | |
| 4270 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4271 | lua_pop(L, 1); |
| 4272 | } |
| 4273 | |
| 4274 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4275 | lua_pop(L, 1); |
| 4276 | } |
| 4277 | |
| 4278 | /* If the http protocol version is 1.1, we expect an header "connection" set |
| 4279 | * to "close" to be HAProxy/keeplive compliant. Otherwise, we expect nothing. |
| 4280 | * If the header conneciton is present, don't change it, if it is not present, |
| 4281 | * we must set. |
| 4282 | * |
| 4283 | * we set a "connection: close" header for ensuring that the keepalive will be |
| 4284 | * respected by haproxy. HAProcy considers that the application cloe the connection |
| 4285 | * and it keep the connection from the client open. |
| 4286 | */ |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 4287 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11 && !hdr_connection) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4288 | chunk_appendf(tmp, "Connection: close\r\n"); |
| 4289 | |
| 4290 | /* If we dont have a content-length set, we must announce a transfer enconding |
| 4291 | * chunked. This is required by haproxy for the keepalive compliance. |
| 4292 | * If the applet annouce a transfer-encoding chunked itslef, don't |
| 4293 | * do anything. |
| 4294 | */ |
| 4295 | if (hdr_contentlength == -1 && hdr_chunked == 0) { |
| 4296 | chunk_appendf(tmp, "Transfer-encoding: chunked\r\n"); |
| 4297 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_CHUNKED; |
| 4298 | } |
| 4299 | |
| 4300 | /* Finalize headers. */ |
| 4301 | chunk_appendf(tmp, "\r\n"); |
| 4302 | |
| 4303 | /* Remove the last entry and the array of headers */ |
| 4304 | lua_pop(L, 2); |
| 4305 | |
| 4306 | /* Push the headers block. */ |
| 4307 | lua_pushlstring(L, tmp->str, tmp->len); |
| 4308 | |
| 4309 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
| 4310 | } |
| 4311 | |
| 4312 | /* |
| 4313 | * |
| 4314 | * |
| 4315 | * Class HTTP |
| 4316 | * |
| 4317 | * |
| 4318 | */ |
| 4319 | |
| 4320 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4321 | * a class stream, otherwise it throws an error. |
| 4322 | */ |
| 4323 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
| 4324 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4325 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4326 | } |
| 4327 | |
| 4328 | /* This function creates and push in the stack a HTTP object |
| 4329 | * according with a current TXN. |
| 4330 | */ |
| 4331 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 4332 | { |
| 4333 | struct hlua_txn *htxn; |
| 4334 | |
| 4335 | /* Check stack size. */ |
| 4336 | if (!lua_checkstack(L, 3)) |
| 4337 | return 0; |
| 4338 | |
| 4339 | /* Create the object: obj[0] = userdata. |
| 4340 | * Note that the base of the Converters object is the |
| 4341 | * same than the TXN object. |
| 4342 | */ |
| 4343 | lua_newtable(L); |
| 4344 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 4345 | lua_rawseti(L, -2, 0); |
| 4346 | |
| 4347 | htxn->s = txn->s; |
| 4348 | htxn->p = txn->p; |
| 4349 | |
| 4350 | /* Pop a class stream metatable and affect it to the table. */ |
| 4351 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 4352 | lua_setmetatable(L, -2); |
| 4353 | |
| 4354 | return 1; |
| 4355 | } |
| 4356 | |
| 4357 | /* This function creates ans returns an array of HTTP headers. |
| 4358 | * This function does not fails. It is used as wrapper with the |
| 4359 | * 2 following functions. |
| 4360 | */ |
| 4361 | __LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4362 | { |
| 4363 | const char *cur_ptr, *cur_next, *p; |
| 4364 | int old_idx, cur_idx; |
| 4365 | struct hdr_idx_elem *cur_hdr; |
| 4366 | const char *hn, *hv; |
| 4367 | int hnl, hvl; |
| 4368 | int type; |
| 4369 | const char *in; |
| 4370 | char *out; |
| 4371 | int len; |
| 4372 | |
| 4373 | /* Create the table. */ |
| 4374 | lua_newtable(L); |
| 4375 | |
| 4376 | if (!htxn->s->txn) |
| 4377 | return 1; |
| 4378 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4379 | /* Check if a valid response is parsed */ |
| 4380 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4381 | return 1; |
| 4382 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4383 | /* Build array of headers. */ |
| 4384 | old_idx = 0; |
| 4385 | cur_next = msg->chn->buf->p + hdr_idx_first_pos(&htxn->s->txn->hdr_idx); |
| 4386 | |
| 4387 | while (1) { |
| 4388 | cur_idx = htxn->s->txn->hdr_idx.v[old_idx].next; |
| 4389 | if (!cur_idx) |
| 4390 | break; |
| 4391 | old_idx = cur_idx; |
| 4392 | |
| 4393 | cur_hdr = &htxn->s->txn->hdr_idx.v[cur_idx]; |
| 4394 | cur_ptr = cur_next; |
| 4395 | cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1; |
| 4396 | |
| 4397 | /* Now we have one full header at cur_ptr of len cur_hdr->len, |
| 4398 | * and the next header starts at cur_next. We'll check |
| 4399 | * this header in the list as well as against the default |
| 4400 | * rule. |
| 4401 | */ |
| 4402 | |
| 4403 | /* look for ': *'. */ |
| 4404 | hn = cur_ptr; |
| 4405 | for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++); |
| 4406 | if (p >= cur_ptr+cur_hdr->len) |
| 4407 | continue; |
| 4408 | hnl = p - hn; |
| 4409 | p++; |
| 4410 | while (p < cur_ptr+cur_hdr->len && ( *p == ' ' || *p == '\t' )) |
| 4411 | p++; |
| 4412 | if (p >= cur_ptr+cur_hdr->len) |
| 4413 | continue; |
| 4414 | hv = p; |
| 4415 | hvl = cur_ptr+cur_hdr->len-p; |
| 4416 | |
| 4417 | /* Lowercase the key. Don't check the size of trash, it have |
| 4418 | * the size of one buffer and the input data contains in one |
| 4419 | * buffer. |
| 4420 | */ |
| 4421 | out = trash.str; |
| 4422 | for (in=hn; in<hn+hnl; in++, out++) |
| 4423 | *out = tolower(*in); |
| 4424 | *out = '\0'; |
| 4425 | |
| 4426 | /* Check for existing entry: |
| 4427 | * assume that the table is on the top of the stack, and |
| 4428 | * push the key in the stack, the function lua_gettable() |
| 4429 | * perform the lookup. |
| 4430 | */ |
| 4431 | lua_pushlstring(L, trash.str, hnl); |
| 4432 | lua_gettable(L, -2); |
| 4433 | type = lua_type(L, -1); |
| 4434 | |
| 4435 | switch (type) { |
| 4436 | case LUA_TNIL: |
| 4437 | /* Table not found, create it. */ |
| 4438 | lua_pop(L, 1); /* remove the nil value. */ |
| 4439 | lua_pushlstring(L, trash.str, hnl); /* push the header name as key. */ |
| 4440 | lua_newtable(L); /* create and push empty table. */ |
| 4441 | lua_pushlstring(L, hv, hvl); /* push header value. */ |
| 4442 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 4443 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
| 4444 | break; |
| 4445 | |
| 4446 | case LUA_TTABLE: |
| 4447 | /* Entry found: push the value in the table. */ |
| 4448 | len = lua_rawlen(L, -1); |
| 4449 | lua_pushlstring(L, hv, hvl); /* push header value. */ |
| 4450 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 4451 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 4452 | break; |
| 4453 | |
| 4454 | default: |
| 4455 | /* Other cases are errors. */ |
| 4456 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 4457 | WILL_LJMP(lua_error(L)); |
| 4458 | } |
| 4459 | } |
| 4460 | |
| 4461 | return 1; |
| 4462 | } |
| 4463 | |
| 4464 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 4465 | { |
| 4466 | struct hlua_txn *htxn; |
| 4467 | |
| 4468 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 4469 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4470 | |
| 4471 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->req); |
| 4472 | } |
| 4473 | |
| 4474 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 4475 | { |
| 4476 | struct hlua_txn *htxn; |
| 4477 | |
| 4478 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 4479 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4480 | |
| 4481 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp); |
| 4482 | } |
| 4483 | |
| 4484 | /* This function replace full header, or just a value in |
| 4485 | * the request or in the response. It is a wrapper fir the |
| 4486 | * 4 following functions. |
| 4487 | */ |
| 4488 | __LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn, |
| 4489 | struct http_msg *msg, int action) |
| 4490 | { |
| 4491 | size_t name_len; |
| 4492 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4493 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 4494 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
| 4495 | struct my_regex re; |
| 4496 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4497 | /* Check if a valid response is parsed */ |
| 4498 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4499 | return 0; |
| 4500 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4501 | if (!regex_comp(reg, &re, 1, 1, NULL)) |
| 4502 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 4503 | |
| 4504 | http_transform_header_str(htxn->s, msg, name, name_len, value, &re, action); |
| 4505 | regex_free(&re); |
| 4506 | return 0; |
| 4507 | } |
| 4508 | |
| 4509 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 4510 | { |
| 4511 | struct hlua_txn *htxn; |
| 4512 | |
| 4513 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4514 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4515 | |
| 4516 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR)); |
| 4517 | } |
| 4518 | |
| 4519 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 4520 | { |
| 4521 | struct hlua_txn *htxn; |
| 4522 | |
| 4523 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 4524 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4525 | |
| 4526 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR)); |
| 4527 | } |
| 4528 | |
| 4529 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 4530 | { |
| 4531 | struct hlua_txn *htxn; |
| 4532 | |
| 4533 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4534 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4535 | |
| 4536 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL)); |
| 4537 | } |
| 4538 | |
| 4539 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 4540 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4541 | struct hlua_txn *htxn; |
| 4542 | |
| 4543 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 4544 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4545 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 4546 | 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] | 4547 | } |
| 4548 | |
| 4549 | /* This function deletes all the occurences of an header. |
| 4550 | * It is a wrapper for the 2 following functions. |
| 4551 | */ |
| 4552 | __LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4553 | { |
| 4554 | size_t len; |
| 4555 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4556 | struct hdr_ctx ctx; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4557 | struct http_txn *txn = htxn->s->txn; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4558 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4559 | /* Check if a valid response is parsed */ |
| 4560 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4561 | return 0; |
| 4562 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4563 | ctx.idx = 0; |
| 4564 | while (http_find_header2(name, len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) |
| 4565 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 4566 | return 0; |
| 4567 | } |
| 4568 | |
| 4569 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 4570 | { |
| 4571 | struct hlua_txn *htxn; |
| 4572 | |
| 4573 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 4574 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4575 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4576 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4577 | } |
| 4578 | |
| 4579 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 4580 | { |
| 4581 | struct hlua_txn *htxn; |
| 4582 | |
| 4583 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 4584 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4585 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4586 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4587 | } |
| 4588 | |
| 4589 | /* This function adds an header. It is a wrapper used by |
| 4590 | * the 2 following functions. |
| 4591 | */ |
| 4592 | __LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4593 | { |
| 4594 | size_t name_len; |
| 4595 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4596 | size_t value_len; |
| 4597 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
| 4598 | char *p; |
| 4599 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4600 | /* Check if a valid message is parsed */ |
| 4601 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4602 | return 0; |
| 4603 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4604 | /* Check length. */ |
| 4605 | trash.len = value_len + name_len + 2; |
| 4606 | if (trash.len > trash.size) |
| 4607 | return 0; |
| 4608 | |
| 4609 | /* Creates the header string. */ |
| 4610 | p = trash.str; |
| 4611 | memcpy(p, name, name_len); |
| 4612 | p += name_len; |
| 4613 | *p = ':'; |
| 4614 | p++; |
| 4615 | *p = ' '; |
| 4616 | p++; |
| 4617 | memcpy(p, value, value_len); |
| 4618 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4619 | 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] | 4620 | trash.str, trash.len) != 0); |
| 4621 | |
| 4622 | return 0; |
| 4623 | } |
| 4624 | |
| 4625 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 4626 | { |
| 4627 | struct hlua_txn *htxn; |
| 4628 | |
| 4629 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 4630 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4631 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4632 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4633 | } |
| 4634 | |
| 4635 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 4636 | { |
| 4637 | struct hlua_txn *htxn; |
| 4638 | |
| 4639 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 4640 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4641 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4642 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4643 | } |
| 4644 | |
| 4645 | static int hlua_http_req_set_hdr(lua_State *L) |
| 4646 | { |
| 4647 | struct hlua_txn *htxn; |
| 4648 | |
| 4649 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 4650 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4651 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4652 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
| 4653 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4654 | } |
| 4655 | |
| 4656 | static int hlua_http_res_set_hdr(lua_State *L) |
| 4657 | { |
| 4658 | struct hlua_txn *htxn; |
| 4659 | |
| 4660 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 4661 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4662 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4663 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
| 4664 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4665 | } |
| 4666 | |
| 4667 | /* This function set the method. */ |
| 4668 | static int hlua_http_req_set_meth(lua_State *L) |
| 4669 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4670 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4671 | size_t name_len; |
| 4672 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4673 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4674 | /* Check if a valid request is parsed */ |
| 4675 | if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 4676 | lua_pushboolean(L, 0); |
| 4677 | return 1; |
| 4678 | } |
| 4679 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4680 | 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] | 4681 | return 1; |
| 4682 | } |
| 4683 | |
| 4684 | /* This function set the method. */ |
| 4685 | static int hlua_http_req_set_path(lua_State *L) |
| 4686 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4687 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4688 | size_t name_len; |
| 4689 | 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] | 4690 | |
| 4691 | /* Check if a valid request is parsed */ |
| 4692 | if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 4693 | lua_pushboolean(L, 0); |
| 4694 | return 1; |
| 4695 | } |
| 4696 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4697 | 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] | 4698 | return 1; |
| 4699 | } |
| 4700 | |
| 4701 | /* This function set the query-string. */ |
| 4702 | static int hlua_http_req_set_query(lua_State *L) |
| 4703 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4704 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4705 | size_t name_len; |
| 4706 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4707 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4708 | /* Check if a valid request is parsed */ |
| 4709 | if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 4710 | lua_pushboolean(L, 0); |
| 4711 | return 1; |
| 4712 | } |
| 4713 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4714 | /* Check length. */ |
| 4715 | if (name_len > trash.size - 1) { |
| 4716 | lua_pushboolean(L, 0); |
| 4717 | return 1; |
| 4718 | } |
| 4719 | |
| 4720 | /* Add the mark question as prefix. */ |
| 4721 | chunk_reset(&trash); |
| 4722 | trash.str[trash.len++] = '?'; |
| 4723 | memcpy(trash.str + trash.len, name, name_len); |
| 4724 | trash.len += name_len; |
| 4725 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4726 | 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] | 4727 | return 1; |
| 4728 | } |
| 4729 | |
| 4730 | /* This function set the uri. */ |
| 4731 | static int hlua_http_req_set_uri(lua_State *L) |
| 4732 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4733 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4734 | size_t name_len; |
| 4735 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4736 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4737 | /* Check if a valid request is parsed */ |
| 4738 | if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 4739 | lua_pushboolean(L, 0); |
| 4740 | return 1; |
| 4741 | } |
| 4742 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4743 | 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] | 4744 | return 1; |
| 4745 | } |
| 4746 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4747 | /* This function set the response code & optionally reason. */ |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 4748 | static int hlua_http_res_set_status(lua_State *L) |
| 4749 | { |
| 4750 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4751 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4752 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 4753 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4754 | /* Check if a valid response is parsed */ |
| 4755 | if (unlikely(htxn->s->txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 4756 | return 0; |
| 4757 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4758 | http_set_status(code, reason, htxn->s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 4759 | return 0; |
| 4760 | } |
| 4761 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4762 | /* |
| 4763 | * |
| 4764 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4765 | * Class TXN |
| 4766 | * |
| 4767 | * |
| 4768 | */ |
| 4769 | |
| 4770 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4771 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4772 | */ |
| 4773 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 4774 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4775 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4776 | } |
| 4777 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 4778 | __LJMP static int hlua_set_var(lua_State *L) |
| 4779 | { |
| 4780 | struct hlua_txn *htxn; |
| 4781 | const char *name; |
| 4782 | size_t len; |
| 4783 | struct sample smp; |
| 4784 | |
| 4785 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 4786 | |
| 4787 | /* It is useles to retrieve the stream, but this function |
| 4788 | * runs only in a stream context. |
| 4789 | */ |
| 4790 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 4791 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4792 | |
| 4793 | /* Converts the third argument in a sample. */ |
| 4794 | hlua_lua2smp(L, 3, &smp); |
| 4795 | |
| 4796 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 4797 | 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] | 4798 | vars_set_by_name(name, len, &smp); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 4799 | return 0; |
| 4800 | } |
| 4801 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 4802 | __LJMP static int hlua_unset_var(lua_State *L) |
| 4803 | { |
| 4804 | struct hlua_txn *htxn; |
| 4805 | const char *name; |
| 4806 | size_t len; |
| 4807 | struct sample smp; |
| 4808 | |
| 4809 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4810 | |
| 4811 | /* It is useles to retrieve the stream, but this function |
| 4812 | * runs only in a stream context. |
| 4813 | */ |
| 4814 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 4815 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4816 | |
| 4817 | /* Unset the variable. */ |
| 4818 | smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR); |
| 4819 | vars_unset_by_name(name, len, &smp); |
| 4820 | return 0; |
| 4821 | } |
| 4822 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 4823 | __LJMP static int hlua_get_var(lua_State *L) |
| 4824 | { |
| 4825 | struct hlua_txn *htxn; |
| 4826 | const char *name; |
| 4827 | size_t len; |
| 4828 | struct sample smp; |
| 4829 | |
| 4830 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4831 | |
| 4832 | /* It is useles to retrieve the stream, but this function |
| 4833 | * runs only in a stream context. |
| 4834 | */ |
| 4835 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 4836 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4837 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 4838 | 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] | 4839 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 4840 | lua_pushnil(L); |
| 4841 | return 1; |
| 4842 | } |
| 4843 | |
| 4844 | return hlua_smp2lua(L, &smp); |
| 4845 | } |
| 4846 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 4847 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 4848 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4849 | struct hlua *hlua; |
| 4850 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 4851 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4852 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4853 | /* It is useles to retrieve the stream, but this function |
| 4854 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 4855 | */ |
| 4856 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4857 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 4858 | |
| 4859 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4860 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 4861 | |
| 4862 | /* Get and store new value. */ |
| 4863 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4864 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4865 | |
| 4866 | return 0; |
| 4867 | } |
| 4868 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 4869 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 4870 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4871 | struct hlua *hlua; |
| 4872 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 4873 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 4874 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4875 | /* It is useles to retrieve the stream, but this function |
| 4876 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 4877 | */ |
| 4878 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4879 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 4880 | |
| 4881 | /* Push configuration index in the stack. */ |
| 4882 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4883 | |
| 4884 | return 1; |
| 4885 | } |
| 4886 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4887 | /* Create stack entry containing a class TXN. This function |
| 4888 | * return 0 if the stack does not contains free slots, |
| 4889 | * otherwise it returns 1. |
| 4890 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 4891 | 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] | 4892 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 4893 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4894 | |
| 4895 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 4896 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4897 | return 0; |
| 4898 | |
| 4899 | /* NOTE: The allocation never fails. The failure |
| 4900 | * throw an error, and the function never returns. |
| 4901 | * if the throw is not avalaible, the process is aborted. |
| 4902 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 4903 | /* Create the object: obj[0] = userdata. */ |
| 4904 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 4905 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 4906 | lua_rawseti(L, -2, 0); |
| 4907 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 4908 | htxn->s = s; |
| 4909 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 4910 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 4911 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4912 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4913 | /* Create the "f" field that contains a list of fetches. */ |
| 4914 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 4915 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +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 | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4918 | |
| 4919 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4920 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 4921 | 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] | 4922 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 4923 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4924 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4925 | /* Create the "c" field that contains a list of converters. */ |
| 4926 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 4927 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +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 | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4930 | |
| 4931 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4932 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4933 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +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 | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4936 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 4937 | /* Create the "req" field that contains the request channel object. */ |
| 4938 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 4939 | if (!hlua_channel_new(L, &s->req)) |
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 | |
| 4943 | /* Create the "res" field that contains the response channel object. */ |
| 4944 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 4945 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 4946 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 4947 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 4948 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4949 | /* Creates the HTTP object is the current proxy allows http. */ |
| 4950 | lua_pushstring(L, "http"); |
| 4951 | if (p->mode == PR_MODE_HTTP) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 4952 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4953 | return 0; |
| 4954 | } |
| 4955 | else |
| 4956 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 4957 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4958 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4959 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 4960 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 4961 | lua_setmetatable(L, -2); |
| 4962 | |
| 4963 | return 1; |
| 4964 | } |
| 4965 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 4966 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 4967 | { |
| 4968 | const char *msg; |
| 4969 | struct hlua_txn *htxn; |
| 4970 | |
| 4971 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 4972 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 4973 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4974 | |
| 4975 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 4976 | return 0; |
| 4977 | } |
| 4978 | |
| 4979 | __LJMP static int hlua_txn_log(lua_State *L) |
| 4980 | { |
| 4981 | int level; |
| 4982 | const char *msg; |
| 4983 | struct hlua_txn *htxn; |
| 4984 | |
| 4985 | MAY_LJMP(check_args(L, 3, "log")); |
| 4986 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 4987 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4988 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 4989 | |
| 4990 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 4991 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 4992 | |
| 4993 | hlua_sendlog(htxn->s->be, level, msg); |
| 4994 | return 0; |
| 4995 | } |
| 4996 | |
| 4997 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 4998 | { |
| 4999 | const char *msg; |
| 5000 | struct hlua_txn *htxn; |
| 5001 | |
| 5002 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 5003 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5004 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5005 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 5006 | return 0; |
| 5007 | } |
| 5008 | |
| 5009 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 5010 | { |
| 5011 | const char *msg; |
| 5012 | struct hlua_txn *htxn; |
| 5013 | |
| 5014 | MAY_LJMP(check_args(L, 2, "Info")); |
| 5015 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5016 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5017 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 5018 | return 0; |
| 5019 | } |
| 5020 | |
| 5021 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 5022 | { |
| 5023 | const char *msg; |
| 5024 | struct hlua_txn *htxn; |
| 5025 | |
| 5026 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 5027 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5028 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5029 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 5030 | return 0; |
| 5031 | } |
| 5032 | |
| 5033 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 5034 | { |
| 5035 | const char *msg; |
| 5036 | struct hlua_txn *htxn; |
| 5037 | |
| 5038 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 5039 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5040 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5041 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 5042 | return 0; |
| 5043 | } |
| 5044 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5045 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 5046 | { |
| 5047 | struct hlua_txn *htxn; |
| 5048 | int ll; |
| 5049 | |
| 5050 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 5051 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5052 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5053 | |
| 5054 | if (ll < 0 || ll > 7) |
| 5055 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 5056 | |
| 5057 | htxn->s->logs.level = ll; |
| 5058 | return 0; |
| 5059 | } |
| 5060 | |
| 5061 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 5062 | { |
| 5063 | struct hlua_txn *htxn; |
| 5064 | struct connection *cli_conn; |
| 5065 | int tos; |
| 5066 | |
| 5067 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 5068 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5069 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5070 | |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 5071 | 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] | 5072 | 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] | 5073 | |
| 5074 | return 0; |
| 5075 | } |
| 5076 | |
| 5077 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 5078 | { |
| 5079 | #ifdef SO_MARK |
| 5080 | struct hlua_txn *htxn; |
| 5081 | struct connection *cli_conn; |
| 5082 | int mark; |
| 5083 | |
| 5084 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 5085 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5086 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5087 | |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 5088 | 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] | 5089 | 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] | 5090 | #endif |
| 5091 | return 0; |
| 5092 | } |
| 5093 | |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5094 | /* This function is an Lua binding that send pending data |
| 5095 | * to the client, and close the stream interface. |
| 5096 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5097 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5098 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5099 | struct hlua_txn *htxn; |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 5100 | struct hlua *hlua; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5101 | struct channel *ic, *oc; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5102 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5103 | MAY_LJMP(check_args(L, 1, "close")); |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5104 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 5105 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5106 | |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5107 | /* If the flags NOTERM is set, we cannot terminate the http |
| 5108 | * session, so we just end the execution of the current |
| 5109 | * lua code. |
| 5110 | */ |
| 5111 | if (htxn->flags & HLUA_TXN_NOTERM) { |
| 5112 | WILL_LJMP(hlua_done(L)); |
| 5113 | return 0; |
| 5114 | } |
| 5115 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5116 | ic = &htxn->s->req; |
| 5117 | oc = &htxn->s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5118 | |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 5119 | if (htxn->s->txn) { |
| 5120 | /* HTTP mode, let's stay in sync with the stream */ |
| 5121 | bi_fast_delete(ic->buf, htxn->s->txn->req.sov); |
| 5122 | htxn->s->txn->req.next -= htxn->s->txn->req.sov; |
| 5123 | htxn->s->txn->req.sov = 0; |
| 5124 | ic->analysers &= AN_REQ_HTTP_XFER_BODY; |
| 5125 | oc->analysers = AN_RES_HTTP_XFER_BODY; |
| 5126 | htxn->s->txn->req.msg_state = HTTP_MSG_CLOSED; |
| 5127 | htxn->s->txn->rsp.msg_state = HTTP_MSG_DONE; |
| 5128 | |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 5129 | /* Note that if we want to support keep-alive, we need |
| 5130 | * to bypass the close/shutr_now calls below, but that |
| 5131 | * may only be done if the HTTP request was already |
| 5132 | * processed and the connection header is known (ie |
| 5133 | * not during TCP rules). |
| 5134 | */ |
| 5135 | } |
| 5136 | |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 5137 | channel_auto_read(ic); |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5138 | channel_abort(ic); |
| 5139 | channel_auto_close(ic); |
| 5140 | channel_erase(ic); |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 5141 | |
| 5142 | oc->wex = tick_add_ifset(now_ms, oc->wto); |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5143 | channel_auto_read(oc); |
| 5144 | channel_auto_close(oc); |
| 5145 | channel_shutr_now(oc); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5146 | |
Willy Tarreau | 0458b08 | 2015-08-28 09:40:04 +0200 | [diff] [blame] | 5147 | ic->analysers = 0; |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5148 | |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 5149 | hlua->flags |= HLUA_STOP; |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5150 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5151 | return 0; |
| 5152 | } |
| 5153 | |
| 5154 | __LJMP static int hlua_log(lua_State *L) |
| 5155 | { |
| 5156 | int level; |
| 5157 | const char *msg; |
| 5158 | |
| 5159 | MAY_LJMP(check_args(L, 2, "log")); |
| 5160 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 5161 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5162 | |
| 5163 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5164 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5165 | |
| 5166 | hlua_sendlog(NULL, level, msg); |
| 5167 | return 0; |
| 5168 | } |
| 5169 | |
| 5170 | __LJMP static int hlua_log_debug(lua_State *L) |
| 5171 | { |
| 5172 | const char *msg; |
| 5173 | |
| 5174 | MAY_LJMP(check_args(L, 1, "debug")); |
| 5175 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5176 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 5177 | return 0; |
| 5178 | } |
| 5179 | |
| 5180 | __LJMP static int hlua_log_info(lua_State *L) |
| 5181 | { |
| 5182 | const char *msg; |
| 5183 | |
| 5184 | MAY_LJMP(check_args(L, 1, "info")); |
| 5185 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5186 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 5187 | return 0; |
| 5188 | } |
| 5189 | |
| 5190 | __LJMP static int hlua_log_warning(lua_State *L) |
| 5191 | { |
| 5192 | const char *msg; |
| 5193 | |
| 5194 | MAY_LJMP(check_args(L, 1, "warning")); |
| 5195 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5196 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 5197 | return 0; |
| 5198 | } |
| 5199 | |
| 5200 | __LJMP static int hlua_log_alert(lua_State *L) |
| 5201 | { |
| 5202 | const char *msg; |
| 5203 | |
| 5204 | MAY_LJMP(check_args(L, 1, "alert")); |
| 5205 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5206 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5207 | return 0; |
| 5208 | } |
| 5209 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5210 | __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] | 5211 | { |
| 5212 | int wakeup_ms = lua_tointeger(L, -1); |
| 5213 | if (now_ms < wakeup_ms) |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5214 | 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] | 5215 | return 0; |
| 5216 | } |
| 5217 | |
| 5218 | __LJMP static int hlua_sleep(lua_State *L) |
| 5219 | { |
| 5220 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5221 | unsigned int 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 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5224 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5225 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5226 | wakeup_ms = tick_add(now_ms, delay); |
| 5227 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5228 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5229 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0)); |
| 5230 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5231 | } |
| 5232 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5233 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5234 | { |
| 5235 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5236 | unsigned int 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 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5239 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5240 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5241 | wakeup_ms = tick_add(now_ms, delay); |
| 5242 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5243 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5244 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0)); |
| 5245 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5246 | } |
| 5247 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5248 | /* This functionis an LUA binding. it permits to give back |
| 5249 | * the hand at the HAProxy scheduler. It is used when the |
| 5250 | * LUA processing consumes a lot of time. |
| 5251 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5252 | __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] | 5253 | { |
| 5254 | return 0; |
| 5255 | } |
| 5256 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5257 | __LJMP static int hlua_yield(lua_State *L) |
| 5258 | { |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5259 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD)); |
| 5260 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5261 | } |
| 5262 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5263 | /* This function change the nice of the currently executed |
| 5264 | * task. It is used set low or high priority at the current |
| 5265 | * task. |
| 5266 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5267 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5268 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5269 | struct hlua *hlua; |
| 5270 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5271 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5272 | MAY_LJMP(check_args(L, 1, "set_nice")); |
| 5273 | hlua = hlua_gethlua(L); |
| 5274 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5275 | |
| 5276 | /* If he task is not set, I'm in a start mode. */ |
| 5277 | if (!hlua || !hlua->task) |
| 5278 | return 0; |
| 5279 | |
| 5280 | if (nice < -1024) |
| 5281 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5282 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5283 | nice = 1024; |
| 5284 | |
| 5285 | hlua->task->nice = nice; |
| 5286 | return 0; |
| 5287 | } |
| 5288 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5289 | /* This function is used as a calback of a task. It is called by the |
| 5290 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 5291 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 5292 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5293 | * |
| 5294 | * Task wrapper are longjmp safe because the only one Lua code |
| 5295 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5296 | */ |
| 5297 | static struct task *hlua_process_task(struct task *task) |
| 5298 | { |
| 5299 | struct hlua *hlua = task->context; |
| 5300 | enum hlua_exec status; |
| 5301 | |
| 5302 | /* We need to remove the task from the wait queue before executing |
| 5303 | * the Lua code because we don't know if it needs to wait for |
| 5304 | * another timer or not in the case of E_AGAIN. |
| 5305 | */ |
| 5306 | task_delete(task); |
| 5307 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5308 | /* If it is the first call to the task, we must initialize the |
| 5309 | * execution timeouts. |
| 5310 | */ |
| 5311 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 5312 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5313 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5314 | /* Execute the Lua code. */ |
| 5315 | status = hlua_ctx_resume(hlua, 1); |
| 5316 | |
| 5317 | switch (status) { |
| 5318 | /* finished or yield */ |
| 5319 | case HLUA_E_OK: |
| 5320 | hlua_ctx_destroy(hlua); |
| 5321 | task_delete(task); |
| 5322 | task_free(task); |
| 5323 | break; |
| 5324 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 5325 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
| 5326 | if (hlua->wake_time != TICK_ETERNITY) |
| 5327 | task_schedule(task, hlua->wake_time); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5328 | break; |
| 5329 | |
| 5330 | /* finished with error. */ |
| 5331 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5332 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5333 | hlua_ctx_destroy(hlua); |
| 5334 | task_delete(task); |
| 5335 | task_free(task); |
| 5336 | break; |
| 5337 | |
| 5338 | case HLUA_E_ERR: |
| 5339 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5340 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5341 | hlua_ctx_destroy(hlua); |
| 5342 | task_delete(task); |
| 5343 | task_free(task); |
| 5344 | break; |
| 5345 | } |
| 5346 | return NULL; |
| 5347 | } |
| 5348 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 5349 | /* This function is an LUA binding that register LUA function to be |
| 5350 | * executed after the HAProxy configuration parsing and before the |
| 5351 | * HAProxy scheduler starts. This function expect only one LUA |
| 5352 | * argument that is a function. This function returns nothing, but |
| 5353 | * throws if an error is encountered. |
| 5354 | */ |
| 5355 | __LJMP static int hlua_register_init(lua_State *L) |
| 5356 | { |
| 5357 | struct hlua_init_function *init; |
| 5358 | int ref; |
| 5359 | |
| 5360 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 5361 | |
| 5362 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 5363 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5364 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 5365 | if (!init) |
| 5366 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5367 | |
| 5368 | init->function_ref = ref; |
| 5369 | LIST_ADDQ(&hlua_init_functions, &init->l); |
| 5370 | return 0; |
| 5371 | } |
| 5372 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5373 | /* This functio is an LUA binding. It permits to register a task |
| 5374 | * executed in parallel of the main HAroxy activity. The task is |
| 5375 | * created and it is set in the HAProxy scheduler. It can be called |
| 5376 | * from the "init" section, "post init" or during the runtime. |
| 5377 | * |
| 5378 | * Lua prototype: |
| 5379 | * |
| 5380 | * <none> core.register_task(<function>) |
| 5381 | */ |
| 5382 | static int hlua_register_task(lua_State *L) |
| 5383 | { |
| 5384 | struct hlua *hlua; |
| 5385 | struct task *task; |
| 5386 | int ref; |
| 5387 | |
| 5388 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 5389 | |
| 5390 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 5391 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5392 | hlua = pool_alloc2(pool2_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5393 | if (!hlua) |
| 5394 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5395 | |
| 5396 | task = task_new(); |
| 5397 | task->context = hlua; |
| 5398 | task->process = hlua_process_task; |
| 5399 | |
| 5400 | if (!hlua_ctx_init(hlua, task)) |
| 5401 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5402 | |
| 5403 | /* Restore the function in the stack. */ |
| 5404 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 5405 | hlua->nargs = 0; |
| 5406 | |
| 5407 | /* Schedule task. */ |
| 5408 | task_schedule(task, now_ms); |
| 5409 | |
| 5410 | return 0; |
| 5411 | } |
| 5412 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5413 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 5414 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 5415 | * resume converters. |
| 5416 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5417 | 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] | 5418 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5419 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5420 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5421 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5422 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5423 | if (!stream) |
| 5424 | return 0; |
| 5425 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5426 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5427 | * Lua context can be not initialized. This behavior |
| 5428 | * permits to save performances because a systematic |
| 5429 | * Lua initialization cause 5% performances loss. |
| 5430 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5431 | if (!stream->hlua) { |
| 5432 | stream->hlua = pool_alloc2(pool2_hlua); |
| 5433 | if (!stream->hlua) { |
| 5434 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 5435 | return 0; |
| 5436 | } |
| 5437 | if (!hlua_ctx_init(stream->hlua, stream->task)) { |
| 5438 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 5439 | return 0; |
| 5440 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5441 | } |
| 5442 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5443 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5444 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5445 | |
| 5446 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5447 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 5448 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 5449 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5450 | else |
| 5451 | error = "critical error"; |
| 5452 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5453 | return 0; |
| 5454 | } |
| 5455 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5456 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5457 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5458 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5459 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5460 | return 0; |
| 5461 | } |
| 5462 | |
| 5463 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5464 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5465 | |
| 5466 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5467 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5468 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5469 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5470 | return 0; |
| 5471 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5472 | hlua_smp2lua(stream->hlua->T, smp); |
| 5473 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5474 | |
| 5475 | /* push keywords in the stack. */ |
| 5476 | if (arg_p) { |
| 5477 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5478 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5479 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5480 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5481 | return 0; |
| 5482 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5483 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 5484 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5485 | } |
| 5486 | } |
| 5487 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5488 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5489 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5490 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5491 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5492 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5493 | } |
| 5494 | |
| 5495 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5496 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5497 | /* finished. */ |
| 5498 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 5499 | /* If the stack is empty, the function fails. */ |
| 5500 | if (lua_gettop(stream->hlua->T) <= 0) |
| 5501 | return 0; |
| 5502 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5503 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5504 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 5505 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5506 | return 1; |
| 5507 | |
| 5508 | /* yield. */ |
| 5509 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5510 | 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] | 5511 | return 0; |
| 5512 | |
| 5513 | /* finished with error. */ |
| 5514 | case HLUA_E_ERRMSG: |
| 5515 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5516 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5517 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 5518 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5519 | return 0; |
| 5520 | |
| 5521 | case HLUA_E_ERR: |
| 5522 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5523 | 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] | 5524 | |
| 5525 | default: |
| 5526 | return 0; |
| 5527 | } |
| 5528 | } |
| 5529 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5530 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 5531 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5532 | * resume sample-fetches. This function will be called by the sample |
| 5533 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5534 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5535 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 5536 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5537 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5538 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5539 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5540 | const char *error; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5541 | const struct chunk msg = { .len = 0 }; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5542 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5543 | if (!stream) |
| 5544 | return 0; |
| 5545 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5546 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5547 | * Lua context can be not initialized. This behavior |
| 5548 | * permits to save performances because a systematic |
| 5549 | * Lua initialization cause 5% performances loss. |
| 5550 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5551 | if (!stream->hlua) { |
| 5552 | stream->hlua = pool_alloc2(pool2_hlua); |
| 5553 | if (!stream->hlua) { |
| 5554 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 5555 | return 0; |
| 5556 | } |
| 5557 | if (!hlua_ctx_init(stream->hlua, stream->task)) { |
| 5558 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 5559 | return 0; |
| 5560 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5561 | } |
| 5562 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5563 | consistency_set(stream, smp->opt, &stream->hlua->cons); |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5564 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5565 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5566 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5567 | |
| 5568 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5569 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 5570 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 5571 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5572 | else |
| 5573 | error = "critical error"; |
| 5574 | 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] | 5575 | return 0; |
| 5576 | } |
| 5577 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5578 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5579 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5580 | 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] | 5581 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5582 | return 0; |
| 5583 | } |
| 5584 | |
| 5585 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5586 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5587 | |
| 5588 | /* push arguments in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5589 | 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] | 5590 | HLUA_TXN_NOTERM)) { |
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 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5596 | |
| 5597 | /* push keywords in the stack. */ |
| 5598 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 5599 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5600 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5601 | 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] | 5602 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5603 | return 0; |
| 5604 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5605 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 5606 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5607 | } |
| 5608 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5609 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5610 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5611 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5612 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5613 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5614 | } |
| 5615 | |
| 5616 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5617 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5618 | /* finished. */ |
| 5619 | case HLUA_E_OK: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5620 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5621 | stream_int_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 5622 | return 0; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5623 | } |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 5624 | /* If the stack is empty, the function fails. */ |
| 5625 | if (lua_gettop(stream->hlua->T) <= 0) |
| 5626 | return 0; |
| 5627 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5628 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5629 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 5630 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5631 | |
| 5632 | /* Set the end of execution flag. */ |
| 5633 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 5634 | return 1; |
| 5635 | |
| 5636 | /* yield. */ |
| 5637 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5638 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5639 | stream_int_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5640 | 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] | 5641 | return 0; |
| 5642 | |
| 5643 | /* finished with error. */ |
| 5644 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5645 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5646 | stream_int_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5647 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5648 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5649 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 5650 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5651 | return 0; |
| 5652 | |
| 5653 | case HLUA_E_ERR: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5654 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5655 | stream_int_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5656 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5657 | 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] | 5658 | |
| 5659 | default: |
| 5660 | return 0; |
| 5661 | } |
| 5662 | } |
| 5663 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5664 | /* This function is an LUA binding used for registering |
| 5665 | * "sample-conv" functions. It expects a converter name used |
| 5666 | * in the haproxy configuration file, and an LUA function. |
| 5667 | */ |
| 5668 | __LJMP static int hlua_register_converters(lua_State *L) |
| 5669 | { |
| 5670 | struct sample_conv_kw_list *sck; |
| 5671 | const char *name; |
| 5672 | int ref; |
| 5673 | int len; |
| 5674 | struct hlua_function *fcn; |
| 5675 | |
| 5676 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 5677 | |
| 5678 | /* First argument : converter name. */ |
| 5679 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5680 | |
| 5681 | /* Second argument : lua function. */ |
| 5682 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 5683 | |
| 5684 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5685 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5686 | if (!sck) |
| 5687 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5688 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5689 | if (!fcn) |
| 5690 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5691 | |
| 5692 | /* Fill fcn. */ |
| 5693 | fcn->name = strdup(name); |
| 5694 | if (!fcn->name) |
| 5695 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5696 | fcn->function_ref = ref; |
| 5697 | |
| 5698 | /* List head */ |
| 5699 | sck->list.n = sck->list.p = NULL; |
| 5700 | |
| 5701 | /* converter keyword. */ |
| 5702 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5703 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5704 | if (!sck->kw[0].kw) |
| 5705 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5706 | |
| 5707 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 5708 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 5709 | 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] | 5710 | sck->kw[0].val_args = NULL; |
| 5711 | sck->kw[0].in_type = SMP_T_STR; |
| 5712 | sck->kw[0].out_type = SMP_T_STR; |
| 5713 | sck->kw[0].private = fcn; |
| 5714 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5715 | /* Register this new converter */ |
| 5716 | sample_register_convs(sck); |
| 5717 | |
| 5718 | return 0; |
| 5719 | } |
| 5720 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5721 | /* This fucntion is an LUA binding used for registering |
| 5722 | * "sample-fetch" functions. It expects a converter name used |
| 5723 | * in the haproxy configuration file, and an LUA function. |
| 5724 | */ |
| 5725 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 5726 | { |
| 5727 | const char *name; |
| 5728 | int ref; |
| 5729 | int len; |
| 5730 | struct sample_fetch_kw_list *sfk; |
| 5731 | struct hlua_function *fcn; |
| 5732 | |
| 5733 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 5734 | |
| 5735 | /* First argument : sample-fetch name. */ |
| 5736 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5737 | |
| 5738 | /* Second argument : lua function. */ |
| 5739 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 5740 | |
| 5741 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5742 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5743 | if (!sfk) |
| 5744 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5745 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5746 | if (!fcn) |
| 5747 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5748 | |
| 5749 | /* Fill fcn. */ |
| 5750 | fcn->name = strdup(name); |
| 5751 | if (!fcn->name) |
| 5752 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5753 | fcn->function_ref = ref; |
| 5754 | |
| 5755 | /* List head */ |
| 5756 | sfk->list.n = sfk->list.p = NULL; |
| 5757 | |
| 5758 | /* sample-fetch keyword. */ |
| 5759 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5760 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5761 | if (!sfk->kw[0].kw) |
| 5762 | return luaL_error(L, "lua out of memory error."); |
| 5763 | |
| 5764 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 5765 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 5766 | 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] | 5767 | sfk->kw[0].val_args = NULL; |
| 5768 | sfk->kw[0].out_type = SMP_T_STR; |
| 5769 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 5770 | sfk->kw[0].val = 0; |
| 5771 | sfk->kw[0].private = fcn; |
| 5772 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5773 | /* Register this new fetch. */ |
| 5774 | sample_register_fetches(sfk); |
| 5775 | |
| 5776 | return 0; |
| 5777 | } |
| 5778 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5779 | /* This function is a wrapper to execute each LUA function declared |
| 5780 | * as an action wrapper during the initialisation period. This function |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5781 | * return ACT_RET_CONT if the processing is finished (with or without |
| 5782 | * error) and return ACT_RET_YIELD if the function must be called again |
| 5783 | * because the LUA returns a yield. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5784 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5785 | 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] | 5786 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5787 | { |
| 5788 | char **arg; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5789 | unsigned int analyzer; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 5790 | int dir; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5791 | const char *error; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5792 | const struct chunk msg = { .len = 0 }; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5793 | |
| 5794 | switch (rule->from) { |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 5795 | case ACT_F_TCP_REQ_CNT: analyzer = AN_REQ_INSPECT_FE ; dir = SMP_OPT_DIR_REQ; break; |
| 5796 | case ACT_F_TCP_RES_CNT: analyzer = AN_RES_INSPECT ; dir = SMP_OPT_DIR_RES; break; |
| 5797 | case ACT_F_HTTP_REQ: analyzer = AN_REQ_HTTP_PROCESS_FE; dir = SMP_OPT_DIR_REQ; break; |
| 5798 | 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] | 5799 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5800 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5801 | return ACT_RET_CONT; |
| 5802 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5803 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5804 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5805 | * Lua context can be not initialized. This behavior |
| 5806 | * permits to save performances because a systematic |
| 5807 | * Lua initialization cause 5% performances loss. |
| 5808 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5809 | if (!s->hlua) { |
| 5810 | s->hlua = pool_alloc2(pool2_hlua); |
| 5811 | if (!s->hlua) { |
| 5812 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 5813 | rule->arg.hlua_rule->fcn.name); |
| 5814 | return ACT_RET_CONT; |
| 5815 | } |
| 5816 | if (!hlua_ctx_init(s->hlua, s->task)) { |
| 5817 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 5818 | rule->arg.hlua_rule->fcn.name); |
| 5819 | return ACT_RET_CONT; |
| 5820 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5821 | } |
| 5822 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5823 | consistency_set(s, dir, &s->hlua->cons); |
| 5824 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5825 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5826 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5827 | |
| 5828 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5829 | if (!SET_SAFE_LJMP(s->hlua->T)) { |
| 5830 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 5831 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5832 | else |
| 5833 | error = "critical error"; |
| 5834 | SEND_ERR(px, "Lua function '%s': %s.\n", |
| 5835 | rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5836 | return ACT_RET_CONT; |
| 5837 | } |
| 5838 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5839 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5840 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5841 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5842 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5843 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 5844 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5845 | } |
| 5846 | |
| 5847 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5848 | 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] | 5849 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5850 | /* Create and and push object stream in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5851 | if (!hlua_txn_new(s->hlua->T, s, px, dir, 0)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5852 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5853 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5854 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 5855 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5856 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5857 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5858 | |
| 5859 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5860 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5861 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5862 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5863 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5864 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 5865 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5866 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5867 | lua_pushstring(s->hlua->T, *arg); |
| 5868 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5869 | } |
| 5870 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5871 | /* Now the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5872 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5873 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5874 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5875 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5876 | } |
| 5877 | |
| 5878 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5879 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_FLAG_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5880 | /* finished. */ |
| 5881 | case HLUA_E_OK: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5882 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5883 | stream_int_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 5884 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5885 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5886 | if (s->hlua->flags & HLUA_STOP) |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 5887 | return ACT_RET_STOP; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 5888 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5889 | |
| 5890 | /* yield. */ |
| 5891 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 5892 | /* Set timeout in the required channel. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5893 | if (s->hlua->wake_time != TICK_ETERNITY) { |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 5894 | if (analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE)) |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5895 | s->req.analyse_exp = s->hlua->wake_time; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 5896 | else if (analyzer & (AN_RES_INSPECT|AN_RES_HTTP_PROCESS_BE)) |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5897 | s->res.analyse_exp = s->hlua->wake_time; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 5898 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5899 | /* Some actions can be wake up when a "write" event |
| 5900 | * is detected on a response channel. This is useful |
| 5901 | * only for actions targetted on the requests. |
| 5902 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5903 | if (HLUA_IS_WAKERESWR(s->hlua)) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5904 | s->res.flags |= CF_WAKE_WRITE; |
Willy Tarreau | 76bd97f | 2015-03-10 17:16:10 +0100 | [diff] [blame] | 5905 | if ((analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE))) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5906 | s->res.analysers |= analyzer; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5907 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5908 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5909 | s->req.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5910 | /* We can quit the fcuntion without consistency check |
| 5911 | * because HAProxy is not able to manipulate data, it |
| 5912 | * is only allowed to call me again. */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 5913 | return ACT_RET_YIELD; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5914 | |
| 5915 | /* finished with error. */ |
| 5916 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5917 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5918 | stream_int_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 5919 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5920 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5921 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5922 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5923 | rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1)); |
| 5924 | lua_pop(s->hlua->T, 1); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 5925 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5926 | |
| 5927 | case HLUA_E_ERR: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5928 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5929 | stream_int_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 5930 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5931 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5932 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5933 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5934 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5935 | |
| 5936 | default: |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 5937 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5938 | } |
| 5939 | } |
| 5940 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 5941 | struct task *hlua_applet_wakeup(struct task *t) |
| 5942 | { |
| 5943 | struct appctx *ctx = t->context; |
| 5944 | struct stream_interface *si = ctx->owner; |
| 5945 | |
| 5946 | /* If the applet is wake up without any expected work, the sheduler |
| 5947 | * remove it from the run queue. This flag indicate that the applet |
| 5948 | * is waiting for write. If the buffer is full, the main processing |
| 5949 | * will send some data and after call the applet, otherwise it call |
| 5950 | * the applet ASAP. |
| 5951 | */ |
| 5952 | si_applet_cant_put(si); |
| 5953 | appctx_wakeup(ctx); |
| 5954 | return NULL; |
| 5955 | } |
| 5956 | |
| 5957 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 5958 | { |
| 5959 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 5960 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 5961 | struct task *task; |
| 5962 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5963 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 5964 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 5965 | hlua = pool_alloc2(pool2_hlua); |
| 5966 | if (!hlua) { |
| 5967 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 5968 | ctx->rule->arg.hlua_rule->fcn.name); |
| 5969 | return 0; |
| 5970 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 5971 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 5972 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 5973 | ctx->ctx.hlua_apptcp.flags = 0; |
| 5974 | |
| 5975 | /* Create task used by signal to wakeup applets. */ |
| 5976 | task = task_new(); |
| 5977 | if (!task) { |
| 5978 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 5979 | ctx->rule->arg.hlua_rule->fcn.name); |
| 5980 | return 0; |
| 5981 | } |
| 5982 | task->nice = 0; |
| 5983 | task->context = ctx; |
| 5984 | task->process = hlua_applet_wakeup; |
| 5985 | ctx->ctx.hlua_apptcp.task = task; |
| 5986 | |
| 5987 | /* In the execution wrappers linked with a stream, the |
| 5988 | * Lua context can be not initialized. This behavior |
| 5989 | * permits to save performances because a systematic |
| 5990 | * Lua initialization cause 5% performances loss. |
| 5991 | */ |
| 5992 | if (!hlua_ctx_init(hlua, task)) { |
| 5993 | SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n", |
| 5994 | ctx->rule->arg.hlua_rule->fcn.name); |
| 5995 | return 0; |
| 5996 | } |
| 5997 | |
| 5998 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 5999 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6000 | |
| 6001 | /* The following Lua calls can fail. */ |
| 6002 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6003 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6004 | error = lua_tostring(hlua->T, -1); |
| 6005 | else |
| 6006 | error = "critical error"; |
| 6007 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 6008 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6009 | RESET_SAFE_LJMP(hlua->T); |
| 6010 | return 0; |
| 6011 | } |
| 6012 | |
| 6013 | /* Check stack available size. */ |
| 6014 | if (!lua_checkstack(hlua->T, 1)) { |
| 6015 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6016 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6017 | RESET_SAFE_LJMP(hlua->T); |
| 6018 | return 0; |
| 6019 | } |
| 6020 | |
| 6021 | /* Restore the function in the stack. */ |
| 6022 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 6023 | |
| 6024 | /* Create and and push object stream in the stack. */ |
| 6025 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 6026 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6027 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6028 | RESET_SAFE_LJMP(hlua->T); |
| 6029 | return 0; |
| 6030 | } |
| 6031 | hlua->nargs = 1; |
| 6032 | |
| 6033 | /* push keywords in the stack. */ |
| 6034 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 6035 | if (!lua_checkstack(hlua->T, 1)) { |
| 6036 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6037 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6038 | RESET_SAFE_LJMP(hlua->T); |
| 6039 | return 0; |
| 6040 | } |
| 6041 | lua_pushstring(hlua->T, *arg); |
| 6042 | hlua->nargs++; |
| 6043 | } |
| 6044 | |
| 6045 | RESET_SAFE_LJMP(hlua->T); |
| 6046 | |
| 6047 | /* Wakeup the applet ASAP. */ |
| 6048 | si_applet_cant_get(si); |
| 6049 | si_applet_cant_put(si); |
| 6050 | |
| 6051 | return 1; |
| 6052 | } |
| 6053 | |
| 6054 | static void hlua_applet_tcp_fct(struct appctx *ctx) |
| 6055 | { |
| 6056 | struct stream_interface *si = ctx->owner; |
| 6057 | struct stream *strm = si_strm(si); |
| 6058 | struct channel *res = si_ic(si); |
| 6059 | struct act_rule *rule = ctx->rule; |
| 6060 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6061 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6062 | |
| 6063 | /* The applet execution is already done. */ |
| 6064 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) |
| 6065 | return; |
| 6066 | |
| 6067 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 6068 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 6069 | return; |
| 6070 | |
| 6071 | /* Execute the function. */ |
| 6072 | switch (hlua_ctx_resume(hlua, 1)) { |
| 6073 | /* finished. */ |
| 6074 | case HLUA_E_OK: |
| 6075 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 6076 | |
| 6077 | /* log time */ |
| 6078 | strm->logs.tv_request = now; |
| 6079 | |
| 6080 | /* eat the whole request */ |
| 6081 | bo_skip(si_oc(si), si_ob(si)->o); |
| 6082 | res->flags |= CF_READ_NULL; |
| 6083 | si_shutr(si); |
| 6084 | return; |
| 6085 | |
| 6086 | /* yield. */ |
| 6087 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 6088 | if (hlua->wake_time != TICK_ETERNITY) |
| 6089 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6090 | return; |
| 6091 | |
| 6092 | /* finished with error. */ |
| 6093 | case HLUA_E_ERRMSG: |
| 6094 | /* Display log. */ |
| 6095 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 6096 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 6097 | lua_pop(hlua->T, 1); |
| 6098 | goto error; |
| 6099 | |
| 6100 | case HLUA_E_ERR: |
| 6101 | /* Display log. */ |
| 6102 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
| 6103 | rule->arg.hlua_rule->fcn.name); |
| 6104 | goto error; |
| 6105 | |
| 6106 | default: |
| 6107 | goto error; |
| 6108 | } |
| 6109 | |
| 6110 | error: |
| 6111 | |
| 6112 | /* For all other cases, just close the stream. */ |
| 6113 | si_shutw(si); |
| 6114 | si_shutr(si); |
| 6115 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 6116 | } |
| 6117 | |
| 6118 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 6119 | { |
| 6120 | task_free(ctx->ctx.hlua_apptcp.task); |
| 6121 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6122 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6123 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6124 | } |
| 6125 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6126 | /* The function returns 1 if the initialisation is complete, 0 if |
| 6127 | * an errors occurs and -1 if more data are required for initializing |
| 6128 | * the applet. |
| 6129 | */ |
| 6130 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6131 | { |
| 6132 | struct stream_interface *si = ctx->owner; |
| 6133 | struct channel *req = si_oc(si); |
| 6134 | struct http_msg *msg; |
| 6135 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6136 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6137 | char **arg; |
| 6138 | struct hdr_ctx hdr; |
| 6139 | struct task *task; |
| 6140 | 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] | 6141 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6142 | |
| 6143 | /* Wait for a full HTTP request. */ |
| 6144 | if (!smp_prefetch_http(px, strm, 0, NULL, &smp, 0)) { |
| 6145 | if (smp.flags & SMP_F_MAY_CHANGE) |
| 6146 | return -1; |
| 6147 | return 0; |
| 6148 | } |
| 6149 | txn = strm->txn; |
| 6150 | msg = &txn->req; |
| 6151 | |
Willy Tarreau | 0078bfc | 2015-10-07 20:20:28 +0200 | [diff] [blame] | 6152 | /* We want two things in HTTP mode : |
| 6153 | * - enforce server-close mode if we were in keep-alive, so that the |
| 6154 | * applet is released after each response ; |
| 6155 | * - enable request body transfer to the applet in order to resync |
| 6156 | * with the response body. |
| 6157 | */ |
| 6158 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) |
| 6159 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | 0078bfc | 2015-10-07 20:20:28 +0200 | [diff] [blame] | 6160 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6161 | hlua = pool_alloc2(pool2_hlua); |
| 6162 | if (!hlua) { |
| 6163 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 6164 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6165 | return 0; |
| 6166 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6167 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6168 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6169 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 6170 | ctx->ctx.hlua_apphttp.flags = 0; |
| 6171 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 6172 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 6173 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 6174 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6175 | /* Create task used by signal to wakeup applets. */ |
| 6176 | task = task_new(); |
| 6177 | if (!task) { |
| 6178 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 6179 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6180 | return 0; |
| 6181 | } |
| 6182 | task->nice = 0; |
| 6183 | task->context = ctx; |
| 6184 | task->process = hlua_applet_wakeup; |
| 6185 | ctx->ctx.hlua_apphttp.task = task; |
| 6186 | |
| 6187 | /* In the execution wrappers linked with a stream, the |
| 6188 | * Lua context can be not initialized. This behavior |
| 6189 | * permits to save performances because a systematic |
| 6190 | * Lua initialization cause 5% performances loss. |
| 6191 | */ |
| 6192 | if (!hlua_ctx_init(hlua, task)) { |
| 6193 | SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n", |
| 6194 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6195 | return 0; |
| 6196 | } |
| 6197 | |
| 6198 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6199 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6200 | |
| 6201 | /* The following Lua calls can fail. */ |
| 6202 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6203 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6204 | error = lua_tostring(hlua->T, -1); |
| 6205 | else |
| 6206 | error = "critical error"; |
| 6207 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 6208 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6209 | return 0; |
| 6210 | } |
| 6211 | |
| 6212 | /* Check stack available size. */ |
| 6213 | if (!lua_checkstack(hlua->T, 1)) { |
| 6214 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6215 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6216 | RESET_SAFE_LJMP(hlua->T); |
| 6217 | return 0; |
| 6218 | } |
| 6219 | |
| 6220 | /* Restore the function in the stack. */ |
| 6221 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 6222 | |
| 6223 | /* Create and and push object stream in the stack. */ |
| 6224 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 6225 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6226 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6227 | RESET_SAFE_LJMP(hlua->T); |
| 6228 | return 0; |
| 6229 | } |
| 6230 | hlua->nargs = 1; |
| 6231 | |
| 6232 | /* Look for a 100-continue expected. */ |
| 6233 | if (msg->flags & HTTP_MSGF_VER_11) { |
| 6234 | hdr.idx = 0; |
| 6235 | if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &hdr) && |
| 6236 | unlikely(hdr.vlen == 12 && strncasecmp(hdr.line+hdr.val, "100-continue", 12) == 0)) |
| 6237 | ctx->ctx.hlua_apphttp.flags |= APPLET_100C; |
| 6238 | } |
| 6239 | |
| 6240 | /* push keywords in the stack. */ |
| 6241 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 6242 | if (!lua_checkstack(hlua->T, 1)) { |
| 6243 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6244 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6245 | RESET_SAFE_LJMP(hlua->T); |
| 6246 | return 0; |
| 6247 | } |
| 6248 | lua_pushstring(hlua->T, *arg); |
| 6249 | hlua->nargs++; |
| 6250 | } |
| 6251 | |
| 6252 | RESET_SAFE_LJMP(hlua->T); |
| 6253 | |
| 6254 | /* Wakeup the applet when data is ready for read. */ |
| 6255 | si_applet_cant_get(si); |
| 6256 | |
| 6257 | return 1; |
| 6258 | } |
| 6259 | |
| 6260 | static void hlua_applet_http_fct(struct appctx *ctx) |
| 6261 | { |
| 6262 | struct stream_interface *si = ctx->owner; |
| 6263 | struct stream *strm = si_strm(si); |
| 6264 | struct channel *res = si_ic(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6265 | struct act_rule *rule = ctx->rule; |
| 6266 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6267 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6268 | char *blk1; |
| 6269 | int len1; |
| 6270 | char *blk2; |
| 6271 | int len2; |
| 6272 | int ret; |
| 6273 | |
| 6274 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 6275 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 6276 | return; |
| 6277 | |
| 6278 | /* Set the currently running flag. */ |
| 6279 | if (!HLUA_IS_RUNNING(hlua) && |
| 6280 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 6281 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6282 | /* Wait for full HTTP analysys. */ |
| 6283 | if (unlikely(strm->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 6284 | si_applet_cant_get(si); |
| 6285 | return; |
| 6286 | } |
| 6287 | |
| 6288 | /* Store the max amount of bytes that we can read. */ |
| 6289 | ctx->ctx.hlua_apphttp.left_bytes = strm->txn->req.body_len; |
| 6290 | |
| 6291 | /* We need to flush the request header. This left the body |
| 6292 | * for the Lua. |
| 6293 | */ |
| 6294 | |
| 6295 | /* Read the maximum amount of data avalaible. */ |
| 6296 | ret = bo_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
| 6297 | if (ret == -1) |
| 6298 | return; |
| 6299 | |
| 6300 | /* No data available, ask for more data. */ |
| 6301 | if (ret == 1) |
| 6302 | len2 = 0; |
| 6303 | if (ret == 0) |
| 6304 | len1 = 0; |
| 6305 | if (len1 + len2 < strm->txn->req.eoh + 2) { |
| 6306 | si_applet_cant_get(si); |
| 6307 | return; |
| 6308 | } |
| 6309 | |
| 6310 | /* skip the requests bytes. */ |
| 6311 | bo_skip(si_oc(si), strm->txn->req.eoh + 2); |
| 6312 | } |
| 6313 | |
| 6314 | /* Executes The applet if it is not done. */ |
| 6315 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 6316 | |
| 6317 | /* Execute the function. */ |
| 6318 | switch (hlua_ctx_resume(hlua, 1)) { |
| 6319 | /* finished. */ |
| 6320 | case HLUA_E_OK: |
| 6321 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 6322 | break; |
| 6323 | |
| 6324 | /* yield. */ |
| 6325 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 6326 | if (hlua->wake_time != TICK_ETERNITY) |
| 6327 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6328 | return; |
| 6329 | |
| 6330 | /* finished with error. */ |
| 6331 | case HLUA_E_ERRMSG: |
| 6332 | /* Display log. */ |
| 6333 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 6334 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 6335 | lua_pop(hlua->T, 1); |
| 6336 | goto error; |
| 6337 | |
| 6338 | case HLUA_E_ERR: |
| 6339 | /* Display log. */ |
| 6340 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
| 6341 | rule->arg.hlua_rule->fcn.name); |
| 6342 | goto error; |
| 6343 | |
| 6344 | default: |
| 6345 | goto error; |
| 6346 | } |
| 6347 | } |
| 6348 | |
| 6349 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
| 6350 | |
| 6351 | /* We must send the final chunk. */ |
| 6352 | if (ctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED && |
| 6353 | !(ctx->ctx.hlua_apphttp.flags & APPLET_LAST_CHK)) { |
| 6354 | |
| 6355 | /* sent last chunk at once. */ |
| 6356 | ret = bi_putblk(res, "0\r\n\r\n", 5); |
| 6357 | |
| 6358 | /* critical error. */ |
| 6359 | if (ret == -2 || ret == -3) { |
| 6360 | SEND_ERR(px, "Lua applet http '%s'cannont send last chunk.\n", |
| 6361 | rule->arg.hlua_rule->fcn.name); |
| 6362 | goto error; |
| 6363 | } |
| 6364 | |
| 6365 | /* no enough space error. */ |
| 6366 | if (ret == -1) { |
| 6367 | si_applet_cant_put(si); |
| 6368 | return; |
| 6369 | } |
| 6370 | |
| 6371 | /* set the last chunk sent. */ |
| 6372 | ctx->ctx.hlua_apphttp.flags |= APPLET_LAST_CHK; |
| 6373 | } |
| 6374 | |
| 6375 | /* close the connection. */ |
| 6376 | |
| 6377 | /* status / log */ |
| 6378 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 6379 | strm->logs.tv_request = now; |
| 6380 | |
| 6381 | /* eat the whole request */ |
| 6382 | bo_skip(si_oc(si), si_ob(si)->o); |
| 6383 | res->flags |= CF_READ_NULL; |
| 6384 | si_shutr(si); |
| 6385 | |
| 6386 | return; |
| 6387 | } |
| 6388 | |
| 6389 | error: |
| 6390 | |
| 6391 | /* If we are in HTTP mode, and we are not send any |
| 6392 | * data, return a 500 server error in best effort: |
| 6393 | * if there are no room avalaible in the buffer, |
| 6394 | * just close the connection. |
| 6395 | */ |
| 6396 | bi_putblk(res, error_500, strlen(error_500)); |
| 6397 | if (!(strm->flags & SF_ERR_MASK)) |
| 6398 | strm->flags |= SF_ERR_RESOURCE; |
| 6399 | si_shutw(si); |
| 6400 | si_shutr(si); |
| 6401 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 6402 | } |
| 6403 | |
| 6404 | static void hlua_applet_http_release(struct appctx *ctx) |
| 6405 | { |
| 6406 | task_free(ctx->ctx.hlua_apphttp.task); |
| 6407 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6408 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6409 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6410 | } |
| 6411 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6412 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
| 6413 | * succes case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6414 | * |
| 6415 | * This function can fail with an abort() due to an Lua critical error. |
| 6416 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 6417 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6418 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6419 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 6420 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6421 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6422 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6423 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6424 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6425 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6426 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6427 | if (!rule->arg.hlua_rule) { |
| 6428 | memprintf(err, "out of memory error"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 6429 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6430 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6431 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6432 | /* Memory for arguments. */ |
| 6433 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *)); |
| 6434 | if (!rule->arg.hlua_rule->args) { |
| 6435 | memprintf(err, "out of memory error"); |
| 6436 | return ACT_RET_PRS_ERR; |
| 6437 | } |
| 6438 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6439 | /* Reference the Lua function and store the reference. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6440 | rule->arg.hlua_rule->fcn = *fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6441 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6442 | /* Expect some arguments */ |
| 6443 | for (i = 0; i < fcn->nargs; i++) { |
| 6444 | if (*args[i+1] == '\0') { |
| 6445 | memprintf(err, "expect %d arguments", fcn->nargs); |
| 6446 | return ACT_RET_PRS_ERR; |
| 6447 | } |
| 6448 | rule->arg.hlua_rule->args[i] = strdup(args[i + 1]); |
| 6449 | if (!rule->arg.hlua_rule->args[i]) { |
| 6450 | memprintf(err, "out of memory error"); |
| 6451 | return ACT_RET_PRS_ERR; |
| 6452 | } |
| 6453 | (*cur_arg)++; |
| 6454 | } |
| 6455 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6456 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 6457 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6458 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 6459 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6460 | } |
| 6461 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6462 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 6463 | struct act_rule *rule, char **err) |
| 6464 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6465 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6466 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 6467 | /* HTTP applets are forbidden in tcp-request rules. |
| 6468 | * HTTP applet request requires everything initilized by |
| 6469 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
| 6470 | * The applet will be immediately initilized, but its before |
| 6471 | * the call of this analyzer. |
| 6472 | */ |
| 6473 | if (rule->from != ACT_F_HTTP_REQ) { |
| 6474 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 6475 | return ACT_RET_PRS_ERR; |
| 6476 | } |
| 6477 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6478 | /* Memory for the rule. */ |
| 6479 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 6480 | if (!rule->arg.hlua_rule) { |
| 6481 | memprintf(err, "out of memory error"); |
| 6482 | return ACT_RET_PRS_ERR; |
| 6483 | } |
| 6484 | |
| 6485 | /* Reference the Lua function and store the reference. */ |
| 6486 | rule->arg.hlua_rule->fcn = *fcn; |
| 6487 | |
| 6488 | /* TODO: later accept arguments. */ |
| 6489 | rule->arg.hlua_rule->args = NULL; |
| 6490 | |
| 6491 | /* Add applet pointer in the rule. */ |
| 6492 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 6493 | rule->applet.name = fcn->name; |
| 6494 | rule->applet.init = hlua_applet_http_init; |
| 6495 | rule->applet.fct = hlua_applet_http_fct; |
| 6496 | rule->applet.release = hlua_applet_http_release; |
| 6497 | rule->applet.timeout = hlua_timeout_applet; |
| 6498 | |
| 6499 | return ACT_RET_PRS_OK; |
| 6500 | } |
| 6501 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6502 | /* This function is an LUA binding used for registering |
| 6503 | * "sample-conv" functions. It expects a converter name used |
| 6504 | * in the haproxy configuration file, and an LUA function. |
| 6505 | */ |
| 6506 | __LJMP static int hlua_register_action(lua_State *L) |
| 6507 | { |
| 6508 | struct action_kw_list *akl; |
| 6509 | const char *name; |
| 6510 | int ref; |
| 6511 | int len; |
| 6512 | struct hlua_function *fcn; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6513 | int nargs; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6514 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6515 | /* Initialise the number of expected arguments at 0. */ |
| 6516 | nargs = 0; |
| 6517 | |
| 6518 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 6519 | 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] | 6520 | |
| 6521 | /* First argument : converter name. */ |
| 6522 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6523 | |
| 6524 | /* Second argument : environment. */ |
| 6525 | if (lua_type(L, 2) != LUA_TTABLE) |
| 6526 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 6527 | |
| 6528 | /* Third argument : lua function. */ |
| 6529 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 6530 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6531 | /* Fouth argument : number of mandatories arguments expected on the configuration line. */ |
| 6532 | if (lua_gettop(L) >= 4) |
| 6533 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 6534 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6535 | /* browse the second argulent as an array. */ |
| 6536 | lua_pushnil(L); |
| 6537 | while (lua_next(L, 2) != 0) { |
| 6538 | if (lua_type(L, -1) != LUA_TSTRING) |
| 6539 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 6540 | |
| 6541 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 6542 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6543 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6544 | if (!akl) |
| 6545 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6546 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6547 | if (!fcn) |
| 6548 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6549 | |
| 6550 | /* Fill fcn. */ |
| 6551 | fcn->name = strdup(name); |
| 6552 | if (!fcn->name) |
| 6553 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6554 | fcn->function_ref = ref; |
| 6555 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6556 | /* Set the expected number od arguments. */ |
| 6557 | fcn->nargs = nargs; |
| 6558 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6559 | /* List head */ |
| 6560 | akl->list.n = akl->list.p = NULL; |
| 6561 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6562 | /* action keyword. */ |
| 6563 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6564 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6565 | if (!akl->kw[0].kw) |
| 6566 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6567 | |
| 6568 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 6569 | |
| 6570 | akl->kw[0].match_pfx = 0; |
| 6571 | akl->kw[0].private = fcn; |
| 6572 | akl->kw[0].parse = action_register_lua; |
| 6573 | |
| 6574 | /* select the action registering point. */ |
| 6575 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 6576 | tcp_req_cont_keywords_register(akl); |
| 6577 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 6578 | tcp_res_cont_keywords_register(akl); |
| 6579 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 6580 | http_req_keywords_register(akl); |
| 6581 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 6582 | http_res_keywords_register(akl); |
| 6583 | else |
| 6584 | WILL_LJMP(luaL_error(L, "lua action environment '%s' is unknown. " |
| 6585 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 6586 | "are expected.", lua_tostring(L, -1))); |
| 6587 | |
| 6588 | /* pop the environment string. */ |
| 6589 | lua_pop(L, 1); |
| 6590 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6591 | return ACT_RET_PRS_OK; |
| 6592 | } |
| 6593 | |
| 6594 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 6595 | struct act_rule *rule, char **err) |
| 6596 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6597 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6598 | |
| 6599 | /* Memory for the rule. */ |
| 6600 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 6601 | if (!rule->arg.hlua_rule) { |
| 6602 | memprintf(err, "out of memory error"); |
| 6603 | return ACT_RET_PRS_ERR; |
| 6604 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6605 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6606 | /* Reference the Lua function and store the reference. */ |
| 6607 | rule->arg.hlua_rule->fcn = *fcn; |
| 6608 | |
| 6609 | /* TODO: later accept arguments. */ |
| 6610 | rule->arg.hlua_rule->args = NULL; |
| 6611 | |
| 6612 | /* Add applet pointer in the rule. */ |
| 6613 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 6614 | rule->applet.name = fcn->name; |
| 6615 | rule->applet.init = hlua_applet_tcp_init; |
| 6616 | rule->applet.fct = hlua_applet_tcp_fct; |
| 6617 | rule->applet.release = hlua_applet_tcp_release; |
| 6618 | rule->applet.timeout = hlua_timeout_applet; |
| 6619 | |
| 6620 | return 0; |
| 6621 | } |
| 6622 | |
| 6623 | /* This function is an LUA binding used for registering |
| 6624 | * "sample-conv" functions. It expects a converter name used |
| 6625 | * in the haproxy configuration file, and an LUA function. |
| 6626 | */ |
| 6627 | __LJMP static int hlua_register_service(lua_State *L) |
| 6628 | { |
| 6629 | struct action_kw_list *akl; |
| 6630 | const char *name; |
| 6631 | const char *env; |
| 6632 | int ref; |
| 6633 | int len; |
| 6634 | struct hlua_function *fcn; |
| 6635 | |
| 6636 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 6637 | |
| 6638 | /* First argument : converter name. */ |
| 6639 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6640 | |
| 6641 | /* Second argument : environment. */ |
| 6642 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6643 | |
| 6644 | /* Third argument : lua function. */ |
| 6645 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 6646 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6647 | /* Allocate and fill the sample fetch keyword struct. */ |
| 6648 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 6649 | if (!akl) |
| 6650 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6651 | fcn = calloc(1, sizeof(*fcn)); |
| 6652 | if (!fcn) |
| 6653 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6654 | |
| 6655 | /* Fill fcn. */ |
| 6656 | len = strlen("<lua.>") + strlen(name) + 1; |
| 6657 | fcn->name = calloc(1, len); |
| 6658 | if (!fcn->name) |
| 6659 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6660 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
| 6661 | fcn->function_ref = ref; |
| 6662 | |
| 6663 | /* List head */ |
| 6664 | akl->list.n = akl->list.p = NULL; |
| 6665 | |
| 6666 | /* converter keyword. */ |
| 6667 | len = strlen("lua.") + strlen(name) + 1; |
| 6668 | akl->kw[0].kw = calloc(1, len); |
| 6669 | if (!akl->kw[0].kw) |
| 6670 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6671 | |
| 6672 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 6673 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 6674 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6675 | if (strcmp(env, "tcp") == 0) |
| 6676 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6677 | else if (strcmp(env, "http") == 0) |
| 6678 | akl->kw[0].parse = action_register_service_http; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6679 | else |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6680 | WILL_LJMP(luaL_error(L, "lua service environment '%s' is unknown. " |
| 6681 | "'tcp' or 'http' are expected.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6682 | |
| 6683 | akl->kw[0].match_pfx = 0; |
| 6684 | akl->kw[0].private = fcn; |
| 6685 | |
| 6686 | /* End of array. */ |
| 6687 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 6688 | |
| 6689 | /* Register this new converter */ |
| 6690 | service_keywords_register(akl); |
| 6691 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6692 | return 0; |
| 6693 | } |
| 6694 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6695 | /* This function initialises Lua cli handler. It copies the |
| 6696 | * arguments in the Lua stack and create channel IO objects. |
| 6697 | */ |
| 6698 | static int hlua_cli_parse_fct(char **args, struct appctx *appctx, void *private) |
| 6699 | { |
| 6700 | struct hlua *hlua; |
| 6701 | struct hlua_function *fcn; |
| 6702 | int i; |
| 6703 | const char *error; |
| 6704 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6705 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6706 | appctx->ctx.hlua_cli.fcn = private; |
| 6707 | |
| 6708 | hlua = pool_alloc2(pool2_hlua); |
| 6709 | if (!hlua) { |
| 6710 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 6711 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6712 | } |
| 6713 | HLUA_INIT(hlua); |
| 6714 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6715 | |
| 6716 | /* Create task used by signal to wakeup applets. |
| 6717 | * We use the same wakeup fonction than the Lua applet_tcp and |
| 6718 | * applet_http. It is absolutely compatible. |
| 6719 | */ |
| 6720 | appctx->ctx.hlua_cli.task = task_new(); |
| 6721 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 6722 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 6723 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6724 | } |
| 6725 | appctx->ctx.hlua_cli.task->nice = 0; |
| 6726 | appctx->ctx.hlua_cli.task->context = appctx; |
| 6727 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 6728 | |
| 6729 | /* Initialises the Lua context */ |
| 6730 | if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task)) { |
| 6731 | 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] | 6732 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6733 | } |
| 6734 | |
| 6735 | /* The following Lua calls can fail. */ |
| 6736 | if (!SET_SAFE_LJMP(hlua->T)) { |
| 6737 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6738 | error = lua_tostring(hlua->T, -1); |
| 6739 | else |
| 6740 | error = "critical error"; |
| 6741 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 6742 | goto error; |
| 6743 | } |
| 6744 | |
| 6745 | /* Check stack available size. */ |
| 6746 | if (!lua_checkstack(hlua->T, 2)) { |
| 6747 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 6748 | goto error; |
| 6749 | } |
| 6750 | |
| 6751 | /* Restore the function in the stack. */ |
| 6752 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
| 6753 | |
| 6754 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 6755 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6756 | */ |
| 6757 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 6758 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 6759 | goto error; |
| 6760 | } |
| 6761 | hlua->nargs = 1; |
| 6762 | |
| 6763 | /* push keywords in the stack. */ |
| 6764 | for (i = 0; *args[i]; i++) { |
| 6765 | /* Check stack available size. */ |
| 6766 | if (!lua_checkstack(hlua->T, 1)) { |
| 6767 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 6768 | goto error; |
| 6769 | } |
| 6770 | lua_pushstring(hlua->T, args[i]); |
| 6771 | hlua->nargs++; |
| 6772 | } |
| 6773 | |
| 6774 | /* We must initialize the execution timeouts. */ |
| 6775 | hlua->max_time = hlua_timeout_session; |
| 6776 | |
| 6777 | /* At this point the execution is safe. */ |
| 6778 | RESET_SAFE_LJMP(hlua->T); |
| 6779 | |
| 6780 | /* It's ok */ |
| 6781 | return 0; |
| 6782 | |
| 6783 | /* It's not ok. */ |
| 6784 | error: |
| 6785 | RESET_SAFE_LJMP(hlua->T); |
| 6786 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 6787 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6788 | return 1; |
| 6789 | } |
| 6790 | |
| 6791 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 6792 | { |
| 6793 | struct hlua *hlua; |
| 6794 | struct stream_interface *si; |
| 6795 | struct hlua_function *fcn; |
| 6796 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6797 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6798 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 6799 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6800 | |
| 6801 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 6802 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 6803 | return 1; |
| 6804 | |
| 6805 | /* Execute the function. */ |
| 6806 | switch (hlua_ctx_resume(hlua, 1)) { |
| 6807 | |
| 6808 | /* finished. */ |
| 6809 | case HLUA_E_OK: |
| 6810 | return 1; |
| 6811 | |
| 6812 | /* yield. */ |
| 6813 | case HLUA_E_AGAIN: |
| 6814 | /* We want write. */ |
| 6815 | if (HLUA_IS_WAKERESWR(hlua)) |
| 6816 | si_applet_cant_put(si); |
| 6817 | /* Set the timeout. */ |
| 6818 | if (hlua->wake_time != TICK_ETERNITY) |
| 6819 | task_schedule(hlua->task, hlua->wake_time); |
| 6820 | return 0; |
| 6821 | |
| 6822 | /* finished with error. */ |
| 6823 | case HLUA_E_ERRMSG: |
| 6824 | /* Display log. */ |
| 6825 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 6826 | fcn->name, lua_tostring(hlua->T, -1)); |
| 6827 | lua_pop(hlua->T, 1); |
| 6828 | return 1; |
| 6829 | |
| 6830 | case HLUA_E_ERR: |
| 6831 | /* Display log. */ |
| 6832 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 6833 | fcn->name); |
| 6834 | return 1; |
| 6835 | |
| 6836 | default: |
| 6837 | return 1; |
| 6838 | } |
| 6839 | |
| 6840 | return 1; |
| 6841 | } |
| 6842 | |
| 6843 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 6844 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6845 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6846 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6847 | } |
| 6848 | |
| 6849 | /* This function is an LUA binding used for registering |
| 6850 | * new keywords in the cli. It expects a list of keywords |
| 6851 | * which are the "path". It is limited to 5 keywords. A |
| 6852 | * description of the command, a function to be executed |
| 6853 | * for the parsing and a function for io handlers. |
| 6854 | */ |
| 6855 | __LJMP static int hlua_register_cli(lua_State *L) |
| 6856 | { |
| 6857 | struct cli_kw_list *cli_kws; |
| 6858 | const char *message; |
| 6859 | int ref_io; |
| 6860 | int len; |
| 6861 | struct hlua_function *fcn; |
| 6862 | int index; |
| 6863 | int i; |
| 6864 | |
| 6865 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 6866 | |
| 6867 | /* First argument : an array of maximum 5 keywords. */ |
| 6868 | if (!lua_istable(L, 1)) |
| 6869 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 6870 | |
| 6871 | /* Second argument : string with contextual message. */ |
| 6872 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6873 | |
| 6874 | /* Third and fourth argument : lua function. */ |
| 6875 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 6876 | |
| 6877 | /* Allocate and fill the sample fetch keyword struct. */ |
| 6878 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
| 6879 | if (!cli_kws) |
| 6880 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6881 | fcn = calloc(1, sizeof(*fcn)); |
| 6882 | if (!fcn) |
| 6883 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6884 | |
| 6885 | /* Fill path. */ |
| 6886 | index = 0; |
| 6887 | lua_pushnil(L); |
| 6888 | while(lua_next(L, 1) != 0) { |
| 6889 | if (index >= 5) |
| 6890 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 6891 | if (lua_type(L, -1) != LUA_TSTRING) |
| 6892 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 6893 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
| 6894 | if (!cli_kws->kw[0].str_kw[index]) |
| 6895 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6896 | index++; |
| 6897 | lua_pop(L, 1); |
| 6898 | } |
| 6899 | |
| 6900 | /* Copy help message. */ |
| 6901 | cli_kws->kw[0].usage = strdup(message); |
| 6902 | if (!cli_kws->kw[0].usage) |
| 6903 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6904 | |
| 6905 | /* Fill fcn io handler. */ |
| 6906 | len = strlen("<lua.cli>") + 1; |
| 6907 | for (i = 0; i < index; i++) |
| 6908 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 6909 | fcn->name = calloc(1, len); |
| 6910 | if (!fcn->name) |
| 6911 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6912 | strncat((char *)fcn->name, "<lua.cli", len); |
| 6913 | for (i = 0; i < index; i++) { |
| 6914 | strncat((char *)fcn->name, ".", len); |
| 6915 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 6916 | } |
| 6917 | strncat((char *)fcn->name, ">", len); |
| 6918 | fcn->function_ref = ref_io; |
| 6919 | |
| 6920 | /* Fill last entries. */ |
| 6921 | cli_kws->kw[0].private = fcn; |
| 6922 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 6923 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 6924 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 6925 | |
| 6926 | /* Register this new converter */ |
| 6927 | cli_register_kw(cli_kws); |
| 6928 | |
| 6929 | return 0; |
| 6930 | } |
| 6931 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6932 | static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx, |
| 6933 | struct proxy *defpx, const char *file, int line, |
| 6934 | char **err, unsigned int *timeout) |
| 6935 | { |
| 6936 | const char *error; |
| 6937 | |
| 6938 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
| 6939 | if (error && *error != '\0') { |
| 6940 | memprintf(err, "%s: invalid timeout", args[0]); |
| 6941 | return -1; |
| 6942 | } |
| 6943 | return 0; |
| 6944 | } |
| 6945 | |
| 6946 | static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx, |
| 6947 | struct proxy *defpx, const char *file, int line, |
| 6948 | char **err) |
| 6949 | { |
| 6950 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 6951 | file, line, err, &hlua_timeout_session); |
| 6952 | } |
| 6953 | |
| 6954 | static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx, |
| 6955 | struct proxy *defpx, const char *file, int line, |
| 6956 | char **err) |
| 6957 | { |
| 6958 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 6959 | file, line, err, &hlua_timeout_task); |
| 6960 | } |
| 6961 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6962 | static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx, |
| 6963 | struct proxy *defpx, const char *file, int line, |
| 6964 | char **err) |
| 6965 | { |
| 6966 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 6967 | file, line, err, &hlua_timeout_applet); |
| 6968 | } |
| 6969 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 6970 | static int hlua_forced_yield(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 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 6977 | if (*error != '\0') { |
| 6978 | memprintf(err, "%s: invalid number", args[0]); |
| 6979 | return -1; |
| 6980 | } |
| 6981 | return 0; |
| 6982 | } |
| 6983 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 6984 | static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx, |
| 6985 | struct proxy *defpx, const char *file, int line, |
| 6986 | char **err) |
| 6987 | { |
| 6988 | char *error; |
| 6989 | |
| 6990 | if (*(args[1]) == 0) { |
| 6991 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 6992 | return -1; |
| 6993 | } |
| 6994 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 6995 | if (*error != '\0') { |
| 6996 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 6997 | return -1; |
| 6998 | } |
| 6999 | return 0; |
| 7000 | } |
| 7001 | |
| 7002 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7003 | /* This function is called by the main configuration key "lua-load". It loads and |
| 7004 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 7005 | * the main lua entry point. |
| 7006 | * |
| 7007 | * This funtion runs with the HAProxy keywords API. It returns -1 if an error is |
| 7008 | * occured, otherwise it returns 0. |
| 7009 | * |
| 7010 | * In some error case, LUA set an error message in top of the stack. This function |
| 7011 | * 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] | 7012 | * |
| 7013 | * This function can fail with an abort() due to an Lua critical error. |
| 7014 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7015 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7016 | */ |
| 7017 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
| 7018 | struct proxy *defpx, const char *file, int line, |
| 7019 | char **err) |
| 7020 | { |
| 7021 | int error; |
| 7022 | |
| 7023 | /* Just load and compile the file. */ |
| 7024 | error = luaL_loadfile(gL.T, args[1]); |
| 7025 | if (error) { |
| 7026 | memprintf(err, "error in lua file '%s': %s", args[1], lua_tostring(gL.T, -1)); |
| 7027 | lua_pop(gL.T, 1); |
| 7028 | return -1; |
| 7029 | } |
| 7030 | |
| 7031 | /* If no syntax error where detected, execute the code. */ |
| 7032 | error = lua_pcall(gL.T, 0, LUA_MULTRET, 0); |
| 7033 | switch (error) { |
| 7034 | case LUA_OK: |
| 7035 | break; |
| 7036 | case LUA_ERRRUN: |
| 7037 | memprintf(err, "lua runtime error: %s\n", lua_tostring(gL.T, -1)); |
| 7038 | lua_pop(gL.T, 1); |
| 7039 | return -1; |
| 7040 | case LUA_ERRMEM: |
| 7041 | memprintf(err, "lua out of memory error\n"); |
| 7042 | return -1; |
| 7043 | case LUA_ERRERR: |
| 7044 | memprintf(err, "lua message handler error: %s\n", lua_tostring(gL.T, -1)); |
| 7045 | lua_pop(gL.T, 1); |
| 7046 | return -1; |
| 7047 | case LUA_ERRGCMM: |
| 7048 | memprintf(err, "lua garbage collector error: %s\n", lua_tostring(gL.T, -1)); |
| 7049 | lua_pop(gL.T, 1); |
| 7050 | return -1; |
| 7051 | default: |
| 7052 | memprintf(err, "lua unknonwn error: %s\n", lua_tostring(gL.T, -1)); |
| 7053 | lua_pop(gL.T, 1); |
| 7054 | return -1; |
| 7055 | } |
| 7056 | |
| 7057 | return 0; |
| 7058 | } |
| 7059 | |
| 7060 | /* configuration keywords declaration */ |
| 7061 | static struct cfg_kw_list cfg_kws = {{ },{ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7062 | { CFG_GLOBAL, "lua-load", hlua_load }, |
| 7063 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 7064 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 7065 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 7066 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7067 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7068 | { 0, NULL, NULL }, |
| 7069 | }}; |
| 7070 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7071 | /* This function can fail with an abort() due to an Lua critical error. |
| 7072 | * We are in the initialisation process of HAProxy, this abort() is |
| 7073 | * tolerated. |
| 7074 | */ |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7075 | int hlua_post_init() |
| 7076 | { |
| 7077 | struct hlua_init_function *init; |
| 7078 | const char *msg; |
| 7079 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7080 | const char *error; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7081 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 7082 | /* Call post initialisation function in safe environement. */ |
| 7083 | if (!SET_SAFE_LJMP(gL.T)) { |
| 7084 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 7085 | error = lua_tostring(gL.T, -1); |
| 7086 | else |
| 7087 | error = "critical error"; |
| 7088 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 7089 | exit(1); |
| 7090 | } |
| 7091 | hlua_fcn_post_init(gL.T); |
| 7092 | RESET_SAFE_LJMP(gL.T); |
| 7093 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7094 | list_for_each_entry(init, &hlua_init_functions, l) { |
| 7095 | lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref); |
| 7096 | ret = hlua_ctx_resume(&gL, 0); |
| 7097 | switch (ret) { |
| 7098 | case HLUA_E_OK: |
| 7099 | lua_pop(gL.T, -1); |
| 7100 | return 1; |
| 7101 | case HLUA_E_AGAIN: |
| 7102 | Alert("lua init: yield not allowed.\n"); |
| 7103 | return 0; |
| 7104 | case HLUA_E_ERRMSG: |
| 7105 | msg = lua_tostring(gL.T, -1); |
| 7106 | Alert("lua init: %s.\n", msg); |
| 7107 | return 0; |
| 7108 | case HLUA_E_ERR: |
| 7109 | default: |
| 7110 | Alert("lua init: unknown runtime error.\n"); |
| 7111 | return 0; |
| 7112 | } |
| 7113 | } |
| 7114 | return 1; |
| 7115 | } |
| 7116 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7117 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 7118 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 7119 | * is the previously allocated size or the kind of object in case of a new |
| 7120 | * allocation. <nsize> is the requested new size. |
| 7121 | */ |
| 7122 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 7123 | { |
| 7124 | struct hlua_mem_allocator *zone = ud; |
| 7125 | |
| 7126 | if (nsize == 0) { |
| 7127 | /* it's a free */ |
| 7128 | if (ptr) |
| 7129 | zone->allocated -= osize; |
| 7130 | free(ptr); |
| 7131 | return NULL; |
| 7132 | } |
| 7133 | |
| 7134 | if (!ptr) { |
| 7135 | /* it's a new allocation */ |
| 7136 | if (zone->limit && zone->allocated + nsize > zone->limit) |
| 7137 | return NULL; |
| 7138 | |
| 7139 | ptr = malloc(nsize); |
| 7140 | if (ptr) |
| 7141 | zone->allocated += nsize; |
| 7142 | return ptr; |
| 7143 | } |
| 7144 | |
| 7145 | /* it's a realloc */ |
| 7146 | if (zone->limit && zone->allocated + nsize - osize > zone->limit) |
| 7147 | return NULL; |
| 7148 | |
| 7149 | ptr = realloc(ptr, nsize); |
| 7150 | if (ptr) |
| 7151 | zone->allocated += nsize - osize; |
| 7152 | return ptr; |
| 7153 | } |
| 7154 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7155 | /* Ithis function can fail with an abort() due to an Lua critical error. |
| 7156 | * We are in the initialisation process of HAProxy, this abort() is |
| 7157 | * tolerated. |
| 7158 | */ |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 7159 | void hlua_init(void) |
| 7160 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7161 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7162 | int idx; |
| 7163 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7164 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7165 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7166 | const char *error_msg; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7167 | #ifdef USE_OPENSSL |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7168 | struct srv_kw *kw; |
| 7169 | int tmp_error; |
| 7170 | char *error; |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 7171 | char *args[] = { /* SSL client configuration. */ |
| 7172 | "ssl", |
| 7173 | "verify", |
| 7174 | "none", |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 7175 | NULL |
| 7176 | }; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7177 | #endif |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7178 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7179 | /* Initialise struct hlua and com signals pool */ |
| 7180 | pool2_hlua = create_pool("hlua", sizeof(struct hlua), MEM_F_SHARED); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 7181 | pool2_hlua_com = create_pool("hlua_com", sizeof(struct hlua_com), MEM_F_SHARED); |
| 7182 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7183 | /* Register configuration keywords. */ |
| 7184 | cfg_register_keywords(&cfg_kws); |
| 7185 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 7186 | /* Init main lua stack. */ |
| 7187 | gL.Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 7188 | gL.flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 7189 | LIST_INIT(&gL.com); |
Willy Tarreau | 42ef75f | 2017-04-12 21:40:29 +0200 | [diff] [blame] | 7190 | gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 7191 | hlua_sethlua(&gL); |
| 7192 | gL.Tref = LUA_REFNIL; |
| 7193 | gL.task = NULL; |
| 7194 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7195 | /* From this point, until the end of the initialisation fucntion, |
| 7196 | * the Lua function can fail with an abort. We are in the initialisation |
| 7197 | * process of HAProxy, this abort() is tolerated. |
| 7198 | */ |
| 7199 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 7200 | /* Initialise lua. */ |
| 7201 | luaL_openlibs(gL.T); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7202 | |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 7203 | /* Set safe environment for the initialisation. */ |
| 7204 | if (!SET_SAFE_LJMP(gL.T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7205 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 7206 | error_msg = lua_tostring(gL.T, -1); |
| 7207 | else |
| 7208 | error_msg = "critical error"; |
| 7209 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 7210 | exit(1); |
| 7211 | } |
| 7212 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7213 | /* |
| 7214 | * |
| 7215 | * Create "core" object. |
| 7216 | * |
| 7217 | */ |
| 7218 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 7219 | /* This table entry is the object "core" base. */ |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7220 | lua_newtable(gL.T); |
| 7221 | |
| 7222 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7223 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7224 | hlua_class_const_int(gL.T, log_levels[i], i); |
| 7225 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7226 | /* Register special functions. */ |
| 7227 | hlua_class_function(gL.T, "register_init", hlua_register_init); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 7228 | hlua_class_function(gL.T, "register_task", hlua_register_task); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7229 | hlua_class_function(gL.T, "register_fetches", hlua_register_fetches); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 7230 | hlua_class_function(gL.T, "register_converters", hlua_register_converters); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7231 | hlua_class_function(gL.T, "register_action", hlua_register_action); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7232 | hlua_class_function(gL.T, "register_service", hlua_register_service); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7233 | hlua_class_function(gL.T, "register_cli", hlua_register_cli); |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 7234 | hlua_class_function(gL.T, "yield", hlua_yield); |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7235 | hlua_class_function(gL.T, "set_nice", hlua_set_nice); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 7236 | hlua_class_function(gL.T, "sleep", hlua_sleep); |
| 7237 | hlua_class_function(gL.T, "msleep", hlua_msleep); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 7238 | hlua_class_function(gL.T, "add_acl", hlua_add_acl); |
| 7239 | hlua_class_function(gL.T, "del_acl", hlua_del_acl); |
| 7240 | hlua_class_function(gL.T, "set_map", hlua_set_map); |
| 7241 | hlua_class_function(gL.T, "del_map", hlua_del_map); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7242 | hlua_class_function(gL.T, "tcp", hlua_socket_new); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 7243 | hlua_class_function(gL.T, "log", hlua_log); |
| 7244 | hlua_class_function(gL.T, "Debug", hlua_log_debug); |
| 7245 | hlua_class_function(gL.T, "Info", hlua_log_info); |
| 7246 | hlua_class_function(gL.T, "Warning", hlua_log_warning); |
| 7247 | hlua_class_function(gL.T, "Alert", hlua_log_alert); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 7248 | hlua_class_function(gL.T, "done", hlua_done); |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 7249 | hlua_fcn_reg_core_fcn(gL.T); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7250 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7251 | lua_setglobal(gL.T, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7252 | |
| 7253 | /* |
| 7254 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7255 | * Register class Map |
| 7256 | * |
| 7257 | */ |
| 7258 | |
| 7259 | /* This table entry is the object "Map" base. */ |
| 7260 | lua_newtable(gL.T); |
| 7261 | |
| 7262 | /* register pattern types. */ |
| 7263 | for (i=0; i<PAT_MATCH_NUM; i++) |
| 7264 | hlua_class_const_int(gL.T, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 7265 | for (i=0; i<PAT_MATCH_NUM; i++) { |
| 7266 | snprintf(trash.str, trash.size, "_%s", pat_match_names[i]); |
| 7267 | hlua_class_const_int(gL.T, trash.str, i); |
| 7268 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7269 | |
| 7270 | /* register constructor. */ |
| 7271 | hlua_class_function(gL.T, "new", hlua_map_new); |
| 7272 | |
| 7273 | /* Create and fill the metatable. */ |
| 7274 | lua_newtable(gL.T); |
| 7275 | |
| 7276 | /* Create and fille the __index entry. */ |
| 7277 | lua_pushstring(gL.T, "__index"); |
| 7278 | lua_newtable(gL.T); |
| 7279 | |
| 7280 | /* Register . */ |
| 7281 | hlua_class_function(gL.T, "lookup", hlua_map_lookup); |
| 7282 | hlua_class_function(gL.T, "slookup", hlua_map_slookup); |
| 7283 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7284 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7285 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7286 | /* Register previous table in the registry with reference and named entry. |
| 7287 | * The function hlua_register_metatable() pops the stack, so we |
| 7288 | * previously create a copy of the table. |
| 7289 | */ |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7290 | 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] | 7291 | class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7292 | |
| 7293 | /* Assign the metatable to the mai Map object. */ |
| 7294 | lua_setmetatable(gL.T, -2); |
| 7295 | |
| 7296 | /* Set a name to the table. */ |
| 7297 | lua_setglobal(gL.T, "Map"); |
| 7298 | |
| 7299 | /* |
| 7300 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7301 | * Register class Channel |
| 7302 | * |
| 7303 | */ |
| 7304 | |
| 7305 | /* Create and fill the metatable. */ |
| 7306 | lua_newtable(gL.T); |
| 7307 | |
| 7308 | /* Create and fille the __index entry. */ |
| 7309 | lua_pushstring(gL.T, "__index"); |
| 7310 | lua_newtable(gL.T); |
| 7311 | |
| 7312 | /* Register . */ |
| 7313 | hlua_class_function(gL.T, "get", hlua_channel_get); |
| 7314 | hlua_class_function(gL.T, "dup", hlua_channel_dup); |
| 7315 | hlua_class_function(gL.T, "getline", hlua_channel_getline); |
| 7316 | hlua_class_function(gL.T, "set", hlua_channel_set); |
| 7317 | hlua_class_function(gL.T, "append", hlua_channel_append); |
| 7318 | hlua_class_function(gL.T, "send", hlua_channel_send); |
| 7319 | hlua_class_function(gL.T, "forward", hlua_channel_forward); |
| 7320 | hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len); |
| 7321 | 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] | 7322 | hlua_class_function(gL.T, "is_full", hlua_channel_is_full); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7323 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7324 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7325 | |
| 7326 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7327 | class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7328 | |
| 7329 | /* |
| 7330 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7331 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7332 | * |
| 7333 | */ |
| 7334 | |
| 7335 | /* Create and fill the metatable. */ |
| 7336 | lua_newtable(gL.T); |
| 7337 | |
| 7338 | /* Create and fille the __index entry. */ |
| 7339 | lua_pushstring(gL.T, "__index"); |
| 7340 | lua_newtable(gL.T); |
| 7341 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7342 | /* Browse existing fetches and create the associated |
| 7343 | * object method. |
| 7344 | */ |
| 7345 | sf = NULL; |
| 7346 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
| 7347 | |
| 7348 | /* Dont register the keywork if the arguments check function are |
| 7349 | * not safe during the runtime. |
| 7350 | */ |
| 7351 | if ((sf->val_args != NULL) && |
| 7352 | (sf->val_args != val_payload_lv) && |
| 7353 | (sf->val_args != val_hdr)) |
| 7354 | continue; |
| 7355 | |
| 7356 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 7357 | * by an underscore. |
| 7358 | */ |
| 7359 | strncpy(trash.str, sf->kw, trash.size); |
| 7360 | trash.str[trash.size - 1] = '\0'; |
| 7361 | for (p = trash.str; *p; p++) |
| 7362 | if (*p == '.' || *p == '-' || *p == '+') |
| 7363 | *p = '_'; |
| 7364 | |
| 7365 | /* Register the function. */ |
| 7366 | lua_pushstring(gL.T, trash.str); |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 7367 | lua_pushlightuserdata(gL.T, sf); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7368 | lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7369 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7370 | } |
| 7371 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7372 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7373 | |
| 7374 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7375 | class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7376 | |
| 7377 | /* |
| 7378 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7379 | * Register class Converters |
| 7380 | * |
| 7381 | */ |
| 7382 | |
| 7383 | /* Create and fill the metatable. */ |
| 7384 | lua_newtable(gL.T); |
| 7385 | |
| 7386 | /* Create and fill the __index entry. */ |
| 7387 | lua_pushstring(gL.T, "__index"); |
| 7388 | lua_newtable(gL.T); |
| 7389 | |
| 7390 | /* Browse existing converters and create the associated |
| 7391 | * object method. |
| 7392 | */ |
| 7393 | sc = NULL; |
| 7394 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
| 7395 | /* Dont register the keywork if the arguments check function are |
| 7396 | * not safe during the runtime. |
| 7397 | */ |
| 7398 | if (sc->val_args != NULL) |
| 7399 | continue; |
| 7400 | |
| 7401 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 7402 | * by an underscore. |
| 7403 | */ |
| 7404 | strncpy(trash.str, sc->kw, trash.size); |
| 7405 | trash.str[trash.size - 1] = '\0'; |
| 7406 | for (p = trash.str; *p; p++) |
| 7407 | if (*p == '.' || *p == '-' || *p == '+') |
| 7408 | *p = '_'; |
| 7409 | |
| 7410 | /* Register the function. */ |
| 7411 | lua_pushstring(gL.T, trash.str); |
| 7412 | lua_pushlightuserdata(gL.T, sc); |
| 7413 | lua_pushcclosure(gL.T, hlua_run_sample_conv, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7414 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7415 | } |
| 7416 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7417 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7418 | |
| 7419 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7420 | class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7421 | |
| 7422 | /* |
| 7423 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7424 | * Register class HTTP |
| 7425 | * |
| 7426 | */ |
| 7427 | |
| 7428 | /* Create and fill the metatable. */ |
| 7429 | lua_newtable(gL.T); |
| 7430 | |
| 7431 | /* Create and fille the __index entry. */ |
| 7432 | lua_pushstring(gL.T, "__index"); |
| 7433 | lua_newtable(gL.T); |
| 7434 | |
| 7435 | /* Register Lua functions. */ |
| 7436 | hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers); |
| 7437 | hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr); |
| 7438 | hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr); |
| 7439 | hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val); |
| 7440 | hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr); |
| 7441 | hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr); |
| 7442 | hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth); |
| 7443 | hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path); |
| 7444 | hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query); |
| 7445 | hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri); |
| 7446 | |
| 7447 | hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers); |
| 7448 | hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr); |
| 7449 | hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr); |
| 7450 | hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val); |
| 7451 | hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr); |
| 7452 | 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] | 7453 | 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] | 7454 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7455 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7456 | |
| 7457 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7458 | class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7459 | |
| 7460 | /* |
| 7461 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7462 | * Register class AppletTCP |
| 7463 | * |
| 7464 | */ |
| 7465 | |
| 7466 | /* Create and fill the metatable. */ |
| 7467 | lua_newtable(gL.T); |
| 7468 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7469 | /* Create and fille the __index entry. */ |
| 7470 | lua_pushstring(gL.T, "__index"); |
| 7471 | lua_newtable(gL.T); |
| 7472 | |
| 7473 | /* Register Lua functions. */ |
Thierry FOURNIER / OZON.IO | 3e1d791 | 2016-12-12 12:29:34 +0100 | [diff] [blame] | 7474 | hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline); |
| 7475 | hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv); |
| 7476 | hlua_class_function(gL.T, "send", hlua_applet_tcp_send); |
| 7477 | hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv); |
| 7478 | 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] | 7479 | hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var); |
| 7480 | hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var); |
| 7481 | hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7482 | |
| 7483 | lua_settable(gL.T, -3); |
| 7484 | |
| 7485 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7486 | class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7487 | |
| 7488 | /* |
| 7489 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7490 | * Register class AppletHTTP |
| 7491 | * |
| 7492 | */ |
| 7493 | |
| 7494 | /* Create and fill the metatable. */ |
| 7495 | lua_newtable(gL.T); |
| 7496 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7497 | /* Create and fille the __index entry. */ |
| 7498 | lua_pushstring(gL.T, "__index"); |
| 7499 | lua_newtable(gL.T); |
| 7500 | |
| 7501 | /* Register Lua functions. */ |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 7502 | hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv); |
| 7503 | 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] | 7504 | hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var); |
| 7505 | hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var); |
| 7506 | hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7507 | hlua_class_function(gL.T, "getline", hlua_applet_http_getline); |
| 7508 | hlua_class_function(gL.T, "receive", hlua_applet_http_recv); |
| 7509 | hlua_class_function(gL.T, "send", hlua_applet_http_send); |
| 7510 | hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader); |
| 7511 | hlua_class_function(gL.T, "set_status", hlua_applet_http_status); |
| 7512 | hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response); |
| 7513 | |
| 7514 | lua_settable(gL.T, -3); |
| 7515 | |
| 7516 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7517 | class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7518 | |
| 7519 | /* |
| 7520 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7521 | * Register class TXN |
| 7522 | * |
| 7523 | */ |
| 7524 | |
| 7525 | /* Create and fill the metatable. */ |
| 7526 | lua_newtable(gL.T); |
| 7527 | |
| 7528 | /* Create and fille the __index entry. */ |
| 7529 | lua_pushstring(gL.T, "__index"); |
| 7530 | lua_newtable(gL.T); |
| 7531 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7532 | /* Register Lua functions. */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7533 | hlua_class_function(gL.T, "set_priv", hlua_set_priv); |
| 7534 | hlua_class_function(gL.T, "get_priv", hlua_get_priv); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7535 | hlua_class_function(gL.T, "set_var", hlua_set_var); |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 7536 | hlua_class_function(gL.T, "unset_var", hlua_unset_var); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7537 | hlua_class_function(gL.T, "get_var", hlua_get_var); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 7538 | hlua_class_function(gL.T, "done", hlua_txn_done); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7539 | hlua_class_function(gL.T, "set_loglevel",hlua_txn_set_loglevel); |
| 7540 | hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos); |
| 7541 | hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 7542 | hlua_class_function(gL.T, "deflog", hlua_txn_deflog); |
| 7543 | hlua_class_function(gL.T, "log", hlua_txn_log); |
| 7544 | hlua_class_function(gL.T, "Debug", hlua_txn_log_debug); |
| 7545 | hlua_class_function(gL.T, "Info", hlua_txn_log_info); |
| 7546 | hlua_class_function(gL.T, "Warning", hlua_txn_log_warning); |
| 7547 | hlua_class_function(gL.T, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7548 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7549 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7550 | |
| 7551 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7552 | class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7553 | |
| 7554 | /* |
| 7555 | * |
| 7556 | * Register class Socket |
| 7557 | * |
| 7558 | */ |
| 7559 | |
| 7560 | /* Create and fill the metatable. */ |
| 7561 | lua_newtable(gL.T); |
| 7562 | |
| 7563 | /* Create and fille the __index entry. */ |
| 7564 | lua_pushstring(gL.T, "__index"); |
| 7565 | lua_newtable(gL.T); |
| 7566 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 7567 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7568 | hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 7569 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7570 | hlua_class_function(gL.T, "connect", hlua_socket_connect); |
| 7571 | hlua_class_function(gL.T, "send", hlua_socket_send); |
| 7572 | hlua_class_function(gL.T, "receive", hlua_socket_receive); |
| 7573 | hlua_class_function(gL.T, "close", hlua_socket_close); |
| 7574 | hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername); |
| 7575 | hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname); |
| 7576 | hlua_class_function(gL.T, "setoption", hlua_socket_setoption); |
| 7577 | hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout); |
| 7578 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7579 | 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] | 7580 | |
| 7581 | /* Register the garbage collector entry. */ |
| 7582 | lua_pushstring(gL.T, "__gc"); |
| 7583 | lua_pushcclosure(gL.T, hlua_socket_gc, 0); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7584 | 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] | 7585 | |
| 7586 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7587 | class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7588 | |
| 7589 | /* Proxy and server configuration initialisation. */ |
| 7590 | memset(&socket_proxy, 0, sizeof(socket_proxy)); |
| 7591 | init_new_proxy(&socket_proxy); |
| 7592 | socket_proxy.parent = NULL; |
| 7593 | socket_proxy.last_change = now.tv_sec; |
| 7594 | socket_proxy.id = "LUA-SOCKET"; |
| 7595 | socket_proxy.cap = PR_CAP_FE | PR_CAP_BE; |
| 7596 | socket_proxy.maxconn = 0; |
| 7597 | socket_proxy.accept = NULL; |
| 7598 | socket_proxy.options2 |= PR_O2_INDEPSTR; |
| 7599 | socket_proxy.srv = NULL; |
| 7600 | socket_proxy.conn_retries = 0; |
| 7601 | socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */ |
| 7602 | |
| 7603 | /* Init TCP server: unchanged parameters */ |
| 7604 | memset(&socket_tcp, 0, sizeof(socket_tcp)); |
| 7605 | socket_tcp.next = NULL; |
| 7606 | socket_tcp.proxy = &socket_proxy; |
| 7607 | socket_tcp.obj_type = OBJ_TYPE_SERVER; |
| 7608 | LIST_INIT(&socket_tcp.actconns); |
| 7609 | LIST_INIT(&socket_tcp.pendconns); |
Willy Tarreau | 600802a | 2015-08-04 17:19:06 +0200 | [diff] [blame] | 7610 | LIST_INIT(&socket_tcp.priv_conns); |
Willy Tarreau | 173a1c6 | 2015-08-05 10:31:57 +0200 | [diff] [blame] | 7611 | LIST_INIT(&socket_tcp.idle_conns); |
Willy Tarreau | 7017cb0 | 2015-08-05 16:35:23 +0200 | [diff] [blame] | 7612 | LIST_INIT(&socket_tcp.safe_conns); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7613 | socket_tcp.state = SRV_ST_RUNNING; /* early server setup */ |
| 7614 | socket_tcp.last_change = 0; |
| 7615 | socket_tcp.id = "LUA-TCP-CONN"; |
| 7616 | socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 7617 | socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 7618 | socket_tcp.pp_opts = 0; /* Remove proxy protocol. */ |
| 7619 | |
| 7620 | /* XXX: Copy default parameter from default server, |
| 7621 | * but the default server is not initialized. |
| 7622 | */ |
| 7623 | socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue; |
| 7624 | socket_tcp.minconn = socket_proxy.defsrv.minconn; |
| 7625 | socket_tcp.maxconn = socket_proxy.defsrv.maxconn; |
| 7626 | socket_tcp.slowstart = socket_proxy.defsrv.slowstart; |
| 7627 | socket_tcp.onerror = socket_proxy.defsrv.onerror; |
| 7628 | socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 7629 | socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 7630 | socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 7631 | socket_tcp.uweight = socket_proxy.defsrv.iweight; |
| 7632 | socket_tcp.iweight = socket_proxy.defsrv.iweight; |
| 7633 | |
| 7634 | socket_tcp.check.status = HCHK_STATUS_INI; |
| 7635 | socket_tcp.check.rise = socket_proxy.defsrv.check.rise; |
| 7636 | socket_tcp.check.fall = socket_proxy.defsrv.check.fall; |
| 7637 | socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */ |
| 7638 | socket_tcp.check.server = &socket_tcp; |
| 7639 | |
| 7640 | socket_tcp.agent.status = HCHK_STATUS_INI; |
| 7641 | socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise; |
| 7642 | socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall; |
| 7643 | socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */ |
| 7644 | socket_tcp.agent.server = &socket_tcp; |
| 7645 | |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 7646 | socket_tcp.xprt = xprt_get(XPRT_RAW); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7647 | |
| 7648 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7649 | /* Init TCP server: unchanged parameters */ |
| 7650 | memset(&socket_ssl, 0, sizeof(socket_ssl)); |
| 7651 | socket_ssl.next = NULL; |
| 7652 | socket_ssl.proxy = &socket_proxy; |
| 7653 | socket_ssl.obj_type = OBJ_TYPE_SERVER; |
| 7654 | LIST_INIT(&socket_ssl.actconns); |
| 7655 | LIST_INIT(&socket_ssl.pendconns); |
Willy Tarreau | 600802a | 2015-08-04 17:19:06 +0200 | [diff] [blame] | 7656 | LIST_INIT(&socket_ssl.priv_conns); |
Willy Tarreau | 173a1c6 | 2015-08-05 10:31:57 +0200 | [diff] [blame] | 7657 | LIST_INIT(&socket_ssl.idle_conns); |
Willy Tarreau | 7017cb0 | 2015-08-05 16:35:23 +0200 | [diff] [blame] | 7658 | LIST_INIT(&socket_ssl.safe_conns); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7659 | socket_ssl.state = SRV_ST_RUNNING; /* early server setup */ |
| 7660 | socket_ssl.last_change = 0; |
| 7661 | socket_ssl.id = "LUA-SSL-CONN"; |
| 7662 | socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 7663 | socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 7664 | socket_ssl.pp_opts = 0; /* Remove proxy protocol. */ |
| 7665 | |
| 7666 | /* XXX: Copy default parameter from default server, |
| 7667 | * but the default server is not initialized. |
| 7668 | */ |
| 7669 | socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue; |
| 7670 | socket_ssl.minconn = socket_proxy.defsrv.minconn; |
| 7671 | socket_ssl.maxconn = socket_proxy.defsrv.maxconn; |
| 7672 | socket_ssl.slowstart = socket_proxy.defsrv.slowstart; |
| 7673 | socket_ssl.onerror = socket_proxy.defsrv.onerror; |
| 7674 | socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 7675 | socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 7676 | socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 7677 | socket_ssl.uweight = socket_proxy.defsrv.iweight; |
| 7678 | socket_ssl.iweight = socket_proxy.defsrv.iweight; |
| 7679 | |
| 7680 | socket_ssl.check.status = HCHK_STATUS_INI; |
| 7681 | socket_ssl.check.rise = socket_proxy.defsrv.check.rise; |
| 7682 | socket_ssl.check.fall = socket_proxy.defsrv.check.fall; |
| 7683 | socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */ |
| 7684 | socket_ssl.check.server = &socket_ssl; |
| 7685 | |
| 7686 | socket_ssl.agent.status = HCHK_STATUS_INI; |
| 7687 | socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise; |
| 7688 | socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall; |
| 7689 | socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */ |
| 7690 | socket_ssl.agent.server = &socket_ssl; |
| 7691 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 7692 | socket_ssl.use_ssl = 1; |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 7693 | socket_ssl.xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7694 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 7695 | for (idx = 0; args[idx] != NULL; idx++) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7696 | if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */ |
| 7697 | /* |
| 7698 | * |
| 7699 | * If the keyword is not known, we can search in the registered |
| 7700 | * server keywords. This is usefull to configure special SSL |
| 7701 | * features like client certificates and ssl_verify. |
| 7702 | * |
| 7703 | */ |
| 7704 | tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error); |
| 7705 | if (tmp_error != 0) { |
| 7706 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 7707 | abort(); /* This must be never arrives because the command line |
| 7708 | not editable by the user. */ |
| 7709 | } |
| 7710 | idx += kw->skip; |
| 7711 | } |
| 7712 | } |
| 7713 | |
| 7714 | /* Initialize SSL server. */ |
Willy Tarreau | 17d4538 | 2016-12-22 21:16:08 +0100 | [diff] [blame] | 7715 | if (socket_ssl.xprt->prepare_srv) |
| 7716 | socket_ssl.xprt->prepare_srv(&socket_ssl); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7717 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 7718 | |
| 7719 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 7720 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 7721 | |
| 7722 | __attribute__((constructor)) |
| 7723 | static void __hlua_init(void) |
| 7724 | { |
| 7725 | char *ptr = NULL; |
| 7726 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 7727 | hap_register_build_opts(ptr, 1); |
| 7728 | } |