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> |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 27 | #include <common/xref.h> |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 28 | #include <common/hathreads.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 29 | |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 30 | #include <types/cli.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 31 | #include <types/hlua.h> |
| 32 | #include <types/proxy.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 33 | #include <types/stats.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 34 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 35 | #include <proto/arg.h> |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 36 | #include <proto/applet.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 37 | #include <proto/channel.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 38 | #include <proto/cli.h> |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 39 | #include <proto/connection.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 40 | #include <proto/stats.h> |
Thierry FOURNIER | 9a819e7 | 2015-02-16 20:22:55 +0100 | [diff] [blame] | 41 | #include <proto/hdr_idx.h> |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 42 | #include <proto/hlua.h> |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 43 | #include <proto/hlua_fcn.h> |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 44 | #include <proto/map.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 45 | #include <proto/obj_type.h> |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 46 | #include <proto/pattern.h> |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 47 | #include <proto/payload.h> |
| 48 | #include <proto/proto_http.h> |
| 49 | #include <proto/sample.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 50 | #include <proto/server.h> |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 51 | #include <proto/session.h> |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 52 | #include <proto/stream.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 53 | #include <proto/stream_interface.h> |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 54 | #include <proto/task.h> |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 55 | #include <proto/tcp_rules.h> |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 56 | #include <proto/vars.h> |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 57 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 58 | /* Lua uses longjmp to perform yield or throwing errors. This |
| 59 | * macro is used only for identifying the function that can |
| 60 | * not return because a longjmp is executed. |
| 61 | * __LJMP marks a prototype of hlua file that can use longjmp. |
| 62 | * WILL_LJMP() marks an lua function that will use longjmp. |
| 63 | * MAY_LJMP() marks an lua function that may use longjmp. |
| 64 | */ |
| 65 | #define __LJMP |
| 66 | #define WILL_LJMP(func) func |
| 67 | #define MAY_LJMP(func) func |
| 68 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 69 | /* This couple of function executes securely some Lua calls outside of |
| 70 | * the lua runtime environment. Each Lua call can return a longjmp |
| 71 | * if it encounter a memory error. |
| 72 | * |
| 73 | * Lua documentation extract: |
| 74 | * |
| 75 | * If an error happens outside any protected environment, Lua calls |
| 76 | * a panic function (see lua_atpanic) and then calls abort, thus |
| 77 | * exiting the host application. Your panic function can avoid this |
| 78 | * exit by never returning (e.g., doing a long jump to your own |
| 79 | * recovery point outside Lua). |
| 80 | * |
| 81 | * The panic function runs as if it were a message handler (see |
| 82 | * §2.3); in particular, the error message is at the top of the |
| 83 | * stack. However, there is no guarantee about stack space. To push |
| 84 | * anything on the stack, the panic function must first check the |
| 85 | * available space (see §4.2). |
| 86 | * |
| 87 | * We must check all the Lua entry point. This includes: |
| 88 | * - The include/proto/hlua.h exported functions |
| 89 | * - the task wrapper function |
| 90 | * - The action wrapper function |
| 91 | * - The converters wrapper function |
| 92 | * - The sample-fetch wrapper functions |
| 93 | * |
| 94 | * It is tolerated that the initilisation function returns an abort. |
| 95 | * Before each Lua abort, an error message is writed on stderr. |
| 96 | * |
| 97 | * The macro SET_SAFE_LJMP initialise the longjmp. The Macro |
| 98 | * RESET_SAFE_LJMP reset the longjmp. These function must be macro |
| 99 | * because they must be exists in the program stack when the longjmp |
| 100 | * is called. |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 101 | * |
| 102 | * Note that the Lua processing is not really thread safe. It provides |
| 103 | * heavy system which consists to add our own lock function in the Lua |
| 104 | * code and recompile the library. This system will probably not accepted |
| 105 | * by maintainers of various distribs. |
| 106 | * |
| 107 | * Our main excution point of the Lua is the function lua_resume(). A |
| 108 | * quick looking on the Lua sources displays a lua_lock() a the start |
| 109 | * of function and a lua_unlock() at the end of the function. So I |
| 110 | * conclude that the Lua thread safe mode just perform a mutex around |
| 111 | * all execution. So I prefer to do this in the HAProxy code, it will be |
| 112 | * easier for distro maintainers. |
| 113 | * |
| 114 | * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP |
| 115 | * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful |
| 116 | * to set mutex around these functions. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 117 | */ |
Christopher Faulet | 9dcf9b6 | 2017-11-13 10:34:01 +0100 | [diff] [blame] | 118 | __decl_hathreads(HA_SPINLOCK_T hlua_global_lock); |
Thierry FOURNIER | ffbad79 | 2017-07-12 11:39:04 +0200 | [diff] [blame] | 119 | THREAD_LOCAL jmp_buf safe_ljmp_env; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 120 | static int hlua_panic_safe(lua_State *L) { return 0; } |
| 121 | static int hlua_panic_ljmp(lua_State *L) { longjmp(safe_ljmp_env, 1); } |
| 122 | |
| 123 | #define SET_SAFE_LJMP(__L) \ |
| 124 | ({ \ |
| 125 | int ret; \ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 126 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 127 | if (setjmp(safe_ljmp_env) != 0) { \ |
| 128 | lua_atpanic(__L, hlua_panic_safe); \ |
| 129 | ret = 0; \ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 130 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 131 | } else { \ |
| 132 | lua_atpanic(__L, hlua_panic_ljmp); \ |
| 133 | ret = 1; \ |
| 134 | } \ |
| 135 | ret; \ |
| 136 | }) |
| 137 | |
| 138 | /* If we are the last function catching Lua errors, we |
| 139 | * must reset the panic function. |
| 140 | */ |
| 141 | #define RESET_SAFE_LJMP(__L) \ |
| 142 | do { \ |
| 143 | lua_atpanic(__L, hlua_panic_safe); \ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 144 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 145 | } while(0) |
| 146 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 147 | /* Applet status flags */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 148 | #define APPLET_DONE 0x01 /* applet processing is done. */ |
| 149 | #define APPLET_100C 0x02 /* 100 continue expected. */ |
| 150 | #define APPLET_HDR_SENT 0x04 /* Response header sent. */ |
| 151 | #define APPLET_CHUNKED 0x08 /* Use transfer encoding chunked. */ |
| 152 | #define APPLET_LAST_CHK 0x10 /* Last chunk sent. */ |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 153 | #define APPLET_HTTP11 0x20 /* Last chunk sent. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 154 | |
| 155 | #define HTTP_100C "HTTP/1.1 100 Continue\r\n\r\n" |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 156 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 157 | /* The main Lua execution context. */ |
| 158 | struct hlua gL; |
| 159 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 160 | /* This is the memory pool containing struct lua for applets |
| 161 | * (including cli). |
| 162 | */ |
| 163 | struct pool_head *pool2_hlua; |
| 164 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 165 | /* Used for Socket connection. */ |
| 166 | static struct proxy socket_proxy; |
| 167 | static struct server socket_tcp; |
| 168 | #ifdef USE_OPENSSL |
| 169 | static struct server socket_ssl; |
| 170 | #endif |
| 171 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 172 | /* List head of the function called at the initialisation time. */ |
| 173 | struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions); |
| 174 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 175 | /* The following variables contains the reference of the different |
| 176 | * Lua classes. These references are useful for identify metadata |
| 177 | * associated with an object. |
| 178 | */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 179 | static int class_txn_ref; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 180 | static int class_socket_ref; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 181 | static int class_channel_ref; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 182 | static int class_fetches_ref; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 183 | static int class_converters_ref; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 184 | static int class_http_ref; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 185 | static int class_map_ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 186 | static int class_applet_tcp_ref; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 187 | static int class_applet_http_ref; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 188 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 189 | /* Global Lua execution timeout. By default Lua, execution linked |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 190 | * with stream (actions, sample-fetches and converters) have a |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 191 | * short timeout. Lua linked with tasks doesn't have a timeout |
| 192 | * because a task may remain alive during all the haproxy execution. |
| 193 | */ |
| 194 | static unsigned int hlua_timeout_session = 4000; /* session timeout. */ |
| 195 | static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 196 | static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 197 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 198 | /* Interrupts the Lua processing each "hlua_nb_instruction" instructions. |
| 199 | * it is used for preventing infinite loops. |
| 200 | * |
| 201 | * I test the scheer with an infinite loop containing one incrementation |
| 202 | * and one test. I run this loop between 10 seconds, I raise a ceil of |
| 203 | * 710M loops from one interrupt each 9000 instructions, so I fix the value |
| 204 | * to one interrupt each 10 000 instructions. |
| 205 | * |
| 206 | * configured | Number of |
| 207 | * instructions | loops executed |
| 208 | * between two | in milions |
| 209 | * forced yields | |
| 210 | * ---------------+--------------- |
| 211 | * 10 | 160 |
| 212 | * 500 | 670 |
| 213 | * 1000 | 680 |
| 214 | * 5000 | 700 |
| 215 | * 7000 | 700 |
| 216 | * 8000 | 700 |
| 217 | * 9000 | 710 <- ceil |
| 218 | * 10000 | 710 |
| 219 | * 100000 | 710 |
| 220 | * 1000000 | 710 |
| 221 | * |
| 222 | */ |
| 223 | static unsigned int hlua_nb_instruction = 10000; |
| 224 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 225 | /* Descriptor for the memory allocation state. If limit is not null, it will |
| 226 | * be enforced on any memory allocation. |
| 227 | */ |
| 228 | struct hlua_mem_allocator { |
| 229 | size_t allocated; |
| 230 | size_t limit; |
| 231 | }; |
| 232 | |
| 233 | static struct hlua_mem_allocator hlua_global_allocator; |
| 234 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 235 | static const char error_500[] = |
Jarno Huuskonen | 16ad94a | 2017-01-09 14:17:10 +0200 | [diff] [blame] | 236 | "HTTP/1.0 500 Internal Server Error\r\n" |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 237 | "Cache-Control: no-cache\r\n" |
| 238 | "Connection: close\r\n" |
| 239 | "Content-Type: text/html\r\n" |
| 240 | "\r\n" |
Jarno Huuskonen | 16ad94a | 2017-01-09 14:17:10 +0200 | [diff] [blame] | 241 | "<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] | 242 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 243 | /* These functions converts types between HAProxy internal args or |
| 244 | * sample and LUA types. Another function permits to check if the |
| 245 | * LUA stack contains arguments according with an required ARG_T |
| 246 | * format. |
| 247 | */ |
| 248 | static int hlua_arg2lua(lua_State *L, const struct arg *arg); |
| 249 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 250 | __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] | 251 | uint64_t mask, struct proxy *p); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 252 | static int hlua_smp2lua(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 253 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 254 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp); |
| 255 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 256 | __LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg); |
| 257 | |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 258 | #define SEND_ERR(__be, __fmt, __args...) \ |
| 259 | do { \ |
| 260 | send_log(__be, LOG_ERR, __fmt, ## __args); \ |
| 261 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \ |
| 262 | Alert(__fmt, ## __args); \ |
| 263 | } while (0) |
| 264 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 265 | /* Used to check an Lua function type in the stack. It creates and |
| 266 | * returns a reference of the function. This function throws an |
| 267 | * error if the rgument is not a "function". |
| 268 | */ |
| 269 | __LJMP unsigned int hlua_checkfunction(lua_State *L, int argno) |
| 270 | { |
| 271 | if (!lua_isfunction(L, argno)) { |
| 272 | const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, -1)); |
| 273 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 274 | } |
| 275 | lua_pushvalue(L, argno); |
| 276 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 277 | } |
| 278 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 279 | /* Return the string that is of the top of the stack. */ |
| 280 | const char *hlua_get_top_error_string(lua_State *L) |
| 281 | { |
| 282 | if (lua_gettop(L) < 1) |
| 283 | return "unknown error"; |
| 284 | if (lua_type(L, -1) != LUA_TSTRING) |
| 285 | return "unknown error"; |
| 286 | return lua_tostring(L, -1); |
| 287 | } |
| 288 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 289 | /* This function check the number of arguments available in the |
| 290 | * stack. If the number of arguments available is not the same |
| 291 | * then <nb> an error is throwed. |
| 292 | */ |
| 293 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 294 | { |
| 295 | if (lua_gettop(L) == nb) |
| 296 | return; |
| 297 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 298 | } |
| 299 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 300 | /* This fucntion push an error string prefixed by the file name |
| 301 | * and the line number where the error is encountered. |
| 302 | */ |
| 303 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 304 | { |
| 305 | va_list argp; |
| 306 | va_start(argp, fmt); |
| 307 | luaL_where(L, 1); |
| 308 | lua_pushvfstring(L, fmt, argp); |
| 309 | va_end(argp); |
| 310 | lua_concat(L, 2); |
| 311 | return 1; |
| 312 | } |
| 313 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 314 | /* This functions is used with sample fetch and converters. It |
| 315 | * converts the HAProxy configuration argument in a lua stack |
| 316 | * values. |
| 317 | * |
| 318 | * It takes an array of "arg", and each entry of the array is |
| 319 | * converted and pushed in the LUA stack. |
| 320 | */ |
| 321 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 322 | { |
| 323 | switch (arg->type) { |
| 324 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 325 | case ARGT_TIME: |
| 326 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 327 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 328 | break; |
| 329 | |
| 330 | case ARGT_STR: |
| 331 | lua_pushlstring(L, arg->data.str.str, arg->data.str.len); |
| 332 | break; |
| 333 | |
| 334 | case ARGT_IPV4: |
| 335 | case ARGT_IPV6: |
| 336 | case ARGT_MSK4: |
| 337 | case ARGT_MSK6: |
| 338 | case ARGT_FE: |
| 339 | case ARGT_BE: |
| 340 | case ARGT_TAB: |
| 341 | case ARGT_SRV: |
| 342 | case ARGT_USR: |
| 343 | case ARGT_MAP: |
| 344 | default: |
| 345 | lua_pushnil(L); |
| 346 | break; |
| 347 | } |
| 348 | return 1; |
| 349 | } |
| 350 | |
| 351 | /* This function take one entrie in an LUA stack at the index "ud", |
| 352 | * and try to convert it in an HAProxy argument entry. This is useful |
| 353 | * with sample fetch wrappers. The input arguments are gived to the |
| 354 | * lua wrapper and converted as arg list by thi function. |
| 355 | */ |
| 356 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 357 | { |
| 358 | switch (lua_type(L, ud)) { |
| 359 | |
| 360 | case LUA_TNUMBER: |
| 361 | case LUA_TBOOLEAN: |
| 362 | arg->type = ARGT_SINT; |
| 363 | arg->data.sint = lua_tointeger(L, ud); |
| 364 | break; |
| 365 | |
| 366 | case LUA_TSTRING: |
| 367 | arg->type = ARGT_STR; |
| 368 | arg->data.str.str = (char *)lua_tolstring(L, ud, (size_t *)&arg->data.str.len); |
| 369 | break; |
| 370 | |
| 371 | case LUA_TUSERDATA: |
| 372 | case LUA_TNIL: |
| 373 | case LUA_TTABLE: |
| 374 | case LUA_TFUNCTION: |
| 375 | case LUA_TTHREAD: |
| 376 | case LUA_TLIGHTUSERDATA: |
| 377 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 378 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 379 | break; |
| 380 | } |
| 381 | return 1; |
| 382 | } |
| 383 | |
| 384 | /* the following functions are used to convert a struct sample |
| 385 | * in Lua type. This useful to convert the return of the |
| 386 | * fetchs or converters. |
| 387 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 388 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 389 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 390 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 391 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 392 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 393 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 394 | break; |
| 395 | |
| 396 | case SMP_T_BIN: |
| 397 | case SMP_T_STR: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 398 | 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] | 399 | break; |
| 400 | |
| 401 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 402 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 403 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 404 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 405 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 406 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 407 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 408 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 409 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 410 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 411 | case HTTP_METH_OTHER: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 412 | 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] | 413 | break; |
| 414 | default: |
| 415 | lua_pushnil(L); |
| 416 | break; |
| 417 | } |
| 418 | break; |
| 419 | |
| 420 | case SMP_T_IPV4: |
| 421 | case SMP_T_IPV6: |
| 422 | 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] | 423 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 424 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 425 | 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] | 426 | else |
| 427 | lua_pushnil(L); |
| 428 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 429 | default: |
| 430 | lua_pushnil(L); |
| 431 | break; |
| 432 | } |
| 433 | return 1; |
| 434 | } |
| 435 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 436 | /* the following functions are used to convert a struct sample |
| 437 | * in Lua strings. This is useful to convert the return of the |
| 438 | * fetchs or converters. |
| 439 | */ |
| 440 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 441 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 442 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 443 | |
| 444 | case SMP_T_BIN: |
| 445 | case SMP_T_STR: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 446 | 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] | 447 | break; |
| 448 | |
| 449 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 450 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 451 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 452 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 453 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 454 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 455 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 456 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 457 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 458 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 459 | case HTTP_METH_OTHER: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 460 | 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] | 461 | break; |
| 462 | default: |
| 463 | lua_pushstring(L, ""); |
| 464 | break; |
| 465 | } |
| 466 | break; |
| 467 | |
| 468 | case SMP_T_SINT: |
| 469 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 470 | case SMP_T_IPV4: |
| 471 | case SMP_T_IPV6: |
| 472 | 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] | 473 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 474 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 475 | lua_pushlstring(L, smp->data.u.str.str, smp->data.u.str.len); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 476 | else |
| 477 | lua_pushstring(L, ""); |
| 478 | break; |
| 479 | default: |
| 480 | lua_pushstring(L, ""); |
| 481 | break; |
| 482 | } |
| 483 | return 1; |
| 484 | } |
| 485 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 486 | /* the following functions are used to convert an Lua type in a |
| 487 | * struct sample. This is useful to provide data from a converter |
| 488 | * to the LUA code. |
| 489 | */ |
| 490 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 491 | { |
| 492 | switch (lua_type(L, ud)) { |
| 493 | |
| 494 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 495 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 496 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 497 | break; |
| 498 | |
| 499 | |
| 500 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 501 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 502 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 503 | break; |
| 504 | |
| 505 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 506 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 507 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 508 | 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] | 509 | break; |
| 510 | |
| 511 | case LUA_TUSERDATA: |
| 512 | case LUA_TNIL: |
| 513 | case LUA_TTABLE: |
| 514 | case LUA_TFUNCTION: |
| 515 | case LUA_TTHREAD: |
| 516 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 517 | case LUA_TNONE: |
| 518 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 519 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 520 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 521 | break; |
| 522 | } |
| 523 | return 1; |
| 524 | } |
| 525 | |
| 526 | /* This function check the "argp" builded by another conversion function |
| 527 | * is in accord with the expected argp defined by the "mask". The fucntion |
| 528 | * 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] | 529 | * |
| 530 | * This function assumes thant the argp argument contains ARGM_NBARGS + 1 |
| 531 | * entries. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 532 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 533 | __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] | 534 | uint64_t mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 535 | { |
| 536 | int min_arg; |
| 537 | int idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 538 | struct proxy *px; |
| 539 | char *sname, *pname; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 540 | |
| 541 | idx = 0; |
| 542 | min_arg = ARGM(mask); |
| 543 | mask >>= ARGM_BITS; |
| 544 | |
| 545 | while (1) { |
| 546 | |
| 547 | /* Check oversize. */ |
| 548 | if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) { |
Cyril Bonté | 577a36a | 2015-03-02 00:08:38 +0100 | [diff] [blame] | 549 | WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | /* Check for mandatory arguments. */ |
| 553 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 554 | if (idx < min_arg) { |
| 555 | |
| 556 | /* If miss other argument than the first one, we return an error. */ |
| 557 | if (idx > 0) |
| 558 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 559 | |
| 560 | /* If first argument have a certain type, some default values |
| 561 | * may be used. See the function smp_resolve_args(). |
| 562 | */ |
| 563 | switch (mask & ARGT_MASK) { |
| 564 | |
| 565 | case ARGT_FE: |
| 566 | if (!(p->cap & PR_CAP_FE)) |
| 567 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 568 | argp[idx].data.prx = p; |
| 569 | argp[idx].type = ARGT_FE; |
| 570 | argp[idx+1].type = ARGT_STOP; |
| 571 | break; |
| 572 | |
| 573 | case ARGT_BE: |
| 574 | if (!(p->cap & PR_CAP_BE)) |
| 575 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 576 | argp[idx].data.prx = p; |
| 577 | argp[idx].type = ARGT_BE; |
| 578 | argp[idx+1].type = ARGT_STOP; |
| 579 | break; |
| 580 | |
| 581 | case ARGT_TAB: |
| 582 | argp[idx].data.prx = p; |
| 583 | argp[idx].type = ARGT_TAB; |
| 584 | argp[idx+1].type = ARGT_STOP; |
| 585 | break; |
| 586 | |
| 587 | default: |
| 588 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 589 | break; |
| 590 | } |
| 591 | } |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 592 | return 0; |
| 593 | } |
| 594 | |
| 595 | /* Check for exceed the number of requiered argument. */ |
| 596 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 597 | argp[idx].type != ARGT_STOP) { |
| 598 | WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected")); |
| 599 | } |
| 600 | |
| 601 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 602 | argp[idx].type == ARGT_STOP) { |
| 603 | return 0; |
| 604 | } |
| 605 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 606 | /* Convert some argument types. */ |
| 607 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 608 | case ARGT_SINT: |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 609 | if (argp[idx].type != ARGT_SINT) |
| 610 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
| 611 | argp[idx].type = ARGT_SINT; |
| 612 | break; |
| 613 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 614 | case ARGT_TIME: |
| 615 | if (argp[idx].type != ARGT_SINT) |
| 616 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 617 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 618 | break; |
| 619 | |
| 620 | case ARGT_SIZE: |
| 621 | if (argp[idx].type != ARGT_SINT) |
| 622 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 623 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 624 | break; |
| 625 | |
| 626 | case ARGT_FE: |
| 627 | if (argp[idx].type != ARGT_STR) |
| 628 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
| 629 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 630 | trash.str[argp[idx].data.str.len] = 0; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 631 | argp[idx].data.prx = proxy_fe_by_name(trash.str); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 632 | if (!argp[idx].data.prx) |
| 633 | WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist")); |
| 634 | argp[idx].type = ARGT_FE; |
| 635 | break; |
| 636 | |
| 637 | case ARGT_BE: |
| 638 | if (argp[idx].type != ARGT_STR) |
| 639 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
| 640 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 641 | trash.str[argp[idx].data.str.len] = 0; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 642 | argp[idx].data.prx = proxy_be_by_name(trash.str); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 643 | if (!argp[idx].data.prx) |
| 644 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 645 | argp[idx].type = ARGT_BE; |
| 646 | break; |
| 647 | |
| 648 | case ARGT_TAB: |
| 649 | if (argp[idx].type != ARGT_STR) |
| 650 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
| 651 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 652 | trash.str[argp[idx].data.str.len] = 0; |
Willy Tarreau | e2dc1fa | 2015-05-26 12:08:07 +0200 | [diff] [blame] | 653 | argp[idx].data.prx = proxy_tbl_by_name(trash.str); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 654 | if (!argp[idx].data.prx) |
| 655 | WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist")); |
| 656 | argp[idx].type = ARGT_TAB; |
| 657 | break; |
| 658 | |
| 659 | case ARGT_SRV: |
| 660 | if (argp[idx].type != ARGT_STR) |
| 661 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
| 662 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 663 | trash.str[argp[idx].data.str.len] = 0; |
| 664 | sname = strrchr(trash.str, '/'); |
| 665 | if (sname) { |
| 666 | *sname++ = '\0'; |
| 667 | pname = trash.str; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 668 | px = proxy_be_by_name(pname); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 669 | if (!px) |
| 670 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 671 | } |
| 672 | else { |
| 673 | sname = trash.str; |
| 674 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 675 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 676 | argp[idx].data.srv = findserver(px, sname); |
| 677 | if (!argp[idx].data.srv) |
| 678 | WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist")); |
| 679 | argp[idx].type = ARGT_SRV; |
| 680 | break; |
| 681 | |
| 682 | case ARGT_IPV4: |
| 683 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 684 | trash.str[argp[idx].data.str.len] = 0; |
| 685 | if (inet_pton(AF_INET, trash.str, &argp[idx].data.ipv4)) |
| 686 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address")); |
| 687 | argp[idx].type = ARGT_IPV4; |
| 688 | break; |
| 689 | |
| 690 | case ARGT_MSK4: |
| 691 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 692 | trash.str[argp[idx].data.str.len] = 0; |
| 693 | if (!str2mask(trash.str, &argp[idx].data.ipv4)) |
| 694 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask")); |
| 695 | argp[idx].type = ARGT_MSK4; |
| 696 | break; |
| 697 | |
| 698 | case ARGT_IPV6: |
| 699 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 700 | trash.str[argp[idx].data.str.len] = 0; |
| 701 | if (inet_pton(AF_INET6, trash.str, &argp[idx].data.ipv6)) |
| 702 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address")); |
| 703 | argp[idx].type = ARGT_IPV6; |
| 704 | break; |
| 705 | |
| 706 | case ARGT_MSK6: |
| 707 | case ARGT_MAP: |
| 708 | case ARGT_REG: |
| 709 | case ARGT_USR: |
| 710 | WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 711 | break; |
| 712 | } |
| 713 | |
| 714 | /* Check for type of argument. */ |
| 715 | if ((mask & ARGT_MASK) != argp[idx].type) { |
| 716 | const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 717 | arg_type_names[(mask & ARGT_MASK)], |
| 718 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 719 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 720 | } |
| 721 | |
| 722 | /* Next argument. */ |
| 723 | mask >>= ARGT_BITS; |
| 724 | idx++; |
| 725 | } |
| 726 | } |
| 727 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 728 | /* |
| 729 | * The following functions are used to make correspondance between the the |
| 730 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 731 | * |
| 732 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 733 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 734 | */ |
| 735 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 736 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 737 | struct hlua **hlua = lua_getextraspace(L); |
| 738 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 739 | } |
| 740 | static inline void hlua_sethlua(struct hlua *hlua) |
| 741 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 742 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 743 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 744 | } |
| 745 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 746 | /* This function is used to send logs. It try to send on screen (stderr) |
| 747 | * and on the default syslog server. |
| 748 | */ |
| 749 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 750 | { |
| 751 | struct tm tm; |
| 752 | char *p; |
| 753 | |
| 754 | /* Cleanup the log message. */ |
| 755 | p = trash.str; |
| 756 | for (; *msg != '\0'; msg++, p++) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 757 | if (p >= trash.str + trash.size - 1) { |
| 758 | /* Break the message if exceed the buffer size. */ |
| 759 | *(p-4) = ' '; |
| 760 | *(p-3) = '.'; |
| 761 | *(p-2) = '.'; |
| 762 | *(p-1) = '.'; |
| 763 | break; |
| 764 | } |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 765 | if (isprint(*msg)) |
| 766 | *p = *msg; |
| 767 | else |
| 768 | *p = '.'; |
| 769 | } |
| 770 | *p = '\0'; |
| 771 | |
Thierry FOURNIER | 5554e29 | 2015-09-09 11:21:37 +0200 | [diff] [blame] | 772 | send_log(px, level, "%s\n", trash.str); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 773 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 774 | get_localtime(date.tv_sec, &tm); |
| 775 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 776 | log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, |
| 777 | (int)getpid(), trash.str); |
| 778 | fflush(stderr); |
| 779 | } |
| 780 | } |
| 781 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 782 | /* This function just ensure that the yield will be always |
| 783 | * returned with a timeout and permit to set some flags |
| 784 | */ |
| 785 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 786 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 787 | { |
| 788 | struct hlua *hlua = hlua_gethlua(L); |
| 789 | |
| 790 | /* Set the wake timeout. If timeout is required, we set |
| 791 | * the expiration time. |
| 792 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 793 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 794 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 795 | hlua->flags |= flags; |
| 796 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 797 | /* Process the yield. */ |
| 798 | WILL_LJMP(lua_yieldk(L, nresults, ctx, k)); |
| 799 | } |
| 800 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 801 | /* This function initialises the Lua environment stored in the stream. |
| 802 | * 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] | 803 | * 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] | 804 | * |
| 805 | * This function is particular. it initialises a new Lua thread. If the |
| 806 | * initialisation fails (example: out of memory error), the lua function |
| 807 | * throws an error (longjmp). |
| 808 | * |
| 809 | * This function manipulates two Lua stack: the main and the thread. Only |
| 810 | * the main stack can fail. The thread is not manipulated. This function |
| 811 | * MUST NOT manipulate the created thread stack state, because is not |
| 812 | * proctected agains error throwed by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 813 | */ |
| 814 | int hlua_ctx_init(struct hlua *lua, struct task *task) |
| 815 | { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 816 | if (!SET_SAFE_LJMP(gL.T)) { |
| 817 | lua->Tref = LUA_REFNIL; |
| 818 | return 0; |
| 819 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 820 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 821 | lua->flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 822 | LIST_INIT(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 823 | lua->T = lua_newthread(gL.T); |
| 824 | if (!lua->T) { |
| 825 | lua->Tref = LUA_REFNIL; |
Thierry FOURNIER | 0a97620 | 2017-07-12 11:18:00 +0200 | [diff] [blame] | 826 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 827 | return 0; |
| 828 | } |
| 829 | hlua_sethlua(lua); |
| 830 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 831 | lua->task = task; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 832 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 833 | return 1; |
| 834 | } |
| 835 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 836 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 837 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 838 | * is not freed. |
| 839 | */ |
| 840 | void hlua_ctx_destroy(struct hlua *lua) |
| 841 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 842 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 843 | return; |
| 844 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 845 | if (!lua->T) |
| 846 | goto end; |
| 847 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 848 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 849 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 850 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 851 | if (!SET_SAFE_LJMP(lua->T)) |
| 852 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 853 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 854 | RESET_SAFE_LJMP(lua->T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 855 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 856 | if (!SET_SAFE_LJMP(gL.T)) |
| 857 | return; |
| 858 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 859 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 860 | /* Forces a garbage collecting process. If the Lua program is finished |
| 861 | * without error, we run the GC on the thread pointer. Its freed all |
| 862 | * the unused memory. |
| 863 | * If the thread is finnish with an error or is currently yielded, |
| 864 | * it seems that the GC applied on the thread doesn't clean anything, |
| 865 | * so e run the GC on the main thread. |
| 866 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 867 | * the garbage collection. |
| 868 | */ |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 869 | if (lua->flags & HLUA_MUST_GC) { |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 870 | if (!SET_SAFE_LJMP(gL.T)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 871 | return; |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 872 | lua_gc(gL.T, LUA_GCCOLLECT, 0); |
| 873 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 874 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 875 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 876 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 877 | |
| 878 | end: |
| 879 | pool_free2(pool2_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | /* This function is used to restore the Lua context when a coroutine |
| 883 | * fails. This function copy the common memory between old coroutine |
| 884 | * and the new coroutine. The old coroutine is destroyed, and its |
| 885 | * replaced by the new coroutine. |
| 886 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 887 | * as string error message and it is copied in the new stack. |
| 888 | */ |
| 889 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 890 | { |
| 891 | lua_State *T; |
| 892 | int new_ref; |
| 893 | |
| 894 | /* Renew the main LUA stack doesn't have sense. */ |
| 895 | if (lua == &gL) |
| 896 | return 0; |
| 897 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 898 | /* New Lua coroutine. */ |
| 899 | T = lua_newthread(gL.T); |
| 900 | if (!T) |
| 901 | return 0; |
| 902 | |
| 903 | /* Copy last error message. */ |
| 904 | if (keep_msg) |
| 905 | lua_xmove(lua->T, T, 1); |
| 906 | |
| 907 | /* Copy data between the coroutines. */ |
| 908 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 909 | lua_xmove(lua->T, T, 1); |
| 910 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */ |
| 911 | |
| 912 | /* Destroy old data. */ |
| 913 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 914 | |
| 915 | /* The thread is garbage collected by Lua. */ |
| 916 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 917 | |
| 918 | /* Fill the struct with the new coroutine values. */ |
| 919 | lua->Mref = new_ref; |
| 920 | lua->T = T; |
| 921 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 922 | |
| 923 | /* Set context. */ |
| 924 | hlua_sethlua(lua); |
| 925 | |
| 926 | return 1; |
| 927 | } |
| 928 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 929 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 930 | { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 931 | struct hlua *hlua = hlua_gethlua(L); |
| 932 | |
| 933 | /* Lua cannot yield when its returning from a function, |
| 934 | * so, we can fix the interrupt hook to 1 instruction, |
| 935 | * expecting that the function is finnished. |
| 936 | */ |
| 937 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 938 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 939 | return; |
| 940 | } |
| 941 | |
| 942 | /* restore the interrupt condition. */ |
| 943 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 944 | |
| 945 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 946 | * If the state is not yieldable, trying yield causes an error. |
| 947 | */ |
| 948 | if (lua_isyieldable(L)) |
| 949 | WILL_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
| 950 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 951 | /* If we cannot yield, update the clock and check the timeout. */ |
| 952 | tv_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 953 | hlua->run_time += now_ms - hlua->start_time; |
| 954 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 955 | lua_pushfstring(L, "execution timeout"); |
| 956 | WILL_LJMP(lua_error(L)); |
| 957 | } |
| 958 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 959 | /* Update the start time. */ |
| 960 | hlua->start_time = now_ms; |
| 961 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 962 | /* Try to interrupt the process at the end of the current |
| 963 | * unyieldable function. |
| 964 | */ |
| 965 | 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] | 966 | } |
| 967 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 968 | /* This function start or resumes the Lua stack execution. If the flag |
| 969 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 970 | * The function return an error. |
| 971 | * |
| 972 | * The function can returns 4 values: |
| 973 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 974 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 975 | * task wakeup. |
| 976 | * - HLUA_E_ERRMSG : An error has occured, an error message is set in |
| 977 | * the top of the stack. |
| 978 | * - HLUA_E_ERR : An error has occured without error message. |
| 979 | * |
| 980 | * If an error occured, the stack is renewed and it is ready to run new |
| 981 | * LUA code. |
| 982 | */ |
| 983 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 984 | { |
| 985 | int ret; |
| 986 | const char *msg; |
| 987 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 988 | /* Initialise run time counter. */ |
| 989 | if (!HLUA_IS_RUNNING(lua)) |
| 990 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 991 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 992 | /* Lock the whole Lua execution. This lock must be before the |
| 993 | * label "resume_execution". |
| 994 | */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 995 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 996 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 997 | resume_execution: |
| 998 | |
| 999 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1000 | * instructions. it is used for preventing infinite loops. |
| 1001 | */ |
| 1002 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1003 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1004 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1005 | HLUA_SET_RUN(lua); |
| 1006 | HLUA_CLR_CTRLYIELD(lua); |
| 1007 | HLUA_CLR_WAKERESWR(lua); |
| 1008 | HLUA_CLR_WAKEREQWR(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1009 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1010 | /* Update the start time. */ |
| 1011 | lua->start_time = now_ms; |
| 1012 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1013 | /* Call the function. */ |
| 1014 | ret = lua_resume(lua->T, gL.T, lua->nargs); |
| 1015 | switch (ret) { |
| 1016 | |
| 1017 | case LUA_OK: |
| 1018 | ret = HLUA_E_OK; |
| 1019 | break; |
| 1020 | |
| 1021 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1022 | /* Check if the execution timeout is expired. It it is the case, we |
| 1023 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1024 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1025 | tv_update_date(0, 1); |
| 1026 | lua->run_time += now_ms - lua->start_time; |
| 1027 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1028 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1029 | if (!lua_checkstack(lua->T, 1)) { |
| 1030 | ret = HLUA_E_ERR; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1031 | break; |
| 1032 | } |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1033 | lua_pushfstring(lua->T, "execution timeout"); |
| 1034 | ret = HLUA_E_ERRMSG; |
| 1035 | break; |
| 1036 | } |
| 1037 | /* Process the forced yield. if the general yield is not allowed or |
| 1038 | * if no task were associated this the current Lua execution |
| 1039 | * coroutine, we resume the execution. Else we want to return in the |
| 1040 | * scheduler and we want to be waked up again, to continue the |
| 1041 | * current Lua execution. So we schedule our own task. |
| 1042 | */ |
| 1043 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1044 | if (!yield_allowed || !lua->task) |
| 1045 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1046 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1047 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1048 | if (!yield_allowed) { |
| 1049 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1050 | if (!lua_checkstack(lua->T, 1)) { |
| 1051 | ret = HLUA_E_ERR; |
| 1052 | break; |
| 1053 | } |
| 1054 | lua_pushfstring(lua->T, "yield not allowed"); |
| 1055 | ret = HLUA_E_ERRMSG; |
| 1056 | break; |
| 1057 | } |
| 1058 | ret = HLUA_E_AGAIN; |
| 1059 | break; |
| 1060 | |
| 1061 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1062 | |
| 1063 | /* Special exit case. The traditionnal exit is returned as an error |
| 1064 | * because the errors ares the only one mean to return immediately |
| 1065 | * from and lua execution. |
| 1066 | */ |
| 1067 | if (lua->flags & HLUA_EXIT) { |
| 1068 | ret = HLUA_E_OK; |
Thierry FOURNIER | e1587b3 | 2015-08-28 09:54:13 +0200 | [diff] [blame] | 1069 | hlua_ctx_renew(lua, 0); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1070 | break; |
| 1071 | } |
| 1072 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1073 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1074 | if (!lua_checkstack(lua->T, 1)) { |
| 1075 | ret = HLUA_E_ERR; |
| 1076 | break; |
| 1077 | } |
| 1078 | msg = lua_tostring(lua->T, -1); |
| 1079 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1080 | lua_pop(lua->T, 1); |
| 1081 | if (msg) |
| 1082 | lua_pushfstring(lua->T, "runtime error: %s", msg); |
| 1083 | else |
| 1084 | lua_pushfstring(lua->T, "unknown runtime error"); |
| 1085 | ret = HLUA_E_ERRMSG; |
| 1086 | break; |
| 1087 | |
| 1088 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1089 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1090 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1091 | if (!lua_checkstack(lua->T, 1)) { |
| 1092 | ret = HLUA_E_ERR; |
| 1093 | break; |
| 1094 | } |
| 1095 | lua_pushfstring(lua->T, "out of memory error"); |
| 1096 | ret = HLUA_E_ERRMSG; |
| 1097 | break; |
| 1098 | |
| 1099 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1100 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1101 | if (!lua_checkstack(lua->T, 1)) { |
| 1102 | ret = HLUA_E_ERR; |
| 1103 | break; |
| 1104 | } |
| 1105 | msg = lua_tostring(lua->T, -1); |
| 1106 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1107 | lua_pop(lua->T, 1); |
| 1108 | if (msg) |
| 1109 | lua_pushfstring(lua->T, "message handler error: %s", msg); |
| 1110 | else |
| 1111 | lua_pushfstring(lua->T, "message handler error"); |
| 1112 | ret = HLUA_E_ERRMSG; |
| 1113 | break; |
| 1114 | |
| 1115 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1116 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1117 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1118 | if (!lua_checkstack(lua->T, 1)) { |
| 1119 | ret = HLUA_E_ERR; |
| 1120 | break; |
| 1121 | } |
| 1122 | lua_pushfstring(lua->T, "unknonwn error"); |
| 1123 | ret = HLUA_E_ERRMSG; |
| 1124 | break; |
| 1125 | } |
| 1126 | |
Thierry FOURNIER | 6ab4d8e | 2015-09-27 22:17:19 +0200 | [diff] [blame] | 1127 | /* This GC permits to destroy some object when a Lua timeout strikes. */ |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1128 | if (lua->flags & HLUA_MUST_GC && |
| 1129 | ret != HLUA_E_AGAIN) |
Thierry FOURNIER | 6ab4d8e | 2015-09-27 22:17:19 +0200 | [diff] [blame] | 1130 | lua_gc(lua->T, LUA_GCCOLLECT, 0); |
| 1131 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1132 | switch (ret) { |
| 1133 | case HLUA_E_AGAIN: |
| 1134 | break; |
| 1135 | |
| 1136 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1137 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1138 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1139 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1140 | break; |
| 1141 | |
| 1142 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1143 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1144 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1145 | hlua_ctx_renew(lua, 0); |
| 1146 | break; |
| 1147 | |
| 1148 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1149 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1150 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1151 | break; |
| 1152 | } |
| 1153 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1154 | /* This is the main exit point, remove the Lua lock. */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1155 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1156 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1157 | return ret; |
| 1158 | } |
| 1159 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1160 | /* This function exit the current code. */ |
| 1161 | __LJMP static int hlua_done(lua_State *L) |
| 1162 | { |
| 1163 | struct hlua *hlua = hlua_gethlua(L); |
| 1164 | |
| 1165 | hlua->flags |= HLUA_EXIT; |
| 1166 | WILL_LJMP(lua_error(L)); |
| 1167 | |
| 1168 | return 0; |
| 1169 | } |
| 1170 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1171 | /* This function is an LUA binding. It provides a function |
| 1172 | * for deleting ACL from a referenced ACL file. |
| 1173 | */ |
| 1174 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1175 | { |
| 1176 | const char *name; |
| 1177 | const char *key; |
| 1178 | struct pat_ref *ref; |
| 1179 | |
| 1180 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1181 | |
| 1182 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1183 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1184 | |
| 1185 | ref = pat_ref_lookup(name); |
| 1186 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1187 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1188 | |
| 1189 | pat_ref_delete(ref, key); |
| 1190 | return 0; |
| 1191 | } |
| 1192 | |
| 1193 | /* This function is an LUA binding. It provides a function |
| 1194 | * for deleting map entry from a referenced map file. |
| 1195 | */ |
| 1196 | static int hlua_del_map(lua_State *L) |
| 1197 | { |
| 1198 | const char *name; |
| 1199 | const char *key; |
| 1200 | struct pat_ref *ref; |
| 1201 | |
| 1202 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1203 | |
| 1204 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1205 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1206 | |
| 1207 | ref = pat_ref_lookup(name); |
| 1208 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1209 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1210 | |
| 1211 | pat_ref_delete(ref, key); |
| 1212 | return 0; |
| 1213 | } |
| 1214 | |
| 1215 | /* This function is an LUA binding. It provides a function |
| 1216 | * for adding ACL pattern from a referenced ACL file. |
| 1217 | */ |
| 1218 | static int hlua_add_acl(lua_State *L) |
| 1219 | { |
| 1220 | const char *name; |
| 1221 | const char *key; |
| 1222 | struct pat_ref *ref; |
| 1223 | |
| 1224 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1225 | |
| 1226 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1227 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1228 | |
| 1229 | ref = pat_ref_lookup(name); |
| 1230 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1231 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1232 | |
| 1233 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1234 | pat_ref_add(ref, key, NULL, NULL); |
| 1235 | return 0; |
| 1236 | } |
| 1237 | |
| 1238 | /* This function is an LUA binding. It provides a function |
| 1239 | * for setting map pattern and sample from a referenced map |
| 1240 | * file. |
| 1241 | */ |
| 1242 | static int hlua_set_map(lua_State *L) |
| 1243 | { |
| 1244 | const char *name; |
| 1245 | const char *key; |
| 1246 | const char *value; |
| 1247 | struct pat_ref *ref; |
| 1248 | |
| 1249 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1250 | |
| 1251 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1252 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1253 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1254 | |
| 1255 | ref = pat_ref_lookup(name); |
| 1256 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1257 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1258 | |
| 1259 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1260 | pat_ref_set(ref, key, value, NULL); |
| 1261 | else |
| 1262 | pat_ref_add(ref, key, value, NULL); |
| 1263 | return 0; |
| 1264 | } |
| 1265 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1266 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1267 | * an integer or user data. This data is associated with a metatable. This |
| 1268 | * metatable have an original version registred in the global context with |
| 1269 | * the name of the object (_G[<name>] = <metable> ). |
| 1270 | * |
| 1271 | * A metable is a table that modify the standard behavior of a standard |
| 1272 | * access to the associated data. The entries of this new metatable are |
| 1273 | * defined as is: |
| 1274 | * |
| 1275 | * http://lua-users.org/wiki/MetatableEvents |
| 1276 | * |
| 1277 | * __index |
| 1278 | * |
| 1279 | * we access an absent field in a table, the result is nil. This is |
| 1280 | * true, but it is not the whole truth. Actually, such access triggers |
| 1281 | * the interpreter to look for an __index metamethod: If there is no |
| 1282 | * such method, as usually happens, then the access results in nil; |
| 1283 | * otherwise, the metamethod will provide the result. |
| 1284 | * |
| 1285 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1286 | * the key does not appear in the table, but the metatable has an __index |
| 1287 | * property: |
| 1288 | * |
| 1289 | * - if the value is a function, the function is called, passing in the |
| 1290 | * table and the key; the return value of that function is returned as |
| 1291 | * the result. |
| 1292 | * |
| 1293 | * - if the value is another table, the value of the key in that table is |
| 1294 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1295 | * table's metatable has an __index property, then it continues on up) |
| 1296 | * |
| 1297 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1298 | * |
| 1299 | * http://www.lua.org/pil/13.4.1.html |
| 1300 | * |
| 1301 | * __newindex |
| 1302 | * |
| 1303 | * Like __index, but control property assignment. |
| 1304 | * |
| 1305 | * __mode - Control weak references. A string value with one or both |
| 1306 | * of the characters 'k' and 'v' which specifies that the the |
| 1307 | * keys and/or values in the table are weak references. |
| 1308 | * |
| 1309 | * __call - Treat a table like a function. When a table is followed by |
| 1310 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1311 | * a __call key pointing to a function, that function is invoked |
| 1312 | * (passing any specified arguments) and the return value is |
| 1313 | * returned. |
| 1314 | * |
| 1315 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1316 | * called, if the metatable for myTable has a __metatable |
| 1317 | * key, the value of that key is returned instead of the |
| 1318 | * actual metatable. |
| 1319 | * |
| 1320 | * __tostring - Control string representation. When the builtin |
| 1321 | * "tostring( myTable )" function is called, if the metatable |
| 1322 | * for myTable has a __tostring property set to a function, |
| 1323 | * that function is invoked (passing myTable to it) and the |
| 1324 | * return value is used as the string representation. |
| 1325 | * |
| 1326 | * __len - Control table length. When the table length is requested using |
| 1327 | * the length operator ( '#' ), if the metatable for myTable has |
| 1328 | * a __len key pointing to a function, that function is invoked |
| 1329 | * (passing myTable to it) and the return value used as the value |
| 1330 | * of "#myTable". |
| 1331 | * |
| 1332 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1333 | * collected, if the metatable has a __gc field pointing to a |
| 1334 | * function, that function is first invoked, passing the userdata |
| 1335 | * to it. The __gc metamethod is not called for tables. |
| 1336 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1337 | * |
| 1338 | * Special metamethods for redefining standard operators: |
| 1339 | * http://www.lua.org/pil/13.1.html |
| 1340 | * |
| 1341 | * __add "+" |
| 1342 | * __sub "-" |
| 1343 | * __mul "*" |
| 1344 | * __div "/" |
| 1345 | * __unm "!" |
| 1346 | * __pow "^" |
| 1347 | * __concat ".." |
| 1348 | * |
| 1349 | * Special methods for redfining standar relations |
| 1350 | * http://www.lua.org/pil/13.2.html |
| 1351 | * |
| 1352 | * __eq "==" |
| 1353 | * __lt "<" |
| 1354 | * __le "<=" |
| 1355 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1356 | |
| 1357 | /* |
| 1358 | * |
| 1359 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1360 | * Class Map |
| 1361 | * |
| 1362 | * |
| 1363 | */ |
| 1364 | |
| 1365 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1366 | * a class session, otherwise it throws an error. |
| 1367 | */ |
| 1368 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1369 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1370 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1371 | } |
| 1372 | |
| 1373 | /* This function is the map constructor. It don't need |
| 1374 | * the class Map object. It creates and return a new Map |
| 1375 | * object. It must be called only during "body" or "init" |
| 1376 | * context because it process some filesystem accesses. |
| 1377 | */ |
| 1378 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1379 | { |
| 1380 | const char *fn; |
| 1381 | int match = PAT_MATCH_STR; |
| 1382 | struct sample_conv conv; |
| 1383 | const char *file = ""; |
| 1384 | int line = 0; |
| 1385 | lua_Debug ar; |
| 1386 | char *err = NULL; |
| 1387 | struct arg args[2]; |
| 1388 | |
| 1389 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1390 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1391 | |
| 1392 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1393 | |
| 1394 | if (lua_gettop(L) >= 2) { |
| 1395 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1396 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1397 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1398 | } |
| 1399 | |
| 1400 | /* Get Lua filename and line number. */ |
| 1401 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1402 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1403 | if (ar.currentline > 0) { /* is there info? */ |
| 1404 | file = ar.short_src; |
| 1405 | line = ar.currentline; |
| 1406 | } |
| 1407 | } |
| 1408 | |
| 1409 | /* fill fake sample_conv struct. */ |
| 1410 | conv.kw = ""; /* unused. */ |
| 1411 | conv.process = NULL; /* unused. */ |
| 1412 | conv.arg_mask = 0; /* unused. */ |
| 1413 | conv.val_args = NULL; /* unused. */ |
| 1414 | conv.out_type = SMP_T_STR; |
| 1415 | conv.private = (void *)(long)match; |
| 1416 | switch (match) { |
| 1417 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1418 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1419 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1420 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1421 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1422 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1423 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1424 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1425 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1426 | default: |
| 1427 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1428 | } |
| 1429 | |
| 1430 | /* fill fake args. */ |
| 1431 | args[0].type = ARGT_STR; |
| 1432 | args[0].data.str.str = (char *)fn; |
| 1433 | args[1].type = ARGT_STOP; |
| 1434 | |
| 1435 | /* load the map. */ |
| 1436 | if (!sample_load_map(args, &conv, file, line, &err)) { |
| 1437 | /* error case: we cant use luaL_error because we must |
| 1438 | * free the err variable. |
| 1439 | */ |
| 1440 | luaL_where(L, 1); |
| 1441 | lua_pushfstring(L, "'new': %s.", err); |
| 1442 | lua_concat(L, 2); |
| 1443 | free(err); |
| 1444 | WILL_LJMP(lua_error(L)); |
| 1445 | } |
| 1446 | |
| 1447 | /* create the lua object. */ |
| 1448 | lua_newtable(L); |
| 1449 | lua_pushlightuserdata(L, args[0].data.map); |
| 1450 | lua_rawseti(L, -2, 0); |
| 1451 | |
| 1452 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1453 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1454 | lua_setmetatable(L, -2); |
| 1455 | |
| 1456 | |
| 1457 | return 1; |
| 1458 | } |
| 1459 | |
| 1460 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1461 | { |
| 1462 | struct map_descriptor *desc; |
| 1463 | struct pattern *pat; |
| 1464 | struct sample smp; |
| 1465 | |
| 1466 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1467 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1468 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1469 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1470 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1471 | } |
| 1472 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1473 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1474 | smp.flags = SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1475 | 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] | 1476 | } |
| 1477 | |
| 1478 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1479 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1480 | if (str) |
| 1481 | lua_pushstring(L, ""); |
| 1482 | else |
| 1483 | lua_pushnil(L); |
| 1484 | return 1; |
| 1485 | } |
| 1486 | |
| 1487 | /* 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] | 1488 | 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] | 1489 | return 1; |
| 1490 | } |
| 1491 | |
| 1492 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1493 | { |
| 1494 | return _hlua_map_lookup(L, 0); |
| 1495 | } |
| 1496 | |
| 1497 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1498 | { |
| 1499 | return _hlua_map_lookup(L, 1); |
| 1500 | } |
| 1501 | |
| 1502 | /* |
| 1503 | * |
| 1504 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1505 | * Class Socket |
| 1506 | * |
| 1507 | * |
| 1508 | */ |
| 1509 | |
| 1510 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1511 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1512 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1513 | } |
| 1514 | |
| 1515 | /* This function is the handler called for each I/O on the established |
| 1516 | * connection. It is used for notify space avalaible to send or data |
| 1517 | * received. |
| 1518 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1519 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1520 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1521 | struct stream_interface *si = appctx->owner; |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 1522 | struct connection *c = cs_conn(objt_cs(si_opposite(si)->end)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1523 | |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1524 | if (appctx->ctx.hlua_cosocket.die) { |
| 1525 | si_shutw(si); |
| 1526 | si_shutr(si); |
| 1527 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1528 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1529 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1530 | stream_shutdown(si_strm(si), SF_ERR_KILLED); |
| 1531 | } |
| 1532 | |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1533 | /* If the connection object is not avalaible, close all the |
| 1534 | * streams and wakeup everithing waiting for. |
| 1535 | */ |
| 1536 | if (!c) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1537 | si_shutw(si); |
| 1538 | si_shutr(si); |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 1539 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1540 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1541 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1542 | return; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1543 | } |
| 1544 | |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1545 | /* If we cant write, wakeup the pending write signals. */ |
| 1546 | if (channel_output_closed(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1547 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1548 | |
| 1549 | /* If we cant read, wakeup the pending read signals. */ |
| 1550 | if (channel_input_closed(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1551 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1552 | |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1553 | /* if the connection is not estabkished, inform the stream that we want |
| 1554 | * to be notified whenever the connection completes. |
| 1555 | */ |
| 1556 | if (!(c->flags & CO_FL_CONNECTED)) { |
| 1557 | si_applet_cant_get(si); |
| 1558 | si_applet_cant_put(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1559 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1560 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1561 | |
| 1562 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1563 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1564 | |
| 1565 | /* 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] | 1566 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1567 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1568 | |
| 1569 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1570 | if (!channel_is_empty(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1571 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1572 | } |
| 1573 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1574 | /* This function is called when the "struct stream" is destroyed. |
| 1575 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1576 | * Wake all the pending signals. |
| 1577 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1578 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1579 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1580 | struct xref *peer; |
| 1581 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1582 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1583 | peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref); |
| 1584 | if (peer) |
| 1585 | xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1586 | |
| 1587 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1588 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1589 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1590 | } |
| 1591 | |
| 1592 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1593 | * uses this object. If the stream does not exists, just quit. |
| 1594 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1595 | * pending signal can rest in the read and write lists. destroy |
| 1596 | * it. |
| 1597 | */ |
| 1598 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1599 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1600 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1601 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1602 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1603 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1604 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1605 | |
| 1606 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1607 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1608 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1609 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1610 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1611 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1612 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1613 | appctx->ctx.hlua_cosocket.die = 1; |
| 1614 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1615 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1616 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1617 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1618 | return 0; |
| 1619 | } |
| 1620 | |
| 1621 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1622 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1623 | */ |
| 1624 | __LJMP static int hlua_socket_close(lua_State *L) |
| 1625 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1626 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1627 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1628 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1629 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1630 | MAY_LJMP(check_args(L, 1, "close")); |
| 1631 | |
| 1632 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1633 | |
| 1634 | /* Check if we run on the same thread than the xreator thread. |
| 1635 | * We cannot access to the socket if the thread is different. |
| 1636 | */ |
| 1637 | if (socket->tid != tid) |
| 1638 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1639 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1640 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1641 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1642 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1643 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1644 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1645 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1646 | appctx->ctx.hlua_cosocket.die = 1; |
| 1647 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1648 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1649 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1650 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1651 | return 0; |
| 1652 | } |
| 1653 | |
| 1654 | /* This Lua function assumes that the stack contain three parameters. |
| 1655 | * 1 - USERDATA containing a struct socket |
| 1656 | * 2 - INTEGER with values of the macro defined below |
| 1657 | * If the integer is -1, we must read at most one line. |
| 1658 | * If the integer is -2, we ust read all the data until the |
| 1659 | * end of the stream. |
| 1660 | * If the integer is positive value, we must read a number of |
| 1661 | * bytes corresponding to this value. |
| 1662 | */ |
| 1663 | #define HLSR_READ_LINE (-1) |
| 1664 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1665 | __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] | 1666 | { |
| 1667 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1668 | int wanted = lua_tointeger(L, 2); |
| 1669 | struct hlua *hlua = hlua_gethlua(L); |
| 1670 | struct appctx *appctx; |
| 1671 | int len; |
| 1672 | int nblk; |
| 1673 | char *blk1; |
| 1674 | int len1; |
| 1675 | char *blk2; |
| 1676 | int len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1677 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1678 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1679 | struct stream_interface *si; |
| 1680 | struct stream *s; |
| 1681 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1682 | |
| 1683 | /* Check if this lua stack is schedulable. */ |
| 1684 | if (!hlua || !hlua->task) |
| 1685 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 1686 | "'frontend', 'backend' or 'task'")); |
| 1687 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1688 | /* Check if we run on the same thread than the xreator thread. |
| 1689 | * We cannot access to the socket if the thread is different. |
| 1690 | */ |
| 1691 | if (socket->tid != tid) |
| 1692 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1693 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1694 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1695 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1696 | if (!peer) |
| 1697 | goto no_peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1698 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 1699 | si = appctx->owner; |
| 1700 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1701 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1702 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1703 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1704 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1705 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1706 | if (nblk < 0) /* Connection close. */ |
| 1707 | goto connection_closed; |
| 1708 | if (nblk == 0) /* No data avalaible. */ |
| 1709 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1710 | |
| 1711 | /* remove final \r\n. */ |
| 1712 | if (nblk == 1) { |
| 1713 | if (blk1[len1-1] == '\n') { |
| 1714 | len1--; |
| 1715 | skip_at_end++; |
| 1716 | if (blk1[len1-1] == '\r') { |
| 1717 | len1--; |
| 1718 | skip_at_end++; |
| 1719 | } |
| 1720 | } |
| 1721 | } |
| 1722 | else { |
| 1723 | if (blk2[len2-1] == '\n') { |
| 1724 | len2--; |
| 1725 | skip_at_end++; |
| 1726 | if (blk2[len2-1] == '\r') { |
| 1727 | len2--; |
| 1728 | skip_at_end++; |
| 1729 | } |
| 1730 | } |
| 1731 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1732 | } |
| 1733 | |
| 1734 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1735 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1736 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1737 | if (nblk < 0) /* Connection close. */ |
| 1738 | goto connection_closed; |
| 1739 | if (nblk == 0) /* No data avalaible. */ |
| 1740 | goto connection_empty; |
| 1741 | } |
| 1742 | |
| 1743 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1744 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1745 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1746 | if (nblk < 0) /* Connection close. */ |
| 1747 | goto connection_closed; |
| 1748 | if (nblk == 0) /* No data avalaible. */ |
| 1749 | goto connection_empty; |
| 1750 | |
| 1751 | if (len1 > wanted) { |
| 1752 | nblk = 1; |
| 1753 | len1 = wanted; |
| 1754 | } if (nblk == 2 && len1 + len2 > wanted) |
| 1755 | len2 = wanted - len1; |
| 1756 | } |
| 1757 | |
| 1758 | len = len1; |
| 1759 | |
| 1760 | luaL_addlstring(&socket->b, blk1, len1); |
| 1761 | if (nblk == 2) { |
| 1762 | len += len2; |
| 1763 | luaL_addlstring(&socket->b, blk2, len2); |
| 1764 | } |
| 1765 | |
| 1766 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1767 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1768 | |
| 1769 | /* Don't wait anything. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1770 | stream_int_notify(&s->si[0]); |
| 1771 | stream_int_update_applet(&s->si[0]); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1772 | |
| 1773 | /* If the pattern reclaim to read all the data |
| 1774 | * in the connection, got out. |
| 1775 | */ |
| 1776 | if (wanted == HLSR_READ_ALL) |
| 1777 | goto connection_empty; |
| 1778 | else if (wanted >= 0 && len < wanted) |
| 1779 | goto connection_empty; |
| 1780 | |
| 1781 | /* Return result. */ |
| 1782 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1783 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1784 | return 1; |
| 1785 | |
| 1786 | connection_closed: |
| 1787 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1788 | xref_unlock(&socket->xref, peer); |
| 1789 | |
| 1790 | no_peer: |
| 1791 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1792 | /* If the buffer containds data. */ |
| 1793 | if (socket->b.n > 0) { |
| 1794 | luaL_pushresult(&socket->b); |
| 1795 | return 1; |
| 1796 | } |
| 1797 | lua_pushnil(L); |
| 1798 | lua_pushstring(L, "connection closed."); |
| 1799 | return 2; |
| 1800 | |
| 1801 | connection_empty: |
| 1802 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1803 | appctx = objt_appctx(s->si[0].end); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1804 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) { |
| 1805 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1806 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1807 | } |
| 1808 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 1809 | 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] | 1810 | return 0; |
| 1811 | } |
| 1812 | |
| 1813 | /* This Lus function gets two parameters. The first one can be string |
| 1814 | * or a number. If the string is "*l", the user require one line. If |
| 1815 | * the string is "*a", the user require all the content of the stream. |
| 1816 | * If the value is a number, the user require a number of bytes equal |
| 1817 | * to the value. The default value is "*l" (a line). |
| 1818 | * |
| 1819 | * This paraeter with a variable type is converted in integer. This |
| 1820 | * integer takes this values: |
| 1821 | * -1 : read a line |
| 1822 | * -2 : read all the stream |
| 1823 | * >0 : amount if bytes. |
| 1824 | * |
| 1825 | * The second parameter is optinal. It contains a string that must be |
| 1826 | * concatenated with the read data. |
| 1827 | */ |
| 1828 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 1829 | { |
| 1830 | int wanted = HLSR_READ_LINE; |
| 1831 | const char *pattern; |
| 1832 | int type; |
| 1833 | char *error; |
| 1834 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1835 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1836 | |
| 1837 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 1838 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 1839 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1840 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1841 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1842 | /* Check if we run on the same thread than the xreator thread. |
| 1843 | * We cannot access to the socket if the thread is different. |
| 1844 | */ |
| 1845 | if (socket->tid != tid) |
| 1846 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1847 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1848 | /* check for pattern. */ |
| 1849 | if (lua_gettop(L) >= 2) { |
| 1850 | type = lua_type(L, 2); |
| 1851 | if (type == LUA_TSTRING) { |
| 1852 | pattern = lua_tostring(L, 2); |
| 1853 | if (strcmp(pattern, "*a") == 0) |
| 1854 | wanted = HLSR_READ_ALL; |
| 1855 | else if (strcmp(pattern, "*l") == 0) |
| 1856 | wanted = HLSR_READ_LINE; |
| 1857 | else { |
| 1858 | wanted = strtoll(pattern, &error, 10); |
| 1859 | if (*error != '\0') |
| 1860 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 1861 | } |
| 1862 | } |
| 1863 | else if (type == LUA_TNUMBER) { |
| 1864 | wanted = lua_tointeger(L, 2); |
| 1865 | if (wanted < 0) |
| 1866 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 1867 | } |
| 1868 | } |
| 1869 | |
| 1870 | /* Set pattern. */ |
| 1871 | lua_pushinteger(L, wanted); |
| 1872 | lua_replace(L, 2); |
| 1873 | |
| 1874 | /* init bufffer, and fiil it wih prefix. */ |
| 1875 | luaL_buffinit(L, &socket->b); |
| 1876 | |
| 1877 | /* Check prefix. */ |
| 1878 | if (lua_gettop(L) >= 3) { |
| 1879 | if (lua_type(L, 3) != LUA_TSTRING) |
| 1880 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 1881 | pattern = lua_tolstring(L, 3, &len); |
| 1882 | luaL_addlstring(&socket->b, pattern, len); |
| 1883 | } |
| 1884 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1885 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1886 | } |
| 1887 | |
| 1888 | /* Write the Lua input string in the output buffer. |
| 1889 | * This fucntion returns a yield if no space are available. |
| 1890 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1891 | 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] | 1892 | { |
| 1893 | struct hlua_socket *socket; |
| 1894 | struct hlua *hlua = hlua_gethlua(L); |
| 1895 | struct appctx *appctx; |
| 1896 | size_t buf_len; |
| 1897 | const char *buf; |
| 1898 | int len; |
| 1899 | int send_len; |
| 1900 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1901 | struct xref *peer; |
| 1902 | struct stream_interface *si; |
| 1903 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1904 | |
| 1905 | /* Check if this lua stack is schedulable. */ |
| 1906 | if (!hlua || !hlua->task) |
| 1907 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 1908 | "'frontend', 'backend' or 'task'")); |
| 1909 | |
| 1910 | /* Get object */ |
| 1911 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1912 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1913 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1914 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1915 | /* Check if we run on the same thread than the xreator thread. |
| 1916 | * We cannot access to the socket if the thread is different. |
| 1917 | */ |
| 1918 | if (socket->tid != tid) |
| 1919 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1920 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1921 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1922 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1923 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1924 | lua_pushinteger(L, -1); |
| 1925 | return 1; |
| 1926 | } |
| 1927 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 1928 | si = appctx->owner; |
| 1929 | s = si_strm(si); |
| 1930 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1931 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1932 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1933 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1934 | lua_pushinteger(L, -1); |
| 1935 | return 1; |
| 1936 | } |
| 1937 | |
| 1938 | /* Update the input buffer data. */ |
| 1939 | buf += sent; |
| 1940 | send_len = buf_len - sent; |
| 1941 | |
| 1942 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1943 | if (sent >= buf_len) { |
| 1944 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1945 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1946 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1947 | |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 1948 | /* Check if the buffer is avalaible because HAProxy doesn't allocate |
| 1949 | * the request buffer if its not required. |
| 1950 | */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1951 | if (s->req.buf->size == 0) { |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 1952 | appctx = hlua->task->context; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1953 | if (!channel_alloc_buffer(&s->req, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 1954 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 1955 | } |
| 1956 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1957 | /* Check for avalaible space. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1958 | len = buffer_total_space(s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 1959 | if (len <= 0) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1960 | appctx = objt_appctx(s->si[0].end); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1961 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 1962 | xref_unlock(&socket->xref, peer); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 1963 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1964 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1965 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 1966 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1967 | |
| 1968 | /* send data */ |
| 1969 | if (len < send_len) |
| 1970 | send_len = len; |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1971 | len = ci_putblk(&s->req, buf+sent, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1972 | |
| 1973 | /* "Not enough space" (-1), "Buffer too little to contain |
| 1974 | * the data" (-2) are not expected because the available length |
| 1975 | * is tested. |
| 1976 | * Other unknown error are also not expected. |
| 1977 | */ |
| 1978 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 1979 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1980 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 1981 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1982 | MAY_LJMP(hlua_socket_close(L)); |
| 1983 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1984 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1985 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1986 | return 1; |
| 1987 | } |
| 1988 | |
| 1989 | /* update buffers. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1990 | stream_int_notify(&s->si[0]); |
| 1991 | stream_int_update_applet(&s->si[0]); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 1992 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1993 | s->req.rex = TICK_ETERNITY; |
| 1994 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1995 | |
| 1996 | /* Update length sent. */ |
| 1997 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1998 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1999 | |
| 2000 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2001 | if (sent + len >= buf_len) { |
| 2002 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2003 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2004 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2005 | |
| 2006 | hlua_socket_write_yield_return: |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2007 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 2008 | 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] | 2009 | return 0; |
| 2010 | } |
| 2011 | |
| 2012 | /* This function initiate the send of data. It just check the input |
| 2013 | * parameters and push an integer in the Lua stack that contain the |
| 2014 | * amount of data writed in the buffer. This is used by the function |
| 2015 | * "hlua_socket_write_yield" that can yield. |
| 2016 | * |
| 2017 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2018 | * the associated object. The second is a string buffer. The third is |
| 2019 | * a facultative integer that represents where is the buffer position |
| 2020 | * of the start of the data that can send. The first byte is the |
| 2021 | * position "1". The default value is "1". The fourth argument is a |
| 2022 | * facultative integer that represents where is the buffer position |
| 2023 | * of the end of the data that can send. The default is the last byte. |
| 2024 | */ |
| 2025 | static int hlua_socket_send(struct lua_State *L) |
| 2026 | { |
| 2027 | int i; |
| 2028 | int j; |
| 2029 | const char *buf; |
| 2030 | size_t buf_len; |
| 2031 | |
| 2032 | /* Check number of arguments. */ |
| 2033 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2034 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2035 | |
| 2036 | /* Get the string. */ |
| 2037 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2038 | |
| 2039 | /* Get and check j. */ |
| 2040 | if (lua_gettop(L) == 4) { |
| 2041 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2042 | if (j < 0) |
| 2043 | j = buf_len + j + 1; |
| 2044 | if (j > buf_len) |
| 2045 | j = buf_len + 1; |
| 2046 | lua_pop(L, 1); |
| 2047 | } |
| 2048 | else |
| 2049 | j = buf_len; |
| 2050 | |
| 2051 | /* Get and check i. */ |
| 2052 | if (lua_gettop(L) == 3) { |
| 2053 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2054 | if (i < 0) |
| 2055 | i = buf_len + i + 1; |
| 2056 | if (i > buf_len) |
| 2057 | i = buf_len + 1; |
| 2058 | lua_pop(L, 1); |
| 2059 | } else |
| 2060 | i = 1; |
| 2061 | |
| 2062 | /* Check bth i and j. */ |
| 2063 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2064 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2065 | return 1; |
| 2066 | } |
| 2067 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2068 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2069 | return 1; |
| 2070 | } |
| 2071 | if (i == 0) |
| 2072 | i = 1; |
| 2073 | if (j == 0) |
| 2074 | j = 1; |
| 2075 | |
| 2076 | /* Pop the string. */ |
| 2077 | lua_pop(L, 1); |
| 2078 | |
| 2079 | /* Update the buffer length. */ |
| 2080 | buf += i - 1; |
| 2081 | buf_len = j - i + 1; |
| 2082 | lua_pushlstring(L, buf, buf_len); |
| 2083 | |
| 2084 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2085 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2086 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2087 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2088 | } |
| 2089 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2090 | #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] | 2091 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2092 | { |
| 2093 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2094 | int ret; |
| 2095 | int len; |
| 2096 | char *p; |
| 2097 | |
| 2098 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2099 | if (ret <= 0) { |
| 2100 | lua_pushnil(L); |
| 2101 | return 1; |
| 2102 | } |
| 2103 | |
| 2104 | if (ret == AF_UNIX) { |
| 2105 | lua_pushstring(L, buffer+1); |
| 2106 | return 1; |
| 2107 | } |
| 2108 | else if (ret == AF_INET6) { |
| 2109 | buffer[0] = '['; |
| 2110 | len = strlen(buffer); |
| 2111 | buffer[len] = ']'; |
| 2112 | len++; |
| 2113 | buffer[len] = ':'; |
| 2114 | len++; |
| 2115 | p = buffer; |
| 2116 | } |
| 2117 | else if (ret == AF_INET) { |
| 2118 | p = buffer + 1; |
| 2119 | len = strlen(p); |
| 2120 | p[len] = ':'; |
| 2121 | len++; |
| 2122 | } |
| 2123 | else { |
| 2124 | lua_pushnil(L); |
| 2125 | return 1; |
| 2126 | } |
| 2127 | |
| 2128 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2129 | lua_pushnil(L); |
| 2130 | return 1; |
| 2131 | } |
| 2132 | |
| 2133 | lua_pushstring(L, p); |
| 2134 | return 1; |
| 2135 | } |
| 2136 | |
| 2137 | /* Returns information about the peer of the connection. */ |
| 2138 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2139 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2140 | struct hlua_socket *socket; |
| 2141 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2142 | struct xref *peer; |
| 2143 | struct appctx *appctx; |
| 2144 | struct stream_interface *si; |
| 2145 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2146 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2147 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2148 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2149 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2150 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2151 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2152 | /* Check if we run on the same thread than the xreator thread. |
| 2153 | * We cannot access to the socket if the thread is different. |
| 2154 | */ |
| 2155 | if (socket->tid != tid) |
| 2156 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2157 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2158 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2159 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2160 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2161 | lua_pushnil(L); |
| 2162 | return 1; |
| 2163 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2164 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2165 | si = appctx->owner; |
| 2166 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2167 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2168 | conn = cs_conn(objt_cs(s->si[1].end)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2169 | if (!conn) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2170 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2171 | lua_pushnil(L); |
| 2172 | return 1; |
| 2173 | } |
| 2174 | |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2175 | conn_get_to_addr(conn); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2176 | if (!(conn->flags & CO_FL_ADDR_TO_SET)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2177 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2178 | lua_pushnil(L); |
| 2179 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2180 | } |
| 2181 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2182 | ret = MAY_LJMP(hlua_socket_info(L, &conn->addr.to)); |
| 2183 | xref_unlock(&socket->xref, peer); |
| 2184 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2185 | } |
| 2186 | |
| 2187 | /* Returns information about my connection side. */ |
| 2188 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2189 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2190 | struct hlua_socket *socket; |
| 2191 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2192 | struct appctx *appctx; |
| 2193 | struct xref *peer; |
| 2194 | struct stream_interface *si; |
| 2195 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2196 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2197 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2198 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2199 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2200 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2201 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2202 | /* Check if we run on the same thread than the xreator thread. |
| 2203 | * We cannot access to the socket if the thread is different. |
| 2204 | */ |
| 2205 | if (socket->tid != tid) |
| 2206 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2207 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2208 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2209 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2210 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2211 | lua_pushnil(L); |
| 2212 | return 1; |
| 2213 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2214 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2215 | si = appctx->owner; |
| 2216 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2217 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2218 | conn = cs_conn(objt_cs(s->si[1].end)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2219 | if (!conn) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2220 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2221 | lua_pushnil(L); |
| 2222 | return 1; |
| 2223 | } |
| 2224 | |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2225 | conn_get_from_addr(conn); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2226 | if (!(conn->flags & CO_FL_ADDR_FROM_SET)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2227 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2228 | lua_pushnil(L); |
| 2229 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2230 | } |
| 2231 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2232 | ret = hlua_socket_info(L, &conn->addr.from); |
| 2233 | xref_unlock(&socket->xref, peer); |
| 2234 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2235 | } |
| 2236 | |
| 2237 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2238 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2239 | .obj_type = OBJ_TYPE_APPLET, |
| 2240 | .name = "<LUA_TCP>", |
| 2241 | .fct = hlua_socket_handler, |
| 2242 | .release = hlua_socket_release, |
| 2243 | }; |
| 2244 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2245 | __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] | 2246 | { |
| 2247 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2248 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2249 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2250 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2251 | struct stream_interface *si; |
| 2252 | struct stream *s; |
| 2253 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2254 | /* Check if we run on the same thread than the xreator thread. |
| 2255 | * We cannot access to the socket if the thread is different. |
| 2256 | */ |
| 2257 | if (socket->tid != tid) |
| 2258 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2259 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2260 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2261 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2262 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2263 | lua_pushnil(L); |
| 2264 | lua_pushstring(L, "Can't connect"); |
| 2265 | return 2; |
| 2266 | } |
| 2267 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2268 | si = appctx->owner; |
| 2269 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2270 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2271 | /* Check if we run on the same thread than the xreator thread. |
| 2272 | * We cannot access to the socket if the thread is different. |
| 2273 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2274 | if (socket->tid != tid) { |
| 2275 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2276 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2277 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2278 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2279 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2280 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2281 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2282 | lua_pushnil(L); |
| 2283 | lua_pushstring(L, "Can't connect"); |
| 2284 | return 2; |
| 2285 | } |
| 2286 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2287 | appctx = objt_appctx(s->si[0].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2288 | |
| 2289 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2290 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2291 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2292 | lua_pushinteger(L, 1); |
| 2293 | return 1; |
| 2294 | } |
| 2295 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2296 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2297 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2298 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2299 | } |
| 2300 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 2301 | 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] | 2302 | return 0; |
| 2303 | } |
| 2304 | |
| 2305 | /* This function fail or initite the connection. */ |
| 2306 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2307 | { |
| 2308 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2309 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2310 | const char *ip; |
| 2311 | struct connection *conn; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2312 | struct hlua *hlua; |
| 2313 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2314 | int low, high; |
| 2315 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2316 | struct xref *peer; |
| 2317 | struct stream_interface *si; |
| 2318 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2319 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2320 | if (lua_gettop(L) < 2) |
| 2321 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2322 | |
| 2323 | /* Get args. */ |
| 2324 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2325 | |
| 2326 | /* Check if we run on the same thread than the xreator thread. |
| 2327 | * We cannot access to the socket if the thread is different. |
| 2328 | */ |
| 2329 | if (socket->tid != tid) |
| 2330 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2331 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2332 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2333 | if (lua_gettop(L) >= 3) |
| 2334 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2335 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2336 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2337 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2338 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2339 | lua_pushnil(L); |
| 2340 | return 1; |
| 2341 | } |
| 2342 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2343 | si = appctx->owner; |
| 2344 | s = si_strm(si); |
| 2345 | |
| 2346 | /* Initialise connection. */ |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2347 | conn = cs_conn(si_alloc_cs(&s->si[1], NULL)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2348 | if (!conn) { |
| 2349 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2350 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2351 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2352 | |
Willy Tarreau | 3adac08 | 2015-09-26 17:51:09 +0200 | [diff] [blame] | 2353 | /* needed for the connection not to be closed */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2354 | conn->target = s->target; |
Willy Tarreau | 3adac08 | 2015-09-26 17:51:09 +0200 | [diff] [blame] | 2355 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2356 | /* Parse ip address. */ |
Willy Tarreau | 48ef4c9 | 2017-01-06 18:32:38 +0100 | [diff] [blame] | 2357 | addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2358 | if (!addr) { |
| 2359 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2360 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2361 | } |
| 2362 | if (low != high) { |
| 2363 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2364 | WILL_LJMP(luaL_error(L, "connect: port ranges not supported : address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2365 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2366 | memcpy(&conn->addr.to, addr, sizeof(struct sockaddr_storage)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2367 | |
| 2368 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2369 | if (low == 0) { |
| 2370 | if (conn->addr.to.ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2371 | if (port == -1) { |
| 2372 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2373 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2374 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2375 | ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(port); |
| 2376 | } else if (conn->addr.to.ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2377 | if (port == -1) { |
| 2378 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2379 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2380 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2381 | ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(port); |
| 2382 | } |
| 2383 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2384 | |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2385 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2386 | appctx = objt_appctx(s->si[0].end); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2387 | |
| 2388 | /* inform the stream that we want to be notified whenever the |
| 2389 | * connection completes. |
| 2390 | */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2391 | si_applet_cant_get(&s->si[0]); |
| 2392 | si_applet_cant_put(&s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2393 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2394 | |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2395 | hlua->flags |= HLUA_MUST_GC; |
| 2396 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2397 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2398 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2399 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2400 | } |
| 2401 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 2402 | 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] | 2403 | |
| 2404 | return 0; |
| 2405 | } |
| 2406 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2407 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2408 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2409 | { |
| 2410 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2411 | struct xref *peer; |
| 2412 | struct appctx *appctx; |
| 2413 | struct stream_interface *si; |
| 2414 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2415 | |
| 2416 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2417 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2418 | |
| 2419 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2420 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2421 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2422 | lua_pushnil(L); |
| 2423 | return 1; |
| 2424 | } |
| 2425 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2426 | si = appctx->owner; |
| 2427 | s = si_strm(si); |
| 2428 | |
| 2429 | s->target = &socket_ssl.obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2430 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2431 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2432 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2433 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2434 | |
| 2435 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2436 | { |
| 2437 | return 0; |
| 2438 | } |
| 2439 | |
| 2440 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2441 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2442 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2443 | int tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2444 | struct xref *peer; |
| 2445 | struct appctx *appctx; |
| 2446 | struct stream_interface *si; |
| 2447 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2448 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2449 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2450 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2451 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2452 | tmout = MAY_LJMP(luaL_checkinteger(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2453 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2454 | /* Check if we run on the same thread than the xreator thread. |
| 2455 | * We cannot access to the socket if the thread is different. |
| 2456 | */ |
| 2457 | if (socket->tid != tid) |
| 2458 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2459 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2460 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2461 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2462 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2463 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2464 | WILL_LJMP(lua_error(L)); |
| 2465 | return 0; |
| 2466 | } |
| 2467 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2468 | si = appctx->owner; |
| 2469 | s = si_strm(si); |
| 2470 | |
| 2471 | s->req.rto = tmout; |
| 2472 | s->req.wto = tmout; |
| 2473 | s->res.rto = tmout; |
| 2474 | s->res.wto = tmout; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2475 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2476 | |
| 2477 | return 0; |
| 2478 | } |
| 2479 | |
| 2480 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2481 | { |
| 2482 | struct hlua_socket *socket; |
| 2483 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2484 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2485 | struct stream *strm; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2486 | |
| 2487 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2488 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2489 | hlua_pusherror(L, "socket: full stack"); |
| 2490 | goto out_fail_conf; |
| 2491 | } |
| 2492 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2493 | /* Create the object: obj[0] = userdata. */ |
| 2494 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2495 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2496 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2497 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2498 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2499 | |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2500 | /* Check if the various memory pools are intialized. */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2501 | if (!pool2_stream || !pool2_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2502 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2503 | goto out_fail_conf; |
| 2504 | } |
| 2505 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2506 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2507 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2508 | lua_setmetatable(L, -2); |
| 2509 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2510 | /* Create the applet context */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 2511 | appctx = appctx_new(&update_applet, tid_bit); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2512 | if (!appctx) { |
| 2513 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2514 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2515 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2516 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2517 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2518 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2519 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 2520 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2521 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2522 | /* Now create a session, task and stream for this applet */ |
| 2523 | sess = session_new(&socket_proxy, NULL, &appctx->obj_type); |
| 2524 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2525 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2526 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2527 | } |
| 2528 | |
Willy Tarreau | 87787ac | 2017-08-28 16:22:54 +0200 | [diff] [blame] | 2529 | strm = stream_new(sess, &appctx->obj_type); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2530 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2531 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2532 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2533 | } |
| 2534 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2535 | /* Initialise cross reference between stream and Lua socket object. */ |
| 2536 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2537 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2538 | /* Configure "right" stream interface. this "si" is used to connect |
| 2539 | * and retrieve data from the server. The connection is initialized |
| 2540 | * with the "struct server". |
| 2541 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2542 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2543 | |
| 2544 | /* Force destination server. */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2545 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET | SF_BE_ASSIGNED; |
| 2546 | strm->target = &socket_tcp.obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2547 | |
Willy Tarreau | 87787ac | 2017-08-28 16:22:54 +0200 | [diff] [blame] | 2548 | task_wakeup(strm->task, TASK_WOKEN_INIT); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2549 | /* Return yield waiting for connection. */ |
| 2550 | return 1; |
| 2551 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2552 | out_fail_stream: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2553 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2554 | out_fail_sess: |
| 2555 | appctx_free(appctx); |
| 2556 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2557 | WILL_LJMP(lua_error(L)); |
| 2558 | return 0; |
| 2559 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2560 | |
| 2561 | /* |
| 2562 | * |
| 2563 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2564 | * Class Channel |
| 2565 | * |
| 2566 | * |
| 2567 | */ |
| 2568 | |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2569 | /* The state between the channel data and the HTTP parser state can be |
| 2570 | * unconsistent, so reset the parser and call it again. Warning, this |
| 2571 | * action not revalidate the request and not send a 400 if the modified |
| 2572 | * resuest is not valid. |
| 2573 | * |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2574 | * This function never fails. The direction is set using dir, which equals |
| 2575 | * either SMP_OPT_DIR_REQ or SMP_OPT_DIR_RES. |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2576 | */ |
| 2577 | static void hlua_resynchonize_proto(struct stream *stream, int dir) |
| 2578 | { |
| 2579 | /* Protocol HTTP. */ |
| 2580 | if (stream->be->mode == PR_MODE_HTTP) { |
| 2581 | |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2582 | if (dir == SMP_OPT_DIR_REQ) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2583 | http_txn_reset_req(stream->txn); |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2584 | else if (dir == SMP_OPT_DIR_RES) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2585 | http_txn_reset_res(stream->txn); |
| 2586 | |
| 2587 | if (stream->txn->hdr_idx.v) |
| 2588 | hdr_idx_init(&stream->txn->hdr_idx); |
| 2589 | |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2590 | if (dir == SMP_OPT_DIR_REQ) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2591 | http_msg_analyzer(&stream->txn->req, &stream->txn->hdr_idx); |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2592 | else if (dir == SMP_OPT_DIR_RES) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2593 | http_msg_analyzer(&stream->txn->rsp, &stream->txn->hdr_idx); |
| 2594 | } |
| 2595 | } |
| 2596 | |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2597 | /* This function is called before the Lua execution. It stores |
| 2598 | * the differents parsers state before executing some Lua code. |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2599 | */ |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2600 | 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] | 2601 | { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2602 | c->mode = stream->be->mode; |
| 2603 | switch (c->mode) { |
| 2604 | case PR_MODE_HTTP: |
| 2605 | c->data.http.dir = opt & SMP_OPT_DIR; |
| 2606 | if (c->data.http.dir == SMP_OPT_DIR_REQ) |
| 2607 | c->data.http.state = stream->txn->req.msg_state; |
| 2608 | else |
| 2609 | c->data.http.state = stream->txn->rsp.msg_state; |
| 2610 | break; |
| 2611 | default: |
| 2612 | break; |
| 2613 | } |
| 2614 | } |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2615 | |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2616 | /* This function is called after the Lua execution. it |
| 2617 | * returns true if the parser state is consistent, otherwise, |
| 2618 | * it return false. |
| 2619 | * |
| 2620 | * In HTTP mode, the parser state must be in the same state |
| 2621 | * or greater when we exit the function. Even if we do a |
| 2622 | * control yield. This prevent to break the HTTP message |
| 2623 | * from the Lua code. |
| 2624 | */ |
| 2625 | static inline int consistency_check(struct stream *stream, int opt, struct hlua_consistency *c) |
| 2626 | { |
| 2627 | if (c->mode != stream->be->mode) |
| 2628 | return 0; |
| 2629 | |
| 2630 | switch (c->mode) { |
| 2631 | case PR_MODE_HTTP: |
| 2632 | if (c->data.http.dir != (opt & SMP_OPT_DIR)) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2633 | return 0; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2634 | if (c->data.http.dir == SMP_OPT_DIR_REQ) |
| 2635 | return stream->txn->req.msg_state >= c->data.http.state; |
| 2636 | else |
| 2637 | return stream->txn->rsp.msg_state >= c->data.http.state; |
| 2638 | default: |
| 2639 | return 1; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2640 | } |
| 2641 | return 1; |
| 2642 | } |
| 2643 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2644 | /* Returns the struct hlua_channel join to the class channel in the |
| 2645 | * stack entry "ud" or throws an argument error. |
| 2646 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2647 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2648 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2649 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2650 | } |
| 2651 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2652 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 2653 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2654 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 2655 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2656 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2657 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2658 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2659 | return 0; |
| 2660 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2661 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2662 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2663 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2664 | |
| 2665 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2666 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 2667 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2668 | return 1; |
| 2669 | } |
| 2670 | |
| 2671 | /* Duplicate all the data present in the input channel and put it |
| 2672 | * in a string LUA variables. Returns -1 and push a nil value in |
| 2673 | * the stack if the channel is closed and all the data are consumed, |
| 2674 | * returns 0 if no data are available, otherwise it returns the length |
| 2675 | * of the builded string. |
| 2676 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2677 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2678 | { |
| 2679 | char *blk1; |
| 2680 | char *blk2; |
| 2681 | int len1; |
| 2682 | int len2; |
| 2683 | int ret; |
| 2684 | luaL_Buffer b; |
| 2685 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2686 | ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2687 | if (unlikely(ret == 0)) |
| 2688 | return 0; |
| 2689 | |
| 2690 | if (unlikely(ret < 0)) { |
| 2691 | lua_pushnil(L); |
| 2692 | return -1; |
| 2693 | } |
| 2694 | |
| 2695 | luaL_buffinit(L, &b); |
| 2696 | luaL_addlstring(&b, blk1, len1); |
| 2697 | if (unlikely(ret == 2)) |
| 2698 | luaL_addlstring(&b, blk2, len2); |
| 2699 | luaL_pushresult(&b); |
| 2700 | |
| 2701 | if (unlikely(ret == 2)) |
| 2702 | return len1 + len2; |
| 2703 | return len1; |
| 2704 | } |
| 2705 | |
| 2706 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2707 | * a yield. This function keep the data in the buffer. |
| 2708 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2709 | __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] | 2710 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2711 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2712 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2713 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2714 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2715 | if (_hlua_channel_dup(chn, L) == 0) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2716 | 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] | 2717 | return 1; |
| 2718 | } |
| 2719 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2720 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 2721 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 2722 | { |
| 2723 | MAY_LJMP(check_args(L, 1, "dup")); |
| 2724 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2725 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 2726 | } |
| 2727 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2728 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2729 | * a yield. This function consumes the data in the buffer. It returns |
| 2730 | * a string containing the data or a nil pointer if no data are available |
| 2731 | * and the channel is closed. |
| 2732 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2733 | __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] | 2734 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2735 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2736 | int ret; |
| 2737 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2738 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2739 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2740 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2741 | if (unlikely(ret == 0)) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2742 | 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] | 2743 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2744 | if (unlikely(ret == -1)) |
| 2745 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2746 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2747 | chn->buf->i -= ret; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2748 | hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2749 | return 1; |
| 2750 | } |
| 2751 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2752 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 2753 | __LJMP static int hlua_channel_get(lua_State *L) |
| 2754 | { |
| 2755 | MAY_LJMP(check_args(L, 1, "get")); |
| 2756 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2757 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 2758 | } |
| 2759 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2760 | /* This functions consumes and returns one line. If the channel is closed, |
| 2761 | * and the last data does not contains a final '\n', the data are returned |
| 2762 | * without the final '\n'. When no more data are avalaible, it returns nil |
| 2763 | * value. |
| 2764 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2765 | __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] | 2766 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2767 | char *blk1; |
| 2768 | char *blk2; |
| 2769 | int len1; |
| 2770 | int len2; |
| 2771 | int len; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2772 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2773 | int ret; |
| 2774 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2775 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2776 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2777 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2778 | ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2779 | if (ret == 0) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2780 | 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] | 2781 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2782 | if (ret == -1) { |
| 2783 | lua_pushnil(L); |
| 2784 | return 1; |
| 2785 | } |
| 2786 | |
| 2787 | luaL_buffinit(L, &b); |
| 2788 | luaL_addlstring(&b, blk1, len1); |
| 2789 | len = len1; |
| 2790 | if (unlikely(ret == 2)) { |
| 2791 | luaL_addlstring(&b, blk2, len2); |
| 2792 | len += len2; |
| 2793 | } |
| 2794 | luaL_pushresult(&b); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2795 | 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] | 2796 | hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2797 | return 1; |
| 2798 | } |
| 2799 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2800 | /* Check arguments for the fucntion "hlua_channel_getline_yield". */ |
| 2801 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 2802 | { |
| 2803 | MAY_LJMP(check_args(L, 1, "getline")); |
| 2804 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2805 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 2806 | } |
| 2807 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2808 | /* This function takes a string as input, and append it at the |
| 2809 | * input side of channel. If the data is too big, but a space |
| 2810 | * is probably available after sending some data, the function |
| 2811 | * yield. If the data is bigger than the buffer, or if the |
| 2812 | * channel is closed, it returns -1. otherwise, it returns the |
| 2813 | * amount of data writed. |
| 2814 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2815 | __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] | 2816 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2817 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2818 | size_t len; |
| 2819 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2820 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2821 | int ret; |
| 2822 | int max; |
| 2823 | |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2824 | /* Check if the buffer is avalaible because HAProxy doesn't allocate |
| 2825 | * the request buffer if its not required. |
| 2826 | */ |
| 2827 | if (chn->buf->size == 0) { |
| 2828 | si_applet_cant_put(chn_prod(chn)); |
| 2829 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0)); |
| 2830 | } |
| 2831 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2832 | max = channel_recv_limit(chn) - buffer_len(chn->buf); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2833 | if (max > len - l) |
| 2834 | max = len - l; |
| 2835 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2836 | ret = ci_putblk(chn, str + l, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2837 | if (ret == -2 || ret == -3) { |
| 2838 | lua_pushinteger(L, -1); |
| 2839 | return 1; |
| 2840 | } |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2841 | if (ret == -1) { |
| 2842 | chn->flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2843 | 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] | 2844 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2845 | l += ret; |
| 2846 | lua_pop(L, 1); |
| 2847 | lua_pushinteger(L, l); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2848 | hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2849 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2850 | max = channel_recv_limit(chn) - buffer_len(chn->buf); |
| 2851 | if (max == 0 && chn->buf->o == 0) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2852 | /* There are no space avalaible, and the output buffer is empty. |
| 2853 | * in this case, we cannot add more data, so we cannot yield, |
| 2854 | * we return the amount of copyied data. |
| 2855 | */ |
| 2856 | return 1; |
| 2857 | } |
| 2858 | if (l < len) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2859 | 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] | 2860 | return 1; |
| 2861 | } |
| 2862 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2863 | /* just a wrapper of "hlua_channel_append_yield". It returns the length |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2864 | * of the writed string, or -1 if the channel is closed or if the |
| 2865 | * buffer size is too little for the data. |
| 2866 | */ |
| 2867 | __LJMP static int hlua_channel_append(lua_State *L) |
| 2868 | { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2869 | size_t len; |
| 2870 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2871 | MAY_LJMP(check_args(L, 2, "append")); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2872 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2873 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2874 | MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2875 | lua_pushinteger(L, 0); |
| 2876 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2877 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2878 | } |
| 2879 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2880 | /* just a wrapper of "hlua_channel_append_yield". This wrapper starts |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2881 | * his process by cleaning the buffer. The result is a replacement |
| 2882 | * of the current data. It returns the length of the writed string, |
| 2883 | * or -1 if the channel is closed or if the buffer size is too |
| 2884 | * little for the data. |
| 2885 | */ |
| 2886 | __LJMP static int hlua_channel_set(lua_State *L) |
| 2887 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2888 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2889 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2890 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2891 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2892 | lua_pushinteger(L, 0); |
| 2893 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2894 | chn->buf->i = 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2895 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2896 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2897 | } |
| 2898 | |
| 2899 | /* Append data in the output side of the buffer. This data is immediatly |
| 2900 | * sent. The fcuntion returns the ammount of data writed. If the buffer |
| 2901 | * cannot contains the data, the function yield. The function returns -1 |
| 2902 | * if the channel is closed. |
| 2903 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2904 | __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] | 2905 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2906 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2907 | size_t len; |
| 2908 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2909 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2910 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2911 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2912 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2913 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2914 | lua_pushinteger(L, -1); |
| 2915 | return 1; |
| 2916 | } |
| 2917 | |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 2918 | /* Check if the buffer is avalaible because HAProxy doesn't allocate |
| 2919 | * the request buffer if its not required. |
| 2920 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2921 | if (chn->buf->size == 0) { |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2922 | si_applet_cant_put(chn_prod(chn)); |
| 2923 | 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] | 2924 | } |
| 2925 | |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2926 | /* the writed data will be immediatly sent, so we can check |
| 2927 | * the avalaible space without taking in account the reserve. |
| 2928 | * The reserve is guaranted for the processing of incoming |
| 2929 | * data, because the buffer will be flushed. |
| 2930 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2931 | max = chn->buf->size - buffer_len(chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2932 | |
| 2933 | /* If there are no space avalaible, and the output buffer is empty. |
| 2934 | * in this case, we cannot add more data, so we cannot yield, |
| 2935 | * we return the amount of copyied data. |
| 2936 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2937 | if (max == 0 && chn->buf->o == 0) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2938 | return 1; |
| 2939 | |
| 2940 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2941 | if (max > len - l) |
| 2942 | max = len - l; |
| 2943 | |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2944 | /* The buffer avalaible size may be not contiguous. This test |
| 2945 | * detects a non contiguous buffer and realign it. |
| 2946 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2947 | if (bi_space_for_replace(chn->buf) < max) |
| 2948 | buffer_slow_realign(chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2949 | |
| 2950 | /* Copy input data in the buffer. */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2951 | 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] | 2952 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2953 | /* buffer replace considers that the input part is filled. |
| 2954 | * so, I must forward these new data in the output part. |
| 2955 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2956 | b_adv(chn->buf, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2957 | |
| 2958 | l += max; |
| 2959 | lua_pop(L, 1); |
| 2960 | lua_pushinteger(L, l); |
| 2961 | |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2962 | /* If there are no space avalaible, and the output buffer is empty. |
| 2963 | * in this case, we cannot add more data, so we cannot yield, |
| 2964 | * we return the amount of copyied data. |
| 2965 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2966 | max = chn->buf->size - buffer_len(chn->buf); |
| 2967 | if (max == 0 && chn->buf->o == 0) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2968 | return 1; |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2969 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2970 | if (l < len) { |
| 2971 | /* If we are waiting for space in the response buffer, we |
| 2972 | * must set the flag WAKERESWR. This flag required the task |
| 2973 | * wake up if any activity is detected on the response buffer. |
| 2974 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2975 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2976 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 2977 | else |
| 2978 | HLUA_SET_WAKEREQWR(hlua); |
| 2979 | 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] | 2980 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2981 | |
| 2982 | return 1; |
| 2983 | } |
| 2984 | |
| 2985 | /* Just a wraper of "_hlua_channel_send". This wrapper permits |
| 2986 | * yield the LUA process, and resume it without checking the |
| 2987 | * input arguments. |
| 2988 | */ |
| 2989 | __LJMP static int hlua_channel_send(lua_State *L) |
| 2990 | { |
| 2991 | MAY_LJMP(check_args(L, 2, "send")); |
| 2992 | lua_pushinteger(L, 0); |
| 2993 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2994 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2995 | } |
| 2996 | |
| 2997 | /* This function forward and amount of butes. The data pass from |
| 2998 | * the input side of the buffer to the output side, and can be |
| 2999 | * forwarded. This function never fails. |
| 3000 | * |
| 3001 | * The Lua function takes an amount of bytes to be forwarded in |
| 3002 | * imput. It returns the number of bytes forwarded. |
| 3003 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3004 | __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] | 3005 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3006 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3007 | int len; |
| 3008 | int l; |
| 3009 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3010 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3011 | |
| 3012 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3013 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3014 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3015 | |
| 3016 | max = len - l; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3017 | if (max > chn->buf->i) |
| 3018 | max = chn->buf->i; |
| 3019 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3020 | l += max; |
| 3021 | |
| 3022 | lua_pop(L, 1); |
| 3023 | lua_pushinteger(L, l); |
| 3024 | |
| 3025 | /* Check if it miss bytes to forward. */ |
| 3026 | if (l < len) { |
| 3027 | /* The the input channel or the output channel are closed, we |
| 3028 | * must return the amount of data forwarded. |
| 3029 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3030 | if (channel_input_closed(chn) || channel_output_closed(chn)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3031 | return 1; |
| 3032 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3033 | /* If we are waiting for space data in the response buffer, we |
| 3034 | * must set the flag WAKERESWR. This flag required the task |
| 3035 | * wake up if any activity is detected on the response buffer. |
| 3036 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3037 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3038 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3039 | else |
| 3040 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3041 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3042 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 3043 | 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] | 3044 | } |
| 3045 | |
| 3046 | return 1; |
| 3047 | } |
| 3048 | |
| 3049 | /* Just check the input and prepare the stack for the previous |
| 3050 | * function "hlua_channel_forward_yield" |
| 3051 | */ |
| 3052 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3053 | { |
| 3054 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3055 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3056 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3057 | |
| 3058 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3059 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3060 | } |
| 3061 | |
| 3062 | /* Just returns the number of bytes available in the input |
| 3063 | * side of the buffer. This function never fails. |
| 3064 | */ |
| 3065 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3066 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3067 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3068 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3069 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3070 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3071 | lua_pushinteger(L, chn->buf->i); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3072 | return 1; |
| 3073 | } |
| 3074 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3075 | /* Returns true if the channel is full. */ |
| 3076 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3077 | { |
| 3078 | struct channel *chn; |
| 3079 | int rem; |
| 3080 | |
| 3081 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3082 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3083 | |
| 3084 | rem = chn->buf->size; |
| 3085 | rem -= chn->buf->o; /* Output size */ |
| 3086 | rem -= chn->buf->i; /* Input size */ |
| 3087 | rem -= global.tune.maxrewrite; /* Rewrite reserved size */ |
| 3088 | |
| 3089 | lua_pushboolean(L, rem <= 0); |
| 3090 | return 1; |
| 3091 | } |
| 3092 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3093 | /* Just returns the number of bytes available in the output |
| 3094 | * side of the buffer. This function never fails. |
| 3095 | */ |
| 3096 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3097 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3098 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3099 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3100 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3101 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3102 | lua_pushinteger(L, chn->buf->o); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3103 | return 1; |
| 3104 | } |
| 3105 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3106 | /* |
| 3107 | * |
| 3108 | * |
| 3109 | * Class Fetches |
| 3110 | * |
| 3111 | * |
| 3112 | */ |
| 3113 | |
| 3114 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3115 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3116 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3117 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3118 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3119 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3120 | } |
| 3121 | |
| 3122 | /* This function creates and push in the stack a fetch object according |
| 3123 | * with a current TXN. |
| 3124 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3125 | 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] | 3126 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3127 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3128 | |
| 3129 | /* Check stack size. */ |
| 3130 | if (!lua_checkstack(L, 3)) |
| 3131 | return 0; |
| 3132 | |
| 3133 | /* Create the object: obj[0] = userdata. |
| 3134 | * Note that the base of the Fetches object is the |
| 3135 | * transaction object. |
| 3136 | */ |
| 3137 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3138 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3139 | lua_rawseti(L, -2, 0); |
| 3140 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3141 | hsmp->s = txn->s; |
| 3142 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3143 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3144 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3145 | |
| 3146 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3147 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 3148 | lua_setmetatable(L, -2); |
| 3149 | |
| 3150 | return 1; |
| 3151 | } |
| 3152 | |
| 3153 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 3154 | * It uses closure argument to store the associated sample-fetch. It |
| 3155 | * returns only one argument or throws an error. An error is thrown |
| 3156 | * only if an error is encountered during the argument parsing. If |
| 3157 | * the "sample-fetch" function fails, nil is returned. |
| 3158 | */ |
| 3159 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 3160 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3161 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3162 | struct sample_fetch *f; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3163 | struct arg args[ARGM_NBARGS + 1]; |
| 3164 | int i; |
| 3165 | struct sample smp; |
| 3166 | |
| 3167 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3168 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3169 | |
| 3170 | /* Get traditionnal arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3171 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3172 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 3173 | /* Check execution authorization. */ |
| 3174 | if (f->use & SMP_USE_HTTP_ANY && |
| 3175 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 3176 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 3177 | "is not available in Lua services", f->kw); |
| 3178 | WILL_LJMP(lua_error(L)); |
| 3179 | } |
| 3180 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3181 | /* Get extra arguments. */ |
| 3182 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 3183 | if (i >= ARGM_NBARGS) |
| 3184 | break; |
| 3185 | hlua_lua2arg(L, i + 2, &args[i]); |
| 3186 | } |
| 3187 | args[i].type = ARGT_STOP; |
David Carlier | abdb00f | 2016-04-27 16:14:50 +0100 | [diff] [blame] | 3188 | args[i].data.str.str = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3189 | |
| 3190 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3191 | 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] | 3192 | |
| 3193 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3194 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3195 | lua_pushfstring(L, "error in arguments"); |
| 3196 | WILL_LJMP(lua_error(L)); |
| 3197 | } |
| 3198 | |
| 3199 | /* Initialise the sample. */ |
| 3200 | memset(&smp, 0, sizeof(smp)); |
| 3201 | |
| 3202 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3203 | 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] | 3204 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3205 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3206 | lua_pushstring(L, ""); |
| 3207 | else |
| 3208 | lua_pushnil(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3209 | return 1; |
| 3210 | } |
| 3211 | |
| 3212 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3213 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3214 | hlua_smp2lua_str(L, &smp); |
| 3215 | else |
| 3216 | hlua_smp2lua(L, &smp); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3217 | return 1; |
| 3218 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3219 | |
| 3220 | /* |
| 3221 | * |
| 3222 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3223 | * Class Converters |
| 3224 | * |
| 3225 | * |
| 3226 | */ |
| 3227 | |
| 3228 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3229 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3230 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3231 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3232 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3233 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3234 | } |
| 3235 | |
| 3236 | /* This function creates and push in the stack a Converters object |
| 3237 | * according with a current TXN. |
| 3238 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3239 | 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] | 3240 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3241 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3242 | |
| 3243 | /* Check stack size. */ |
| 3244 | if (!lua_checkstack(L, 3)) |
| 3245 | return 0; |
| 3246 | |
| 3247 | /* Create the object: obj[0] = userdata. |
| 3248 | * Note that the base of the Converters object is the |
| 3249 | * same than the TXN object. |
| 3250 | */ |
| 3251 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3252 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3253 | lua_rawseti(L, -2, 0); |
| 3254 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3255 | hsmp->s = txn->s; |
| 3256 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3257 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3258 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3259 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3260 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3261 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 3262 | lua_setmetatable(L, -2); |
| 3263 | |
| 3264 | return 1; |
| 3265 | } |
| 3266 | |
| 3267 | /* This function is an LUA binding. It is called with each converter. |
| 3268 | * It uses closure argument to store the associated converter. It |
| 3269 | * returns only one argument or throws an error. An error is thrown |
| 3270 | * only if an error is encountered during the argument parsing. If |
| 3271 | * the converter function function fails, nil is returned. |
| 3272 | */ |
| 3273 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 3274 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3275 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3276 | struct sample_conv *conv; |
| 3277 | struct arg args[ARGM_NBARGS + 1]; |
| 3278 | int i; |
| 3279 | struct sample smp; |
| 3280 | |
| 3281 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3282 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3283 | |
| 3284 | /* Get traditionnal arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3285 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3286 | |
| 3287 | /* Get extra arguments. */ |
| 3288 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 3289 | if (i >= ARGM_NBARGS) |
| 3290 | break; |
| 3291 | hlua_lua2arg(L, i + 3, &args[i]); |
| 3292 | } |
| 3293 | args[i].type = ARGT_STOP; |
David Carlier | abdb00f | 2016-04-27 16:14:50 +0100 | [diff] [blame] | 3294 | args[i].data.str.str = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3295 | |
| 3296 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3297 | 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] | 3298 | |
| 3299 | /* Run the special args checker. */ |
| 3300 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 3301 | hlua_pusherror(L, "error in arguments"); |
| 3302 | WILL_LJMP(lua_error(L)); |
| 3303 | } |
| 3304 | |
| 3305 | /* Initialise the sample. */ |
| 3306 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 3307 | hlua_pusherror(L, "error in the input argument"); |
| 3308 | WILL_LJMP(lua_error(L)); |
| 3309 | } |
| 3310 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3311 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 3312 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3313 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3314 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3315 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3316 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3317 | WILL_LJMP(lua_error(L)); |
| 3318 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3319 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 3320 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3321 | hlua_pusherror(L, "error during the input argument casting"); |
| 3322 | WILL_LJMP(lua_error(L)); |
| 3323 | } |
| 3324 | |
| 3325 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3326 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3327 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3328 | lua_pushstring(L, ""); |
| 3329 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3330 | lua_pushnil(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3331 | return 1; |
| 3332 | } |
| 3333 | |
| 3334 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3335 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3336 | hlua_smp2lua_str(L, &smp); |
| 3337 | else |
| 3338 | hlua_smp2lua(L, &smp); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3339 | return 1; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3340 | } |
| 3341 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3342 | /* |
| 3343 | * |
| 3344 | * |
| 3345 | * Class AppletTCP |
| 3346 | * |
| 3347 | * |
| 3348 | */ |
| 3349 | |
| 3350 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 3351 | * a class stream, otherwise it throws an error. |
| 3352 | */ |
| 3353 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 3354 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3355 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3356 | } |
| 3357 | |
| 3358 | /* This function creates and push in the stack an Applet object |
| 3359 | * according with a current TXN. |
| 3360 | */ |
| 3361 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 3362 | { |
| 3363 | struct hlua_appctx *appctx; |
| 3364 | struct stream_interface *si = ctx->owner; |
| 3365 | struct stream *s = si_strm(si); |
| 3366 | struct proxy *p = s->be; |
| 3367 | |
| 3368 | /* Check stack size. */ |
| 3369 | if (!lua_checkstack(L, 3)) |
| 3370 | return 0; |
| 3371 | |
| 3372 | /* Create the object: obj[0] = userdata. |
| 3373 | * Note that the base of the Converters object is the |
| 3374 | * same than the TXN object. |
| 3375 | */ |
| 3376 | lua_newtable(L); |
| 3377 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
| 3378 | lua_rawseti(L, -2, 0); |
| 3379 | appctx->appctx = ctx; |
| 3380 | appctx->htxn.s = s; |
| 3381 | appctx->htxn.p = p; |
| 3382 | |
| 3383 | /* Create the "f" field that contains a list of fetches. */ |
| 3384 | lua_pushstring(L, "f"); |
| 3385 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3386 | return 0; |
| 3387 | lua_settable(L, -3); |
| 3388 | |
| 3389 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3390 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3391 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3392 | return 0; |
| 3393 | lua_settable(L, -3); |
| 3394 | |
| 3395 | /* Create the "c" field that contains a list of converters. */ |
| 3396 | lua_pushstring(L, "c"); |
| 3397 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3398 | return 0; |
| 3399 | lua_settable(L, -3); |
| 3400 | |
| 3401 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3402 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3403 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3404 | return 0; |
| 3405 | lua_settable(L, -3); |
| 3406 | |
| 3407 | /* Pop a class stream metatable and affect it to the table. */ |
| 3408 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 3409 | lua_setmetatable(L, -2); |
| 3410 | |
| 3411 | return 1; |
| 3412 | } |
| 3413 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3414 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 3415 | { |
| 3416 | struct hlua_appctx *appctx; |
| 3417 | struct stream *s; |
| 3418 | const char *name; |
| 3419 | size_t len; |
| 3420 | struct sample smp; |
| 3421 | |
| 3422 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 3423 | |
| 3424 | /* It is useles to retrieve the stream, but this function |
| 3425 | * runs only in a stream context. |
| 3426 | */ |
| 3427 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3428 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3429 | s = appctx->htxn.s; |
| 3430 | |
| 3431 | /* Converts the third argument in a sample. */ |
| 3432 | hlua_lua2smp(L, 3, &smp); |
| 3433 | |
| 3434 | /* Store the sample in a variable. */ |
| 3435 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3436 | vars_set_by_name(name, len, &smp); |
| 3437 | return 0; |
| 3438 | } |
| 3439 | |
| 3440 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 3441 | { |
| 3442 | struct hlua_appctx *appctx; |
| 3443 | struct stream *s; |
| 3444 | const char *name; |
| 3445 | size_t len; |
| 3446 | struct sample smp; |
| 3447 | |
| 3448 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3449 | |
| 3450 | /* It is useles to retrieve the stream, but this function |
| 3451 | * runs only in a stream context. |
| 3452 | */ |
| 3453 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3454 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3455 | s = appctx->htxn.s; |
| 3456 | |
| 3457 | /* Unset the variable. */ |
| 3458 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3459 | vars_unset_by_name(name, len, &smp); |
| 3460 | return 0; |
| 3461 | } |
| 3462 | |
| 3463 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 3464 | { |
| 3465 | struct hlua_appctx *appctx; |
| 3466 | struct stream *s; |
| 3467 | const char *name; |
| 3468 | size_t len; |
| 3469 | struct sample smp; |
| 3470 | |
| 3471 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3472 | |
| 3473 | /* It is useles to retrieve the stream, but this function |
| 3474 | * runs only in a stream context. |
| 3475 | */ |
| 3476 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3477 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3478 | s = appctx->htxn.s; |
| 3479 | |
| 3480 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3481 | if (!vars_get_by_name(name, len, &smp)) { |
| 3482 | lua_pushnil(L); |
| 3483 | return 1; |
| 3484 | } |
| 3485 | |
| 3486 | return hlua_smp2lua(L, &smp); |
| 3487 | } |
| 3488 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3489 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 3490 | { |
| 3491 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3492 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3493 | struct hlua *hlua; |
| 3494 | |
| 3495 | /* 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] | 3496 | if (!s->hlua) |
| 3497 | return 0; |
| 3498 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3499 | |
| 3500 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3501 | |
| 3502 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 3503 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3504 | |
| 3505 | /* Get and store new value. */ |
| 3506 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3507 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3508 | |
| 3509 | return 0; |
| 3510 | } |
| 3511 | |
| 3512 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 3513 | { |
| 3514 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3515 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3516 | struct hlua *hlua; |
| 3517 | |
| 3518 | /* 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] | 3519 | if (!s->hlua) { |
| 3520 | lua_pushnil(L); |
| 3521 | return 1; |
| 3522 | } |
| 3523 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3524 | |
| 3525 | /* Push configuration index in the stack. */ |
| 3526 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3527 | |
| 3528 | return 1; |
| 3529 | } |
| 3530 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3531 | /* If expected data not yet available, it returns a yield. This function |
| 3532 | * consumes the data in the buffer. It returns a string containing the |
| 3533 | * data. This string can be empty. |
| 3534 | */ |
| 3535 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 3536 | { |
| 3537 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3538 | struct stream_interface *si = appctx->appctx->owner; |
| 3539 | int ret; |
| 3540 | char *blk1; |
| 3541 | int len1; |
| 3542 | char *blk2; |
| 3543 | int len2; |
| 3544 | |
| 3545 | /* Read the maximum amount of data avalaible. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3546 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3547 | |
| 3548 | /* Data not yet avalaible. return yield. */ |
| 3549 | if (ret == 0) { |
| 3550 | si_applet_cant_get(si); |
| 3551 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0)); |
| 3552 | } |
| 3553 | |
| 3554 | /* End of data: commit the total strings and return. */ |
| 3555 | if (ret < 0) { |
| 3556 | luaL_pushresult(&appctx->b); |
| 3557 | return 1; |
| 3558 | } |
| 3559 | |
| 3560 | /* Ensure that the block 2 length is usable. */ |
| 3561 | if (ret == 1) |
| 3562 | len2 = 0; |
| 3563 | |
| 3564 | /* dont check the max length read and dont check. */ |
| 3565 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3566 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3567 | |
| 3568 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3569 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3570 | luaL_pushresult(&appctx->b); |
| 3571 | return 1; |
| 3572 | } |
| 3573 | |
| 3574 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 3575 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 3576 | { |
| 3577 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3578 | |
| 3579 | /* Initialise the string catenation. */ |
| 3580 | luaL_buffinit(L, &appctx->b); |
| 3581 | |
| 3582 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 3583 | } |
| 3584 | |
| 3585 | /* If expected data not yet available, it returns a yield. This function |
| 3586 | * consumes the data in the buffer. It returns a string containing the |
| 3587 | * data. This string can be empty. |
| 3588 | */ |
| 3589 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 3590 | { |
| 3591 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3592 | struct stream_interface *si = appctx->appctx->owner; |
| 3593 | int len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3594 | int ret; |
| 3595 | char *blk1; |
| 3596 | int len1; |
| 3597 | char *blk2; |
| 3598 | int len2; |
| 3599 | |
| 3600 | /* Read the maximum amount of data avalaible. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3601 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3602 | |
| 3603 | /* Data not yet avalaible. return yield. */ |
| 3604 | if (ret == 0) { |
| 3605 | si_applet_cant_get(si); |
| 3606 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0)); |
| 3607 | } |
| 3608 | |
| 3609 | /* End of data: commit the total strings and return. */ |
| 3610 | if (ret < 0) { |
| 3611 | luaL_pushresult(&appctx->b); |
| 3612 | return 1; |
| 3613 | } |
| 3614 | |
| 3615 | /* Ensure that the block 2 length is usable. */ |
| 3616 | if (ret == 1) |
| 3617 | len2 = 0; |
| 3618 | |
| 3619 | if (len == -1) { |
| 3620 | |
| 3621 | /* If len == -1, catenate all the data avalaile and |
| 3622 | * yield because we want to get all the data until |
| 3623 | * the end of data stream. |
| 3624 | */ |
| 3625 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3626 | luaL_addlstring(&appctx->b, blk2, len2); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3627 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3628 | si_applet_cant_get(si); |
| 3629 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0)); |
| 3630 | |
| 3631 | } else { |
| 3632 | |
| 3633 | /* Copy the fisrt block caping to the length required. */ |
| 3634 | if (len1 > len) |
| 3635 | len1 = len; |
| 3636 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3637 | len -= len1; |
| 3638 | |
| 3639 | /* Copy the second block. */ |
| 3640 | if (len2 > len) |
| 3641 | len2 = len; |
| 3642 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3643 | len -= len2; |
| 3644 | |
| 3645 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3646 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3647 | |
| 3648 | /* If we are no other data avalaible, yield waiting for new data. */ |
| 3649 | if (len > 0) { |
| 3650 | lua_pushinteger(L, len); |
| 3651 | lua_replace(L, 2); |
| 3652 | si_applet_cant_get(si); |
| 3653 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0)); |
| 3654 | } |
| 3655 | |
| 3656 | /* return the result. */ |
| 3657 | luaL_pushresult(&appctx->b); |
| 3658 | return 1; |
| 3659 | } |
| 3660 | |
| 3661 | /* we never executes this */ |
| 3662 | hlua_pusherror(L, "Lua: internal error"); |
| 3663 | WILL_LJMP(lua_error(L)); |
| 3664 | return 0; |
| 3665 | } |
| 3666 | |
| 3667 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 3668 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 3669 | { |
| 3670 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3671 | int len = -1; |
| 3672 | |
| 3673 | if (lua_gettop(L) > 2) |
| 3674 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 3675 | if (lua_gettop(L) >= 2) { |
| 3676 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3677 | lua_pop(L, 1); |
| 3678 | } |
| 3679 | |
| 3680 | /* Confirm or set the required length */ |
| 3681 | lua_pushinteger(L, len); |
| 3682 | |
| 3683 | /* Initialise the string catenation. */ |
| 3684 | luaL_buffinit(L, &appctx->b); |
| 3685 | |
| 3686 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 3687 | } |
| 3688 | |
| 3689 | /* Append data in the output side of the buffer. This data is immediatly |
| 3690 | * sent. The fcuntion returns the ammount of data writed. If the buffer |
| 3691 | * cannot contains the data, the function yield. The function returns -1 |
| 3692 | * if the channel is closed. |
| 3693 | */ |
| 3694 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 3695 | { |
| 3696 | size_t len; |
| 3697 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3698 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3699 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3700 | struct stream_interface *si = appctx->appctx->owner; |
| 3701 | struct channel *chn = si_ic(si); |
| 3702 | int max; |
| 3703 | |
| 3704 | /* Get the max amount of data which can write as input in the channel. */ |
| 3705 | max = channel_recv_max(chn); |
| 3706 | if (max > (len - l)) |
| 3707 | max = len - l; |
| 3708 | |
| 3709 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3710 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3711 | |
| 3712 | /* update counters. */ |
| 3713 | l += max; |
| 3714 | lua_pop(L, 1); |
| 3715 | lua_pushinteger(L, l); |
| 3716 | |
| 3717 | /* If some data is not send, declares the situation to the |
| 3718 | * applet, and returns a yield. |
| 3719 | */ |
| 3720 | if (l < len) { |
| 3721 | si_applet_cant_put(si); |
| 3722 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0)); |
| 3723 | } |
| 3724 | |
| 3725 | return 1; |
| 3726 | } |
| 3727 | |
| 3728 | /* Just a wraper of "hlua_applet_tcp_send_yield". This wrapper permits |
| 3729 | * yield the LUA process, and resume it without checking the |
| 3730 | * input arguments. |
| 3731 | */ |
| 3732 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 3733 | { |
| 3734 | MAY_LJMP(check_args(L, 2, "send")); |
| 3735 | lua_pushinteger(L, 0); |
| 3736 | |
| 3737 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 3738 | } |
| 3739 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3740 | /* |
| 3741 | * |
| 3742 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3743 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3744 | * |
| 3745 | * |
| 3746 | */ |
| 3747 | |
| 3748 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3749 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3750 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3751 | __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] | 3752 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3753 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3754 | } |
| 3755 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3756 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3757 | * according with a current TXN. |
| 3758 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3759 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3760 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3761 | struct hlua_appctx *appctx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3762 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3763 | struct stream_interface *si = ctx->owner; |
| 3764 | struct stream *s = si_strm(si); |
| 3765 | struct proxy *px = s->be; |
| 3766 | struct http_txn *txn = s->txn; |
| 3767 | const char *path; |
| 3768 | const char *end; |
| 3769 | const char *p; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3770 | |
| 3771 | /* Check stack size. */ |
| 3772 | if (!lua_checkstack(L, 3)) |
| 3773 | return 0; |
| 3774 | |
| 3775 | /* Create the object: obj[0] = userdata. |
| 3776 | * Note that the base of the Converters object is the |
| 3777 | * same than the TXN object. |
| 3778 | */ |
| 3779 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3780 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3781 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3782 | appctx->appctx = ctx; |
| 3783 | appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 3784 | 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] | 3785 | appctx->htxn.s = s; |
| 3786 | appctx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3787 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3788 | /* Create the "f" field that contains a list of fetches. */ |
| 3789 | lua_pushstring(L, "f"); |
| 3790 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3791 | return 0; |
| 3792 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3793 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3794 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3795 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3796 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3797 | return 0; |
| 3798 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3799 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3800 | /* Create the "c" field that contains a list of converters. */ |
| 3801 | lua_pushstring(L, "c"); |
| 3802 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3803 | return 0; |
| 3804 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3805 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3806 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3807 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3808 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3809 | return 0; |
| 3810 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3811 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3812 | /* Stores the request method. */ |
| 3813 | lua_pushstring(L, "method"); |
| 3814 | lua_pushlstring(L, txn->req.chn->buf->p, txn->req.sl.rq.m_l); |
| 3815 | lua_settable(L, -3); |
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 | /* Stores the http version. */ |
| 3818 | lua_pushstring(L, "version"); |
| 3819 | lua_pushlstring(L, txn->req.chn->buf->p + txn->req.sl.rq.v, txn->req.sl.rq.v_l); |
| 3820 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3821 | |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3822 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 3823 | * the array on the top of the stack. |
| 3824 | */ |
| 3825 | lua_pushstring(L, "headers"); |
| 3826 | htxn.s = s; |
| 3827 | htxn.p = px; |
| 3828 | htxn.dir = SMP_OPT_DIR_REQ; |
| 3829 | if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req)) |
| 3830 | return 0; |
| 3831 | lua_settable(L, -3); |
| 3832 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3833 | /* Get path and qs */ |
| 3834 | path = http_get_path(txn); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3835 | if (path) { |
| 3836 | end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
| 3837 | p = path; |
| 3838 | while (p < end && *p != '?') |
| 3839 | p++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3840 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3841 | /* Stores the request path. */ |
| 3842 | lua_pushstring(L, "path"); |
| 3843 | lua_pushlstring(L, path, p - path); |
| 3844 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3845 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3846 | /* Stores the query string. */ |
| 3847 | lua_pushstring(L, "qs"); |
| 3848 | if (*p == '?') |
| 3849 | p++; |
| 3850 | lua_pushlstring(L, p, end - p); |
| 3851 | lua_settable(L, -3); |
| 3852 | } |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3853 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3854 | /* Stores the request path. */ |
| 3855 | lua_pushstring(L, "length"); |
| 3856 | lua_pushinteger(L, txn->req.body_len); |
| 3857 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3858 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3859 | /* Create an array of HTTP request headers. */ |
| 3860 | lua_pushstring(L, "headers"); |
| 3861 | MAY_LJMP(hlua_http_get_headers(L, &appctx->htxn, &appctx->htxn.s->txn->req)); |
| 3862 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3863 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3864 | /* Create an empty array of HTTP request headers. */ |
| 3865 | lua_pushstring(L, "response"); |
| 3866 | lua_newtable(L); |
| 3867 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3868 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3869 | /* Pop a class stream metatable and affect it to the table. */ |
| 3870 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 3871 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3872 | |
| 3873 | return 1; |
| 3874 | } |
| 3875 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3876 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 3877 | { |
| 3878 | struct hlua_appctx *appctx; |
| 3879 | struct stream *s; |
| 3880 | const char *name; |
| 3881 | size_t len; |
| 3882 | struct sample smp; |
| 3883 | |
| 3884 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 3885 | |
| 3886 | /* It is useles to retrieve the stream, but this function |
| 3887 | * runs only in a stream context. |
| 3888 | */ |
| 3889 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3890 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3891 | s = appctx->htxn.s; |
| 3892 | |
| 3893 | /* Converts the third argument in a sample. */ |
| 3894 | hlua_lua2smp(L, 3, &smp); |
| 3895 | |
| 3896 | /* Store the sample in a variable. */ |
| 3897 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3898 | vars_set_by_name(name, len, &smp); |
| 3899 | return 0; |
| 3900 | } |
| 3901 | |
| 3902 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 3903 | { |
| 3904 | struct hlua_appctx *appctx; |
| 3905 | struct stream *s; |
| 3906 | const char *name; |
| 3907 | size_t len; |
| 3908 | struct sample smp; |
| 3909 | |
| 3910 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3911 | |
| 3912 | /* It is useles to retrieve the stream, but this function |
| 3913 | * runs only in a stream context. |
| 3914 | */ |
| 3915 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3916 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3917 | s = appctx->htxn.s; |
| 3918 | |
| 3919 | /* Unset the variable. */ |
| 3920 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3921 | vars_unset_by_name(name, len, &smp); |
| 3922 | return 0; |
| 3923 | } |
| 3924 | |
| 3925 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 3926 | { |
| 3927 | struct hlua_appctx *appctx; |
| 3928 | struct stream *s; |
| 3929 | const char *name; |
| 3930 | size_t len; |
| 3931 | struct sample smp; |
| 3932 | |
| 3933 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3934 | |
| 3935 | /* It is useles to retrieve the stream, but this function |
| 3936 | * runs only in a stream context. |
| 3937 | */ |
| 3938 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3939 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3940 | s = appctx->htxn.s; |
| 3941 | |
| 3942 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3943 | if (!vars_get_by_name(name, len, &smp)) { |
| 3944 | lua_pushnil(L); |
| 3945 | return 1; |
| 3946 | } |
| 3947 | |
| 3948 | return hlua_smp2lua(L, &smp); |
| 3949 | } |
| 3950 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3951 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 3952 | { |
| 3953 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3954 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3955 | struct hlua *hlua; |
| 3956 | |
| 3957 | /* 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] | 3958 | if (!s->hlua) |
| 3959 | return 0; |
| 3960 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3961 | |
| 3962 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3963 | |
| 3964 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 3965 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3966 | |
| 3967 | /* Get and store new value. */ |
| 3968 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3969 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3970 | |
| 3971 | return 0; |
| 3972 | } |
| 3973 | |
| 3974 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 3975 | { |
| 3976 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3977 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3978 | struct hlua *hlua; |
| 3979 | |
| 3980 | /* 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] | 3981 | if (!s->hlua) { |
| 3982 | lua_pushnil(L); |
| 3983 | return 1; |
| 3984 | } |
| 3985 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3986 | |
| 3987 | /* Push configuration index in the stack. */ |
| 3988 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3989 | |
| 3990 | return 1; |
| 3991 | } |
| 3992 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3993 | /* If expected data not yet available, it returns a yield. This function |
| 3994 | * consumes the data in the buffer. It returns a string containing the |
| 3995 | * data. This string can be empty. |
| 3996 | */ |
| 3997 | __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] | 3998 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3999 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4000 | struct stream_interface *si = appctx->appctx->owner; |
| 4001 | struct channel *chn = si_ic(si); |
| 4002 | int ret; |
| 4003 | char *blk1; |
| 4004 | int len1; |
| 4005 | char *blk2; |
| 4006 | int len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4007 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4008 | /* Maybe we cant send a 100-continue ? */ |
| 4009 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) { |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4010 | ret = ci_putblk(chn, HTTP_100C, strlen(HTTP_100C)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4011 | /* if ret == -2 or -3 the channel closed or the message si too |
| 4012 | * big for the buffers. We cant send anything. So, we ignoring |
| 4013 | * the error, considers that the 100-continue is sent, and try |
| 4014 | * to receive. |
| 4015 | * If ret is -1, we dont have room in the buffer, so we yield. |
| 4016 | */ |
| 4017 | if (ret == -1) { |
| 4018 | si_applet_cant_put(si); |
| 4019 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0)); |
| 4020 | } |
| 4021 | appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C; |
| 4022 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4023 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4024 | /* Check for the end of the data. */ |
| 4025 | if (appctx->appctx->ctx.hlua_apphttp.left_bytes <= 0) { |
| 4026 | luaL_pushresult(&appctx->b); |
| 4027 | return 1; |
| 4028 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4029 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4030 | /* Read the maximum amount of data avalaible. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4031 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4032 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4033 | /* Data not yet avalaible. return yield. */ |
| 4034 | if (ret == 0) { |
| 4035 | si_applet_cant_get(si); |
| 4036 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0)); |
| 4037 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4038 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4039 | /* End of data: commit the total strings and return. */ |
| 4040 | if (ret < 0) { |
| 4041 | luaL_pushresult(&appctx->b); |
| 4042 | return 1; |
| 4043 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4044 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4045 | /* Ensure that the block 2 length is usable. */ |
| 4046 | if (ret == 1) |
| 4047 | len2 = 0; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4048 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4049 | /* Copy the fisrt block caping to the length required. */ |
| 4050 | if (len1 > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4051 | len1 = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4052 | luaL_addlstring(&appctx->b, blk1, len1); |
| 4053 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4054 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4055 | /* Copy the second block. */ |
| 4056 | if (len2 > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4057 | len2 = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4058 | luaL_addlstring(&appctx->b, blk2, len2); |
| 4059 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4060 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4061 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4062 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4063 | luaL_pushresult(&appctx->b); |
| 4064 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4065 | } |
| 4066 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4067 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 4068 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4069 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4070 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4071 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4072 | /* Initialise the string catenation. */ |
| 4073 | luaL_buffinit(L, &appctx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4074 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4075 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4076 | } |
| 4077 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4078 | /* If expected data not yet available, it returns a yield. This function |
| 4079 | * consumes the data in the buffer. It returns a string containing the |
| 4080 | * data. This string can be empty. |
| 4081 | */ |
| 4082 | __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] | 4083 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4084 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4085 | struct stream_interface *si = appctx->appctx->owner; |
| 4086 | int len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4087 | struct channel *chn = si_ic(si); |
| 4088 | int ret; |
| 4089 | char *blk1; |
| 4090 | int len1; |
| 4091 | char *blk2; |
| 4092 | int len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4093 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4094 | /* Maybe we cant send a 100-continue ? */ |
| 4095 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) { |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4096 | ret = ci_putblk(chn, HTTP_100C, strlen(HTTP_100C)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4097 | /* if ret == -2 or -3 the channel closed or the message si too |
| 4098 | * big for the buffers. We cant send anything. So, we ignoring |
| 4099 | * the error, considers that the 100-continue is sent, and try |
| 4100 | * to receive. |
| 4101 | * If ret is -1, we dont have room in the buffer, so we yield. |
| 4102 | */ |
| 4103 | if (ret == -1) { |
| 4104 | si_applet_cant_put(si); |
| 4105 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0)); |
| 4106 | } |
| 4107 | appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C; |
| 4108 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4109 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4110 | /* Read the maximum amount of data avalaible. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4111 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4112 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4113 | /* Data not yet avalaible. return yield. */ |
| 4114 | if (ret == 0) { |
| 4115 | si_applet_cant_get(si); |
| 4116 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0)); |
| 4117 | } |
| 4118 | |
| 4119 | /* End of data: commit the total strings and return. */ |
| 4120 | if (ret < 0) { |
| 4121 | luaL_pushresult(&appctx->b); |
| 4122 | return 1; |
| 4123 | } |
| 4124 | |
| 4125 | /* Ensure that the block 2 length is usable. */ |
| 4126 | if (ret == 1) |
| 4127 | len2 = 0; |
| 4128 | |
| 4129 | /* Copy the fisrt block caping to the length required. */ |
| 4130 | if (len1 > len) |
| 4131 | len1 = len; |
| 4132 | luaL_addlstring(&appctx->b, blk1, len1); |
| 4133 | len -= len1; |
| 4134 | |
| 4135 | /* Copy the second block. */ |
| 4136 | if (len2 > len) |
| 4137 | len2 = len; |
| 4138 | luaL_addlstring(&appctx->b, blk2, len2); |
| 4139 | len -= len2; |
| 4140 | |
| 4141 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4142 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4143 | if (appctx->appctx->ctx.hlua_apphttp.left_bytes != -1) |
| 4144 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len; |
| 4145 | |
| 4146 | /* If we are no other data avalaible, yield waiting for new data. */ |
| 4147 | if (len > 0) { |
| 4148 | lua_pushinteger(L, len); |
| 4149 | lua_replace(L, 2); |
| 4150 | si_applet_cant_get(si); |
| 4151 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0)); |
| 4152 | } |
| 4153 | |
| 4154 | /* return the result. */ |
| 4155 | luaL_pushresult(&appctx->b); |
| 4156 | return 1; |
| 4157 | } |
| 4158 | |
| 4159 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 4160 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 4161 | { |
| 4162 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4163 | int len = -1; |
| 4164 | |
| 4165 | /* Check arguments. */ |
| 4166 | if (lua_gettop(L) > 2) |
| 4167 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4168 | if (lua_gettop(L) >= 2) { |
| 4169 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4170 | lua_pop(L, 1); |
| 4171 | } |
| 4172 | |
| 4173 | /* Check the required length */ |
| 4174 | if (len == -1 || len > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4175 | len = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4176 | lua_pushinteger(L, len); |
| 4177 | |
| 4178 | /* Initialise the string catenation. */ |
| 4179 | luaL_buffinit(L, &appctx->b); |
| 4180 | |
| 4181 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
| 4182 | } |
| 4183 | |
| 4184 | /* Append data in the output side of the buffer. This data is immediatly |
| 4185 | * sent. The fcuntion returns the ammount of data writed. If the buffer |
| 4186 | * cannot contains the data, the function yield. The function returns -1 |
| 4187 | * if the channel is closed. |
| 4188 | */ |
| 4189 | __LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4190 | { |
| 4191 | size_t len; |
| 4192 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4193 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4194 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4195 | struct stream_interface *si = appctx->appctx->owner; |
| 4196 | struct channel *chn = si_ic(si); |
| 4197 | int max; |
| 4198 | |
| 4199 | /* Get the max amount of data which can write as input in the channel. */ |
| 4200 | max = channel_recv_max(chn); |
| 4201 | if (max > (len - l)) |
| 4202 | max = len - l; |
| 4203 | |
| 4204 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4205 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4206 | |
| 4207 | /* update counters. */ |
| 4208 | l += max; |
| 4209 | lua_pop(L, 1); |
| 4210 | lua_pushinteger(L, l); |
| 4211 | |
| 4212 | /* If some data is not send, declares the situation to the |
| 4213 | * applet, and returns a yield. |
| 4214 | */ |
| 4215 | if (l < len) { |
| 4216 | si_applet_cant_put(si); |
| 4217 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0)); |
| 4218 | } |
| 4219 | |
| 4220 | return 1; |
| 4221 | } |
| 4222 | |
| 4223 | /* Just a wraper of "hlua_applet_send_yield". This wrapper permits |
| 4224 | * yield the LUA process, and resume it without checking the |
| 4225 | * input arguments. |
| 4226 | */ |
| 4227 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 4228 | { |
| 4229 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4230 | size_t len; |
| 4231 | char hex[10]; |
| 4232 | |
| 4233 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4234 | |
| 4235 | /* If transfer encoding chunked is selected, we surround the data |
| 4236 | * by chunk data. |
| 4237 | */ |
| 4238 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED) { |
| 4239 | snprintf(hex, 9, "%x", (unsigned int)len); |
| 4240 | lua_pushfstring(L, "%s\r\n", hex); |
| 4241 | lua_insert(L, 2); /* swap the last 2 entries. */ |
| 4242 | lua_pushstring(L, "\r\n"); |
| 4243 | lua_concat(L, 3); |
| 4244 | } |
| 4245 | |
| 4246 | /* This interger is used for followinf the amount of data sent. */ |
| 4247 | lua_pushinteger(L, 0); |
| 4248 | |
| 4249 | /* We want to send some data. Headers must be sent. */ |
| 4250 | if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 4251 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 4252 | WILL_LJMP(lua_error(L)); |
| 4253 | } |
| 4254 | |
| 4255 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
| 4256 | } |
| 4257 | |
| 4258 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 4259 | { |
| 4260 | const char *name; |
| 4261 | int ret; |
| 4262 | |
| 4263 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4264 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4265 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 4266 | |
| 4267 | /* Push in the stack the "response" entry. */ |
| 4268 | ret = lua_getfield(L, 1, "response"); |
| 4269 | if (ret != LUA_TTABLE) { |
| 4270 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 4271 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 4272 | WILL_LJMP(lua_error(L)); |
| 4273 | } |
| 4274 | |
| 4275 | /* check if the header is already registered if it is not |
| 4276 | * the case, register it. |
| 4277 | */ |
| 4278 | ret = lua_getfield(L, -1, name); |
| 4279 | if (ret == LUA_TNIL) { |
| 4280 | |
| 4281 | /* Entry not found. */ |
| 4282 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 4283 | |
| 4284 | /* Insert the new header name in the array in the top of the stack. |
| 4285 | * It left the new array in the top of the stack. |
| 4286 | */ |
| 4287 | lua_newtable(L); |
| 4288 | lua_pushvalue(L, 2); |
| 4289 | lua_pushvalue(L, -2); |
| 4290 | lua_settable(L, -4); |
| 4291 | |
| 4292 | } else if (ret != LUA_TTABLE) { |
| 4293 | |
| 4294 | /* corruption error. */ |
| 4295 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 4296 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 4297 | WILL_LJMP(lua_error(L)); |
| 4298 | } |
| 4299 | |
| 4300 | /* Now the top od thestack is an array of values. We push |
| 4301 | * the header value as new entry. |
| 4302 | */ |
| 4303 | lua_pushvalue(L, 3); |
| 4304 | ret = lua_rawlen(L, -2); |
| 4305 | lua_rawseti(L, -2, ret + 1); |
| 4306 | lua_pushboolean(L, 1); |
| 4307 | return 1; |
| 4308 | } |
| 4309 | |
| 4310 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 4311 | { |
| 4312 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4313 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4314 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4315 | |
| 4316 | if (status < 100 || status > 599) { |
| 4317 | lua_pushboolean(L, 0); |
| 4318 | return 1; |
| 4319 | } |
| 4320 | |
| 4321 | appctx->appctx->ctx.hlua_apphttp.status = status; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4322 | appctx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4323 | lua_pushboolean(L, 1); |
| 4324 | return 1; |
| 4325 | } |
| 4326 | |
| 4327 | /* We will build the status line and the headers of the HTTP response. |
| 4328 | * We will try send at once if its not possible, we give back the hand |
| 4329 | * waiting for more room. |
| 4330 | */ |
| 4331 | __LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx) |
| 4332 | { |
| 4333 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4334 | struct stream_interface *si = appctx->appctx->owner; |
| 4335 | struct channel *chn = si_ic(si); |
| 4336 | int ret; |
| 4337 | size_t len; |
| 4338 | const char *msg; |
| 4339 | |
| 4340 | /* Get the message as the first argument on the stack. */ |
| 4341 | msg = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4342 | |
| 4343 | /* Send the message at once. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4344 | ret = ci_putblk(chn, msg, len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4345 | |
| 4346 | /* if ret == -2 or -3 the channel closed or the message si too |
| 4347 | * big for the buffers. |
| 4348 | */ |
| 4349 | if (ret == -2 || ret == -3) { |
| 4350 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 4351 | WILL_LJMP(lua_error(L)); |
| 4352 | } |
| 4353 | |
| 4354 | /* If ret is -1, we dont have room in the buffer, so we yield. */ |
| 4355 | if (ret == -1) { |
| 4356 | si_applet_cant_put(si); |
| 4357 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0)); |
| 4358 | } |
| 4359 | |
| 4360 | /* Headers sent, set the flag. */ |
| 4361 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
| 4362 | return 0; |
| 4363 | } |
| 4364 | |
| 4365 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
| 4366 | { |
| 4367 | struct chunk *tmp = get_trash_chunk(); |
| 4368 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4369 | const char *name; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4370 | size_t name_len; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4371 | const char *value; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4372 | size_t value_len; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4373 | int id; |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 4374 | long long hdr_contentlength = -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4375 | int hdr_chunked = 0; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4376 | const char *reason = appctx->appctx->ctx.hlua_apphttp.reason; |
| 4377 | |
| 4378 | if (reason == NULL) |
| 4379 | reason = get_reason(appctx->appctx->ctx.hlua_apphttp.status); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4380 | |
| 4381 | /* Use the same http version than the request. */ |
| 4382 | chunk_appendf(tmp, "HTTP/1.%c %d %s\r\n", |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 4383 | appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11 ? '1' : '0', |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4384 | appctx->appctx->ctx.hlua_apphttp.status, |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4385 | reason); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4386 | |
| 4387 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 4388 | lua_pushvalue(L, 0); |
| 4389 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 4390 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
| 4391 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4392 | WILL_LJMP(lua_error(L)); |
| 4393 | } |
| 4394 | |
| 4395 | /* Browse the list of headers. */ |
| 4396 | lua_pushnil(L); |
| 4397 | while(lua_next(L, -2) != 0) { |
| 4398 | |
| 4399 | /* We expect a string as -2. */ |
| 4400 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 4401 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
| 4402 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4403 | lua_typename(L, lua_type(L, -2))); |
| 4404 | WILL_LJMP(lua_error(L)); |
| 4405 | } |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4406 | name = lua_tolstring(L, -2, &name_len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4407 | |
| 4408 | /* We expect an array as -1. */ |
| 4409 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 4410 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
| 4411 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4412 | name, |
| 4413 | lua_typename(L, lua_type(L, -1))); |
| 4414 | WILL_LJMP(lua_error(L)); |
| 4415 | } |
| 4416 | |
| 4417 | /* Browse the table who is on the top of the stack. */ |
| 4418 | lua_pushnil(L); |
| 4419 | while(lua_next(L, -2) != 0) { |
| 4420 | |
| 4421 | /* We expect a number as -2. */ |
| 4422 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 4423 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
| 4424 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4425 | name, |
| 4426 | lua_typename(L, lua_type(L, -2))); |
| 4427 | WILL_LJMP(lua_error(L)); |
| 4428 | } |
| 4429 | id = lua_tointeger(L, -2); |
| 4430 | |
| 4431 | /* We expect a string as -2. */ |
| 4432 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 4433 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
| 4434 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4435 | name, id, |
| 4436 | lua_typename(L, lua_type(L, -1))); |
| 4437 | WILL_LJMP(lua_error(L)); |
| 4438 | } |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4439 | value = lua_tolstring(L, -1, &value_len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4440 | |
| 4441 | /* Catenate a new header. */ |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4442 | if (tmp->len + name_len + 2 + value_len + 2 < tmp->size) { |
| 4443 | memcpy(tmp->str + tmp->len, name, name_len); |
| 4444 | tmp->len += name_len; |
| 4445 | tmp->str[tmp->len++] = ':'; |
| 4446 | tmp->str[tmp->len++] = ' '; |
| 4447 | |
| 4448 | memcpy(tmp->str + tmp->len, value, value_len); |
| 4449 | tmp->len += value_len; |
| 4450 | tmp->str[tmp->len++] = '\r'; |
| 4451 | tmp->str[tmp->len++] = '\n'; |
| 4452 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4453 | |
| 4454 | /* Protocol checks. */ |
| 4455 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4456 | /* Copy the header content length. The length conversion |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 4457 | * is done without control. If it contains a bad value, |
| 4458 | * the content-length remains negative so that we can |
| 4459 | * switch to either chunked encoding or close. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4460 | */ |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4461 | if (name_len == 14 && strcasecmp("content-length", name) == 0) |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 4462 | strl2llrc(value, strlen(value), &hdr_contentlength); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4463 | |
| 4464 | /* Check if the client annouces a transfer-encoding chunked it self. */ |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4465 | if (name_len == 17 && value_len == 7 && |
| 4466 | strcasecmp("transfer-encoding", name) == 0 && |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4467 | strcasecmp("chunked", value) == 0) |
| 4468 | hdr_chunked = 1; |
| 4469 | |
| 4470 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4471 | lua_pop(L, 1); |
| 4472 | } |
| 4473 | |
| 4474 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4475 | lua_pop(L, 1); |
| 4476 | } |
| 4477 | |
Willy Tarreau | 06c75fe | 2017-08-23 09:10:38 +0200 | [diff] [blame] | 4478 | /* If we dont have a content-length set, and the HTTP version is 1.1 |
| 4479 | * and the status code implies the presence of a message body, we must |
| 4480 | * announce a transfer encoding chunked. This is required by haproxy |
| 4481 | * for the keepalive compliance. If the applet annouces a transfer-encoding |
| 4482 | * chunked itslef, don't do anything. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4483 | */ |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 4484 | if (hdr_contentlength < 0 && hdr_chunked == 0 && |
Willy Tarreau | 06c75fe | 2017-08-23 09:10:38 +0200 | [diff] [blame] | 4485 | (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) && |
| 4486 | appctx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 4487 | appctx->appctx->ctx.hlua_apphttp.status != 204 && |
| 4488 | appctx->appctx->ctx.hlua_apphttp.status != 304) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4489 | chunk_appendf(tmp, "Transfer-encoding: chunked\r\n"); |
| 4490 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_CHUNKED; |
| 4491 | } |
| 4492 | |
| 4493 | /* Finalize headers. */ |
| 4494 | chunk_appendf(tmp, "\r\n"); |
| 4495 | |
| 4496 | /* Remove the last entry and the array of headers */ |
| 4497 | lua_pop(L, 2); |
| 4498 | |
| 4499 | /* Push the headers block. */ |
| 4500 | lua_pushlstring(L, tmp->str, tmp->len); |
| 4501 | |
| 4502 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
| 4503 | } |
| 4504 | |
| 4505 | /* |
| 4506 | * |
| 4507 | * |
| 4508 | * Class HTTP |
| 4509 | * |
| 4510 | * |
| 4511 | */ |
| 4512 | |
| 4513 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4514 | * a class stream, otherwise it throws an error. |
| 4515 | */ |
| 4516 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
| 4517 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4518 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4519 | } |
| 4520 | |
| 4521 | /* This function creates and push in the stack a HTTP object |
| 4522 | * according with a current TXN. |
| 4523 | */ |
| 4524 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 4525 | { |
| 4526 | struct hlua_txn *htxn; |
| 4527 | |
| 4528 | /* Check stack size. */ |
| 4529 | if (!lua_checkstack(L, 3)) |
| 4530 | return 0; |
| 4531 | |
| 4532 | /* Create the object: obj[0] = userdata. |
| 4533 | * Note that the base of the Converters object is the |
| 4534 | * same than the TXN object. |
| 4535 | */ |
| 4536 | lua_newtable(L); |
| 4537 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 4538 | lua_rawseti(L, -2, 0); |
| 4539 | |
| 4540 | htxn->s = txn->s; |
| 4541 | htxn->p = txn->p; |
| 4542 | |
| 4543 | /* Pop a class stream metatable and affect it to the table. */ |
| 4544 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 4545 | lua_setmetatable(L, -2); |
| 4546 | |
| 4547 | return 1; |
| 4548 | } |
| 4549 | |
| 4550 | /* This function creates ans returns an array of HTTP headers. |
| 4551 | * This function does not fails. It is used as wrapper with the |
| 4552 | * 2 following functions. |
| 4553 | */ |
| 4554 | __LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4555 | { |
| 4556 | const char *cur_ptr, *cur_next, *p; |
| 4557 | int old_idx, cur_idx; |
| 4558 | struct hdr_idx_elem *cur_hdr; |
| 4559 | const char *hn, *hv; |
| 4560 | int hnl, hvl; |
| 4561 | int type; |
| 4562 | const char *in; |
| 4563 | char *out; |
| 4564 | int len; |
| 4565 | |
| 4566 | /* Create the table. */ |
| 4567 | lua_newtable(L); |
| 4568 | |
| 4569 | if (!htxn->s->txn) |
| 4570 | return 1; |
| 4571 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4572 | /* Check if a valid response is parsed */ |
| 4573 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4574 | return 1; |
| 4575 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4576 | /* Build array of headers. */ |
| 4577 | old_idx = 0; |
| 4578 | cur_next = msg->chn->buf->p + hdr_idx_first_pos(&htxn->s->txn->hdr_idx); |
| 4579 | |
| 4580 | while (1) { |
| 4581 | cur_idx = htxn->s->txn->hdr_idx.v[old_idx].next; |
| 4582 | if (!cur_idx) |
| 4583 | break; |
| 4584 | old_idx = cur_idx; |
| 4585 | |
| 4586 | cur_hdr = &htxn->s->txn->hdr_idx.v[cur_idx]; |
| 4587 | cur_ptr = cur_next; |
| 4588 | cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1; |
| 4589 | |
| 4590 | /* Now we have one full header at cur_ptr of len cur_hdr->len, |
| 4591 | * and the next header starts at cur_next. We'll check |
| 4592 | * this header in the list as well as against the default |
| 4593 | * rule. |
| 4594 | */ |
| 4595 | |
| 4596 | /* look for ': *'. */ |
| 4597 | hn = cur_ptr; |
| 4598 | for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++); |
| 4599 | if (p >= cur_ptr+cur_hdr->len) |
| 4600 | continue; |
| 4601 | hnl = p - hn; |
| 4602 | p++; |
| 4603 | while (p < cur_ptr+cur_hdr->len && ( *p == ' ' || *p == '\t' )) |
| 4604 | p++; |
| 4605 | if (p >= cur_ptr+cur_hdr->len) |
| 4606 | continue; |
| 4607 | hv = p; |
| 4608 | hvl = cur_ptr+cur_hdr->len-p; |
| 4609 | |
| 4610 | /* Lowercase the key. Don't check the size of trash, it have |
| 4611 | * the size of one buffer and the input data contains in one |
| 4612 | * buffer. |
| 4613 | */ |
| 4614 | out = trash.str; |
| 4615 | for (in=hn; in<hn+hnl; in++, out++) |
| 4616 | *out = tolower(*in); |
| 4617 | *out = '\0'; |
| 4618 | |
| 4619 | /* Check for existing entry: |
| 4620 | * assume that the table is on the top of the stack, and |
| 4621 | * push the key in the stack, the function lua_gettable() |
| 4622 | * perform the lookup. |
| 4623 | */ |
| 4624 | lua_pushlstring(L, trash.str, hnl); |
| 4625 | lua_gettable(L, -2); |
| 4626 | type = lua_type(L, -1); |
| 4627 | |
| 4628 | switch (type) { |
| 4629 | case LUA_TNIL: |
| 4630 | /* Table not found, create it. */ |
| 4631 | lua_pop(L, 1); /* remove the nil value. */ |
| 4632 | lua_pushlstring(L, trash.str, hnl); /* push the header name as key. */ |
| 4633 | lua_newtable(L); /* create and push empty table. */ |
| 4634 | lua_pushlstring(L, hv, hvl); /* push header value. */ |
| 4635 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 4636 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
| 4637 | break; |
| 4638 | |
| 4639 | case LUA_TTABLE: |
| 4640 | /* Entry found: push the value in the table. */ |
| 4641 | len = lua_rawlen(L, -1); |
| 4642 | lua_pushlstring(L, hv, hvl); /* push header value. */ |
| 4643 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 4644 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 4645 | break; |
| 4646 | |
| 4647 | default: |
| 4648 | /* Other cases are errors. */ |
| 4649 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 4650 | WILL_LJMP(lua_error(L)); |
| 4651 | } |
| 4652 | } |
| 4653 | |
| 4654 | return 1; |
| 4655 | } |
| 4656 | |
| 4657 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 4658 | { |
| 4659 | struct hlua_txn *htxn; |
| 4660 | |
| 4661 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 4662 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4663 | |
| 4664 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->req); |
| 4665 | } |
| 4666 | |
| 4667 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 4668 | { |
| 4669 | struct hlua_txn *htxn; |
| 4670 | |
| 4671 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 4672 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4673 | |
| 4674 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp); |
| 4675 | } |
| 4676 | |
| 4677 | /* This function replace full header, or just a value in |
| 4678 | * the request or in the response. It is a wrapper fir the |
| 4679 | * 4 following functions. |
| 4680 | */ |
| 4681 | __LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn, |
| 4682 | struct http_msg *msg, int action) |
| 4683 | { |
| 4684 | size_t name_len; |
| 4685 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4686 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 4687 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
| 4688 | struct my_regex re; |
| 4689 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4690 | /* Check if a valid response is parsed */ |
| 4691 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4692 | return 0; |
| 4693 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4694 | if (!regex_comp(reg, &re, 1, 1, NULL)) |
| 4695 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 4696 | |
| 4697 | http_transform_header_str(htxn->s, msg, name, name_len, value, &re, action); |
| 4698 | regex_free(&re); |
| 4699 | return 0; |
| 4700 | } |
| 4701 | |
| 4702 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 4703 | { |
| 4704 | struct hlua_txn *htxn; |
| 4705 | |
| 4706 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4707 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4708 | |
| 4709 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR)); |
| 4710 | } |
| 4711 | |
| 4712 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 4713 | { |
| 4714 | struct hlua_txn *htxn; |
| 4715 | |
| 4716 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 4717 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4718 | |
| 4719 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR)); |
| 4720 | } |
| 4721 | |
| 4722 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 4723 | { |
| 4724 | struct hlua_txn *htxn; |
| 4725 | |
| 4726 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4727 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4728 | |
| 4729 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL)); |
| 4730 | } |
| 4731 | |
| 4732 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 4733 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4734 | struct hlua_txn *htxn; |
| 4735 | |
| 4736 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 4737 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4738 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 4739 | 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] | 4740 | } |
| 4741 | |
| 4742 | /* This function deletes all the occurences of an header. |
| 4743 | * It is a wrapper for the 2 following functions. |
| 4744 | */ |
| 4745 | __LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4746 | { |
| 4747 | size_t len; |
| 4748 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4749 | struct hdr_ctx ctx; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4750 | struct http_txn *txn = htxn->s->txn; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4751 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4752 | /* Check if a valid response is parsed */ |
| 4753 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4754 | return 0; |
| 4755 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4756 | ctx.idx = 0; |
| 4757 | while (http_find_header2(name, len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) |
| 4758 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 4759 | return 0; |
| 4760 | } |
| 4761 | |
| 4762 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 4763 | { |
| 4764 | struct hlua_txn *htxn; |
| 4765 | |
| 4766 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 4767 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4768 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4769 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4770 | } |
| 4771 | |
| 4772 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 4773 | { |
| 4774 | struct hlua_txn *htxn; |
| 4775 | |
| 4776 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 4777 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4778 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4779 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4780 | } |
| 4781 | |
| 4782 | /* This function adds an header. It is a wrapper used by |
| 4783 | * the 2 following functions. |
| 4784 | */ |
| 4785 | __LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4786 | { |
| 4787 | size_t name_len; |
| 4788 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4789 | size_t value_len; |
| 4790 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
| 4791 | char *p; |
| 4792 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4793 | /* Check if a valid message is parsed */ |
| 4794 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4795 | return 0; |
| 4796 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4797 | /* Check length. */ |
| 4798 | trash.len = value_len + name_len + 2; |
| 4799 | if (trash.len > trash.size) |
| 4800 | return 0; |
| 4801 | |
| 4802 | /* Creates the header string. */ |
| 4803 | p = trash.str; |
| 4804 | memcpy(p, name, name_len); |
| 4805 | p += name_len; |
| 4806 | *p = ':'; |
| 4807 | p++; |
| 4808 | *p = ' '; |
| 4809 | p++; |
| 4810 | memcpy(p, value, value_len); |
| 4811 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4812 | 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] | 4813 | trash.str, trash.len) != 0); |
| 4814 | |
| 4815 | return 0; |
| 4816 | } |
| 4817 | |
| 4818 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 4819 | { |
| 4820 | struct hlua_txn *htxn; |
| 4821 | |
| 4822 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 4823 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4824 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4825 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4826 | } |
| 4827 | |
| 4828 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 4829 | { |
| 4830 | struct hlua_txn *htxn; |
| 4831 | |
| 4832 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 4833 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4834 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4835 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4836 | } |
| 4837 | |
| 4838 | static int hlua_http_req_set_hdr(lua_State *L) |
| 4839 | { |
| 4840 | struct hlua_txn *htxn; |
| 4841 | |
| 4842 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 4843 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4844 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4845 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
| 4846 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4847 | } |
| 4848 | |
| 4849 | static int hlua_http_res_set_hdr(lua_State *L) |
| 4850 | { |
| 4851 | struct hlua_txn *htxn; |
| 4852 | |
| 4853 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 4854 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4855 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4856 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
| 4857 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4858 | } |
| 4859 | |
| 4860 | /* This function set the method. */ |
| 4861 | static int hlua_http_req_set_meth(lua_State *L) |
| 4862 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4863 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4864 | size_t name_len; |
| 4865 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4866 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4867 | /* Check if a valid request is parsed */ |
| 4868 | if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 4869 | lua_pushboolean(L, 0); |
| 4870 | return 1; |
| 4871 | } |
| 4872 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4873 | 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] | 4874 | return 1; |
| 4875 | } |
| 4876 | |
| 4877 | /* This function set the method. */ |
| 4878 | static int hlua_http_req_set_path(lua_State *L) |
| 4879 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4880 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4881 | size_t name_len; |
| 4882 | 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] | 4883 | |
| 4884 | /* Check if a valid request is parsed */ |
| 4885 | if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 4886 | lua_pushboolean(L, 0); |
| 4887 | return 1; |
| 4888 | } |
| 4889 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4890 | 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] | 4891 | return 1; |
| 4892 | } |
| 4893 | |
| 4894 | /* This function set the query-string. */ |
| 4895 | static int hlua_http_req_set_query(lua_State *L) |
| 4896 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4897 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4898 | size_t name_len; |
| 4899 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4900 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4901 | /* Check if a valid request is parsed */ |
| 4902 | if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 4903 | lua_pushboolean(L, 0); |
| 4904 | return 1; |
| 4905 | } |
| 4906 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4907 | /* Check length. */ |
| 4908 | if (name_len > trash.size - 1) { |
| 4909 | lua_pushboolean(L, 0); |
| 4910 | return 1; |
| 4911 | } |
| 4912 | |
| 4913 | /* Add the mark question as prefix. */ |
| 4914 | chunk_reset(&trash); |
| 4915 | trash.str[trash.len++] = '?'; |
| 4916 | memcpy(trash.str + trash.len, name, name_len); |
| 4917 | trash.len += name_len; |
| 4918 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4919 | 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] | 4920 | return 1; |
| 4921 | } |
| 4922 | |
| 4923 | /* This function set the uri. */ |
| 4924 | static int hlua_http_req_set_uri(lua_State *L) |
| 4925 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4926 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4927 | size_t name_len; |
| 4928 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4929 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4930 | /* Check if a valid request is parsed */ |
| 4931 | if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 4932 | lua_pushboolean(L, 0); |
| 4933 | return 1; |
| 4934 | } |
| 4935 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4936 | 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] | 4937 | return 1; |
| 4938 | } |
| 4939 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4940 | /* This function set the response code & optionally reason. */ |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 4941 | static int hlua_http_res_set_status(lua_State *L) |
| 4942 | { |
| 4943 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4944 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4945 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 4946 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4947 | /* Check if a valid response is parsed */ |
| 4948 | if (unlikely(htxn->s->txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 4949 | return 0; |
| 4950 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4951 | http_set_status(code, reason, htxn->s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 4952 | return 0; |
| 4953 | } |
| 4954 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4955 | /* |
| 4956 | * |
| 4957 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4958 | * Class TXN |
| 4959 | * |
| 4960 | * |
| 4961 | */ |
| 4962 | |
| 4963 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4964 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4965 | */ |
| 4966 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 4967 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4968 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4969 | } |
| 4970 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 4971 | __LJMP static int hlua_set_var(lua_State *L) |
| 4972 | { |
| 4973 | struct hlua_txn *htxn; |
| 4974 | const char *name; |
| 4975 | size_t len; |
| 4976 | struct sample smp; |
| 4977 | |
| 4978 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 4979 | |
| 4980 | /* It is useles to retrieve the stream, but this function |
| 4981 | * runs only in a stream context. |
| 4982 | */ |
| 4983 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 4984 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4985 | |
| 4986 | /* Converts the third argument in a sample. */ |
| 4987 | hlua_lua2smp(L, 3, &smp); |
| 4988 | |
| 4989 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 4990 | 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] | 4991 | vars_set_by_name(name, len, &smp); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 4992 | return 0; |
| 4993 | } |
| 4994 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 4995 | __LJMP static int hlua_unset_var(lua_State *L) |
| 4996 | { |
| 4997 | struct hlua_txn *htxn; |
| 4998 | const char *name; |
| 4999 | size_t len; |
| 5000 | struct sample smp; |
| 5001 | |
| 5002 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 5003 | |
| 5004 | /* It is useles to retrieve the stream, but this function |
| 5005 | * runs only in a stream context. |
| 5006 | */ |
| 5007 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5008 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5009 | |
| 5010 | /* Unset the variable. */ |
| 5011 | smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR); |
| 5012 | vars_unset_by_name(name, len, &smp); |
| 5013 | return 0; |
| 5014 | } |
| 5015 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5016 | __LJMP static int hlua_get_var(lua_State *L) |
| 5017 | { |
| 5018 | struct hlua_txn *htxn; |
| 5019 | const char *name; |
| 5020 | size_t len; |
| 5021 | struct sample smp; |
| 5022 | |
| 5023 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 5024 | |
| 5025 | /* It is useles to retrieve the stream, but this function |
| 5026 | * runs only in a stream context. |
| 5027 | */ |
| 5028 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5029 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5030 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5031 | 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] | 5032 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5033 | lua_pushnil(L); |
| 5034 | return 1; |
| 5035 | } |
| 5036 | |
| 5037 | return hlua_smp2lua(L, &smp); |
| 5038 | } |
| 5039 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5040 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5041 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5042 | struct hlua *hlua; |
| 5043 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5044 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5045 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5046 | /* It is useles to retrieve the stream, but this function |
| 5047 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5048 | */ |
| 5049 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5050 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5051 | |
| 5052 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5053 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5054 | |
| 5055 | /* Get and store new value. */ |
| 5056 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5057 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5058 | |
| 5059 | return 0; |
| 5060 | } |
| 5061 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5062 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5063 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5064 | struct hlua *hlua; |
| 5065 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5066 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 5067 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5068 | /* It is useles to retrieve the stream, but this function |
| 5069 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5070 | */ |
| 5071 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5072 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5073 | |
| 5074 | /* Push configuration index in the stack. */ |
| 5075 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5076 | |
| 5077 | return 1; |
| 5078 | } |
| 5079 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5080 | /* Create stack entry containing a class TXN. This function |
| 5081 | * return 0 if the stack does not contains free slots, |
| 5082 | * otherwise it returns 1. |
| 5083 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5084 | 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] | 5085 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5086 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5087 | |
| 5088 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5089 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5090 | return 0; |
| 5091 | |
| 5092 | /* NOTE: The allocation never fails. The failure |
| 5093 | * throw an error, and the function never returns. |
| 5094 | * if the throw is not avalaible, the process is aborted. |
| 5095 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5096 | /* Create the object: obj[0] = userdata. */ |
| 5097 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5098 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5099 | lua_rawseti(L, -2, 0); |
| 5100 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5101 | htxn->s = s; |
| 5102 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 5103 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5104 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5105 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5106 | /* Create the "f" field that contains a list of fetches. */ |
| 5107 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5108 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5109 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5110 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5111 | |
| 5112 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 5113 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5114 | 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] | 5115 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5116 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5117 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5118 | /* Create the "c" field that contains a list of converters. */ |
| 5119 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5120 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5121 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5122 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5123 | |
| 5124 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 5125 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 5126 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5127 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5128 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5129 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5130 | /* Create the "req" field that contains the request channel object. */ |
| 5131 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5132 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5133 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5134 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5135 | |
| 5136 | /* Create the "res" field that contains the response channel object. */ |
| 5137 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5138 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5139 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5140 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5141 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5142 | /* Creates the HTTP object is the current proxy allows http. */ |
| 5143 | lua_pushstring(L, "http"); |
| 5144 | if (p->mode == PR_MODE_HTTP) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5145 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5146 | return 0; |
| 5147 | } |
| 5148 | else |
| 5149 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5150 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5151 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5152 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5153 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 5154 | lua_setmetatable(L, -2); |
| 5155 | |
| 5156 | return 1; |
| 5157 | } |
| 5158 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5159 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 5160 | { |
| 5161 | const char *msg; |
| 5162 | struct hlua_txn *htxn; |
| 5163 | |
| 5164 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 5165 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5166 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5167 | |
| 5168 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 5169 | return 0; |
| 5170 | } |
| 5171 | |
| 5172 | __LJMP static int hlua_txn_log(lua_State *L) |
| 5173 | { |
| 5174 | int level; |
| 5175 | const char *msg; |
| 5176 | struct hlua_txn *htxn; |
| 5177 | |
| 5178 | MAY_LJMP(check_args(L, 3, "log")); |
| 5179 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5180 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5181 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5182 | |
| 5183 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5184 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5185 | |
| 5186 | hlua_sendlog(htxn->s->be, level, msg); |
| 5187 | return 0; |
| 5188 | } |
| 5189 | |
| 5190 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 5191 | { |
| 5192 | const char *msg; |
| 5193 | struct hlua_txn *htxn; |
| 5194 | |
| 5195 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 5196 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5197 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5198 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 5199 | return 0; |
| 5200 | } |
| 5201 | |
| 5202 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 5203 | { |
| 5204 | const char *msg; |
| 5205 | struct hlua_txn *htxn; |
| 5206 | |
| 5207 | MAY_LJMP(check_args(L, 2, "Info")); |
| 5208 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5209 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5210 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 5211 | return 0; |
| 5212 | } |
| 5213 | |
| 5214 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 5215 | { |
| 5216 | const char *msg; |
| 5217 | struct hlua_txn *htxn; |
| 5218 | |
| 5219 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 5220 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5221 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5222 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 5223 | return 0; |
| 5224 | } |
| 5225 | |
| 5226 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 5227 | { |
| 5228 | const char *msg; |
| 5229 | struct hlua_txn *htxn; |
| 5230 | |
| 5231 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 5232 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5233 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5234 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 5235 | return 0; |
| 5236 | } |
| 5237 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5238 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 5239 | { |
| 5240 | struct hlua_txn *htxn; |
| 5241 | int ll; |
| 5242 | |
| 5243 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 5244 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5245 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5246 | |
| 5247 | if (ll < 0 || ll > 7) |
| 5248 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 5249 | |
| 5250 | htxn->s->logs.level = ll; |
| 5251 | return 0; |
| 5252 | } |
| 5253 | |
| 5254 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 5255 | { |
| 5256 | struct hlua_txn *htxn; |
| 5257 | struct connection *cli_conn; |
| 5258 | int tos; |
| 5259 | |
| 5260 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 5261 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5262 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5263 | |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 5264 | if ((cli_conn = objt_conn(htxn->s->sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 5265 | inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5266 | |
| 5267 | return 0; |
| 5268 | } |
| 5269 | |
| 5270 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 5271 | { |
| 5272 | #ifdef SO_MARK |
| 5273 | struct hlua_txn *htxn; |
| 5274 | struct connection *cli_conn; |
| 5275 | int mark; |
| 5276 | |
| 5277 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 5278 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5279 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5280 | |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 5281 | if ((cli_conn = objt_conn(htxn->s->sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 5282 | setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5283 | #endif |
| 5284 | return 0; |
| 5285 | } |
| 5286 | |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5287 | /* This function is an Lua binding that send pending data |
| 5288 | * to the client, and close the stream interface. |
| 5289 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5290 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5291 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5292 | struct hlua_txn *htxn; |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 5293 | struct hlua *hlua; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5294 | struct channel *ic, *oc; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5295 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5296 | MAY_LJMP(check_args(L, 1, "close")); |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5297 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 5298 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5299 | |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5300 | /* If the flags NOTERM is set, we cannot terminate the http |
| 5301 | * session, so we just end the execution of the current |
| 5302 | * lua code. |
| 5303 | */ |
| 5304 | if (htxn->flags & HLUA_TXN_NOTERM) { |
| 5305 | WILL_LJMP(hlua_done(L)); |
| 5306 | return 0; |
| 5307 | } |
| 5308 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5309 | ic = &htxn->s->req; |
| 5310 | oc = &htxn->s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5311 | |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 5312 | if (htxn->s->txn) { |
| 5313 | /* HTTP mode, let's stay in sync with the stream */ |
| 5314 | bi_fast_delete(ic->buf, htxn->s->txn->req.sov); |
| 5315 | htxn->s->txn->req.next -= htxn->s->txn->req.sov; |
| 5316 | htxn->s->txn->req.sov = 0; |
| 5317 | ic->analysers &= AN_REQ_HTTP_XFER_BODY; |
| 5318 | oc->analysers = AN_RES_HTTP_XFER_BODY; |
| 5319 | htxn->s->txn->req.msg_state = HTTP_MSG_CLOSED; |
| 5320 | htxn->s->txn->rsp.msg_state = HTTP_MSG_DONE; |
| 5321 | |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 5322 | /* Note that if we want to support keep-alive, we need |
| 5323 | * to bypass the close/shutr_now calls below, but that |
| 5324 | * may only be done if the HTTP request was already |
| 5325 | * processed and the connection header is known (ie |
| 5326 | * not during TCP rules). |
| 5327 | */ |
| 5328 | } |
| 5329 | |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 5330 | channel_auto_read(ic); |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5331 | channel_abort(ic); |
| 5332 | channel_auto_close(ic); |
| 5333 | channel_erase(ic); |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 5334 | |
| 5335 | oc->wex = tick_add_ifset(now_ms, oc->wto); |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5336 | channel_auto_read(oc); |
| 5337 | channel_auto_close(oc); |
| 5338 | channel_shutr_now(oc); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5339 | |
Willy Tarreau | 0458b08 | 2015-08-28 09:40:04 +0200 | [diff] [blame] | 5340 | ic->analysers = 0; |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5341 | |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 5342 | hlua->flags |= HLUA_STOP; |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5343 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5344 | return 0; |
| 5345 | } |
| 5346 | |
| 5347 | __LJMP static int hlua_log(lua_State *L) |
| 5348 | { |
| 5349 | int level; |
| 5350 | const char *msg; |
| 5351 | |
| 5352 | MAY_LJMP(check_args(L, 2, "log")); |
| 5353 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 5354 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5355 | |
| 5356 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5357 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5358 | |
| 5359 | hlua_sendlog(NULL, level, msg); |
| 5360 | return 0; |
| 5361 | } |
| 5362 | |
| 5363 | __LJMP static int hlua_log_debug(lua_State *L) |
| 5364 | { |
| 5365 | const char *msg; |
| 5366 | |
| 5367 | MAY_LJMP(check_args(L, 1, "debug")); |
| 5368 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5369 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 5370 | return 0; |
| 5371 | } |
| 5372 | |
| 5373 | __LJMP static int hlua_log_info(lua_State *L) |
| 5374 | { |
| 5375 | const char *msg; |
| 5376 | |
| 5377 | MAY_LJMP(check_args(L, 1, "info")); |
| 5378 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5379 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 5380 | return 0; |
| 5381 | } |
| 5382 | |
| 5383 | __LJMP static int hlua_log_warning(lua_State *L) |
| 5384 | { |
| 5385 | const char *msg; |
| 5386 | |
| 5387 | MAY_LJMP(check_args(L, 1, "warning")); |
| 5388 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5389 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 5390 | return 0; |
| 5391 | } |
| 5392 | |
| 5393 | __LJMP static int hlua_log_alert(lua_State *L) |
| 5394 | { |
| 5395 | const char *msg; |
| 5396 | |
| 5397 | MAY_LJMP(check_args(L, 1, "alert")); |
| 5398 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5399 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5400 | return 0; |
| 5401 | } |
| 5402 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5403 | __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] | 5404 | { |
| 5405 | int wakeup_ms = lua_tointeger(L, -1); |
| 5406 | if (now_ms < wakeup_ms) |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5407 | 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] | 5408 | return 0; |
| 5409 | } |
| 5410 | |
| 5411 | __LJMP static int hlua_sleep(lua_State *L) |
| 5412 | { |
| 5413 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5414 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5415 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5416 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5417 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5418 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5419 | wakeup_ms = tick_add(now_ms, delay); |
| 5420 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5421 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5422 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0)); |
| 5423 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5424 | } |
| 5425 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5426 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5427 | { |
| 5428 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5429 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5430 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5431 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5432 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5433 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5434 | wakeup_ms = tick_add(now_ms, delay); |
| 5435 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5436 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5437 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0)); |
| 5438 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5439 | } |
| 5440 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5441 | /* This functionis an LUA binding. it permits to give back |
| 5442 | * the hand at the HAProxy scheduler. It is used when the |
| 5443 | * LUA processing consumes a lot of time. |
| 5444 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5445 | __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] | 5446 | { |
| 5447 | return 0; |
| 5448 | } |
| 5449 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5450 | __LJMP static int hlua_yield(lua_State *L) |
| 5451 | { |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5452 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD)); |
| 5453 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5454 | } |
| 5455 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5456 | /* This function change the nice of the currently executed |
| 5457 | * task. It is used set low or high priority at the current |
| 5458 | * task. |
| 5459 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5460 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5461 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5462 | struct hlua *hlua; |
| 5463 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5464 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5465 | MAY_LJMP(check_args(L, 1, "set_nice")); |
| 5466 | hlua = hlua_gethlua(L); |
| 5467 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5468 | |
| 5469 | /* If he task is not set, I'm in a start mode. */ |
| 5470 | if (!hlua || !hlua->task) |
| 5471 | return 0; |
| 5472 | |
| 5473 | if (nice < -1024) |
| 5474 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5475 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5476 | nice = 1024; |
| 5477 | |
| 5478 | hlua->task->nice = nice; |
| 5479 | return 0; |
| 5480 | } |
| 5481 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5482 | /* This function is used as a calback of a task. It is called by the |
| 5483 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 5484 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 5485 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5486 | * |
| 5487 | * Task wrapper are longjmp safe because the only one Lua code |
| 5488 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5489 | */ |
| 5490 | static struct task *hlua_process_task(struct task *task) |
| 5491 | { |
| 5492 | struct hlua *hlua = task->context; |
| 5493 | enum hlua_exec status; |
| 5494 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5495 | /* If it is the first call to the task, we must initialize the |
| 5496 | * execution timeouts. |
| 5497 | */ |
| 5498 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 5499 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5500 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5501 | /* Execute the Lua code. */ |
| 5502 | status = hlua_ctx_resume(hlua, 1); |
| 5503 | |
| 5504 | switch (status) { |
| 5505 | /* finished or yield */ |
| 5506 | case HLUA_E_OK: |
| 5507 | hlua_ctx_destroy(hlua); |
| 5508 | task_delete(task); |
| 5509 | task_free(task); |
| 5510 | break; |
| 5511 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 5512 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
| 5513 | if (hlua->wake_time != TICK_ETERNITY) |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 5514 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5515 | break; |
| 5516 | |
| 5517 | /* finished with error. */ |
| 5518 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5519 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5520 | hlua_ctx_destroy(hlua); |
| 5521 | task_delete(task); |
| 5522 | task_free(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 5523 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5524 | break; |
| 5525 | |
| 5526 | case HLUA_E_ERR: |
| 5527 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5528 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5529 | hlua_ctx_destroy(hlua); |
| 5530 | task_delete(task); |
| 5531 | task_free(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 5532 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5533 | break; |
| 5534 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 5535 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5536 | } |
| 5537 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 5538 | /* This function is an LUA binding that register LUA function to be |
| 5539 | * executed after the HAProxy configuration parsing and before the |
| 5540 | * HAProxy scheduler starts. This function expect only one LUA |
| 5541 | * argument that is a function. This function returns nothing, but |
| 5542 | * throws if an error is encountered. |
| 5543 | */ |
| 5544 | __LJMP static int hlua_register_init(lua_State *L) |
| 5545 | { |
| 5546 | struct hlua_init_function *init; |
| 5547 | int ref; |
| 5548 | |
| 5549 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 5550 | |
| 5551 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 5552 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5553 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 5554 | if (!init) |
| 5555 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5556 | |
| 5557 | init->function_ref = ref; |
| 5558 | LIST_ADDQ(&hlua_init_functions, &init->l); |
| 5559 | return 0; |
| 5560 | } |
| 5561 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5562 | /* This functio is an LUA binding. It permits to register a task |
| 5563 | * executed in parallel of the main HAroxy activity. The task is |
| 5564 | * created and it is set in the HAProxy scheduler. It can be called |
| 5565 | * from the "init" section, "post init" or during the runtime. |
| 5566 | * |
| 5567 | * Lua prototype: |
| 5568 | * |
| 5569 | * <none> core.register_task(<function>) |
| 5570 | */ |
| 5571 | static int hlua_register_task(lua_State *L) |
| 5572 | { |
| 5573 | struct hlua *hlua; |
| 5574 | struct task *task; |
| 5575 | int ref; |
| 5576 | |
| 5577 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 5578 | |
| 5579 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 5580 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5581 | hlua = pool_alloc2(pool2_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5582 | if (!hlua) |
| 5583 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5584 | |
Emeric Brun | c60def8 | 2017-09-27 14:59:38 +0200 | [diff] [blame] | 5585 | task = task_new(MAX_THREADS_MASK); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5586 | task->context = hlua; |
| 5587 | task->process = hlua_process_task; |
| 5588 | |
| 5589 | if (!hlua_ctx_init(hlua, task)) |
| 5590 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5591 | |
| 5592 | /* Restore the function in the stack. */ |
| 5593 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 5594 | hlua->nargs = 0; |
| 5595 | |
| 5596 | /* Schedule task. */ |
| 5597 | task_schedule(task, now_ms); |
| 5598 | |
| 5599 | return 0; |
| 5600 | } |
| 5601 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5602 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 5603 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 5604 | * resume converters. |
| 5605 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5606 | 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] | 5607 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5608 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5609 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5610 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5611 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5612 | if (!stream) |
| 5613 | return 0; |
| 5614 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5615 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5616 | * Lua context can be not initialized. This behavior |
| 5617 | * permits to save performances because a systematic |
| 5618 | * Lua initialization cause 5% performances loss. |
| 5619 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5620 | if (!stream->hlua) { |
| 5621 | stream->hlua = pool_alloc2(pool2_hlua); |
| 5622 | if (!stream->hlua) { |
| 5623 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 5624 | return 0; |
| 5625 | } |
| 5626 | if (!hlua_ctx_init(stream->hlua, stream->task)) { |
| 5627 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 5628 | return 0; |
| 5629 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5630 | } |
| 5631 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5632 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5633 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5634 | |
| 5635 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5636 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 5637 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 5638 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5639 | else |
| 5640 | error = "critical error"; |
| 5641 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5642 | return 0; |
| 5643 | } |
| 5644 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5645 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5646 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5647 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5648 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5649 | return 0; |
| 5650 | } |
| 5651 | |
| 5652 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5653 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5654 | |
| 5655 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5656 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5657 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5658 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5659 | return 0; |
| 5660 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5661 | hlua_smp2lua(stream->hlua->T, smp); |
| 5662 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5663 | |
| 5664 | /* push keywords in the stack. */ |
| 5665 | if (arg_p) { |
| 5666 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5667 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5668 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5669 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5670 | return 0; |
| 5671 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5672 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 5673 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5674 | } |
| 5675 | } |
| 5676 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5677 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5678 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5679 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5680 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5681 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5682 | } |
| 5683 | |
| 5684 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5685 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5686 | /* finished. */ |
| 5687 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 5688 | /* If the stack is empty, the function fails. */ |
| 5689 | if (lua_gettop(stream->hlua->T) <= 0) |
| 5690 | return 0; |
| 5691 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5692 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5693 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 5694 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5695 | return 1; |
| 5696 | |
| 5697 | /* yield. */ |
| 5698 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5699 | 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] | 5700 | return 0; |
| 5701 | |
| 5702 | /* finished with error. */ |
| 5703 | case HLUA_E_ERRMSG: |
| 5704 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5705 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5706 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 5707 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5708 | return 0; |
| 5709 | |
| 5710 | case HLUA_E_ERR: |
| 5711 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5712 | 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] | 5713 | |
| 5714 | default: |
| 5715 | return 0; |
| 5716 | } |
| 5717 | } |
| 5718 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5719 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 5720 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5721 | * resume sample-fetches. This function will be called by the sample |
| 5722 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5723 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5724 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 5725 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5726 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5727 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5728 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5729 | const char *error; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5730 | const struct chunk msg = { .len = 0 }; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5731 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5732 | if (!stream) |
| 5733 | return 0; |
| 5734 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5735 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5736 | * Lua context can be not initialized. This behavior |
| 5737 | * permits to save performances because a systematic |
| 5738 | * Lua initialization cause 5% performances loss. |
| 5739 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5740 | if (!stream->hlua) { |
| 5741 | stream->hlua = pool_alloc2(pool2_hlua); |
| 5742 | if (!stream->hlua) { |
| 5743 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 5744 | return 0; |
| 5745 | } |
| 5746 | if (!hlua_ctx_init(stream->hlua, stream->task)) { |
| 5747 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 5748 | return 0; |
| 5749 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5750 | } |
| 5751 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5752 | consistency_set(stream, smp->opt, &stream->hlua->cons); |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5753 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5754 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5755 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5756 | |
| 5757 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5758 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 5759 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 5760 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5761 | else |
| 5762 | error = "critical error"; |
| 5763 | 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] | 5764 | return 0; |
| 5765 | } |
| 5766 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5767 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5768 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5769 | 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] | 5770 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5771 | return 0; |
| 5772 | } |
| 5773 | |
| 5774 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5775 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5776 | |
| 5777 | /* push arguments in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5778 | 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] | 5779 | HLUA_TXN_NOTERM)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5780 | 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] | 5781 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5782 | return 0; |
| 5783 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5784 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5785 | |
| 5786 | /* push keywords in the stack. */ |
| 5787 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 5788 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5789 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5790 | 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] | 5791 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5792 | return 0; |
| 5793 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5794 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 5795 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5796 | } |
| 5797 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5798 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5799 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5800 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5801 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5802 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5803 | } |
| 5804 | |
| 5805 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5806 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5807 | /* finished. */ |
| 5808 | case HLUA_E_OK: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5809 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5810 | stream_int_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 5811 | return 0; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5812 | } |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 5813 | /* If the stack is empty, the function fails. */ |
| 5814 | if (lua_gettop(stream->hlua->T) <= 0) |
| 5815 | return 0; |
| 5816 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5817 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5818 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 5819 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5820 | |
| 5821 | /* Set the end of execution flag. */ |
| 5822 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 5823 | return 1; |
| 5824 | |
| 5825 | /* yield. */ |
| 5826 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5827 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5828 | stream_int_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5829 | 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] | 5830 | return 0; |
| 5831 | |
| 5832 | /* finished with error. */ |
| 5833 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5834 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5835 | stream_int_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5836 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5837 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5838 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 5839 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5840 | return 0; |
| 5841 | |
| 5842 | case HLUA_E_ERR: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5843 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5844 | stream_int_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5845 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5846 | 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] | 5847 | |
| 5848 | default: |
| 5849 | return 0; |
| 5850 | } |
| 5851 | } |
| 5852 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5853 | /* This function is an LUA binding used for registering |
| 5854 | * "sample-conv" functions. It expects a converter name used |
| 5855 | * in the haproxy configuration file, and an LUA function. |
| 5856 | */ |
| 5857 | __LJMP static int hlua_register_converters(lua_State *L) |
| 5858 | { |
| 5859 | struct sample_conv_kw_list *sck; |
| 5860 | const char *name; |
| 5861 | int ref; |
| 5862 | int len; |
| 5863 | struct hlua_function *fcn; |
| 5864 | |
| 5865 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 5866 | |
| 5867 | /* First argument : converter name. */ |
| 5868 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5869 | |
| 5870 | /* Second argument : lua function. */ |
| 5871 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 5872 | |
| 5873 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5874 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5875 | if (!sck) |
| 5876 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5877 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5878 | if (!fcn) |
| 5879 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5880 | |
| 5881 | /* Fill fcn. */ |
| 5882 | fcn->name = strdup(name); |
| 5883 | if (!fcn->name) |
| 5884 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5885 | fcn->function_ref = ref; |
| 5886 | |
| 5887 | /* List head */ |
| 5888 | sck->list.n = sck->list.p = NULL; |
| 5889 | |
| 5890 | /* converter keyword. */ |
| 5891 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5892 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5893 | if (!sck->kw[0].kw) |
| 5894 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5895 | |
| 5896 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 5897 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 5898 | 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] | 5899 | sck->kw[0].val_args = NULL; |
| 5900 | sck->kw[0].in_type = SMP_T_STR; |
| 5901 | sck->kw[0].out_type = SMP_T_STR; |
| 5902 | sck->kw[0].private = fcn; |
| 5903 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5904 | /* Register this new converter */ |
| 5905 | sample_register_convs(sck); |
| 5906 | |
| 5907 | return 0; |
| 5908 | } |
| 5909 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5910 | /* This fucntion is an LUA binding used for registering |
| 5911 | * "sample-fetch" functions. It expects a converter name used |
| 5912 | * in the haproxy configuration file, and an LUA function. |
| 5913 | */ |
| 5914 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 5915 | { |
| 5916 | const char *name; |
| 5917 | int ref; |
| 5918 | int len; |
| 5919 | struct sample_fetch_kw_list *sfk; |
| 5920 | struct hlua_function *fcn; |
| 5921 | |
| 5922 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 5923 | |
| 5924 | /* First argument : sample-fetch name. */ |
| 5925 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5926 | |
| 5927 | /* Second argument : lua function. */ |
| 5928 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 5929 | |
| 5930 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5931 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5932 | if (!sfk) |
| 5933 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5934 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5935 | if (!fcn) |
| 5936 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5937 | |
| 5938 | /* Fill fcn. */ |
| 5939 | fcn->name = strdup(name); |
| 5940 | if (!fcn->name) |
| 5941 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5942 | fcn->function_ref = ref; |
| 5943 | |
| 5944 | /* List head */ |
| 5945 | sfk->list.n = sfk->list.p = NULL; |
| 5946 | |
| 5947 | /* sample-fetch keyword. */ |
| 5948 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5949 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5950 | if (!sfk->kw[0].kw) |
| 5951 | return luaL_error(L, "lua out of memory error."); |
| 5952 | |
| 5953 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 5954 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 5955 | 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] | 5956 | sfk->kw[0].val_args = NULL; |
| 5957 | sfk->kw[0].out_type = SMP_T_STR; |
| 5958 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 5959 | sfk->kw[0].val = 0; |
| 5960 | sfk->kw[0].private = fcn; |
| 5961 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5962 | /* Register this new fetch. */ |
| 5963 | sample_register_fetches(sfk); |
| 5964 | |
| 5965 | return 0; |
| 5966 | } |
| 5967 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5968 | /* This function is a wrapper to execute each LUA function declared |
| 5969 | * as an action wrapper during the initialisation period. This function |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5970 | * return ACT_RET_CONT if the processing is finished (with or without |
| 5971 | * error) and return ACT_RET_YIELD if the function must be called again |
| 5972 | * because the LUA returns a yield. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5973 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5974 | 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] | 5975 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5976 | { |
| 5977 | char **arg; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5978 | unsigned int analyzer; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 5979 | int dir; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5980 | const char *error; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5981 | const struct chunk msg = { .len = 0 }; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5982 | |
| 5983 | switch (rule->from) { |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 5984 | case ACT_F_TCP_REQ_CNT: analyzer = AN_REQ_INSPECT_FE ; dir = SMP_OPT_DIR_REQ; break; |
| 5985 | case ACT_F_TCP_RES_CNT: analyzer = AN_RES_INSPECT ; dir = SMP_OPT_DIR_RES; break; |
| 5986 | case ACT_F_HTTP_REQ: analyzer = AN_REQ_HTTP_PROCESS_FE; dir = SMP_OPT_DIR_REQ; break; |
| 5987 | 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] | 5988 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5989 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5990 | return ACT_RET_CONT; |
| 5991 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5992 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5993 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5994 | * Lua context can be not initialized. This behavior |
| 5995 | * permits to save performances because a systematic |
| 5996 | * Lua initialization cause 5% performances loss. |
| 5997 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5998 | if (!s->hlua) { |
| 5999 | s->hlua = pool_alloc2(pool2_hlua); |
| 6000 | if (!s->hlua) { |
| 6001 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6002 | rule->arg.hlua_rule->fcn.name); |
| 6003 | return ACT_RET_CONT; |
| 6004 | } |
| 6005 | if (!hlua_ctx_init(s->hlua, s->task)) { |
| 6006 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6007 | rule->arg.hlua_rule->fcn.name); |
| 6008 | return ACT_RET_CONT; |
| 6009 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6010 | } |
| 6011 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6012 | consistency_set(s, dir, &s->hlua->cons); |
| 6013 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6014 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6015 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6016 | |
| 6017 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6018 | if (!SET_SAFE_LJMP(s->hlua->T)) { |
| 6019 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 6020 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6021 | else |
| 6022 | error = "critical error"; |
| 6023 | SEND_ERR(px, "Lua function '%s': %s.\n", |
| 6024 | rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6025 | return ACT_RET_CONT; |
| 6026 | } |
| 6027 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6028 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6029 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6030 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6031 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6032 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6033 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6034 | } |
| 6035 | |
| 6036 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6037 | 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] | 6038 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6039 | /* Create and and push object stream in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6040 | if (!hlua_txn_new(s->hlua->T, s, px, dir, 0)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6041 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6042 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6043 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6044 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6045 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6046 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6047 | |
| 6048 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6049 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6050 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6051 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6052 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6053 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6054 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6055 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6056 | lua_pushstring(s->hlua->T, *arg); |
| 6057 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6058 | } |
| 6059 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6060 | /* Now the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6061 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6062 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6063 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6064 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6065 | } |
| 6066 | |
| 6067 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6068 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_FLAG_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6069 | /* finished. */ |
| 6070 | case HLUA_E_OK: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6071 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6072 | stream_int_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6073 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6074 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6075 | if (s->hlua->flags & HLUA_STOP) |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 6076 | return ACT_RET_STOP; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6077 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6078 | |
| 6079 | /* yield. */ |
| 6080 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6081 | /* Set timeout in the required channel. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6082 | if (s->hlua->wake_time != TICK_ETERNITY) { |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6083 | if (analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE)) |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6084 | s->req.analyse_exp = s->hlua->wake_time; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6085 | else if (analyzer & (AN_RES_INSPECT|AN_RES_HTTP_PROCESS_BE)) |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6086 | s->res.analyse_exp = s->hlua->wake_time; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6087 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6088 | /* Some actions can be wake up when a "write" event |
| 6089 | * is detected on a response channel. This is useful |
| 6090 | * only for actions targetted on the requests. |
| 6091 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6092 | if (HLUA_IS_WAKERESWR(s->hlua)) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6093 | s->res.flags |= CF_WAKE_WRITE; |
Willy Tarreau | 76bd97f | 2015-03-10 17:16:10 +0100 | [diff] [blame] | 6094 | if ((analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE))) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6095 | s->res.analysers |= analyzer; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6096 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6097 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6098 | s->req.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6099 | /* We can quit the fcuntion without consistency check |
| 6100 | * because HAProxy is not able to manipulate data, it |
| 6101 | * is only allowed to call me again. */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6102 | return ACT_RET_YIELD; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6103 | |
| 6104 | /* finished with error. */ |
| 6105 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6106 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6107 | stream_int_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6108 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6109 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6110 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6111 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6112 | rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1)); |
| 6113 | lua_pop(s->hlua->T, 1); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6114 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6115 | |
| 6116 | case HLUA_E_ERR: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6117 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6118 | stream_int_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6119 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6120 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6121 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6122 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6123 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6124 | |
| 6125 | default: |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6126 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6127 | } |
| 6128 | } |
| 6129 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6130 | struct task *hlua_applet_wakeup(struct task *t) |
| 6131 | { |
| 6132 | struct appctx *ctx = t->context; |
| 6133 | struct stream_interface *si = ctx->owner; |
| 6134 | |
| 6135 | /* If the applet is wake up without any expected work, the sheduler |
| 6136 | * remove it from the run queue. This flag indicate that the applet |
| 6137 | * is waiting for write. If the buffer is full, the main processing |
| 6138 | * will send some data and after call the applet, otherwise it call |
| 6139 | * the applet ASAP. |
| 6140 | */ |
| 6141 | si_applet_cant_put(si); |
| 6142 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 6143 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 6144 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6145 | } |
| 6146 | |
| 6147 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6148 | { |
| 6149 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6150 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6151 | struct task *task; |
| 6152 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6153 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6154 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6155 | hlua = pool_alloc2(pool2_hlua); |
| 6156 | if (!hlua) { |
| 6157 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6158 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6159 | return 0; |
| 6160 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6161 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6162 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6163 | ctx->ctx.hlua_apptcp.flags = 0; |
| 6164 | |
| 6165 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 6166 | task = task_new(tid_bit); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6167 | if (!task) { |
| 6168 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6169 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6170 | return 0; |
| 6171 | } |
| 6172 | task->nice = 0; |
| 6173 | task->context = ctx; |
| 6174 | task->process = hlua_applet_wakeup; |
| 6175 | ctx->ctx.hlua_apptcp.task = task; |
| 6176 | |
| 6177 | /* In the execution wrappers linked with a stream, the |
| 6178 | * Lua context can be not initialized. This behavior |
| 6179 | * permits to save performances because a systematic |
| 6180 | * Lua initialization cause 5% performances loss. |
| 6181 | */ |
| 6182 | if (!hlua_ctx_init(hlua, task)) { |
| 6183 | SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n", |
| 6184 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6185 | return 0; |
| 6186 | } |
| 6187 | |
| 6188 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6189 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6190 | |
| 6191 | /* The following Lua calls can fail. */ |
| 6192 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6193 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6194 | error = lua_tostring(hlua->T, -1); |
| 6195 | else |
| 6196 | error = "critical error"; |
| 6197 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 6198 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6199 | RESET_SAFE_LJMP(hlua->T); |
| 6200 | return 0; |
| 6201 | } |
| 6202 | |
| 6203 | /* Check stack available size. */ |
| 6204 | if (!lua_checkstack(hlua->T, 1)) { |
| 6205 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6206 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6207 | RESET_SAFE_LJMP(hlua->T); |
| 6208 | return 0; |
| 6209 | } |
| 6210 | |
| 6211 | /* Restore the function in the stack. */ |
| 6212 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 6213 | |
| 6214 | /* Create and and push object stream in the stack. */ |
| 6215 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 6216 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6217 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6218 | RESET_SAFE_LJMP(hlua->T); |
| 6219 | return 0; |
| 6220 | } |
| 6221 | hlua->nargs = 1; |
| 6222 | |
| 6223 | /* push keywords in the stack. */ |
| 6224 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 6225 | if (!lua_checkstack(hlua->T, 1)) { |
| 6226 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6227 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6228 | RESET_SAFE_LJMP(hlua->T); |
| 6229 | return 0; |
| 6230 | } |
| 6231 | lua_pushstring(hlua->T, *arg); |
| 6232 | hlua->nargs++; |
| 6233 | } |
| 6234 | |
| 6235 | RESET_SAFE_LJMP(hlua->T); |
| 6236 | |
| 6237 | /* Wakeup the applet ASAP. */ |
| 6238 | si_applet_cant_get(si); |
| 6239 | si_applet_cant_put(si); |
| 6240 | |
| 6241 | return 1; |
| 6242 | } |
| 6243 | |
| 6244 | static void hlua_applet_tcp_fct(struct appctx *ctx) |
| 6245 | { |
| 6246 | struct stream_interface *si = ctx->owner; |
| 6247 | struct stream *strm = si_strm(si); |
| 6248 | struct channel *res = si_ic(si); |
| 6249 | struct act_rule *rule = ctx->rule; |
| 6250 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6251 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6252 | |
| 6253 | /* The applet execution is already done. */ |
| 6254 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) |
| 6255 | return; |
| 6256 | |
| 6257 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 6258 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 6259 | return; |
| 6260 | |
| 6261 | /* Execute the function. */ |
| 6262 | switch (hlua_ctx_resume(hlua, 1)) { |
| 6263 | /* finished. */ |
| 6264 | case HLUA_E_OK: |
| 6265 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 6266 | |
| 6267 | /* log time */ |
| 6268 | strm->logs.tv_request = now; |
| 6269 | |
| 6270 | /* eat the whole request */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6271 | co_skip(si_oc(si), si_ob(si)->o); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6272 | res->flags |= CF_READ_NULL; |
| 6273 | si_shutr(si); |
| 6274 | return; |
| 6275 | |
| 6276 | /* yield. */ |
| 6277 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 6278 | if (hlua->wake_time != TICK_ETERNITY) |
| 6279 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6280 | return; |
| 6281 | |
| 6282 | /* finished with error. */ |
| 6283 | case HLUA_E_ERRMSG: |
| 6284 | /* Display log. */ |
| 6285 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 6286 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 6287 | lua_pop(hlua->T, 1); |
| 6288 | goto error; |
| 6289 | |
| 6290 | case HLUA_E_ERR: |
| 6291 | /* Display log. */ |
| 6292 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
| 6293 | rule->arg.hlua_rule->fcn.name); |
| 6294 | goto error; |
| 6295 | |
| 6296 | default: |
| 6297 | goto error; |
| 6298 | } |
| 6299 | |
| 6300 | error: |
| 6301 | |
| 6302 | /* For all other cases, just close the stream. */ |
| 6303 | si_shutw(si); |
| 6304 | si_shutr(si); |
| 6305 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 6306 | } |
| 6307 | |
| 6308 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 6309 | { |
Willy Tarreau | bd7fc95 | 2017-07-24 17:35:27 +0200 | [diff] [blame] | 6310 | task_delete(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6311 | task_free(ctx->ctx.hlua_apptcp.task); |
| 6312 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6313 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6314 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6315 | } |
| 6316 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6317 | /* The function returns 1 if the initialisation is complete, 0 if |
| 6318 | * an errors occurs and -1 if more data are required for initializing |
| 6319 | * the applet. |
| 6320 | */ |
| 6321 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6322 | { |
| 6323 | struct stream_interface *si = ctx->owner; |
| 6324 | struct channel *req = si_oc(si); |
| 6325 | struct http_msg *msg; |
| 6326 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6327 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6328 | char **arg; |
| 6329 | struct hdr_ctx hdr; |
| 6330 | struct task *task; |
| 6331 | 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] | 6332 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6333 | |
| 6334 | /* Wait for a full HTTP request. */ |
| 6335 | if (!smp_prefetch_http(px, strm, 0, NULL, &smp, 0)) { |
| 6336 | if (smp.flags & SMP_F_MAY_CHANGE) |
| 6337 | return -1; |
| 6338 | return 0; |
| 6339 | } |
| 6340 | txn = strm->txn; |
| 6341 | msg = &txn->req; |
| 6342 | |
Willy Tarreau | 0078bfc | 2015-10-07 20:20:28 +0200 | [diff] [blame] | 6343 | /* We want two things in HTTP mode : |
| 6344 | * - enforce server-close mode if we were in keep-alive, so that the |
| 6345 | * applet is released after each response ; |
| 6346 | * - enable request body transfer to the applet in order to resync |
| 6347 | * with the response body. |
| 6348 | */ |
| 6349 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) |
| 6350 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | 0078bfc | 2015-10-07 20:20:28 +0200 | [diff] [blame] | 6351 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6352 | hlua = pool_alloc2(pool2_hlua); |
| 6353 | if (!hlua) { |
| 6354 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 6355 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6356 | return 0; |
| 6357 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6358 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6359 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6360 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 6361 | ctx->ctx.hlua_apphttp.flags = 0; |
| 6362 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 6363 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 6364 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 6365 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6366 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 6367 | task = task_new(tid_bit); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6368 | if (!task) { |
| 6369 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 6370 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6371 | return 0; |
| 6372 | } |
| 6373 | task->nice = 0; |
| 6374 | task->context = ctx; |
| 6375 | task->process = hlua_applet_wakeup; |
| 6376 | ctx->ctx.hlua_apphttp.task = task; |
| 6377 | |
| 6378 | /* In the execution wrappers linked with a stream, the |
| 6379 | * Lua context can be not initialized. This behavior |
| 6380 | * permits to save performances because a systematic |
| 6381 | * Lua initialization cause 5% performances loss. |
| 6382 | */ |
| 6383 | if (!hlua_ctx_init(hlua, task)) { |
| 6384 | SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n", |
| 6385 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6386 | return 0; |
| 6387 | } |
| 6388 | |
| 6389 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6390 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6391 | |
| 6392 | /* The following Lua calls can fail. */ |
| 6393 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6394 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6395 | error = lua_tostring(hlua->T, -1); |
| 6396 | else |
| 6397 | error = "critical error"; |
| 6398 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 6399 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6400 | return 0; |
| 6401 | } |
| 6402 | |
| 6403 | /* Check stack available size. */ |
| 6404 | if (!lua_checkstack(hlua->T, 1)) { |
| 6405 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6406 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6407 | RESET_SAFE_LJMP(hlua->T); |
| 6408 | return 0; |
| 6409 | } |
| 6410 | |
| 6411 | /* Restore the function in the stack. */ |
| 6412 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 6413 | |
| 6414 | /* Create and and push object stream in the stack. */ |
| 6415 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 6416 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6417 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6418 | RESET_SAFE_LJMP(hlua->T); |
| 6419 | return 0; |
| 6420 | } |
| 6421 | hlua->nargs = 1; |
| 6422 | |
| 6423 | /* Look for a 100-continue expected. */ |
| 6424 | if (msg->flags & HTTP_MSGF_VER_11) { |
| 6425 | hdr.idx = 0; |
| 6426 | if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &hdr) && |
| 6427 | unlikely(hdr.vlen == 12 && strncasecmp(hdr.line+hdr.val, "100-continue", 12) == 0)) |
| 6428 | ctx->ctx.hlua_apphttp.flags |= APPLET_100C; |
| 6429 | } |
| 6430 | |
| 6431 | /* push keywords in the stack. */ |
| 6432 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 6433 | if (!lua_checkstack(hlua->T, 1)) { |
| 6434 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6435 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6436 | RESET_SAFE_LJMP(hlua->T); |
| 6437 | return 0; |
| 6438 | } |
| 6439 | lua_pushstring(hlua->T, *arg); |
| 6440 | hlua->nargs++; |
| 6441 | } |
| 6442 | |
| 6443 | RESET_SAFE_LJMP(hlua->T); |
| 6444 | |
| 6445 | /* Wakeup the applet when data is ready for read. */ |
| 6446 | si_applet_cant_get(si); |
| 6447 | |
| 6448 | return 1; |
| 6449 | } |
| 6450 | |
| 6451 | static void hlua_applet_http_fct(struct appctx *ctx) |
| 6452 | { |
| 6453 | struct stream_interface *si = ctx->owner; |
| 6454 | struct stream *strm = si_strm(si); |
| 6455 | struct channel *res = si_ic(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6456 | struct act_rule *rule = ctx->rule; |
| 6457 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6458 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6459 | char *blk1; |
| 6460 | int len1; |
| 6461 | char *blk2; |
| 6462 | int len2; |
| 6463 | int ret; |
| 6464 | |
| 6465 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 6466 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 6467 | return; |
| 6468 | |
| 6469 | /* Set the currently running flag. */ |
| 6470 | if (!HLUA_IS_RUNNING(hlua) && |
| 6471 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 6472 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6473 | /* Wait for full HTTP analysys. */ |
| 6474 | if (unlikely(strm->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 6475 | si_applet_cant_get(si); |
| 6476 | return; |
| 6477 | } |
| 6478 | |
| 6479 | /* Store the max amount of bytes that we can read. */ |
| 6480 | ctx->ctx.hlua_apphttp.left_bytes = strm->txn->req.body_len; |
| 6481 | |
| 6482 | /* We need to flush the request header. This left the body |
| 6483 | * for the Lua. |
| 6484 | */ |
| 6485 | |
| 6486 | /* Read the maximum amount of data avalaible. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6487 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6488 | if (ret == -1) |
| 6489 | return; |
| 6490 | |
| 6491 | /* No data available, ask for more data. */ |
| 6492 | if (ret == 1) |
| 6493 | len2 = 0; |
| 6494 | if (ret == 0) |
| 6495 | len1 = 0; |
| 6496 | if (len1 + len2 < strm->txn->req.eoh + 2) { |
| 6497 | si_applet_cant_get(si); |
| 6498 | return; |
| 6499 | } |
| 6500 | |
| 6501 | /* skip the requests bytes. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6502 | co_skip(si_oc(si), strm->txn->req.eoh + 2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6503 | } |
| 6504 | |
| 6505 | /* Executes The applet if it is not done. */ |
| 6506 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 6507 | |
| 6508 | /* Execute the function. */ |
| 6509 | switch (hlua_ctx_resume(hlua, 1)) { |
| 6510 | /* finished. */ |
| 6511 | case HLUA_E_OK: |
| 6512 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 6513 | break; |
| 6514 | |
| 6515 | /* yield. */ |
| 6516 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 6517 | if (hlua->wake_time != TICK_ETERNITY) |
| 6518 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6519 | return; |
| 6520 | |
| 6521 | /* finished with error. */ |
| 6522 | case HLUA_E_ERRMSG: |
| 6523 | /* Display log. */ |
| 6524 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 6525 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 6526 | lua_pop(hlua->T, 1); |
| 6527 | goto error; |
| 6528 | |
| 6529 | case HLUA_E_ERR: |
| 6530 | /* Display log. */ |
| 6531 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
| 6532 | rule->arg.hlua_rule->fcn.name); |
| 6533 | goto error; |
| 6534 | |
| 6535 | default: |
| 6536 | goto error; |
| 6537 | } |
| 6538 | } |
| 6539 | |
| 6540 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
| 6541 | |
| 6542 | /* We must send the final chunk. */ |
| 6543 | if (ctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED && |
| 6544 | !(ctx->ctx.hlua_apphttp.flags & APPLET_LAST_CHK)) { |
| 6545 | |
| 6546 | /* sent last chunk at once. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6547 | ret = ci_putblk(res, "0\r\n\r\n", 5); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6548 | |
| 6549 | /* critical error. */ |
| 6550 | if (ret == -2 || ret == -3) { |
| 6551 | SEND_ERR(px, "Lua applet http '%s'cannont send last chunk.\n", |
| 6552 | rule->arg.hlua_rule->fcn.name); |
| 6553 | goto error; |
| 6554 | } |
| 6555 | |
| 6556 | /* no enough space error. */ |
| 6557 | if (ret == -1) { |
| 6558 | si_applet_cant_put(si); |
| 6559 | return; |
| 6560 | } |
| 6561 | |
| 6562 | /* set the last chunk sent. */ |
| 6563 | ctx->ctx.hlua_apphttp.flags |= APPLET_LAST_CHK; |
| 6564 | } |
| 6565 | |
| 6566 | /* close the connection. */ |
| 6567 | |
| 6568 | /* status / log */ |
| 6569 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 6570 | strm->logs.tv_request = now; |
| 6571 | |
| 6572 | /* eat the whole request */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6573 | co_skip(si_oc(si), si_ob(si)->o); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6574 | res->flags |= CF_READ_NULL; |
| 6575 | si_shutr(si); |
| 6576 | |
| 6577 | return; |
| 6578 | } |
| 6579 | |
| 6580 | error: |
| 6581 | |
| 6582 | /* If we are in HTTP mode, and we are not send any |
| 6583 | * data, return a 500 server error in best effort: |
| 6584 | * if there are no room avalaible in the buffer, |
| 6585 | * just close the connection. |
| 6586 | */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6587 | ci_putblk(res, error_500, strlen(error_500)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6588 | if (!(strm->flags & SF_ERR_MASK)) |
| 6589 | strm->flags |= SF_ERR_RESOURCE; |
| 6590 | si_shutw(si); |
| 6591 | si_shutr(si); |
| 6592 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 6593 | } |
| 6594 | |
| 6595 | static void hlua_applet_http_release(struct appctx *ctx) |
| 6596 | { |
Willy Tarreau | bd7fc95 | 2017-07-24 17:35:27 +0200 | [diff] [blame] | 6597 | task_delete(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6598 | task_free(ctx->ctx.hlua_apphttp.task); |
| 6599 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6600 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6601 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6602 | } |
| 6603 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6604 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
| 6605 | * succes case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6606 | * |
| 6607 | * This function can fail with an abort() due to an Lua critical error. |
| 6608 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 6609 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6610 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6611 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 6612 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6613 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6614 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6615 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6616 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6617 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6618 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6619 | if (!rule->arg.hlua_rule) { |
| 6620 | memprintf(err, "out of memory error"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 6621 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6622 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6623 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6624 | /* Memory for arguments. */ |
| 6625 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *)); |
| 6626 | if (!rule->arg.hlua_rule->args) { |
| 6627 | memprintf(err, "out of memory error"); |
| 6628 | return ACT_RET_PRS_ERR; |
| 6629 | } |
| 6630 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6631 | /* Reference the Lua function and store the reference. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6632 | rule->arg.hlua_rule->fcn = *fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6633 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6634 | /* Expect some arguments */ |
| 6635 | for (i = 0; i < fcn->nargs; i++) { |
| 6636 | if (*args[i+1] == '\0') { |
| 6637 | memprintf(err, "expect %d arguments", fcn->nargs); |
| 6638 | return ACT_RET_PRS_ERR; |
| 6639 | } |
| 6640 | rule->arg.hlua_rule->args[i] = strdup(args[i + 1]); |
| 6641 | if (!rule->arg.hlua_rule->args[i]) { |
| 6642 | memprintf(err, "out of memory error"); |
| 6643 | return ACT_RET_PRS_ERR; |
| 6644 | } |
| 6645 | (*cur_arg)++; |
| 6646 | } |
| 6647 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6648 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 6649 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6650 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 6651 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6652 | } |
| 6653 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6654 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 6655 | struct act_rule *rule, char **err) |
| 6656 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6657 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6658 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 6659 | /* HTTP applets are forbidden in tcp-request rules. |
| 6660 | * HTTP applet request requires everything initilized by |
| 6661 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
| 6662 | * The applet will be immediately initilized, but its before |
| 6663 | * the call of this analyzer. |
| 6664 | */ |
| 6665 | if (rule->from != ACT_F_HTTP_REQ) { |
| 6666 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 6667 | return ACT_RET_PRS_ERR; |
| 6668 | } |
| 6669 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6670 | /* Memory for the rule. */ |
| 6671 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 6672 | if (!rule->arg.hlua_rule) { |
| 6673 | memprintf(err, "out of memory error"); |
| 6674 | return ACT_RET_PRS_ERR; |
| 6675 | } |
| 6676 | |
| 6677 | /* Reference the Lua function and store the reference. */ |
| 6678 | rule->arg.hlua_rule->fcn = *fcn; |
| 6679 | |
| 6680 | /* TODO: later accept arguments. */ |
| 6681 | rule->arg.hlua_rule->args = NULL; |
| 6682 | |
| 6683 | /* Add applet pointer in the rule. */ |
| 6684 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 6685 | rule->applet.name = fcn->name; |
| 6686 | rule->applet.init = hlua_applet_http_init; |
| 6687 | rule->applet.fct = hlua_applet_http_fct; |
| 6688 | rule->applet.release = hlua_applet_http_release; |
| 6689 | rule->applet.timeout = hlua_timeout_applet; |
| 6690 | |
| 6691 | return ACT_RET_PRS_OK; |
| 6692 | } |
| 6693 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6694 | /* This function is an LUA binding used for registering |
| 6695 | * "sample-conv" functions. It expects a converter name used |
| 6696 | * in the haproxy configuration file, and an LUA function. |
| 6697 | */ |
| 6698 | __LJMP static int hlua_register_action(lua_State *L) |
| 6699 | { |
| 6700 | struct action_kw_list *akl; |
| 6701 | const char *name; |
| 6702 | int ref; |
| 6703 | int len; |
| 6704 | struct hlua_function *fcn; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6705 | int nargs; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6706 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6707 | /* Initialise the number of expected arguments at 0. */ |
| 6708 | nargs = 0; |
| 6709 | |
| 6710 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 6711 | 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] | 6712 | |
| 6713 | /* First argument : converter name. */ |
| 6714 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6715 | |
| 6716 | /* Second argument : environment. */ |
| 6717 | if (lua_type(L, 2) != LUA_TTABLE) |
| 6718 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 6719 | |
| 6720 | /* Third argument : lua function. */ |
| 6721 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 6722 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6723 | /* Fouth argument : number of mandatories arguments expected on the configuration line. */ |
| 6724 | if (lua_gettop(L) >= 4) |
| 6725 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 6726 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6727 | /* browse the second argulent as an array. */ |
| 6728 | lua_pushnil(L); |
| 6729 | while (lua_next(L, 2) != 0) { |
| 6730 | if (lua_type(L, -1) != LUA_TSTRING) |
| 6731 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 6732 | |
| 6733 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 6734 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6735 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6736 | if (!akl) |
| 6737 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6738 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6739 | if (!fcn) |
| 6740 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6741 | |
| 6742 | /* Fill fcn. */ |
| 6743 | fcn->name = strdup(name); |
| 6744 | if (!fcn->name) |
| 6745 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6746 | fcn->function_ref = ref; |
| 6747 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6748 | /* Set the expected number od arguments. */ |
| 6749 | fcn->nargs = nargs; |
| 6750 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6751 | /* List head */ |
| 6752 | akl->list.n = akl->list.p = NULL; |
| 6753 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6754 | /* action keyword. */ |
| 6755 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6756 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6757 | if (!akl->kw[0].kw) |
| 6758 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6759 | |
| 6760 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 6761 | |
| 6762 | akl->kw[0].match_pfx = 0; |
| 6763 | akl->kw[0].private = fcn; |
| 6764 | akl->kw[0].parse = action_register_lua; |
| 6765 | |
| 6766 | /* select the action registering point. */ |
| 6767 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 6768 | tcp_req_cont_keywords_register(akl); |
| 6769 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 6770 | tcp_res_cont_keywords_register(akl); |
| 6771 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 6772 | http_req_keywords_register(akl); |
| 6773 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 6774 | http_res_keywords_register(akl); |
| 6775 | else |
| 6776 | WILL_LJMP(luaL_error(L, "lua action environment '%s' is unknown. " |
| 6777 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 6778 | "are expected.", lua_tostring(L, -1))); |
| 6779 | |
| 6780 | /* pop the environment string. */ |
| 6781 | lua_pop(L, 1); |
| 6782 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6783 | return ACT_RET_PRS_OK; |
| 6784 | } |
| 6785 | |
| 6786 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 6787 | struct act_rule *rule, char **err) |
| 6788 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6789 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6790 | |
| 6791 | /* Memory for the rule. */ |
| 6792 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 6793 | if (!rule->arg.hlua_rule) { |
| 6794 | memprintf(err, "out of memory error"); |
| 6795 | return ACT_RET_PRS_ERR; |
| 6796 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6797 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6798 | /* Reference the Lua function and store the reference. */ |
| 6799 | rule->arg.hlua_rule->fcn = *fcn; |
| 6800 | |
| 6801 | /* TODO: later accept arguments. */ |
| 6802 | rule->arg.hlua_rule->args = NULL; |
| 6803 | |
| 6804 | /* Add applet pointer in the rule. */ |
| 6805 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 6806 | rule->applet.name = fcn->name; |
| 6807 | rule->applet.init = hlua_applet_tcp_init; |
| 6808 | rule->applet.fct = hlua_applet_tcp_fct; |
| 6809 | rule->applet.release = hlua_applet_tcp_release; |
| 6810 | rule->applet.timeout = hlua_timeout_applet; |
| 6811 | |
| 6812 | return 0; |
| 6813 | } |
| 6814 | |
| 6815 | /* This function is an LUA binding used for registering |
| 6816 | * "sample-conv" functions. It expects a converter name used |
| 6817 | * in the haproxy configuration file, and an LUA function. |
| 6818 | */ |
| 6819 | __LJMP static int hlua_register_service(lua_State *L) |
| 6820 | { |
| 6821 | struct action_kw_list *akl; |
| 6822 | const char *name; |
| 6823 | const char *env; |
| 6824 | int ref; |
| 6825 | int len; |
| 6826 | struct hlua_function *fcn; |
| 6827 | |
| 6828 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 6829 | |
| 6830 | /* First argument : converter name. */ |
| 6831 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6832 | |
| 6833 | /* Second argument : environment. */ |
| 6834 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6835 | |
| 6836 | /* Third argument : lua function. */ |
| 6837 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 6838 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6839 | /* Allocate and fill the sample fetch keyword struct. */ |
| 6840 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 6841 | if (!akl) |
| 6842 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6843 | fcn = calloc(1, sizeof(*fcn)); |
| 6844 | if (!fcn) |
| 6845 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6846 | |
| 6847 | /* Fill fcn. */ |
| 6848 | len = strlen("<lua.>") + strlen(name) + 1; |
| 6849 | fcn->name = calloc(1, len); |
| 6850 | if (!fcn->name) |
| 6851 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6852 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
| 6853 | fcn->function_ref = ref; |
| 6854 | |
| 6855 | /* List head */ |
| 6856 | akl->list.n = akl->list.p = NULL; |
| 6857 | |
| 6858 | /* converter keyword. */ |
| 6859 | len = strlen("lua.") + strlen(name) + 1; |
| 6860 | akl->kw[0].kw = calloc(1, len); |
| 6861 | if (!akl->kw[0].kw) |
| 6862 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6863 | |
| 6864 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 6865 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 6866 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6867 | if (strcmp(env, "tcp") == 0) |
| 6868 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6869 | else if (strcmp(env, "http") == 0) |
| 6870 | akl->kw[0].parse = action_register_service_http; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6871 | else |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6872 | WILL_LJMP(luaL_error(L, "lua service environment '%s' is unknown. " |
| 6873 | "'tcp' or 'http' are expected.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6874 | |
| 6875 | akl->kw[0].match_pfx = 0; |
| 6876 | akl->kw[0].private = fcn; |
| 6877 | |
| 6878 | /* End of array. */ |
| 6879 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 6880 | |
| 6881 | /* Register this new converter */ |
| 6882 | service_keywords_register(akl); |
| 6883 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6884 | return 0; |
| 6885 | } |
| 6886 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6887 | /* This function initialises Lua cli handler. It copies the |
| 6888 | * arguments in the Lua stack and create channel IO objects. |
| 6889 | */ |
| 6890 | static int hlua_cli_parse_fct(char **args, struct appctx *appctx, void *private) |
| 6891 | { |
| 6892 | struct hlua *hlua; |
| 6893 | struct hlua_function *fcn; |
| 6894 | int i; |
| 6895 | const char *error; |
| 6896 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6897 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6898 | appctx->ctx.hlua_cli.fcn = private; |
| 6899 | |
| 6900 | hlua = pool_alloc2(pool2_hlua); |
| 6901 | if (!hlua) { |
| 6902 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 6903 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6904 | } |
| 6905 | HLUA_INIT(hlua); |
| 6906 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6907 | |
| 6908 | /* Create task used by signal to wakeup applets. |
| 6909 | * We use the same wakeup fonction than the Lua applet_tcp and |
| 6910 | * applet_http. It is absolutely compatible. |
| 6911 | */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 6912 | appctx->ctx.hlua_cli.task = task_new(tid_bit); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6913 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 6914 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 6915 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6916 | } |
| 6917 | appctx->ctx.hlua_cli.task->nice = 0; |
| 6918 | appctx->ctx.hlua_cli.task->context = appctx; |
| 6919 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 6920 | |
| 6921 | /* Initialises the Lua context */ |
| 6922 | if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task)) { |
| 6923 | 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] | 6924 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6925 | } |
| 6926 | |
| 6927 | /* The following Lua calls can fail. */ |
| 6928 | if (!SET_SAFE_LJMP(hlua->T)) { |
| 6929 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6930 | error = lua_tostring(hlua->T, -1); |
| 6931 | else |
| 6932 | error = "critical error"; |
| 6933 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 6934 | goto error; |
| 6935 | } |
| 6936 | |
| 6937 | /* Check stack available size. */ |
| 6938 | if (!lua_checkstack(hlua->T, 2)) { |
| 6939 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 6940 | goto error; |
| 6941 | } |
| 6942 | |
| 6943 | /* Restore the function in the stack. */ |
| 6944 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
| 6945 | |
| 6946 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 6947 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6948 | */ |
| 6949 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 6950 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 6951 | goto error; |
| 6952 | } |
| 6953 | hlua->nargs = 1; |
| 6954 | |
| 6955 | /* push keywords in the stack. */ |
| 6956 | for (i = 0; *args[i]; i++) { |
| 6957 | /* Check stack available size. */ |
| 6958 | if (!lua_checkstack(hlua->T, 1)) { |
| 6959 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 6960 | goto error; |
| 6961 | } |
| 6962 | lua_pushstring(hlua->T, args[i]); |
| 6963 | hlua->nargs++; |
| 6964 | } |
| 6965 | |
| 6966 | /* We must initialize the execution timeouts. */ |
| 6967 | hlua->max_time = hlua_timeout_session; |
| 6968 | |
| 6969 | /* At this point the execution is safe. */ |
| 6970 | RESET_SAFE_LJMP(hlua->T); |
| 6971 | |
| 6972 | /* It's ok */ |
| 6973 | return 0; |
| 6974 | |
| 6975 | /* It's not ok. */ |
| 6976 | error: |
| 6977 | RESET_SAFE_LJMP(hlua->T); |
| 6978 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 6979 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6980 | return 1; |
| 6981 | } |
| 6982 | |
| 6983 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 6984 | { |
| 6985 | struct hlua *hlua; |
| 6986 | struct stream_interface *si; |
| 6987 | struct hlua_function *fcn; |
| 6988 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6989 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6990 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 6991 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6992 | |
| 6993 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 6994 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 6995 | return 1; |
| 6996 | |
| 6997 | /* Execute the function. */ |
| 6998 | switch (hlua_ctx_resume(hlua, 1)) { |
| 6999 | |
| 7000 | /* finished. */ |
| 7001 | case HLUA_E_OK: |
| 7002 | return 1; |
| 7003 | |
| 7004 | /* yield. */ |
| 7005 | case HLUA_E_AGAIN: |
| 7006 | /* We want write. */ |
| 7007 | if (HLUA_IS_WAKERESWR(hlua)) |
| 7008 | si_applet_cant_put(si); |
| 7009 | /* Set the timeout. */ |
| 7010 | if (hlua->wake_time != TICK_ETERNITY) |
| 7011 | task_schedule(hlua->task, hlua->wake_time); |
| 7012 | return 0; |
| 7013 | |
| 7014 | /* finished with error. */ |
| 7015 | case HLUA_E_ERRMSG: |
| 7016 | /* Display log. */ |
| 7017 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 7018 | fcn->name, lua_tostring(hlua->T, -1)); |
| 7019 | lua_pop(hlua->T, 1); |
| 7020 | return 1; |
| 7021 | |
| 7022 | case HLUA_E_ERR: |
| 7023 | /* Display log. */ |
| 7024 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 7025 | fcn->name); |
| 7026 | return 1; |
| 7027 | |
| 7028 | default: |
| 7029 | return 1; |
| 7030 | } |
| 7031 | |
| 7032 | return 1; |
| 7033 | } |
| 7034 | |
| 7035 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 7036 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7037 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7038 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7039 | } |
| 7040 | |
| 7041 | /* This function is an LUA binding used for registering |
| 7042 | * new keywords in the cli. It expects a list of keywords |
| 7043 | * which are the "path". It is limited to 5 keywords. A |
| 7044 | * description of the command, a function to be executed |
| 7045 | * for the parsing and a function for io handlers. |
| 7046 | */ |
| 7047 | __LJMP static int hlua_register_cli(lua_State *L) |
| 7048 | { |
| 7049 | struct cli_kw_list *cli_kws; |
| 7050 | const char *message; |
| 7051 | int ref_io; |
| 7052 | int len; |
| 7053 | struct hlua_function *fcn; |
| 7054 | int index; |
| 7055 | int i; |
| 7056 | |
| 7057 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 7058 | |
| 7059 | /* First argument : an array of maximum 5 keywords. */ |
| 7060 | if (!lua_istable(L, 1)) |
| 7061 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 7062 | |
| 7063 | /* Second argument : string with contextual message. */ |
| 7064 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7065 | |
| 7066 | /* Third and fourth argument : lua function. */ |
| 7067 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7068 | |
| 7069 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7070 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
| 7071 | if (!cli_kws) |
| 7072 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 7073 | fcn = calloc(1, sizeof(*fcn)); |
| 7074 | if (!fcn) |
| 7075 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 7076 | |
| 7077 | /* Fill path. */ |
| 7078 | index = 0; |
| 7079 | lua_pushnil(L); |
| 7080 | while(lua_next(L, 1) != 0) { |
| 7081 | if (index >= 5) |
| 7082 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 7083 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7084 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 7085 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
| 7086 | if (!cli_kws->kw[0].str_kw[index]) |
| 7087 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 7088 | index++; |
| 7089 | lua_pop(L, 1); |
| 7090 | } |
| 7091 | |
| 7092 | /* Copy help message. */ |
| 7093 | cli_kws->kw[0].usage = strdup(message); |
| 7094 | if (!cli_kws->kw[0].usage) |
| 7095 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 7096 | |
| 7097 | /* Fill fcn io handler. */ |
| 7098 | len = strlen("<lua.cli>") + 1; |
| 7099 | for (i = 0; i < index; i++) |
| 7100 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 7101 | fcn->name = calloc(1, len); |
| 7102 | if (!fcn->name) |
| 7103 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 7104 | strncat((char *)fcn->name, "<lua.cli", len); |
| 7105 | for (i = 0; i < index; i++) { |
| 7106 | strncat((char *)fcn->name, ".", len); |
| 7107 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 7108 | } |
| 7109 | strncat((char *)fcn->name, ">", len); |
| 7110 | fcn->function_ref = ref_io; |
| 7111 | |
| 7112 | /* Fill last entries. */ |
| 7113 | cli_kws->kw[0].private = fcn; |
| 7114 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 7115 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 7116 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 7117 | |
| 7118 | /* Register this new converter */ |
| 7119 | cli_register_kw(cli_kws); |
| 7120 | |
| 7121 | return 0; |
| 7122 | } |
| 7123 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7124 | static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx, |
| 7125 | struct proxy *defpx, const char *file, int line, |
| 7126 | char **err, unsigned int *timeout) |
| 7127 | { |
| 7128 | const char *error; |
| 7129 | |
| 7130 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
| 7131 | if (error && *error != '\0') { |
| 7132 | memprintf(err, "%s: invalid timeout", args[0]); |
| 7133 | return -1; |
| 7134 | } |
| 7135 | return 0; |
| 7136 | } |
| 7137 | |
| 7138 | static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx, |
| 7139 | struct proxy *defpx, const char *file, int line, |
| 7140 | char **err) |
| 7141 | { |
| 7142 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7143 | file, line, err, &hlua_timeout_session); |
| 7144 | } |
| 7145 | |
| 7146 | static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx, |
| 7147 | struct proxy *defpx, const char *file, int line, |
| 7148 | char **err) |
| 7149 | { |
| 7150 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7151 | file, line, err, &hlua_timeout_task); |
| 7152 | } |
| 7153 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7154 | static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx, |
| 7155 | struct proxy *defpx, const char *file, int line, |
| 7156 | char **err) |
| 7157 | { |
| 7158 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7159 | file, line, err, &hlua_timeout_applet); |
| 7160 | } |
| 7161 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 7162 | static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx, |
| 7163 | struct proxy *defpx, const char *file, int line, |
| 7164 | char **err) |
| 7165 | { |
| 7166 | char *error; |
| 7167 | |
| 7168 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 7169 | if (*error != '\0') { |
| 7170 | memprintf(err, "%s: invalid number", args[0]); |
| 7171 | return -1; |
| 7172 | } |
| 7173 | return 0; |
| 7174 | } |
| 7175 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7176 | static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx, |
| 7177 | struct proxy *defpx, const char *file, int line, |
| 7178 | char **err) |
| 7179 | { |
| 7180 | char *error; |
| 7181 | |
| 7182 | if (*(args[1]) == 0) { |
| 7183 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 7184 | return -1; |
| 7185 | } |
| 7186 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 7187 | if (*error != '\0') { |
| 7188 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 7189 | return -1; |
| 7190 | } |
| 7191 | return 0; |
| 7192 | } |
| 7193 | |
| 7194 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7195 | /* This function is called by the main configuration key "lua-load". It loads and |
| 7196 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 7197 | * the main lua entry point. |
| 7198 | * |
| 7199 | * This funtion runs with the HAProxy keywords API. It returns -1 if an error is |
| 7200 | * occured, otherwise it returns 0. |
| 7201 | * |
| 7202 | * In some error case, LUA set an error message in top of the stack. This function |
| 7203 | * 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] | 7204 | * |
| 7205 | * This function can fail with an abort() due to an Lua critical error. |
| 7206 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7207 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7208 | */ |
| 7209 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
| 7210 | struct proxy *defpx, const char *file, int line, |
| 7211 | char **err) |
| 7212 | { |
| 7213 | int error; |
| 7214 | |
| 7215 | /* Just load and compile the file. */ |
| 7216 | error = luaL_loadfile(gL.T, args[1]); |
| 7217 | if (error) { |
| 7218 | memprintf(err, "error in lua file '%s': %s", args[1], lua_tostring(gL.T, -1)); |
| 7219 | lua_pop(gL.T, 1); |
| 7220 | return -1; |
| 7221 | } |
| 7222 | |
| 7223 | /* If no syntax error where detected, execute the code. */ |
| 7224 | error = lua_pcall(gL.T, 0, LUA_MULTRET, 0); |
| 7225 | switch (error) { |
| 7226 | case LUA_OK: |
| 7227 | break; |
| 7228 | case LUA_ERRRUN: |
| 7229 | memprintf(err, "lua runtime error: %s\n", lua_tostring(gL.T, -1)); |
| 7230 | lua_pop(gL.T, 1); |
| 7231 | return -1; |
| 7232 | case LUA_ERRMEM: |
| 7233 | memprintf(err, "lua out of memory error\n"); |
| 7234 | return -1; |
| 7235 | case LUA_ERRERR: |
| 7236 | memprintf(err, "lua message handler error: %s\n", lua_tostring(gL.T, -1)); |
| 7237 | lua_pop(gL.T, 1); |
| 7238 | return -1; |
| 7239 | case LUA_ERRGCMM: |
| 7240 | memprintf(err, "lua garbage collector error: %s\n", lua_tostring(gL.T, -1)); |
| 7241 | lua_pop(gL.T, 1); |
| 7242 | return -1; |
| 7243 | default: |
| 7244 | memprintf(err, "lua unknonwn error: %s\n", lua_tostring(gL.T, -1)); |
| 7245 | lua_pop(gL.T, 1); |
| 7246 | return -1; |
| 7247 | } |
| 7248 | |
| 7249 | return 0; |
| 7250 | } |
| 7251 | |
| 7252 | /* configuration keywords declaration */ |
| 7253 | static struct cfg_kw_list cfg_kws = {{ },{ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7254 | { CFG_GLOBAL, "lua-load", hlua_load }, |
| 7255 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 7256 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 7257 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 7258 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7259 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7260 | { 0, NULL, NULL }, |
| 7261 | }}; |
| 7262 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7263 | /* This function can fail with an abort() due to an Lua critical error. |
| 7264 | * We are in the initialisation process of HAProxy, this abort() is |
| 7265 | * tolerated. |
| 7266 | */ |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7267 | int hlua_post_init() |
| 7268 | { |
| 7269 | struct hlua_init_function *init; |
| 7270 | const char *msg; |
| 7271 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7272 | const char *error; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7273 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 7274 | /* Call post initialisation function in safe environement. */ |
| 7275 | if (!SET_SAFE_LJMP(gL.T)) { |
| 7276 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 7277 | error = lua_tostring(gL.T, -1); |
| 7278 | else |
| 7279 | error = "critical error"; |
| 7280 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 7281 | exit(1); |
| 7282 | } |
| 7283 | hlua_fcn_post_init(gL.T); |
| 7284 | RESET_SAFE_LJMP(gL.T); |
| 7285 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7286 | list_for_each_entry(init, &hlua_init_functions, l) { |
| 7287 | lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref); |
| 7288 | ret = hlua_ctx_resume(&gL, 0); |
| 7289 | switch (ret) { |
| 7290 | case HLUA_E_OK: |
| 7291 | lua_pop(gL.T, -1); |
| 7292 | return 1; |
| 7293 | case HLUA_E_AGAIN: |
| 7294 | Alert("lua init: yield not allowed.\n"); |
| 7295 | return 0; |
| 7296 | case HLUA_E_ERRMSG: |
| 7297 | msg = lua_tostring(gL.T, -1); |
| 7298 | Alert("lua init: %s.\n", msg); |
| 7299 | return 0; |
| 7300 | case HLUA_E_ERR: |
| 7301 | default: |
| 7302 | Alert("lua init: unknown runtime error.\n"); |
| 7303 | return 0; |
| 7304 | } |
| 7305 | } |
| 7306 | return 1; |
| 7307 | } |
| 7308 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7309 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 7310 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 7311 | * is the previously allocated size or the kind of object in case of a new |
| 7312 | * allocation. <nsize> is the requested new size. |
| 7313 | */ |
| 7314 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 7315 | { |
| 7316 | struct hlua_mem_allocator *zone = ud; |
| 7317 | |
| 7318 | if (nsize == 0) { |
| 7319 | /* it's a free */ |
| 7320 | if (ptr) |
| 7321 | zone->allocated -= osize; |
| 7322 | free(ptr); |
| 7323 | return NULL; |
| 7324 | } |
| 7325 | |
| 7326 | if (!ptr) { |
| 7327 | /* it's a new allocation */ |
| 7328 | if (zone->limit && zone->allocated + nsize > zone->limit) |
| 7329 | return NULL; |
| 7330 | |
| 7331 | ptr = malloc(nsize); |
| 7332 | if (ptr) |
| 7333 | zone->allocated += nsize; |
| 7334 | return ptr; |
| 7335 | } |
| 7336 | |
| 7337 | /* it's a realloc */ |
| 7338 | if (zone->limit && zone->allocated + nsize - osize > zone->limit) |
| 7339 | return NULL; |
| 7340 | |
| 7341 | ptr = realloc(ptr, nsize); |
| 7342 | if (ptr) |
| 7343 | zone->allocated += nsize - osize; |
| 7344 | return ptr; |
| 7345 | } |
| 7346 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7347 | /* Ithis function can fail with an abort() due to an Lua critical error. |
| 7348 | * We are in the initialisation process of HAProxy, this abort() is |
| 7349 | * tolerated. |
| 7350 | */ |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 7351 | void hlua_init(void) |
| 7352 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7353 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7354 | int idx; |
| 7355 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7356 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7357 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7358 | const char *error_msg; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7359 | #ifdef USE_OPENSSL |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7360 | struct srv_kw *kw; |
| 7361 | int tmp_error; |
| 7362 | char *error; |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 7363 | char *args[] = { /* SSL client configuration. */ |
| 7364 | "ssl", |
| 7365 | "verify", |
| 7366 | "none", |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 7367 | NULL |
| 7368 | }; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7369 | #endif |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7370 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 7371 | HA_SPIN_INIT(&hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 7372 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7373 | /* Initialise struct hlua and com signals pool */ |
| 7374 | pool2_hlua = create_pool("hlua", sizeof(struct hlua), MEM_F_SHARED); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 7375 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7376 | /* Register configuration keywords. */ |
| 7377 | cfg_register_keywords(&cfg_kws); |
| 7378 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 7379 | /* Init main lua stack. */ |
| 7380 | gL.Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 7381 | gL.flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 7382 | LIST_INIT(&gL.com); |
Willy Tarreau | 42ef75f | 2017-04-12 21:40:29 +0200 | [diff] [blame] | 7383 | gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 7384 | hlua_sethlua(&gL); |
| 7385 | gL.Tref = LUA_REFNIL; |
| 7386 | gL.task = NULL; |
| 7387 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7388 | /* From this point, until the end of the initialisation fucntion, |
| 7389 | * the Lua function can fail with an abort. We are in the initialisation |
| 7390 | * process of HAProxy, this abort() is tolerated. |
| 7391 | */ |
| 7392 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 7393 | /* Initialise lua. */ |
| 7394 | luaL_openlibs(gL.T); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7395 | |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 7396 | /* Set safe environment for the initialisation. */ |
| 7397 | if (!SET_SAFE_LJMP(gL.T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7398 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 7399 | error_msg = lua_tostring(gL.T, -1); |
| 7400 | else |
| 7401 | error_msg = "critical error"; |
| 7402 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 7403 | exit(1); |
| 7404 | } |
| 7405 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7406 | /* |
| 7407 | * |
| 7408 | * Create "core" object. |
| 7409 | * |
| 7410 | */ |
| 7411 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 7412 | /* This table entry is the object "core" base. */ |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7413 | lua_newtable(gL.T); |
| 7414 | |
| 7415 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7416 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7417 | hlua_class_const_int(gL.T, log_levels[i], i); |
| 7418 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7419 | /* Register special functions. */ |
| 7420 | hlua_class_function(gL.T, "register_init", hlua_register_init); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 7421 | hlua_class_function(gL.T, "register_task", hlua_register_task); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7422 | hlua_class_function(gL.T, "register_fetches", hlua_register_fetches); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 7423 | hlua_class_function(gL.T, "register_converters", hlua_register_converters); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7424 | hlua_class_function(gL.T, "register_action", hlua_register_action); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7425 | hlua_class_function(gL.T, "register_service", hlua_register_service); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7426 | hlua_class_function(gL.T, "register_cli", hlua_register_cli); |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 7427 | hlua_class_function(gL.T, "yield", hlua_yield); |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7428 | hlua_class_function(gL.T, "set_nice", hlua_set_nice); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 7429 | hlua_class_function(gL.T, "sleep", hlua_sleep); |
| 7430 | hlua_class_function(gL.T, "msleep", hlua_msleep); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 7431 | hlua_class_function(gL.T, "add_acl", hlua_add_acl); |
| 7432 | hlua_class_function(gL.T, "del_acl", hlua_del_acl); |
| 7433 | hlua_class_function(gL.T, "set_map", hlua_set_map); |
| 7434 | hlua_class_function(gL.T, "del_map", hlua_del_map); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7435 | hlua_class_function(gL.T, "tcp", hlua_socket_new); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 7436 | hlua_class_function(gL.T, "log", hlua_log); |
| 7437 | hlua_class_function(gL.T, "Debug", hlua_log_debug); |
| 7438 | hlua_class_function(gL.T, "Info", hlua_log_info); |
| 7439 | hlua_class_function(gL.T, "Warning", hlua_log_warning); |
| 7440 | hlua_class_function(gL.T, "Alert", hlua_log_alert); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 7441 | hlua_class_function(gL.T, "done", hlua_done); |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 7442 | hlua_fcn_reg_core_fcn(gL.T); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7443 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7444 | lua_setglobal(gL.T, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7445 | |
| 7446 | /* |
| 7447 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7448 | * Register class Map |
| 7449 | * |
| 7450 | */ |
| 7451 | |
| 7452 | /* This table entry is the object "Map" base. */ |
| 7453 | lua_newtable(gL.T); |
| 7454 | |
| 7455 | /* register pattern types. */ |
| 7456 | for (i=0; i<PAT_MATCH_NUM; i++) |
| 7457 | hlua_class_const_int(gL.T, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 7458 | for (i=0; i<PAT_MATCH_NUM; i++) { |
| 7459 | snprintf(trash.str, trash.size, "_%s", pat_match_names[i]); |
| 7460 | hlua_class_const_int(gL.T, trash.str, i); |
| 7461 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7462 | |
| 7463 | /* register constructor. */ |
| 7464 | hlua_class_function(gL.T, "new", hlua_map_new); |
| 7465 | |
| 7466 | /* Create and fill the metatable. */ |
| 7467 | lua_newtable(gL.T); |
| 7468 | |
| 7469 | /* Create and fille the __index entry. */ |
| 7470 | lua_pushstring(gL.T, "__index"); |
| 7471 | lua_newtable(gL.T); |
| 7472 | |
| 7473 | /* Register . */ |
| 7474 | hlua_class_function(gL.T, "lookup", hlua_map_lookup); |
| 7475 | hlua_class_function(gL.T, "slookup", hlua_map_slookup); |
| 7476 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7477 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7478 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7479 | /* Register previous table in the registry with reference and named entry. |
| 7480 | * The function hlua_register_metatable() pops the stack, so we |
| 7481 | * previously create a copy of the table. |
| 7482 | */ |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7483 | 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] | 7484 | class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7485 | |
| 7486 | /* Assign the metatable to the mai Map object. */ |
| 7487 | lua_setmetatable(gL.T, -2); |
| 7488 | |
| 7489 | /* Set a name to the table. */ |
| 7490 | lua_setglobal(gL.T, "Map"); |
| 7491 | |
| 7492 | /* |
| 7493 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7494 | * Register class Channel |
| 7495 | * |
| 7496 | */ |
| 7497 | |
| 7498 | /* Create and fill the metatable. */ |
| 7499 | lua_newtable(gL.T); |
| 7500 | |
| 7501 | /* Create and fille the __index entry. */ |
| 7502 | lua_pushstring(gL.T, "__index"); |
| 7503 | lua_newtable(gL.T); |
| 7504 | |
| 7505 | /* Register . */ |
| 7506 | hlua_class_function(gL.T, "get", hlua_channel_get); |
| 7507 | hlua_class_function(gL.T, "dup", hlua_channel_dup); |
| 7508 | hlua_class_function(gL.T, "getline", hlua_channel_getline); |
| 7509 | hlua_class_function(gL.T, "set", hlua_channel_set); |
| 7510 | hlua_class_function(gL.T, "append", hlua_channel_append); |
| 7511 | hlua_class_function(gL.T, "send", hlua_channel_send); |
| 7512 | hlua_class_function(gL.T, "forward", hlua_channel_forward); |
| 7513 | hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len); |
| 7514 | 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] | 7515 | hlua_class_function(gL.T, "is_full", hlua_channel_is_full); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7516 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7517 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7518 | |
| 7519 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7520 | class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7521 | |
| 7522 | /* |
| 7523 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7524 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7525 | * |
| 7526 | */ |
| 7527 | |
| 7528 | /* Create and fill the metatable. */ |
| 7529 | lua_newtable(gL.T); |
| 7530 | |
| 7531 | /* Create and fille the __index entry. */ |
| 7532 | lua_pushstring(gL.T, "__index"); |
| 7533 | lua_newtable(gL.T); |
| 7534 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7535 | /* Browse existing fetches and create the associated |
| 7536 | * object method. |
| 7537 | */ |
| 7538 | sf = NULL; |
| 7539 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
| 7540 | |
| 7541 | /* Dont register the keywork if the arguments check function are |
| 7542 | * not safe during the runtime. |
| 7543 | */ |
| 7544 | if ((sf->val_args != NULL) && |
| 7545 | (sf->val_args != val_payload_lv) && |
| 7546 | (sf->val_args != val_hdr)) |
| 7547 | continue; |
| 7548 | |
| 7549 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 7550 | * by an underscore. |
| 7551 | */ |
| 7552 | strncpy(trash.str, sf->kw, trash.size); |
| 7553 | trash.str[trash.size - 1] = '\0'; |
| 7554 | for (p = trash.str; *p; p++) |
| 7555 | if (*p == '.' || *p == '-' || *p == '+') |
| 7556 | *p = '_'; |
| 7557 | |
| 7558 | /* Register the function. */ |
| 7559 | lua_pushstring(gL.T, trash.str); |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 7560 | lua_pushlightuserdata(gL.T, sf); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7561 | lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7562 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7563 | } |
| 7564 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7565 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7566 | |
| 7567 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7568 | class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7569 | |
| 7570 | /* |
| 7571 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7572 | * Register class Converters |
| 7573 | * |
| 7574 | */ |
| 7575 | |
| 7576 | /* Create and fill the metatable. */ |
| 7577 | lua_newtable(gL.T); |
| 7578 | |
| 7579 | /* Create and fill the __index entry. */ |
| 7580 | lua_pushstring(gL.T, "__index"); |
| 7581 | lua_newtable(gL.T); |
| 7582 | |
| 7583 | /* Browse existing converters and create the associated |
| 7584 | * object method. |
| 7585 | */ |
| 7586 | sc = NULL; |
| 7587 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
| 7588 | /* Dont register the keywork if the arguments check function are |
| 7589 | * not safe during the runtime. |
| 7590 | */ |
| 7591 | if (sc->val_args != NULL) |
| 7592 | continue; |
| 7593 | |
| 7594 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 7595 | * by an underscore. |
| 7596 | */ |
| 7597 | strncpy(trash.str, sc->kw, trash.size); |
| 7598 | trash.str[trash.size - 1] = '\0'; |
| 7599 | for (p = trash.str; *p; p++) |
| 7600 | if (*p == '.' || *p == '-' || *p == '+') |
| 7601 | *p = '_'; |
| 7602 | |
| 7603 | /* Register the function. */ |
| 7604 | lua_pushstring(gL.T, trash.str); |
| 7605 | lua_pushlightuserdata(gL.T, sc); |
| 7606 | lua_pushcclosure(gL.T, hlua_run_sample_conv, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7607 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7608 | } |
| 7609 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7610 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7611 | |
| 7612 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7613 | class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7614 | |
| 7615 | /* |
| 7616 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7617 | * Register class HTTP |
| 7618 | * |
| 7619 | */ |
| 7620 | |
| 7621 | /* Create and fill the metatable. */ |
| 7622 | lua_newtable(gL.T); |
| 7623 | |
| 7624 | /* Create and fille the __index entry. */ |
| 7625 | lua_pushstring(gL.T, "__index"); |
| 7626 | lua_newtable(gL.T); |
| 7627 | |
| 7628 | /* Register Lua functions. */ |
| 7629 | hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers); |
| 7630 | hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr); |
| 7631 | hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr); |
| 7632 | hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val); |
| 7633 | hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr); |
| 7634 | hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr); |
| 7635 | hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth); |
| 7636 | hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path); |
| 7637 | hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query); |
| 7638 | hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri); |
| 7639 | |
| 7640 | hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers); |
| 7641 | hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr); |
| 7642 | hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr); |
| 7643 | hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val); |
| 7644 | hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr); |
| 7645 | 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] | 7646 | 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] | 7647 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7648 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7649 | |
| 7650 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7651 | class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7652 | |
| 7653 | /* |
| 7654 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7655 | * Register class AppletTCP |
| 7656 | * |
| 7657 | */ |
| 7658 | |
| 7659 | /* Create and fill the metatable. */ |
| 7660 | lua_newtable(gL.T); |
| 7661 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7662 | /* Create and fille the __index entry. */ |
| 7663 | lua_pushstring(gL.T, "__index"); |
| 7664 | lua_newtable(gL.T); |
| 7665 | |
| 7666 | /* Register Lua functions. */ |
Thierry FOURNIER / OZON.IO | 3e1d791 | 2016-12-12 12:29:34 +0100 | [diff] [blame] | 7667 | hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline); |
| 7668 | hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv); |
| 7669 | hlua_class_function(gL.T, "send", hlua_applet_tcp_send); |
| 7670 | hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv); |
| 7671 | 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] | 7672 | hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var); |
| 7673 | hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var); |
| 7674 | hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7675 | |
| 7676 | lua_settable(gL.T, -3); |
| 7677 | |
| 7678 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7679 | class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7680 | |
| 7681 | /* |
| 7682 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7683 | * Register class AppletHTTP |
| 7684 | * |
| 7685 | */ |
| 7686 | |
| 7687 | /* Create and fill the metatable. */ |
| 7688 | lua_newtable(gL.T); |
| 7689 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7690 | /* Create and fille the __index entry. */ |
| 7691 | lua_pushstring(gL.T, "__index"); |
| 7692 | lua_newtable(gL.T); |
| 7693 | |
| 7694 | /* Register Lua functions. */ |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 7695 | hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv); |
| 7696 | 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] | 7697 | hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var); |
| 7698 | hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var); |
| 7699 | hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7700 | hlua_class_function(gL.T, "getline", hlua_applet_http_getline); |
| 7701 | hlua_class_function(gL.T, "receive", hlua_applet_http_recv); |
| 7702 | hlua_class_function(gL.T, "send", hlua_applet_http_send); |
| 7703 | hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader); |
| 7704 | hlua_class_function(gL.T, "set_status", hlua_applet_http_status); |
| 7705 | hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response); |
| 7706 | |
| 7707 | lua_settable(gL.T, -3); |
| 7708 | |
| 7709 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7710 | class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7711 | |
| 7712 | /* |
| 7713 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7714 | * Register class TXN |
| 7715 | * |
| 7716 | */ |
| 7717 | |
| 7718 | /* Create and fill the metatable. */ |
| 7719 | lua_newtable(gL.T); |
| 7720 | |
| 7721 | /* Create and fille the __index entry. */ |
| 7722 | lua_pushstring(gL.T, "__index"); |
| 7723 | lua_newtable(gL.T); |
| 7724 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7725 | /* Register Lua functions. */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7726 | hlua_class_function(gL.T, "set_priv", hlua_set_priv); |
| 7727 | hlua_class_function(gL.T, "get_priv", hlua_get_priv); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7728 | hlua_class_function(gL.T, "set_var", hlua_set_var); |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 7729 | hlua_class_function(gL.T, "unset_var", hlua_unset_var); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7730 | hlua_class_function(gL.T, "get_var", hlua_get_var); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 7731 | hlua_class_function(gL.T, "done", hlua_txn_done); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7732 | hlua_class_function(gL.T, "set_loglevel",hlua_txn_set_loglevel); |
| 7733 | hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos); |
| 7734 | hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 7735 | hlua_class_function(gL.T, "deflog", hlua_txn_deflog); |
| 7736 | hlua_class_function(gL.T, "log", hlua_txn_log); |
| 7737 | hlua_class_function(gL.T, "Debug", hlua_txn_log_debug); |
| 7738 | hlua_class_function(gL.T, "Info", hlua_txn_log_info); |
| 7739 | hlua_class_function(gL.T, "Warning", hlua_txn_log_warning); |
| 7740 | hlua_class_function(gL.T, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7741 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7742 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7743 | |
| 7744 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7745 | class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7746 | |
| 7747 | /* |
| 7748 | * |
| 7749 | * Register class Socket |
| 7750 | * |
| 7751 | */ |
| 7752 | |
| 7753 | /* Create and fill the metatable. */ |
| 7754 | lua_newtable(gL.T); |
| 7755 | |
| 7756 | /* Create and fille the __index entry. */ |
| 7757 | lua_pushstring(gL.T, "__index"); |
| 7758 | lua_newtable(gL.T); |
| 7759 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 7760 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7761 | hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 7762 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7763 | hlua_class_function(gL.T, "connect", hlua_socket_connect); |
| 7764 | hlua_class_function(gL.T, "send", hlua_socket_send); |
| 7765 | hlua_class_function(gL.T, "receive", hlua_socket_receive); |
| 7766 | hlua_class_function(gL.T, "close", hlua_socket_close); |
| 7767 | hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername); |
| 7768 | hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname); |
| 7769 | hlua_class_function(gL.T, "setoption", hlua_socket_setoption); |
| 7770 | hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout); |
| 7771 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7772 | 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] | 7773 | |
| 7774 | /* Register the garbage collector entry. */ |
| 7775 | lua_pushstring(gL.T, "__gc"); |
| 7776 | lua_pushcclosure(gL.T, hlua_socket_gc, 0); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7777 | 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] | 7778 | |
| 7779 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7780 | class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7781 | |
| 7782 | /* Proxy and server configuration initialisation. */ |
| 7783 | memset(&socket_proxy, 0, sizeof(socket_proxy)); |
| 7784 | init_new_proxy(&socket_proxy); |
| 7785 | socket_proxy.parent = NULL; |
| 7786 | socket_proxy.last_change = now.tv_sec; |
| 7787 | socket_proxy.id = "LUA-SOCKET"; |
| 7788 | socket_proxy.cap = PR_CAP_FE | PR_CAP_BE; |
| 7789 | socket_proxy.maxconn = 0; |
| 7790 | socket_proxy.accept = NULL; |
| 7791 | socket_proxy.options2 |= PR_O2_INDEPSTR; |
| 7792 | socket_proxy.srv = NULL; |
| 7793 | socket_proxy.conn_retries = 0; |
| 7794 | socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */ |
| 7795 | |
| 7796 | /* Init TCP server: unchanged parameters */ |
| 7797 | memset(&socket_tcp, 0, sizeof(socket_tcp)); |
| 7798 | socket_tcp.next = NULL; |
| 7799 | socket_tcp.proxy = &socket_proxy; |
| 7800 | socket_tcp.obj_type = OBJ_TYPE_SERVER; |
| 7801 | LIST_INIT(&socket_tcp.actconns); |
| 7802 | LIST_INIT(&socket_tcp.pendconns); |
Christopher Faulet | 40a007c | 2017-07-03 15:41:01 +0200 | [diff] [blame] | 7803 | socket_tcp.priv_conns = NULL; |
| 7804 | socket_tcp.idle_conns = NULL; |
| 7805 | socket_tcp.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 7806 | socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7807 | socket_tcp.last_change = 0; |
| 7808 | socket_tcp.id = "LUA-TCP-CONN"; |
| 7809 | socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 7810 | socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 7811 | socket_tcp.pp_opts = 0; /* Remove proxy protocol. */ |
| 7812 | |
| 7813 | /* XXX: Copy default parameter from default server, |
| 7814 | * but the default server is not initialized. |
| 7815 | */ |
| 7816 | socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue; |
| 7817 | socket_tcp.minconn = socket_proxy.defsrv.minconn; |
| 7818 | socket_tcp.maxconn = socket_proxy.defsrv.maxconn; |
| 7819 | socket_tcp.slowstart = socket_proxy.defsrv.slowstart; |
| 7820 | socket_tcp.onerror = socket_proxy.defsrv.onerror; |
| 7821 | socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 7822 | socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 7823 | socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 7824 | socket_tcp.uweight = socket_proxy.defsrv.iweight; |
| 7825 | socket_tcp.iweight = socket_proxy.defsrv.iweight; |
| 7826 | |
| 7827 | socket_tcp.check.status = HCHK_STATUS_INI; |
| 7828 | socket_tcp.check.rise = socket_proxy.defsrv.check.rise; |
| 7829 | socket_tcp.check.fall = socket_proxy.defsrv.check.fall; |
| 7830 | socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */ |
| 7831 | socket_tcp.check.server = &socket_tcp; |
| 7832 | |
| 7833 | socket_tcp.agent.status = HCHK_STATUS_INI; |
| 7834 | socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise; |
| 7835 | socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall; |
| 7836 | socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */ |
| 7837 | socket_tcp.agent.server = &socket_tcp; |
| 7838 | |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 7839 | socket_tcp.xprt = xprt_get(XPRT_RAW); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7840 | |
| 7841 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7842 | /* Init TCP server: unchanged parameters */ |
| 7843 | memset(&socket_ssl, 0, sizeof(socket_ssl)); |
| 7844 | socket_ssl.next = NULL; |
| 7845 | socket_ssl.proxy = &socket_proxy; |
| 7846 | socket_ssl.obj_type = OBJ_TYPE_SERVER; |
| 7847 | LIST_INIT(&socket_ssl.actconns); |
| 7848 | LIST_INIT(&socket_ssl.pendconns); |
Christopher Faulet | 40a007c | 2017-07-03 15:41:01 +0200 | [diff] [blame] | 7849 | socket_tcp.priv_conns = NULL; |
| 7850 | socket_tcp.idle_conns = NULL; |
| 7851 | socket_tcp.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 7852 | socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7853 | socket_ssl.last_change = 0; |
| 7854 | socket_ssl.id = "LUA-SSL-CONN"; |
| 7855 | socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 7856 | socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 7857 | socket_ssl.pp_opts = 0; /* Remove proxy protocol. */ |
| 7858 | |
| 7859 | /* XXX: Copy default parameter from default server, |
| 7860 | * but the default server is not initialized. |
| 7861 | */ |
| 7862 | socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue; |
| 7863 | socket_ssl.minconn = socket_proxy.defsrv.minconn; |
| 7864 | socket_ssl.maxconn = socket_proxy.defsrv.maxconn; |
| 7865 | socket_ssl.slowstart = socket_proxy.defsrv.slowstart; |
| 7866 | socket_ssl.onerror = socket_proxy.defsrv.onerror; |
| 7867 | socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 7868 | socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 7869 | socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 7870 | socket_ssl.uweight = socket_proxy.defsrv.iweight; |
| 7871 | socket_ssl.iweight = socket_proxy.defsrv.iweight; |
| 7872 | |
| 7873 | socket_ssl.check.status = HCHK_STATUS_INI; |
| 7874 | socket_ssl.check.rise = socket_proxy.defsrv.check.rise; |
| 7875 | socket_ssl.check.fall = socket_proxy.defsrv.check.fall; |
| 7876 | socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */ |
| 7877 | socket_ssl.check.server = &socket_ssl; |
| 7878 | |
| 7879 | socket_ssl.agent.status = HCHK_STATUS_INI; |
| 7880 | socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise; |
| 7881 | socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall; |
| 7882 | socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */ |
| 7883 | socket_ssl.agent.server = &socket_ssl; |
| 7884 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 7885 | socket_ssl.use_ssl = 1; |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 7886 | socket_ssl.xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7887 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 7888 | for (idx = 0; args[idx] != NULL; idx++) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7889 | if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */ |
| 7890 | /* |
| 7891 | * |
| 7892 | * If the keyword is not known, we can search in the registered |
| 7893 | * server keywords. This is usefull to configure special SSL |
| 7894 | * features like client certificates and ssl_verify. |
| 7895 | * |
| 7896 | */ |
| 7897 | tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error); |
| 7898 | if (tmp_error != 0) { |
| 7899 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 7900 | abort(); /* This must be never arrives because the command line |
| 7901 | not editable by the user. */ |
| 7902 | } |
| 7903 | idx += kw->skip; |
| 7904 | } |
| 7905 | } |
| 7906 | |
| 7907 | /* Initialize SSL server. */ |
Willy Tarreau | 17d4538 | 2016-12-22 21:16:08 +0100 | [diff] [blame] | 7908 | if (socket_ssl.xprt->prepare_srv) |
| 7909 | socket_ssl.xprt->prepare_srv(&socket_ssl); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7910 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 7911 | |
| 7912 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 7913 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 7914 | |
| 7915 | __attribute__((constructor)) |
| 7916 | static void __hlua_init(void) |
| 7917 | { |
| 7918 | char *ptr = NULL; |
| 7919 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 7920 | hap_register_build_opts(ptr, 1); |
| 7921 | } |